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