]> git.donarmstrong.com Git - mothur.git/blob - removeotulabelscommand.cpp
added get.otulabels and remove.otulabels commands per user request
[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); 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, "RemoveOtuLabelsCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string RemoveOtuLabelsCommand::getHelpString(){ 
32         try {
33                 string helpString = "";
34                 helpString += "The remove.otulabels command can be used to remove specific otus with the output from classify.otu, otu.association, or corr.axes.\n";
35                 helpString += "The remove.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 remove.otulabels commmand should be in the following format: \n";
40                 helpString += "remove.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "RemoveOtuLabelsCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(){       
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, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
60                 exit(1);
61         }
62 }
63 //**********************************************************************************************************************
64 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(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, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
167                 exit(1);
168         }
169 }
170 //**********************************************************************************************************************
171
172 int RemoveOtuLabelsCommand::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 RemoveOtuLabelsCommand::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 removedCount = 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                 out << otu << '\t' << size << '\t' << tax << endl;
233             }else {  removedCount++;  }
234         }
235         in.close();
236         out.close();
237                 
238                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
239                 outputNames.push_back(outputFileName);  outputTypes["constaxonomy"].push_back(outputFileName);
240                 
241                 m->mothurOut("Removed " + toString(removedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
242                 
243                 return 0;
244                 
245         }
246         catch(exception& e) {
247                 m->errorOut(e, "RemoveOtuLabelsCommand", "readClassifyOtu");
248                 exit(1);
249         }
250 }
251 //**********************************************************************************************************************
252 int RemoveOtuLabelsCommand::readOtuAssociation(){
253         try {
254                 string thisOutputDir = outputDir;
255                 if (outputDir == "") {  thisOutputDir += m->hasPath(otucorrfile);  }
256                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile)) + "pick.corr";
257                 ofstream out;
258                 m->openOutputFile(outputFileName, out);
259                 
260                 ifstream in;
261                 m->openInputFile(otucorrfile, in);
262                 
263                 bool wroteSomething = false;
264                 int removedCount = 0;
265                 
266         //read headers
267         string headers = m->getline(in);
268         out << headers << endl;
269         
270         while (!in.eof()) {
271             
272             if (m->control_pressed) { break; }
273             
274             string otu1 = ""; 
275             string otu2 = ""; 
276             in >> otu1 >> otu2;
277             string line = m->getline(in); m->gobble(in);
278             
279             if ((labels.count(otu1) == 0) && (labels.count(otu2) == 0)){
280                                 wroteSomething = true;
281                 
282                 out << otu1 << '\t' << otu2 << '\t' << line << endl;
283             }else { removedCount++; }
284         }
285         in.close();
286         out.close();
287                 
288                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
289                 outputNames.push_back(outputFileName);  outputTypes["otu.corr"].push_back(outputFileName);
290                 
291                 m->mothurOut("Removed " + toString(removedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
292                 
293                 return 0;
294                 
295         }
296         catch(exception& e) {
297                 m->errorOut(e, "RemoveOtuLabelsCommand", "readOtuAssociation");
298                 exit(1);
299         }
300 }
301 //**********************************************************************************************************************
302 int RemoveOtuLabelsCommand::readCorrAxes(){
303         try {
304                 string thisOutputDir = outputDir;
305                 if (outputDir == "") {  thisOutputDir += m->hasPath(corraxesfile);  }
306                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile)) + "pick.axes";
307                 ofstream out;
308                 m->openOutputFile(outputFileName, out);
309                 
310         
311                 ifstream in;
312                 m->openInputFile(corraxesfile, in);
313                 
314                 bool wroteSomething = false;
315                 int removedCount = 0;
316                 
317         //read headers
318         string headers = m->getline(in);
319         out << headers << endl;
320         
321         while (!in.eof()) {
322             
323             if (m->control_pressed) { break; }
324             
325             string otu = ""; 
326             in >> otu;
327             string line = m->getline(in); m->gobble(in);
328             
329             if (labels.count(otu) == 0) {
330                                 wroteSomething = true;
331                 
332                 out << otu << '\t' << line << endl;
333             }else { removedCount++; }
334         }
335         in.close();
336         out.close();
337                 
338                 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine();  }
339                 outputNames.push_back(outputFileName);  outputTypes["corr.axes"].push_back(outputFileName);
340                 
341                 m->mothurOut("Removed " + toString(removedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
342                 
343                 return 0;
344                 
345         }
346         catch(exception& e) {
347                 m->errorOut(e, "RemoveOtuLabelsCommand", "readCorrAxes");
348                 exit(1);
349         }
350 }
351
352 //**********************************************************************************************************************
353 int RemoveOtuLabelsCommand::readAccnos(){
354         try {
355                 
356                 ifstream in;
357                 m->openInputFile(accnosfile, in);
358                 string name;
359                 
360                 while(!in.eof()){
361                         in >> name;
362             
363                         labels.insert(name);
364                         
365                         m->gobble(in);
366                 }
367                 in.close();     
368                 
369                 return 0;
370         
371         }
372         catch(exception& e) {
373                 m->errorOut(e, "RemoveOtuLabelsCommand", "readAccnos");
374                 exit(1);
375         }
376 }
377 //**********************************************************************************************************************
378
379
380