]> git.donarmstrong.com Git - mothur.git/blob - removeotulabelscommand.cpp
fixes while testing 1.33.0
[mothur.git] / removeotulabelscommand.cpp
1 //
2 //  removeotulabels.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 5/21/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "removeotulabelscommand.h"
10
11 //**********************************************************************************************************************
12 vector<string> RemoveOtuLabelsCommand::setParameters(){ 
13         try {
14         CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(paccnos);
15         CommandParameter pconstaxonomy("constaxonomy", "InputTypes", "", "", "none", "FNGLT", "none","constaxonomy",false,false); parameters.push_back(pconstaxonomy);
16                 CommandParameter potucorr("otucorr", "InputTypes", "", "", "none", "FNGLT", "none","otucorr",false,false); parameters.push_back(potucorr);
17         CommandParameter pcorraxes("corraxes", "InputTypes", "", "", "none", "FNGLT", "none","corraxes",false,false); parameters.push_back(pcorraxes);
18         CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none","list",false,false, true); parameters.push_back(plist);
19         CommandParameter pshared("shared", "InputTypes", "", "", "none", "FNGLT", "none","shared",false,false, true); parameters.push_back(pshared);
20         CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
21         CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "RemoveOtuLabelsCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string RemoveOtuLabelsCommand::getHelpString(){ 
35         try {
36                 string helpString = "";
37                 helpString += "The remove.otulabels command can be used to remove specific otus with the output from classify.otu, otu.association, or corr.axes. It can also be used to select a set of otus from a shared or list file.\n";
38                 helpString += "The remove.otulabels parameters are: constaxonomy, otucorr, corraxes, shared, list, label and accnos.\n";
39                 helpString += "The constaxonomy parameter is input the results of the classify.otu command.\n";
40         helpString += "The otucorr parameter is input the results of the otu.association command.\n";
41         helpString += "The corraxes parameter is input the results of the corr.axes command.\n";
42         helpString += "The label parameter is used to analyze specific labels in your input. \n";
43                 helpString += "The remove.otulabels commmand should be in the following format: \n";
44                 helpString += "remove.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n";
45                 return helpString;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "RemoveOtuLabelsCommand", "getHelpString");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 string RemoveOtuLabelsCommand::getOutputPattern(string type) {
54     try {
55         string pattern = "";
56         
57         if (type == "constaxonomy")         {   pattern = "[filename],pick,[extension]";                }
58         else if (type == "otucorr")         {   pattern = "[filename],pick,[extension]";                }
59         else if (type == "corraxes")        {   pattern = "[filename],pick,[extension]";                }
60         else if (type == "list")            {   pattern = "[filename],[distance],pick,[extension]";     }
61         else if (type == "shared")          {   pattern = "[filename],[distance],pick,[extension]";     }
62         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
63         
64         return pattern;
65     }
66     catch(exception& e) {
67         m->errorOut(e, "RemoveOtuLabelsCommand", "getOutputPattern");
68         exit(1);
69     }
70 }
71 //**********************************************************************************************************************
72 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(){       
73         try {
74                 abort = true; calledHelp = true;
75                 setParameters();
76         vector<string> tempOutNames;
77                 outputTypes["constaxonomy"] = tempOutNames; 
78         outputTypes["otucorr"] = tempOutNames;
79         outputTypes["corraxes"] = tempOutNames;
80         outputTypes["shared"] = tempOutNames;
81         outputTypes["list"] = tempOutNames;
82         }
83         catch(exception& e) {
84                 m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
85                 exit(1);
86         }
87 }
88 //**********************************************************************************************************************
89 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(string option)  {
90         try {
91                 abort = false; calledHelp = false;   
92         
93                 //allow user to run help
94                 if(option == "help") { help(); abort = true; calledHelp = true; }
95                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
96                 
97                 else {
98                         //valid paramters for this command
99                         vector<string> myArray = setParameters();
100                         
101                         OptionParser parser(option);
102                         map<string,string> parameters = parser.getParameters();
103                         
104                         ValidParameters validParameter;
105                         map<string,string>::iterator it;
106                         //check to make sure all parameters are valid for command
107                         for (it = parameters.begin(); it != parameters.end(); it++) { 
108                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
109                         }
110                         
111                         
112                         //if the user changes the input directory command factory will send this info to us in the output parameter 
113                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
114                         if (inputDir == "not found"){   inputDir = "";          }
115                         else {
116                 
117                 //edit file types below to include only the types you added as parameters
118                 
119                                 string path;
120                 it = parameters.find("constaxonomy");
121                                 //user has given a template file
122                                 if(it != parameters.end()){ 
123                                         path = m->hasPath(it->second);
124                                         //if the user has not given a path then, add inputdir. else leave path alone.
125                                         if (path == "") {       parameters["constaxonomy"] = inputDir + it->second;             }
126                                 }
127                 
128                 it = parameters.find("accnos");
129                                 //user has given a template file
130                                 if(it != parameters.end()){ 
131                                         path = m->hasPath(it->second);
132                                         //if the user has not given a path then, add inputdir. else leave path alone.
133                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
134                                 }
135                 
136                 it = parameters.find("corraxes");
137                                 //user has given a template file
138                                 if(it != parameters.end()){ 
139                                         path = m->hasPath(it->second);
140                                         //if the user has not given a path then, add inputdir. else leave path alone.
141                                         if (path == "") {       parameters["corraxes"] = inputDir + it->second;         }
142                                 }
143                 
144                 it = parameters.find("otucorr");
145                                 //user has given a template file
146                                 if(it != parameters.end()){ 
147                                         path = m->hasPath(it->second);
148                                         //if the user has not given a path then, add inputdir. else leave path alone.
149                                         if (path == "") {       parameters["otucorr"] = inputDir + it->second;          }
150                                 }
151                 
152                 it = parameters.find("list");
153                                 //user has given a template file
154                                 if(it != parameters.end()){ 
155                                         path = m->hasPath(it->second);
156                                         //if the user has not given a path then, add inputdir. else leave path alone.
157                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
158                                 }
159                 
160                 it = parameters.find("shared");
161                                 //user has given a template file
162                                 if(it != parameters.end()){ 
163                                         path = m->hasPath(it->second);
164                                         //if the user has not given a path then, add inputdir. else leave path alone.
165                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
166                                 }
167             }
168             
169             vector<string> tempOutNames;
170             outputTypes["constaxonomy"] = tempOutNames; 
171             outputTypes["otucorr"] = tempOutNames;
172             outputTypes["corraxes"] = tempOutNames;
173             outputTypes["shared"] = tempOutNames;
174             outputTypes["list"] = tempOutNames;
175
176             
177                         //check for parameters
178             accnosfile = validParameter.validFile(parameters, "accnos", true);
179                         if (accnosfile == "not open") { abort = true; }
180                         else if (accnosfile == "not found") {  
181                                 accnosfile = m->getAccnosFile(); 
182                                 if (accnosfile != "") {  m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
183                                 else { 
184                                         m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); 
185                                         abort = true;
186                                 } 
187                         }else { m->setAccnosFile(accnosfile); } 
188                         
189                         constaxonomyfile = validParameter.validFile(parameters, "constaxonomy", true);
190                         if (constaxonomyfile == "not open") { constaxonomyfile = ""; abort = true; }
191                         else if (constaxonomyfile == "not found") {  constaxonomyfile = "";  }
192             
193             corraxesfile = validParameter.validFile(parameters, "corraxes", true);
194                         if (corraxesfile == "not open") { corraxesfile = ""; abort = true; }
195                         else if (corraxesfile == "not found") {  corraxesfile = "";  }
196             
197             otucorrfile = validParameter.validFile(parameters, "otucorr", true);
198                         if (otucorrfile == "not open") { otucorrfile = ""; abort = true; }
199                         else if (otucorrfile == "not found") {  otucorrfile = "";  }
200             
201             listfile = validParameter.validFile(parameters, "list", true);
202                         if (listfile == "not open") { listfile = ""; abort = true; }
203                         else if (listfile == "not found") {  listfile = "";  }
204             else { m->setListFile(listfile); }
205             
206             sharedfile = validParameter.validFile(parameters, "shared", true);
207                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
208                         else if (sharedfile == "not found") {  sharedfile = "";  }
209             else { m->setSharedFile(sharedfile); }
210             
211             //if the user changes the output directory command factory will send this info to us in the output parameter 
212                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){   outputDir = "";        }
213             
214             if ((constaxonomyfile == "") && (corraxesfile == "") && (otucorrfile == "") && (sharedfile == "") && (listfile == ""))  { m->mothurOut("You must provide one of the following: constaxonomy, corraxes, otucorr, shared or list."); m->mothurOutEndLine(); abort = true; }
215             
216             if ((sharedfile != "") || (listfile != "")) {
217                 label = validParameter.validFile(parameters, "label", false);                   
218                 if (label == "not found") { label = ""; m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); label=""; }
219             }
220                 }
221                 
222         }
223         catch(exception& e) {
224                 m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
225                 exit(1);
226         }
227 }
228 //**********************************************************************************************************************
229
230 int RemoveOtuLabelsCommand::execute(){
231         try {
232                 
233                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
234         
235         //get labels you want to keep
236                 labels = m->readAccnos(accnosfile);
237         //simplfy labels
238         set<string> newLabels;
239         for (set<string>::iterator it = labels.begin(); it != labels.end(); it++) {  newLabels.insert(m->getSimpleLabel(*it)); }
240         labels = newLabels;
241         
242         if (m->debug) { m->mothurOut("[DEBUG]: numlabels = " + toString(labels.size()) + "\n"); }
243                 
244                 if (m->control_pressed) { return 0; }
245                 
246                 //read through the correct file and output lines you want to keep
247                 if (constaxonomyfile != "")     {               readClassifyOtu();      }
248                 if (corraxesfile != "")         {               readCorrAxes();         }
249                 if (otucorrfile != "")          {               readOtuAssociation();   }
250         if (listfile != "")         {           readList();             }
251         if (sharedfile != "")           {               readShared();           }
252         
253         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
254         
255         //output files created by command
256                 m->mothurOutEndLine();
257                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
258                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
259                 m->mothurOutEndLine();
260         
261         string current = "";
262         itTypes = outputTypes.find("list");
263         if (itTypes != outputTypes.end()) {
264             if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
265         }
266         
267         itTypes = outputTypes.find("shared");
268         if (itTypes != outputTypes.end()) {
269             if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
270         }
271         
272         return 0;
273     }
274         catch(exception& e) {
275                 m->errorOut(e, "GetOtuLabelsCommand", "execute");
276                 exit(1);
277         }
278 }
279 //**********************************************************************************************************************
280 int RemoveOtuLabelsCommand::readClassifyOtu(){
281         try {
282                 string thisOutputDir = outputDir;
283                 if (outputDir == "") {  thisOutputDir += m->hasPath(constaxonomyfile);  }
284                 map<string, string> variables; 
285         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile));
286         variables["[extension]"] = m->getExtension(constaxonomyfile);
287                 string outputFileName = getOutputFileName("constaxonomy", variables);
288                 ofstream out;
289                 m->openOutputFile(outputFileName, out);
290                 
291                 ifstream in;
292                 m->openInputFile(constaxonomyfile, in);
293                 
294                 bool wroteSomething = false;
295                 int removedCount = 0;
296                 
297         //read headers
298         string headers = m->getline(in); m->gobble(in);
299         out << headers << endl;
300         
301         while (!in.eof()) {
302             
303             if (m->control_pressed) { break; }
304             
305             string otu = ""; string tax = "unknown";
306             int size = 0;
307             
308             in >> otu >> size >> tax; m->gobble(in);
309             
310             if (m->debug) { m->mothurOut("[DEBUG]: " + otu + toString(size) + tax + "\n"); }
311             
312             if (labels.count(m->getSimpleLabel(otu)) == 0) {
313                                 wroteSomething = true;
314                 out << otu << '\t' << size << '\t' << tax << endl;
315             }else {  removedCount++;  }
316         }
317         in.close();
318         out.close();
319                 
320                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
321                 outputNames.push_back(outputFileName);  outputTypes["constaxonomy"].push_back(outputFileName);
322                 
323                 m->mothurOut("Removed " + toString(removedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
324                 
325                 return 0;
326                 
327         }
328         catch(exception& e) {
329                 m->errorOut(e, "RemoveOtuLabelsCommand", "readClassifyOtu");
330                 exit(1);
331         }
332 }
333 //**********************************************************************************************************************
334 int RemoveOtuLabelsCommand::readOtuAssociation(){
335         try {
336                 string thisOutputDir = outputDir;
337                 if (outputDir == "") {  thisOutputDir += m->hasPath(otucorrfile);  }
338         map<string, string> variables; 
339         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile));
340         variables["[extension]"] = m->getExtension(otucorrfile);
341                 string outputFileName = getOutputFileName("otucorr", variables);
342                 ofstream out;
343                 m->openOutputFile(outputFileName, out);
344                 
345                 ifstream in;
346                 m->openInputFile(otucorrfile, in);
347                 
348                 bool wroteSomething = false;
349                 int removedCount = 0;
350                 
351         //read headers
352         string headers = m->getline(in); m->gobble(in);
353         out << headers << endl;
354         
355         while (!in.eof()) {
356             
357             if (m->control_pressed) { break; }
358             
359             string otu1 = ""; 
360             string otu2 = ""; 
361             in >> otu1 >> otu2;
362             string line = m->getline(in); m->gobble(in);
363             
364             if ((labels.count(m->getSimpleLabel(otu1)) == 0) && (labels.count(m->getSimpleLabel(otu2)) == 0)){
365                                 wroteSomething = true;
366                 
367                 out << otu1 << '\t' << otu2 << '\t' << line << endl;
368             }else { removedCount++; }
369         }
370         in.close();
371         out.close();
372                 
373                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
374                 outputNames.push_back(outputFileName);  outputTypes["otucorr"].push_back(outputFileName);
375                 
376                 m->mothurOut("Removed " + toString(removedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
377                 
378                 return 0;
379                 
380         }
381         catch(exception& e) {
382                 m->errorOut(e, "RemoveOtuLabelsCommand", "readOtuAssociation");
383                 exit(1);
384         }
385 }
386 //**********************************************************************************************************************
387 int RemoveOtuLabelsCommand::readCorrAxes(){
388         try {
389                 string thisOutputDir = outputDir;
390                 if (outputDir == "") {  thisOutputDir += m->hasPath(corraxesfile);  }
391         map<string, string> variables; 
392         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile));
393         variables["[extension]"] = m->getExtension(corraxesfile);
394                 string outputFileName = getOutputFileName("corraxes", variables);
395                 ofstream out;
396                 m->openOutputFile(outputFileName, out);
397                 
398         
399                 ifstream in;
400                 m->openInputFile(corraxesfile, in);
401                 
402                 bool wroteSomething = false;
403                 int removedCount = 0;
404                 
405         //read headers
406         string headers = m->getline(in); m->gobble(in);
407         out << headers << endl;
408         
409         while (!in.eof()) {
410             
411             if (m->control_pressed) { break; }
412             
413             string otu = ""; 
414             in >> otu;
415             string line = m->getline(in); m->gobble(in);
416             
417             if (labels.count(m->getSimpleLabel(otu)) == 0) {
418                                 wroteSomething = true;
419                 
420                 out << otu << '\t' << line << endl;
421             }else { removedCount++; }
422         }
423         in.close();
424         out.close();
425                 
426                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
427                 outputNames.push_back(outputFileName);  outputTypes["corraxes"].push_back(outputFileName);
428                 
429                 m->mothurOut("Removed " + toString(removedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
430                 
431                 return 0;
432                 
433         }
434         catch(exception& e) {
435                 m->errorOut(e, "RemoveOtuLabelsCommand", "readCorrAxes");
436                 exit(1);
437         }
438 }
439 //**********************************************************************************************************************
440 int RemoveOtuLabelsCommand::readShared(){
441         try {
442         
443         getShared();
444         
445         if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } return 0; }
446         
447         vector<string> newLabels;
448         
449         //create new "filtered" lookup
450         vector<SharedRAbundVector*> newLookup;
451         for (int i = 0; i < lookup.size(); i++) {
452             SharedRAbundVector* temp = new SharedRAbundVector();
453                         temp->setLabel(lookup[i]->getLabel());
454                         temp->setGroup(lookup[i]->getGroup());
455                         newLookup.push_back(temp);
456         }
457         
458         bool wroteSomething = false;
459         int numRemoved = 0;
460         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
461             
462             if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } for (int j = 0; j < lookup.size(); j++) { delete lookup[j]; } return 0; }
463             
464             //is this otu on the list
465             if (labels.count(m->getSimpleLabel(m->currentSharedBinLabels[i])) == 0) {
466                 wroteSomething = true;
467                 newLabels.push_back(m->currentSharedBinLabels[i]);
468                 for (int j = 0; j < newLookup.size(); j++) { //add this OTU to the new lookup
469                     newLookup[j]->push_back(lookup[j]->getAbundance(i), lookup[j]->getGroup());
470                 }
471             }else { numRemoved++; }
472         }
473         
474         string thisOutputDir = outputDir;
475                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
476         map<string, string> variables; 
477                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile));
478         variables["[extension]"] = m->getExtension(sharedfile);
479         variables["[distance]"] = lookup[0]->getLabel();
480                 string outputFileName = getOutputFileName("shared", variables); 
481         ofstream out;
482                 m->openOutputFile(outputFileName, out);
483                 outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
484         
485                 for (int j = 0; j < lookup.size(); j++) { delete lookup[j]; }
486         
487         m->currentSharedBinLabels = newLabels;
488         
489                 newLookup[0]->printHeaders(out);
490                 
491                 for (int i = 0; i < newLookup.size(); i++) {
492                         out << newLookup[i]->getLabel() << '\t' << newLookup[i]->getGroup() << '\t';
493                         newLookup[i]->print(out);
494                 }
495                 out.close();
496         
497         for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; }
498         
499         if (wroteSomething == false) { m->mothurOut("Your file contains only OTUs from the .accnos file."); m->mothurOutEndLine();  }
500         
501                 m->mothurOut("Removed " + toString(numRemoved) + " OTUs from your shared file."); m->mothurOutEndLine();
502         
503         return 0;
504     }
505         catch(exception& e) {
506                 m->errorOut(e, "RemoveOtuLabelsCommand", "readShared");
507                 exit(1);
508         }
509 }
510 //**********************************************************************************************************************
511 int RemoveOtuLabelsCommand::readList(){
512         try {
513         getListVector();
514         
515         if (m->control_pressed) { delete list; return 0;}
516         
517         ListVector newList;
518         newList.setLabel(list->getLabel());
519         int removedCount = 0;
520         bool wroteSomething = false;
521         
522         vector<string> binLabels = list->getLabels();
523         vector<string> newLabels;
524         for (int i = 0; i < list->getNumBins(); i++) {
525             
526             if (m->control_pressed) { delete list; return 0;}
527             
528             if (labels.count(m->getSimpleLabel(binLabels[i])) == 0) {
529                 newList.push_back(list->get(i));
530                 newLabels.push_back(binLabels[i]);
531             }else { removedCount++; }
532         }
533         
534         string thisOutputDir = outputDir;
535                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
536         map<string, string> variables; 
537                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
538         variables["[extension]"] = m->getExtension(listfile);
539         variables["[distance]"] = list->getLabel();
540                 string outputFileName = getOutputFileName("list", variables);
541                 ofstream out;
542                 m->openOutputFile(outputFileName, out);
543         
544                 delete list;
545         //print new listvector
546         if (newList.getNumBins() != 0) {
547             wroteSomething = true;
548             newList.setLabels(newLabels);
549             newList.printHeaders(out);
550             newList.print(out);
551         }
552                 out.close();
553                 
554                 if (wroteSomething == false) { m->mothurOut("Your file contains only OTUs from the .accnos file."); m->mothurOutEndLine();  }
555                 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
556                 
557                 m->mothurOut("Removed " + toString(removedCount) + " OTUs from your list file."); m->mothurOutEndLine();
558         
559         return 0;
560     }
561     catch(exception& e) {
562         m->errorOut(e, "RemoveOtuLabelsCommand", "readList");
563         exit(1);
564     }
565 }
566 //**********************************************************************************************************************
567 int RemoveOtuLabelsCommand::getListVector(){
568         try {
569                 InputData input(listfile, "list");
570                 list = input.getListVector();
571                 string lastLabel = list->getLabel();
572                 
573                 if (label == "") { label = lastLabel;  return 0; }
574                 
575                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
576                 set<string> labels; labels.insert(label);
577                 set<string> processedLabels;
578                 set<string> userLabels = labels;
579                 
580                 //as long as you are not at the end of the file or done wih the lines you want
581                 while((list != NULL) && (userLabels.size() != 0)) {
582                         if (m->control_pressed) {  return 0;  }
583                         
584                         if(labels.count(list->getLabel()) == 1){
585                                 processedLabels.insert(list->getLabel());
586                                 userLabels.erase(list->getLabel());
587                                 break;
588                         }
589                         
590                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
591                                 string saveLabel = list->getLabel();
592                                 
593                                 delete list;
594                                 list = input.getListVector(lastLabel);
595                                 
596                                 processedLabels.insert(list->getLabel());
597                                 userLabels.erase(list->getLabel());
598                                 
599                                 //restore real lastlabel to save below
600                                 list->setLabel(saveLabel);
601                                 break;
602                         }
603                         
604                         lastLabel = list->getLabel();                   
605                         
606                         //get next line to process
607                         //prevent memory leak
608                         delete list; 
609                         list = input.getListVector();
610                 }
611                 
612                 
613                 if (m->control_pressed) {  return 0;  }
614                 
615                 //output error messages about any remaining user labels
616                 set<string>::iterator it;
617                 bool needToRun = false;
618                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
619                         m->mothurOut("Your file does not include the label " + *it); 
620                         if (processedLabels.count(lastLabel) != 1) {
621                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
622                                 needToRun = true;
623                         }else {
624                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
625                         }
626                 }
627                 
628                 //run last label if you need to
629                 if (needToRun == true)  {
630                         delete list; 
631                         list = input.getListVector(lastLabel);
632                 }       
633                 
634                 return 0;
635         }
636         catch(exception& e) {
637                 m->errorOut(e, "RemoveOtuLabelsCommand", "getListVector");      
638                 exit(1);
639         }
640 }
641 //**********************************************************************************************************************
642 int RemoveOtuLabelsCommand::getShared(){
643         try {
644                 InputData input(sharedfile, "sharedfile");
645                 lookup = input.getSharedRAbundVectors();
646                 string lastLabel = lookup[0]->getLabel();
647                 
648                 if (label == "") { label = lastLabel;  return 0; }
649                 
650                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
651                 set<string> labels; labels.insert(label);
652                 set<string> processedLabels;
653                 set<string> userLabels = labels;
654                 
655                 //as long as you are not at the end of the file or done wih the lines you want
656                 while((lookup[0] != NULL) && (userLabels.size() != 0)) {
657                         if (m->control_pressed) {   return 0;  }
658                         
659                         if(labels.count(lookup[0]->getLabel()) == 1){
660                                 processedLabels.insert(lookup[0]->getLabel());
661                                 userLabels.erase(lookup[0]->getLabel());
662                                 break;
663                         }
664                         
665                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
666                                 string saveLabel = lookup[0]->getLabel();
667                                 
668                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
669                                 lookup = input.getSharedRAbundVectors(lastLabel);
670                                 
671                                 processedLabels.insert(lookup[0]->getLabel());
672                                 userLabels.erase(lookup[0]->getLabel());
673                                 
674                                 //restore real lastlabel to save below
675                                 lookup[0]->setLabel(saveLabel);
676                                 break;
677                         }
678                         
679                         lastLabel = lookup[0]->getLabel();                      
680                         
681                         //get next line to process
682                         //prevent memory leak
683                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
684                         lookup = input.getSharedRAbundVectors();
685                 }
686                 
687                 
688                 if (m->control_pressed) {  return 0;  }
689                 
690                 //output error messages about any remaining user labels
691                 set<string>::iterator it;
692                 bool needToRun = false;
693                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
694                         m->mothurOut("Your file does not include the label " + *it); 
695                         if (processedLabels.count(lastLabel) != 1) {
696                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
697                                 needToRun = true;
698                         }else {
699                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
700                         }
701                 }
702                 
703                 //run last label if you need to
704                 if (needToRun == true)  {
705                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i];       } } 
706                         lookup = input.getSharedRAbundVectors(lastLabel);
707                 }       
708                 
709                 return 0;
710         }
711         catch(exception& e) {
712                 m->errorOut(e, "RemoveOtuLabelsCommand", "getShared");  
713                 exit(1);
714         }
715 }
716 //**********************************************************************************************************************
717
718
719