]> git.donarmstrong.com Git - mothur.git/blob - nameassignment.cpp
fixes while testing 1.33.0
[mothur.git] / nameassignment.cpp
1
2
3 #include "nameassignment.hpp"
4
5 //**********************************************************************************************************************
6
7 NameAssignment::NameAssignment(string nameMapFile){
8         m = MothurOut::getInstance();
9         m->openInputFile(nameMapFile, fileHandle);
10         
11 }
12 //**********************************************************************************************************************
13 NameAssignment::NameAssignment(){ m = MothurOut::getInstance(); }
14 //**********************************************************************************************************************
15
16 void NameAssignment::readMap(){
17         try{
18                 string firstCol, secondCol, skip;
19         //      int index = 0;
20         
21                 
22                 map<string, int>::iterator itData;
23                 int rowIndex = 0;
24                 
25                 while(fileHandle){
26                         fileHandle >> firstCol; m->gobble(fileHandle);                  //read from first column
27                         fileHandle >> secondCol;                        //read from second column
28                                                 
29                         itData = (*this).find(firstCol);
30                         if (itData == (*this).end()) {
31                         
32                                 (*this)[firstCol] = rowIndex++;
33                                 list.push_back(secondCol);              //adds data's value to list
34                                 reverse[rowIndex] = firstCol;
35                                 
36                         }else{  m->mothurOut(firstCol + " is already in namesfile. I will use first definition."); m->mothurOutEndLine();  }
37                         
38                         m->gobble(fileHandle);
39                 }
40                 fileHandle.close();
41         
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "NameAssignment", "readMap");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 void NameAssignment::push_back(string name) {
50         try{
51         
52                 int num = (*this).size();
53                 (*this)[name] = num;
54                 reverse[num] = name;
55                 
56                 list.push_back(name);
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "NameAssignment", "push_back");
60                 exit(1);
61         }
62 }
63
64 //**********************************************************************************************************************
65
66 ListVector NameAssignment::getListVector(void){
67
68         return list;
69         
70 }
71
72 //**********************************************************************************************************************
73
74 void NameAssignment::print(ostream& out){
75         try {
76                 map<string,int>::iterator it;
77 //cout << (*this).size() << endl;
78                 for(it = (*this).begin(); it!=(*this).end(); it++){
79                         out << it->first << '\t' <<  it->second << endl;  //prints out keys and values of the map this.
80                         //out << it->first << '\t' <<  it->first << endl;
81                 }
82         }
83         catch(exception& e) {
84                 m->errorOut(e, "NameAssignment", "print");
85                 exit(1);
86         }
87 }
88
89 //**********************************************************************************************************************
90
91 int NameAssignment::get(string key){
92         try {
93                 map<string, int>::iterator itGet = (*this).find(key);
94                 
95                 //if you can't find it
96                 if (itGet == (*this).end()) { return -1; }
97                 
98                 return  (*this)[key];   
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "NameAssignment", "get");
102                 exit(1);
103         }
104 }
105 //**********************************************************************************************************************
106
107 string NameAssignment::get(int key){
108         try {
109         
110                 map<int, string>::iterator itGet = reverse.find(key);
111         
112                 if (itGet == reverse.end()) { return "not found"; }
113         
114                 return  reverse[key];   
115         
116         }
117         catch(exception& e) {
118                 m->errorOut(e, "NameAssignment", "get");
119                 exit(1);
120         }
121 }
122 //**********************************************************************************************************************
123