]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
bugs fixes while testing for 1.5 release
[mothur.git] / treemap.cpp
1 /*
2  *  treemap.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/26/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "treemap.h"
11
12 /************************************************************/
13
14  TreeMap::TreeMap(string filename) {
15         groupFileName = filename;
16         openInputFile(filename, fileHandle);
17 }
18
19 /************************************************************/
20  TreeMap::~TreeMap(){}
21
22 /************************************************************/
23 void TreeMap::readMap() {
24                 string seqName, seqGroup;
25         
26                 while(fileHandle){
27                         fileHandle >> seqName;                  //read from first column
28                         fileHandle >> seqGroup;                 //read from second column
29                         
30                         namesOfSeqs.push_back(seqName);
31                         setNamesOfGroups(seqGroup);
32                                                 
33                         treemap[seqName].groupname = seqGroup;  //store data in map
34                         
35                         it2 = seqsPerGroup.find(seqGroup);
36                         if (it2 == seqsPerGroup.end()) { //if it's a new group
37                                 seqsPerGroup[seqGroup] = 1;
38                         }else {//it's a group we already have
39                                 seqsPerGroup[seqGroup]++;
40                         }
41
42                         gobble(fileHandle);
43                 }
44                 fileHandle.close();
45 }
46 /************************************************************/
47 void TreeMap::removeSeq(string seqName) {
48         
49         //erase name from namesOfSeqs
50         for (int i = 0; i < namesOfSeqs.size(); i++) {
51                 if (namesOfSeqs[i] == seqName)  {
52                         namesOfSeqs.erase (namesOfSeqs.begin()+i);
53                         break;
54                 }
55         }
56         
57         //decrement sequences in this group
58         string group = treemap[seqName].groupname;
59         seqsPerGroup[group]--;
60         
61         //remove seq from treemap
62         it = treemap.find(seqName);
63         treemap.erase(it);
64         
65
66 }
67 /************************************************************/
68
69 int TreeMap::getNumGroups() {
70                         
71         return namesOfGroups.size();    
72                 
73 }
74 /************************************************************/
75
76 int TreeMap::getNumSeqs() {
77                         
78         return namesOfSeqs.size();      
79                 
80 }
81
82 /************************************************************/
83
84 string TreeMap::getGroup(string sequenceName) {
85                         
86         it = treemap.find(sequenceName);
87         if (it != treemap.end()) { //sequence name was in group file
88                 return it->second.groupname;    
89         }else {
90                 return "not found";
91         }
92                 
93 }
94 /************************************************************/
95 void TreeMap::setIndex(string seq, int index) {
96         it = treemap.find(seq);
97         if (it != treemap.end()) { //sequence name was in group file
98                 treemap[seq].vectorIndex = index;       
99         }else {
100                 treemap[seq].vectorIndex = index;
101                 treemap[seq].groupname = "not found";
102         }
103 }
104 /************************************************************/
105 int TreeMap::getIndex(string seq) {
106         
107         it = treemap.find(seq);
108         // if it is a valid sequence name then return index
109         if (it != treemap.end()) { return treemap[seq].vectorIndex; }
110         // if not return error code
111         else { return -1; }
112         
113 }
114 /************************************************************/
115
116 void TreeMap::setNamesOfGroups(string seqGroup) {
117                         int i, count;
118                         count = 0;
119                         for (i=0; i<namesOfGroups.size(); i++) {
120                                 if (namesOfGroups[i] != seqGroup) {
121                                         count++; //you have not found this group
122                                 }else {
123                                         break; //you already have it
124                                 }
125                         }
126                         if (count == namesOfGroups.size()) {
127                                 namesOfGroups.push_back(seqGroup); //new group
128                         }
129 }
130 /************************************************************/
131 bool TreeMap::isValidGroup(string groupname) {
132         try {
133                 for (int i = 0; i < namesOfGroups.size(); i++) {
134                         if (groupname == namesOfGroups[i]) { return true; }
135                 }
136                 
137                 return false;
138         }
139         catch(exception& e) {
140                 errorOut(e, "TreeMap", "isValidGroup");
141                 exit(1);
142         }
143 }
144 /***********************************************************************/
145
146 void TreeMap::print(ostream& output){
147         try {
148                 
149                 for(it = treemap.begin(); it != treemap.end(); it++){
150                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
151                 }
152         }
153         catch(exception& e) {
154                 errorOut(e, "TreeMap", "print");
155                 exit(1);
156         }
157 }
158
159 /************************************************************/
160 void TreeMap::makeSim(GroupMap* groupmap) {
161         try {
162                 //set names of groups
163                 namesOfGroups = groupmap->namesOfGroups;
164                 
165                 //set names of seqs to names of groups
166                 namesOfSeqs = groupmap->namesOfGroups;
167                 
168                 // make map where key and value are both the group name since that what the tree.shared command wants
169                 for (int i = 0; i < namesOfGroups.size(); i++) {
170                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
171                         seqsPerGroup[namesOfGroups[i]] = 1;
172                 }
173                 
174                 numGroups = namesOfGroups.size();
175                 
176         }
177         catch(exception& e) {
178                 errorOut(e, "TreeMap", "makeSim");
179                 exit(1);
180         }
181 }
182 /************************************************************/
183 void TreeMap::makeSim(ListVector* list) {
184         try {
185                 //set names of groups
186                 namesOfGroups.clear();
187                 for(int i = 0; i < list->size(); i++) {
188                         namesOfGroups.push_back(list->get(i));
189                 }
190                 
191                 //set names of seqs to names of groups
192                 namesOfSeqs = namesOfGroups;
193                 
194                 // make map where key and value are both the group name since that what the tree.shared command wants
195                 for (int i = 0; i < namesOfGroups.size(); i++) {
196                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
197                         seqsPerGroup[namesOfGroups[i]] = 1;
198                 }
199                 
200                 numGroups = namesOfGroups.size();
201                 
202         }
203         catch(exception& e) {
204                 errorOut(e, "TreeMap", "makeSim");
205                 exit(1);
206         }
207 }
208
209 /************************************************************/
210