5 * Created by Sarah Westcott on 4/29/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
10 #include "concensuscommand.h"
12 //**********************************************************************************************************************
14 ConcensusCommand::ConcensusCommand(){
16 globaldata = GlobalData::getInstance();
17 t = globaldata->gTree;
20 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function ConcensusCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
24 cout << "An unknown error has occurred in the ConcensusCommand class function ConcensusCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
28 //**********************************************************************************************************************
30 ConcensusCommand::~ConcensusCommand(){}
32 //**********************************************************************************************************************
34 int ConcensusCommand::execute(){
37 if (t.size() == 0) { return 0; }
39 numNodes = t[0]->getNumNodes();
40 numLeaves = t[0]->getNumLeaves();
43 //initialize nodepairs
44 nodePairs.resize(numNodes-numLeaves);
46 //process each tree and fill counts
47 for (int i = 0; i < t.size(); i++) {
48 //process each nonleaf node
49 for (int j = numLeaves; j < numNodes; j++) {
51 int leftChild = t[i]->tree[j].getLChild();
52 int rightChild = t[i]->tree[j].getRChild();
53 string pair = toString(leftChild) + "-" + toString(rightChild);
55 //if this is an existing pairing for this node then increment the count otherwise add new pairing.
56 it = nodePairs[j-numLeaves].find(pair);
57 if (it != nodePairs[j-numLeaves].end()) {//already have that score
58 nodePairs[j-numLeaves][pair]++;
59 }else{//first time we have seen this score
60 nodePairs[j-numLeaves][pair] = 1;
66 //print out pairings for testing
67 /*for (int i = 0; i < nodePairs.size(); i++) {
68 cout << "pairs for node " << i+numLeaves << endl;
69 for (it = nodePairs[i].begin(); it != nodePairs[i].end(); it++) {
70 cout << " pair = " << it->first << " count = " << it->second << endl;
74 //open file for pairing not included in the tree
75 notIncluded = getRootName(globaldata->inputFileName) + "concensuspairs";
76 openOutputFile(notIncluded, out2);
78 concensusTree = new Tree();
80 //set relationships for nonleaf nodes
81 for (int j = numLeaves; j < numNodes; j++) {
83 //find that nodes pairing with the highest count
85 for (it = nodePairs[j-numLeaves].begin(); it != nodePairs[j-numLeaves].end(); it++) {
86 if (it->second > large) { large = it->second; it2 = it; }
89 string pair = it2->first;
90 int pos = pair.find_first_of('-');
91 string lefts = pair.substr(0, pos);
92 string rights = pair.substr(pos+1, pair.length());
94 //converts string to int
95 int left = atoi(lefts.c_str());
96 int right = atoi(rights.c_str());
98 //set parents and children
99 concensusTree->tree[j].setChildren(left, right);
100 concensusTree->tree[left].setParent(j);
101 concensusTree->tree[right].setParent(j);
103 // set Branchlengths //
104 //if your children are leaves remove their branchlengths
105 if (concensusTree->tree[left].getLChild() == -1) { concensusTree->tree[left].setBranchLength(1.0); }
106 if (concensusTree->tree[right].getLChild() == -1) { concensusTree->tree[right].setBranchLength(1.0); }
108 //set your branch length to the percentage of trees with this pairing
109 float bLength = (float) it2->second / (float) t.size();
110 concensusTree->tree[j].setBranchLength(bLength);
113 string leftName, rightName;
114 getNames(it2->first, leftName, rightName);
116 out2 << "Node " << j+1 << " in concensus tree: " << leftName << "-" << rightName << '\t' << (float)it2->second / (float) t.size() << endl;
117 out2 << "Node " << j+1 << " sets not included in concensus tree: " << endl;
119 //print out sets not included
120 for (it = nodePairs[j-numLeaves].begin(); it != nodePairs[j-numLeaves].end(); it++) {
122 getNames(it->first, leftName, rightName);
123 out2 << leftName << "-" << rightName << '\t' << (float)it->second / (float) t.size() << endl;
130 concensusTree->assembleTree();
132 outputFile = getRootName(globaldata->inputFileName) + "concensus.tre";
133 openOutputFile(outputFile, out);
135 concensusTree->print(out);
137 out.close(); out2.close();
139 delete concensusTree;
143 catch(exception& e) {
144 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148 cout << "An unknown error has occurred in the ConcensusCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152 //**********************************************************************************************************************
154 void ConcensusCommand::getNames(string pair, string& leftName, string& rightName) {
156 int pos = pair.find_first_of('-');
157 string lefts = pair.substr(0, pos);
158 string rights = pair.substr(pos+1, pair.length());
160 //converts string to int
161 int left = atoi(lefts.c_str());
162 int right = atoi(rights.c_str());
165 leftName = concensusTree->tree[left].getName();
166 rightName = concensusTree->tree[right].getName();
168 //if you are not a leaf use node number as name
169 if (leftName == "") { leftName = toString(left+1); }
170 if (rightName == "") { rightName = toString(right+1); }
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function getNames. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the ConcensusCommand class function getNames. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 //**********************************************************************************************************************