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; }
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string,string> parameters = parser.getParameters();
93 ValidParameters validParameter;
94 map<string,string>::iterator it;
96 //check to make sure all parameters are valid for command
97 for (it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
101 //initialize outputTypes
102 vector<string> tempOutNames;
103 outputTypes["rabund"] = tempOutNames;
104 outputTypes["sabund"] = tempOutNames;
105 outputTypes["list"] = tempOutNames;
106 outputTypes["group"] = tempOutNames;
107 outputTypes["shared"] = tempOutNames;
109 //if the user changes the output directory command factory will send this info to us in the output parameter
110 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
112 //if the user changes the input directory command factory will send this info to us in the output parameter
113 string inputDir = validParameter.validFile(parameters, "inputdir", false);
114 if (inputDir == "not found"){ inputDir = ""; }
117 it = parameters.find("list");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["list"] = inputDir + it->second; }
125 it = parameters.find("group");
126 //user has given a template file
127 if(it != parameters.end()){
128 path = m->hasPath(it->second);
129 //if the user has not given a path then, add inputdir. else leave path alone.
130 if (path == "") { parameters["group"] = inputDir + it->second; }
133 it = parameters.find("sabund");
134 //user has given a template file
135 if(it != parameters.end()){
136 path = m->hasPath(it->second);
137 //if the user has not given a path then, add inputdir. else leave path alone.
138 if (path == "") { parameters["sabund"] = inputDir + it->second; }
141 it = parameters.find("rabund");
142 //user has given a template file
143 if(it != parameters.end()){
144 path = m->hasPath(it->second);
145 //if the user has not given a path then, add inputdir. else leave path alone.
146 if (path == "") { parameters["rabund"] = inputDir + it->second; }
149 it = parameters.find("shared");
150 //user has given a template file
151 if(it != parameters.end()){
152 path = m->hasPath(it->second);
153 //if the user has not given a path then, add inputdir. else leave path alone.
154 if (path == "") { parameters["shared"] = inputDir + it->second; }
159 //check for file parameters
160 listfile = validParameter.validFile(parameters, "list", true);
161 if (listfile == "not open") { abort = true; }
162 else if (listfile == "not found") { listfile = ""; }
164 sabundfile = validParameter.validFile(parameters, "sabund", true);
165 if (sabundfile == "not open") { abort = true; }
166 else if (sabundfile == "not found") { sabundfile = ""; }
168 rabundfile = validParameter.validFile(parameters, "rabund", true);
169 if (rabundfile == "not open") { abort = true; }
170 else if (rabundfile == "not found") { rabundfile = ""; }
172 groupfile = validParameter.validFile(parameters, "group", true);
173 if (groupfile == "not open") { groupfile = ""; abort = true; }
174 else if (groupfile == "not found") { groupfile = ""; }
176 sharedfile = validParameter.validFile(parameters, "shared", true);
177 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
178 else if (sharedfile == "not found") { sharedfile = ""; }
180 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
181 //is there are current file available for any of these?
182 //give priority to shared, then list, then rabund, then sabund
183 //if there is a current shared file, use it
184 sharedfile = m->getSharedFile();
185 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
187 listfile = m->getListFile();
188 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
190 rabundfile = m->getRabundFile();
191 if (rabundfile != "") { m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
193 sabundfile = m->getSabundFile();
194 if (sabundfile != "") { m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
196 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine();
204 groups = validParameter.validFile(parameters, "groups", false);
205 if (groups == "not found") { groups = "all"; }
206 m->splitAtDash(groups, Groups);
208 label = validParameter.validFile(parameters, "label", false);
209 if (label == "not found") { label = ""; }
211 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
212 else { allLines = 1; }
215 string temp = validParameter.validFile(parameters, "nseqs", false);
216 if (temp == "not found") { m->mothurOut("nseqs is a required parameter."); m->mothurOutEndLine(); abort = true; }
217 else { convert(temp, nseqs); }
219 temp = validParameter.validFile(parameters, "bygroup", false); if (temp == "not found") { temp = "f"; }
220 byGroup = m->isTrue(temp);
222 if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); }
224 if ((groupfile != "") && (listfile == "")) { m->mothurOut("A groupfile is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; }
228 catch(exception& e) {
229 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
233 //**********************************************************************************************************************
235 int RemoveRareCommand::execute(){
238 if (abort == true) { if (calledHelp) { return 0; } return 2; }
240 if (m->control_pressed) { return 0; }
242 //read through the correct file and output lines you want to keep
243 if (sabundfile != "") { processSabund(); }
244 if (rabundfile != "") { processRabund(); }
245 if (listfile != "") { processList(); }
246 if (sharedfile != "") { processShared(); }
248 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
250 if (outputNames.size() != 0) {
251 m->mothurOutEndLine();
252 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
253 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
254 m->mothurOutEndLine();
256 //set rabund file as new current rabundfile
258 itTypes = outputTypes.find("rabund");
259 if (itTypes != outputTypes.end()) {
260 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
263 itTypes = outputTypes.find("sabund");
264 if (itTypes != outputTypes.end()) {
265 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
268 itTypes = outputTypes.find("group");
269 if (itTypes != outputTypes.end()) {
270 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
273 itTypes = outputTypes.find("list");
274 if (itTypes != outputTypes.end()) {
275 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
278 itTypes = outputTypes.find("shared");
279 if (itTypes != outputTypes.end()) {
280 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
287 catch(exception& e) {
288 m->errorOut(e, "RemoveRareCommand", "execute");
293 //**********************************************************************************************************************
294 int RemoveRareCommand::processList(){
296 string thisOutputDir = outputDir;
297 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
298 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
299 string outputGroupFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
301 ofstream out, outGroup;
302 m->openOutputFile(outputFileName, out);
304 bool wroteSomething = false;
306 //you must provide a label because the names in the listfile need to be consistent
307 string thisLabel = "";
308 if (allLines) { m->mothurOut("For the listfile you must select one label, using first label in your listfile."); m->mothurOutEndLine(); }
309 else if (labels.size() > 1) { m->mothurOut("For the listfile you must select one label, using " + (*labels.begin()) + "."); m->mothurOutEndLine(); thisLabel = *labels.begin(); }
310 else { thisLabel = *labels.begin(); }
312 InputData input(listfile, "list");
313 ListVector* list = input.getListVector();
315 //get first one or the one we want
316 if (thisLabel != "") {
317 //use smart distancing
318 set<string> userLabels; userLabels.insert(thisLabel);
319 set<string> processedLabels;
320 string lastLabel = list->getLabel();
321 while((list != NULL) && (userLabels.size() != 0)) {
322 if(userLabels.count(list->getLabel()) == 1){
323 processedLabels.insert(list->getLabel());
324 userLabels.erase(list->getLabel());
328 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
329 processedLabels.insert(list->getLabel());
330 userLabels.erase(list->getLabel());
332 list = input.getListVector(lastLabel);
335 lastLabel = list->getLabel();
337 list = input.getListVector();
339 if (userLabels.size() != 0) {
340 m->mothurOut("Your file does not include the label " + thisLabel + ". I will use " + lastLabel + "."); m->mothurOutEndLine();
341 list = input.getListVector(lastLabel);
345 //if groupfile is given then use it
347 if (groupfile != "") {
348 groupMap = new GroupMap(groupfile); groupMap->readMap();
350 util.setGroups(Groups, groupMap->namesOfGroups);
351 m->openOutputFile(outputGroupFileName, outGroup);
356 //make a new list vector
358 newList.setLabel(list->getLabel());
361 for (int i = 0; i < list->getNumBins(); i++) {
362 if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); remove(outputGroupFileName.c_str()); } out.close(); remove(outputFileName.c_str()); return 0; }
364 //parse out names that are in accnos file
365 string binnames = list->get(i);
366 vector<string> names;
367 string saveBinNames = binnames;
368 m->splitAtComma(binnames, names);
370 vector<string> newGroupFile;
371 if (groupfile != "") {
372 vector<string> newNames;
374 for(int k = 0; k < names.size(); k++) {
375 string group = groupMap->getGroup(names[k]);
377 if (m->inUsersGroups(group, Groups)) {
378 newGroupFile.push_back(names[k] + "\t" + group);
380 newNames.push_back(names[k]);
381 saveBinNames += names[k] + ",";
385 saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
388 if (names.size() > nseqs) { //keep bin
389 newList.push_back(saveBinNames);
390 for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; }
394 //print new listvector
395 if (newList.getNumBins() != 0) {
396 wroteSomething = true;
402 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
404 if (wroteSomething == false) { m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine(); }
405 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
409 catch(exception& e) {
410 m->errorOut(e, "RemoveRareCommand", "processList");
414 //**********************************************************************************************************************
415 int RemoveRareCommand::processSabund(){
417 string thisOutputDir = outputDir;
418 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
419 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "pick" + m->getExtension(sabundfile);
420 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
423 m->openOutputFile(outputFileName, out);
425 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
426 InputData input(sabundfile, "sabund");
427 SAbundVector* sabund = input.getSAbundVector();
428 string lastLabel = sabund->getLabel();
429 set<string> processedLabels;
430 set<string> userLabels = labels;
432 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
434 if (m->control_pressed) { delete sabund; out.close(); return 0; }
436 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
438 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
439 processedLabels.insert(sabund->getLabel());
440 userLabels.erase(sabund->getLabel());
442 if (sabund->getMaxRank() > nseqs) {
443 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
444 }else { sabund->clear(); }
446 if (sabund->getNumBins() > 0) { sabund->print(out); }
449 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
450 string saveLabel = sabund->getLabel();
453 sabund = input.getSAbundVector(lastLabel);
455 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
456 processedLabels.insert(sabund->getLabel());
457 userLabels.erase(sabund->getLabel());
459 if (sabund->getMaxRank() > nseqs) {
460 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
461 }else { sabund->clear(); }
463 if (sabund->getNumBins() > 0) { sabund->print(out); }
465 //restore real lastlabel to save below
466 sabund->setLabel(saveLabel);
469 lastLabel = sabund->getLabel();
472 sabund = input.getSAbundVector();
475 if (m->control_pressed) { out.close(); return 0; }
477 //output error messages about any remaining user labels
478 set<string>::iterator it;
479 bool needToRun = false;
480 for (it = userLabels.begin(); it != userLabels.end(); it++) {
481 m->mothurOut("Your file does not include the label " + *it);
482 if (processedLabels.count(lastLabel) != 1) {
483 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
486 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
490 //run last label if you need to
491 if (needToRun == true) {
492 if (sabund != NULL) { delete sabund; }
493 sabund = input.getSAbundVector(lastLabel);
495 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
497 if (sabund->getMaxRank() > nseqs) {
498 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
499 }else { sabund->clear(); }
501 if (sabund->getNumBins() > 0) { sabund->print(out); }
508 catch(exception& e) {
509 m->errorOut(e, "RemoveRareCommand", "processSabund");
513 //**********************************************************************************************************************
514 int RemoveRareCommand::processRabund(){
516 string thisOutputDir = outputDir;
517 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
518 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" + m->getExtension(rabundfile);
519 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
522 m->openOutputFile(outputFileName, out);
524 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
525 InputData input(rabundfile, "rabund");
526 RAbundVector* rabund = input.getRAbundVector();
527 string lastLabel = rabund->getLabel();
528 set<string> processedLabels;
529 set<string> userLabels = labels;
531 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
533 if (m->control_pressed) { delete rabund; out.close(); return 0; }
535 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
537 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
538 processedLabels.insert(rabund->getLabel());
539 userLabels.erase(rabund->getLabel());
541 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
542 for (int i = 0; i < rabund->getNumBins(); i++) {
543 if (rabund->get(i) > nseqs) {
544 newRabund.push_back(rabund->get(i));
547 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
550 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
551 string saveLabel = rabund->getLabel();
554 rabund = input.getRAbundVector(lastLabel);
556 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
557 processedLabels.insert(rabund->getLabel());
558 userLabels.erase(rabund->getLabel());
560 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
561 for (int i = 0; i < rabund->getNumBins(); i++) {
562 if (rabund->get(i) > nseqs) {
563 newRabund.push_back(rabund->get(i));
566 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
568 //restore real lastlabel to save below
569 rabund->setLabel(saveLabel);
572 lastLabel = rabund->getLabel();
575 rabund = input.getRAbundVector();
578 if (m->control_pressed) { out.close(); return 0; }
580 //output error messages about any remaining user labels
581 set<string>::iterator it;
582 bool needToRun = false;
583 for (it = userLabels.begin(); it != userLabels.end(); it++) {
584 m->mothurOut("Your file does not include the label " + *it);
585 if (processedLabels.count(lastLabel) != 1) {
586 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
589 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
593 //run last label if you need to
594 if (needToRun == true) {
595 if (rabund != NULL) { delete rabund; }
596 rabund = input.getRAbundVector(lastLabel);
598 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
600 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
601 for (int i = 0; i < rabund->getNumBins(); i++) {
602 if (rabund->get(i) > nseqs) {
603 newRabund.push_back(rabund->get(i));
606 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
613 catch(exception& e) {
614 m->errorOut(e, "RemoveRareCommand", "processRabund");
618 //**********************************************************************************************************************
619 int RemoveRareCommand::processShared(){
623 string thisOutputDir = outputDir;
624 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
625 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "pick" + m->getExtension(sharedfile);
626 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
629 m->openOutputFile(outputFileName, out);
631 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
632 InputData input(sharedfile, "sharedfile");
633 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
634 string lastLabel = lookup[0]->getLabel();
635 set<string> processedLabels;
636 set<string> userLabels = labels;
638 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
640 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } out.close(); return 0; }
642 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
644 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
645 processedLabels.insert(lookup[0]->getLabel());
646 userLabels.erase(lookup[0]->getLabel());
648 processLookup(lookup, out);
651 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
652 string saveLabel = lookup[0]->getLabel();
654 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
655 lookup = input.getSharedRAbundVectors(lastLabel);
657 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
658 processedLabels.insert(lookup[0]->getLabel());
659 userLabels.erase(lookup[0]->getLabel());
661 processLookup(lookup, out);
663 //restore real lastlabel to save below
664 lookup[0]->setLabel(saveLabel);
667 lastLabel = lookup[0]->getLabel();
669 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
670 lookup = input.getSharedRAbundVectors();
673 if (m->control_pressed) { out.close(); return 0; }
675 //output error messages about any remaining user labels
676 set<string>::iterator it;
677 bool needToRun = false;
678 for (it = userLabels.begin(); it != userLabels.end(); it++) {
679 m->mothurOut("Your file does not include the label " + *it);
680 if (processedLabels.count(lastLabel) != 1) {
681 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
684 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
688 //run last label if you need to
689 if (needToRun == true) {
690 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
691 lookup = input.getSharedRAbundVectors(lastLabel);
693 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
695 processLookup(lookup, out);
697 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
702 catch(exception& e) {
703 m->errorOut(e, "RemoveRareCommand", "processSabund");
707 //**********************************************************************************************************************
708 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
711 vector<SharedRAbundVector> newRabunds; newRabunds.resize(lookup.size());
712 for (int i = 0; i < lookup.size(); i++) {
713 newRabunds[i].setGroup(lookup[i]->getGroup());
714 newRabunds[i].setLabel(lookup[i]->getLabel());
720 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
723 if (m->control_pressed) { return 0; }
726 for (int j = 0; j < lookup.size(); j++) {
729 if (lookup[j]->getAbundance(i) > nseqs) {
730 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
733 newRabunds[j].push_back(0, newRabunds[j].getGroup());
737 //eliminates zero otus
738 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
742 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
744 if (m->control_pressed) { return 0; }
747 //get total otu abundance
748 for (int j = 0; j < lookup.size(); j++) {
749 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
750 totalAbund += lookup[j]->getAbundance(i);
753 //eliminates otus below rare cutoff
754 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
758 //do we have any otus above the rare cutoff
759 if (newRabunds[0].getNumBins() != 0) {
760 for (int j = 0; j < newRabunds.size(); j++) {
761 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
762 newRabunds[j].print(out);
768 catch(exception& e) {
769 m->errorOut(e, "RemoveRareCommand", "processLookup");
773 //**********************************************************************************************************************