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