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