2 * chimeracheckcommand.cpp
5 * Created by westcott on 3/31/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeracheckcommand.h"
12 //**********************************************************************************************************************
13 vector<string> ChimeraCheckCommand::setParameters(){
15 CommandParameter ptemplate("reference", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptemplate);
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter psvg("svg", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psvg);
19 CommandParameter pincrement("increment", "Number", "", "10", "", "", "",false,false); parameters.push_back(pincrement);
20 CommandParameter pksize("ksize", "Number", "", "7", "", "", "",false,false); parameters.push_back(pksize);
21 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
22 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
23 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
25 vector<string> myArray;
26 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
30 m->errorOut(e, "ChimeraCheckCommand", "setParameters");
34 //**********************************************************************************************************************
35 string ChimeraCheckCommand::getHelpString(){
37 string helpString = "";
38 helpString += "The chimera.check command reads a fastafile and referencefile and outputs potentially chimeric sequences.\n";
39 helpString += "This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n";
40 helpString += "The chimera.check command parameters are fasta, reference, processors, ksize, increment, svg and name.\n";
41 helpString += "The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required unless you have a valid current fasta file. \n";
42 helpString += "You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n";
43 helpString += "The reference parameter allows you to enter a reference file containing known non-chimeric sequences, and is required. \n";
44 helpString += "The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n";
46 helpString += "When using MPI, the processors parameter is set to the number of MPI processes running. \n";
48 helpString += "The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n";
49 helpString += "The ksize parameter allows you to input kmersize, default is 7. \n";
50 helpString += "The svg parameter allows you to specify whether or not you would like a svg file outputted for each query sequence, default is False.\n";
51 helpString += "The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n";
52 helpString += "You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n";
53 helpString += "The chimera.check command should be in the following format: \n";
54 helpString += "chimera.check(fasta=yourFastaFile, reference=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n";
55 helpString += "Example: chimera.check(fasta=AD.fasta, reference=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n";
56 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
60 m->errorOut(e, "ChimeraCheckCommand", "getHelpString");
64 //**********************************************************************************************************************
65 ChimeraCheckCommand::ChimeraCheckCommand(){
67 abort = true; calledHelp = true;
69 vector<string> tempOutNames;
70 outputTypes["chimera"] = tempOutNames;
73 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
77 //***************************************************************************************************************
78 ChimeraCheckCommand::ChimeraCheckCommand(string option) {
80 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
92 ValidParameters validParameter("chimera.check");
93 map<string,string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 vector<string> tempOutNames;
101 outputTypes["chimera"] = tempOutNames;
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
107 it = parameters.find("reference");
108 //user has given a template file
109 if(it != parameters.end()){
110 string path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["reference"] = inputDir + it->second; }
116 //check for required parameters
117 fastafile = validParameter.validFile(parameters, "fasta", false);
118 if (fastafile == "not found") {
119 //if there is a current fasta file, use it
120 string filename = m->getFastaFile();
121 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
122 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
124 m->splitAtDash(fastafile, fastaFileNames);
126 //go through files and make sure they are good, if not, then disregard them
127 for (int i = 0; i < fastaFileNames.size(); i++) {
130 if (fastaFileNames[i] == "current") {
131 fastaFileNames[i] = m->getFastaFile();
132 if (fastaFileNames[i] != "") { m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
134 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
135 //erase from file list
136 fastaFileNames.erase(fastaFileNames.begin()+i);
144 if (inputDir != "") {
145 string path = m->hasPath(fastaFileNames[i]);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
153 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
155 //if you can't open it, try default location
156 if (ableToOpen == 1) {
157 if (m->getDefaultPath() != "") { //default path is set
158 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
159 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
161 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
163 fastaFileNames[i] = tryPath;
167 //if you can't open it, try default location
168 if (ableToOpen == 1) {
169 if (m->getOutputDir() != "") { //default path is set
170 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
171 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
173 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
175 fastaFileNames[i] = tryPath;
181 if (ableToOpen == 1) {
182 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine();
183 //erase from file list
184 fastaFileNames.erase(fastaFileNames.begin()+i);
190 //make sure there is at least one valid file left
191 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
194 //if the user changes the output directory command factory will send this info to us in the output parameter
195 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
197 templatefile = validParameter.validFile(parameters, "reference", true);
198 if (templatefile == "not open") { abort = true; }
199 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("reference is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
201 namefile = validParameter.validFile(parameters, "name", false);
202 if (namefile == "not found") { namefile = ""; }
204 m->splitAtDash(namefile, nameFileNames);
206 //go through files and make sure they are good, if not, then disregard them
207 for (int i = 0; i < nameFileNames.size(); i++) {
210 if (nameFileNames[i] == "current") {
211 nameFileNames[i] = m->getNameFile();
212 if (nameFileNames[i] != "") { m->mothurOut("Using " + nameFileNames[i] + " as input file for the name parameter where you had given current."); m->mothurOutEndLine(); }
214 m->mothurOut("You have no current namefile, ignoring current."); m->mothurOutEndLine(); ignore=true;
215 //erase from file list
216 nameFileNames.erase(nameFileNames.begin()+i);
223 if (inputDir != "") {
224 string path = m->hasPath(nameFileNames[i]);
225 //if the user has not given a path then, add inputdir. else leave path alone.
226 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
232 ableToOpen = m->openInputFile(nameFileNames[i], in, "noerror");
234 //if you can't open it, try default location
235 if (ableToOpen == 1) {
236 if (m->getDefaultPath() != "") { //default path is set
237 string tryPath = m->getDefaultPath() + m->getSimpleName(nameFileNames[i]);
238 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
240 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
242 nameFileNames[i] = tryPath;
246 //if you can't open it, try default location
247 if (ableToOpen == 1) {
248 if (m->getOutputDir() != "") { //default path is set
249 string tryPath = m->getOutputDir() + m->getSimpleName(nameFileNames[i]);
250 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
252 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
254 nameFileNames[i] = tryPath;
260 if (ableToOpen == 1) {
261 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
262 //erase from file list
263 nameFileNames.erase(nameFileNames.begin()+i);
269 //make sure there is at least one valid file left
270 if (nameFileNames.size() != 0) {
271 if (nameFileNames.size() != fastaFileNames.size()) {
272 m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine();
278 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
279 m->setProcessors(temp);
280 convert(temp, processors);
282 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
283 convert(temp, ksize);
285 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
286 svg = m->isTrue(temp);
287 if (nameFileNames.size() != 0) { svg = true; }
289 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
290 convert(temp, increment);
293 catch(exception& e) {
294 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
298 //***************************************************************************************************************
300 int ChimeraCheckCommand::execute(){
303 if (abort == true) { if (calledHelp) { return 0; } return 2; }
305 for (int i = 0; i < fastaFileNames.size(); i++) {
307 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
309 int start = time(NULL);
311 string thisNameFile = "";
312 if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
314 chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);
316 if (m->control_pressed) { delete chimera; return 0; }
318 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]); }//if user entered a file with a path then preserve it
319 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) + "chimeracheck.chimeras";
320 outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
324 int pid, numSeqsPerProcessor;
326 vector<unsigned long int> MPIPos;
329 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
330 MPI_Comm_size(MPI_COMM_WORLD, &processors);
335 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
336 int inMode=MPI_MODE_RDONLY;
338 char outFilename[1024];
339 strcpy(outFilename, outputFileName.c_str());
341 char inFileName[1024];
342 strcpy(inFileName, fastaFileNames[i].c_str());
344 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
345 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
347 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear(); delete chimera; return 0; }
349 if (pid == 0) { //you are the root process
350 MPIPos = m->setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
352 //send file positions to all processes
353 for(int j = 1; j < processors; j++) {
354 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
355 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
358 //figure out how many sequences you have to align
359 numSeqsPerProcessor = numSeqs / processors;
360 int startIndex = pid * numSeqsPerProcessor;
361 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
365 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
367 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear(); delete chimera; return 0; }
370 for(int j = 1; j < processors; j++) {
372 MPI_Recv(buf, 5, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status);
374 }else{ //you are a child process
375 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
376 MPIPos.resize(numSeqs+1);
377 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
379 //figure out how many sequences you have to align
380 numSeqsPerProcessor = numSeqs / processors;
381 int startIndex = pid * numSeqsPerProcessor;
382 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
385 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
387 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear(); delete chimera; return 0; }
389 //tell parent you are done.
392 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
396 MPI_File_close(&inMPI);
397 MPI_File_close(&outMPI);
398 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
401 vector<unsigned long int> positions = m->divideFile(fastaFileNames[i], processors);
403 for (int s = 0; s < (positions.size()-1); s++) {
404 lines.push_back(new linePair(positions[s], positions[(s+1)]));
408 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
410 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
412 if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } for (int j = 0; j < lines.size(); j++) { delete lines[j]; } outputTypes.clear(); lines.clear(); delete chimera; return 0; }
415 processIDS.resize(0);
417 numSeqs = createProcesses(outputFileName, fastaFileNames[i]);
419 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
421 //append output files
422 for(int j=1;j<processors;j++){
423 m->appendFiles((outputFileName + toString(processIDS[j]) + ".temp"), outputFileName);
424 remove((outputFileName + toString(processIDS[j]) + ".temp").c_str());
427 if (m->control_pressed) {
428 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear();
429 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
436 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
438 if (m->control_pressed) { for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear(); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear(); delete chimera; return 0; }
442 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
444 m->mothurOutEndLine(); m->mothurOut("This method does not determine if a sequence is chimeric, but allows you to make that determination based on the IS values."); m->mothurOutEndLine();
445 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
449 m->mothurOutEndLine();
450 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
451 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
452 m->mothurOutEndLine();
457 catch(exception& e) {
458 m->errorOut(e, "ChimeraCheckCommand", "execute");
462 //**********************************************************************************************************************
464 int ChimeraCheckCommand::driver(linePair* filePos, string outputFName, string filename){
467 m->openOutputFile(outputFName, out);
472 m->openInputFile(filename, inFASTA);
474 inFASTA.seekg(filePos->start);
481 if (m->control_pressed) { return 1; }
483 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
485 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
487 chimera->getChimeras(candidateSeq);
489 if (m->control_pressed) { delete candidateSeq; return 1; }
492 chimera->print(out, out2);
496 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
497 unsigned long int pos = inFASTA.tellg();
498 if ((pos == -1) || (pos >= filePos->end)) { break; }
500 if (inFASTA.eof()) { break; }
504 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
507 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
514 catch(exception& e) {
515 m->errorOut(e, "ChimeraCheckCommand", "driver");
519 //**********************************************************************************************************************
521 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos){
526 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
528 for(int i=0;i<num;i++){
530 if (m->control_pressed) { return 0; }
533 int length = MPIPos[start+i+1] - MPIPos[start+i];
535 char* buf4 = new char[length];
536 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
538 string tempBuf = buf4;
539 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
540 istringstream iss (tempBuf,istringstream::in);
543 Sequence* candidateSeq = new Sequence(iss); m->gobble(iss);
545 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
547 chimera->getChimeras(candidateSeq);
550 chimera->print(outMPI, outAccMPI);
555 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
558 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
562 catch(exception& e) {
563 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
569 /**************************************************************************************************/
571 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
573 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
577 //loop through and create all the processes you want
578 while (process != processors) {
582 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
585 num = driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
587 //pass numSeqs to parent
589 string tempFile = outputFileName + toString(getpid()) + ".num.temp";
590 m->openOutputFile(tempFile, out);
596 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
597 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
602 //force parent to wait until all the processes are done
603 for (int i=0;i<processors;i++) {
604 int temp = processIDS[i];
608 for (int i = 0; i < processIDS.size(); i++) {
610 string tempFile = outputFileName + toString(processIDS[i]) + ".num.temp";
611 m->openInputFile(tempFile, in);
612 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
613 in.close(); remove(tempFile.c_str());
619 catch(exception& e) {
620 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
624 /**************************************************************************************************/