]> git.donarmstrong.com Git - mothur.git/blob - chimerabellerophoncommand.cpp
added parse.fastaq command, added permute option to venn command, fixed bug with...
[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("chimera.bellerophon");
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                                 m->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 = m->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                                         ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
59                                 
60                                         //if you can't open it, try default location
61                                         if (ableToOpen == 1) {
62                                                 if (m->getDefaultPath() != "") { //default path is set
63                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
64                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
65                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
66                                                         fastaFileNames[i] = tryPath;
67                                                 }
68                                         }
69                                         in.close();
70
71                                         if (ableToOpen == 1) { 
72                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
73                                                 //erase from file list
74                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
75                                                 i--;
76                                         }
77                                         
78                                 }
79                                 
80                                 //make sure there is at least one valid file left
81                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
82                         }
83                         
84                         //if the user changes the output directory command factory will send this info to us in the output parameter 
85                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
86
87                         string temp;
88                         temp = validParameter.validFile(parameters, "filter", false);                   if (temp == "not found") { temp = "F"; }
89                         filter = m->isTrue(temp);
90                         
91                         temp = validParameter.validFile(parameters, "correction", false);               if (temp == "not found") { temp = "T"; }
92                         correction = m->isTrue(temp);
93                         
94                         temp = validParameter.validFile(parameters, "processors", false);               if (temp == "not found") { temp = "1"; }
95                         convert(temp, processors);
96                         
97                         temp = validParameter.validFile(parameters, "window", false);                   if (temp == "not found") { temp = "0"; }
98                         convert(temp, window);
99                         
100                         temp = validParameter.validFile(parameters, "increment", false);                if (temp == "not found") { temp = "25"; }
101                         convert(temp, increment);
102                 }
103         }
104         catch(exception& e) {
105                 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
106                 exit(1);
107         }
108 }
109 //**********************************************************************************************************************
110
111 void ChimeraBellerophonCommand::help(){
112         try {
113                 m->mothurOut("The chimera.bellerophon command reads a fastafile and creates list of potentially chimeric sequences.\n");
114                 m->mothurOut("The chimera.bellerophon command parameters are fasta, filter, correction, processors, window, increment. The fasta parameter is required.\n");
115                 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");
116                 m->mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter, default=false. \n");
117                 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");
118                 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n");
119                 #ifdef USE_MPI
120                 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
121                 #endif
122                 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras, default is 1/4 sequence length. \n");
123                 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 25.\n");
124                 m->mothurOut("chimera.bellerophon(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors) \n");
125                 m->mothurOut("Example: chimera.bellerophon(fasta=AD.align, filter=True, correction=true, window=200) \n");
126                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "ChimeraBellerophonCommand", "help");
130                 exit(1);
131         }
132 }
133
134 //***************************************************************************************************************
135
136 ChimeraBellerophonCommand::~ChimeraBellerophonCommand(){        /*      do nothing      */      }
137
138 //***************************************************************************************************************
139
140 int ChimeraBellerophonCommand::execute(){
141         try{
142                 
143                 if (abort == true) { return 0; }
144                 
145                 for (int i = 0; i < fastaFileNames.size(); i++) {
146                         
147                         m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
148                         
149                         int start = time(NULL); 
150                         
151                         chimera = new Bellerophon(fastaFileNames[i], filter, correction, window, increment, processors, outputDir);     
152                         
153                         if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]);  }//if user entered a file with a path then preserve it               
154                         string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) +  "bellerophon.chimeras";
155                         string accnosFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) + "bellerophon.accnos";
156                         
157                         chimera->getChimeras();
158                         
159                         if (m->control_pressed) { delete chimera; for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0;     }
160                         
161                 #ifdef USE_MPI
162                         MPI_File outMPI;
163                         MPI_File outMPIAccnos;
164                         
165                         int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
166                         
167                         char outFilename[1024];
168                         strcpy(outFilename, accnosFileName.c_str());
169
170                         char FileName[1024];
171                         strcpy(FileName, outputFileName.c_str());
172
173                         MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI);  //comm, filename, mode, info, filepointer
174                         MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
175         
176                         numSeqs = chimera->print(outMPI, outMPIAccnos);
177                         
178                         MPI_File_close(&outMPI);
179                         MPI_File_close(&outMPIAccnos);
180
181                 #else
182                 
183                         ofstream out;
184                         m->openOutputFile(outputFileName, out);
185                         
186                         ofstream out2;
187                         m->openOutputFile(accnosFileName, out2);
188                         
189                         numSeqs = chimera->print(out, out2);
190                         out.close();
191                         out2.close(); 
192                         
193                 #endif
194                         
195                         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;       }
196                         
197                         m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
198                         
199                         outputNames.push_back(outputFileName);
200                         outputNames.push_back(accnosFileName);
201                         
202                         delete chimera;
203                 }
204                 
205                 m->mothurOutEndLine();
206                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
207                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
208                 m->mothurOutEndLine();
209                 
210                 return 0;
211                                 
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
215                 exit(1);
216         }
217 }
218 //**********************************************************************************************************************
219