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