2 * chimerabellerophoncommand.cpp
5 * Created by westcott on 4/1/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimerabellerophoncommand.h"
11 #include "bellerophon.h"
13 //**********************************************************************************************************************
14 vector<string> ChimeraBellerophonCommand::getValidParameters(){
16 string AlignArray[] = {"fasta","filter","correction","processors","window","increment","outputdir","inputdir"};
17 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
21 m->errorOut(e, "ChimeraBellerophonCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 vector<string> ChimeraBellerophonCommand::getRequiredParameters(){
28 string AlignArray[] = {"fasta"};
29 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
33 m->errorOut(e, "ChimeraBellerophonCommand", "getRequiredParameters");
37 //**********************************************************************************************************************
38 vector<string> ChimeraBellerophonCommand::getRequiredFiles(){
40 vector<string> myArray;
44 m->errorOut(e, "ChimeraBellerophonCommand", "getRequiredFiles");
48 //**********************************************************************************************************************
49 ChimeraBellerophonCommand::ChimeraBellerophonCommand(){
52 //initialize outputTypes
53 vector<string> tempOutNames;
54 outputTypes["chimera"] = tempOutNames;
55 outputTypes["accnos"] = tempOutNames;
56 outputTypes["fasta"] = tempOutNames;
59 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
63 //***************************************************************************************************************
64 ChimeraBellerophonCommand::ChimeraBellerophonCommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"fasta","filter","correction","processors","window","increment","outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter("chimera.bellerophon");
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["chimera"] = tempOutNames;
90 outputTypes["accnos"] = tempOutNames;
91 outputTypes["fasta"] = tempOutNames;
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
97 fastafile = validParameter.validFile(parameters, "fasta", false);
98 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.bellerophon command."); m->mothurOutEndLine(); abort = true; }
100 m->splitAtDash(fastafile, fastaFileNames);
102 //go through files and make sure they are good, if not, then disregard them
103 for (int i = 0; i < fastaFileNames.size(); i++) {
104 if (inputDir != "") {
105 string path = m->hasPath(fastaFileNames[i]);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
113 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
115 //if you can't open it, try default location
116 if (ableToOpen == 1) {
117 if (m->getDefaultPath() != "") { //default path is set
118 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
119 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
121 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
123 fastaFileNames[i] = tryPath;
127 //if you can't open it, try default location
128 if (ableToOpen == 1) {
129 if (m->getOutputDir() != "") { //default path is set
130 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
131 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
133 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
135 fastaFileNames[i] = tryPath;
141 if (ableToOpen == 1) {
142 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
143 //erase from file list
144 fastaFileNames.erase(fastaFileNames.begin()+i);
150 //make sure there is at least one valid file left
151 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
154 //if the user changes the output directory command factory will send this info to us in the output parameter
155 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
158 temp = validParameter.validFile(parameters, "filter", false); if (temp == "not found") { temp = "F"; }
159 filter = m->isTrue(temp);
161 temp = validParameter.validFile(parameters, "correction", false); if (temp == "not found") { temp = "T"; }
162 correction = m->isTrue(temp);
164 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
165 convert(temp, processors);
167 temp = validParameter.validFile(parameters, "window", false); if (temp == "not found") { temp = "0"; }
168 convert(temp, window);
170 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "25"; }
171 convert(temp, increment);
174 catch(exception& e) {
175 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
179 //**********************************************************************************************************************
181 void ChimeraBellerophonCommand::help(){
183 m->mothurOut("The chimera.bellerophon command reads a fastafile and creates list of potentially chimeric sequences.\n");
184 m->mothurOut("The chimera.bellerophon command parameters are fasta, filter, correction, processors, window, increment. The fasta parameter is required.\n");
185 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");
186 m->mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter, default=false. \n");
187 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");
188 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
190 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
192 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras, default is 1/4 sequence length. \n");
193 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 25.\n");
194 m->mothurOut("chimera.bellerophon(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors) \n");
195 m->mothurOut("Example: chimera.bellerophon(fasta=AD.align, filter=True, correction=true, window=200) \n");
196 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
198 catch(exception& e) {
199 m->errorOut(e, "ChimeraBellerophonCommand", "help");
204 //***************************************************************************************************************
206 ChimeraBellerophonCommand::~ChimeraBellerophonCommand(){ /* do nothing */ }
208 //***************************************************************************************************************
210 int ChimeraBellerophonCommand::execute(){
213 if (abort == true) { return 0; }
215 for (int i = 0; i < fastaFileNames.size(); i++) {
217 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
219 int start = time(NULL);
221 chimera = new Bellerophon(fastaFileNames[i], filter, correction, window, increment, processors, outputDir);
223 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]); }//if user entered a file with a path then preserve it
224 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) + "bellerophon.chimeras";
225 string accnosFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) + "bellerophon.accnos";
227 chimera->getChimeras();
229 if (m->control_pressed) { delete chimera; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); return 0; }
233 MPI_File outMPIAccnos;
235 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
237 char outFilename[1024];
238 strcpy(outFilename, accnosFileName.c_str());
241 strcpy(FileName, outputFileName.c_str());
243 MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI); //comm, filename, mode, info, filepointer
244 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
246 numSeqs = chimera->print(outMPI, outMPIAccnos, "");
248 MPI_File_close(&outMPI);
249 MPI_File_close(&outMPIAccnos);
254 m->openOutputFile(outputFileName, out);
257 m->openOutputFile(accnosFileName, out2);
259 numSeqs = chimera->print(out, out2, "");
265 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 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
269 outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
270 outputNames.push_back(accnosFileName); outputTypes["accnos"].push_back(accnosFileName);
275 m->mothurOutEndLine();
276 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
277 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
278 m->mothurOutEndLine();
283 catch(exception& e) {
284 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
288 //**********************************************************************************************************************