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