]> git.donarmstrong.com Git - mothur.git/blob - removerarecommand.cpp
working on current change
[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 { convert(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++) {        remove(outputNames[i].c_str()); } 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                         util.setGroups(Groups, groupMap->namesOfGroups);
357                         m->openOutputFile(outputGroupFileName, outGroup);
358                 }
359                 
360                 
361                 if (list != NULL) {     
362                         //make a new list vector
363                         ListVector newList;
364                         newList.setLabel(list->getLabel());
365                         
366                         //for each bin
367                         for (int i = 0; i < list->getNumBins(); i++) {
368                                 if (m->control_pressed) {  if (groupfile != "") { delete groupMap; outGroup.close(); remove(outputGroupFileName.c_str()); } out.close();  remove(outputFileName.c_str());  return 0; }
369                                 
370                                 //parse out names that are in accnos file
371                                 string binnames = list->get(i);
372                                 vector<string> names;
373                                 string saveBinNames = binnames;
374                                 m->splitAtComma(binnames, names);
375                                 
376                                 vector<string> newGroupFile;
377                                 if (groupfile != "") {
378                                         vector<string> newNames;
379                                         saveBinNames = "";
380                                         for(int k = 0; k < names.size(); k++) {
381                                                 string group = groupMap->getGroup(names[k]);
382                                                 
383                                                 if (m->inUsersGroups(group, Groups)) {
384                                                         newGroupFile.push_back(names[k] + "\t" + group); 
385                                                                 
386                                                         newNames.push_back(names[k]);   
387                                                         saveBinNames += names[k] + ",";
388                                                 }
389                                         }
390                                         names = newNames;
391                                         saveBinNames = saveBinNames.substr(0, saveBinNames.length()-1);
392                                 }
393
394                                 if (names.size() > nseqs) { //keep bin
395                                         newList.push_back(saveBinNames);
396                                         for(int k = 0; k < newGroupFile.size(); k++) { outGroup << newGroupFile[k] << endl; }
397                                 }
398                         }
399                         
400                         //print new listvector
401                         if (newList.getNumBins() != 0) {
402                                 wroteSomething = true;
403                                 newList.print(out);
404                         }
405                 }       
406                 
407                 out.close();
408                 if (groupfile != "") { outGroup.close(); outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName); }
409                 
410                 if (wroteSomething == false) {  m->mothurOut("Your file contains only rare sequences."); m->mothurOutEndLine();  }
411                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
412                 
413                 return 0;
414         }
415         catch(exception& e) {
416                 m->errorOut(e, "RemoveRareCommand", "processList");
417                 exit(1);
418         }
419 }
420 //**********************************************************************************************************************
421 int RemoveRareCommand::processSabund(){
422         try {
423                 string thisOutputDir = outputDir;
424                 if (outputDir == "") {  thisOutputDir += m->hasPath(sabundfile);  }
425                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "pick" +  m->getExtension(sabundfile);
426                 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
427
428                 ofstream out;
429                 m->openOutputFile(outputFileName, out);
430                 
431                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
432                 InputData input(sabundfile, "sabund");
433                 SAbundVector* sabund = input.getSAbundVector();
434                 string lastLabel = sabund->getLabel();
435                 set<string> processedLabels;
436                 set<string> userLabels = labels;
437                 
438                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
439                         
440                         if (m->control_pressed) { delete sabund; out.close(); return 0; }
441                         
442                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
443                                 
444                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
445                                 processedLabels.insert(sabund->getLabel());
446                                 userLabels.erase(sabund->getLabel());
447                                 
448                                 if (sabund->getMaxRank() > nseqs) {
449                                         for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
450                                 }else { sabund->clear(); }
451                                 
452                                 if (sabund->getNumBins() > 0) { sabund->print(out); }
453                         }
454                         
455                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
456                                 string saveLabel = sabund->getLabel();
457                                 
458                                 delete sabund;
459                                 sabund = input.getSAbundVector(lastLabel);
460                                 
461                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
462                                 processedLabels.insert(sabund->getLabel());
463                                 userLabels.erase(sabund->getLabel());
464                                 
465                                 if (sabund->getMaxRank() > nseqs) {
466                                         for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
467                                 }else { sabund->clear(); }
468                                 
469                                 if (sabund->getNumBins() > 0) { sabund->print(out); }
470                                                                 
471                                 //restore real lastlabel to save below
472                                 sabund->setLabel(saveLabel);
473                         }               
474                         
475                         lastLabel = sabund->getLabel();                 
476                         
477                         delete sabund;
478                         sabund = input.getSAbundVector();
479                 }
480                 
481                 if (m->control_pressed) {  out.close(); return 0; }     
482                 
483                 //output error messages about any remaining user labels
484                 set<string>::iterator it;
485                 bool needToRun = false;
486                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
487                         m->mothurOut("Your file does not include the label " + *it); 
488                         if (processedLabels.count(lastLabel) != 1) {
489                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
490                                 needToRun = true;
491                         }else {
492                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
493                         }
494                 }
495                 
496                 //run last label if you need to
497                 if (needToRun == true)  {
498                         if (sabund != NULL) {   delete sabund;  }
499                         sabund = input.getSAbundVector(lastLabel);
500                         
501                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
502                         
503                         if (sabund->getMaxRank() > nseqs) {
504                                 for(int i = 1; i <=nseqs; i++) {  sabund->set(i, 0); }
505                         }else { sabund->clear(); }
506                         
507                         if (sabund->getNumBins() > 0) { sabund->print(out); }
508                         
509                         delete sabund;
510                 }
511                 
512                 return 0;
513         }
514         catch(exception& e) {
515                 m->errorOut(e, "RemoveRareCommand", "processSabund");
516                 exit(1);
517         }
518 }
519 //**********************************************************************************************************************
520 int RemoveRareCommand::processRabund(){
521         try {
522                 string thisOutputDir = outputDir;
523                 if (outputDir == "") {  thisOutputDir += m->hasPath(rabundfile);  }
524                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "pick" +  m->getExtension(rabundfile);
525                 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
526                 
527                 ofstream out;
528                 m->openOutputFile(outputFileName, out);
529                 
530                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
531                 InputData input(rabundfile, "rabund");
532                 RAbundVector* rabund = input.getRAbundVector();
533                 string lastLabel = rabund->getLabel();
534                 set<string> processedLabels;
535                 set<string> userLabels = labels;
536                 
537                 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
538                         
539                         if (m->control_pressed) { delete rabund; out.close(); return 0; }
540                         
541                         if(allLines == 1 || labels.count(rabund->getLabel()) == 1){                     
542                                 
543                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
544                                 processedLabels.insert(rabund->getLabel());
545                                 userLabels.erase(rabund->getLabel());
546                                 
547                                 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
548                                 for (int i = 0; i < rabund->getNumBins(); i++) {
549                                         if (rabund->get(i) > nseqs) {
550                                                 newRabund.push_back(rabund->get(i));
551                                         }
552                                 }
553                                 if (newRabund.getNumBins() > 0) { newRabund.print(out); }
554                         }
555                         
556                         if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
557                                 string saveLabel = rabund->getLabel();
558                                 
559                                 delete rabund;
560                                 rabund = input.getRAbundVector(lastLabel);
561                                 
562                                 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
563                                 processedLabels.insert(rabund->getLabel());
564                                 userLabels.erase(rabund->getLabel());
565                                 
566                                 RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
567                                 for (int i = 0; i < rabund->getNumBins(); i++) {
568                                         if (rabund->get(i) > nseqs) {
569                                                 newRabund.push_back(rabund->get(i));
570                                         }
571                                 }
572                                 if (newRabund.getNumBins() > 0) { newRabund.print(out); }                               
573                                 
574                                 //restore real lastlabel to save below
575                                 rabund->setLabel(saveLabel);
576                         }               
577                         
578                         lastLabel = rabund->getLabel();                 
579                         
580                         delete rabund;
581                         rabund = input.getRAbundVector();
582                 }
583                 
584                 if (m->control_pressed) {  out.close(); return 0; }     
585                 
586                 //output error messages about any remaining user labels
587                 set<string>::iterator it;
588                 bool needToRun = false;
589                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
590                         m->mothurOut("Your file does not include the label " + *it); 
591                         if (processedLabels.count(lastLabel) != 1) {
592                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
593                                 needToRun = true;
594                         }else {
595                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
596                         }
597                 }
598                 
599                 //run last label if you need to
600                 if (needToRun == true)  {
601                         if (rabund != NULL) {   delete rabund;  }
602                         rabund = input.getRAbundVector(lastLabel);
603                         
604                         m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
605                         
606                         RAbundVector newRabund; newRabund.setLabel(rabund->getLabel());
607                         for (int i = 0; i < rabund->getNumBins(); i++) {
608                                 if (rabund->get(i) > nseqs) {
609                                         newRabund.push_back(rabund->get(i));
610                                 }
611                         }
612                         if (newRabund.getNumBins() > 0) { newRabund.print(out); }       
613                         
614                         delete rabund;
615                 }
616                 
617                 return 0;
618         }
619         catch(exception& e) {
620                 m->errorOut(e, "RemoveRareCommand", "processRabund");
621                 exit(1);
622         }
623 }
624 //**********************************************************************************************************************
625 int RemoveRareCommand::processShared(){
626         try {
627                 m->Groups = Groups;
628                 
629                 string thisOutputDir = outputDir;
630                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
631                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "pick" +  m->getExtension(sharedfile);
632                 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
633                 
634                 ofstream out;
635                 m->openOutputFile(outputFileName, out);
636                 
637                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
638                 InputData input(sharedfile, "sharedfile");
639                 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
640                 string lastLabel = lookup[0]->getLabel();
641                 set<string> processedLabels;
642                 set<string> userLabels = labels;
643                 
644                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
645                         
646                         if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  out.close(); return 0; }
647                         
648                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
649                                 
650                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
651                                 processedLabels.insert(lookup[0]->getLabel());
652                                 userLabels.erase(lookup[0]->getLabel());
653                                 
654                                 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
655                                 processLookup(lookup, out);
656                         }
657                         
658                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
659                                 string saveLabel = lookup[0]->getLabel();
660                                 
661                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
662                                 lookup = input.getSharedRAbundVectors(lastLabel);
663                                 
664                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
665                                 processedLabels.insert(lookup[0]->getLabel());
666                                 userLabels.erase(lookup[0]->getLabel());
667                                 
668                                 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
669                                 processLookup(lookup, out);                     
670                                 
671                                 //restore real lastlabel to save below
672                                 lookup[0]->setLabel(saveLabel);
673                         }               
674                         
675                         lastLabel = lookup[0]->getLabel();                      
676                         
677                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
678                         lookup = input.getSharedRAbundVectors();
679                 }
680                 
681                 if (m->control_pressed) {  out.close(); return 0; }     
682                 
683                 //output error messages about any remaining user labels
684                 set<string>::iterator it;
685                 bool needToRun = false;
686                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
687                         m->mothurOut("Your file does not include the label " + *it); 
688                         if (processedLabels.count(lastLabel) != 1) {
689                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
690                                 needToRun = true;
691                         }else {
692                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
693                         }
694                 }
695                 
696                 //run last label if you need to
697                 if (needToRun == true)  {
698                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i];       }  }
699                         lookup = input.getSharedRAbundVectors(lastLabel);
700                         
701                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
702                         
703                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
704                         processLookup(lookup, out);     
705                         
706                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
707                 }
708                 
709                 return 0;
710         }
711         catch(exception& e) {
712                 m->errorOut(e, "RemoveRareCommand", "processSabund");
713                 exit(1);
714         }
715 }
716 //**********************************************************************************************************************
717 int RemoveRareCommand::processLookup(vector<SharedRAbundVector*>& lookup, ofstream& out){
718         try {
719                 
720                 vector<SharedRAbundVector> newRabunds;  newRabunds.resize(lookup.size());
721                 for (int i = 0; i < lookup.size(); i++) {  
722                         newRabunds[i].setGroup(lookup[i]->getGroup());
723                         newRabunds[i].setLabel(lookup[i]->getLabel());
724                 }
725                 
726                 if (byGroup) {
727                         
728                         //for each otu
729                         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
730                                 bool allZero = true;
731                                 
732                                 if (m->control_pressed) { return 0; }
733                                 
734                                 //for each group
735                                 for (int j = 0; j < lookup.size(); j++) {
736                                         
737                                         //are you rare?
738                                         if (lookup[j]->getAbundance(i) > nseqs) {
739                                                 newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
740                                                 allZero = false;
741                                         }else {
742                                                 newRabunds[j].push_back(0, newRabunds[j].getGroup());
743                                         }
744                                 }
745                                 
746                                 //eliminates zero otus
747                                 if (allZero) { for (int j = 0; j < newRabunds.size(); j++) {  newRabunds[j].pop_back(); } }
748                         }
749                 }else {
750                         //for each otu
751                         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
752                                 
753                                 if (m->control_pressed) { return 0; }
754                                 
755                                 int totalAbund = 0;
756                                 //get total otu abundance
757                                 for (int j = 0; j < lookup.size(); j++) {
758                                         newRabunds[j].push_back(lookup[j]->getAbundance(i), newRabunds[j].getGroup());
759                                         totalAbund += lookup[j]->getAbundance(i);
760                                 }
761                                 
762                                 //eliminates otus below rare cutoff
763                                 if (totalAbund <= nseqs) { for (int j = 0; j < newRabunds.size(); j++) {  newRabunds[j].pop_back(); } }
764                         }
765                 }
766                 
767                 //do we have any otus above the rare cutoff
768                 if (newRabunds[0].getNumBins() != 0) { 
769                         for (int j = 0; j < newRabunds.size(); j++) { 
770                                 out << newRabunds[j].getLabel() << '\t' << newRabunds[j].getGroup() << '\t';
771                                 newRabunds[j].print(out); 
772                         }
773                 }
774                 
775                 return 0;
776         }
777         catch(exception& e) {
778                 m->errorOut(e, "RemoveRareCommand", "processLookup");
779                 exit(1);
780         }
781 }
782 //**********************************************************************************************************************
783
784
785
786