]> git.donarmstrong.com Git - mothur.git/blob - consensuscommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / consensuscommand.cpp
1 /*
2  *  consensuscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/29/09.
6  *  Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
7  *
8  */
9
10 #include "consensuscommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> ConcensusCommand::getValidParameters(){  
14         try {
15                 vector<string> myArray; 
16                 return myArray;
17         }
18         catch(exception& e) {
19                 m->errorOut(e, "ConcensusCommand", "getValidParameters");
20                 exit(1);
21         }
22 }
23 //**********************************************************************************************************************
24 vector<string> ConcensusCommand::getRequiredParameters(){       
25         try {
26                 vector<string> myArray; 
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "ConcensusCommand", "getRequiredParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 vector<string> ConcensusCommand::getRequiredFiles(){    
36         try {
37                 string AlignArray[] =  {"tree","group"};
38                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
39                 return myArray;
40         }
41         catch(exception& e) {
42                 m->errorOut(e, "ConcensusCommand", "getRequiredFiles");
43                 exit(1);
44         }
45 }
46 //**********************************************************************************************************************
47 ConcensusCommand::ConcensusCommand(){   
48         try {
49                 abort = true; calledHelp = true; 
50                 vector<string> tempOutNames;
51                 outputTypes["tree"] = tempOutNames;
52                 outputTypes["nodepairs"] = tempOutNames;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "ConcensusCommand", "ConcensusCommand");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60
61 ConcensusCommand::ConcensusCommand(string fileroot)  {
62         try {
63                 globaldata = GlobalData::getInstance();
64                 abort = false; calledHelp = false;   
65                 
66                 //initialize outputTypes
67                 vector<string> tempOutNames;
68                 outputTypes["tree"] = tempOutNames;
69                 outputTypes["nodepairs"] = tempOutNames;
70                 
71                 filename = fileroot;
72                 
73                 t = globaldata->gTree;
74         
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "ConcensusCommand", "ConcensusCommand");
78                 exit(1);
79         }
80 }
81
82 //**********************************************************************************************************************
83
84 void ConcensusCommand::help(){
85         try {
86                 m->mothurOut("The consensus command can only be executed after a successful read.tree command.\n");
87                 m->mothurOut("The consensus command has no parameters.\n");
88                 m->mothurOut("The consensus command should be in the following format: consensus().\n");
89                 m->mothurOut("The consensus command output two files: .consensus.tre and .consensuspairs.\n");
90                 m->mothurOut("The .consensus.tre file contains the consensus tree of the trees in your input file.\n");
91                 m->mothurOut("The branch lengths are the percentage of trees in your input file that had the given pair.\n");
92                 m->mothurOut("The .consensuspairs file contains a list of the internal nodes in your tree.  For each node, the pair that was used in the consensus tree \n");
93                 m->mothurOut("is reported with its percentage, as well as the other pairs that were seen for that node but not used and their percentages.\n\n");               
94         }
95         catch(exception& e) {
96                 m->errorOut(e, "ConcensusCommand", "help");
97                 exit(1);
98         }
99 }
100
101 //**********************************************************************************************************************
102
103 ConcensusCommand::~ConcensusCommand(){}
104
105 //**********************************************************************************************************************
106
107 int ConcensusCommand::execute(){
108         try {
109                 
110                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
111                 else {  
112                         numNodes = t[0]->getNumNodes();
113                         numLeaves = t[0]->getNumLeaves();
114                 }
115                 
116                 //get the possible pairings
117                 getSets();      
118                 
119                 if (m->control_pressed) { return 0; }
120                 
121                 //open file for pairing not included in the tree
122                 notIncluded = filename + ".cons.pairs"; outputNames.push_back(notIncluded);  outputTypes["nodepairs"].push_back(notIncluded);
123                 m->openOutputFile(notIncluded, out2);
124                 
125                 consensusTree = new Tree();
126                 
127                 it2 = nodePairs.find(treeSet);
128                 
129                 nodePairsInTree[treeSet] = it2->second; 
130                 
131                 //erase treeset because you are adding it
132                 nodePairs.erase(treeSet);
133                 
134                 //set count to numLeaves;
135                 count = numLeaves;
136                 
137                 buildConcensusTree(treeSet);
138                 
139                 if (m->control_pressed) { delete consensusTree; return 0; }
140                 
141                 consensusTree->assembleTree();
142                 
143                 if (m->control_pressed) { delete consensusTree; return 0; }
144                 
145                 //output species in order
146                 out2 << "Species in Order: " << endl << endl;
147                 for (int i = 0; i < treeSet.size(); i++) {  out2 << i+1 << ".  " << treeSet[i] << endl; }
148                 
149                 //output sets included
150                 out2 << endl << "Sets included in the consensus tree:" << endl << endl;
151                 
152                 if (m->control_pressed) { delete consensusTree; return 0; }
153                 
154                 vector<string> temp;
155                 for (it2 = nodePairsInTree.begin(); it2 != nodePairsInTree.end(); it2++) {
156                 
157                         if (m->control_pressed) { delete consensusTree; return 0; }
158                         
159                         //only output pairs not leaves
160                         if (it2->first.size() > 1) { 
161                                 temp.clear();
162                                 //initialize temp to all "."
163                                 temp.resize(treeSet.size(), ".");
164                                 
165                                 //set the spot in temp that represents it2->first[i] to a "*" 
166                                 for (int i = 0; i < it2->first.size(); i++) {
167                                         //find spot 
168                                         int index = findSpot(it2->first[i]);
169                                         temp[index] = "*";
170                                         //temp[index] = it2->first[i] + "  ";
171                                 }
172                                 
173                                 //output temp
174                                 for (int j = 0; j < temp.size(); j++) { 
175                                         out2 << temp[j];
176                                 }
177                                 out2 << '\t' << it2->second << endl;
178                         }
179                 }
180                 
181                 //output sets not included
182                 out2 << endl << "Sets NOT included in the consensus tree:" << endl << endl;
183                 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
184                 
185                         if (m->control_pressed) { delete consensusTree; return 0; }
186                         
187                         temp.clear();
188                         //initialize temp to all "."
189                         temp.resize(treeSet.size(), ".");
190                                 
191                         //set the spot in temp that represents it2->first[i] to a "*" 
192                         for (int i = 0; i < it2->first.size(); i++) {
193                                 //find spot 
194                                 int index = findSpot(it2->first[i]);
195                                 temp[index] = "*";
196                         }
197                                 
198                         //output temp
199                         for (int j = 0; j < temp.size(); j++) { 
200                                 out2 << temp[j];
201                         }
202                         out2 << '\t' << it2->second << endl;
203                 }
204                 
205                 outputFile = filename + ".cons.tre";  outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
206                 m->openOutputFile(outputFile, out);
207                 
208                 consensusTree->print(out, "boot");
209                 
210                 out.close(); out2.close();
211                 
212                 delete consensusTree; 
213                 
214                 return 0;
215         }
216         catch(exception& e) {
217                 m->errorOut(e, "ConcensusCommand", "execute");
218                 exit(1);
219         }
220 }
221
222 //**********************************************************************************************************************
223 int ConcensusCommand::buildConcensusTree(vector<string> nodeSet) {
224         try {
225                 vector<string> leftChildSet;
226                 vector<string> rightChildSet;
227                 
228                 if (m->control_pressed) { return 1; }
229                 
230                 //if you are at a leaf
231                 if (nodeSet.size() == 1) {
232                         //return the vector index of the leaf you are at
233                         return consensusTree->getIndex(nodeSet[0]);
234                 //terminate recursion
235                 }else if (count == numNodes) { return 0; }
236                 else {
237                         //finds best child pair
238                         leftChildSet = getNextAvailableSet(nodeSet, rightChildSet);
239                         int left = buildConcensusTree(leftChildSet);
240                         int right = buildConcensusTree(rightChildSet);
241                         consensusTree->tree[count].setChildren(left, right);
242                         consensusTree->tree[count].setLabel(nodePairsInTree[nodeSet]); 
243                         consensusTree->tree[left].setParent(count);
244                         consensusTree->tree[right].setParent(count);
245                         count++;
246                         return (count-1);
247                 }
248         
249         }
250         catch(exception& e) {
251                 m->errorOut(e, "ConcensusCommand", "buildConcensusTree");
252                 exit(1);
253         }
254 }
255
256 //**********************************************************************************************************************
257 int ConcensusCommand::getSets() {
258         try {
259                 vector<string> temp;
260                 treeSet.clear();
261                 
262                 //for each tree add the possible pairs you find
263                 for (int i = 0; i < t.size(); i++) {
264                         
265                         //for each non-leaf node get descendant info.
266                         for (int j = numLeaves; j < numNodes; j++) {
267                                 
268                                 if (m->control_pressed) { return 1; }
269                                 
270                                 temp.clear();
271                                 //go through pcounts and pull out descendants
272                                 for (it = t[i]->tree[j].pcount.begin(); it != t[i]->tree[j].pcount.end(); it++) {
273                                         temp.push_back(it->first);
274                                 }
275                                 
276                                 //sort temp
277                                 sort(temp.begin(), temp.end());
278                                 
279                                 it2 = nodePairs.find(temp);
280                                 if (it2 != nodePairs.end()) {
281                                         nodePairs[temp]++;
282                                 }else{
283                                         nodePairs[temp] = 1;
284                                 }
285                         }
286                 }
287                 
288                 
289                 //add each leaf to terminate recursion in consensus
290                 //you want the leaves in there but with insignifigant sightings value so it is added last
291                 //for each leaf node get descendant info.
292                 for (int j = 0; j < numLeaves; j++) {
293                 
294                         if (m->control_pressed) { return 1; }
295                         
296                         //only need the first one since leaves have no descendants but themselves
297                         it = t[0]->tree[j].pcount.begin(); 
298                         temp.clear();  temp.push_back(it->first);
299                         
300                         //fill treeSet
301                         treeSet.push_back(it->first);
302                         
303                         //add leaf to list but with sighting value less then all non leaf pairs 
304                         nodePairs[temp] = 0;
305                 }
306
307                 sort(treeSet.begin(), treeSet.end());
308                 
309                 
310                 map< vector<string>, int> nodePairsCopy = nodePairs;
311                 
312                 
313                 //set initial rating on pairs to sightings + subgroup sightings
314                 while (nodePairsCopy.size() != 0) {
315                         if (m->control_pressed) { return 1; }
316                 
317                         vector<string> smallOne = getSmallest(nodePairsCopy);
318                         
319                         int subgrouprate = getSubgroupRating(smallOne);
320                 
321                         nodePairsInitialRate[smallOne] = nodePairs[smallOne] + subgrouprate;
322                         
323                         nodePairsCopy.erase(smallOne);
324                 }
325                 
326                 return 0;
327         }
328         catch(exception& e) {
329                 m->errorOut(e, "ConcensusCommand", "getSets");
330                 exit(1);
331         }
332 }
333 //**********************************************************************************************************************
334 vector<string> ConcensusCommand::getSmallest(map< vector<string>, int> nodes) {
335         try{
336                 vector<string> smallest = nodes.begin()->first;
337                 int smallsize = smallest.size();
338                 
339                 for(it2 = nodes.begin(); it2 != nodes.end(); it2++) {
340                         if(it2->first.size() < smallsize) { smallsize = it2->first.size();  smallest = it2->first;  }
341                 }
342                 
343                 return smallest;
344         }
345         catch(exception& e) {
346                 m->errorOut(e, "ConcensusCommand", "getSmallest");
347                 exit(1);
348         }
349 }
350
351 //**********************************************************************************************************************
352 vector<string> ConcensusCommand::getNextAvailableSet(vector<string> bigset, vector<string>& rest) {
353         try {
354 //cout << "new call " << endl << endl << endl;
355                 vector<string> largest; largest.clear();
356                 rest.clear();
357                 
358                 //if you are just 2 groups
359                 if (bigset.size() == 2)  {   
360                         rest.push_back(bigset[0]);
361                         largest.push_back(bigset[1]);
362                 }else{
363                         rest = bestSplit[bigset][0];
364                         largest = bestSplit[bigset][1];
365                 }
366                 
367                 
368                 //save for printing out later and for branch lengths
369                 nodePairsInTree[rest] = nodePairs[rest];
370                 
371                 //delete whatever set you return because it is no longer available
372                 nodePairs.erase(rest);
373
374                 //save for printing out later and for branch lengths
375                 nodePairsInTree[largest] = nodePairs[largest];
376                 
377                 //delete whatever set you return because it is no longer available
378                 nodePairs.erase(largest);
379                 
380                 return largest;
381         
382         }
383         catch(exception& e) {
384                 m->errorOut(e, "ConcensusCommand", "getNextAvailableSet");
385                 exit(1);
386         }
387 }
388
389 /**********************************************************************************************************************/
390 int ConcensusCommand::getSubgroupRating(vector<string> group) {
391         try {
392                 map< vector<string>, int>::iterator ittemp;
393                 map< vector< vector<string> > , int >::iterator it3;
394                 int rate = 0;
395                 
396                 // ***********************************************************************************//
397                 //1. this function must be called passing it littlest sets to biggest 
398                 //              since it the rating is made from your sighting plus you best splits rating
399                 //2. it saves the top pair to use later
400                 // ***********************************************************************************//
401
402                 
403                 if (group.size() < 3) {  return rate;  }
404                 
405                 map< vector<string>, int> possiblePairing;  //this is all the subsets of group
406                 
407                 //go through the sets
408                 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
409                         //are you a subset of bigset, then save in possiblePairings
410                         if (isSubset(group, it2->first) == true) {  possiblePairing[it2->first] = it2->second;  }
411                 }               
412         
413                 map< vector< vector<string> > , int > rating;
414                 
415                 while (possiblePairing.size() != 0) {
416                 
417                         it2 = possiblePairing.begin(); 
418                         vector<string> temprest = getRestSet(group, it2->first);
419                         
420                         //is the rest a set available in possiblePairings
421                         ittemp = possiblePairing.find(temprest);
422                         if (ittemp != possiblePairing.end()) {  //if the rest is in the possible pairings then add this pair to rating map
423                                 vector< vector<string> > temprate;
424                                 temprate.push_back(it2->first);  temprate.push_back(temprest);
425                                 
426                                 rating[temprate] = (nodePairsInitialRate[it2->first] + nodePairsInitialRate[temprest]);
427                                 
428                                 //erase so you dont add 1,2 and 2,1.
429                                 possiblePairing.erase(temprest);
430                         }
431                         
432                         possiblePairing.erase(it2);
433                 }
434
435
436                 it3 = rating.begin();
437                 rate = it3->second;
438                 vector< vector<string> > topPair = it3->first;
439                 
440                 //choose the split with the best rating
441                 for (it3 = rating.begin(); it3 != rating.end(); it3++) {
442                         
443                         if (it3->second > rate) {  rate = it3->second;  topPair = it3->first;  }
444                 }
445                 
446                 bestSplit[group] = topPair;
447                 
448                 return rate;
449         }
450         catch(exception& e) {
451                 m->errorOut(e, "ConcensusCommand", "getSubgroupRating");
452                 exit(1);
453         }
454 }
455
456 //**********************************************************************************************************************
457 vector<string> ConcensusCommand::getRestSet(vector<string> bigset, vector<string> subset) {
458         try {
459                 vector<string> rest;
460                 
461                 for (int i = 0; i < bigset.size(); i++) {
462                         bool inSubset = false;
463                         for (int j = 0; j < subset.size(); j++) {
464                                 if (bigset[i] == subset[j]) { inSubset = true; break; }
465                         }
466                         
467                         //its not in the subset so put it in the rest
468                         if (inSubset == false) { rest.push_back(bigset[i]); }
469                 }
470
471                 return rest;
472         
473         }
474         catch(exception& e) {
475                 m->errorOut(e, "ConcensusCommand", "getRestSet");
476                 exit(1);
477         }
478 }
479
480 //**********************************************************************************************************************
481 bool ConcensusCommand::isSubset(vector<string> bigset, vector<string> subset) {
482         try {
483                 
484         
485                 if (subset.size() > bigset.size()) { return false;  }
486                 
487                 //check if each guy in suset is also in bigset
488                 for (int i = 0; i < subset.size(); i++) {
489                         bool match = false;
490                         for (int j = 0; j < bigset.size(); j++) {
491                                 if (subset[i] == bigset[j]) { match = true; break; }
492                         }
493                         
494                         //you have a guy in subset that had no match in bigset
495                         if (match == false) { return false; }
496                 }
497                 
498                 return true;
499         
500         }
501         catch(exception& e) {
502                 m->errorOut(e, "ConcensusCommand", "isSubset");
503                 exit(1);
504         }
505 }
506 //**********************************************************************************************************************
507 int ConcensusCommand::findSpot(string node) {
508         try {
509                 int spot;
510                 
511                 //check if each guy in suset is also in bigset
512                 for (int i = 0; i < treeSet.size(); i++) {
513                         if (treeSet[i] == node) { spot = i; break; }
514                 }
515                 
516                 return spot;
517         
518         }
519         catch(exception& e) {
520                 m->errorOut(e, "ConcensusCommand", "findSpot");
521                 exit(1);
522         }
523 }
524 //**********************************************************************************************************************
525
526
527
528