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 { convert(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++) { remove(outputNames[i].c_str()); } 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 util.setGroups(Groups, groupMap->namesOfGroups);
357 m->openOutputFile(outputGroupFileName, outGroup);
362 //make a new list vector
364 newList.setLabel(list->getLabel());
367 for (int i = 0; i < list->getNumBins(); i++) {
368 if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); remove(outputGroupFileName.c_str()); } out.close(); remove(outputFileName.c_str()); return 0; }
370 //parse out names that are in accnos file
371 string binnames = list->get(i);
372 vector<string> names;
373 string saveBinNames = binnames;
374 m->splitAtComma(binnames, names);
376 vector<string> newGroupFile;
377 if (groupfile != "") {
378 vector<string> newNames;
380 for(int k = 0; k < names.size(); k++) {
381 string group = groupMap->getGroup(names[k]);
383 if (m->inUsersGroups(group, Groups)) {
384 newGroupFile.push_back(names[k] + "\t" + group);
386 newNames.push_back(names[k]);
387 saveBinNames += names[k] + ",";
391 saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
394 if (names.size() > nseqs) { //keep bin
395 newList.push_back(saveBinNames);
396 for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; }
400 //print new listvector
401 if (newList.getNumBins() != 0) {
402 wroteSomething = true;
408 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
410 if (wroteSomething == false) { m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine(); }
411 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
415 catch(exception& e) {
416 m->errorOut(e, "RemoveRareCommand", "processList");
420 //**********************************************************************************************************************
421 int RemoveRareCommand::processSabund(){
423 string thisOutputDir = outputDir;
424 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
425 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "pick" + m->getExtension(sabundfile);
426 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
429 m->openOutputFile(outputFileName, out);
431 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
432 InputData input(sabundfile, "sabund");
433 SAbundVector* sabund = input.getSAbundVector();
434 string lastLabel = sabund->getLabel();
435 set<string> processedLabels;
436 set<string> userLabels = labels;
438 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
440 if (m->control_pressed) { delete sabund; out.close(); return 0; }
442 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
444 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
445 processedLabels.insert(sabund->getLabel());
446 userLabels.erase(sabund->getLabel());
448 if (sabund->getMaxRank() > nseqs) {
449 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
450 }else { sabund->clear(); }
452 if (sabund->getNumBins() > 0) { sabund->print(out); }
455 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
456 string saveLabel = sabund->getLabel();
459 sabund = input.getSAbundVector(lastLabel);
461 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
462 processedLabels.insert(sabund->getLabel());
463 userLabels.erase(sabund->getLabel());
465 if (sabund->getMaxRank() > nseqs) {
466 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
467 }else { sabund->clear(); }
469 if (sabund->getNumBins() > 0) { sabund->print(out); }
471 //restore real lastlabel to save below
472 sabund->setLabel(saveLabel);
475 lastLabel = sabund->getLabel();
478 sabund = input.getSAbundVector();
481 if (m->control_pressed) { out.close(); return 0; }
483 //output error messages about any remaining user labels
484 set<string>::iterator it;
485 bool needToRun = false;
486 for (it = userLabels.begin(); it != userLabels.end(); it++) {
487 m->mothurOut("Your file does not include the label " + *it);
488 if (processedLabels.count(lastLabel) != 1) {
489 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
492 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
496 //run last label if you need to
497 if (needToRun == true) {
498 if (sabund != NULL) { delete sabund; }
499 sabund = input.getSAbundVector(lastLabel);
501 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
503 if (sabund->getMaxRank() > nseqs) {
504 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
505 }else { sabund->clear(); }
507 if (sabund->getNumBins() > 0) { sabund->print(out); }
514 catch(exception& e) {
515 m->errorOut(e, "RemoveRareCommand", "processSabund");
519 //**********************************************************************************************************************
520 int RemoveRareCommand::processRabund(){
522 string thisOutputDir = outputDir;
523 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
524 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" + m->getExtension(rabundfile);
525 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
528 m->openOutputFile(outputFileName, out);
530 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
531 InputData input(rabundfile, "rabund");
532 RAbundVector* rabund = input.getRAbundVector();
533 string lastLabel = rabund->getLabel();
534 set<string> processedLabels;
535 set<string> userLabels = labels;
537 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
539 if (m->control_pressed) { delete rabund; out.close(); return 0; }
541 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
543 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
544 processedLabels.insert(rabund->getLabel());
545 userLabels.erase(rabund->getLabel());
547 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
548 for (int i = 0; i < rabund->getNumBins(); i++) {
549 if (rabund->get(i) > nseqs) {
550 newRabund.push_back(rabund->get(i));
553 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
556 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
557 string saveLabel = rabund->getLabel();
560 rabund = input.getRAbundVector(lastLabel);
562 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
563 processedLabels.insert(rabund->getLabel());
564 userLabels.erase(rabund->getLabel());
566 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
567 for (int i = 0; i < rabund->getNumBins(); i++) {
568 if (rabund->get(i) > nseqs) {
569 newRabund.push_back(rabund->get(i));
572 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
574 //restore real lastlabel to save below
575 rabund->setLabel(saveLabel);
578 lastLabel = rabund->getLabel();
581 rabund = input.getRAbundVector();
584 if (m->control_pressed) { out.close(); return 0; }
586 //output error messages about any remaining user labels
587 set<string>::iterator it;
588 bool needToRun = false;
589 for (it = userLabels.begin(); it != userLabels.end(); it++) {
590 m->mothurOut("Your file does not include the label " + *it);
591 if (processedLabels.count(lastLabel) != 1) {
592 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
595 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
599 //run last label if you need to
600 if (needToRun == true) {
601 if (rabund != NULL) { delete rabund; }
602 rabund = input.getRAbundVector(lastLabel);
604 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
606 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
607 for (int i = 0; i < rabund->getNumBins(); i++) {
608 if (rabund->get(i) > nseqs) {
609 newRabund.push_back(rabund->get(i));
612 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
619 catch(exception& e) {
620 m->errorOut(e, "RemoveRareCommand", "processRabund");
624 //**********************************************************************************************************************
625 int RemoveRareCommand::processShared(){
629 string thisOutputDir = outputDir;
630 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
631 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "pick" + m->getExtension(sharedfile);
632 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
635 m->openOutputFile(outputFileName, out);
637 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
638 InputData input(sharedfile, "sharedfile");
639 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
640 string lastLabel = lookup[0]->getLabel();
641 set<string> processedLabels;
642 set<string> userLabels = labels;
644 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
646 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } out.close(); return 0; }
648 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
650 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
651 processedLabels.insert(lookup[0]->getLabel());
652 userLabels.erase(lookup[0]->getLabel());
654 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
655 processLookup(lookup, out);
658 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
659 string saveLabel = lookup[0]->getLabel();
661 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
662 lookup = input.getSharedRAbundVectors(lastLabel);
664 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
665 processedLabels.insert(lookup[0]->getLabel());
666 userLabels.erase(lookup[0]->getLabel());
668 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
669 processLookup(lookup, out);
671 //restore real lastlabel to save below
672 lookup[0]->setLabel(saveLabel);
675 lastLabel = lookup[0]->getLabel();
677 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
678 lookup = input.getSharedRAbundVectors();
681 if (m->control_pressed) { out.close(); return 0; }
683 //output error messages about any remaining user labels
684 set<string>::iterator it;
685 bool needToRun = false;
686 for (it = userLabels.begin(); it != userLabels.end(); it++) {
687 m->mothurOut("Your file does not include the label " + *it);
688 if (processedLabels.count(lastLabel) != 1) {
689 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
692 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
696 //run last label if you need to
697 if (needToRun == true) {
698 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
699 lookup = input.getSharedRAbundVectors(lastLabel);
701 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
703 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
704 processLookup(lookup, out);
706 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
711 catch(exception& e) {
712 m->errorOut(e, "RemoveRareCommand", "processSabund");
716 //**********************************************************************************************************************
717 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
720 vector<SharedRAbundVector> newRabunds; newRabunds.resize(lookup.size());
721 for (int i = 0; i < lookup.size(); i++) {
722 newRabunds[i].setGroup(lookup[i]->getGroup());
723 newRabunds[i].setLabel(lookup[i]->getLabel());
729 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
732 if (m->control_pressed) { return 0; }
735 for (int j = 0; j < lookup.size(); j++) {
738 if (lookup[j]->getAbundance(i) > nseqs) {
739 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
742 newRabunds[j].push_back(0, newRabunds[j].getGroup());
746 //eliminates zero otus
747 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
751 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
753 if (m->control_pressed) { return 0; }
756 //get total otu abundance
757 for (int j = 0; j < lookup.size(); j++) {
758 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
759 totalAbund += lookup[j]->getAbundance(i);
762 //eliminates otus below rare cutoff
763 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
767 //do we have any otus above the rare cutoff
768 if (newRabunds[0].getNumBins() != 0) {
769 for (int j = 0; j < newRabunds.size(); j++) {
770 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
771 newRabunds[j].print(out);
777 catch(exception& e) {
778 m->errorOut(e, "RemoveRareCommand", "processLookup");
782 //**********************************************************************************************************************