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