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("template");
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["template"] = inputDir + it->second; }
52 //check for required parameters
53 fastafile = validParameter.validFile(parameters, "fasta", false);
54 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.ccode command."); m->mothurOutEndLine(); abort = true; }
56 splitAtDash(fastafile, fastaFileNames);
58 //go through files and make sure they are good, if not, then disregard them
59 for (int i = 0; i < fastaFileNames.size(); i++) {
61 string path = hasPath(fastaFileNames[i]);
62 //if the user has not given a path then, add inputdir. else leave path alone.
63 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
71 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
72 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
77 ableToOpen = openInputFile(fastaFileNames[i], in);
81 for (int j = 1; j < processors; j++) {
82 MPI_Send(&ableToOpen, 1, MPI_INT, j, 2001, MPI_COMM_WORLD);
86 MPI_Recv(&ableToOpen, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
91 if (ableToOpen == 1) {
92 m->mothurOut(fastaFileNames[i] + " will be disregarded."); m->mothurOutEndLine();
93 //erase from file list
94 fastaFileNames.erase(fastaFileNames.begin()+i);
99 //make sure there is at least one valid file left
100 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
103 //if the user changes the output directory command factory will send this info to us in the output parameter
104 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
106 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
109 templatefile = validParameter.validFile(parameters, "template", true);
110 if (templatefile == "not open") { abort = true; }
111 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.ccode command."); m->mothurOutEndLine(); abort = true; }
113 maskfile = validParameter.validFile(parameters, "mask", false);
114 if (maskfile == "not found") { maskfile = ""; }
115 else if (maskfile != "default") {
116 if (inputDir != "") {
117 string path = hasPath(maskfile);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { maskfile = inputDir + maskfile; }
123 int ableToOpen = openInputFile(maskfile, in);
124 if (ableToOpen == 1) { abort = true; }
129 temp = validParameter.validFile(parameters, "filter", false); if (temp == "not found") { temp = "F"; }
130 filter = isTrue(temp);
132 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
133 convert(temp, processors);
135 temp = validParameter.validFile(parameters, "window", false); if (temp == "not found") { temp = "0"; }
136 convert(temp, window);
138 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found") { temp = "20"; }
139 convert(temp, numwanted);
143 catch(exception& e) {
144 m->errorOut(e, "ChimeraCcodeCommand", "ChimeraCcodeCommand");
148 //**********************************************************************************************************************
150 void ChimeraCcodeCommand::help(){
153 m->mothurOut("The chimera.ccode command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
154 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");
155 m->mothurOut("The chimera.ccode command parameters are fasta, template, filter, mask, processors, window and numwanted.\n");
156 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
157 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
158 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
159 m->mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter. \n");
160 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
162 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
164 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");
165 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras. \n");
166 m->mothurOut("The numwanted parameter allows you to specify how many sequences you would each query sequence compared with.\n");
167 m->mothurOut("The chimera.ccode command should be in the following format: \n");
168 m->mothurOut("chimera.ccode(fasta=yourFastaFile, template=yourTemplate) \n");
169 m->mothurOut("Example: chimera.ccode(fasta=AD.align, template=core_set_aligned.imputed.fasta) \n");
170 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
172 catch(exception& e) {
173 m->errorOut(e, "ChimeraCcodeCommand", "help");
178 //***************************************************************************************************************
180 ChimeraCcodeCommand::~ChimeraCcodeCommand(){ /* do nothing */ }
182 //***************************************************************************************************************
184 int ChimeraCcodeCommand::execute(){
187 if (abort == true) { return 0; }
189 for (int s = 0; s < fastaFileNames.size(); s++) {
191 m->mothurOut("Checking sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
193 int start = time(NULL);
196 if (maskfile == "default") { m->mothurOut("I am using the default 236627 EU009184.1 Shigella dysenteriae str. FBD013."); m->mothurOutEndLine(); }
198 chimera = new Ccode(fastaFileNames[s], templatefile, filter, maskfile, window, numwanted, outputDir);
200 //is your template aligned?
201 if (chimera->getUnaligned()) { m->mothurOut("Your template sequences are different lengths, please correct."); m->mothurOutEndLine(); delete chimera; return 0; }
202 templateSeqsLength = chimera->getLength();
204 string outputFileName, accnosFileName;
205 if (maskfile != "") {
206 outputFileName = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + maskfile + ".ccode.chimeras";
207 accnosFileName = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + maskfile + ".ccode.accnos";
209 outputFileName = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "ccode.chimeras";
210 accnosFileName = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "ccode.accnos";
213 string mapInfo = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "mapinfo";
215 if (m->control_pressed) { delete chimera; for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
219 int pid, end, numSeqsPerProcessor;
224 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
225 MPI_Comm_size(MPI_COMM_WORLD, &processors);
229 MPI_File outMPIAccnos;
231 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
232 int inMode=MPI_MODE_RDONLY;
234 char outFilename[1024];
235 strcpy(outFilename, outputFileName.c_str());
237 char outAccnosFilename[1024];
238 strcpy(outAccnosFilename, accnosFileName.c_str());
240 char inFileName[1024];
241 strcpy(inFileName, fastaFileNames[s].c_str());
243 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
244 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
245 MPI_File_open(MPI_COMM_WORLD, outAccnosFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
247 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
249 if (pid == 0) { //you are the root process
250 string outTemp = "For full window mapping info refer to " + mapInfo + "\n\n";
253 int length = outTemp.length();
254 char* buf2 = new char[length];
255 memcpy(buf2, outTemp.c_str(), length);
257 MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &status);
260 MPIPos = setFilePosFasta(fastaFileNames[s], numSeqs); //fills MPIPos, returns numSeqs
262 //send file positions to all processes
263 for(int i = 1; i < processors; i++) {
264 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
265 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
268 //figure out how many sequences you have to align
269 numSeqsPerProcessor = numSeqs / processors;
270 int startIndex = pid * numSeqsPerProcessor;
271 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
275 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
277 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); remove(outputFileName.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
279 }else{ //you are a child process
280 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
281 MPIPos.resize(numSeqs+1);
282 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
284 //figure out how many sequences you have to align
285 numSeqsPerProcessor = numSeqs / processors;
286 int startIndex = pid * numSeqsPerProcessor;
287 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
291 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
293 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
297 MPI_File_close(&inMPI);
298 MPI_File_close(&outMPI);
299 MPI_File_close(&outMPIAccnos);
301 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
305 string tempHeader = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + maskfile + "ccode.chimeras.tempHeader";
306 openOutputFile(tempHeader, outHeader);
308 outHeader << "For full window mapping info refer to " << mapInfo << endl << endl;
313 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
316 openInputFile(fastaFileNames[s], inFASTA);
317 getNumSeqs(inFASTA, numSeqs);
320 lines.push_back(new linePair(0, numSeqs));
322 driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
324 if (m->control_pressed) {
325 remove(outputFileName.c_str());
326 remove(tempHeader.c_str());
327 remove(accnosFileName.c_str());
328 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
329 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
335 vector<int> positions;
336 processIDS.resize(0);
339 openInputFile(fastaFileNames[s], inFASTA);
342 while(!inFASTA.eof()){
343 input = getline(inFASTA);
344 if (input.length() != 0) {
345 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
350 numSeqs = positions.size();
352 int numSeqsPerProcessor = numSeqs / processors;
354 for (int i = 0; i < processors; i++) {
355 long int startPos = positions[ i * numSeqsPerProcessor ];
356 if(i == processors - 1){
357 numSeqsPerProcessor = numSeqs - i * numSeqsPerProcessor;
359 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
363 createProcesses(outputFileName, fastaFileNames[s], accnosFileName);
365 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
366 rename((accnosFileName + toString(processIDS[0]) + ".temp").c_str(), accnosFileName.c_str());
368 //append output files
369 for(int i=1;i<processors;i++){
370 appendFiles((outputFileName + toString(processIDS[i]) + ".temp"), outputFileName);
371 remove((outputFileName + toString(processIDS[i]) + ".temp").c_str());
374 //append output files
375 for(int i=1;i<processors;i++){
376 appendFiles((accnosFileName + toString(processIDS[i]) + ".temp"), accnosFileName);
377 remove((accnosFileName + toString(processIDS[i]) + ".temp").c_str());
380 if (m->control_pressed) {
381 remove(outputFileName.c_str());
382 remove(accnosFileName.c_str());
383 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
384 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
393 openInputFile(fastaFileNames[s], inFASTA);
394 getNumSeqs(inFASTA, numSeqs);
396 lines.push_back(new linePair(0, numSeqs));
398 driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
400 if (m->control_pressed) {
401 remove(outputFileName.c_str());
402 remove(tempHeader.c_str());
403 remove(accnosFileName.c_str());
404 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
405 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
412 appendFiles(outputFileName, tempHeader);
414 remove(outputFileName.c_str());
415 rename(tempHeader.c_str(), outputFileName.c_str());
420 outputNames.push_back(outputFileName);
421 outputNames.push_back(mapInfo);
422 outputNames.push_back(accnosFileName);
424 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
426 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
429 m->mothurOutEndLine();
430 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
431 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
432 m->mothurOutEndLine();
437 catch(exception& e) {
438 m->errorOut(e, "ChimeraCcodeCommand", "execute");
442 //**********************************************************************************************************************
444 int ChimeraCcodeCommand::driver(linePair* line, string outputFName, string filename, string accnos){
447 openOutputFile(outputFName, out);
450 openOutputFile(accnos, out2);
453 openInputFile(filename, inFASTA);
455 inFASTA.seekg(line->start);
457 for(int i=0;i<line->numSeqs;i++){
459 if (m->control_pressed) { return 1; }
461 Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
463 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
465 if (candidateSeq->getAligned().length() != templateSeqsLength) {
466 m->mothurOut(candidateSeq->getName() + " is not the same length as the template sequences. Skipping."); m->mothurOutEndLine();
469 chimera->getChimeras(candidateSeq);
471 if (m->control_pressed) { delete candidateSeq; return 1; }
474 chimera->print(out, out2);
480 if((i+1) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine(); }
483 if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine(); }
491 catch(exception& e) {
492 m->errorOut(e, "ChimeraCcodeCommand", "driver");
496 //**********************************************************************************************************************
498 int ChimeraCcodeCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, MPI_File& outAccMPI, vector<long>& MPIPos){
503 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
505 for(int i=0;i<num;i++){
507 if (m->control_pressed) { return 0; }
510 int length = MPIPos[start+i+1] - MPIPos[start+i];
512 char* buf4 = new char[length];
514 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
516 string tempBuf = buf4;
517 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
518 istringstream iss (tempBuf,istringstream::in);
521 Sequence* candidateSeq = new Sequence(iss); gobble(iss);
523 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
525 if (candidateSeq->getAligned().length() != templateSeqsLength) {
526 m->mothurOut(candidateSeq->getName() + " is not the same length as the template sequences. Skipping."); m->mothurOutEndLine();
529 chimera->getChimeras(candidateSeq);
531 if (m->control_pressed) { delete candidateSeq; return 1; }
534 bool isChimeric = chimera->print(outMPI, outAccMPI);
540 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
543 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
548 catch(exception& e) {
549 m->errorOut(e, "ChimeraCcodeCommand", "driverMPI");
555 /**************************************************************************************************/
557 int ChimeraCcodeCommand::createProcesses(string outputFileName, string filename, string accnos) {
559 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
561 // processIDS.resize(0);
563 //loop through and create all the processes you want
564 while (process != processors) {
568 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
571 driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename, accnos + toString(getpid()) + ".temp");
573 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
576 //force parent to wait until all the processes are done
577 for (int i=0;i<processors;i++) {
578 int temp = processIDS[i];
585 catch(exception& e) {
586 m->errorOut(e, "ChimeraCcodeCommand", "createProcesses");
590 //**********************************************************************************************************************