2 * removerarecommand.cpp
5 * Created by westcott on 1/21/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "removerarecommand.h"
11 #include "sequence.hpp"
13 #include "sharedutilities.h"
14 #include "inputdata.h"
16 //**********************************************************************************************************************
17 vector<string> RemoveRareCommand::setParameters(){
19 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plist);
20 CommandParameter prabund("rabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(prabund);
21 CommandParameter psabund("sabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psabund);
22 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pshared);
23 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
24 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
25 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
26 CommandParameter pnseqs("nseqs", "Number", "", "0", "", "", "",false,true); parameters.push_back(pnseqs);
27 CommandParameter pbygroup("bygroup", "Boolean", "", "f", "", "", "",false,true); parameters.push_back(pbygroup);
28 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
29 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
31 vector<string> myArray;
32 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
36 m->errorOut(e, "RemoveRareCommand", "setParameters");
40 //**********************************************************************************************************************
41 string RemoveRareCommand::getHelpString(){
43 string helpString = "";
44 helpString += "The remove.rare command parameters are list, rabund, sabund, shared, group, label, groups, bygroup and nseqs.\n";
45 helpString += "The remove.rare command reads one of the following file types: list, rabund, sabund or shared file. It outputs a new file after removing the rare otus.\n";
46 helpString += "The groups parameter allows you to specify which of the groups you would like analyzed. Default=all. You may separate group names with dashes.\n";
47 helpString += "The label parameter is used to analyze specific labels in your input. default=all. You may separate label names with dashes.\n";
48 helpString += "The bygroup parameter is only valid with the shared file. default=f, meaning remove any OTU that has nseqs or fewer sequences across all groups.\n";
49 helpString += "bygroups=T means remove any OTU that has nseqs or fewer sequences in each group (if groupA has 1 sequence and group B has 100 sequences in OTUZ and nseqs=1, then set the groupA count for OTUZ to 0 and keep groupB's count at 100.) \n";
50 helpString += "The nseqs parameter allows you to set the cutoff for an otu to be deemed rare. It is required.\n";
51 helpString += "The remove.rare command should be in the following format: remove.rare(shared=yourSharedFile, nseqs=yourRareCutoff).\n";
52 helpString += "Example remove.rare(shared=amazon.fn.shared, nseqs=2).\n";
53 helpString += "Note: No spaces between parameter labels (i.e. shared), '=' and parameters (i.e.yourSharedFile).\n";
57 m->errorOut(e, "RemoveRareCommand", "getHelpString");
61 //**********************************************************************************************************************
62 RemoveRareCommand::RemoveRareCommand(){
64 abort = true; calledHelp = true;
66 vector<string> tempOutNames;
67 outputTypes["rabund"] = tempOutNames;
68 outputTypes["sabund"] = tempOutNames;
69 outputTypes["list"] = tempOutNames;
70 outputTypes["group"] = tempOutNames;
71 outputTypes["shared"] = tempOutNames;
74 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
78 //**********************************************************************************************************************
79 RemoveRareCommand::RemoveRareCommand(string option) {
81 abort = false; calledHelp = false;
84 //allow user to run help
85 if(option == "help") { help(); abort = true; calledHelp = true; }
86 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
89 vector<string> myArray = setParameters();
91 OptionParser parser(option);
92 map<string,string> parameters = parser.getParameters();
94 ValidParameters validParameter;
95 map<string,string>::iterator it;
97 //check to make sure all parameters are valid for command
98 for (it = parameters.begin(); it != parameters.end(); it++) {
99 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
102 //initialize outputTypes
103 vector<string> tempOutNames;
104 outputTypes["rabund"] = tempOutNames;
105 outputTypes["sabund"] = tempOutNames;
106 outputTypes["list"] = tempOutNames;
107 outputTypes["group"] = tempOutNames;
108 outputTypes["shared"] = tempOutNames;
110 //if the user changes the output directory command factory will send this info to us in the output parameter
111 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
113 //if the user changes the input directory command factory will send this info to us in the output parameter
114 string inputDir = validParameter.validFile(parameters, "inputdir", false);
115 if (inputDir == "not found"){ inputDir = ""; }
118 it = parameters.find("list");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["list"] = inputDir + it->second; }
126 it = parameters.find("group");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["group"] = inputDir + it->second; }
134 it = parameters.find("sabund");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["sabund"] = inputDir + it->second; }
142 it = parameters.find("rabund");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["rabund"] = inputDir + it->second; }
150 it = parameters.find("shared");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["shared"] = inputDir + it->second; }
160 //check for file parameters
161 listfile = validParameter.validFile(parameters, "list", true);
162 if (listfile == "not open") { abort = true; }
163 else if (listfile == "not found") { listfile = ""; }
164 else { m->setListFile(listfile); }
166 sabundfile = validParameter.validFile(parameters, "sabund", true);
167 if (sabundfile == "not open") { abort = true; }
168 else if (sabundfile == "not found") { sabundfile = ""; }
169 else { m->setSabundFile(sabundfile); }
171 rabundfile = validParameter.validFile(parameters, "rabund", true);
172 if (rabundfile == "not open") { abort = true; }
173 else if (rabundfile == "not found") { rabundfile = ""; }
174 else { m->setRabundFile(rabundfile); }
176 groupfile = validParameter.validFile(parameters, "group", true);
177 if (groupfile == "not open") { groupfile = ""; abort = true; }
178 else if (groupfile == "not found") { groupfile = ""; }
179 else { m->setGroupFile(groupfile); }
181 sharedfile = validParameter.validFile(parameters, "shared", true);
182 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
183 else if (sharedfile == "not found") { sharedfile = ""; }
184 else { m->setSharedFile(sharedfile); }
186 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
187 //is there are current file available for any of these?
188 //give priority to shared, then list, then rabund, then sabund
189 //if there is a current shared file, use it
190 sharedfile = m->getSharedFile();
191 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
193 listfile = m->getListFile();
194 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
196 rabundfile = m->getRabundFile();
197 if (rabundfile != "") { m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
199 sabundfile = m->getSabundFile();
200 if (sabundfile != "") { m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
202 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine();
210 groups = validParameter.validFile(parameters, "groups", false);
211 if (groups == "not found") { groups = "all"; }
212 m->splitAtDash(groups, Groups);
214 label = validParameter.validFile(parameters, "label", false);
215 if (label == "not found") { label = ""; }
217 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
218 else { allLines = 1; }
221 string temp = validParameter.validFile(parameters, "nseqs", false);
222 if (temp == "not found") { m->mothurOut("nseqs is a required parameter."); m->mothurOutEndLine(); abort = true; }
223 else { m->mothurConvert(temp, nseqs); }
225 temp = validParameter.validFile(parameters, "bygroup", false); if (temp == "not found") { temp = "f"; }
226 byGroup = m->isTrue(temp);
228 if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); }
230 if ((groupfile != "") && (listfile == "")) { m->mothurOut("A groupfile is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; }
234 catch(exception& e) {
235 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
239 //**********************************************************************************************************************
241 int RemoveRareCommand::execute(){
244 if (abort == true) { if (calledHelp) { return 0; } return 2; }
246 if (m->control_pressed) { return 0; }
248 //read through the correct file and output lines you want to keep
249 if (sabundfile != "") { processSabund(); }
250 if (rabundfile != "") { processRabund(); }
251 if (listfile != "") { processList(); }
252 if (sharedfile != "") { processShared(); }
254 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
256 if (outputNames.size() != 0) {
257 m->mothurOutEndLine();
258 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
259 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
260 m->mothurOutEndLine();
262 //set rabund file as new current rabundfile
264 itTypes = outputTypes.find("rabund");
265 if (itTypes != outputTypes.end()) {
266 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
269 itTypes = outputTypes.find("sabund");
270 if (itTypes != outputTypes.end()) {
271 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
274 itTypes = outputTypes.find("group");
275 if (itTypes != outputTypes.end()) {
276 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
279 itTypes = outputTypes.find("list");
280 if (itTypes != outputTypes.end()) {
281 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
284 itTypes = outputTypes.find("shared");
285 if (itTypes != outputTypes.end()) {
286 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
293 catch(exception& e) {
294 m->errorOut(e, "RemoveRareCommand", "execute");
299 //**********************************************************************************************************************
300 int RemoveRareCommand::processList(){
302 string thisOutputDir = outputDir;
303 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
304 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
305 string outputGroupFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
307 ofstream out, outGroup;
308 m->openOutputFile(outputFileName, out);
310 bool wroteSomething = false;
312 //you must provide a label because the names in the listfile need to be consistent
313 string thisLabel = "";
314 if (allLines) { m->mothurOut("For the listfile you must select one label, using first label in your listfile."); m->mothurOutEndLine(); }
315 else if (labels.size() > 1) { m->mothurOut("For the listfile you must select one label, using " + (*labels.begin()) + "."); m->mothurOutEndLine(); thisLabel = *labels.begin(); }
316 else { thisLabel = *labels.begin(); }
318 InputData input(listfile, "list");
319 ListVector* list = input.getListVector();
321 //get first one or the one we want
322 if (thisLabel != "") {
323 //use smart distancing
324 set<string> userLabels; userLabels.insert(thisLabel);
325 set<string> processedLabels;
326 string lastLabel = list->getLabel();
327 while((list != NULL) && (userLabels.size() != 0)) {
328 if(userLabels.count(list->getLabel()) == 1){
329 processedLabels.insert(list->getLabel());
330 userLabels.erase(list->getLabel());
334 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
335 processedLabels.insert(list->getLabel());
336 userLabels.erase(list->getLabel());
338 list = input.getListVector(lastLabel);
341 lastLabel = list->getLabel();
343 list = input.getListVector();
345 if (userLabels.size() != 0) {
346 m->mothurOut("Your file does not include the label " + thisLabel + ". I will use " + lastLabel + "."); m->mothurOutEndLine();
347 list = input.getListVector(lastLabel);
351 //if groupfile is given then use it
353 if (groupfile != "") {
354 groupMap = new GroupMap(groupfile); groupMap->readMap();
356 vector<string> namesGroups = groupMap->getNamesOfGroups();
357 util.setGroups(Groups, namesGroups);
358 m->openOutputFile(outputGroupFileName, outGroup);
363 //make a new list vector
365 newList.setLabel(list->getLabel());
368 for (int i = 0; i < list->getNumBins(); i++) {
369 if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); m->mothurRemove(outputGroupFileName); } out.close(); m->mothurRemove(outputFileName); return 0; }
371 //parse out names that are in accnos file
372 string binnames = list->get(i);
373 vector<string> names;
374 string saveBinNames = binnames;
375 m->splitAtComma(binnames, names);
377 vector<string> newGroupFile;
378 if (groupfile != "") {
379 vector<string> newNames;
381 for(int k = 0; k < names.size(); k++) {
382 string group = groupMap->getGroup(names[k]);
384 if (m->inUsersGroups(group, Groups)) {
385 newGroupFile.push_back(names[k] + "\t" + group);
387 newNames.push_back(names[k]);
388 saveBinNames += names[k] + ",";
392 saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
395 if (names.size() > nseqs) { //keep bin
396 newList.push_back(saveBinNames);
397 for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; }
401 //print new listvector
402 if (newList.getNumBins() != 0) {
403 wroteSomething = true;
409 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
411 if (wroteSomething == false) { m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine(); }
412 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
416 catch(exception& e) {
417 m->errorOut(e, "RemoveRareCommand", "processList");
421 //**********************************************************************************************************************
422 int RemoveRareCommand::processSabund(){
424 string thisOutputDir = outputDir;
425 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
426 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "pick" + m->getExtension(sabundfile);
427 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
430 m->openOutputFile(outputFileName, out);
432 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
433 InputData input(sabundfile, "sabund");
434 SAbundVector* sabund = input.getSAbundVector();
435 string lastLabel = sabund->getLabel();
436 set<string> processedLabels;
437 set<string> userLabels = labels;
439 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
441 if (m->control_pressed) { delete sabund; out.close(); return 0; }
443 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
445 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
446 processedLabels.insert(sabund->getLabel());
447 userLabels.erase(sabund->getLabel());
449 if (sabund->getMaxRank() > nseqs) {
450 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
451 }else { sabund->clear(); }
453 if (sabund->getNumBins() > 0) { sabund->print(out); }
456 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
457 string saveLabel = sabund->getLabel();
460 sabund = input.getSAbundVector(lastLabel);
462 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
463 processedLabels.insert(sabund->getLabel());
464 userLabels.erase(sabund->getLabel());
466 if (sabund->getMaxRank() > nseqs) {
467 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
468 }else { sabund->clear(); }
470 if (sabund->getNumBins() > 0) { sabund->print(out); }
472 //restore real lastlabel to save below
473 sabund->setLabel(saveLabel);
476 lastLabel = sabund->getLabel();
479 sabund = input.getSAbundVector();
482 if (m->control_pressed) { out.close(); return 0; }
484 //output error messages about any remaining user labels
485 set<string>::iterator it;
486 bool needToRun = false;
487 for (it = userLabels.begin(); it != userLabels.end(); it++) {
488 m->mothurOut("Your file does not include the label " + *it);
489 if (processedLabels.count(lastLabel) != 1) {
490 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
493 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
497 //run last label if you need to
498 if (needToRun == true) {
499 if (sabund != NULL) { delete sabund; }
500 sabund = input.getSAbundVector(lastLabel);
502 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
504 if (sabund->getMaxRank() > nseqs) {
505 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
506 }else { sabund->clear(); }
508 if (sabund->getNumBins() > 0) { sabund->print(out); }
515 catch(exception& e) {
516 m->errorOut(e, "RemoveRareCommand", "processSabund");
520 //**********************************************************************************************************************
521 int RemoveRareCommand::processRabund(){
523 string thisOutputDir = outputDir;
524 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
525 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" + m->getExtension(rabundfile);
526 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
529 m->openOutputFile(outputFileName, out);
531 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
532 InputData input(rabundfile, "rabund");
533 RAbundVector* rabund = input.getRAbundVector();
534 string lastLabel = rabund->getLabel();
535 set<string> processedLabels;
536 set<string> userLabels = labels;
538 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
540 if (m->control_pressed) { delete rabund; out.close(); return 0; }
542 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
544 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
545 processedLabels.insert(rabund->getLabel());
546 userLabels.erase(rabund->getLabel());
548 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
549 for (int i = 0; i < rabund->getNumBins(); i++) {
550 if (rabund->get(i) > nseqs) {
551 newRabund.push_back(rabund->get(i));
554 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
557 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
558 string saveLabel = rabund->getLabel();
561 rabund = input.getRAbundVector(lastLabel);
563 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
564 processedLabels.insert(rabund->getLabel());
565 userLabels.erase(rabund->getLabel());
567 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
568 for (int i = 0; i < rabund->getNumBins(); i++) {
569 if (rabund->get(i) > nseqs) {
570 newRabund.push_back(rabund->get(i));
573 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
575 //restore real lastlabel to save below
576 rabund->setLabel(saveLabel);
579 lastLabel = rabund->getLabel();
582 rabund = input.getRAbundVector();
585 if (m->control_pressed) { out.close(); return 0; }
587 //output error messages about any remaining user labels
588 set<string>::iterator it;
589 bool needToRun = false;
590 for (it = userLabels.begin(); it != userLabels.end(); it++) {
591 m->mothurOut("Your file does not include the label " + *it);
592 if (processedLabels.count(lastLabel) != 1) {
593 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
596 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
600 //run last label if you need to
601 if (needToRun == true) {
602 if (rabund != NULL) { delete rabund; }
603 rabund = input.getRAbundVector(lastLabel);
605 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
607 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
608 for (int i = 0; i < rabund->getNumBins(); i++) {
609 if (rabund->get(i) > nseqs) {
610 newRabund.push_back(rabund->get(i));
613 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
620 catch(exception& e) {
621 m->errorOut(e, "RemoveRareCommand", "processRabund");
625 //**********************************************************************************************************************
626 int RemoveRareCommand::processShared(){
628 m->setGroups(Groups);
630 string thisOutputDir = outputDir;
631 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
632 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "pick" + m->getExtension(sharedfile);
633 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
636 m->openOutputFile(outputFileName, out);
638 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
639 InputData input(sharedfile, "sharedfile");
640 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
641 string lastLabel = lookup[0]->getLabel();
642 set<string> processedLabels;
643 set<string> userLabels = labels;
645 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
647 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } out.close(); return 0; }
649 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
651 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
652 processedLabels.insert(lookup[0]->getLabel());
653 userLabels.erase(lookup[0]->getLabel());
655 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
656 processLookup(lookup, out);
659 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
660 string saveLabel = lookup[0]->getLabel();
662 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
663 lookup = input.getSharedRAbundVectors(lastLabel);
665 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
666 processedLabels.insert(lookup[0]->getLabel());
667 userLabels.erase(lookup[0]->getLabel());
669 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
670 processLookup(lookup, out);
672 //restore real lastlabel to save below
673 lookup[0]->setLabel(saveLabel);
676 lastLabel = lookup[0]->getLabel();
678 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
679 lookup = input.getSharedRAbundVectors();
682 if (m->control_pressed) { out.close(); return 0; }
684 //output error messages about any remaining user labels
685 set<string>::iterator it;
686 bool needToRun = false;
687 for (it = userLabels.begin(); it != userLabels.end(); it++) {
688 m->mothurOut("Your file does not include the label " + *it);
689 if (processedLabels.count(lastLabel) != 1) {
690 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
693 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
697 //run last label if you need to
698 if (needToRun == true) {
699 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
700 lookup = input.getSharedRAbundVectors(lastLabel);
702 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
704 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
705 processLookup(lookup, out);
707 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
712 catch(exception& e) {
713 m->errorOut(e, "RemoveRareCommand", "processSabund");
717 //**********************************************************************************************************************
718 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
721 vector<SharedRAbundVector> newRabunds; newRabunds.resize(lookup.size());
722 for (int i = 0; i < lookup.size(); i++) {
723 newRabunds[i].setGroup(lookup[i]->getGroup());
724 newRabunds[i].setLabel(lookup[i]->getLabel());
730 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
733 if (m->control_pressed) { return 0; }
736 for (int j = 0; j < lookup.size(); j++) {
739 if (lookup[j]->getAbundance(i) > nseqs) {
740 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
743 newRabunds[j].push_back(0, newRabunds[j].getGroup());
747 //eliminates zero otus
748 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
752 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
754 if (m->control_pressed) { return 0; }
757 //get total otu abundance
758 for (int j = 0; j < lookup.size(); j++) {
759 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
760 totalAbund += lookup[j]->getAbundance(i);
763 //eliminates otus below rare cutoff
764 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
768 //do we have any otus above the rare cutoff
769 if (newRabunds[0].getNumBins() != 0) {
770 for (int j = 0; j < newRabunds.size(); j++) {
771 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
772 newRabunds[j].print(out);
778 catch(exception& e) {
779 m->errorOut(e, "RemoveRareCommand", "processLookup");
783 //**********************************************************************************************************************