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", "atleast", "none","list",false,false,true); parameters.push_back(plist);
20 CommandParameter prabund("rabund", "InputTypes", "", "", "none", "atleast", "none","rabund",false,false,true); parameters.push_back(prabund);
21 CommandParameter psabund("sabund", "InputTypes", "", "", "none", "atleast", "none","sabund",false,false,true); parameters.push_back(psabund);
22 CommandParameter pshared("shared", "InputTypes", "", "", "none", "atleast", "none","shared",false,false,true); parameters.push_back(pshared);
23 CommandParameter pcount("count", "InputTypes", "", "", "CountGroup", "none", "none","count",false,false); parameters.push_back(pcount);
24 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none","group",false,false); parameters.push_back(pgroup);
25 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
26 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
27 CommandParameter pnseqs("nseqs", "Number", "", "0", "", "", "","",false,true,true); parameters.push_back(pnseqs);
28 CommandParameter pbygroup("bygroup", "Boolean", "", "f", "", "", "","",false,false); parameters.push_back(pbygroup);
29 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
30 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
32 vector<string> myArray;
33 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
37 m->errorOut(e, "RemoveRareCommand", "setParameters");
41 //**********************************************************************************************************************
42 string RemoveRareCommand::getHelpString(){
44 string helpString = "";
45 helpString += "The remove.rare command parameters are list, rabund, sabund, shared, group, count, label, groups, bygroup and nseqs.\n";
46 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";
47 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";
48 helpString += "The label parameter is used to analyze specific labels in your input. default=all. You may separate label names with dashes.\n";
49 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";
50 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";
51 helpString += "The nseqs parameter allows you to set the cutoff for an otu to be deemed rare. It is required.\n";
52 helpString += "The remove.rare command should be in the following format: remove.rare(shared=yourSharedFile, nseqs=yourRareCutoff).\n";
53 helpString += "Example remove.rare(shared=amazon.fn.shared, nseqs=2).\n";
54 helpString += "Note: No spaces between parameter labels (i.e. shared), '=' and parameters (i.e.yourSharedFile).\n";
58 m->errorOut(e, "RemoveRareCommand", "getHelpString");
62 //**********************************************************************************************************************
63 string RemoveRareCommand::getOutputPattern(string type) {
67 if (type == "rabund") { pattern = "[filename],pick,[extension]"; }
68 else if (type == "sabund") { pattern = "[filename],pick,[extension]"; }
69 else if (type == "group") { pattern = "[filename],pick,[extension]"; }
70 else if (type == "count") { pattern = "[filename],pick,[extension]"; }
71 else if (type == "list") { pattern = "[filename],[tag],pick,[extension]"; }
72 else if (type == "shared") { pattern = "[filename],[tag],pick,[extension]"; }
73 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
78 m->errorOut(e, "RemoveRareCommand", "getOutputPattern");
82 //**********************************************************************************************************************
83 RemoveRareCommand::RemoveRareCommand(){
85 abort = true; calledHelp = true;
87 vector<string> tempOutNames;
88 outputTypes["rabund"] = tempOutNames;
89 outputTypes["sabund"] = tempOutNames;
90 outputTypes["list"] = tempOutNames;
91 outputTypes["group"] = tempOutNames;
92 outputTypes["count"] = tempOutNames;
93 outputTypes["shared"] = tempOutNames;
96 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
100 //**********************************************************************************************************************
101 RemoveRareCommand::RemoveRareCommand(string option) {
103 abort = false; calledHelp = false;
106 //allow user to run help
107 if(option == "help") { help(); abort = true; calledHelp = true; }
108 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
111 vector<string> myArray = setParameters();
113 OptionParser parser(option);
114 map<string,string> parameters = parser.getParameters();
116 ValidParameters validParameter;
117 map<string,string>::iterator it;
119 //check to make sure all parameters are valid for command
120 for (it = parameters.begin(); it != parameters.end(); it++) {
121 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
124 //initialize outputTypes
125 vector<string> tempOutNames;
126 outputTypes["rabund"] = tempOutNames;
127 outputTypes["sabund"] = tempOutNames;
128 outputTypes["list"] = tempOutNames;
129 outputTypes["group"] = tempOutNames;
130 outputTypes["shared"] = tempOutNames;
131 outputTypes["count"] = tempOutNames;
133 //if the user changes the output directory command factory will send this info to us in the output parameter
134 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
136 //if the user changes the input directory command factory will send this info to us in the output parameter
137 string inputDir = validParameter.validFile(parameters, "inputdir", false);
138 if (inputDir == "not found"){ inputDir = ""; }
141 it = parameters.find("list");
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["list"] = inputDir + it->second; }
149 it = parameters.find("group");
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["group"] = inputDir + it->second; }
157 it = parameters.find("sabund");
158 //user has given a template file
159 if(it != parameters.end()){
160 path = m->hasPath(it->second);
161 //if the user has not given a path then, add inputdir. else leave path alone.
162 if (path == "") { parameters["sabund"] = inputDir + it->second; }
165 it = parameters.find("rabund");
166 //user has given a template file
167 if(it != parameters.end()){
168 path = m->hasPath(it->second);
169 //if the user has not given a path then, add inputdir. else leave path alone.
170 if (path == "") { parameters["rabund"] = inputDir + it->second; }
173 it = parameters.find("shared");
174 //user has given a template file
175 if(it != parameters.end()){
176 path = m->hasPath(it->second);
177 //if the user has not given a path then, add inputdir. else leave path alone.
178 if (path == "") { parameters["shared"] = inputDir + it->second; }
181 it = parameters.find("count");
182 //user has given a template file
183 if(it != parameters.end()){
184 path = m->hasPath(it->second);
185 //if the user has not given a path then, add inputdir. else leave path alone.
186 if (path == "") { parameters["count"] = inputDir + it->second; }
191 //check for file parameters
192 listfile = validParameter.validFile(parameters, "list", true);
193 if (listfile == "not open") { abort = true; }
194 else if (listfile == "not found") { listfile = ""; }
195 else { m->setListFile(listfile); }
197 sabundfile = validParameter.validFile(parameters, "sabund", true);
198 if (sabundfile == "not open") { abort = true; }
199 else if (sabundfile == "not found") { sabundfile = ""; }
200 else { m->setSabundFile(sabundfile); }
202 rabundfile = validParameter.validFile(parameters, "rabund", true);
203 if (rabundfile == "not open") { abort = true; }
204 else if (rabundfile == "not found") { rabundfile = ""; }
205 else { m->setRabundFile(rabundfile); }
207 groupfile = validParameter.validFile(parameters, "group", true);
208 if (groupfile == "not open") { groupfile = ""; abort = true; }
209 else if (groupfile == "not found") { groupfile = ""; }
210 else { m->setGroupFile(groupfile); }
212 sharedfile = validParameter.validFile(parameters, "shared", true);
213 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
214 else if (sharedfile == "not found") { sharedfile = ""; }
215 else { m->setSharedFile(sharedfile); }
217 countfile = validParameter.validFile(parameters, "count", true);
218 if (countfile == "not open") { countfile = ""; abort = true; }
219 else if (countfile == "not found") { countfile = ""; }
220 else { m->setCountTableFile(countfile); }
222 if ((groupfile != "") && (countfile != "")) {
223 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
226 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
227 //is there are current file available for any of these?
228 //give priority to shared, then list, then rabund, then sabund
229 //if there is a current shared file, use it
230 sharedfile = m->getSharedFile();
231 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
233 listfile = m->getListFile();
234 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
236 rabundfile = m->getRabundFile();
237 if (rabundfile != "") { m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
239 sabundfile = m->getSabundFile();
240 if (sabundfile != "") { m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
242 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine();
250 groups = validParameter.validFile(parameters, "groups", false);
251 if (groups == "not found") { groups = "all"; }
252 m->splitAtDash(groups, Groups);
254 label = validParameter.validFile(parameters, "label", false);
255 if (label == "not found") { label = ""; }
257 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
258 else { allLines = 1; }
261 string temp = validParameter.validFile(parameters, "nseqs", false);
262 if (temp == "not found") { m->mothurOut("nseqs is a required parameter."); m->mothurOutEndLine(); abort = true; }
263 else { m->mothurConvert(temp, nseqs); }
265 temp = validParameter.validFile(parameters, "bygroup", false); if (temp == "not found") { temp = "f"; }
266 byGroup = m->isTrue(temp);
268 if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); }
270 if (((groupfile != "") || (countfile != "")) && (listfile == "")) { m->mothurOut("A group or count file is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; countfile = ""; }
274 catch(exception& e) {
275 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
279 //**********************************************************************************************************************
281 int RemoveRareCommand::execute(){
284 if (abort == true) { if (calledHelp) { return 0; } return 2; }
286 if (m->control_pressed) { return 0; }
288 //read through the correct file and output lines you want to keep
289 if (sabundfile != "") { processSabund(); }
290 if (rabundfile != "") { processRabund(); }
291 if (listfile != "") { processList(); }
292 if (sharedfile != "") { processShared(); }
294 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
296 if (outputNames.size() != 0) {
297 m->mothurOutEndLine();
298 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
299 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
300 m->mothurOutEndLine();
302 //set rabund file as new current rabundfile
304 itTypes = outputTypes.find("rabund");
305 if (itTypes != outputTypes.end()) {
306 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
309 itTypes = outputTypes.find("sabund");
310 if (itTypes != outputTypes.end()) {
311 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
314 itTypes = outputTypes.find("group");
315 if (itTypes != outputTypes.end()) {
316 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
319 itTypes = outputTypes.find("list");
320 if (itTypes != outputTypes.end()) {
321 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
324 itTypes = outputTypes.find("shared");
325 if (itTypes != outputTypes.end()) {
326 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
329 itTypes = outputTypes.find("count");
330 if (itTypes != outputTypes.end()) {
331 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
338 catch(exception& e) {
339 m->errorOut(e, "RemoveRareCommand", "execute");
344 //**********************************************************************************************************************
345 int RemoveRareCommand::processList(){
348 //you must provide a label because the names in the listfile need to be consistent
349 string thisLabel = "";
350 if (allLines) { m->mothurOut("For the listfile you must select one label, using first label in your listfile."); m->mothurOutEndLine(); }
351 else if (labels.size() > 1) { m->mothurOut("For the listfile you must select one label, using " + (*labels.begin()) + "."); m->mothurOutEndLine(); thisLabel = *labels.begin(); }
352 else { thisLabel = *labels.begin(); }
354 InputData input(listfile, "list");
355 ListVector* list = input.getListVector();
357 //get first one or the one we want
358 if (thisLabel != "") {
359 //use smart distancing
360 set<string> userLabels; userLabels.insert(thisLabel);
361 set<string> processedLabels;
362 string lastLabel = list->getLabel();
363 while((list != NULL) && (userLabels.size() != 0)) {
364 if(userLabels.count(list->getLabel()) == 1){
365 processedLabels.insert(list->getLabel());
366 userLabels.erase(list->getLabel());
370 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
371 processedLabels.insert(list->getLabel());
372 userLabels.erase(list->getLabel());
374 list = input.getListVector(lastLabel);
377 lastLabel = list->getLabel();
379 list = input.getListVector();
381 if (userLabels.size() != 0) {
382 m->mothurOut("Your file does not include the label " + thisLabel + ". I will use " + lastLabel + "."); m->mothurOutEndLine();
383 list = input.getListVector(lastLabel);
387 string thisOutputDir = outputDir;
388 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
389 map<string, string> variables;
390 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
391 variables["[extension]"] = m->getExtension(listfile);
392 variables["[tag]"] = list->getLabel();
393 string outputFileName = getOutputFileName("list", variables);
394 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
395 variables["[extension]"] = m->getExtension(groupfile);
396 string outputGroupFileName = getOutputFileName("group", variables);
397 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
398 variables["[extension]"] = m->getExtension(countfile);
399 string outputCountFileName = getOutputFileName("count", variables);
401 ofstream out, outGroup;
402 m->openOutputFile(outputFileName, out);
404 bool wroteSomething = false;
407 //if groupfile is given then use it
410 if (groupfile != "") {
411 groupMap = new GroupMap(groupfile); groupMap->readMap();
413 vector<string> namesGroups = groupMap->getNamesOfGroups();
414 util.setGroups(Groups, namesGroups);
415 m->openOutputFile(outputGroupFileName, outGroup);
416 }else if (countfile != "") {
417 ct.readTable(countfile, true, false);
418 if (ct.hasGroupInfo()) {
419 vector<string> namesGroups = ct.getNamesOfGroups();
421 util.setGroups(Groups, namesGroups);
428 vector<string> binLabels = list->getLabels();
429 vector<string> newLabels;
431 //make a new list vector
433 newList.setLabel(list->getLabel());
436 for (int i = 0; i < list->getNumBins(); i++) {
437 if (m->control_pressed) { if (groupfile != "") { delete groupMap; outGroup.close(); m->mothurRemove(outputGroupFileName); } out.close(); m->mothurRemove(outputFileName); return 0; }
439 //parse out names that are in accnos file
440 string binnames = list->get(i);
441 vector<string> names;
442 string saveBinNames = binnames;
443 m->splitAtComma(binnames, names);
444 int binsize = names.size();
446 vector<string> newGroupFile;
447 if (groupfile != "") {
448 vector<string> newNames;
450 for(int k = 0; k < names.size(); k++) {
451 string group = groupMap->getGroup(names[k]);
453 if (m->inUsersGroups(group, Groups)) {
454 newGroupFile.push_back(names[k] + "\t" + group);
456 newNames.push_back(names[k]);
457 saveBinNames += names[k] + ",";
460 names = newNames; binsize = names.size();
461 saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
462 }else if (countfile != "") {
465 for(int k = 0; k < names.size(); k++) {
466 if (ct.hasGroupInfo()) {
467 vector<string> thisSeqsGroups = ct.getGroups(names[k]);
469 int thisSeqsCount = 0;
470 for (int n = 0; n < thisSeqsGroups.size(); n++) {
471 if (m->inUsersGroups(thisSeqsGroups[n], Groups)) {
472 thisSeqsCount += ct.getGroupCount(names[k], thisSeqsGroups[n]);
475 binsize += thisSeqsCount;
476 //if you don't have any seqs from the groups the user wants, then remove you.
477 if (thisSeqsCount == 0) { newGroupFile.push_back(names[k]); }
478 else { saveBinNames += names[k] + ","; }
480 binsize += ct.getNumSeqs(names[k]);
481 saveBinNames += names[k] + ",";
484 saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
487 if (binsize > nseqs) { //keep bin
488 newList.push_back(saveBinNames);
489 newLabels.push_back(binLabels[i]);
490 if (groupfile != "") { for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; } }
491 else if (countfile != "") { for(int k = 0; k < newGroupFile.size(); k++) { ct.remove(newGroupFile[k]); } }
492 }else { if (countfile != "") { for(int k = 0; k < names.size(); k++) { ct.remove(names[k]); } } }
495 //print new listvector
496 if (newList.getNumBins() != 0) {
497 wroteSomething = true;
498 newList.setLabels(newLabels);
499 newList.printHeaders(out);
505 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
506 if (countfile != "") {
507 if (ct.hasGroupInfo()) {
508 vector<string> allGroups = ct.getNamesOfGroups();
509 for (int i = 0; i < allGroups.size(); i++) {
510 if (!m->inUsersGroups(allGroups[i], Groups)) { ct.removeGroup(allGroups[i]); }
514 ct.printTable(outputCountFileName);
515 outputTypes["count"].push_back(outputCountFileName); outputNames.push_back(outputCountFileName);
518 if (wroteSomething == false) { m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine(); }
519 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
523 catch(exception& e) {
524 m->errorOut(e, "RemoveRareCommand", "processList");
528 //**********************************************************************************************************************
529 int RemoveRareCommand::processSabund(){
531 string thisOutputDir = outputDir;
532 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
533 map<string, string> variables;
534 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile));
535 variables["[extension]"] = m->getExtension(sabundfile);
536 string outputFileName = getOutputFileName("sabund", variables);
537 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
540 m->openOutputFile(outputFileName, out);
542 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
543 InputData input(sabundfile, "sabund");
544 SAbundVector* sabund = input.getSAbundVector();
545 string lastLabel = sabund->getLabel();
546 set<string> processedLabels;
547 set<string> userLabels = labels;
549 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
551 if (m->control_pressed) { delete sabund; out.close(); return 0; }
553 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
555 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
556 processedLabels.insert(sabund->getLabel());
557 userLabels.erase(sabund->getLabel());
559 if (sabund->getMaxRank() > nseqs) {
560 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
561 }else { sabund->clear(); }
563 if (sabund->getNumBins() > 0) { sabund->print(out); }
566 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
567 string saveLabel = sabund->getLabel();
570 sabund = input.getSAbundVector(lastLabel);
572 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
573 processedLabels.insert(sabund->getLabel());
574 userLabels.erase(sabund->getLabel());
576 if (sabund->getMaxRank() > nseqs) {
577 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
578 }else { sabund->clear(); }
580 if (sabund->getNumBins() > 0) { sabund->print(out); }
582 //restore real lastlabel to save below
583 sabund->setLabel(saveLabel);
586 lastLabel = sabund->getLabel();
589 sabund = input.getSAbundVector();
592 if (m->control_pressed) { out.close(); return 0; }
594 //output error messages about any remaining user labels
595 set<string>::iterator it;
596 bool needToRun = false;
597 for (it = userLabels.begin(); it != userLabels.end(); it++) {
598 m->mothurOut("Your file does not include the label " + *it);
599 if (processedLabels.count(lastLabel) != 1) {
600 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
603 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
607 //run last label if you need to
608 if (needToRun == true) {
609 if (sabund != NULL) { delete sabund; }
610 sabund = input.getSAbundVector(lastLabel);
612 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
614 if (sabund->getMaxRank() > nseqs) {
615 for(int i = 1; i <=nseqs; i++) { sabund->set(i, 0); }
616 }else { sabund->clear(); }
618 if (sabund->getNumBins() > 0) { sabund->print(out); }
625 catch(exception& e) {
626 m->errorOut(e, "RemoveRareCommand", "processSabund");
630 //**********************************************************************************************************************
631 int RemoveRareCommand::processRabund(){
633 string thisOutputDir = outputDir;
634 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
635 map<string, string> variables;
636 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile));
637 variables["[extension]"] = m->getExtension(rabundfile);
638 string outputFileName = getOutputFileName("rabund", variables);
639 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
642 m->openOutputFile(outputFileName, out);
644 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
645 InputData input(rabundfile, "rabund");
646 RAbundVector* rabund = input.getRAbundVector();
647 string lastLabel = rabund->getLabel();
648 set<string> processedLabels;
649 set<string> userLabels = labels;
651 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
653 if (m->control_pressed) { delete rabund; out.close(); return 0; }
655 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
657 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
658 processedLabels.insert(rabund->getLabel());
659 userLabels.erase(rabund->getLabel());
661 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
662 for (int i = 0; i < rabund->getNumBins(); i++) {
663 if (rabund->get(i) > nseqs) {
664 newRabund.push_back(rabund->get(i));
667 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
670 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
671 string saveLabel = rabund->getLabel();
674 rabund = input.getRAbundVector(lastLabel);
676 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
677 processedLabels.insert(rabund->getLabel());
678 userLabels.erase(rabund->getLabel());
680 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
681 for (int i = 0; i < rabund->getNumBins(); i++) {
682 if (rabund->get(i) > nseqs) {
683 newRabund.push_back(rabund->get(i));
686 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
688 //restore real lastlabel to save below
689 rabund->setLabel(saveLabel);
692 lastLabel = rabund->getLabel();
695 rabund = input.getRAbundVector();
698 if (m->control_pressed) { out.close(); return 0; }
700 //output error messages about any remaining user labels
701 set<string>::iterator it;
702 bool needToRun = false;
703 for (it = userLabels.begin(); it != userLabels.end(); it++) {
704 m->mothurOut("Your file does not include the label " + *it);
705 if (processedLabels.count(lastLabel) != 1) {
706 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
709 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
713 //run last label if you need to
714 if (needToRun == true) {
715 if (rabund != NULL) { delete rabund; }
716 rabund = input.getRAbundVector(lastLabel);
718 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
720 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
721 for (int i = 0; i < rabund->getNumBins(); i++) {
722 if (rabund->get(i) > nseqs) {
723 newRabund.push_back(rabund->get(i));
726 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
733 catch(exception& e) {
734 m->errorOut(e, "RemoveRareCommand", "processRabund");
738 //**********************************************************************************************************************
739 int RemoveRareCommand::processShared(){
741 m->setGroups(Groups);
743 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
744 InputData input(sharedfile, "sharedfile");
745 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
746 string lastLabel = lookup[0]->getLabel();
747 set<string> processedLabels;
748 set<string> userLabels = labels;
750 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
752 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
754 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
756 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
757 processedLabels.insert(lookup[0]->getLabel());
758 userLabels.erase(lookup[0]->getLabel());
760 processLookup(lookup);
763 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
764 string saveLabel = lookup[0]->getLabel();
766 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
767 lookup = input.getSharedRAbundVectors(lastLabel);
769 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
770 processedLabels.insert(lookup[0]->getLabel());
771 userLabels.erase(lookup[0]->getLabel());
773 processLookup(lookup);
775 //restore real lastlabel to save below
776 lookup[0]->setLabel(saveLabel);
779 lastLabel = lookup[0]->getLabel();
781 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
782 lookup = input.getSharedRAbundVectors();
785 if (m->control_pressed) { return 0; }
787 //output error messages about any remaining user labels
788 set<string>::iterator it;
789 bool needToRun = false;
790 for (it = userLabels.begin(); it != userLabels.end(); it++) {
791 m->mothurOut("Your file does not include the label " + *it);
792 if (processedLabels.count(lastLabel) != 1) {
793 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
796 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
800 //run last label if you need to
801 if (needToRun == true) {
802 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
803 lookup = input.getSharedRAbundVectors(lastLabel);
805 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
806 processLookup(lookup);
808 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
813 catch(exception& e) {
814 m->errorOut(e, "RemoveRareCommand", "processSabund");
818 //**********************************************************************************************************************
819 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup){
822 string thisOutputDir = outputDir;
823 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
824 map<string, string> variables;
825 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile));
826 variables["[extension]"] = m->getExtension(sharedfile);
827 variables["[tag]"] = lookup[0]->getLabel();
828 string outputFileName = getOutputFileName("shared", variables);
829 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
832 m->openOutputFile(outputFileName, out);
834 vector<SharedRAbundVector> newRabunds; newRabunds.resize(lookup.size());
835 vector<string> headers;
836 for (int i = 0; i < lookup.size(); i++) {
837 newRabunds[i].setGroup(lookup[i]->getGroup());
838 newRabunds[i].setLabel(lookup[i]->getLabel());
844 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
847 if (m->control_pressed) { out.close(); return 0; }
850 for (int j = 0; j < lookup.size(); j++) {
853 if (lookup[j]->getAbundance(i) > nseqs) {
854 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
857 newRabunds[j].push_back(0, newRabunds[j].getGroup());
861 //eliminates zero otus
862 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
863 else { headers.push_back(m->currentSharedBinLabels[i]); }
867 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
869 if (m->control_pressed) { out.close(); return 0; }
872 //get total otu abundance
873 for (int j = 0; j < lookup.size(); j++) {
874 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
875 totalAbund += lookup[j]->getAbundance(i);
878 //eliminates otus below rare cutoff
879 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) { newRabunds[j].pop_back(); } }
880 else { headers.push_back(m->currentSharedBinLabels[i]); }
884 //do we have any otus above the rare cutoff
885 if (newRabunds[0].getNumBins() != 0) {
886 out << "label\tGroup\tnumOtus\t";
887 for (int j = 0; j < headers.size(); j++) { out << headers[j] << '\t'; }
889 for (int j = 0; j < newRabunds.size(); j++) {
890 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
891 newRabunds[j].print(out);
899 catch(exception& e) {
900 m->errorOut(e, "RemoveRareCommand", "processLookup");
904 //**********************************************************************************************************************