]> git.donarmstrong.com Git - mothur.git/blob - removerarecommand.cpp
added classify.shared command and random forest files. added count file to pcr.seqs...
[mothur.git] / removerarecommand.cpp
1 /*
2  *  removerarecommand.cpp
3  *  mothur
4  *
5  *  Created by westcott on 1/21/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "removerarecommand.h"
11 #include "sequence.hpp"
12 #include "groupmap.h"
13 #include "sharedutilities.h"
14 #include "inputdata.h"
15
16 //**********************************************************************************************************************
17 vector<string> RemoveRareCommand::setParameters(){      
18         try {
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 pcount("count", "InputTypes", "", "", "CountGroup", "none", "none",false,false); parameters.push_back(pcount);
24                 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none",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); parameters.push_back(pnseqs);
28                 CommandParameter pbygroup("bygroup", "Boolean", "", "f", "", "", "",false,true); 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);
31                 
32                 vector<string> myArray;
33                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
34                 return myArray;
35         }
36         catch(exception& e) {
37                 m->errorOut(e, "RemoveRareCommand", "setParameters");
38                 exit(1);
39         }
40 }
41 //**********************************************************************************************************************
42 string RemoveRareCommand::getHelpString(){      
43         try {
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";
55                 return helpString;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "RemoveRareCommand", "getHelpString");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63 string RemoveRareCommand::getOutputFileNameTag(string type, string inputName=""){       
64         try {
65         string outputFileName = "";
66                 map<string, vector<string> >::iterator it;
67         
68         //is this a type this command creates
69         it = outputTypes.find(type);
70         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
71         else {
72             if (type == "rabund")            {   outputFileName =  "pick" + m->getExtension(inputName);   }
73             else if (type == "sabund")    {   outputFileName =  "pick" + m->getExtension(inputName);   }
74             else if (type == "shared")        {   outputFileName =  "pick" + m->getExtension(inputName);   }
75             else if (type == "group")       {   outputFileName =  "pick" + m->getExtension(inputName);   }
76             else if (type == "count")       {   outputFileName =  "pick" + m->getExtension(inputName);   }
77             else if (type == "list")        {   outputFileName =  "pick" + m->getExtension(inputName);   }
78             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
79         }
80         return outputFileName;
81         }
82         catch(exception& e) {
83                 m->errorOut(e, "RemoveRareCommand", "getOutputFileNameTag");
84                 exit(1);
85         }
86 }
87
88 //**********************************************************************************************************************
89 RemoveRareCommand::RemoveRareCommand(){ 
90         try {
91                 abort = true; calledHelp = true; 
92                 setParameters();
93                 vector<string> tempOutNames;
94                 outputTypes["rabund"] = tempOutNames;
95                 outputTypes["sabund"] = tempOutNames;
96                 outputTypes["list"] = tempOutNames;
97                 outputTypes["group"] = tempOutNames;
98         outputTypes["count"] = tempOutNames;
99                 outputTypes["shared"] = tempOutNames;
100         }
101         catch(exception& e) {
102                 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
103                 exit(1);
104         }
105 }
106 //**********************************************************************************************************************
107 RemoveRareCommand::RemoveRareCommand(string option)  {
108         try {
109                 abort = false; calledHelp = false;   
110                 allLines = 1;
111                 
112                 //allow user to run help
113                 if(option == "help") { help(); abort = true; calledHelp = true; }
114                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
115                 
116                 else {
117                         vector<string> myArray = setParameters();
118                         
119                         OptionParser parser(option);
120                         map<string,string> parameters = parser.getParameters();
121                         
122                         ValidParameters validParameter;
123                         map<string,string>::iterator it;
124                         
125                         //check to make sure all parameters are valid for command
126                         for (it = parameters.begin(); it != parameters.end(); it++) { 
127                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
128                         }
129                         
130                         //initialize outputTypes
131                         vector<string> tempOutNames;
132                         outputTypes["rabund"] = tempOutNames;
133                         outputTypes["sabund"] = tempOutNames;
134                         outputTypes["list"] = tempOutNames;
135                         outputTypes["group"] = tempOutNames;
136                         outputTypes["shared"] = tempOutNames;   
137             outputTypes["count"] = tempOutNames;
138                         
139                         //if the user changes the output directory command factory will send this info to us in the output parameter 
140                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
141                         
142                         //if the user changes the input directory command factory will send this info to us in the output parameter 
143                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
144                         if (inputDir == "not found"){   inputDir = "";          }
145                         else {
146                                 string path;
147                                 it = parameters.find("list");
148                                 //user has given a template file
149                                 if(it != parameters.end()){ 
150                                         path = m->hasPath(it->second);
151                                         //if the user has not given a path then, add inputdir. else leave path alone.
152                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
153                                 }
154                                 
155                                 it = parameters.find("group");
156                                 //user has given a template file
157                                 if(it != parameters.end()){ 
158                                         path = m->hasPath(it->second);
159                                         //if the user has not given a path then, add inputdir. else leave path alone.
160                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
161                                 }
162                                 
163                                 it = parameters.find("sabund");
164                                 //user has given a template file
165                                 if(it != parameters.end()){ 
166                                         path = m->hasPath(it->second);
167                                         //if the user has not given a path then, add inputdir. else leave path alone.
168                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
169                                 }
170                                 
171                                 it = parameters.find("rabund");
172                                 //user has given a template file
173                                 if(it != parameters.end()){ 
174                                         path = m->hasPath(it->second);
175                                         //if the user has not given a path then, add inputdir. else leave path alone.
176                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
177                                 }
178                                 
179                                 it = parameters.find("shared");
180                                 //user has given a template file
181                                 if(it != parameters.end()){ 
182                                         path = m->hasPath(it->second);
183                                         //if the user has not given a path then, add inputdir. else leave path alone.
184                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
185                                 }
186                 
187                 it = parameters.find("count");
188                                 //user has given a template file
189                                 if(it != parameters.end()){ 
190                                         path = m->hasPath(it->second);
191                                         //if the user has not given a path then, add inputdir. else leave path alone.
192                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
193                                 }
194                         }
195                         
196                         
197                         //check for file parameters
198                         listfile = validParameter.validFile(parameters, "list", true);
199                         if (listfile == "not open") { abort = true; }
200                         else if (listfile == "not found") {  listfile = "";  }
201                         else { m->setListFile(listfile); }
202                         
203                         sabundfile = validParameter.validFile(parameters, "sabund", true);
204                         if (sabundfile == "not open") { abort = true; }
205                         else if (sabundfile == "not found") {  sabundfile = "";  }      
206                         else { m->setSabundFile(sabundfile); }
207                         
208                         rabundfile = validParameter.validFile(parameters, "rabund", true);
209                         if (rabundfile == "not open") { abort = true; }
210                         else if (rabundfile == "not found") {  rabundfile = "";  }                              
211                         else { m->setRabundFile(rabundfile); }
212                         
213                         groupfile = validParameter.validFile(parameters, "group", true);
214                         if (groupfile == "not open") { groupfile = ""; abort = true; }
215                         else if (groupfile == "not found") {  groupfile = "";  }        
216                         else { m->setGroupFile(groupfile); }
217                         
218                         sharedfile = validParameter.validFile(parameters, "shared", true);
219                         if (sharedfile == "not open") { sharedfile = "";  abort = true; }
220                         else if (sharedfile == "not found") {  sharedfile = "";  }
221                         else { m->setSharedFile(sharedfile); }
222             
223             countfile = validParameter.validFile(parameters, "count", true);
224                         if (countfile == "not open") { countfile = ""; abort = true; }
225                         else if (countfile == "not found") { countfile = "";  } 
226                         else { m->setCountTableFile(countfile); }
227                                 
228             if ((groupfile != "") && (countfile != "")) {
229                 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
230             }
231                         
232                         if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
233                                 //is there are current file available for any of these?
234                                 //give priority to shared, then list, then rabund, then sabund
235                                 //if there is a current shared file, use it
236                                 sharedfile = m->getSharedFile(); 
237                                 if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
238                                 else { 
239                                         listfile = m->getListFile(); 
240                                         if (listfile != "") {  m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
241                                         else { 
242                                                 rabundfile = m->getRabundFile(); 
243                                                 if (rabundfile != "") {  m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
244                                                 else { 
245                                                         sabundfile = m->getSabundFile(); 
246                                                         if (sabundfile != "") {  m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
247                                                         else { 
248                                                                 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine(); 
249                                                                 abort = true;
250                                                         }
251                                                 }
252                                         }
253                                 }
254                         } 
255                         
256                         groups = validParameter.validFile(parameters, "groups", false);                 
257                         if (groups == "not found") { groups = "all"; }
258                         m->splitAtDash(groups, Groups);
259                         
260                         label = validParameter.validFile(parameters, "label", false);                   
261                         if (label == "not found") { label = ""; }
262                         else { 
263                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
264                                 else { allLines = 1;  }
265                         }
266                         
267                         string temp = validParameter.validFile(parameters, "nseqs", false);      
268                         if (temp == "not found") { m->mothurOut("nseqs is a required parameter."); m->mothurOutEndLine(); abort = true; }
269                         else { m->mothurConvert(temp, nseqs); }
270                         
271                         temp = validParameter.validFile(parameters, "bygroup", false);   if (temp == "not found") { temp = "f"; }
272                         byGroup = m->isTrue(temp);
273                         
274                         if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); }
275                         
276                         if (((groupfile != "") || (countfile != "")) && (listfile == "")) { m->mothurOut("A group or count file is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; countfile = ""; }
277                 }
278                 
279         }
280         catch(exception& e) {
281                 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
282                 exit(1);
283         }
284 }
285 //**********************************************************************************************************************
286
287 int RemoveRareCommand::execute(){
288         try {
289                 
290                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
291                 
292                 if (m->control_pressed) { return 0; }
293                 
294                 //read through the correct file and output lines you want to keep
295                 if (sabundfile != "")           {               processSabund();        }
296                 if (rabundfile != "")           {               processRabund();        }
297                 if (listfile != "")                     {               processList();          }
298                 if (sharedfile != "")           {               processShared();        }
299                 
300                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
301                         
302                 if (outputNames.size() != 0) {
303                         m->mothurOutEndLine();
304                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
305                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
306                         m->mothurOutEndLine();
307                         
308                         //set rabund file as new current rabundfile
309                         string current = "";
310                         itTypes = outputTypes.find("rabund");
311                         if (itTypes != outputTypes.end()) {
312                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
313                         }
314                         
315                         itTypes = outputTypes.find("sabund");
316                         if (itTypes != outputTypes.end()) {
317                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
318                         }
319                         
320                         itTypes = outputTypes.find("group");
321                         if (itTypes != outputTypes.end()) {
322                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
323                         }
324                         
325                         itTypes = outputTypes.find("list");
326                         if (itTypes != outputTypes.end()) {
327                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
328                         }
329                         
330                         itTypes = outputTypes.find("shared");
331                         if (itTypes != outputTypes.end()) {
332                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
333                         }
334             
335             itTypes = outputTypes.find("count");
336                         if (itTypes != outputTypes.end()) {
337                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
338                         }
339                 }
340                 
341                 return 0;               
342         }
343         
344         catch(exception& e) {
345                 m->errorOut(e, "RemoveRareCommand", "execute");
346                 exit(1);
347         }
348 }
349
350 //**********************************************************************************************************************
351 int RemoveRareCommand::processList(){
352         try {
353                 string thisOutputDir = outputDir;
354                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
355                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + getOutputFileNameTag("list", listfile);
356                 string outputGroupFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile);
357         string outputCountFileName = thisOutputDir + m->getRootName(m->getSimpleName(countfile)) + getOutputFileNameTag("count", countfile);
358         
359                 ofstream out, outGroup;
360                 m->openOutputFile(outputFileName, out);
361                 
362                 bool wroteSomething = false;
363                 
364                 //you must provide a label because the names in the listfile need to be consistent
365                 string thisLabel = "";
366                 if (allLines) { m->mothurOut("For the listfile you must select one label, using first label in your listfile."); m->mothurOutEndLine(); }
367                 else if (labels.size() > 1) { m->mothurOut("For the listfile you must select one label, using " + (*labels.begin()) + "."); m->mothurOutEndLine(); thisLabel = *labels.begin(); }
368                 else { thisLabel = *labels.begin(); }
369                 
370                 InputData input(listfile, "list");
371                 ListVector* list = input.getListVector();
372                 
373                 //get first one or the one we want
374                 if (thisLabel != "") {  
375                         //use smart distancing
376                         set<string> userLabels; userLabels.insert(thisLabel);
377                         set<string> processedLabels;
378                         string lastLabel = list->getLabel();
379                         while((list != NULL) && (userLabels.size() != 0)) {
380                                 if(userLabels.count(list->getLabel()) == 1){
381                                         processedLabels.insert(list->getLabel());
382                                         userLabels.erase(list->getLabel());
383                                         break;
384                                 }
385                                 
386                                 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
387                                         processedLabels.insert(list->getLabel());
388                                         userLabels.erase(list->getLabel());
389                                         delete list;
390                                         list = input.getListVector(lastLabel);
391                                         break;
392                                 }
393                                 lastLabel = list->getLabel();
394                                 delete list;
395                                 list = input.getListVector();
396                         }
397                         if (userLabels.size() != 0) { 
398                                 m->mothurOut("Your file does not include the label " + thisLabel + ". I will use " + lastLabel + ".");  m->mothurOutEndLine();
399                                 list = input.getListVector(lastLabel); 
400                         }
401                 }
402                 
403                 //if groupfile is given then use it
404                 GroupMap* groupMap;
405         CountTable ct;
406                 if (groupfile != "") { 
407                         groupMap = new GroupMap(groupfile); groupMap->readMap(); 
408                         SharedUtil util;
409                         vector<string> namesGroups = groupMap->getNamesOfGroups();
410                         util.setGroups(Groups, namesGroups);
411                         m->openOutputFile(outputGroupFileName, outGroup);
412                 }else if (countfile != "") {
413             ct.readTable(countfile);
414             if (ct.hasGroupInfo()) {
415                 vector<string> namesGroups = ct.getNamesOfGroups();
416                 SharedUtil util;
417                 util.setGroups(Groups, namesGroups);
418             }
419         }
420                 
421                 
422                 if (list != NULL) {     
423                         //make a new list vector
424                         ListVector newList;
425                         newList.setLabel(list->getLabel());
426                         
427                         //for each bin
428                         for (int i = 0; i < list->getNumBins(); i++) {
429                                 if (m->control_pressed) {  if (groupfile != "") { delete groupMap; outGroup.close(); m->mothurRemove(outputGroupFileName); } out.close();  m->mothurRemove(outputFileName);  return 0; }
430                                 
431                                 //parse out names that are in accnos file
432                                 string binnames = list->get(i);
433                                 vector<string> names;
434                                 string saveBinNames = binnames;
435                                 m->splitAtComma(binnames, names);
436                 int binsize = names.size();
437                                 
438                                 vector<string> newGroupFile;
439                                 if (groupfile != "") {
440                                         vector<string> newNames;
441                                         saveBinNames = "";
442                                         for(int k = 0; k < names.size(); k++) {
443                                                 string group = groupMap->getGroup(names[k]);
444                                                 
445                                                 if (m->inUsersGroups(group, Groups)) {
446                                                         newGroupFile.push_back(names[k] + "\t" + group); 
447                                                                 
448                                                         newNames.push_back(names[k]);   
449                                                         saveBinNames += names[k] + ",";
450                                                 }
451                                         }
452                                         names = newNames; binsize = names.size();
453                                         saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
454                                 }else if (countfile != "") {
455                                         saveBinNames = "";
456                     binsize = 0;
457                                         for(int k = 0; k < names.size(); k++) {
458                         if (ct.hasGroupInfo()) {
459                             vector<string> thisSeqsGroups = ct.getGroups(names[k]);
460                             
461                             int thisSeqsCount = 0;
462                             for (int n = 0; n < thisSeqsGroups.size(); n++) {
463                                 if (m->inUsersGroups(thisSeqsGroups[n], Groups)) {
464                                     thisSeqsCount += ct.getGroupCount(names[k], thisSeqsGroups[n]);
465                                 }
466                             }
467                             binsize += thisSeqsCount;
468                             //if you don't have any seqs from the groups the user wants, then remove you.
469                             if (thisSeqsCount == 0) { newGroupFile.push_back(names[k]); }
470                             else { saveBinNames += names[k] + ","; }
471                         }else {
472                             binsize += ct.getNumSeqs(names[k]); 
473                             saveBinNames += names[k] + ",";
474                         }
475                                         }
476                                         saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
477                 }
478
479                                 if (binsize > nseqs) { //keep bin
480                                         newList.push_back(saveBinNames);
481                                         if (groupfile != "") {  for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; }  }
482                     else if (countfile != "") { for(int k = 0; k < newGroupFile.size(); k++) {  ct.remove(newGroupFile[k]); } }  
483                                 }else {  if (countfile != "") {  for(int k = 0; k < names.size(); k++) {  ct.remove(names[k]); } }  }
484                         }
485                         
486                         //print new listvector
487                         if (newList.getNumBins() != 0) {
488                                 wroteSomething = true;
489                                 newList.print(out);
490                         }
491                 }       
492                 
493                 out.close();
494                 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
495         if (countfile != "") { 
496             if (ct.hasGroupInfo()) {
497                 vector<string> allGroups = ct.getNamesOfGroups();
498                 for (int i = 0; i < allGroups.size(); i++) {
499                     if (!m->inUsersGroups(allGroups[i], Groups)) { ct.removeGroup(allGroups[i]); }
500                 }
501
502             }
503             ct.printTable(outputCountFileName);
504             outputTypes["count"].push_back(outputCountFileName); outputNames.push_back(outputCountFileName); 
505         }
506                 
507                 if (wroteSomething == false) {  m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine();  }
508                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
509                 
510                 return 0;
511         }
512         catch(exception& e) {
513                 m->errorOut(e, "RemoveRareCommand", "processList");
514                 exit(1);
515         }
516 }
517 //**********************************************************************************************************************
518 int RemoveRareCommand::processSabund(){
519         try {
520                 string thisOutputDir = outputDir;
521                 if (outputDir == "") {  thisOutputDir += m->hasPath(sabundfile);  }
522                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + getOutputFileNameTag("sabund", sabundfile);
523                 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
524
525                 ofstream out;
526                 m->openOutputFile(outputFileName, out);
527                 
528                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
529                 InputData input(sabundfile, "sabund");
530                 SAbundVector* sabund = input.getSAbundVector();
531                 string lastLabel = sabund->getLabel();
532                 set<string> processedLabels;
533                 set<string> userLabels = labels;
534                 
535                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
536                         
537                         if (m->control_pressed) { delete sabund; out.close(); return 0; }
538                         
539                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
540                                 
541                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
542                                 processedLabels.insert(sabund->getLabel());
543                                 userLabels.erase(sabund->getLabel());
544                                 
545                                 if (sabund->getMaxRank() > nseqs) {
546                                         for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
547                                 }else { sabund->clear(); }
548                                 
549                                 if (sabund->getNumBins() > 0) { sabund->print(out); }
550                         }
551                         
552                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
553                                 string saveLabel = sabund->getLabel();
554                                 
555                                 delete sabund;
556                                 sabund = input.getSAbundVector(lastLabel);
557                                 
558                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
559                                 processedLabels.insert(sabund->getLabel());
560                                 userLabels.erase(sabund->getLabel());
561                                 
562                                 if (sabund->getMaxRank() > nseqs) {
563                                         for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
564                                 }else { sabund->clear(); }
565                                 
566                                 if (sabund->getNumBins() > 0) { sabund->print(out); }
567                                                                 
568                                 //restore real lastlabel to save below
569                                 sabund->setLabel(saveLabel);
570                         }               
571                         
572                         lastLabel = sabund->getLabel();                 
573                         
574                         delete sabund;
575                         sabund = input.getSAbundVector();
576                 }
577                 
578                 if (m->control_pressed) {  out.close(); return 0; }     
579                 
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();
587                                 needToRun = true;
588                         }else {
589                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
590                         }
591                 }
592                 
593                 //run last label if you need to
594                 if (needToRun == true)  {
595                         if (sabund != NULL) {   delete sabund;  }
596                         sabund = input.getSAbundVector(lastLabel);
597                         
598                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
599                         
600                         if (sabund->getMaxRank() > nseqs) {
601                                 for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
602                         }else { sabund->clear(); }
603                         
604                         if (sabund->getNumBins() > 0) { sabund->print(out); }
605                         
606                         delete sabund;
607                 }
608                 
609                 return 0;
610         }
611         catch(exception& e) {
612                 m->errorOut(e, "RemoveRareCommand", "processSabund");
613                 exit(1);
614         }
615 }
616 //**********************************************************************************************************************
617 int RemoveRareCommand::processRabund(){
618         try {
619                 string thisOutputDir = outputDir;
620                 if (outputDir == "") {  thisOutputDir += m->hasPath(rabundfile);  }
621                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + getOutputFileNameTag("rabund", rabundfile);
622                 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
623                 
624                 ofstream out;
625                 m->openOutputFile(outputFileName, out);
626                 
627                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
628                 InputData input(rabundfile, "rabund");
629                 RAbundVector* rabund = input.getRAbundVector();
630                 string lastLabel = rabund->getLabel();
631                 set<string> processedLabels;
632                 set<string> userLabels = labels;
633                 
634                 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
635                         
636                         if (m->control_pressed) { delete rabund; out.close(); return 0; }
637                         
638                         if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
639                                 
640                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
641                                 processedLabels.insert(rabund->getLabel());
642                                 userLabels.erase(rabund->getLabel());
643                                 
644                                 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
645                                 for (int i = 0; i < rabund->getNumBins(); i++) {
646                                         if (rabund->get(i) > nseqs) {
647                                                 newRabund.push_back(rabund->get(i));
648                                         }
649                                 }
650                                 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
651                         }
652                         
653                         if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
654                                 string saveLabel = rabund->getLabel();
655                                 
656                                 delete rabund;
657                                 rabund = input.getRAbundVector(lastLabel);
658                                 
659                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
660                                 processedLabels.insert(rabund->getLabel());
661                                 userLabels.erase(rabund->getLabel());
662                                 
663                                 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
664                                 for (int i = 0; i < rabund->getNumBins(); i++) {
665                                         if (rabund->get(i) > nseqs) {
666                                                 newRabund.push_back(rabund->get(i));
667                                         }
668                                 }
669                                 if (newRabund.getNumBins() > 0) { newRabund.print(out); }                               
670                                 
671                                 //restore real lastlabel to save below
672                                 rabund->setLabel(saveLabel);
673                         }               
674                         
675                         lastLabel = rabund->getLabel();                 
676                         
677                         delete rabund;
678                         rabund = input.getRAbundVector();
679                 }
680                 
681                 if (m->control_pressed) {  out.close(); return 0; }     
682                 
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();
690                                 needToRun = true;
691                         }else {
692                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
693                         }
694                 }
695                 
696                 //run last label if you need to
697                 if (needToRun == true)  {
698                         if (rabund != NULL) {   delete rabund;  }
699                         rabund = input.getRAbundVector(lastLabel);
700                         
701                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
702                         
703                         RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
704                         for (int i = 0; i < rabund->getNumBins(); i++) {
705                                 if (rabund->get(i) > nseqs) {
706                                         newRabund.push_back(rabund->get(i));
707                                 }
708                         }
709                         if (newRabund.getNumBins() > 0) { newRabund.print(out); }       
710                         
711                         delete rabund;
712                 }
713                 
714                 return 0;
715         }
716         catch(exception& e) {
717                 m->errorOut(e, "RemoveRareCommand", "processRabund");
718                 exit(1);
719         }
720 }
721 //**********************************************************************************************************************
722 int RemoveRareCommand::processShared(){
723         try {
724                 m->setGroups(Groups);
725                 
726                 string thisOutputDir = outputDir;
727                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
728                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + getOutputFileNameTag("shared", sharedfile);
729                 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
730                 
731                 ofstream out;
732                 m->openOutputFile(outputFileName, out);
733                 
734                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
735                 InputData input(sharedfile, "sharedfile");
736                 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
737                 string lastLabel = lookup[0]->getLabel();
738                 set<string> processedLabels;
739                 set<string> userLabels = labels;
740                 
741                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
742                         
743                         if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  out.close(); return 0; }
744                         
745                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
746                                 
747                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
748                                 processedLabels.insert(lookup[0]->getLabel());
749                                 userLabels.erase(lookup[0]->getLabel());
750                                 
751                                 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
752                                 processLookup(lookup, out);
753                         }
754                         
755                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
756                                 string saveLabel = lookup[0]->getLabel();
757                                 
758                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
759                                 lookup = input.getSharedRAbundVectors(lastLabel);
760                                 
761                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
762                                 processedLabels.insert(lookup[0]->getLabel());
763                                 userLabels.erase(lookup[0]->getLabel());
764                                 
765                                 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
766                                 processLookup(lookup, out);                     
767                                 
768                                 //restore real lastlabel to save below
769                                 lookup[0]->setLabel(saveLabel);
770                         }               
771                         
772                         lastLabel = lookup[0]->getLabel();                      
773                         
774                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
775                         lookup = input.getSharedRAbundVectors();
776                 }
777                 
778                 if (m->control_pressed) {  out.close(); return 0; }     
779                 
780                 //output error messages about any remaining user labels
781                 set<string>::iterator it;
782                 bool needToRun = false;
783                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
784                         m->mothurOut("Your file does not include the label " + *it); 
785                         if (processedLabels.count(lastLabel) != 1) {
786                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
787                                 needToRun = true;
788                         }else {
789                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
790                         }
791                 }
792                 
793                 //run last label if you need to
794                 if (needToRun == true)  {
795                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i];       }  }
796                         lookup = input.getSharedRAbundVectors(lastLabel);
797                         
798                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
799                         
800                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
801                         processLookup(lookup, out);     
802                         
803                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
804                 }
805                 
806                 return 0;
807         }
808         catch(exception& e) {
809                 m->errorOut(e, "RemoveRareCommand", "processSabund");
810                 exit(1);
811         }
812 }
813 //**********************************************************************************************************************
814 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
815         try {
816                 
817                 vector<SharedRAbundVector> newRabunds;  newRabunds.resize(lookup.size());
818                 for (int i = 0; i < lookup.size(); i++) {  
819                         newRabunds[i].setGroup(lookup[i]->getGroup());
820                         newRabunds[i].setLabel(lookup[i]->getLabel());
821                 }
822                 
823                 if (byGroup) {
824                         
825                         //for each otu
826                         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
827                                 bool allZero = true;
828                                 
829                                 if (m->control_pressed) { return 0; }
830                                 
831                                 //for each group
832                                 for (int j = 0; j < lookup.size(); j++) {
833                                         
834                                         //are you rare?
835                                         if (lookup[j]->getAbundance(i) > nseqs) {
836                                                 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
837                                                 allZero = false;
838                                         }else {
839                                                 newRabunds[j].push_back(0, newRabunds[j].getGroup());
840                                         }
841                                 }
842                                 
843                                 //eliminates zero otus
844                                 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) {  newRabunds[j].pop_back(); } }
845                         }
846                 }else {
847                         //for each otu
848                         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
849                                 
850                                 if (m->control_pressed) { return 0; }
851                                 
852                                 int totalAbund = 0;
853                                 //get total otu abundance
854                                 for (int j = 0; j < lookup.size(); j++) {
855                                         newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
856                                         totalAbund += lookup[j]->getAbundance(i);
857                                 }
858                                 
859                                 //eliminates otus below rare cutoff
860                                 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) {  newRabunds[j].pop_back(); } }
861                         }
862                 }
863                 
864                 //do we have any otus above the rare cutoff
865                 if (newRabunds[0].getNumBins() != 0) { 
866                         for (int j = 0; j < newRabunds.size(); j++) { 
867                                 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
868                                 newRabunds[j].print(out); 
869                         }
870                 }
871                 
872                 return 0;
873         }
874         catch(exception& e) {
875                 m->errorOut(e, "RemoveRareCommand", "processLookup");
876                 exit(1);
877         }
878 }
879 //**********************************************************************************************************************
880
881
882
883