]> git.donarmstrong.com Git - mothur.git/blob - nameassignment.cpp
Initial revision
[mothur.git] / nameassignment.cpp
1 using namespace std;
2
3 #include <string>
4 #include <map>
5 #include <exception>
6
7 #include "nameassignment.hpp"
8
9 //**********************************************************************************************************************
10
11 NameAssignment::NameAssignment(string nameMapFile){
12         
13         openInputFile(nameMapFile, fileHandle);
14         
15 }
16
17 //**********************************************************************************************************************
18
19 void NameAssignment::readMap(int colA, int colB){
20         try{
21                 string firstCol, secondCol, skip;
22         //      int index = 0;
23         
24                 int skipNCols = colB-colA-1;
25         
26                 map<string, string> data;
27         
28                 while(fileHandle){
29                         fileHandle >> firstCol;                         //read from first column
30                 
31                         for(int i=0;i<skipNCols;i++){           //allows for anticipated file format
32                                 fileHandle >> skip;
33                         }
34                 
35                         fileHandle >> secondCol;                        //read from second column
36                 
37                         data[firstCol] = secondCol;                     //store data in map
38                 
39                         gobble(fileHandle);
40                 }
41                 fileHandle.close();
42         
43                 int rowIndex = 0;
44                 map<string, string>::iterator it = data.begin();
45                 for(it;it!=data.end();it++){
46                         list.push_back(it->second);             //adds data's value to list
47                         (*this)[it->first] = rowIndex;
48                         rowIndex++;
49                 }
50         }
51         catch(exception& e) {
52                 cout << "Standard Error: " << e.what() << " has occurred in the NameAssignment class Function readMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53                 exit(1);
54         }
55         catch(...) {
56                 cout << "An unknown error has occurred in the NameAssignment class function readMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57                 exit(1);
58         }
59 }
60
61 //**********************************************************************************************************************
62
63 ListVector NameAssignment::getListVector(void){
64
65         return list;
66         
67 }
68
69 //**********************************************************************************************************************
70
71 void NameAssignment::print(void){
72         try {
73                 map<string,int>::iterator it = (*this).begin();
74                 for(it;it!=(*this).end();it++){
75                         cout << it->first << '\t' << it->second << endl;  //prints out keys and values of the map this.
76                 }
77         }
78         catch(exception& e) {
79                 cout << "Standard Error: " << e.what() << " has occurred in the NameAssignment class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80                 exit(1);
81         }
82         catch(...) {
83                 cout << "An unknown error has occurred in the NameAssignment class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }
86 }
87
88 //**********************************************************************************************************************
89
90 int NameAssignment::get(string key){
91         
92         return  (*this)[key];   
93
94 }
95
96 //**********************************************************************************************************************
97