]> git.donarmstrong.com Git - mothur.git/blob - chimerabellerophoncommand.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / chimerabellerophoncommand.cpp
1 /*
2  *  chimerabellerophoncommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 4/1/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chimerabellerophoncommand.h"
11 #include "bellerophon.h"
12
13 //**********************************************************************************************************************
14 vector<string> ChimeraBellerophonCommand::setParameters(){      
15         try {
16                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none","none","none",false,true); parameters.push_back(pfasta);
17                 CommandParameter pfilter("filter", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pfilter);
18                 CommandParameter pcorrection("correction", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pcorrection);
19                 CommandParameter pwindow("window", "Number", "", "0", "", "", "",false,false); parameters.push_back(pwindow);
20                 CommandParameter pincrement("increment", "Number", "", "25", "", "", "",false,false); parameters.push_back(pincrement);
21                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
22                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
23                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
24                 
25                 vector<string> myArray;
26                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "ChimeraBellerophonCommand", "setParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 string ChimeraBellerophonCommand::getHelpString(){      
36         try {
37                 string helpString = "";
38                 helpString += "The chimera.bellerophon command reads a fastafile and creates list of potentially chimeric sequences.\n";
39                 helpString += "The chimera.bellerophon command parameters are fasta, filter, correction, processors, window, increment. The fasta parameter is required, unless you have a valid current file.\n";
40                 helpString += "The fasta parameter is required.  You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n";
41                 helpString += "The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter, default=false. \n";
42                 helpString += "The correction parameter allows you to put more emphasis on the distance between highly similar sequences and less emphasis on the differences between remote homologs, default=true.\n";
43                 helpString += "The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n";
44 #ifdef USE_MPI
45                 helpString += "When using MPI, the processors parameter is set to the number of MPI processes running. \n";
46 #endif
47                 helpString += "The window parameter allows you to specify the window size for searching for chimeras, default is 1/4 sequence length. \n";
48                 helpString += "The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 25.\n";
49                 helpString += "chimera.bellerophon(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors) \n";
50                 helpString += "Example: chimera.bellerophon(fasta=AD.align, filter=True, correction=true, window=200) \n";
51                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";       
52                 return helpString;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "ChimeraBellerophonCommand", "getHelpString");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 string ChimeraBellerophonCommand::getOutputFileNameTag(string type, string inputName=""){       
61         try {
62         string outputFileName = "";
63                 map<string, vector<string> >::iterator it;
64         
65         //is this a type this command creates
66         it = outputTypes.find(type);
67         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
68         else {
69             if (type == "chimera") {  outputFileName =  "bellerophon.chimeras"; }
70             else if (type == "accnos") {  outputFileName =  "bellerophon.accnos"; }
71             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
72         }
73         return outputFileName;
74         }
75         catch(exception& e) {
76                 m->errorOut(e, "ChimeraBellerophonCommand", "getOutputFileNameTag");
77                 exit(1);
78         }
79 }
80 //**********************************************************************************************************************
81 ChimeraBellerophonCommand::ChimeraBellerophonCommand(){ 
82         try {
83                 abort = true; calledHelp = true; 
84                 setParameters();
85                 vector<string> tempOutNames;
86                 outputTypes["chimera"] = tempOutNames;
87                 outputTypes["accnos"] = tempOutNames;
88         }
89         catch(exception& e) {
90                 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
91                 exit(1);
92         }
93 }
94 //***************************************************************************************************************
95 ChimeraBellerophonCommand::ChimeraBellerophonCommand(string option)  {
96         try {
97                 abort = false; calledHelp = false;   
98                 
99                 //allow user to run help
100                 if(option == "help") { help(); abort = true; calledHelp = true; }
101                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
102                 
103                 else {
104                         
105                         vector<string> myArray = setParameters();
106                         
107                         OptionParser parser(option);
108                         map<string,string> parameters = parser.getParameters();
109                         
110                         ValidParameters validParameter("chimera.bellerophon");
111                         map<string,string>::iterator it;
112                         
113                         //check to make sure all parameters are valid for command
114                         for (it = parameters.begin(); it != parameters.end(); it++) { 
115                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
116                         }
117                         
118                         //initialize outputTypes
119                         vector<string> tempOutNames;
120                         outputTypes["chimera"] = tempOutNames;
121                         outputTypes["accnos"] = tempOutNames;
122                 
123                         //if the user changes the input directory command factory will send this info to us in the output parameter 
124                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
125                         if (inputDir == "not found"){   inputDir = "";          }
126                         
127                         fastafile = validParameter.validFile(parameters, "fasta", false);
128                         if (fastafile == "not found") {                                 
129                                 //if there is a current fasta file, use it
130                                 string filename = m->getFastaFile(); 
131                                 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
132                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
133                         }else { 
134                                 m->splitAtDash(fastafile, fastaFileNames);
135                                 
136                                 //go through files and make sure they are good, if not, then disregard them
137                                 for (int i = 0; i < fastaFileNames.size(); i++) {
138                                         
139                                         bool ignore = false;
140                                         if (fastaFileNames[i] == "current") { 
141                                                 fastaFileNames[i] = m->getFastaFile(); 
142                                                 if (fastaFileNames[i] != "") {  m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
143                                                 else {  
144                                                         m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
145                                                         //erase from file list
146                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
147                                                         i--;
148                                                 }
149                                         }
150                                         
151                                         if (!ignore) {
152                                         
153                                                 if (inputDir != "") {
154                                                         string path = m->hasPath(fastaFileNames[i]);
155                                                         //if the user has not given a path then, add inputdir. else leave path alone.
156                                                         if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
157                                                 }
158                 
159                                                 int ableToOpen;
160                                                 ifstream in;
161
162                                                 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
163                                         
164                                                 //if you can't open it, try default location
165                                                 if (ableToOpen == 1) {
166                                                         if (m->getDefaultPath() != "") { //default path is set
167                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
168                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
169                                                                 ifstream in2;
170                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
171                                                                 in2.close();
172                                                                 fastaFileNames[i] = tryPath;
173                                                         }
174                                                 }
175                                                 
176                                                 //if you can't open it, try default location
177                                                 if (ableToOpen == 1) {
178                                                         if (m->getOutputDir() != "") { //default path is set
179                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
180                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
181                                                                 ifstream in2;
182                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
183                                                                 in2.close();
184                                                                 fastaFileNames[i] = tryPath;
185                                                         }
186                                                 }
187                                                 
188                                                 in.close();
189
190                                                 if (ableToOpen == 1) { 
191                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
192                                                         //erase from file list
193                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
194                                                         i--;
195                                                 }else {
196                                                         m->setFastaFile(fastaFileNames[i]);
197                                                 }
198                                         }
199                                 }
200                                 
201                                 //make sure there is at least one valid file left
202                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
203                         }
204                         
205                         //if the user changes the output directory command factory will send this info to us in the output parameter 
206                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
207
208                         string temp;
209                         temp = validParameter.validFile(parameters, "filter", false);                   if (temp == "not found") { temp = "F"; }
210                         filter = m->isTrue(temp);
211                         
212                         temp = validParameter.validFile(parameters, "correction", false);               if (temp == "not found") { temp = "T"; }
213                         correction = m->isTrue(temp);
214                         
215                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
216                         m->setProcessors(temp);
217                         m->mothurConvert(temp, processors);
218                         
219                         temp = validParameter.validFile(parameters, "window", false);                   if (temp == "not found") { temp = "0"; }
220                         m->mothurConvert(temp, window);
221                         
222                         temp = validParameter.validFile(parameters, "increment", false);                if (temp == "not found") { temp = "25"; }
223                         m->mothurConvert(temp, increment);
224                 }
225         }
226         catch(exception& e) {
227                 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
228                 exit(1);
229         }
230 }
231 //***************************************************************************************************************
232 int ChimeraBellerophonCommand::execute(){
233         try{
234                 
235                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
236                 
237                 for (int i = 0; i < fastaFileNames.size(); i++) {
238                         
239                         m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
240                         
241                         int start = time(NULL); 
242                         
243                         chimera = new Bellerophon(fastaFileNames[i], filter, correction, window, increment, processors, outputDir);     
244                         
245                         if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]);  }//if user entered a file with a path then preserve it               
246                         string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) +  getOutputFileNameTag("chimera");
247                         string accnosFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) +  getOutputFileNameTag("accnos");
248                         
249                         chimera->getChimeras();
250                         
251                         if (m->control_pressed) { delete chimera; for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        } outputTypes.clear(); return 0;        }
252                         
253                 #ifdef USE_MPI
254                         MPI_File outMPI;
255                         MPI_File outMPIAccnos;
256                         
257                         int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
258                         
259                         char outFilename[1024];
260                         strcpy(outFilename, accnosFileName.c_str());
261
262                         char FileName[1024];
263                         strcpy(FileName, outputFileName.c_str());
264
265                         MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI);  //comm, filename, mode, info, filepointer
266                         MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
267         
268                         numSeqs = chimera->print(outMPI, outMPIAccnos, "");
269                         
270                         MPI_File_close(&outMPI);
271                         MPI_File_close(&outMPIAccnos);
272
273                 #else
274                 
275                         ofstream out;
276                         m->openOutputFile(outputFileName, out);
277                         
278                         ofstream out2;
279                         m->openOutputFile(accnosFileName, out2);
280                         
281                         numSeqs = chimera->print(out, out2, "");
282                         out.close();
283                         out2.close(); 
284                         
285                 #endif
286                         
287                         if (m->control_pressed) { m->mothurRemove(accnosFileName); m->mothurRemove(outputFileName); for (int i = 0; i < outputNames.size(); i++) {      m->mothurRemove(outputNames[i]);        } outputTypes.clear(); delete chimera;  return 0;       }
288                         
289                         m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
290                         
291                         outputNames.push_back(outputFileName);  outputTypes["chimera"].push_back(outputFileName);
292                         outputNames.push_back(accnosFileName);  outputTypes["accnos"].push_back(accnosFileName);
293                         
294                         delete chimera;
295                 }
296                 
297                 //set accnos file as new current accnosfile
298                 string current = "";
299                 itTypes = outputTypes.find("accnos");
300                 if (itTypes != outputTypes.end()) {
301                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
302                 }
303                 
304                 m->mothurOutEndLine();
305                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
306                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
307                 m->mothurOutEndLine();
308                 
309                 return 0;
310                                 
311         }
312         catch(exception& e) {
313                 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
314                 exit(1);
315         }
316 }
317 //**********************************************************************************************************************
318