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