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