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