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