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 = ""; }
165 sabundfile = validParameter.validFile(parameters, "sabund", true);
166 if (sabundfile == "not open") { abort = true; }
167 else if (sabundfile == "not found") { sabundfile = ""; }
169 rabundfile = validParameter.validFile(parameters, "rabund", true);
170 if (rabundfile == "not open") { abort = true; }
171 else if (rabundfile == "not found") { rabundfile = ""; }
173 groupfile = validParameter.validFile(parameters, "group", true);
174 if (groupfile == "not open") { groupfile = ""; abort = true; }
175 else if (groupfile == "not found") { groupfile = ""; }
177 sharedfile = validParameter.validFile(parameters, "shared", true);
178 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
179 else if (sharedfile == "not found") { sharedfile = ""; }
181 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
182 //is there are current file available for any of these?
183 //give priority to shared, then list, then rabund, then sabund
184 //if there is a current shared file, use it
185 sharedfile = m->getSharedFile();
186 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
188 listfile = m->getListFile();
189 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
191 rabundfile = m->getRabundFile();
192 if (rabundfile != "") { m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
194 sabundfile = m->getSabundFile();
195 if (sabundfile != "") { m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
197 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine();
205 groups = validParameter.validFile(parameters, "groups", false);
206 if (groups == "not found") { groups = "all"; }
207 m->splitAtDash(groups, Groups);
209 label = validParameter.validFile(parameters, "label", false);
210 if (label == "not found") { label = ""; }
212 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
213 else { allLines = 1; }
216 string temp = validParameter.validFile(parameters, "nseqs", false);
217 if (temp == "not found") { m->mothurOut("nseqs is a required parameter."); m->mothurOutEndLine(); abort = true; }
218 else { convert(temp, nseqs); }
220 temp = validParameter.validFile(parameters, "bygroup", false); if (temp == "not found") { temp = "f"; }
221 byGroup = m->isTrue(temp);
223 if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); }
225 if ((groupfile != "") && (listfile == "")) { m->mothurOut("A groupfile is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; }
229 catch(exception& e) {
230 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
234 //**********************************************************************************************************************
236 int RemoveRareCommand::execute(){
239 if (abort == true) { if (calledHelp) { return 0; } return 2; }
241 if (m->control_pressed) { return 0; }
243 //read through the correct file and output lines you want to keep
244 if (sabundfile != "") { processSabund(); }
245 if (rabundfile != "") { processRabund(); }
246 if (listfile != "") { processList(); }
247 if (sharedfile != "") { processShared(); }
249 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
251 if (outputNames.size() != 0) {
252 m->mothurOutEndLine();
253 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
254 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
255 m->mothurOutEndLine();
257 //set rabund file as new current rabundfile
259 itTypes = outputTypes.find("rabund");
260 if (itTypes != outputTypes.end()) {
261 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
264 itTypes = outputTypes.find("sabund");
265 if (itTypes != outputTypes.end()) {
266 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
269 itTypes = outputTypes.find("group");
270 if (itTypes != outputTypes.end()) {
271 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
274 itTypes = outputTypes.find("list");
275 if (itTypes != outputTypes.end()) {
276 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
279 itTypes = outputTypes.find("shared");
280 if (itTypes != outputTypes.end()) {
281 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
288 catch(exception& e) {
289 m->errorOut(e, "RemoveRareCommand", "execute");
294 //**********************************************************************************************************************
295 int RemoveRareCommand::processList(){
297 string thisOutputDir = outputDir;
298 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
299 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
300 string outputGroupFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
302 ofstream out, outGroup;
303 m->openOutputFile(outputFileName, out);
305 bool wroteSomething = false;
307 //you must provide a label because the names in the listfile need to be consistent
308 string thisLabel = "";
309 if (allLines) { m->mothurOut("For the listfile you must select one label, using first label in your listfile."); m->mothurOutEndLine(); }
310 else if (labels.size() > 1) { m->mothurOut("For the listfile you must select one label, using " + (*labels.begin()) + "."); m->mothurOutEndLine(); thisLabel = *labels.begin(); }
311 else { thisLabel = *labels.begin(); }
313 InputData input(listfile, "list");
314 ListVector* list = input.getListVector();
316 //get first one or the one we want
317 if (thisLabel != "") {
318 //use smart distancing
319 set<string> userLabels; userLabels.insert(thisLabel);
320 set<string> processedLabels;
321 string lastLabel = list->getLabel();
322 while((list != NULL) && (userLabels.size() != 0)) {
323 if(userLabels.count(list->getLabel()) == 1){
324 processedLabels.insert(list->getLabel());
325 userLabels.erase(list->getLabel());
329 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
330 processedLabels.insert(list->getLabel());
331 userLabels.erase(list->getLabel());
333 list = input.getListVector(lastLabel);
336 lastLabel = list->getLabel();
338 list = input.getListVector();
340 if (userLabels.size() != 0) {
341 m->mothurOut("Your file does not include the label " + thisLabel + ". I will use " + lastLabel + "."); m->mothurOutEndLine();
342 list = input.getListVector(lastLabel);
346 //if groupfile is given then use it
348 if (groupfile != "") {
349 groupMap = new GroupMap(groupfile); groupMap->readMap();
351 util.setGroups(Groups, groupMap->namesOfGroups);
352 m->openOutputFile(outputGroupFileName, outGroup);
357 //make a new list vector
359 newList.setLabel(list->getLabel());
362 for (int i = 0; i < list->getNumBins(); i++) {
363 if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); remove(outputGroupFileName.c_str()); } out.close(); remove(outputFileName.c_str()); return 0; }
365 //parse out names that are in accnos file
366 string binnames = list->get(i);
367 vector<string> names;
368 string saveBinNames = binnames;
369 m->splitAtComma(binnames, names);
371 vector<string> newGroupFile;
372 if (groupfile != "") {
373 vector<string> newNames;
375 for(int k = 0; k < names.size(); k++) {
376 string group = groupMap->getGroup(names[k]);
378 if (m->inUsersGroups(group, Groups)) {
379 newGroupFile.push_back(names[k] + "\t" + group);
381 newNames.push_back(names[k]);
382 saveBinNames += names[k] + ",";
386 saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
389 if (names.size() > nseqs) { //keep bin
390 newList.push_back(saveBinNames);
391 for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; }
395 //print new listvector
396 if (newList.getNumBins() != 0) {
397 wroteSomething = true;
403 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
405 if (wroteSomething == false) { m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine(); }
406 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
410 catch(exception& e) {
411 m->errorOut(e, "RemoveRareCommand", "processList");
415 //**********************************************************************************************************************
416 int RemoveRareCommand::processSabund(){
418 string thisOutputDir = outputDir;
419 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
420 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "pick" + m->getExtension(sabundfile);
421 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
424 m->openOutputFile(outputFileName, out);
426 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
427 InputData input(sabundfile, "sabund");
428 SAbundVector* sabund = input.getSAbundVector();
429 string lastLabel = sabund->getLabel();
430 set<string> processedLabels;
431 set<string> userLabels = labels;
433 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
435 if (m->control_pressed) { delete sabund; out.close(); return 0; }
437 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
439 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
440 processedLabels.insert(sabund->getLabel());
441 userLabels.erase(sabund->getLabel());
443 if (sabund->getMaxRank() > nseqs) {
444 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
445 }else { sabund->clear(); }
447 if (sabund->getNumBins() > 0) { sabund->print(out); }
450 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
451 string saveLabel = sabund->getLabel();
454 sabund = input.getSAbundVector(lastLabel);
456 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
457 processedLabels.insert(sabund->getLabel());
458 userLabels.erase(sabund->getLabel());
460 if (sabund->getMaxRank() > nseqs) {
461 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
462 }else { sabund->clear(); }
464 if (sabund->getNumBins() > 0) { sabund->print(out); }
466 //restore real lastlabel to save below
467 sabund->setLabel(saveLabel);
470 lastLabel = sabund->getLabel();
473 sabund = input.getSAbundVector();
476 if (m->control_pressed) { out.close(); return 0; }
478 //output error messages about any remaining user labels
479 set<string>::iterator it;
480 bool needToRun = false;
481 for (it = userLabels.begin(); it != userLabels.end(); it++) {
482 m->mothurOut("Your file does not include the label " + *it);
483 if (processedLabels.count(lastLabel) != 1) {
484 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
487 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
491 //run last label if you need to
492 if (needToRun == true) {
493 if (sabund != NULL) { delete sabund; }
494 sabund = input.getSAbundVector(lastLabel);
496 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
498 if (sabund->getMaxRank() > nseqs) {
499 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
500 }else { sabund->clear(); }
502 if (sabund->getNumBins() > 0) { sabund->print(out); }
509 catch(exception& e) {
510 m->errorOut(e, "RemoveRareCommand", "processSabund");
514 //**********************************************************************************************************************
515 int RemoveRareCommand::processRabund(){
517 string thisOutputDir = outputDir;
518 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
519 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" + m->getExtension(rabundfile);
520 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
523 m->openOutputFile(outputFileName, out);
525 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
526 InputData input(rabundfile, "rabund");
527 RAbundVector* rabund = input.getRAbundVector();
528 string lastLabel = rabund->getLabel();
529 set<string> processedLabels;
530 set<string> userLabels = labels;
532 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
534 if (m->control_pressed) { delete rabund; out.close(); return 0; }
536 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
538 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
539 processedLabels.insert(rabund->getLabel());
540 userLabels.erase(rabund->getLabel());
542 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
543 for (int i = 0; i < rabund->getNumBins(); i++) {
544 if (rabund->get(i) > nseqs) {
545 newRabund.push_back(rabund->get(i));
548 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
551 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
552 string saveLabel = rabund->getLabel();
555 rabund = input.getRAbundVector(lastLabel);
557 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
558 processedLabels.insert(rabund->getLabel());
559 userLabels.erase(rabund->getLabel());
561 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
562 for (int i = 0; i < rabund->getNumBins(); i++) {
563 if (rabund->get(i) > nseqs) {
564 newRabund.push_back(rabund->get(i));
567 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
569 //restore real lastlabel to save below
570 rabund->setLabel(saveLabel);
573 lastLabel = rabund->getLabel();
576 rabund = input.getRAbundVector();
579 if (m->control_pressed) { out.close(); return 0; }
581 //output error messages about any remaining user labels
582 set<string>::iterator it;
583 bool needToRun = false;
584 for (it = userLabels.begin(); it != userLabels.end(); it++) {
585 m->mothurOut("Your file does not include the label " + *it);
586 if (processedLabels.count(lastLabel) != 1) {
587 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
590 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
594 //run last label if you need to
595 if (needToRun == true) {
596 if (rabund != NULL) { delete rabund; }
597 rabund = input.getRAbundVector(lastLabel);
599 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
601 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
602 for (int i = 0; i < rabund->getNumBins(); i++) {
603 if (rabund->get(i) > nseqs) {
604 newRabund.push_back(rabund->get(i));
607 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
614 catch(exception& e) {
615 m->errorOut(e, "RemoveRareCommand", "processRabund");
619 //**********************************************************************************************************************
620 int RemoveRareCommand::processShared(){
624 string thisOutputDir = outputDir;
625 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
626 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "pick" + m->getExtension(sharedfile);
627 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
630 m->openOutputFile(outputFileName, out);
632 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
633 InputData input(sharedfile, "sharedfile");
634 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
635 string lastLabel = lookup[0]->getLabel();
636 set<string> processedLabels;
637 set<string> userLabels = labels;
639 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
641 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } out.close(); return 0; }
643 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
645 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
646 processedLabels.insert(lookup[0]->getLabel());
647 userLabels.erase(lookup[0]->getLabel());
649 processLookup(lookup, out);
652 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
653 string saveLabel = lookup[0]->getLabel();
655 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
656 lookup = input.getSharedRAbundVectors(lastLabel);
658 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
659 processedLabels.insert(lookup[0]->getLabel());
660 userLabels.erase(lookup[0]->getLabel());
662 processLookup(lookup, out);
664 //restore real lastlabel to save below
665 lookup[0]->setLabel(saveLabel);
668 lastLabel = lookup[0]->getLabel();
670 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
671 lookup = input.getSharedRAbundVectors();
674 if (m->control_pressed) { out.close(); return 0; }
676 //output error messages about any remaining user labels
677 set<string>::iterator it;
678 bool needToRun = false;
679 for (it = userLabels.begin(); it != userLabels.end(); it++) {
680 m->mothurOut("Your file does not include the label " + *it);
681 if (processedLabels.count(lastLabel) != 1) {
682 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
685 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
689 //run last label if you need to
690 if (needToRun == true) {
691 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
692 lookup = input.getSharedRAbundVectors(lastLabel);
694 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
696 processLookup(lookup, out);
698 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
703 catch(exception& e) {
704 m->errorOut(e, "RemoveRareCommand", "processSabund");
708 //**********************************************************************************************************************
709 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
712 vector<SharedRAbundVector> newRabunds; newRabunds.resize(lookup.size());
713 for (int i = 0; i < lookup.size(); i++) {
714 newRabunds[i].setGroup(lookup[i]->getGroup());
715 newRabunds[i].setLabel(lookup[i]->getLabel());
721 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
724 if (m->control_pressed) { return 0; }
727 for (int j = 0; j < lookup.size(); j++) {
730 if (lookup[j]->getAbundance(i) > nseqs) {
731 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
734 newRabunds[j].push_back(0, newRabunds[j].getGroup());
738 //eliminates zero otus
739 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
743 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
745 if (m->control_pressed) { return 0; }
748 //get total otu abundance
749 for (int j = 0; j < lookup.size(); j++) {
750 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
751 totalAbund += lookup[j]->getAbundance(i);
754 //eliminates otus below rare cutoff
755 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
759 //do we have any otus above the rare cutoff
760 if (newRabunds[0].getNumBins() != 0) {
761 for (int j = 0; j < newRabunds.size(); j++) {
762 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
763 newRabunds[j].print(out);
769 catch(exception& e) {
770 m->errorOut(e, "RemoveRareCommand", "processLookup");
774 //**********************************************************************************************************************