]> git.donarmstrong.com Git - mothur.git/blob - nameassignment.cpp
fixed cluster.split with average method
[mothur.git] / nameassignment.cpp
1
2
3 #include "nameassignment.hpp"
4
5 //**********************************************************************************************************************
6
7 NameAssignment::NameAssignment(string nameMapFile){
8         m = MothurOut::getInstance();
9         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                         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                 }
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "NameAssignment", "print");
83                 exit(1);
84         }
85 }
86
87 //**********************************************************************************************************************
88
89 int NameAssignment::get(string key){
90         try {
91                 map<string, int>::iterator itGet = (*this).find(key);
92                 
93                 //if you can't find it
94                 if (itGet == (*this).end()) { return -1; }
95                 
96                 return  (*this)[key];   
97         }
98         catch(exception& e) {
99                 m->errorOut(e, "NameAssignment", "get");
100                 exit(1);
101         }
102 }
103 //**********************************************************************************************************************
104
105 string NameAssignment::get(int key){
106         try {
107         
108                 map<int, string>::iterator itGet = reverse.find(key);
109         
110                 if (itGet == reverse.end()) { return "not found"; }
111         
112                 return  reverse[key];   
113         
114         }
115         catch(exception& e) {
116                 m->errorOut(e, "NameAssignment", "get");
117                 exit(1);
118         }
119 }
120 //**********************************************************************************************************************
121