]> git.donarmstrong.com Git - mothur.git/blob - chimerabellerophoncommand.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[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","chimera-accnos",false,true,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,true); 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::getOutputPattern(string type) {
61     try {
62         string pattern = "";
63         
64         if (type == "chimera") {  pattern = "[filename],bellerophon.chimeras"; } 
65         else if (type == "accnos") {  pattern = "[filename],bellerophon.accnos"; } 
66         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
67         
68         return pattern;
69     }
70     catch(exception& e) {
71         m->errorOut(e, "ChimeraBellerophonCommand", "getOutputPattern");
72         exit(1);
73     }
74 }
75 //**********************************************************************************************************************
76 ChimeraBellerophonCommand::ChimeraBellerophonCommand(){ 
77         try {
78                 abort = true; calledHelp = true; 
79                 setParameters();
80                 vector<string> tempOutNames;
81                 outputTypes["chimera"] = tempOutNames;
82                 outputTypes["accnos"] = tempOutNames;
83         }
84         catch(exception& e) {
85                 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
86                 exit(1);
87         }
88 }
89 //***************************************************************************************************************
90 ChimeraBellerophonCommand::ChimeraBellerophonCommand(string option)  {
91         try {
92                 abort = false; calledHelp = false;   
93                 
94                 //allow user to run help
95                 if(option == "help") { help(); abort = true; calledHelp = true; }
96                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
97                 
98                 else {
99                         
100                         vector<string> myArray = setParameters();
101                         
102                         OptionParser parser(option);
103                         map<string,string> parameters = parser.getParameters();
104                         
105                         ValidParameters validParameter("chimera.bellerophon");
106                         map<string,string>::iterator it;
107                         
108                         //check to make sure all parameters are valid for command
109                         for (it = parameters.begin(); it != parameters.end(); it++) { 
110                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
111                         }
112                         
113                         //initialize outputTypes
114                         vector<string> tempOutNames;
115                         outputTypes["chimera"] = tempOutNames;
116                         outputTypes["accnos"] = tempOutNames;
117                 
118                         //if the user changes the input directory command factory will send this info to us in the output parameter 
119                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
120                         if (inputDir == "not found"){   inputDir = "";          }
121                         
122                         fastafile = validParameter.validFile(parameters, "fasta", false);
123                         if (fastafile == "not found") {                                 
124                                 //if there is a current fasta file, use it
125                                 string filename = m->getFastaFile(); 
126                                 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
127                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
128                         }else { 
129                                 m->splitAtDash(fastafile, fastaFileNames);
130                                 
131                                 //go through files and make sure they are good, if not, then disregard them
132                                 for (int i = 0; i < fastaFileNames.size(); i++) {
133                                         
134                                         bool ignore = false;
135                                         if (fastaFileNames[i] == "current") { 
136                                                 fastaFileNames[i] = m->getFastaFile(); 
137                                                 if (fastaFileNames[i] != "") {  m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
138                                                 else {  
139                                                         m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
140                                                         //erase from file list
141                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
142                                                         i--;
143                                                 }
144                                         }
145                                         
146                                         if (!ignore) {
147                                         
148                                                 if (inputDir != "") {
149                                                         string path = m->hasPath(fastaFileNames[i]);
150                                                         //if the user has not given a path then, add inputdir. else leave path alone.
151                                                         if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
152                                                 }
153                 
154                                                 int ableToOpen;
155                                                 ifstream in;
156
157                                                 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
158                                         
159                                                 //if you can't open it, try default location
160                                                 if (ableToOpen == 1) {
161                                                         if (m->getDefaultPath() != "") { //default path is set
162                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
163                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
164                                                                 ifstream in2;
165                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
166                                                                 in2.close();
167                                                                 fastaFileNames[i] = tryPath;
168                                                         }
169                                                 }
170                                                 
171                                                 //if you can't open it, try default location
172                                                 if (ableToOpen == 1) {
173                                                         if (m->getOutputDir() != "") { //default path is set
174                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
175                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
176                                                                 ifstream in2;
177                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
178                                                                 in2.close();
179                                                                 fastaFileNames[i] = tryPath;
180                                                         }
181                                                 }
182                                                 
183                                                 in.close();
184
185                                                 if (ableToOpen == 1) { 
186                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
187                                                         //erase from file list
188                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
189                                                         i--;
190                                                 }else {
191                                                         m->setFastaFile(fastaFileNames[i]);
192                                                 }
193                                         }
194                                 }
195                                 
196                                 //make sure there is at least one valid file left
197                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
198                         }
199                         
200                         //if the user changes the output directory command factory will send this info to us in the output parameter 
201                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
202
203                         string temp;
204                         temp = validParameter.validFile(parameters, "filter", false);                   if (temp == "not found") { temp = "F"; }
205                         filter = m->isTrue(temp);
206                         
207                         temp = validParameter.validFile(parameters, "correction", false);               if (temp == "not found") { temp = "T"; }
208                         correction = m->isTrue(temp);
209                         
210                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
211                         m->setProcessors(temp);
212                         m->mothurConvert(temp, processors);
213                         
214                         temp = validParameter.validFile(parameters, "window", false);                   if (temp == "not found") { temp = "0"; }
215                         m->mothurConvert(temp, window);
216                         
217                         temp = validParameter.validFile(parameters, "increment", false);                if (temp == "not found") { temp = "25"; }
218                         m->mothurConvert(temp, increment);
219                 }
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
223                 exit(1);
224         }
225 }
226 //***************************************************************************************************************
227 int ChimeraBellerophonCommand::execute(){
228         try{
229                 
230                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
231                 
232                 for (int i = 0; i < fastaFileNames.size(); i++) {
233                         
234                         m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
235                         
236                         int start = time(NULL); 
237                         
238                         chimera = new Bellerophon(fastaFileNames[i], filter, correction, window, increment, processors, outputDir);     
239                         
240                         if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]);  }//if user entered a file with a path then preserve it       
241             map<string, string> variables; 
242             variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i]));
243                         string outputFileName = getOutputFileName("chimera", variables);
244                         string accnosFileName = getOutputFileName("accnos", variables);
245                         
246                         chimera->getChimeras();
247                         
248                         if (m->control_pressed) { delete chimera; for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        } outputTypes.clear(); return 0;        }
249                         
250                 #ifdef USE_MPI
251                         MPI_File outMPI;
252                         MPI_File outMPIAccnos;
253                         
254                         int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
255                         
256                         char outFilename[1024];
257                         strcpy(outFilename, accnosFileName.c_str());
258
259                         char FileName[1024];
260                         strcpy(FileName, outputFileName.c_str());
261
262                         MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI);  //comm, filename, mode, info, filepointer
263                         MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
264         
265                         numSeqs = chimera->print(outMPI, outMPIAccnos, "");
266                         
267                         MPI_File_close(&outMPI);
268                         MPI_File_close(&outMPIAccnos);
269
270                 #else
271                 
272                         ofstream out;
273                         m->openOutputFile(outputFileName, out);
274                         
275                         ofstream out2;
276                         m->openOutputFile(accnosFileName, out2);
277                         
278                         numSeqs = chimera->print(out, out2, "");
279                         out.close();
280                         out2.close(); 
281                         
282                 #endif
283                         
284                         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;       }
285                         
286                         m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
287                         
288                         outputNames.push_back(outputFileName);  outputTypes["chimera"].push_back(outputFileName);
289                         outputNames.push_back(accnosFileName);  outputTypes["accnos"].push_back(accnosFileName);
290                         
291                         delete chimera;
292                 }
293                 
294                 //set accnos file as new current accnosfile
295                 string current = "";
296                 itTypes = outputTypes.find("accnos");
297                 if (itTypes != outputTypes.end()) {
298                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
299                 }
300                 
301                 m->mothurOutEndLine();
302                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
303                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
304                 m->mothurOutEndLine();
305                 
306                 return 0;
307                                 
308         }
309         catch(exception& e) {
310                 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
311                 exit(1);
312         }
313 }
314 //**********************************************************************************************************************
315