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