]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
added tree reader class to handle reading trees. Reworked the tree map to tree class...
[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         m = MothurOut::getInstance();
16         groupFileName = filename;
17         m->openInputFile(filename, fileHandle);
18 }
19
20 /************************************************************/
21  TreeMap::~TreeMap(){}
22 /************************************************************/
23 int TreeMap::readMap(string gf) {
24     
25     groupFileName = gf;
26         m->openInputFile(gf, fileHandle);
27     
28     string seqName, seqGroup;
29     int error = 0;
30     
31     while(fileHandle){
32         fileHandle >> seqName;       m->gobble(fileHandle);     //read from first column
33         fileHandle >> seqGroup;                 //read from second column
34         
35         if (m->control_pressed) {  fileHandle.close();  return 1; }
36         
37         setNamesOfGroups(seqGroup);
38         
39         map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
40         if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
41         else {
42             namesOfSeqs.push_back(seqName);
43             treemap[seqName].groupname = seqGroup;      //store data in map
44             
45             it2 = seqsPerGroup.find(seqGroup);
46             if (it2 == seqsPerGroup.end()) { //if it's a new group
47                 seqsPerGroup[seqGroup] = 1;
48             }else {//it's a group we already have
49                 seqsPerGroup[seqGroup]++;
50             }                           
51         }
52         
53         m->gobble(fileHandle);
54     }
55     fileHandle.close();
56     
57     return error;
58 }
59
60 /************************************************************/
61 int TreeMap::readMap() {
62                 string seqName, seqGroup;
63                 int error = 0;
64                 
65                 while(fileHandle){
66                         fileHandle >> seqName;           m->gobble(fileHandle); //read from first column
67                         fileHandle >> seqGroup;                 //read from second column
68                         
69                         if (m->control_pressed) {  fileHandle.close();  return 1; }
70                         
71                         setNamesOfGroups(seqGroup);
72                                         
73                         map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
74                         if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
75                         else {
76                                 namesOfSeqs.push_back(seqName);
77                                 treemap[seqName].groupname = seqGroup;  //store data in map
78                                 
79                                 it2 = seqsPerGroup.find(seqGroup);
80                                 if (it2 == seqsPerGroup.end()) { //if it's a new group
81                                         seqsPerGroup[seqGroup] = 1;
82                                 }else {//it's a group we already have
83                                         seqsPerGroup[seqGroup]++;
84                                 }                               
85                         }
86                         
87                         m->gobble(fileHandle);
88                 }
89                 fileHandle.close();
90         
91
92                 return error;
93 }
94 /************************************************************/
95 void TreeMap::addSeq(string seqName, string seqGroup) {
96         
97                 namesOfSeqs.push_back(seqName);
98                 setNamesOfGroups(seqGroup);
99                                         
100                 treemap[seqName].groupname = seqGroup;  //store data in map
101                         
102                 it2 = seqsPerGroup.find(seqGroup);
103                 if (it2 == seqsPerGroup.end()) { //if it's a new group
104                         seqsPerGroup[seqGroup] = 1;
105                 }else {//it's a group we already have
106                         seqsPerGroup[seqGroup]++;
107                 }
108 }
109 /************************************************************/
110 void TreeMap::removeSeq(string seqName) {
111         
112         //erase name from namesOfSeqs
113         for (int i = 0; i < namesOfSeqs.size(); i++) {
114                 if (namesOfSeqs[i] == seqName)  {
115                         namesOfSeqs.erase(namesOfSeqs.begin()+i);
116                         break;
117                 }
118         }
119         
120         //decrement sequences in this group
121         string group = treemap[seqName].groupname;
122         seqsPerGroup[group]--;
123         
124         //remove seq from treemap
125         it = treemap.find(seqName);
126         treemap.erase(it);
127 }
128 /************************************************************/
129
130 int TreeMap::getNumGroups() {
131                         
132         return namesOfGroups.size();    
133                 
134 }
135 /************************************************************/
136
137 int TreeMap::getNumSeqs() {
138                         
139         return namesOfSeqs.size();      
140                 
141 }
142
143 /************************************************************/
144
145 string TreeMap::getGroup(string sequenceName) {
146                         
147         it = treemap.find(sequenceName);
148         if (it != treemap.end()) { //sequence name was in group file
149                 return it->second.groupname;    
150         }else {
151                 return "not found";
152         }
153                 
154 }
155 /************************************************************/
156 void TreeMap::setIndex(string seq, int index) {
157         it = treemap.find(seq);
158         if (it != treemap.end()) { //sequence name was in group file
159                 treemap[seq].vectorIndex = index;       
160         }else {
161                 treemap[seq].vectorIndex = index;
162                 treemap[seq].groupname = "not found";
163         }
164 }
165 /************************************************************/
166 int TreeMap::getIndex(string seq) {
167         
168         it = treemap.find(seq);
169         // if it is a valid sequence name then return index
170         if (it != treemap.end()) { return treemap[seq].vectorIndex; }
171         // if not return error code
172         else { return -1; }
173         
174 }
175 /************************************************************/
176
177 void TreeMap::setNamesOfGroups(string seqGroup) {
178                         int i, count;
179                         count = 0;
180                         for (i=0; i<namesOfGroups.size(); i++) {
181                                 if (namesOfGroups[i] != seqGroup) {
182                                         count++; //you have not found this group
183                                 }else {
184                                         break; //you already have it
185                                 }
186                         }
187                         if (count == namesOfGroups.size()) {
188                                 namesOfGroups.push_back(seqGroup); //new group
189                         }
190 }
191 /************************************************************/
192 bool TreeMap::isValidGroup(string groupname) {
193         try {
194                 for (int i = 0; i < namesOfGroups.size(); i++) {
195                         if (groupname == namesOfGroups[i]) { return true; }
196                 }
197                 
198                 return false;
199         }
200         catch(exception& e) {
201                 m->errorOut(e, "TreeMap", "isValidGroup");
202                 exit(1);
203         }
204 }
205 /***********************************************************************/
206
207 void TreeMap::print(ostream& output){
208         try {
209                 
210                 for(it = treemap.begin(); it != treemap.end(); it++){
211                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
212                 }
213         }
214         catch(exception& e) {
215                 m->errorOut(e, "TreeMap", "print");
216                 exit(1);
217         }
218 }
219
220 /************************************************************/
221 void TreeMap::makeSim(vector<string> ThisnamesOfGroups) {
222         try {
223                 //set names of groups
224                 namesOfGroups = ThisnamesOfGroups;
225                 
226                 //set names of seqs to names of groups
227                 namesOfSeqs = ThisnamesOfGroups;
228                 
229                 // make map where key and value are both the group name since that what the tree.shared command wants
230                 for (int i = 0; i < namesOfGroups.size(); i++) {
231                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
232                         seqsPerGroup[namesOfGroups[i]] = 1;
233                 }
234                 
235                 numGroups = namesOfGroups.size();
236                 
237         }
238         catch(exception& e) {
239                 m->errorOut(e, "TreeMap", "makeSim");
240                 exit(1);
241         }
242 }
243 /************************************************************/
244 void TreeMap::makeSim(ListVector* list) {
245         try {
246                 //set names of groups
247                 namesOfGroups.clear();
248                 for(int i = 0; i < list->size(); i++) {
249                         namesOfGroups.push_back(list->get(i));
250                 }
251                 
252                 //set names of seqs to names of groups
253                 namesOfSeqs = namesOfGroups;
254                 
255                 // make map where key and value are both the group name since that what the tree.shared command wants
256                 for (int i = 0; i < namesOfGroups.size(); i++) {
257                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
258                         seqsPerGroup[namesOfGroups[i]] = 1;
259                 }
260                 
261                 numGroups = namesOfGroups.size();
262                 
263         }
264         catch(exception& e) {
265                 m->errorOut(e, "TreeMap", "makeSim");
266                 exit(1);
267         }
268 }
269 /************************************************************/
270 int TreeMap::getCopy(TreeMap& copy){
271         try {
272          
273         namesOfGroups = copy.getNamesOfGroups();
274                 numGroups = copy.getNumGroups();
275         namesOfSeqs = copy.namesOfSeqs;
276         seqsPerGroup = copy.seqsPerGroup;
277         treemap = copy.treemap;
278         
279         return 0;
280         }
281         catch(exception& e) {
282                 m->errorOut(e, "TreeMap", "getCopy");
283                 exit(1);
284         }
285 }
286 /************************************************************/
287 vector<string> TreeMap::getNamesSeqs(){
288         try {
289         
290                 vector<string> names;
291                 
292         for(it = treemap.begin(); it != treemap.end(); it++){
293             names.push_back(it->first);
294                 }
295                 
296                 return names;
297         }
298         catch(exception& e) {
299                 m->errorOut(e, "TreeMap", "getNamesSeqs");
300                 exit(1);
301         }
302 }
303 /************************************************************/
304 vector<string> TreeMap::getNamesSeqs(vector<string> picked){
305         try {
306                 
307                 vector<string> names;
308                 
309                 for(it = treemap.begin(); it != treemap.end(); it++){
310                         //if you are belong to one the the groups in the picked vector add you
311                         if (m->inUsersGroups(it->second.groupname, picked)) {
312                                 names.push_back(it->first);
313                         }
314                 }
315                 
316                 return names;
317         }
318         catch(exception& e) {
319                 m->errorOut(e, "TreeMap", "getNamesSeqs");
320                 exit(1);
321         }
322 }
323
324 /************************************************************/
325