5 * Created by westcott on 11/12/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "indicatorcommand.h"
11 //**********************************************************************************************************************
12 vector<string> IndicatorCommand::getValidParameters(){
14 string Array[] = {"tree","shared","relabund","label","groups","outputdir","inputdir"};
15 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
19 m->errorOut(e, "IndicatorCommand", "getValidParameters");
23 //**********************************************************************************************************************
24 vector<string> IndicatorCommand::getRequiredParameters(){
26 string Array[] = {"tree"};
27 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
31 m->errorOut(e, "IndicatorCommand", "getRequiredParameters");
35 //**********************************************************************************************************************
36 IndicatorCommand::IndicatorCommand(){
39 //initialize outputTypes
40 vector<string> tempOutNames;
41 outputTypes["tree"] = tempOutNames;
42 outputTypes["summary"] = tempOutNames;
45 m->errorOut(e, "IndicatorCommand", "IndicatorCommand");
50 //**********************************************************************************************************************
51 vector<string> IndicatorCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "IndicatorCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 IndicatorCommand::IndicatorCommand(string option) {
64 globaldata = GlobalData::getInstance();
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"tree","shared","relabund","groups","label","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 globaldata->newRead();
88 vector<string> tempOutNames;
89 outputTypes["tree"] = tempOutNames;
90 outputTypes["summary"] = tempOutNames;
92 //if the user changes the input directory command factory will send this info to us in the output parameter
93 string inputDir = validParameter.validFile(parameters, "inputdir", false);
94 if (inputDir == "not found"){ inputDir = ""; }
97 it = parameters.find("tree");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["tree"] = inputDir + it->second; }
105 it = parameters.find("shared");
106 //user has given a template file
107 if(it != parameters.end()){
108 path = m->hasPath(it->second);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { parameters["shared"] = inputDir + it->second; }
113 it = parameters.find("relabund");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["relabund"] = inputDir + it->second; }
123 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
125 //check for required parameters
126 treefile = validParameter.validFile(parameters, "tree", true);
127 if (treefile == "not open") { abort = true; }
128 else if (treefile == "not found") { treefile = ""; m->mothurOut("tree is a required parameter for the indicator command."); m->mothurOutEndLine(); abort = true; }
129 else { globaldata->setTreeFile(treefile); globaldata->setFormat("tree"); }
131 sharedfile = validParameter.validFile(parameters, "shared", true);
132 if (sharedfile == "not open") { abort = true; }
133 else if (sharedfile == "not found") { sharedfile = ""; }
134 else { inputFileName = sharedfile; }
136 relabundfile = validParameter.validFile(parameters, "relabund", true);
137 if (relabundfile == "not open") { abort = true; }
138 else if (relabundfile == "not found") { relabundfile = ""; }
139 else { inputFileName = relabundfile; }
141 groups = validParameter.validFile(parameters, "groups", false);
142 if (groups == "not found") { groups = ""; Groups.push_back("all"); }
143 else { m->splitAtDash(groups, Groups); }
144 globaldata->Groups = Groups;
146 label = validParameter.validFile(parameters, "label", false);
147 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=""; }
149 if ((relabundfile == "") && (sharedfile == "")) { m->mothurOut("You must provide either a shared or relabund file."); m->mothurOutEndLine(); abort = true; }
151 if ((relabundfile != "") && (sharedfile != "")) { m->mothurOut("You may not use both a shared and relabund file."); m->mothurOutEndLine(); abort = true; }
155 catch(exception& e) {
156 m->errorOut(e, "IndicatorCommand", "IndicatorCommand");
160 //**********************************************************************************************************************
162 void IndicatorCommand::help(){
164 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");
165 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");
166 m->mothurOut("The summary file lists the indicator value for each OTU for each node.\n");
167 m->mothurOut("The indicator command parameters are tree, groups, shared, relabund and label. The tree parameter is required as well as either shared or relabund.\n");
168 m->mothurOut("The groups parameter allows you to specify which of the groups in your shared or relabund you would like analyzed. The groups may be entered separated by dashes.\n");
169 m->mothurOut("The label parameter indicates at what distance your tree relates to the shared or relabund.\n");
170 m->mothurOut("The indicator command should be used in the following format: indicator(tree=test.tre, shared=test.shared, label=0.03)\n");
171 m->mothurOut("Note: No spaces between parameter labels (i.e. tree), '=' and parameters (i.e.yourTreefile).\n\n");
173 catch(exception& e) {
174 m->errorOut(e, "IndicatorCommand", "help");
179 //**********************************************************************************************************************
181 IndicatorCommand::~IndicatorCommand(){}
183 //**********************************************************************************************************************
185 int IndicatorCommand::execute(){
188 if (abort == true) { return 0; }
190 /***************************************************/
191 // use smart distancing to get right sharedRabund //
192 /***************************************************/
193 if (sharedfile != "") {
195 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
196 if (lookup[0] == NULL) { m->mothurOut("[ERROR] reading shared file."); m->mothurOutEndLine(); return 0; }
199 if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
200 if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
203 /***************************************************/
204 // reading tree info //
205 /***************************************************/
206 string groupfile = "";
207 Tree* tree = new Tree(treefile); delete tree; //extracts names from tree to make faked out groupmap
209 globaldata->setGroupFile(groupfile);
210 treeMap = new TreeMap();
211 bool mismatch = false;
212 for (int i = 0; i < globaldata->Treenames.size(); i++) {
213 //sanity check - is this a group that is not in the sharedfile?
214 if (!(m->inUsersGroups(globaldata->Treenames[i], globaldata->gGroupmap->namesOfGroups))) {
215 m->mothurOut("[ERROR]: " + globaldata->Treenames[i] + " is not a group in your shared or relabund file."); m->mothurOutEndLine();
218 treeMap->addSeq(globaldata->Treenames[i], "Group1");
221 if (mismatch) { //cleanup and exit
222 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
223 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
228 globaldata->gTreemap = treeMap;
230 read = new ReadNewickTree(treefile);
231 int readOk = read->read();
233 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); globaldata->gTree.clear(); delete globaldata->gTreemap; delete read; return 0; }
235 vector<Tree*> T = globaldata->gTree;
239 if (m->control_pressed) {
240 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
241 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
242 for (int i = 0; i < T.size(); i++) { delete T[i]; } globaldata->gTree.clear(); delete globaldata->gTreemap; return 0;
245 T[0]->assembleTree();
247 /***************************************************/
248 // create ouptut tree - respecting pickedGroups //
249 /***************************************************/
250 Tree* outputTree = new Tree(globaldata->Groups.size());
252 outputTree->getSubTree(T[0], globaldata->Groups);
253 outputTree->assembleTree();
255 //no longer need original tree, we have output tree to use and label
256 for (int i = 0; i < T.size(); i++) { delete T[i]; } globaldata->gTree.clear();
259 if (m->control_pressed) {
260 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
261 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
262 delete outputTree; delete globaldata->gTreemap; return 0;
265 /***************************************************/
266 // get indicator species values //
267 /***************************************************/
268 GetIndicatorSpecies(outputTree);
270 if (m->control_pressed) {
271 if (sharedfile != "") { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } }
272 else { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } }
273 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
274 delete outputTree; delete globaldata->gTreemap; return 0;
277 m->mothurOutEndLine();
278 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
279 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
280 m->mothurOutEndLine();
284 catch(exception& e) {
285 m->errorOut(e, "IndicatorCommand", "execute");
289 //**********************************************************************************************************************
290 //traverse tree finding indicator species values for each otu at each node
291 //label node with otu number that has highest indicator value
292 //report all otu values to file
293 int IndicatorCommand::GetIndicatorSpecies(Tree*& T){
296 string thisOutputDir = outputDir;
297 if (outputDir == "") { thisOutputDir += m->hasPath(inputFileName); }
298 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(inputFileName)) + "indicator.summary";
299 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
302 m->openOutputFile(outputFileName, out);
303 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
305 string treeOutputDir = outputDir;
306 if (outputDir == "") { treeOutputDir += m->hasPath(treefile); }
307 string outputTreeFileName = treeOutputDir + m->getRootName(m->getSimpleName(treefile)) + "indicator.tre";
310 //create a map from tree node index to names of descendants, save time later to know which sharedRabund you need
311 map<int, set<string> > nodeToDescendants;
312 map<int, set<int> > descendantNodes;
313 for (int i = 0; i < T->getNumNodes(); i++) {
314 if (m->control_pressed) { return 0; }
316 nodeToDescendants[i] = getDescendantList(T, i, nodeToDescendants, descendantNodes);
319 //you need the distances to leaf to decide grouping below
320 //this will also set branch lengths if the tree does not include them
321 map<int, float> distToLeaf = getLengthToLeaf(T);
324 for (int i = T->getNumLeaves(); i < T->getNumNodes(); i++) {
326 if (m->control_pressed) { out.close(); return 0; }
328 /*****************************************************/
329 //create vectors containing rabund info //
330 /*****************************************************/
332 vector<float> indicatorValues; //size of numBins
334 if (sharedfile != "") {
335 vector< vector<SharedRAbundVector*> > groupings;
337 //get nodes that will be a valid grouping
338 //you are valid if you are not one of my descendants
339 //AND your distToLeaf is <= mine
340 //AND your distToLeaf is >= my smallest childs
341 //AND you were not added as part of a larger groupings
343 set<string> groupsAlreadyAdded;
344 //create a grouping with my grouping
345 vector<SharedRAbundVector*> subset;
347 int doneCount = nodeToDescendants[i].size();
348 for (int k = 0; k < lookup.size(); k++) {
349 //is this descendant of i
350 if ((nodeToDescendants[i].count(lookup[k]->getGroup()) != 0)) {
351 subset.push_back(lookup[k]);
352 groupsAlreadyAdded.insert(lookup[k]->getGroup());
355 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
357 if (subset.size() != 0) { groupings.push_back(subset); }
360 for (int j = (T->getNumNodes()-1); j >= 0; j--) {
361 if ((descendantNodes[i].count(j) == 0) && (distToLeaf[j] <= distToLeaf[i]) && ((distToLeaf[j] >= distToLeaf[T->tree[i].getLChild()]) || (distToLeaf[j] >= distToLeaf[T->tree[i].getRChild()]))) {
362 vector<SharedRAbundVector*> subset;
364 int doneCount = nodeToDescendants[j].size();
365 for (int k = 0; k < lookup.size(); k++) {
366 //is this descendant of j, and we didn't already add this as part of a larger grouping
367 if ((nodeToDescendants[j].count(lookup[k]->getGroup()) != 0) && (groupsAlreadyAdded.count(lookup[k]->getGroup()) == 0)) {
368 subset.push_back(lookup[k]);
369 groupsAlreadyAdded.insert(lookup[k]->getGroup());
372 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
375 //if subset.size == 0 then the node was added as part of a larger grouping
376 if (subset.size() != 0) { groupings.push_back(subset); }
380 if (groupsAlreadyAdded.size() != lookup.size()) { cout << i << '\t' << groupsAlreadyAdded.size() << '\t' << lookup.size() << endl; m->mothurOut("[ERROR]: could not make proper groupings."); m->mothurOutEndLine(); }
381 for (int k = 0; k < lookup.size(); k++) {
382 if (groupsAlreadyAdded.count(lookup[k]->getGroup()) == 0) { cout << lookup[k]->getGroup() << endl; }
385 indicatorValues = getValues(groupings);
388 vector< vector<SharedRAbundFloatVector*> > groupings;
390 //get nodes that will be a valid grouping
391 //you are valid if you are not one of my descendants
392 //AND your distToLeaf is <= mine
393 //AND your distToLeaf is >= my smallest childs
394 //AND you were not added as part of a larger grouping
396 set<string> groupsAlreadyAdded;
397 //create a grouping with my grouping
398 vector<SharedRAbundFloatVector*> subset;
400 int doneCount = nodeToDescendants[i].size();
401 for (int k = 0; k < lookupFloat.size(); k++) {
402 //is this descendant of i
403 if ((nodeToDescendants[i].count(lookupFloat[k]->getGroup()) != 0)) {
404 subset.push_back(lookupFloat[k]);
405 groupsAlreadyAdded.insert(lookupFloat[k]->getGroup());
408 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
410 if (subset.size() != 0) { groupings.push_back(subset); }
412 for (int j = (T->getNumNodes()-1); j >= 0; j--) {
413 if ((descendantNodes[i].count(j) == 0) && (distToLeaf[j] <= distToLeaf[i]) && ((distToLeaf[j] >= distToLeaf[T->tree[i].getLChild()]) || (distToLeaf[j] >= distToLeaf[T->tree[i].getRChild()]))) {
414 vector<SharedRAbundFloatVector*> subset;
416 int doneCount = nodeToDescendants[j].size();
417 for (int k = 0; k < lookupFloat.size(); k++) {
418 //is this descendant of j, and we didn't already add this as part of a larger grouping
419 if ((nodeToDescendants[j].count(lookupFloat[k]->getGroup()) != 0) && (groupsAlreadyAdded.count(lookupFloat[k]->getGroup()) == 0)) {
420 subset.push_back(lookupFloat[k]);
421 groupsAlreadyAdded.insert(lookupFloat[k]->getGroup());
424 if (count == doneCount) { break; } //quit once you get the rabunds for this grouping
427 //if subset.size == 0 then the node was added as part of a larger grouping
428 if (subset.size() != 0) { groupings.push_back(subset); }
432 if (groupsAlreadyAdded.size() != lookupFloat.size()) { m->mothurOut("[ERROR]: could not make proper groupings."); m->mothurOutEndLine(); }
434 indicatorValues = getValues(groupings);
437 if (m->control_pressed) { out.close(); return 0; }
440 /******************************************************/
441 //output indicator values to table form + label tree //
442 /*****************************************************/
443 out << (i+1) << '\t';
444 for (int j = 0; j < indicatorValues.size(); j++) {
446 if (m->control_pressed) { out.close(); return 0; }
448 out << indicatorValues[j] << '\t';
452 T->tree[i].setLabel((i+1));
458 m->openOutputFile(outputTreeFileName, outTree);
459 outputNames.push_back(outputTreeFileName); outputTypes["tree"].push_back(outputTreeFileName);
461 T->print(outTree, "both");
466 catch(exception& e) {
467 m->errorOut(e, "IndicatorCommand", "GetIndicatorSpecies");
471 //**********************************************************************************************************************
472 vector<float> IndicatorCommand::getValues(vector< vector<SharedRAbundFloatVector*> >& groupings){
474 vector<float> values;
477 for (int i = 0; i < groupings[0][0]->getNumBins(); i++) {
479 if (m->control_pressed) { return values; }
482 float AijDenominator = 0.0;
484 //get overall abundance of each grouping
485 for (int j = 0; j < groupings.size(); j++) {
487 float totalAbund = 0;
489 for (int k = 0; k < groupings[j].size(); k++) {
490 totalAbund += groupings[j][k]->getAbundance(i);
491 if (groupings[j][k]->getAbundance(i) != 0) { numNotZero++; }
494 float Aij = (totalAbund / (float) groupings[j].size());
495 terms.push_back(Aij);
497 //percentage of sites represented
498 Bij.push_back(numNotZero / (float) groupings[j].size());
500 AijDenominator += Aij;
503 float maxIndVal = 0.0;
504 for (int j = 0; j < terms.size(); j++) {
505 float thisAij = (terms[j] / AijDenominator);
506 float thisValue = thisAij * Bij[j] * 100.0;
509 if (thisValue > maxIndVal) { maxIndVal = thisValue; }
512 values.push_back(maxIndVal);
517 catch(exception& e) {
518 m->errorOut(e, "IndicatorCommand", "getValues");
522 //**********************************************************************************************************************
523 //same as above, just data type difference
524 vector<float> IndicatorCommand::getValues(vector< vector<SharedRAbundVector*> >& groupings){
526 vector<float> values;
528 /*for (int j = 0; j < groupings.size(); j++) {
529 cout << "grouping " << j << endl;
530 for (int k = 0; k < groupings[j].size(); k++) {
531 cout << groupings[j][k]->getGroup() << endl;
535 for (int i = 0; i < groupings[0][0]->getNumBins(); i++) {
537 float AijDenominator = 0.0;
539 //get overall abundance of each grouping
540 for (int j = 0; j < groupings.size(); j++) {
542 int totalAbund = 0.0;
544 for (int k = 0; k < groupings[j].size(); k++) {
545 totalAbund += groupings[j][k]->getAbundance(i);
546 if (groupings[j][k]->getAbundance(i) != 0.0) { numNotZero++; }
550 float Aij = (totalAbund / (float) groupings[j].size());
551 terms.push_back(Aij);
553 //percentage of sites represented
554 Bij.push_back(numNotZero / (float) groupings[j].size());
556 AijDenominator += Aij;
559 float maxIndVal = 0.0;
560 for (int j = 0; j < terms.size(); j++) {
561 float thisAij = (terms[j] / AijDenominator);
562 float thisValue = thisAij * Bij[j] * 100.0;
565 if (thisValue > maxIndVal) { maxIndVal = thisValue; }
568 values.push_back(maxIndVal);
573 catch(exception& e) {
574 m->errorOut(e, "IndicatorCommand", "getValues");
578 //**********************************************************************************************************************
579 //you need the distances to leaf to decide groupings
580 //this will also set branch lengths if the tree does not include them
581 map<int, float> IndicatorCommand::getLengthToLeaf(Tree*& T){
583 map<int, float> dists;
585 bool hasBranchLengths = false;
586 for (int i = 0; i < T->getNumNodes(); i++) {
587 if (T->tree[i].getBranchLength() > 0.0) { hasBranchLengths = true; break; }
590 for (int i = 0; i < T->getNumNodes(); i++) {
592 int lc = T->tree[i].getLChild();
593 int rc = T->tree[i].getRChild();
595 //if you have no branch length, set it then calc
596 if (!hasBranchLengths) {
597 if (lc == -1) { // you are a leaf
598 //if you are a leaf set you priliminary length to 1.0, this may adjust later
599 T->tree[i].setBranchLength(1.0);
601 }else{ // you are an internal node
602 //look at your children's length to leaf
603 float ldist = dists[lc];
604 float rdist = dists[rc];
606 float greater = ldist;
607 if (rdist > greater) { greater = rdist; dists[i] = ldist + 1.0; }
608 else { dists[i] = rdist + 1.0; }
610 //branch length = difference + 1
611 T->tree[lc].setBranchLength((abs(ldist-greater) + 1.0));
612 T->tree[rc].setBranchLength((abs(rdist-greater) + 1.0));
616 if (lc == -1) { dists[i] = T->tree[i].getBranchLength(); }
617 else { //smaller of my two children distances plus my branch length
618 //look at your children's length to leaf
619 float ldist = dists[lc];
620 float rdist = dists[rc];
622 float smaller = ldist;
623 if (rdist < smaller) { smaller = rdist; }
625 dists[i] = smaller + T->tree[i].getBranchLength();
633 catch(exception& e) {
634 m->errorOut(e, "IndicatorCommand", "getLengthToLeaf");
638 //**********************************************************************************************************************
639 set<string> IndicatorCommand::getDescendantList(Tree*& T, int i, map<int, set<string> > descendants, map<int, set<int> >& nodes){
643 set<string>::iterator it;
645 int lc = T->tree[i].getLChild();
646 int rc = T->tree[i].getRChild();
648 if (lc == -1) { //you are a leaf your only descendant is yourself
649 set<int> temp; temp.insert(i);
650 names.insert(T->tree[i].getName());
652 }else{ //your descedants are the combination of your childrens descendants
653 names = descendants[lc];
654 nodes[i] = nodes[lc];
655 for (it = descendants[rc].begin(); it != descendants[rc].end(); it++) {
658 for (set<int>::iterator itNum = nodes[rc].begin(); itNum != nodes[rc].end(); itNum++) {
659 nodes[i].insert(*itNum);
661 //you are your own descendant
667 catch(exception& e) {
668 m->errorOut(e, "IndicatorCommand", "getDescendantList");
672 //**********************************************************************************************************************
673 int IndicatorCommand::getShared(){
675 InputData* input = new InputData(sharedfile, "sharedfile");
676 lookup = input->getSharedRAbundVectors();
677 string lastLabel = lookup[0]->getLabel();
679 if (label == "") { label = lastLabel; delete input; return 0; }
681 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
682 set<string> labels; labels.insert(label);
683 set<string> processedLabels;
684 set<string> userLabels = labels;
686 //as long as you are not at the end of the file or done wih the lines you want
687 while((lookup[0] != NULL) && (userLabels.size() != 0)) {
688 if (m->control_pressed) { delete input; return 0; }
690 if(labels.count(lookup[0]->getLabel()) == 1){
691 processedLabels.insert(lookup[0]->getLabel());
692 userLabels.erase(lookup[0]->getLabel());
696 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
697 string saveLabel = lookup[0]->getLabel();
699 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
700 lookup = input->getSharedRAbundVectors(lastLabel);
702 processedLabels.insert(lookup[0]->getLabel());
703 userLabels.erase(lookup[0]->getLabel());
705 //restore real lastlabel to save below
706 lookup[0]->setLabel(saveLabel);
710 lastLabel = lookup[0]->getLabel();
712 //get next line to process
713 //prevent memory leak
714 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
715 lookup = input->getSharedRAbundVectors();
719 if (m->control_pressed) { delete input; return 0; }
721 //output error messages about any remaining user labels
722 set<string>::iterator it;
723 bool needToRun = false;
724 for (it = userLabels.begin(); it != userLabels.end(); it++) {
725 m->mothurOut("Your file does not include the label " + *it);
726 if (processedLabels.count(lastLabel) != 1) {
727 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
730 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
734 //run last label if you need to
735 if (needToRun == true) {
736 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
737 lookup = input->getSharedRAbundVectors(lastLabel);
743 catch(exception& e) {
744 m->errorOut(e, "IndicatorCommand", "getShared");
748 //**********************************************************************************************************************
749 int IndicatorCommand::getSharedFloat(){
751 InputData* input = new InputData(relabundfile, "relabund");
752 lookupFloat = input->getSharedRAbundFloatVectors();
753 string lastLabel = lookupFloat[0]->getLabel();
755 if (label == "") { label = lastLabel; delete input; return 0; }
757 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
758 set<string> labels; labels.insert(label);
759 set<string> processedLabels;
760 set<string> userLabels = labels;
762 //as long as you are not at the end of the file or done wih the lines you want
763 while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
765 if (m->control_pressed) { delete input; return 0; }
767 if(labels.count(lookupFloat[0]->getLabel()) == 1){
768 processedLabels.insert(lookupFloat[0]->getLabel());
769 userLabels.erase(lookupFloat[0]->getLabel());
773 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
774 string saveLabel = lookupFloat[0]->getLabel();
776 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
777 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
779 processedLabels.insert(lookupFloat[0]->getLabel());
780 userLabels.erase(lookupFloat[0]->getLabel());
782 //restore real lastlabel to save below
783 lookupFloat[0]->setLabel(saveLabel);
787 lastLabel = lookupFloat[0]->getLabel();
789 //get next line to process
790 //prevent memory leak
791 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
792 lookupFloat = input->getSharedRAbundFloatVectors();
796 if (m->control_pressed) { delete input; return 0; }
798 //output error messages about any remaining user labels
799 set<string>::iterator it;
800 bool needToRun = false;
801 for (it = userLabels.begin(); it != userLabels.end(); it++) {
802 m->mothurOut("Your file does not include the label " + *it);
803 if (processedLabels.count(lastLabel) != 1) {
804 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
807 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
811 //run last label if you need to
812 if (needToRun == true) {
813 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
814 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
820 catch(exception& e) {
821 m->errorOut(e, "IndicatorCommand", "getShared");
825 /*****************************************************************/