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 //get the possible pairings
46 //print out pairings for testing
47 /*cout << "possible pairing " << endl;
48 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
49 for (int i = 0; i < it2->first.size(); i++) {
50 cout << it2->first[i] << " ";
52 cout << '\t' << it2->second << endl;
56 //open file for pairing not included in the tree
57 notIncluded = getRootName(globaldata->inputFileName) + "concensuspairs";
58 openOutputFile(notIncluded, out2);
60 concensusTree = new Tree();
62 it2 = nodePairs.find(treeSet);
64 nodePairsInTree[treeSet] = it2->second;
66 //erase treeset because you are adding it
67 nodePairs.erase(treeSet);
69 //set count to numLeaves;
72 buildConcensusTree(treeSet);
74 concensusTree->assembleTree();
76 //output species in order
77 out2 << "Species in Order: " << endl << endl;
78 for (int i = 0; i < treeSet.size(); i++) { out2 << i+1 << ". " << treeSet[i] << endl; }
81 //output sets included
82 out2 << endl << "Sets included in the concensus tree:" << endl << endl;
83 for (it2 = nodePairsInTree.begin(); it2 != nodePairsInTree.end(); it2++) {
84 //only output pairs not leaves
85 if (it2->first.size() > 1) {
87 //initialize temp to all "."
88 temp.resize(treeSet.size(), ".");
90 //set the spot in temp that represents it2->first[i] to a "*"
91 for (int i = 0; i < it2->first.size(); i++) {
93 int index = findSpot(it2->first[i]);
98 for (int j = 0; j < temp.size(); j++) {
101 out2 << '\t' << it2->second << endl;
105 //output sets not included
106 out2 << endl << "Sets NOT included in the concensus tree:" << endl << endl;
107 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
109 //initialize temp to all "."
110 temp.resize(treeSet.size(), ".");
112 //set the spot in temp that represents it2->first[i] to a "*"
113 for (int i = 0; i < it2->first.size(); i++) {
115 int index = findSpot(it2->first[i]);
120 for (int j = 0; j < temp.size(); j++) {
123 out2 << '\t' << it2->second << endl;
126 outputFile = getRootName(globaldata->inputFileName) + "concensus.tre";
127 openOutputFile(outputFile, out);
129 concensusTree->printForBoot(out);
131 out.close(); out2.close();
133 delete concensusTree;
137 catch(exception& e) {
138 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142 cout << "An unknown error has occurred in the ConcensusCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147 //**********************************************************************************************************************
148 int ConcensusCommand::buildConcensusTree(vector<string> nodeSet) {
150 vector<string> leftChildSet;
151 vector<string> rightChildSet;
153 //if you are at a leaf
154 if (nodeSet.size() == 1) {
155 //return the vector index of the leaf you are at
156 return concensusTree->getIndex(nodeSet[0]);
157 //terminate recursion
158 }else if (count == numNodes) { return 0; }
160 leftChildSet = getNextAvailableSet(nodeSet);
161 rightChildSet = getRestSet(nodeSet, leftChildSet);
162 int left = buildConcensusTree(leftChildSet);
163 int right = buildConcensusTree(rightChildSet);
164 concensusTree->tree[count].setChildren(left, right);
165 concensusTree->tree[count].setLabel(nodePairsInTree[nodeSet]);
166 concensusTree->tree[left].setParent(count);
167 concensusTree->tree[right].setParent(count);
173 catch(exception& e) {
174 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function buildConcensusTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178 cout << "An unknown error has occurred in the ConcensusCommand class function buildConcensusTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 //**********************************************************************************************************************
184 void ConcensusCommand::getSets() {
189 //for each tree add the possible pairs you find
190 for (int i = 0; i < t.size(); i++) {
192 //for each non-leaf node get descendant info.
193 for (int j = numLeaves; j < numNodes; j++) {
195 //go through pcounts and pull out descendants
196 for (it = t[i]->tree[j].pcount.begin(); it != t[i]->tree[j].pcount.end(); it++) {
197 temp.push_back(it->first);
201 sort(temp.begin(), temp.end());
203 it2 = nodePairs.find(temp);
204 if (it2 != nodePairs.end()) {
212 //add each leaf to terminate recursion in concensus
213 //you want the leaves in there but with insignifigant sightings value so it is added last
214 //for each leaf node get descendant info.
215 for (int j = 0; j < numLeaves; j++) {
217 //only need the first one since leaves have no descendants but themselves
218 it = t[0]->tree[j].pcount.begin();
219 temp.clear(); temp.push_back(it->first);
222 treeSet.push_back(it->first);
224 //add leaf to list but with sighting value less then all non leaf pairs
228 sort(treeSet.begin(), treeSet.end());
230 catch(exception& e) {
231 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function getSets. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 cout << "An unknown error has occurred in the ConcensusCommand class function getSets. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 //**********************************************************************************************************************
241 vector<string> ConcensusCommand::getNextAvailableSet(vector<string> bigset) {
243 vector<string> largest; largest.clear();
244 int largestSighting = -1;
246 //go through the sets
247 for (it2 = nodePairs.begin(); it2 != nodePairs.end(); it2++) {
248 //are you a subset of bigset
249 if (isSubset(bigset, it2->first) == true) {
251 //are you the largest. if you are the same size as current largest refer to sighting
252 if (it2->first.size() > largest.size()) { largest = it2->first; largestSighting = it2->second; }
253 else if (it2->first.size() == largest.size()) {
254 if (it2->second > largestSighting) { largest = it2->first; largestSighting = it2->second; }
260 //save for printing out later and for branch lengths
261 nodePairsInTree[largest] = nodePairs[largest];
263 //delete whatever set you return because it is no longer available
264 nodePairs.erase(largest);
269 catch(exception& e) {
270 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function getNextAvailableSet. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274 cout << "An unknown error has occurred in the ConcensusCommand class function getNextAvailableSet. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280 //**********************************************************************************************************************
281 vector<string> ConcensusCommand::getRestSet(vector<string> bigset, vector<string> subset) {
285 for (int i = 0; i < bigset.size(); i++) {
286 bool inSubset = false;
287 for (int j = 0; j < subset.size(); j++) {
288 if (bigset[i] == subset[j]) { inSubset = true; break; }
291 //its not in the subset so put it in the rest
292 if (inSubset == false) { rest.push_back(bigset[i]); }
295 //save for printing out later and for branch lengths
296 nodePairsInTree[rest] = nodePairs[rest];
298 //delete whatever set you return because it is no longer available
299 nodePairs.erase(rest);
304 catch(exception& e) {
305 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function getRestSet. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
309 cout << "An unknown error has occurred in the ConcensusCommand class function getRestSet. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315 //**********************************************************************************************************************
316 bool ConcensusCommand::isSubset(vector<string> bigset, vector<string> subset) {
319 //check if each guy in suset is also in bigset
320 for (int i = 0; i < subset.size(); i++) {
322 for (int j = 0; j < bigset.size(); j++) {
323 if (subset[i] == bigset[j]) { match = true; break; }
326 //you have a guy in subset that had no match in bigset
327 if (match == false) { return false; }
333 catch(exception& e) {
334 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function isSubset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
338 cout << "An unknown error has occurred in the ConcensusCommand class function isSubset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342 //**********************************************************************************************************************
343 int ConcensusCommand::findSpot(string node) {
347 //check if each guy in suset is also in bigset
348 for (int i = 0; i < treeSet.size(); i++) {
349 if (treeSet[i] == node) { spot = i; break; }
355 catch(exception& e) {
356 cout << "Standard Error: " << e.what() << " has occurred in the ConcensusCommand class Function findSpot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360 cout << "An unknown error has occurred in the ConcensusCommand class function findSpot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364 //**********************************************************************************************************************