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