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 //***************************************************************************************************************
15 ChimeraBellerophonCommand::ChimeraBellerophonCommand(string option) {
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
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)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
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; }
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 = ""; }
43 it = parameters.find("fasta");
44 //user has given a template file
45 if(it != parameters.end()){
46 path = hasPath(it->second);
47 //if the user has not given a path then, add inputdir. else leave path alone.
48 if (path == "") { parameters["fasta"] = inputDir + it->second; }
53 //check for required parameters
54 fastafile = validParameter.validFile(parameters, "fasta", true);
55 if (fastafile == "not open") { abort = true; }
56 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.bellerophon command."); m->mothurOutEndLine(); abort = true; }
58 //if the user changes the output directory command factory will send this info to us in the output parameter
59 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
61 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
65 temp = validParameter.validFile(parameters, "filter", false); if (temp == "not found") { temp = "F"; }
66 filter = isTrue(temp);
68 temp = validParameter.validFile(parameters, "correction", false); if (temp == "not found") { temp = "T"; }
69 correction = isTrue(temp);
71 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
72 convert(temp, processors);
74 temp = validParameter.validFile(parameters, "window", false); if (temp == "not found") { temp = "0"; }
75 convert(temp, window);
77 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "25"; }
78 convert(temp, increment);
82 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
86 //**********************************************************************************************************************
88 void ChimeraBellerophonCommand::help(){
90 m->mothurOut("The chimera.bellerophon command reads a fastafile and creates list of potentially chimeric sequences.\n");
91 m->mothurOut("The chimera.bellerophon command parameters are fasta, filter, correction, processors, window, increment. The fasta parameter is required.\n");
92 m->mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter, default=false. \n");
93 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");
94 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
96 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
98 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras, default is 1/4 sequence length. \n");
99 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 25.\n");
100 m->mothurOut("chimera.bellerophon(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors) \n");
101 m->mothurOut("Example: chimera.seqs(fasta=AD.align, filter=True, correction=true, window=200) \n");
102 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
104 catch(exception& e) {
105 m->errorOut(e, "ChimeraBellerophonCommand", "help");
110 //***************************************************************************************************************
112 ChimeraBellerophonCommand::~ChimeraBellerophonCommand(){ /* do nothing */ }
114 //***************************************************************************************************************
116 int ChimeraBellerophonCommand::execute(){
119 if (abort == true) { return 0; }
121 int start = time(NULL);
123 chimera = new Bellerophon(fastafile, filter, correction, window, increment, processors, outputDir);
125 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "bellerophon.chimeras";
126 string accnosFileName = outputDir + getRootName(getSimpleName(fastafile)) + "bellerophon.accnos";
127 bool hasAccnos = true;
129 chimera->getChimeras();
131 if (m->control_pressed) { delete chimera; return 0; }
135 MPI_File outMPIAccnos;
137 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
139 //char* outFilename = new char[accnosFileName.length()];
140 //memcpy(outFilename, accnosFileName.c_str(), accnosFileName.length());
142 char outFilename[1024];
143 strcpy(outFilename, accnosFileName.c_str());
145 //char* FileName = new char[outputFileName.length()];
146 //memcpy(FileName, outputFileName.c_str(), outputFileName.length());
149 strcpy(FileName, outputFileName.c_str());
151 MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI); //comm, filename, mode, info, filepointer
152 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
155 //delete outFilename;
157 numSeqs = chimera->print(outMPI, outMPIAccnos);
159 MPI_File_close(&outMPI);
160 MPI_File_close(&outMPIAccnos);
165 openOutputFile(outputFileName, out);
168 openOutputFile(accnosFileName, out2);
170 numSeqs = chimera->print(out, out2);
176 if (m->control_pressed) { remove(accnosFileName.c_str()); remove(outputFileName.c_str()); delete chimera; return 0; }
178 //delete accnos file if its blank
179 if (isBlank(accnosFileName)) { remove(accnosFileName.c_str()); hasAccnos = false; }
181 m->mothurOutEndLine();
182 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
183 m->mothurOut(outputFileName); m->mothurOutEndLine();
184 if (hasAccnos) { m->mothurOut(accnosFileName); m->mothurOutEndLine(); }
185 m->mothurOutEndLine();
186 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
193 catch(exception& e) {
194 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
198 //**********************************************************************************************************************