5 * Created by Sarah Westcott on 4/29/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
10 #include "consensuscommand.h"
12 //**********************************************************************************************************************
13 vector<string> ConcensusCommand::getValidParameters(){
15 vector<string> myArray;
19 m->errorOut(e, "ConcensusCommand", "getValidParameters");
23 //**********************************************************************************************************************
24 vector<string> ConcensusCommand::getRequiredParameters(){
26 vector<string> myArray;
30 m->errorOut(e, "ConcensusCommand", "getRequiredParameters");
34 //**********************************************************************************************************************
35 vector<string> ConcensusCommand::getRequiredFiles(){
37 string AlignArray[] = {"tree","group"};
38 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
42 m->errorOut(e, "ConcensusCommand", "getRequiredFiles");
46 //**********************************************************************************************************************
47 ConcensusCommand::ConcensusCommand(){
49 //initialize outputTypes
50 vector<string> tempOutNames;
51 outputTypes["tree"] = tempOutNames;
52 outputTypes["nodepairs"] = tempOutNames;
55 m->errorOut(e, "ConcensusCommand", "ConcensusCommand");
59 //**********************************************************************************************************************
61 ConcensusCommand::ConcensusCommand(string fileroot) {
63 globaldata = GlobalData::getInstance();
66 //initialize outputTypes
67 vector<string> tempOutNames;
68 outputTypes["tree"] = tempOutNames;
69 outputTypes["nodepairs"] = tempOutNames;
73 t = globaldata->gTree;
77 m->errorOut(e, "ConcensusCommand", "ConcensusCommand");
82 //**********************************************************************************************************************
84 void ConcensusCommand::help(){
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");
96 m->errorOut(e, "ConcensusCommand", "help");
101 //**********************************************************************************************************************
103 ConcensusCommand::~ConcensusCommand(){}
105 //**********************************************************************************************************************
107 int ConcensusCommand::execute(){
110 if (abort == true) { return 0; }
112 numNodes = t[0]->getNumNodes();
113 numLeaves = t[0]->getNumLeaves();
116 //get the possible pairings
119 if (m->control_pressed) { return 0; }
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);
125 consensusTree = new Tree();
127 it2 = nodePairs.find(treeSet);
129 nodePairsInTree[treeSet] = it2->second;
131 //erase treeset because you are adding it
132 nodePairs.erase(treeSet);
134 //set count to numLeaves;
137 buildConcensusTree(treeSet);
139 if (m->control_pressed) { delete consensusTree; return 0; }
141 consensusTree->assembleTree();
143 if (m->control_pressed) { delete consensusTree; return 0; }
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; }
149 //output sets included
150 out2 << endl << "Sets included in the consensus tree:" << endl << endl;
152 if (m->control_pressed) { delete consensusTree; return 0; }
155 for (it2 = nodePairsInTree.begin(); it2 != nodePairsInTree.end(); it2++) {
157 if (m->control_pressed) { delete consensusTree; return 0; }
159 //only output pairs not leaves
160 if (it2->first.size() > 1) {
162 //initialize temp to all "."
163 temp.resize(treeSet.size(), ".");
165 //set the spot in temp that represents it2->first[i] to a "*"
166 for (int i = 0; i < it2->first.size(); i++) {
168 int index = findSpot(it2->first[i]);
170 //temp[index] = it2->first[i] + " ";
174 for (int j = 0; j < temp.size(); j++) {
177 out2 << '\t' << it2->second << endl;
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++) {
185 if (m->control_pressed) { delete consensusTree; return 0; }
188 //initialize temp to all "."
189 temp.resize(treeSet.size(), ".");
191 //set the spot in temp that represents it2->first[i] to a "*"
192 for (int i = 0; i < it2->first.size(); i++) {
194 int index = findSpot(it2->first[i]);
199 for (int j = 0; j < temp.size(); j++) {
202 out2 << '\t' << it2->second << endl;
205 outputFile = filename + ".cons.tre"; outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
206 m->openOutputFile(outputFile, out);
208 consensusTree->printForBoot(out);
210 out.close(); out2.close();
212 delete consensusTree;
216 catch(exception& e) {
217 m->errorOut(e, "ConcensusCommand", "execute");
222 //**********************************************************************************************************************
223 int ConcensusCommand::buildConcensusTree(vector<string> nodeSet) {
225 vector<string> leftChildSet;
226 vector<string> rightChildSet;
228 if (m->control_pressed) { return 1; }
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; }
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);
250 catch(exception& e) {
251 m->errorOut(e, "ConcensusCommand", "buildConcensusTree");
256 //**********************************************************************************************************************
257 int ConcensusCommand::getSets() {
262 //for each tree add the possible pairs you find
263 for (int i = 0; i < t.size(); i++) {
265 //for each non-leaf node get descendant info.
266 for (int j = numLeaves; j < numNodes; j++) {
268 if (m->control_pressed) { return 1; }
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);
277 sort(temp.begin(), temp.end());
279 it2 = nodePairs.find(temp);
280 if (it2 != nodePairs.end()) {
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++) {
294 if (m->control_pressed) { return 1; }
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);
301 treeSet.push_back(it->first);
303 //add leaf to list but with sighting value less then all non leaf pairs
307 sort(treeSet.begin(), treeSet.end());
310 map< vector<string>, int> nodePairsCopy = nodePairs;
313 //set initial rating on pairs to sightings + subgroup sightings
314 while (nodePairsCopy.size() != 0) {
315 if (m->control_pressed) { return 1; }
317 vector<string> smallOne = getSmallest(nodePairsCopy);
319 int subgrouprate = getSubgroupRating(smallOne);
321 nodePairsInitialRate[smallOne] = nodePairs[smallOne] + subgrouprate;
323 nodePairsCopy.erase(smallOne);
328 catch(exception& e) {
329 m->errorOut(e, "ConcensusCommand", "getSets");
333 //**********************************************************************************************************************
334 vector<string> ConcensusCommand::getSmallest(map< vector<string>, int> nodes) {
336 vector<string> smallest = nodes.begin()->first;
337 int smallsize = smallest.size();
339 for(it2 = nodes.begin(); it2 != nodes.end(); it2++) {
340 if(it2->first.size() < smallsize) { smallsize = it2->first.size(); smallest = it2->first; }
345 catch(exception& e) {
346 m->errorOut(e, "ConcensusCommand", "getSmallest");
351 //**********************************************************************************************************************
352 vector<string> ConcensusCommand::getNextAvailableSet(vector<string> bigset, vector<string>& rest) {
354 //cout << "new call " << endl << endl << endl;
355 vector<string> largest; largest.clear();
358 //if you are just 2 groups
359 if (bigset.size() == 2) {
360 rest.push_back(bigset[0]);
361 largest.push_back(bigset[1]);
363 rest = bestSplit[bigset][0];
364 largest = bestSplit[bigset][1];
368 //save for printing out later and for branch lengths
369 nodePairsInTree[rest] = nodePairs[rest];
371 //delete whatever set you return because it is no longer available
372 nodePairs.erase(rest);
374 //save for printing out later and for branch lengths
375 nodePairsInTree[largest] = nodePairs[largest];
377 //delete whatever set you return because it is no longer available
378 nodePairs.erase(largest);
383 catch(exception& e) {
384 m->errorOut(e, "ConcensusCommand", "getNextAvailableSet");
389 /**********************************************************************************************************************/
390 int ConcensusCommand::getSubgroupRating(vector<string> group) {
392 map< vector<string>, int>::iterator ittemp;
393 map< vector< vector<string> > , int >::iterator it3;
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 // ***********************************************************************************//
403 if (group.size() < 3) { return rate; }
405 map< vector<string>, int> possiblePairing; //this is all the subsets of group
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; }
413 map< vector< vector<string> > , int > rating;
415 while (possiblePairing.size() != 0) {
417 it2 = possiblePairing.begin();
418 vector<string> temprest = getRestSet(group, it2->first);
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);
426 rating[temprate] = (nodePairsInitialRate[it2->first] + nodePairsInitialRate[temprest]);
428 //erase so you dont add 1,2 and 2,1.
429 possiblePairing.erase(temprest);
432 possiblePairing.erase(it2);
436 it3 = rating.begin();
438 vector< vector<string> > topPair = it3->first;
440 //choose the split with the best rating
441 for (it3 = rating.begin(); it3 != rating.end(); it3++) {
443 if (it3->second > rate) { rate = it3->second; topPair = it3->first; }
446 bestSplit[group] = topPair;
450 catch(exception& e) {
451 m->errorOut(e, "ConcensusCommand", "getSubgroupRating");
456 //**********************************************************************************************************************
457 vector<string> ConcensusCommand::getRestSet(vector<string> bigset, vector<string> subset) {
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; }
467 //its not in the subset so put it in the rest
468 if (inSubset == false) { rest.push_back(bigset[i]); }
474 catch(exception& e) {
475 m->errorOut(e, "ConcensusCommand", "getRestSet");
480 //**********************************************************************************************************************
481 bool ConcensusCommand::isSubset(vector<string> bigset, vector<string> subset) {
485 if (subset.size() > bigset.size()) { return false; }
487 //check if each guy in suset is also in bigset
488 for (int i = 0; i < subset.size(); i++) {
490 for (int j = 0; j < bigset.size(); j++) {
491 if (subset[i] == bigset[j]) { match = true; break; }
494 //you have a guy in subset that had no match in bigset
495 if (match == false) { return false; }
501 catch(exception& e) {
502 m->errorOut(e, "ConcensusCommand", "isSubset");
506 //**********************************************************************************************************************
507 int ConcensusCommand::findSpot(string node) {
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; }
519 catch(exception& e) {
520 m->errorOut(e, "ConcensusCommand", "findSpot");
524 //**********************************************************************************************************************