5 * Created by Sarah Westcott on 4/29/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
10 #include "consensuscommand.h"
12 //**********************************************************************************************************************
14 ConcensusCommand::ConcensusCommand(string fileroot){
16 globaldata = GlobalData::getInstance();
21 t = globaldata->gTree;
25 errorOut(e, "ConcensusCommand", "ConcensusCommand");
30 //**********************************************************************************************************************
32 void ConcensusCommand::help(){
34 mothurOut("The consensus command can only be executed after a successful read.tree command.\n");
35 mothurOut("The consensus command has no parameters.\n");
36 mothurOut("The consensus command should be in the following format: consensus().\n");
37 mothurOut("The consensus command output two files: .consensus.tre and .consensuspairs.\n");
38 mothurOut("The .consensus.tre file contains the consensus tree of the trees in your input file.\n");
39 mothurOut("The branch lengths are the percentage of trees in your input file that had the given pair.\n");
40 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");
41 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");
44 errorOut(e, "ConcensusCommand", "help");
49 //**********************************************************************************************************************
51 ConcensusCommand::~ConcensusCommand(){}
53 //**********************************************************************************************************************
55 int ConcensusCommand::execute(){
58 if (abort == true) { return 0; }
60 numNodes = t[0]->getNumNodes();
61 numLeaves = t[0]->getNumLeaves();
64 //get the possible pairings
67 //open file for pairing not included in the tree
68 notIncluded = filename + ".cons.pairs";
69 openOutputFile(notIncluded, out2);
71 consensusTree = new Tree();
73 it2 = nodePairs.find(treeSet);
75 nodePairsInTree[treeSet] = it2->second;
77 //erase treeset because you are adding it
78 nodePairs.erase(treeSet);
80 //set count to numLeaves;
83 buildConcensusTree(treeSet);
85 consensusTree->assembleTree();
87 //output species in order
88 out2 << "Species in Order: " << endl << endl;
89 for (int i = 0; i < treeSet.size(); i++) { out2 << i+1 << ". " << treeSet[i] << endl; }
91 //output sets included
92 out2 << endl << "Sets included in the consensus tree:" << endl << endl;
95 for (it2 = nodePairsInTree.begin(); it2 != nodePairsInTree.end(); it2++) {
96 //only output pairs not leaves
97 if (it2->first.size() > 1) {
99 //initialize temp to all "."
100 temp.resize(treeSet.size(), ".");
102 //set the spot in temp that represents it2->first[i] to a "*"
103 for (int i = 0; i < it2->first.size(); i++) {
105 int index = findSpot(it2->first[i]);
107 //temp[index] = it2->first[i] + " ";
111 for (int j = 0; j < temp.size(); j++) {
114 out2 << '\t' << it2->second << endl;
118 //output sets not included
119 out2 << endl << "Sets NOT included in the consensus tree:" << endl << endl;
120 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
122 //initialize temp to all "."
123 temp.resize(treeSet.size(), ".");
125 //set the spot in temp that represents it2->first[i] to a "*"
126 for (int i = 0; i < it2->first.size(); i++) {
128 int index = findSpot(it2->first[i]);
133 for (int j = 0; j < temp.size(); j++) {
136 out2 << '\t' << it2->second << endl;
139 outputFile = filename + ".cons.tre";
140 openOutputFile(outputFile, out);
142 consensusTree->printForBoot(out);
144 out.close(); out2.close();
146 delete consensusTree;
150 catch(exception& e) {
151 errorOut(e, "ConcensusCommand", "execute");
156 //**********************************************************************************************************************
157 int ConcensusCommand::buildConcensusTree(vector<string> nodeSet) {
159 vector<string> leftChildSet;
160 vector<string> rightChildSet;
162 //if you are at a leaf
163 if (nodeSet.size() == 1) {
164 //return the vector index of the leaf you are at
165 return consensusTree->getIndex(nodeSet[0]);
166 //terminate recursion
167 }else if (count == numNodes) { return 0; }
169 //finds best child pair
170 leftChildSet = getNextAvailableSet(nodeSet, rightChildSet);
171 int left = buildConcensusTree(leftChildSet);
172 int right = buildConcensusTree(rightChildSet);
173 consensusTree->tree[count].setChildren(left, right);
174 consensusTree->tree[count].setLabel(nodePairsInTree[nodeSet]);
175 consensusTree->tree[left].setParent(count);
176 consensusTree->tree[right].setParent(count);
182 catch(exception& e) {
183 errorOut(e, "ConcensusCommand", "buildConcensusTree");
188 //**********************************************************************************************************************
189 void ConcensusCommand::getSets() {
194 //for each tree add the possible pairs you find
195 for (int i = 0; i < t.size(); i++) {
197 //for each non-leaf node get descendant info.
198 for (int j = numLeaves; j < numNodes; j++) {
200 //go through pcounts and pull out descendants
201 for (it = t[i]->tree[j].pcount.begin(); it != t[i]->tree[j].pcount.end(); it++) {
202 temp.push_back(it->first);
206 sort(temp.begin(), temp.end());
208 it2 = nodePairs.find(temp);
209 if (it2 != nodePairs.end()) {
218 //add each leaf to terminate recursion in consensus
219 //you want the leaves in there but with insignifigant sightings value so it is added last
220 //for each leaf node get descendant info.
221 for (int j = 0; j < numLeaves; j++) {
223 //only need the first one since leaves have no descendants but themselves
224 it = t[0]->tree[j].pcount.begin();
225 temp.clear(); temp.push_back(it->first);
228 treeSet.push_back(it->first);
230 //add leaf to list but with sighting value less then all non leaf pairs
234 sort(treeSet.begin(), treeSet.end());
237 map< vector<string>, int> nodePairsCopy = nodePairs;
240 //set initial rating on pairs to sightings + subgroup sightings
241 while (nodePairsCopy.size() != 0) {
243 vector<string> small = getSmallest(nodePairsCopy);
245 int subgrouprate = getSubgroupRating(small);
247 nodePairsInitialRate[small] = nodePairs[small] + subgrouprate;
249 nodePairsCopy.erase(small);
253 catch(exception& e) {
254 errorOut(e, "ConcensusCommand", "getSets");
258 //**********************************************************************************************************************
259 vector<string> ConcensusCommand::getSmallest(map< vector<string>, int> nodes) {
261 vector<string> smallest = nodes.begin()->first;
262 int smallsize = smallest.size();
264 for(it2 = nodes.begin(); it2 != nodes.end(); it2++) {
265 if(it2->first.size() < smallsize) { smallsize = it2->first.size(); smallest = it2->first; }
270 catch(exception& e) {
271 errorOut(e, "ConcensusCommand", "getSmallest");
276 //**********************************************************************************************************************
277 vector<string> ConcensusCommand::getNextAvailableSet(vector<string> bigset, vector<string>& rest) {
279 //cout << "new call " << endl << endl << endl;
280 vector<string> largest; largest.clear();
283 //if you are just 2 groups
284 if (bigset.size() == 2) {
285 rest.push_back(bigset[0]);
286 largest.push_back(bigset[1]);
288 rest = bestSplit[bigset][0];
289 largest = bestSplit[bigset][1];
293 //save for printing out later and for branch lengths
294 nodePairsInTree[rest] = nodePairs[rest];
296 //delete whatever set you return because it is no longer available
297 nodePairs.erase(rest);
299 //save for printing out later and for branch lengths
300 nodePairsInTree[largest] = nodePairs[largest];
302 //delete whatever set you return because it is no longer available
303 nodePairs.erase(largest);
308 catch(exception& e) {
309 errorOut(e, "ConcensusCommand", "getNextAvailableSet");
314 /**********************************************************************************************************************/
315 int ConcensusCommand::getSubgroupRating(vector<string> group) {
317 map< vector<string>, int>::iterator ittemp;
318 map< vector< vector<string> > , int >::iterator it3;
321 // ***********************************************************************************//
322 //1. this function must be called passing it littlest sets to biggest
323 // since it the rating is made from your sighting plus you best splits rating
324 //2. it saves the top pair to use later
325 // ***********************************************************************************//
328 if (group.size() < 3) { return rate; }
330 map< vector<string>, int> possiblePairing; //this is all the subsets of group
332 //go through the sets
333 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
334 //are you a subset of bigset, then save in possiblePairings
335 if (isSubset(group, it2->first) == true) { possiblePairing[it2->first] = it2->second; }
338 map< vector< vector<string> > , int > rating;
340 while (possiblePairing.size() != 0) {
342 it2 = possiblePairing.begin();
343 vector<string> temprest = getRestSet(group, it2->first);
345 //is the rest a set available in possiblePairings
346 ittemp = possiblePairing.find(temprest);
347 if (ittemp != possiblePairing.end()) { //if the rest is in the possible pairings then add this pair to rating map
348 vector< vector<string> > temprate;
349 temprate.push_back(it2->first); temprate.push_back(temprest);
351 rating[temprate] = (nodePairsInitialRate[it2->first] + nodePairsInitialRate[temprest]);
353 //erase so you dont add 1,2 and 2,1.
354 possiblePairing.erase(temprest);
357 possiblePairing.erase(it2);
361 it3 = rating.begin();
363 vector< vector<string> > topPair = it3->first;
365 //choose the split with the best rating
366 for (it3 = rating.begin(); it3 != rating.end(); it3++) {
368 if (it3->second > rate) { rate = it3->second; topPair = it3->first; }
371 bestSplit[group] = topPair;
375 catch(exception& e) {
376 errorOut(e, "ConcensusCommand", "getSubgroupRating");
381 //**********************************************************************************************************************
382 vector<string> ConcensusCommand::getRestSet(vector<string> bigset, vector<string> subset) {
386 for (int i = 0; i < bigset.size(); i++) {
387 bool inSubset = false;
388 for (int j = 0; j < subset.size(); j++) {
389 if (bigset[i] == subset[j]) { inSubset = true; break; }
392 //its not in the subset so put it in the rest
393 if (inSubset == false) { rest.push_back(bigset[i]); }
399 catch(exception& e) {
400 errorOut(e, "ConcensusCommand", "getRestSet");
405 //**********************************************************************************************************************
406 bool ConcensusCommand::isSubset(vector<string> bigset, vector<string> subset) {
410 if (subset.size() > bigset.size()) { return false; }
412 //check if each guy in suset is also in bigset
413 for (int i = 0; i < subset.size(); i++) {
415 for (int j = 0; j < bigset.size(); j++) {
416 if (subset[i] == bigset[j]) { match = true; break; }
419 //you have a guy in subset that had no match in bigset
420 if (match == false) { return false; }
426 catch(exception& e) {
427 errorOut(e, "ConcensusCommand", "isSubset");
431 //**********************************************************************************************************************
432 int ConcensusCommand::findSpot(string node) {
436 //check if each guy in suset is also in bigset
437 for (int i = 0; i < treeSet.size(); i++) {
438 if (treeSet[i] == node) { spot = i; break; }
444 catch(exception& e) {
445 errorOut(e, "ConcensusCommand", "findSpot");
449 //**********************************************************************************************************************