5 * Created by westcott on 11/12/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "indicatorcommand.h"
11 #include "sharedutilities.h"
13 //**********************************************************************************************************************
14 vector<string> IndicatorCommand::getValidParameters(){
16 string Array[] = {"tree","shared","relabund","design","label","groups","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "IndicatorCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 vector<string> IndicatorCommand::getRequiredParameters(){
28 string Array[] = {"tree"};
29 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
33 m->errorOut(e, "IndicatorCommand", "getRequiredParameters");
37 //**********************************************************************************************************************
38 IndicatorCommand::IndicatorCommand(){
40 abort = true; calledHelp = true;
41 vector<string> tempOutNames;
42 outputTypes["tree"] = tempOutNames;
43 outputTypes["summary"] = tempOutNames;
46 m->errorOut(e, "IndicatorCommand", "IndicatorCommand");
51 //**********************************************************************************************************************
52 vector<string> IndicatorCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "IndicatorCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
63 IndicatorCommand::IndicatorCommand(string option) {
65 globaldata = GlobalData::getInstance();
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
72 //valid paramters for this command
73 string Array[] = {"tree","shared","design","relabund","groups","label","outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 globaldata->newRead();
89 vector<string> tempOutNames;
90 outputTypes["tree"] = tempOutNames;
91 outputTypes["summary"] = tempOutNames;
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("tree");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["tree"] = inputDir + it->second; }
106 it = parameters.find("shared");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["shared"] = inputDir + it->second; }
114 it = parameters.find("relabund");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["relabund"] = inputDir + it->second; }
122 it = parameters.find("design");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["design"] = inputDir + it->second; }
131 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
133 //check for required parameters
134 treefile = validParameter.validFile(parameters, "tree", true);
135 if (treefile == "not open") { abort = true; }
136 else if (treefile == "not found") { treefile = ""; m->mothurOut("tree is a required parameter for the indicator command."); m->mothurOutEndLine(); abort = true; }
137 else { globaldata->setTreeFile(treefile); globaldata->setFormat("tree"); }
139 sharedfile = validParameter.validFile(parameters, "shared", true);
140 if (sharedfile == "not open") { abort = true; }
141 else if (sharedfile == "not found") { sharedfile = ""; }
142 else { inputFileName = sharedfile; }
144 relabundfile = validParameter.validFile(parameters, "relabund", true);
145 if (relabundfile == "not open") { abort = true; }
146 else if (relabundfile == "not found") { relabundfile = ""; }
147 else { inputFileName = relabundfile; }
149 designfile = validParameter.validFile(parameters, "design", true);
150 if (designfile == "not open") { abort = true; }
151 else if (designfile == "not found") { designfile = ""; }
153 groups = validParameter.validFile(parameters, "groups", false);
154 if (groups == "not found") { groups = ""; Groups.push_back("all"); }
155 else { m->splitAtDash(groups, Groups); }
156 globaldata->Groups = Groups;
158 label = validParameter.validFile(parameters, "label", false);
159 if (label == "not found") { label = ""; m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); label=""; }
161 if ((relabundfile == "") && (sharedfile == "")) { m->mothurOut("You must provide either a shared or relabund file."); m->mothurOutEndLine(); abort = true; }
163 if ((relabundfile != "") && (sharedfile != "")) { m->mothurOut("You may not use both a shared and relabund file."); m->mothurOutEndLine(); abort = true; }
167 catch(exception& e) {
168 m->errorOut(e, "IndicatorCommand", "IndicatorCommand");
172 //**********************************************************************************************************************
174 void IndicatorCommand::help(){
176 m->mothurOut("The indicator command reads a shared or relabund file and a tree file, and outputs a .indicator.tre and .indicator.summary file. \n");
177 m->mothurOut("The new tree contains labels at each internal node. The label is the node number so you can relate the tree to the summary file.\n");
178 m->mothurOut("The summary file lists the indicator value for each OTU for each node.\n");
179 m->mothurOut("The indicator command parameters are tree, groups, shared, relabund, design and label. The tree parameter is required as well as either shared or relabund.\n");
180 m->mothurOut("The design parameter allows you to provide a design file to relate the tree to the shared or relabund file.\n");
181 m->mothurOut("The groups parameter allows you to specify which of the groups in your shared or relabund you would like analyzed, or if you provide a design file the groups in your design file. The groups may be entered separated by dashes.\n");
182 m->mothurOut("The label parameter indicates at what distance your tree relates to the shared or relabund.\n");
183 m->mothurOut("The indicator command should be used in the following format: indicator(tree=test.tre, shared=test.shared, label=0.03)\n");
184 m->mothurOut("Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreefile).\n\n");
186 catch(exception& e) {
187 m->errorOut(e, "IndicatorCommand", "help");
192 //**********************************************************************************************************************
194 IndicatorCommand::~IndicatorCommand(){}
196 //**********************************************************************************************************************
198 int IndicatorCommand::execute(){
201 if (abort == true) { if (calledHelp) { return 0; } return 2; }
203 //read designfile if given and set up globaldatas groups for read of sharedfiles
204 if (designfile != "") {
205 designMap = new GroupMap(designfile);
206 designMap->readDesignMap();
208 //fill Groups - checks for "all" and for any typo groups
209 SharedUtil* util = new SharedUtil();
210 util->setGroups(Groups, designMap->namesOfGroups);
213 //loop through the Groups and fill Globaldata's Groups with the design file info
214 globaldata->Groups = designMap->getNamesSeqs(Groups);
217 /***************************************************/
218 // use smart distancing to get right sharedRabund //
219 /***************************************************/
220 if (sharedfile != "") {
222 if (m->control_pressed) { if (designfile != "") { delete designMap; } for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
223 if (lookup[0] == NULL) { m->mothurOut("[ERROR] reading shared file."); m->mothurOutEndLine(); return 0; }
226 if (m->control_pressed) { if (designfile != "") { delete designMap; } for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
227 if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
230 //reset Globaldatas groups if needed
231 if (designfile != "") { globaldata->Groups = Groups; }
233 /***************************************************/
234 // reading tree info //
235 /***************************************************/
236 string groupfile = "";
237 Tree* tree = new Tree(treefile); delete tree; //extracts names from tree to make faked out groupmap
239 globaldata->setGroupFile(groupfile);
240 treeMap = new TreeMap();
241 bool mismatch = false;
243 for (int i = 0; i < globaldata->Treenames.size(); i++) {
244 //sanity check - is this a group that is not in the sharedfile?
245 if (designfile == "") {
246 if (!(m->inUsersGroups(globaldata->Treenames[i], globaldata->gGroupmap->namesOfGroups))) {
247 m->mothurOut("[ERROR]: " + globaldata->Treenames[i] + " is not a group in your shared or relabund file."); m->mothurOutEndLine();
250 treeMap->addSeq(globaldata->Treenames[i], "Group1");
252 vector<string> myGroups; myGroups.push_back(globaldata->Treenames[i]);
253 vector<string> myNames = designMap->getNamesSeqs(myGroups);
255 for(int k = 0; k < myNames.size(); k++) {
256 if (!(m->inUsersGroups(myNames[k], globaldata->gGroupmap->namesOfGroups))) {
257 m->mothurOut("[ERROR]: " + myNames[k] + " is not a group in your shared or relabund file."); m->mothurOutEndLine();
261 treeMap->addSeq(globaldata->Treenames[i], "Group1");
265 if ((designfile != "") && (globaldata->Treenames.size() != Groups.size())) { m->mothurOut("[ERROR]: You design file does not match your tree, aborting."); m->mothurOutEndLine(); mismatch = true; }
267 if (mismatch) { //cleanup and exit
268 if (designfile != "") { delete designMap; }
269 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
270 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
275 globaldata->gTreemap = treeMap;
277 read = new ReadNewickTree(treefile);
278 int readOk = read->read();
280 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; delete read; return 0; }
282 vector<Tree*> T = globaldata->gTree;
286 if (m->control_pressed) {
287 if (designfile != "") { delete designMap; }
288 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
289 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
290 for (int i = 0; i < T.size(); i++) { delete T[i]; } globaldata->gTree.clear(); delete globaldata->gTreemap; return 0;
293 T[0]->assembleTree();
295 /***************************************************/
296 // create ouptut tree - respecting pickedGroups //
297 /***************************************************/
298 Tree* outputTree = new Tree(globaldata->Groups.size());
300 outputTree->getSubTree(T[0], globaldata->Groups);
301 outputTree->assembleTree();
303 //no longer need original tree, we have output tree to use and label
304 for (int i = 0; i < T.size(); i++) { delete T[i]; } globaldata->gTree.clear();
307 if (m->control_pressed) {
308 if (designfile != "") { delete designMap; }
309 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
310 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
311 delete outputTree; delete globaldata->gTreemap; return 0;
314 /***************************************************/
315 // get indicator species values //
316 /***************************************************/
317 GetIndicatorSpecies(outputTree);
319 if (designfile != "") { delete designMap; }
321 if (m->control_pressed) {
322 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
323 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
324 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
325 delete outputTree; delete globaldata->gTreemap; return 0;
328 //set tree file as new current treefile
330 itTypes = outputTypes.find("tree");
331 if (itTypes != outputTypes.end()) {
332 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
335 m->mothurOutEndLine();
336 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
337 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
338 m->mothurOutEndLine();
342 catch(exception& e) {
343 m->errorOut(e, "IndicatorCommand", "execute");
347 //**********************************************************************************************************************
348 //traverse tree finding indicator species values for each otu at each node
349 //label node with otu number that has highest indicator value
350 //report all otu values to file
351 int IndicatorCommand::GetIndicatorSpecies(Tree*& T){
354 string thisOutputDir = outputDir;
355 if (outputDir == "") { thisOutputDir += m->hasPath(inputFileName); }
356 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(inputFileName)) + "indicator.summary";
357 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
360 m->openOutputFile(outputFileName, out);
361 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
364 if (sharedfile != "") { numBins = lookup[0]->getNumBins(); }
365 else { numBins = lookupFloat[0]->getNumBins(); }
369 for (int i = 0; i < numBins; i++) { out << "OTU-" << (i+1) << '\t'; }
372 string treeOutputDir = outputDir;
373 if (outputDir == "") { treeOutputDir += m->hasPath(treefile); }
374 string outputTreeFileName = treeOutputDir + m->getRootName(m->getSimpleName(treefile)) + "indicator.tre";
377 //create a map from tree node index to names of descendants, save time later to know which sharedRabund you need
378 map<int, set<string> > nodeToDescendants;
379 map<int, set<int> > descendantNodes;
380 for (int i = 0; i < T->getNumNodes(); i++) {
381 if (m->control_pressed) { return 0; }
383 nodeToDescendants[i] = getDescendantList(T, i, nodeToDescendants, descendantNodes);
386 //you need the distances to leaf to decide grouping below
387 //this will also set branch lengths if the tree does not include them
388 map<int, float> distToRoot = getDistToRoot(T);
391 for (int i = T->getNumLeaves(); i < T->getNumNodes(); i++) {
393 if (m->control_pressed) { out.close(); return 0; }
395 /*****************************************************/
396 //create vectors containing rabund info //
397 /*****************************************************/
399 vector<float> indicatorValues; //size of numBins
401 if (sharedfile != "") {
402 vector< vector<SharedRAbundVector*> > groupings;
404 //get nodes that will be a valid grouping
405 //you are valid if you are not one of my descendants
406 //AND your distToRoot is >= mine
407 //AND you were not added as part of a larger grouping. Largest nodes are added first.
409 set<string> groupsAlreadyAdded;
410 //create a grouping with my grouping
411 vector<SharedRAbundVector*> subset;
413 int doneCount = nodeToDescendants[i].size();
414 for (int k = 0; k < lookup.size(); k++) {
415 //is this descendant of i
416 if ((nodeToDescendants[i].count(lookup[k]->getGroup()) != 0)) {
417 subset.push_back(lookup[k]);
418 groupsAlreadyAdded.insert(lookup[k]->getGroup());
421 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
423 if (subset.size() != 0) { groupings.push_back(subset); }
426 for (int j = (T->getNumNodes()-1); j >= 0; j--) {
429 if ((descendantNodes[i].count(j) == 0) && (distToRoot[j] >= distToRoot[i])) {
430 vector<SharedRAbundVector*> subset;
432 int doneCount = nodeToDescendants[j].size();
433 for (int k = 0; k < lookup.size(); k++) {
434 //is this descendant of j, and we didn't already add this as part of a larger grouping
435 if ((nodeToDescendants[j].count(lookup[k]->getGroup()) != 0) && (groupsAlreadyAdded.count(lookup[k]->getGroup()) == 0)) {
436 subset.push_back(lookup[k]);
437 groupsAlreadyAdded.insert(lookup[k]->getGroup());
440 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
443 //if subset.size == 0 then the node was added as part of a larger grouping
444 if (subset.size() != 0) { groupings.push_back(subset); }
448 if (groupsAlreadyAdded.size() != lookup.size()) { m->mothurOut("[ERROR]: could not make proper groupings."); m->mothurOutEndLine(); }
450 indicatorValues = getValues(groupings);
453 vector< vector<SharedRAbundFloatVector*> > groupings;
455 //get nodes that will be a valid grouping
456 //you are valid if you are not one of my descendants
457 //AND your distToRoot is >= mine
458 //AND you were not added as part of a larger grouping. Largest nodes are added first.
460 set<string> groupsAlreadyAdded;
461 //create a grouping with my grouping
462 vector<SharedRAbundFloatVector*> subset;
464 int doneCount = nodeToDescendants[i].size();
465 for (int k = 0; k < lookupFloat.size(); k++) {
466 //is this descendant of i
467 if ((nodeToDescendants[i].count(lookupFloat[k]->getGroup()) != 0)) {
468 subset.push_back(lookupFloat[k]);
469 groupsAlreadyAdded.insert(lookupFloat[k]->getGroup());
472 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
474 if (subset.size() != 0) { groupings.push_back(subset); }
476 for (int j = (T->getNumNodes()-1); j >= 0; j--) {
477 if ((descendantNodes[i].count(j) == 0) && (distToRoot[j] >= distToRoot[i])) {
478 vector<SharedRAbundFloatVector*> subset;
480 int doneCount = nodeToDescendants[j].size();
481 for (int k = 0; k < lookupFloat.size(); k++) {
482 //is this descendant of j, and we didn't already add this as part of a larger grouping
483 if ((nodeToDescendants[j].count(lookupFloat[k]->getGroup()) != 0) && (groupsAlreadyAdded.count(lookupFloat[k]->getGroup()) == 0)) {
484 subset.push_back(lookupFloat[k]);
485 groupsAlreadyAdded.insert(lookupFloat[k]->getGroup());
488 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
491 //if subset.size == 0 then the node was added as part of a larger grouping
492 if (subset.size() != 0) { groupings.push_back(subset); }
496 if (groupsAlreadyAdded.size() != lookupFloat.size()) { m->mothurOut("[ERROR]: could not make proper groupings."); m->mothurOutEndLine(); }
498 indicatorValues = getValues(groupings);
501 if (m->control_pressed) { out.close(); return 0; }
504 /******************************************************/
505 //output indicator values to table form + label tree //
506 /*****************************************************/
507 out << (i+1) << '\t';
508 for (int j = 0; j < indicatorValues.size(); j++) {
510 if (m->control_pressed) { out.close(); return 0; }
512 out << indicatorValues[j] << '\t';
516 T->tree[i].setLabel((i+1));
522 m->openOutputFile(outputTreeFileName, outTree);
523 outputNames.push_back(outputTreeFileName); outputTypes["tree"].push_back(outputTreeFileName);
525 T->print(outTree, "both");
530 catch(exception& e) {
531 m->errorOut(e, "IndicatorCommand", "GetIndicatorSpecies");
535 //**********************************************************************************************************************
536 vector<float> IndicatorCommand::getValues(vector< vector<SharedRAbundFloatVector*> >& groupings){
538 vector<float> values;
541 for (int i = 0; i < groupings[0][0]->getNumBins(); i++) {
543 if (m->control_pressed) { return values; }
546 float AijDenominator = 0.0;
548 //get overall abundance of each grouping
549 for (int j = 0; j < groupings.size(); j++) {
551 float totalAbund = 0;
553 for (int k = 0; k < groupings[j].size(); k++) {
554 totalAbund += groupings[j][k]->getAbundance(i);
555 if (groupings[j][k]->getAbundance(i) != 0) { numNotZero++; }
558 float Aij = (totalAbund / (float) groupings[j].size());
559 terms.push_back(Aij);
561 //percentage of sites represented
562 Bij.push_back(numNotZero / (float) groupings[j].size());
564 AijDenominator += Aij;
567 float maxIndVal = 0.0;
568 for (int j = 0; j < terms.size(); j++) {
569 float thisAij = (terms[j] / AijDenominator);
570 float thisValue = thisAij * Bij[j] * 100.0;
573 if (thisValue > maxIndVal) { maxIndVal = thisValue; }
576 values.push_back(maxIndVal);
581 catch(exception& e) {
582 m->errorOut(e, "IndicatorCommand", "getValues");
586 //**********************************************************************************************************************
587 //same as above, just data type difference
588 vector<float> IndicatorCommand::getValues(vector< vector<SharedRAbundVector*> >& groupings){
590 vector<float> values;
592 /*for (int j = 0; j < groupings.size(); j++) {
593 cout << "grouping " << j << endl;
594 for (int k = 0; k < groupings[j].size(); k++) {
595 cout << groupings[j][k]->getGroup() << endl;
599 for (int i = 0; i < groupings[0][0]->getNumBins(); i++) {
601 float AijDenominator = 0.0;
603 //get overall abundance of each grouping
604 for (int j = 0; j < groupings.size(); j++) {
606 int totalAbund = 0.0;
608 for (int k = 0; k < groupings[j].size(); k++) {
609 totalAbund += groupings[j][k]->getAbundance(i);
610 if (groupings[j][k]->getAbundance(i) != 0.0) { numNotZero++; }
614 float Aij = (totalAbund / (float) groupings[j].size());
615 terms.push_back(Aij);
617 //percentage of sites represented
618 Bij.push_back(numNotZero / (float) groupings[j].size());
620 AijDenominator += Aij;
623 float maxIndVal = 0.0;
624 for (int j = 0; j < terms.size(); j++) {
625 float thisAij = (terms[j] / AijDenominator);
626 float thisValue = thisAij * Bij[j] * 100.0;
629 if (thisValue > maxIndVal) { maxIndVal = thisValue; }
632 values.push_back(maxIndVal);
637 catch(exception& e) {
638 m->errorOut(e, "IndicatorCommand", "getValues");
642 //**********************************************************************************************************************
643 //you need the distances to root to decide groupings
644 //this will also set branch lengths if the tree does not include them
645 map<int, float> IndicatorCommand::getDistToRoot(Tree*& T){
647 map<int, float> dists;
649 bool hasBranchLengths = false;
650 for (int i = 0; i < T->getNumNodes(); i++) {
651 if (T->tree[i].getBranchLength() > 0.0) { hasBranchLengths = true; break; }
654 //set branchlengths if needed
655 if (!hasBranchLengths) {
656 for (int i = 0; i < T->getNumNodes(); i++) {
658 int lc = T->tree[i].getLChild();
659 int rc = T->tree[i].getRChild();
661 if (lc == -1) { // you are a leaf
662 //if you are a leaf set you priliminary length to 1.0, this may adjust later
663 T->tree[i].setBranchLength(1.0);
665 }else{ // you are an internal node
666 //look at your children's length to leaf
667 float ldist = dists[lc];
668 float rdist = dists[rc];
670 float greater = ldist;
671 if (rdist > greater) { greater = rdist; dists[i] = ldist + 1.0;}
672 else { dists[i] = rdist + 1.0; }
675 //branch length = difference + 1
676 T->tree[lc].setBranchLength((abs(ldist-greater) + 1.0));
677 T->tree[rc].setBranchLength((abs(rdist-greater) + 1.0));
684 for (int i = 0; i < T->getNumNodes(); i++) {
689 while(T->tree[index].getParent() != -1){
690 if (T->tree[index].getBranchLength() != -1) {
691 sum += abs(T->tree[index].getBranchLength());
693 index = T->tree[index].getParent();
701 catch(exception& e) {
702 m->errorOut(e, "IndicatorCommand", "getLengthToLeaf");
706 //**********************************************************************************************************************
707 set<string> IndicatorCommand::getDescendantList(Tree*& T, int i, map<int, set<string> > descendants, map<int, set<int> >& nodes){
711 set<string>::iterator it;
713 int lc = T->tree[i].getLChild();
714 int rc = T->tree[i].getRChild();
716 if (lc == -1) { //you are a leaf your only descendant is yourself
717 set<int> temp; temp.insert(i);
720 if (designfile == "") {
721 names.insert(T->tree[i].getName());
723 vector<string> myGroup; myGroup.push_back(T->tree[i].getName());
724 vector<string> myReps = designMap->getNamesSeqs(myGroup);
725 for (int k = 0; k < myReps.size(); k++) {
726 names.insert(myReps[k]);
730 }else{ //your descedants are the combination of your childrens descendants
731 names = descendants[lc];
732 nodes[i] = nodes[lc];
733 for (it = descendants[rc].begin(); it != descendants[rc].end(); it++) {
736 for (set<int>::iterator itNum = nodes[rc].begin(); itNum != nodes[rc].end(); itNum++) {
737 nodes[i].insert(*itNum);
739 //you are your own descendant
745 catch(exception& e) {
746 m->errorOut(e, "IndicatorCommand", "getDescendantList");
750 //**********************************************************************************************************************
751 int IndicatorCommand::getShared(){
753 InputData* input = new InputData(sharedfile, "sharedfile");
754 lookup = input->getSharedRAbundVectors();
755 string lastLabel = lookup[0]->getLabel();
757 if (label == "") { label = lastLabel; delete input; return 0; }
759 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
760 set<string> labels; labels.insert(label);
761 set<string> processedLabels;
762 set<string> userLabels = labels;
764 //as long as you are not at the end of the file or done wih the lines you want
765 while((lookup[0] != NULL) && (userLabels.size() != 0)) {
766 if (m->control_pressed) { delete input; return 0; }
768 if(labels.count(lookup[0]->getLabel()) == 1){
769 processedLabels.insert(lookup[0]->getLabel());
770 userLabels.erase(lookup[0]->getLabel());
774 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
775 string saveLabel = lookup[0]->getLabel();
777 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
778 lookup = input->getSharedRAbundVectors(lastLabel);
780 processedLabels.insert(lookup[0]->getLabel());
781 userLabels.erase(lookup[0]->getLabel());
783 //restore real lastlabel to save below
784 lookup[0]->setLabel(saveLabel);
788 lastLabel = lookup[0]->getLabel();
790 //get next line to process
791 //prevent memory leak
792 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
793 lookup = input->getSharedRAbundVectors();
797 if (m->control_pressed) { delete input; return 0; }
799 //output error messages about any remaining user labels
800 set<string>::iterator it;
801 bool needToRun = false;
802 for (it = userLabels.begin(); it != userLabels.end(); it++) {
803 m->mothurOut("Your file does not include the label " + *it);
804 if (processedLabels.count(lastLabel) != 1) {
805 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
808 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
812 //run last label if you need to
813 if (needToRun == true) {
814 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
815 lookup = input->getSharedRAbundVectors(lastLabel);
821 catch(exception& e) {
822 m->errorOut(e, "IndicatorCommand", "getShared");
826 //**********************************************************************************************************************
827 int IndicatorCommand::getSharedFloat(){
829 InputData* input = new InputData(relabundfile, "relabund");
830 lookupFloat = input->getSharedRAbundFloatVectors();
831 string lastLabel = lookupFloat[0]->getLabel();
833 if (label == "") { label = lastLabel; delete input; return 0; }
835 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
836 set<string> labels; labels.insert(label);
837 set<string> processedLabels;
838 set<string> userLabels = labels;
840 //as long as you are not at the end of the file or done wih the lines you want
841 while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
843 if (m->control_pressed) { delete input; return 0; }
845 if(labels.count(lookupFloat[0]->getLabel()) == 1){
846 processedLabels.insert(lookupFloat[0]->getLabel());
847 userLabels.erase(lookupFloat[0]->getLabel());
851 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
852 string saveLabel = lookupFloat[0]->getLabel();
854 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
855 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
857 processedLabels.insert(lookupFloat[0]->getLabel());
858 userLabels.erase(lookupFloat[0]->getLabel());
860 //restore real lastlabel to save below
861 lookupFloat[0]->setLabel(saveLabel);
865 lastLabel = lookupFloat[0]->getLabel();
867 //get next line to process
868 //prevent memory leak
869 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
870 lookupFloat = input->getSharedRAbundFloatVectors();
874 if (m->control_pressed) { delete input; return 0; }
876 //output error messages about any remaining user labels
877 set<string>::iterator it;
878 bool needToRun = false;
879 for (it = userLabels.begin(); it != userLabels.end(); it++) {
880 m->mothurOut("Your file does not include the label " + *it);
881 if (processedLabels.count(lastLabel) != 1) {
882 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
885 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
889 //run last label if you need to
890 if (needToRun == true) {
891 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
892 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
898 catch(exception& e) {
899 m->errorOut(e, "IndicatorCommand", "getShared");
903 /*****************************************************************/