2 * chimeraccodecommand.cpp
5 * Created by westcott on 3/30/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeraccodecommand.h"
13 //***************************************************************************************************************
15 ChimeraCcodeCommand::ChimeraCcodeCommand(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", "processors", "window", "template", "mask", "numwanted", "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; }
51 it = parameters.find("template");
52 //user has given a template file
53 if(it != parameters.end()){
54 path = hasPath(it->second);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { parameters["template"] = inputDir + it->second; }
61 //check for required parameters
62 fastafile = validParameter.validFile(parameters, "fasta", true);
63 if (fastafile == "not open") { abort = true; }
64 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.ccode command."); m->mothurOutEndLine(); abort = true; }
66 //if the user changes the output directory command factory will send this info to us in the output parameter
67 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
69 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
72 templatefile = validParameter.validFile(parameters, "template", true);
73 if (templatefile == "not open") { abort = true; }
74 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.ccode command."); m->mothurOutEndLine(); abort = true; }
76 maskfile = validParameter.validFile(parameters, "mask", false);
77 if (maskfile == "not found") { maskfile = ""; }
78 else if (maskfile != "default") {
80 string path = hasPath(maskfile);
81 //if the user has not given a path then, add inputdir. else leave path alone.
82 if (path == "") { maskfile = inputDir + maskfile; }
86 int ableToOpen = openInputFile(maskfile, in);
87 if (ableToOpen == 1) { abort = true; }
92 temp = validParameter.validFile(parameters, "filter", false); if (temp == "not found") { temp = "F"; }
93 filter = isTrue(temp);
95 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
96 convert(temp, processors);
98 temp = validParameter.validFile(parameters, "window", false); if (temp == "not found") { temp = "0"; }
99 convert(temp, window);
101 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found") { temp = "20"; }
102 convert(temp, numwanted);
106 catch(exception& e) {
107 m->errorOut(e, "ChimeraCcodeCommand", "ChimeraCcodeCommand");
111 //**********************************************************************************************************************
113 void ChimeraCcodeCommand::help(){
116 m->mothurOut("The chimera.ccode command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
117 m->mothurOut("This command was created using the algorythms described in the 'Evaluating putative chimeric sequences from PCR-amplified products' paper by Juan M. Gonzalez, Johannes Zimmerman and Cesareo Saiz-Jimenez.\n");
118 m->mothurOut("The chimera.ccode command parameters are fasta, template, filter, mask, processors, window and numwanted.\n");
119 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
120 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
121 m->mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter. \n");
122 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
124 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
126 m->mothurOut("The mask parameter allows you to specify a file containing one sequence you wish to use as a mask for the your sequences. \n");
127 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras. \n");
128 m->mothurOut("The numwanted parameter allows you to specify how many sequences you would each query sequence compared with.\n");
129 m->mothurOut("The chimera.ccode command should be in the following format: \n");
130 m->mothurOut("chimera.ccode(fasta=yourFastaFile, template=yourTemplate) \n");
131 m->mothurOut("Example: chimera.seqs(fasta=AD.align, template=core_set_aligned.imputed.fasta) \n");
132 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
134 catch(exception& e) {
135 m->errorOut(e, "ChimeraCcodeCommand", "help");
140 //***************************************************************************************************************
142 ChimeraCcodeCommand::~ChimeraCcodeCommand(){ /* do nothing */ }
144 //***************************************************************************************************************
146 int ChimeraCcodeCommand::execute(){
149 if (abort == true) { return 0; }
151 int start = time(NULL);
154 if (maskfile == "default") { m->mothurOut("I am using the default 236627 EU009184.1 Shigella dysenteriae str. FBD013."); m->mothurOutEndLine(); }
156 chimera = new Ccode(fastafile, templatefile, filter, maskfile, window, numwanted, outputDir);
158 //is your template aligned?
159 if (chimera->getUnaligned()) { m->mothurOut("Your template sequences are different lengths, please correct."); m->mothurOutEndLine(); delete chimera; return 0; }
160 templateSeqsLength = chimera->getLength();
162 string outputFileName, accnosFileName;
163 if (maskfile != "") {
164 outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + maskfile + ".ccode.chimeras";
165 accnosFileName = outputDir + getRootName(getSimpleName(fastafile)) + maskfile + ".ccode.accnos";
167 outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "ccode.chimeras";
168 accnosFileName = outputDir + getRootName(getSimpleName(fastafile)) + "ccode.accnos";
171 string mapInfo = outputDir + getRootName(getSimpleName(fastafile)) + "mapinfo";
172 bool hasAccnos = true;
174 if (m->control_pressed) { delete chimera; return 0; }
178 int pid, end, numSeqsPerProcessor;
181 MPIWroteAccnos = false;
184 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
185 MPI_Comm_size(MPI_COMM_WORLD, &processors);
189 MPI_File outMPIAccnos;
191 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
192 int inMode=MPI_MODE_RDONLY;
194 //char* outFilename = new char[outputFileName.length()];
195 //memcpy(outFilename, outputFileName.c_str(), outputFileName.length());
197 char outFilename[1024];
198 strcpy(outFilename, outputFileName.c_str());
200 //char* outAccnosFilename = new char[accnosFileName.length()];
201 //memcpy(outAccnosFilename, accnosFileName.c_str(), accnosFileName.length());
203 char outAccnosFilename[1024];
204 strcpy(outAccnosFilename, accnosFileName.c_str());
206 //char* inFileName = new char[fastafile.length()];
207 //memcpy(inFileName, fastafile.c_str(), fastafile.length());
209 char inFileName[1024];
210 strcpy(inFileName, fastafile.c_str());
212 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
213 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
214 MPI_File_open(MPI_COMM_WORLD, outAccnosFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
217 //delete outFilename;
218 //delete outAccnosFilename;
220 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); delete chimera; return 0; }
222 if (pid == 0) { //you are the root process
223 string outTemp = "For full window mapping info refer to " + mapInfo + "\n\n";
226 int length = outTemp.length();
227 char* buf2 = new char[length];
228 memcpy(buf2, outTemp.c_str(), length);
230 MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &status);
233 MPIPos = setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs
235 //send file positions to all processes
236 for(int i = 1; i < processors; i++) {
237 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
238 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
241 //figure out how many sequences you have to align
242 numSeqsPerProcessor = numSeqs / processors;
243 int startIndex = pid * numSeqsPerProcessor;
244 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
248 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
250 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); remove(outputFileName.c_str()); remove(accnosFileName.c_str()); delete chimera; return 0; }
252 for (int i = 1; i < processors; i++) {
254 MPI_Recv(&tempResult, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
255 if (tempResult != 0) { MPIWroteAccnos = true; }
257 }else{ //you are a child process
258 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
259 MPIPos.resize(numSeqs+1);
260 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
262 //figure out how many sequences you have to align
263 numSeqsPerProcessor = numSeqs / processors;
264 int startIndex = pid * numSeqsPerProcessor;
265 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
269 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
271 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); delete chimera; return 0; }
273 MPI_Send(&MPIWroteAccnos, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
277 MPI_File_close(&inMPI);
278 MPI_File_close(&outMPI);
279 MPI_File_close(&outMPIAccnos);
281 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
283 //delete accnos file if blank
285 if (!MPIWroteAccnos) {
287 //MPI_File_delete(outAccnosFilename, info);
289 remove(accnosFileName.c_str());
295 string tempHeader = outputDir + getRootName(getSimpleName(fastafile)) + maskfile + "ccode.chimeras.tempHeader";
296 openOutputFile(tempHeader, outHeader);
298 outHeader << "For full window mapping info refer to " << mapInfo << endl << endl;
303 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
306 openInputFile(fastafile, inFASTA);
307 getNumSeqs(inFASTA, numSeqs);
310 lines.push_back(new linePair(0, numSeqs));
312 driver(lines[0], outputFileName, fastafile, accnosFileName);
314 if (m->control_pressed) {
315 remove(outputFileName.c_str());
316 remove(tempHeader.c_str());
317 remove(accnosFileName.c_str());
318 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
323 //delete accnos file if its blank
324 if (isBlank(accnosFileName)) { remove(accnosFileName.c_str()); hasAccnos = false; }
327 vector<int> positions;
328 processIDS.resize(0);
331 openInputFile(fastafile, inFASTA);
334 while(!inFASTA.eof()){
335 input = getline(inFASTA);
336 if (input.length() != 0) {
337 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
342 numSeqs = positions.size();
344 int numSeqsPerProcessor = numSeqs / processors;
346 for (int i = 0; i < processors; i++) {
347 long int startPos = positions[ i * numSeqsPerProcessor ];
348 if(i == processors - 1){
349 numSeqsPerProcessor = numSeqs - i * numSeqsPerProcessor;
351 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
355 createProcesses(outputFileName, fastafile, accnosFileName);
357 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
359 //append output files
360 for(int i=1;i<processors;i++){
361 appendFiles((outputFileName + toString(processIDS[i]) + ".temp"), outputFileName);
362 remove((outputFileName + toString(processIDS[i]) + ".temp").c_str());
365 vector<string> nonBlankAccnosFiles;
366 //delete blank accnos files generated with multiple processes
367 for(int i=0;i<processors;i++){
368 if (!(isBlank(accnosFileName + toString(processIDS[i]) + ".temp"))) {
369 nonBlankAccnosFiles.push_back(accnosFileName + toString(processIDS[i]) + ".temp");
370 }else { remove((accnosFileName + toString(processIDS[i]) + ".temp").c_str()); }
373 //append accnos files
374 if (nonBlankAccnosFiles.size() != 0) {
375 rename(nonBlankAccnosFiles[0].c_str(), accnosFileName.c_str());
377 for (int h=1; h < nonBlankAccnosFiles.size(); h++) {
378 appendFiles(nonBlankAccnosFiles[h], accnosFileName);
379 remove(nonBlankAccnosFiles[h].c_str());
381 }else{ hasAccnos = false; }
383 if (m->control_pressed) {
384 remove(outputFileName.c_str());
385 remove(accnosFileName.c_str());
386 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
395 openInputFile(fastafile, inFASTA);
396 getNumSeqs(inFASTA, numSeqs);
398 lines.push_back(new linePair(0, numSeqs));
400 driver(lines[0], outputFileName, fastafile, accnosFileName);
402 if (m->control_pressed) {
403 remove(outputFileName.c_str());
404 remove(tempHeader.c_str());
405 remove(accnosFileName.c_str());
406 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
411 //delete accnos file if its blank
412 if (isBlank(accnosFileName)) { remove(accnosFileName.c_str()); hasAccnos = false; }
415 //m->mothurOut("Output File Names: ");
416 //if ((filter) && (method == "bellerophon")) { m->mothurOut(
417 //if (outputDir == "") { fastafile = getRootName(fastafile) + "filter.fasta"; }
418 // else { fastafile = outputDir + getRootName(getSimpleName(fastafile)) + "filter.fasta"; }
420 appendFiles(outputFileName, tempHeader);
422 remove(outputFileName.c_str());
423 rename(tempHeader.c_str(), outputFileName.c_str());
429 m->mothurOutEndLine();
430 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
431 m->mothurOut(outputFileName); m->mothurOutEndLine();
432 if (hasAccnos) { m->mothurOut(accnosFileName); m->mothurOutEndLine(); }
433 m->mothurOutEndLine();
435 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
437 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
442 catch(exception& e) {
443 m->errorOut(e, "ChimeraCcodeCommand", "execute");
447 //**********************************************************************************************************************
449 int ChimeraCcodeCommand::driver(linePair* line, string outputFName, string filename, string accnos){
452 openOutputFile(outputFName, out);
455 openOutputFile(accnos, out2);
458 openInputFile(filename, inFASTA);
460 inFASTA.seekg(line->start);
462 for(int i=0;i<line->numSeqs;i++){
464 if (m->control_pressed) { return 1; }
466 Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
468 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
470 if (candidateSeq->getAligned().length() != templateSeqsLength) {
471 m->mothurOut(candidateSeq->getName() + " is not the same length as the template sequences. Skipping."); m->mothurOutEndLine();
474 chimera->getChimeras(candidateSeq);
476 if (m->control_pressed) { delete candidateSeq; return 1; }
479 chimera->print(out, out2);
485 if((i+1) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine(); }
488 if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine(); }
496 catch(exception& e) {
497 m->errorOut(e, "ChimeraCcodeCommand", "driver");
501 //**********************************************************************************************************************
503 int ChimeraCcodeCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, MPI_File& outAccMPI, vector<long>& MPIPos){
508 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
510 for(int i=0;i<num;i++){
512 if (m->control_pressed) { return 0; }
515 int length = MPIPos[start+i+1] - MPIPos[start+i];
517 char* buf4 = new char[length];
519 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
521 string tempBuf = buf4;
522 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
523 istringstream iss (tempBuf,istringstream::in);
526 Sequence* candidateSeq = new Sequence(iss); gobble(iss);
528 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
530 if (candidateSeq->getAligned().length() != templateSeqsLength) {
531 m->mothurOut(candidateSeq->getName() + " is not the same length as the template sequences. Skipping."); m->mothurOutEndLine();
534 chimera->getChimeras(candidateSeq);
536 if (m->control_pressed) { delete candidateSeq; return 1; }
539 bool isChimeric = chimera->print(outMPI, outAccMPI);
540 if (isChimeric) { MPIWroteAccnos = true; }
546 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
549 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
554 catch(exception& e) {
555 m->errorOut(e, "ChimeraCcodeCommand", "driverMPI");
561 /**************************************************************************************************/
563 int ChimeraCcodeCommand::createProcesses(string outputFileName, string filename, string accnos) {
565 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
567 // processIDS.resize(0);
569 //loop through and create all the processes you want
570 while (process != processors) {
574 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
577 driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename, accnos + toString(getpid()) + ".temp");
579 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
582 //force parent to wait until all the processes are done
583 for (int i=0;i<processors;i++) {
584 int temp = processIDS[i];
591 catch(exception& e) {
592 m->errorOut(e, "ChimeraCcodeCommand", "createProcesses");
596 //**********************************************************************************************************************