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(){
50 //initialize outputTypes
51 vector<string> tempOutNames;
52 outputTypes["tree"] = tempOutNames;
53 outputTypes["nodepairs"] = tempOutNames;
56 m->errorOut(e, "ConcensusCommand", "ConcensusCommand");
60 //**********************************************************************************************************************
62 ConcensusCommand::ConcensusCommand(string fileroot) {
64 globaldata = GlobalData::getInstance();
67 //initialize outputTypes
68 vector<string> tempOutNames;
69 outputTypes["tree"] = tempOutNames;
70 outputTypes["nodepairs"] = tempOutNames;
74 t = globaldata->gTree;
78 m->errorOut(e, "ConcensusCommand", "ConcensusCommand");
83 //**********************************************************************************************************************
85 void ConcensusCommand::help(){
87 m->mothurOut("The consensus command can only be executed after a successful read.tree command.\n");
88 m->mothurOut("The consensus command has no parameters.\n");
89 m->mothurOut("The consensus command should be in the following format: consensus().\n");
90 m->mothurOut("The consensus command output two files: .consensus.tre and .consensuspairs.\n");
91 m->mothurOut("The .consensus.tre file contains the consensus tree of the trees in your input file.\n");
92 m->mothurOut("The branch lengths are the percentage of trees in your input file that had the given pair.\n");
93 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");
94 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");
97 m->errorOut(e, "ConcensusCommand", "help");
102 //**********************************************************************************************************************
104 ConcensusCommand::~ConcensusCommand(){}
106 //**********************************************************************************************************************
108 int ConcensusCommand::execute(){
111 if (abort == true) { return 0; }
113 numNodes = t[0]->getNumNodes();
114 numLeaves = t[0]->getNumLeaves();
117 //get the possible pairings
120 if (m->control_pressed) { return 0; }
122 //open file for pairing not included in the tree
123 notIncluded = filename + ".cons.pairs"; outputNames.push_back(notIncluded); outputTypes["nodepairs"].push_back(notIncluded);
124 m->openOutputFile(notIncluded, out2);
126 consensusTree = new Tree();
128 it2 = nodePairs.find(treeSet);
130 nodePairsInTree[treeSet] = it2->second;
132 //erase treeset because you are adding it
133 nodePairs.erase(treeSet);
135 //set count to numLeaves;
138 buildConcensusTree(treeSet);
140 if (m->control_pressed) { delete consensusTree; return 0; }
142 consensusTree->assembleTree();
144 if (m->control_pressed) { delete consensusTree; return 0; }
146 //output species in order
147 out2 << "Species in Order: " << endl << endl;
148 for (int i = 0; i < treeSet.size(); i++) { out2 << i+1 << ". " << treeSet[i] << endl; }
150 //output sets included
151 out2 << endl << "Sets included in the consensus tree:" << endl << endl;
153 if (m->control_pressed) { delete consensusTree; return 0; }
156 for (it2 = nodePairsInTree.begin(); it2 != nodePairsInTree.end(); it2++) {
158 if (m->control_pressed) { delete consensusTree; return 0; }
160 //only output pairs not leaves
161 if (it2->first.size() > 1) {
163 //initialize temp to all "."
164 temp.resize(treeSet.size(), ".");
166 //set the spot in temp that represents it2->first[i] to a "*"
167 for (int i = 0; i < it2->first.size(); i++) {
169 int index = findSpot(it2->first[i]);
171 //temp[index] = it2->first[i] + " ";
175 for (int j = 0; j < temp.size(); j++) {
178 out2 << '\t' << it2->second << endl;
182 //output sets not included
183 out2 << endl << "Sets NOT included in the consensus tree:" << endl << endl;
184 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
186 if (m->control_pressed) { delete consensusTree; return 0; }
189 //initialize temp to all "."
190 temp.resize(treeSet.size(), ".");
192 //set the spot in temp that represents it2->first[i] to a "*"
193 for (int i = 0; i < it2->first.size(); i++) {
195 int index = findSpot(it2->first[i]);
200 for (int j = 0; j < temp.size(); j++) {
203 out2 << '\t' << it2->second << endl;
206 outputFile = filename + ".cons.tre"; outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
207 m->openOutputFile(outputFile, out);
209 consensusTree->printForBoot(out);
211 out.close(); out2.close();
213 delete consensusTree;
217 catch(exception& e) {
218 m->errorOut(e, "ConcensusCommand", "execute");
223 //**********************************************************************************************************************
224 int ConcensusCommand::buildConcensusTree(vector<string> nodeSet) {
226 vector<string> leftChildSet;
227 vector<string> rightChildSet;
229 if (m->control_pressed) { return 1; }
231 //if you are at a leaf
232 if (nodeSet.size() == 1) {
233 //return the vector index of the leaf you are at
234 return consensusTree->getIndex(nodeSet[0]);
235 //terminate recursion
236 }else if (count == numNodes) { return 0; }
238 //finds best child pair
239 leftChildSet = getNextAvailableSet(nodeSet, rightChildSet);
240 int left = buildConcensusTree(leftChildSet);
241 int right = buildConcensusTree(rightChildSet);
242 consensusTree->tree[count].setChildren(left, right);
243 consensusTree->tree[count].setLabel(nodePairsInTree[nodeSet]);
244 consensusTree->tree[left].setParent(count);
245 consensusTree->tree[right].setParent(count);
251 catch(exception& e) {
252 m->errorOut(e, "ConcensusCommand", "buildConcensusTree");
257 //**********************************************************************************************************************
258 int ConcensusCommand::getSets() {
263 //for each tree add the possible pairs you find
264 for (int i = 0; i < t.size(); i++) {
266 //for each non-leaf node get descendant info.
267 for (int j = numLeaves; j < numNodes; j++) {
269 if (m->control_pressed) { return 1; }
272 //go through pcounts and pull out descendants
273 for (it = t[i]->tree[j].pcount.begin(); it != t[i]->tree[j].pcount.end(); it++) {
274 temp.push_back(it->first);
278 sort(temp.begin(), temp.end());
280 it2 = nodePairs.find(temp);
281 if (it2 != nodePairs.end()) {
290 //add each leaf to terminate recursion in consensus
291 //you want the leaves in there but with insignifigant sightings value so it is added last
292 //for each leaf node get descendant info.
293 for (int j = 0; j < numLeaves; j++) {
295 if (m->control_pressed) { return 1; }
297 //only need the first one since leaves have no descendants but themselves
298 it = t[0]->tree[j].pcount.begin();
299 temp.clear(); temp.push_back(it->first);
302 treeSet.push_back(it->first);
304 //add leaf to list but with sighting value less then all non leaf pairs
308 sort(treeSet.begin(), treeSet.end());
311 map< vector<string>, int> nodePairsCopy = nodePairs;
314 //set initial rating on pairs to sightings + subgroup sightings
315 while (nodePairsCopy.size() != 0) {
316 if (m->control_pressed) { return 1; }
318 vector<string> smallOne = getSmallest(nodePairsCopy);
320 int subgrouprate = getSubgroupRating(smallOne);
322 nodePairsInitialRate[smallOne] = nodePairs[smallOne] + subgrouprate;
324 nodePairsCopy.erase(smallOne);
329 catch(exception& e) {
330 m->errorOut(e, "ConcensusCommand", "getSets");
334 //**********************************************************************************************************************
335 vector<string> ConcensusCommand::getSmallest(map< vector<string>, int> nodes) {
337 vector<string> smallest = nodes.begin()->first;
338 int smallsize = smallest.size();
340 for(it2 = nodes.begin(); it2 != nodes.end(); it2++) {
341 if(it2->first.size() < smallsize) { smallsize = it2->first.size(); smallest = it2->first; }
346 catch(exception& e) {
347 m->errorOut(e, "ConcensusCommand", "getSmallest");
352 //**********************************************************************************************************************
353 vector<string> ConcensusCommand::getNextAvailableSet(vector<string> bigset, vector<string>& rest) {
355 //cout << "new call " << endl << endl << endl;
356 vector<string> largest; largest.clear();
359 //if you are just 2 groups
360 if (bigset.size() == 2) {
361 rest.push_back(bigset[0]);
362 largest.push_back(bigset[1]);
364 rest = bestSplit[bigset][0];
365 largest = bestSplit[bigset][1];
369 //save for printing out later and for branch lengths
370 nodePairsInTree[rest] = nodePairs[rest];
372 //delete whatever set you return because it is no longer available
373 nodePairs.erase(rest);
375 //save for printing out later and for branch lengths
376 nodePairsInTree[largest] = nodePairs[largest];
378 //delete whatever set you return because it is no longer available
379 nodePairs.erase(largest);
384 catch(exception& e) {
385 m->errorOut(e, "ConcensusCommand", "getNextAvailableSet");
390 /**********************************************************************************************************************/
391 int ConcensusCommand::getSubgroupRating(vector<string> group) {
393 map< vector<string>, int>::iterator ittemp;
394 map< vector< vector<string> > , int >::iterator it3;
397 // ***********************************************************************************//
398 //1. this function must be called passing it littlest sets to biggest
399 // since it the rating is made from your sighting plus you best splits rating
400 //2. it saves the top pair to use later
401 // ***********************************************************************************//
404 if (group.size() < 3) { return rate; }
406 map< vector<string>, int> possiblePairing; //this is all the subsets of group
408 //go through the sets
409 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
410 //are you a subset of bigset, then save in possiblePairings
411 if (isSubset(group, it2->first) == true) { possiblePairing[it2->first] = it2->second; }
414 map< vector< vector<string> > , int > rating;
416 while (possiblePairing.size() != 0) {
418 it2 = possiblePairing.begin();
419 vector<string> temprest = getRestSet(group, it2->first);
421 //is the rest a set available in possiblePairings
422 ittemp = possiblePairing.find(temprest);
423 if (ittemp != possiblePairing.end()) { //if the rest is in the possible pairings then add this pair to rating map
424 vector< vector<string> > temprate;
425 temprate.push_back(it2->first); temprate.push_back(temprest);
427 rating[temprate] = (nodePairsInitialRate[it2->first] + nodePairsInitialRate[temprest]);
429 //erase so you dont add 1,2 and 2,1.
430 possiblePairing.erase(temprest);
433 possiblePairing.erase(it2);
437 it3 = rating.begin();
439 vector< vector<string> > topPair = it3->first;
441 //choose the split with the best rating
442 for (it3 = rating.begin(); it3 != rating.end(); it3++) {
444 if (it3->second > rate) { rate = it3->second; topPair = it3->first; }
447 bestSplit[group] = topPair;
451 catch(exception& e) {
452 m->errorOut(e, "ConcensusCommand", "getSubgroupRating");
457 //**********************************************************************************************************************
458 vector<string> ConcensusCommand::getRestSet(vector<string> bigset, vector<string> subset) {
462 for (int i = 0; i < bigset.size(); i++) {
463 bool inSubset = false;
464 for (int j = 0; j < subset.size(); j++) {
465 if (bigset[i] == subset[j]) { inSubset = true; break; }
468 //its not in the subset so put it in the rest
469 if (inSubset == false) { rest.push_back(bigset[i]); }
475 catch(exception& e) {
476 m->errorOut(e, "ConcensusCommand", "getRestSet");
481 //**********************************************************************************************************************
482 bool ConcensusCommand::isSubset(vector<string> bigset, vector<string> subset) {
486 if (subset.size() > bigset.size()) { return false; }
488 //check if each guy in suset is also in bigset
489 for (int i = 0; i < subset.size(); i++) {
491 for (int j = 0; j < bigset.size(); j++) {
492 if (subset[i] == bigset[j]) { match = true; break; }
495 //you have a guy in subset that had no match in bigset
496 if (match == false) { return false; }
502 catch(exception& e) {
503 m->errorOut(e, "ConcensusCommand", "isSubset");
507 //**********************************************************************************************************************
508 int ConcensusCommand::findSpot(string node) {
512 //check if each guy in suset is also in bigset
513 for (int i = 0; i < treeSet.size(); i++) {
514 if (treeSet[i] == node) { spot = i; break; }
520 catch(exception& e) {
521 m->errorOut(e, "ConcensusCommand", "findSpot");
525 //**********************************************************************************************************************