]> git.donarmstrong.com Git - mothur.git/blob - getotulabelscommand.cpp
Merge remote-tracking branch 'origin/master'
[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 potucorr("otucorr", "InputTypes", "", "", "none", "FNGLT", "none","otucorr",false,false, true); parameters.push_back(potucorr);
17         CommandParameter pcorraxes("corraxes", "InputTypes", "", "", "none", "FNGLT", "none","corraxes",false,false, true); parameters.push_back(pcorraxes);
18         CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
19                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "GetOtuLabelsCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string GetOtuLabelsCommand::getHelpString(){    
32         try {
33                 string helpString = "";
34                 helpString += "The get.otulabels command can be used to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n";
35                 helpString += "The get.otulabels parameters are: constaxonomy, otucorr, corraxes, and accnos.\n";
36                 helpString += "The constaxonomy parameter is used to input the results of the classify.otu command.\n";
37         helpString += "The otucorr parameter is used to input the results of the otu.association command.\n";
38         helpString += "The corraxes parameter is used to input the results of the corr.axes command.\n";
39                 helpString += "The get.otulabels commmand should be in the following format: \n";
40                 helpString += "get.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "GetOtuLabelsCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 string GetOtuLabelsCommand::getOutputPattern(string type) {
50     try {
51         string pattern = "";
52         
53         if (type == "constaxonomy")            {   pattern = "[filename],pick,[extension]";    }
54         else if (type == "otucorr")    {   pattern = "[filename],pick,[extension]";    }
55         else if (type == "corraxes")        {   pattern = "[filename],pick,[extension]";    }
56         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
57         
58         return pattern;
59     }
60     catch(exception& e) {
61         m->errorOut(e, "GetOtuLabelsCommand", "getOutputPattern");
62         exit(1);
63     }
64 }
65
66 //**********************************************************************************************************************
67 GetOtuLabelsCommand::GetOtuLabelsCommand(){     
68         try {
69                 abort = true; calledHelp = true;
70                 setParameters();
71         vector<string> tempOutNames;
72                 outputTypes["constaxonomy"] = tempOutNames; 
73         outputTypes["otucorr"] = tempOutNames;
74         outputTypes["corraxes"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "GetOtuLabelsCommand", "GetOtuLabelsCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82 GetOtuLabelsCommand::GetOtuLabelsCommand(string option)  {
83         try {
84                 abort = false; calledHelp = false;   
85         
86                 //allow user to run help
87                 if(option == "help") { help(); abort = true; calledHelp = true; }
88                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
89                 
90                 else {
91                         //valid paramters for this command
92                         vector<string> myArray = setParameters();
93                         
94                         OptionParser parser(option);
95                         map<string,string> parameters = parser.getParameters();
96                         
97                         ValidParameters validParameter;
98                         map<string,string>::iterator it;
99                         //check to make sure all parameters are valid for command
100                         for (it = parameters.begin(); it != parameters.end(); it++) { 
101                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
102                         }
103                         
104                         
105                         //if the user changes the input directory command factory will send this info to us in the output parameter 
106                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
107                         if (inputDir == "not found"){   inputDir = "";          }
108                         else {
109                 
110                 //edit file types below to include only the types you added as parameters
111                 
112                                 string path;
113                 it = parameters.find("constaxonomy");
114                                 //user has given a template file
115                                 if(it != parameters.end()){ 
116                                         path = m->hasPath(it->second);
117                                         //if the user has not given a path then, add inputdir. else leave path alone.
118                                         if (path == "") {       parameters["constaxonomy"] = inputDir + it->second;             }
119                                 }
120                 
121                 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second;           }
127                                 }
128                 
129                 it = parameters.find("corraxes");
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["corraxes"] = inputDir + it->second;         }
135                                 }
136                 
137                 it = parameters.find("otucorr");
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["otucorr"] = inputDir + it->second;          }
143                                 }
144             }
145             
146             vector<string> tempOutNames;
147             outputTypes["constaxonomy"] = tempOutNames; 
148             outputTypes["otucorr"] = tempOutNames;
149             outputTypes["corraxes"] = tempOutNames;
150             
151                         //check for parameters
152             accnosfile = validParameter.validFile(parameters, "accnos", true);
153                         if (accnosfile == "not open") { abort = true; }
154                         else if (accnosfile == "not found") {  
155                                 accnosfile = m->getAccnosFile(); 
156                                 if (accnosfile != "") {  m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
157                                 else { 
158                                         m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); 
159                                         abort = true;
160                                 } 
161                         }else { m->setAccnosFile(accnosfile); } 
162                         
163                         constaxonomyfile = validParameter.validFile(parameters, "constaxonomy", true);
164                         if (constaxonomyfile == "not open") { constaxonomyfile = ""; abort = true; }
165                         else if (constaxonomyfile == "not found") {  constaxonomyfile = "";  }
166             
167             corraxesfile = validParameter.validFile(parameters, "corraxes", true);
168                         if (corraxesfile == "not open") { corraxesfile = ""; abort = true; }
169                         else if (corraxesfile == "not found") {  corraxesfile = "";  }
170             
171             otucorrfile = validParameter.validFile(parameters, "otucorr", true);
172                         if (otucorrfile == "not open") { otucorrfile = ""; abort = true; }
173                         else if (otucorrfile == "not found") {  otucorrfile = "";  }
174
175             
176             //if the user changes the output directory command factory will send this info to us in the output parameter 
177                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){   outputDir = "";        }
178             
179             if ((constaxonomyfile == "") && (corraxesfile == "") && (otucorrfile == ""))  { m->mothurOut("You must provide one of the following: constaxonomy, corraxes or otucorr."); m->mothurOutEndLine(); abort = true; }
180                 }
181                 
182         }
183         catch(exception& e) {
184                 m->errorOut(e, "GetOtuLabelsCommand", "GetOtuLabelsCommand");
185                 exit(1);
186         }
187 }
188 //**********************************************************************************************************************
189
190 int GetOtuLabelsCommand::execute(){
191         try {
192                 
193                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
194         
195         //get labels you want to keep
196                 labels = m->readAccnos(accnosfile);
197                 
198                 if (m->control_pressed) { return 0; }
199                 
200                 //read through the correct file and output lines you want to keep
201                 if (constaxonomyfile != "")     {               readClassifyOtu();      }
202                 if (corraxesfile != "")         {               readCorrAxes();         }
203                 if (otucorrfile != "")          {               readOtuAssociation();   }
204         
205         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
206         
207         //output files created by command
208                 m->mothurOutEndLine();
209                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
210                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
211                 m->mothurOutEndLine();
212         
213         return 0;
214     }
215         catch(exception& e) {
216                 m->errorOut(e, "GetOtuLabelsCommand", "execute");
217                 exit(1);
218         }
219 }
220 //**********************************************************************************************************************
221 int GetOtuLabelsCommand::readClassifyOtu(){
222         try {
223                 string thisOutputDir = outputDir;
224                 if (outputDir == "") {  thisOutputDir += m->hasPath(constaxonomyfile);  }
225         map<string, string> variables; 
226         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile));
227         variables["[extension]"] = m->getExtension(constaxonomyfile);
228                 string outputFileName = getOutputFileName("constaxonomy", variables);
229                 
230                 ofstream out;
231                 m->openOutputFile(outputFileName, out);
232                 
233                 ifstream in;
234                 m->openInputFile(constaxonomyfile, in);
235                 
236                 bool wroteSomething = false;
237                 int selectedCount = 0;
238                 
239         //read headers
240         string headers = m->getline(in);
241         out << headers << endl;
242         
243         while (!in.eof()) {
244             
245             if (m->control_pressed) { break; }
246             
247             string otu = ""; string tax = "unknown";
248             int size = 0;
249             
250             in >> otu >> size >> tax; m->gobble(in);
251             
252             if (labels.count(otu) != 0) {
253                                 wroteSomething = true;
254                                 selectedCount++;
255                 
256                 out << otu << '\t' << size << '\t' << tax << endl;
257             }
258         }
259         in.close();
260         out.close();
261                 
262                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine();  }
263                 outputNames.push_back(outputFileName);  outputTypes["constaxonomy"].push_back(outputFileName);
264                 
265                 m->mothurOut("Selected " + toString(selectedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
266                 
267                 return 0;
268                 
269         }
270         catch(exception& e) {
271                 m->errorOut(e, "GetOtuLabelsCommand", "readClassifyOtu");
272                 exit(1);
273         }
274 }
275 //**********************************************************************************************************************
276 int GetOtuLabelsCommand::readOtuAssociation(){
277         try {
278                 string thisOutputDir = outputDir;
279                 if (outputDir == "") {  thisOutputDir += m->hasPath(otucorrfile);  }
280         map<string, string> variables; 
281         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile));
282         variables["[extension]"] = m->getExtension(otucorrfile);
283                 string outputFileName = getOutputFileName("otucorr", variables);
284
285                 ofstream out;
286                 m->openOutputFile(outputFileName, out);
287                 
288                 ifstream in;
289                 m->openInputFile(otucorrfile, in);
290                 
291                 bool wroteSomething = false;
292                 int selectedCount = 0;
293                 
294         //read headers
295         string headers = m->getline(in);
296         out << headers << endl;
297         
298         while (!in.eof()) {
299             
300             if (m->control_pressed) { break; }
301             
302             string otu1 = ""; 
303             string otu2 = ""; 
304             in >> otu1 >> otu2;
305             string line = m->getline(in); m->gobble(in);
306             
307             if ((labels.count(otu1) != 0) && (labels.count(otu2) != 0)){
308                                 wroteSomething = true;
309                                 selectedCount++;
310                 
311                 out << otu1 << '\t' << otu2 << '\t' << line << endl;
312             }
313         }
314         in.close();
315         out.close();
316                 
317                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine();  }
318                 outputNames.push_back(outputFileName);  outputTypes["otucorr"].push_back(outputFileName);
319                 
320                 m->mothurOut("Selected " + toString(selectedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
321                 
322                 return 0;
323                 
324         }
325         catch(exception& e) {
326                 m->errorOut(e, "GetOtuLabelsCommand", "readOtuAssociation");
327                 exit(1);
328         }
329 }
330 //**********************************************************************************************************************
331 int GetOtuLabelsCommand::readCorrAxes(){
332         try {
333                 string thisOutputDir = outputDir;
334                 if (outputDir == "") {  thisOutputDir += m->hasPath(corraxesfile);  }
335         map<string, string> variables; 
336         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile));
337         variables["[extension]"] = m->getExtension(corraxesfile);
338                 string outputFileName = getOutputFileName("corraxes", variables);
339
340                 ofstream out;
341                 m->openOutputFile(outputFileName, out);
342                 
343         
344                 ifstream in;
345                 m->openInputFile(corraxesfile, in);
346                 
347                 bool wroteSomething = false;
348                 int selectedCount = 0;
349                 
350         //read headers
351         string headers = m->getline(in);
352         out << headers << endl;
353         
354         while (!in.eof()) {
355             
356             if (m->control_pressed) { break; }
357             
358             string otu = ""; 
359             in >> otu;
360             string line = m->getline(in); m->gobble(in);
361             
362             if (labels.count(otu) != 0) {
363                                 wroteSomething = true;
364                                 selectedCount++;
365                 
366                 out << otu << '\t' << line << endl;
367             }
368         }
369         in.close();
370         out.close();
371                 
372                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine();  }
373                 outputNames.push_back(outputFileName);  outputTypes["corraxes"].push_back(outputFileName);
374                 
375                 m->mothurOut("Selected " + toString(selectedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
376                 
377                 return 0;
378                 
379         }
380         catch(exception& e) {
381                 m->errorOut(e, "GetOtuLabelsCommand", "readCorrAxes");
382                 exit(1);
383         }
384 }
385 //**********************************************************************************************************************