]> git.donarmstrong.com Git - mothur.git/blob - removerarecommand.cpp
added getCommandInfoCommand for gui
[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 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);
30                 
31                 vector<string> myArray;
32                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
33                 return myArray;
34         }
35         catch(exception& e) {
36                 m->errorOut(e, "RemoveRareCommand", "setParameters");
37                 exit(1);
38         }
39 }
40 //**********************************************************************************************************************
41 string RemoveRareCommand::getHelpString(){      
42         try {
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";
54                 return helpString;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "RemoveRareCommand", "getHelpString");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 RemoveRareCommand::RemoveRareCommand(){ 
63         try {
64                 abort = true; calledHelp = true; 
65                 setParameters();
66                 vector<string> tempOutNames;
67                 outputTypes["rabund"] = tempOutNames;
68                 outputTypes["sabund"] = tempOutNames;
69                 outputTypes["list"] = tempOutNames;
70                 outputTypes["group"] = tempOutNames;
71                 outputTypes["shared"] = tempOutNames;
72         }
73         catch(exception& e) {
74                 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
75                 exit(1);
76         }
77 }
78 //**********************************************************************************************************************
79 RemoveRareCommand::RemoveRareCommand(string option)  {
80         try {
81                 abort = false; calledHelp = false;   
82                 allLines = 1;
83                 
84                 //allow user to run help
85                 if(option == "help") { help(); abort = true; calledHelp = true; }
86                 
87                 else {
88                         vector<string> myArray = setParameters();
89                         
90                         OptionParser parser(option);
91                         map<string,string> parameters = parser.getParameters();
92                         
93                         ValidParameters validParameter;
94                         map<string,string>::iterator it;
95                         
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;  }
99                         }
100                         
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;   
108                         
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 = "";         }
111                         
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 = "";          }
115                         else {
116                                 string path;
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;             }
123                                 }
124                                 
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;            }
131                                 }
132                                 
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;           }
139                                 }
140                                 
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;           }
147                                 }
148                                 
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;           }
155                                 }
156                         }
157                         
158                         
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 = "";  }  
163                         
164                         sabundfile = validParameter.validFile(parameters, "sabund", true);
165                         if (sabundfile == "not open") { abort = true; }
166                         else if (sabundfile == "not found") {  sabundfile = "";  }      
167                         
168                         rabundfile = validParameter.validFile(parameters, "rabund", true);
169                         if (rabundfile == "not open") { abort = true; }
170                         else if (rabundfile == "not found") {  rabundfile = "";  }                              
171                         
172                         groupfile = validParameter.validFile(parameters, "group", true);
173                         if (groupfile == "not open") { groupfile = ""; abort = true; }
174                         else if (groupfile == "not found") {  groupfile = "";  }        
175                         
176                         sharedfile = validParameter.validFile(parameters, "shared", true);
177                         if (sharedfile == "not open") { sharedfile = "";  abort = true; }
178                         else if (sharedfile == "not found") {  sharedfile = "";  }
179                         
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(); }
186                                 else { 
187                                         listfile = m->getListFile(); 
188                                         if (listfile != "") {  m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
189                                         else { 
190                                                 rabundfile = m->getRabundFile(); 
191                                                 if (rabundfile != "") {  m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
192                                                 else { 
193                                                         sabundfile = m->getSabundFile(); 
194                                                         if (sabundfile != "") {  m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
195                                                         else { 
196                                                                 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file."); m->mothurOutEndLine(); 
197                                                                 abort = true;
198                                                         }
199                                                 }
200                                         }
201                                 }
202                         } 
203                         
204                         groups = validParameter.validFile(parameters, "groups", false);                 
205                         if (groups == "not found") { groups = "all"; }
206                         m->splitAtDash(groups, Groups);
207                         
208                         label = validParameter.validFile(parameters, "label", false);                   
209                         if (label == "not found") { label = ""; }
210                         else { 
211                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
212                                 else { allLines = 1;  }
213                         }
214                         
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); }
218                         
219                         temp = validParameter.validFile(parameters, "bygroup", false);   if (temp == "not found") { temp = "f"; }
220                         byGroup = m->isTrue(temp);
221                         
222                         if (byGroup && (sharedfile == "")) { m->mothurOut("The byGroup parameter is only valid with a shared file."); m->mothurOutEndLine(); }
223                         
224                         if ((groupfile != "") && (listfile == "")) { m->mothurOut("A groupfile is only valid with a list file."); m->mothurOutEndLine(); groupfile = ""; }
225                 }
226                 
227         }
228         catch(exception& e) {
229                 m->errorOut(e, "RemoveRareCommand", "RemoveRareCommand");
230                 exit(1);
231         }
232 }
233 //**********************************************************************************************************************
234
235 int RemoveRareCommand::execute(){
236         try {
237                 
238                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
239                 
240                 if (m->control_pressed) { return 0; }
241                 
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();        }
247                 
248                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
249                         
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();
255                         
256                         //set rabund file as new current rabundfile
257                         string current = "";
258                         itTypes = outputTypes.find("rabund");
259                         if (itTypes != outputTypes.end()) {
260                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
261                         }
262                         
263                         itTypes = outputTypes.find("sabund");
264                         if (itTypes != outputTypes.end()) {
265                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
266                         }
267                         
268                         itTypes = outputTypes.find("group");
269                         if (itTypes != outputTypes.end()) {
270                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
271                         }
272                         
273                         itTypes = outputTypes.find("list");
274                         if (itTypes != outputTypes.end()) {
275                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
276                         }
277                         
278                         itTypes = outputTypes.find("shared");
279                         if (itTypes != outputTypes.end()) {
280                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
281                         }
282                 }
283                 
284                 return 0;               
285         }
286         
287         catch(exception& e) {
288                 m->errorOut(e, "RemoveRareCommand", "execute");
289                 exit(1);
290         }
291 }
292
293 //**********************************************************************************************************************
294 int RemoveRareCommand::processList(){
295         try {
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);
300                 
301                 ofstream out, outGroup;
302                 m->openOutputFile(outputFileName, out);
303                 
304                 bool wroteSomething = false;
305                 
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(); }
311                 
312                 InputData input(listfile, "list");
313                 ListVector* list = input.getListVector();
314                 
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());
325                                         break;
326                                 }
327                                 
328                                 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
329                                         processedLabels.insert(list->getLabel());
330                                         userLabels.erase(list->getLabel());
331                                         delete list;
332                                         list = input.getListVector(lastLabel);
333                                         break;
334                                 }
335                                 lastLabel = list->getLabel();
336                                 delete list;
337                                 list = input.getListVector();
338                         }
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); 
342                         }
343                 }
344                 
345                 //if groupfile is given then use it
346                 GroupMap* groupMap;
347                 if (groupfile != "") { 
348                         groupMap = new GroupMap(groupfile); groupMap->readMap(); 
349                         SharedUtil util;
350                         util.setGroups(Groups, groupMap->namesOfGroups);
351                         m->openOutputFile(outputGroupFileName, outGroup);
352                 }
353                 
354                 
355                 if (list != NULL) {     
356                         //make a new list vector
357                         ListVector newList;
358                         newList.setLabel(list->getLabel());
359                         
360                         //for each bin
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; }
363                                 
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);
369                                 
370                                 vector<string> newGroupFile;
371                                 if (groupfile != "") {
372                                         vector<string> newNames;
373                                         saveBinNames = "";
374                                         for(int k = 0; k < names.size(); k++) {
375                                                 string group = groupMap->getGroup(names[k]);
376                                                 
377                                                 if (m->inUsersGroups(group, Groups)) {
378                                                         newGroupFile.push_back(names[k] + "\t" + group); 
379                                                                 
380                                                         newNames.push_back(names[k]);   
381                                                         saveBinNames += names[k] + ",";
382                                                 }
383                                         }
384                                         names = newNames;
385                                         saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
386                                 }
387
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; }
391                                 }
392                         }
393                         
394                         //print new listvector
395                         if (newList.getNumBins() != 0) {
396                                 wroteSomething = true;
397                                 newList.print(out);
398                         }
399                 }       
400                 
401                 out.close();
402                 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
403                 
404                 if (wroteSomething == false) {  m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine();  }
405                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
406                 
407                 return 0;
408         }
409         catch(exception& e) {
410                 m->errorOut(e, "RemoveRareCommand", "processList");
411                 exit(1);
412         }
413 }
414 //**********************************************************************************************************************
415 int RemoveRareCommand::processSabund(){
416         try {
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);
421
422                 ofstream out;
423                 m->openOutputFile(outputFileName, out);
424                 
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;
431                 
432                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
433                         
434                         if (m->control_pressed) { delete sabund; out.close(); return 0; }
435                         
436                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
437                                 
438                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
439                                 processedLabels.insert(sabund->getLabel());
440                                 userLabels.erase(sabund->getLabel());
441                                 
442                                 if (sabund->getMaxRank() > nseqs) {
443                                         for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
444                                 }else { sabund->clear(); }
445                                 
446                                 if (sabund->getNumBins() > 0) { sabund->print(out); }
447                         }
448                         
449                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
450                                 string saveLabel = sabund->getLabel();
451                                 
452                                 delete sabund;
453                                 sabund = input.getSAbundVector(lastLabel);
454                                 
455                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
456                                 processedLabels.insert(sabund->getLabel());
457                                 userLabels.erase(sabund->getLabel());
458                                 
459                                 if (sabund->getMaxRank() > nseqs) {
460                                         for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
461                                 }else { sabund->clear(); }
462                                 
463                                 if (sabund->getNumBins() > 0) { sabund->print(out); }
464                                                                 
465                                 //restore real lastlabel to save below
466                                 sabund->setLabel(saveLabel);
467                         }               
468                         
469                         lastLabel = sabund->getLabel();                 
470                         
471                         delete sabund;
472                         sabund = input.getSAbundVector();
473                 }
474                 
475                 if (m->control_pressed) {  out.close(); return 0; }     
476                 
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();
484                                 needToRun = true;
485                         }else {
486                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
487                         }
488                 }
489                 
490                 //run last label if you need to
491                 if (needToRun == true)  {
492                         if (sabund != NULL) {   delete sabund;  }
493                         sabund = input.getSAbundVector(lastLabel);
494                         
495                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
496                         
497                         if (sabund->getMaxRank() > nseqs) {
498                                 for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
499                         }else { sabund->clear(); }
500                         
501                         if (sabund->getNumBins() > 0) { sabund->print(out); }
502                         
503                         delete sabund;
504                 }
505                 
506                 return 0;
507         }
508         catch(exception& e) {
509                 m->errorOut(e, "RemoveRareCommand", "processSabund");
510                 exit(1);
511         }
512 }
513 //**********************************************************************************************************************
514 int RemoveRareCommand::processRabund(){
515         try {
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);
520                 
521                 ofstream out;
522                 m->openOutputFile(outputFileName, out);
523                 
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;
530                 
531                 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
532                         
533                         if (m->control_pressed) { delete rabund; out.close(); return 0; }
534                         
535                         if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
536                                 
537                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
538                                 processedLabels.insert(rabund->getLabel());
539                                 userLabels.erase(rabund->getLabel());
540                                 
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));
545                                         }
546                                 }
547                                 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
548                         }
549                         
550                         if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
551                                 string saveLabel = rabund->getLabel();
552                                 
553                                 delete rabund;
554                                 rabund = input.getRAbundVector(lastLabel);
555                                 
556                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
557                                 processedLabels.insert(rabund->getLabel());
558                                 userLabels.erase(rabund->getLabel());
559                                 
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));
564                                         }
565                                 }
566                                 if (newRabund.getNumBins() > 0) { newRabund.print(out); }                               
567                                 
568                                 //restore real lastlabel to save below
569                                 rabund->setLabel(saveLabel);
570                         }               
571                         
572                         lastLabel = rabund->getLabel();                 
573                         
574                         delete rabund;
575                         rabund = input.getRAbundVector();
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 (rabund != NULL) {   delete rabund;  }
596                         rabund = input.getRAbundVector(lastLabel);
597                         
598                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
599                         
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));
604                                 }
605                         }
606                         if (newRabund.getNumBins() > 0) { newRabund.print(out); }       
607                         
608                         delete rabund;
609                 }
610                 
611                 return 0;
612         }
613         catch(exception& e) {
614                 m->errorOut(e, "RemoveRareCommand", "processRabund");
615                 exit(1);
616         }
617 }
618 //**********************************************************************************************************************
619 int RemoveRareCommand::processShared(){
620         try {
621                 m->Groups = Groups;
622                 
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);
627                 
628                 ofstream out;
629                 m->openOutputFile(outputFileName, out);
630                 
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;
637                 
638                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
639                         
640                         if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  out.close(); return 0; }
641                         
642                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
643                                 
644                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
645                                 processedLabels.insert(lookup[0]->getLabel());
646                                 userLabels.erase(lookup[0]->getLabel());
647                                 
648                                 processLookup(lookup, out);
649                         }
650                         
651                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
652                                 string saveLabel = lookup[0]->getLabel();
653                                 
654                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
655                                 lookup = input.getSharedRAbundVectors(lastLabel);
656                                 
657                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
658                                 processedLabels.insert(lookup[0]->getLabel());
659                                 userLabels.erase(lookup[0]->getLabel());
660                                 
661                                 processLookup(lookup, out);                     
662                                 
663                                 //restore real lastlabel to save below
664                                 lookup[0]->setLabel(saveLabel);
665                         }               
666                         
667                         lastLabel = lookup[0]->getLabel();                      
668                         
669                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
670                         lookup = input.getSharedRAbundVectors();
671                 }
672                 
673                 if (m->control_pressed) {  out.close(); return 0; }     
674                 
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();
682                                 needToRun = true;
683                         }else {
684                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
685                         }
686                 }
687                 
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);
692                         
693                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
694                         
695                         processLookup(lookup, out);     
696                         
697                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
698                 }
699                 
700                 return 0;
701         }
702         catch(exception& e) {
703                 m->errorOut(e, "RemoveRareCommand", "processSabund");
704                 exit(1);
705         }
706 }
707 //**********************************************************************************************************************
708 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
709         try {
710                 
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());
715                 }
716                 
717                 if (byGroup) {
718                         
719                         //for each otu
720                         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
721                                 bool allZero = true;
722                                 
723                                 if (m->control_pressed) { return 0; }
724                                 
725                                 //for each group
726                                 for (int j = 0; j < lookup.size(); j++) {
727                                         
728                                         //are you rare?
729                                         if (lookup[j]->getAbundance(i) > nseqs) {
730                                                 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
731                                                 allZero = false;
732                                         }else {
733                                                 newRabunds[j].push_back(0, newRabunds[j].getGroup());
734                                         }
735                                 }
736                                 
737                                 //eliminates zero otus
738                                 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) {  newRabunds[j].pop_back(); } }
739                         }
740                 }else {
741                         //for each otu
742                         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
743                                 
744                                 if (m->control_pressed) { return 0; }
745                                 
746                                 int totalAbund = 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);
751                                 }
752                                 
753                                 //eliminates otus below rare cutoff
754                                 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) {  newRabunds[j].pop_back(); } }
755                         }
756                 }
757                 
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); 
763                         }
764                 }
765                 
766                 return 0;
767         }
768         catch(exception& e) {
769                 m->errorOut(e, "RemoveRareCommand", "processLookup");
770                 exit(1);
771         }
772 }
773 //**********************************************************************************************************************
774
775
776
777