]> git.donarmstrong.com Git - mothur.git/blob - removeotulabelscommand.cpp
changing command name classify.shared to classifyrf.shared
[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         
238         if (m->debug) { m->mothurOut("[DEBUG]: numlabels = " + toString(labels.size()) + "\n"); }
239                 
240                 if (m->control_pressed) { return 0; }
241                 
242                 //read through the correct file and output lines you want to keep
243                 if (constaxonomyfile != "")     {               readClassifyOtu();      }
244                 if (corraxesfile != "")         {               readCorrAxes();         }
245                 if (otucorrfile != "")          {               readOtuAssociation();   }
246         if (listfile != "")         {           readList();             }
247         if (sharedfile != "")           {               readShared();           }
248         
249         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
250         
251         //output files created by command
252                 m->mothurOutEndLine();
253                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
254                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
255                 m->mothurOutEndLine();
256         
257         string current = "";
258         itTypes = outputTypes.find("list");
259         if (itTypes != outputTypes.end()) {
260             if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
261         }
262         
263         itTypes = outputTypes.find("shared");
264         if (itTypes != outputTypes.end()) {
265             if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
266         }
267         
268         return 0;
269     }
270         catch(exception& e) {
271                 m->errorOut(e, "GetOtuLabelsCommand", "execute");
272                 exit(1);
273         }
274 }
275 //**********************************************************************************************************************
276 int RemoveOtuLabelsCommand::readClassifyOtu(){
277         try {
278                 string thisOutputDir = outputDir;
279                 if (outputDir == "") {  thisOutputDir += m->hasPath(constaxonomyfile);  }
280                 map<string, string> variables; 
281         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile));
282         variables["[extension]"] = m->getExtension(constaxonomyfile);
283                 string outputFileName = getOutputFileName("constaxonomy", variables);
284                 ofstream out;
285                 m->openOutputFile(outputFileName, out);
286                 
287                 ifstream in;
288                 m->openInputFile(constaxonomyfile, in);
289                 
290                 bool wroteSomething = false;
291                 int removedCount = 0;
292                 
293         //read headers
294         string headers = m->getline(in); m->gobble(in);
295         out << headers << endl;
296         
297         while (!in.eof()) {
298             
299             if (m->control_pressed) { break; }
300             
301             string otu = ""; string tax = "unknown";
302             int size = 0;
303             
304             in >> otu >> size >> tax; m->gobble(in);
305             
306             if (m->debug) { m->mothurOut("[DEBUG]: " + otu + toString(size) + tax + "\n"); }
307             
308             if (labels.count(otu) == 0) {
309                                 wroteSomething = true;
310                 out << otu << '\t' << size << '\t' << tax << endl;
311             }else {  removedCount++;  }
312         }
313         in.close();
314         out.close();
315                 
316                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
317                 outputNames.push_back(outputFileName);  outputTypes["constaxonomy"].push_back(outputFileName);
318                 
319                 m->mothurOut("Removed " + toString(removedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
320                 
321                 return 0;
322                 
323         }
324         catch(exception& e) {
325                 m->errorOut(e, "RemoveOtuLabelsCommand", "readClassifyOtu");
326                 exit(1);
327         }
328 }
329 //**********************************************************************************************************************
330 int RemoveOtuLabelsCommand::readOtuAssociation(){
331         try {
332                 string thisOutputDir = outputDir;
333                 if (outputDir == "") {  thisOutputDir += m->hasPath(otucorrfile);  }
334         map<string, string> variables; 
335         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile));
336         variables["[extension]"] = m->getExtension(otucorrfile);
337                 string outputFileName = getOutputFileName("otucorr", variables);
338                 ofstream out;
339                 m->openOutputFile(outputFileName, out);
340                 
341                 ifstream in;
342                 m->openInputFile(otucorrfile, in);
343                 
344                 bool wroteSomething = false;
345                 int removedCount = 0;
346                 
347         //read headers
348         string headers = m->getline(in); m->gobble(in);
349         out << headers << endl;
350         
351         while (!in.eof()) {
352             
353             if (m->control_pressed) { break; }
354             
355             string otu1 = ""; 
356             string otu2 = ""; 
357             in >> otu1 >> otu2;
358             string line = m->getline(in); m->gobble(in);
359             
360             if ((labels.count(otu1) == 0) && (labels.count(otu2) == 0)){
361                                 wroteSomething = true;
362                 
363                 out << otu1 << '\t' << otu2 << '\t' << line << endl;
364             }else { removedCount++; }
365         }
366         in.close();
367         out.close();
368                 
369                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
370                 outputNames.push_back(outputFileName);  outputTypes["otucorr"].push_back(outputFileName);
371                 
372                 m->mothurOut("Removed " + toString(removedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
373                 
374                 return 0;
375                 
376         }
377         catch(exception& e) {
378                 m->errorOut(e, "RemoveOtuLabelsCommand", "readOtuAssociation");
379                 exit(1);
380         }
381 }
382 //**********************************************************************************************************************
383 int RemoveOtuLabelsCommand::readCorrAxes(){
384         try {
385                 string thisOutputDir = outputDir;
386                 if (outputDir == "") {  thisOutputDir += m->hasPath(corraxesfile);  }
387         map<string, string> variables; 
388         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile));
389         variables["[extension]"] = m->getExtension(corraxesfile);
390                 string outputFileName = getOutputFileName("corraxes", variables);
391                 ofstream out;
392                 m->openOutputFile(outputFileName, out);
393                 
394         
395                 ifstream in;
396                 m->openInputFile(corraxesfile, in);
397                 
398                 bool wroteSomething = false;
399                 int removedCount = 0;
400                 
401         //read headers
402         string headers = m->getline(in); m->gobble(in);
403         out << headers << endl;
404         
405         while (!in.eof()) {
406             
407             if (m->control_pressed) { break; }
408             
409             string otu = ""; 
410             in >> otu;
411             string line = m->getline(in); m->gobble(in);
412             
413             if (labels.count(otu) == 0) {
414                                 wroteSomething = true;
415                 
416                 out << otu << '\t' << line << endl;
417             }else { removedCount++; }
418         }
419         in.close();
420         out.close();
421                 
422                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
423                 outputNames.push_back(outputFileName);  outputTypes["corraxes"].push_back(outputFileName);
424                 
425                 m->mothurOut("Removed " + toString(removedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
426                 
427                 return 0;
428                 
429         }
430         catch(exception& e) {
431                 m->errorOut(e, "RemoveOtuLabelsCommand", "readCorrAxes");
432                 exit(1);
433         }
434 }
435 //**********************************************************************************************************************
436 int RemoveOtuLabelsCommand::readShared(){
437         try {
438         
439         getShared();
440         
441         if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } return 0; }
442         
443         vector<string> newLabels;
444         
445         //create new "filtered" lookup
446         vector<SharedRAbundVector*> newLookup;
447         for (int i = 0; i < lookup.size(); i++) {
448             SharedRAbundVector* temp = new SharedRAbundVector();
449                         temp->setLabel(lookup[i]->getLabel());
450                         temp->setGroup(lookup[i]->getGroup());
451                         newLookup.push_back(temp);
452         }
453         
454         bool wroteSomething = false;
455         int numRemoved = 0;
456         for (int i = 0; i < lookup[0]->getNumBins(); i++) {
457             
458             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; }
459             
460             //is this otu on the list
461             if (labels.count(m->currentBinLabels[i]) == 0) {
462                 wroteSomething = true;
463                 newLabels.push_back(m->currentBinLabels[i]);
464                 for (int j = 0; j < newLookup.size(); j++) { //add this OTU to the new lookup
465                     newLookup[j]->push_back(lookup[j]->getAbundance(i), lookup[j]->getGroup());
466                 }
467             }else { numRemoved++; }
468         }
469         
470         string thisOutputDir = outputDir;
471                 if (outputDir == "") {  thisOutputDir += m->hasPath(sharedfile);  }
472         map<string, string> variables; 
473                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile));
474         variables["[extension]"] = m->getExtension(sharedfile);
475         variables["[distance]"] = lookup[0]->getLabel();
476                 string outputFileName = getOutputFileName("shared", variables); 
477         ofstream out;
478                 m->openOutputFile(outputFileName, out);
479                 outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
480         
481                 for (int j = 0; j < lookup.size(); j++) { delete lookup[j]; }
482         
483         m->currentBinLabels = newLabels;
484         
485                 newLookup[0]->printHeaders(out);
486                 
487                 for (int i = 0; i < newLookup.size(); i++) {
488                         out << newLookup[i]->getLabel() << '\t' << newLookup[i]->getGroup() << '\t';
489                         newLookup[i]->print(out);
490                 }
491                 out.close();
492         
493         for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; }
494         
495         if (wroteSomething == false) { m->mothurOut("Your file contains only OTUs from the .accnos file."); m->mothurOutEndLine();  }
496         
497                 m->mothurOut("Removed " + toString(numRemoved) + " OTUs from your shared file."); m->mothurOutEndLine();
498         
499         return 0;
500     }
501         catch(exception& e) {
502                 m->errorOut(e, "RemoveOtuLabelsCommand", "readShared");
503                 exit(1);
504         }
505 }
506 //**********************************************************************************************************************
507 int RemoveOtuLabelsCommand::readList(){
508         try {
509         getListVector();
510         
511         if (m->control_pressed) { delete list; return 0;}
512         
513         ListVector newList;
514         newList.setLabel(list->getLabel());
515         int removedCount = 0;
516         bool wroteSomething = false;
517         string snumBins = toString(list->getNumBins());
518         
519         for (int i = 0; i < list->getNumBins(); i++) {
520             
521             if (m->control_pressed) { delete list; return 0;}
522             
523             //create a label for this otu
524             string otuLabel = "Otu";
525             string sbinNumber = toString(i+1);
526             if (sbinNumber.length() < snumBins.length()) { 
527                 int diff = snumBins.length() - sbinNumber.length();
528                 for (int h = 0; h < diff; h++) { otuLabel += "0"; }
529             }
530             otuLabel += sbinNumber; 
531             
532             if (labels.count(otuLabel) == 0) {
533                 newList.push_back(list->get(i));
534             }else { removedCount++; }
535         }
536         
537         string thisOutputDir = outputDir;
538                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
539         map<string, string> variables; 
540                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
541         variables["[extension]"] = m->getExtension(listfile);
542         variables["[distance]"] = list->getLabel();
543                 string outputFileName = getOutputFileName("list", variables);
544                 ofstream out;
545                 m->openOutputFile(outputFileName, out);
546         
547                 delete list;
548         //print new listvector
549         if (newList.getNumBins() != 0) {
550             wroteSomething = true;
551             newList.print(out);
552         }
553                 out.close();
554                 
555                 if (wroteSomething == false) { m->mothurOut("Your file contains only OTUs from the .accnos file."); m->mothurOutEndLine();  }
556                 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
557                 
558                 m->mothurOut("Removed " + toString(removedCount) + " OTUs from your list file."); m->mothurOutEndLine();
559         
560         return 0;
561     }
562     catch(exception& e) {
563         m->errorOut(e, "RemoveOtuLabelsCommand", "readList");
564         exit(1);
565     }
566 }
567 //**********************************************************************************************************************
568 int RemoveOtuLabelsCommand::getListVector(){
569         try {
570                 InputData input(listfile, "list");
571                 list = input.getListVector();
572                 string lastLabel = list->getLabel();
573                 
574                 if (label == "") { label = lastLabel;  return 0; }
575                 
576                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
577                 set<string> labels; labels.insert(label);
578                 set<string> processedLabels;
579                 set<string> userLabels = labels;
580                 
581                 //as long as you are not at the end of the file or done wih the lines you want
582                 while((list != NULL) && (userLabels.size() != 0)) {
583                         if (m->control_pressed) {  return 0;  }
584                         
585                         if(labels.count(list->getLabel()) == 1){
586                                 processedLabels.insert(list->getLabel());
587                                 userLabels.erase(list->getLabel());
588                                 break;
589                         }
590                         
591                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
592                                 string saveLabel = list->getLabel();
593                                 
594                                 delete list;
595                                 list = input.getListVector(lastLabel);
596                                 
597                                 processedLabels.insert(list->getLabel());
598                                 userLabels.erase(list->getLabel());
599                                 
600                                 //restore real lastlabel to save below
601                                 list->setLabel(saveLabel);
602                                 break;
603                         }
604                         
605                         lastLabel = list->getLabel();                   
606                         
607                         //get next line to process
608                         //prevent memory leak
609                         delete list; 
610                         list = input.getListVector();
611                 }
612                 
613                 
614                 if (m->control_pressed) {  return 0;  }
615                 
616                 //output error messages about any remaining user labels
617                 set<string>::iterator it;
618                 bool needToRun = false;
619                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
620                         m->mothurOut("Your file does not include the label " + *it); 
621                         if (processedLabels.count(lastLabel) != 1) {
622                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
623                                 needToRun = true;
624                         }else {
625                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
626                         }
627                 }
628                 
629                 //run last label if you need to
630                 if (needToRun == true)  {
631                         delete list; 
632                         list = input.getListVector(lastLabel);
633                 }       
634                 
635                 return 0;
636         }
637         catch(exception& e) {
638                 m->errorOut(e, "RemoveOtuLabelsCommand", "getListVector");      
639                 exit(1);
640         }
641 }
642 //**********************************************************************************************************************
643 int RemoveOtuLabelsCommand::getShared(){
644         try {
645                 InputData input(sharedfile, "sharedfile");
646                 lookup = input.getSharedRAbundVectors();
647                 string lastLabel = lookup[0]->getLabel();
648                 
649                 if (label == "") { label = lastLabel;  return 0; }
650                 
651                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
652                 set<string> labels; labels.insert(label);
653                 set<string> processedLabels;
654                 set<string> userLabels = labels;
655                 
656                 //as long as you are not at the end of the file or done wih the lines you want
657                 while((lookup[0] != NULL) && (userLabels.size() != 0)) {
658                         if (m->control_pressed) {   return 0;  }
659                         
660                         if(labels.count(lookup[0]->getLabel()) == 1){
661                                 processedLabels.insert(lookup[0]->getLabel());
662                                 userLabels.erase(lookup[0]->getLabel());
663                                 break;
664                         }
665                         
666                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
667                                 string saveLabel = lookup[0]->getLabel();
668                                 
669                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
670                                 lookup = input.getSharedRAbundVectors(lastLabel);
671                                 
672                                 processedLabels.insert(lookup[0]->getLabel());
673                                 userLabels.erase(lookup[0]->getLabel());
674                                 
675                                 //restore real lastlabel to save below
676                                 lookup[0]->setLabel(saveLabel);
677                                 break;
678                         }
679                         
680                         lastLabel = lookup[0]->getLabel();                      
681                         
682                         //get next line to process
683                         //prevent memory leak
684                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
685                         lookup = input.getSharedRAbundVectors();
686                 }
687                 
688                 
689                 if (m->control_pressed) {  return 0;  }
690                 
691                 //output error messages about any remaining user labels
692                 set<string>::iterator it;
693                 bool needToRun = false;
694                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
695                         m->mothurOut("Your file does not include the label " + *it); 
696                         if (processedLabels.count(lastLabel) != 1) {
697                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
698                                 needToRun = true;
699                         }else {
700                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
701                         }
702                 }
703                 
704                 //run last label if you need to
705                 if (needToRun == true)  {
706                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i];       } } 
707                         lookup = input.getSharedRAbundVectors(lastLabel);
708                 }       
709                 
710                 return 0;
711         }
712         catch(exception& e) {
713                 m->errorOut(e, "RemoveOtuLabelsCommand", "getShared");  
714                 exit(1);
715         }
716 }
717 //**********************************************************************************************************************
718
719
720