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::getValidParameters(){
15 string AlignArray[] = {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
16 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
20 m->errorOut(e, "ChimeraCheckCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 vector<string> ChimeraCheckCommand::getRequiredParameters(){
27 string AlignArray[] = {"template","fasta"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
32 m->errorOut(e, "ChimeraCheckCommand", "getRequiredParameters");
36 //**********************************************************************************************************************
37 vector<string> ChimeraCheckCommand::getRequiredFiles(){
39 vector<string> myArray;
43 m->errorOut(e, "ChimeraCheckCommand", "getRequiredFiles");
47 //**********************************************************************************************************************
48 ChimeraCheckCommand::ChimeraCheckCommand(){
50 vector<string> tempOutNames;
51 outputTypes["chimera"] = tempOutNames;
54 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
58 //***************************************************************************************************************
59 ChimeraCheckCommand::ChimeraCheckCommand(string option) {
63 //allow user to run help
64 if(option == "help") { help(); abort = true; }
67 //valid paramters for this command
68 string Array[] = {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
69 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
71 OptionParser parser(option);
72 map<string,string> parameters = parser.getParameters();
74 ValidParameters validParameter("chimera.check");
75 map<string,string>::iterator it;
77 //check to make sure all parameters are valid for command
78 for (it = parameters.begin(); it != parameters.end(); it++) {
79 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
82 vector<string> tempOutNames;
83 outputTypes["chimera"] = tempOutNames;
85 //if the user changes the input directory command factory will send this info to us in the output parameter
86 string inputDir = validParameter.validFile(parameters, "inputdir", false);
87 if (inputDir == "not found"){ inputDir = ""; }
89 it = parameters.find("template");
90 //user has given a template file
91 if(it != parameters.end()){
92 string path = m->hasPath(it->second);
93 //if the user has not given a path then, add inputdir. else leave path alone.
94 if (path == "") { parameters["template"] = inputDir + it->second; }
98 //check for required parameters
99 fastafile = validParameter.validFile(parameters, "fasta", false);
100 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
102 m->splitAtDash(fastafile, fastaFileNames);
104 //go through files and make sure they are good, if not, then disregard them
105 for (int i = 0; i < fastaFileNames.size(); i++) {
106 if (inputDir != "") {
107 string path = m->hasPath(fastaFileNames[i]);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
115 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
117 //if you can't open it, try default location
118 if (ableToOpen == 1) {
119 if (m->getDefaultPath() != "") { //default path is set
120 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
121 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
123 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
125 fastaFileNames[i] = tryPath;
129 //if you can't open it, try default location
130 if (ableToOpen == 1) {
131 if (m->getOutputDir() != "") { //default path is set
132 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
133 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
135 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
137 fastaFileNames[i] = tryPath;
143 if (ableToOpen == 1) {
144 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine();
145 //erase from file list
146 fastaFileNames.erase(fastaFileNames.begin()+i);
151 //make sure there is at least one valid file left
152 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
155 //if the user changes the output directory command factory will send this info to us in the output parameter
156 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
158 templatefile = validParameter.validFile(parameters, "template", true);
159 if (templatefile == "not open") { abort = true; }
160 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
162 namefile = validParameter.validFile(parameters, "name", false);
163 if (namefile == "not found") { namefile = ""; }
165 m->splitAtDash(namefile, nameFileNames);
167 //go through files and make sure they are good, if not, then disregard them
168 for (int i = 0; i < nameFileNames.size(); i++) {
169 if (inputDir != "") {
170 string path = m->hasPath(nameFileNames[i]);
171 //if the user has not given a path then, add inputdir. else leave path alone.
172 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
178 ableToOpen = m->openInputFile(nameFileNames[i], in, "noerror");
180 //if you can't open it, try default location
181 if (ableToOpen == 1) {
182 if (m->getDefaultPath() != "") { //default path is set
183 string tryPath = m->getDefaultPath() + m->getSimpleName(nameFileNames[i]);
184 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
186 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
188 nameFileNames[i] = tryPath;
192 //if you can't open it, try default location
193 if (ableToOpen == 1) {
194 if (m->getOutputDir() != "") { //default path is set
195 string tryPath = m->getOutputDir() + m->getSimpleName(nameFileNames[i]);
196 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
198 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
200 nameFileNames[i] = tryPath;
206 if (ableToOpen == 1) {
207 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
208 //erase from file list
209 nameFileNames.erase(nameFileNames.begin()+i);
215 //make sure there is at least one valid file left
216 if (nameFileNames.size() != 0) {
217 if (nameFileNames.size() != fastaFileNames.size()) {
218 m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine();
224 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
225 convert(temp, processors);
227 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
228 convert(temp, ksize);
230 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
231 svg = m->isTrue(temp);
232 if (nameFileNames.size() != 0) { svg = true; }
234 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
235 convert(temp, increment);
238 catch(exception& e) {
239 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
243 //**********************************************************************************************************************
245 void ChimeraCheckCommand::help(){
248 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
249 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
250 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
251 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
252 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
253 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
254 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
256 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
258 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
259 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
260 m->mothurOut("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");
261 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
262 m->mothurOut("You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n");
263 m->mothurOut("The chimera.check command should be in the following format: \n");
264 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
265 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
266 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
268 catch(exception& e) {
269 m->errorOut(e, "ChimeraCheckCommand", "help");
274 //***************************************************************************************************************
276 ChimeraCheckCommand::~ChimeraCheckCommand(){ /* do nothing */ }
278 //***************************************************************************************************************
280 int ChimeraCheckCommand::execute(){
283 if (abort == true) { return 0; }
285 for (int i = 0; i < fastaFileNames.size(); i++) {
287 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
289 int start = time(NULL);
291 string thisNameFile = "";
292 if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
294 chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);
296 if (m->control_pressed) { delete chimera; return 0; }
298 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]); }//if user entered a file with a path then preserve it
299 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) + "chimeracheck.chimeras";
300 outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
304 int pid, end, numSeqsPerProcessor;
306 vector<unsigned long int> MPIPos;
309 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
310 MPI_Comm_size(MPI_COMM_WORLD, &processors);
315 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
316 int inMode=MPI_MODE_RDONLY;
318 char outFilename[1024];
319 strcpy(outFilename, outputFileName.c_str());
321 char inFileName[1024];
322 strcpy(inFileName, fastaFileNames[i].c_str());
324 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
325 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
327 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; }
329 if (pid == 0) { //you are the root process
330 MPIPos = m->setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
332 //send file positions to all processes
333 for(int j = 1; j < processors; j++) {
334 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
335 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
338 //figure out how many sequences you have to align
339 numSeqsPerProcessor = numSeqs / processors;
340 int startIndex = pid * numSeqsPerProcessor;
341 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
345 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
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; }
350 for(int j = 1; j < processors; j++) {
352 MPI_Recv(buf, 5, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status);
354 }else{ //you are a child process
355 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
356 MPIPos.resize(numSeqs+1);
357 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
359 //figure out how many sequences you have to align
360 numSeqsPerProcessor = numSeqs / processors;
361 int startIndex = pid * numSeqsPerProcessor;
362 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; }
369 //tell parent you are done.
372 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
376 MPI_File_close(&inMPI);
377 MPI_File_close(&outMPI);
378 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
381 vector<unsigned long int> positions = m->divideFile(fastaFileNames[i], processors);
383 for (int s = 0; s < (positions.size()-1); s++) {
384 lines.push_back(new linePair(positions[s], positions[(s+1)]));
388 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
390 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
392 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; }
395 processIDS.resize(0);
397 numSeqs = createProcesses(outputFileName, fastaFileNames[i]);
399 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
401 //append output files
402 for(int j=1;j<processors;j++){
403 m->appendFiles((outputFileName + toString(processIDS[j]) + ".temp"), outputFileName);
404 remove((outputFileName + toString(processIDS[j]) + ".temp").c_str());
407 if (m->control_pressed) {
408 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear();
409 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
416 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
418 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; }
422 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
424 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();
425 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 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, "ChimeraCheckCommand", "execute");
442 //**********************************************************************************************************************
444 int ChimeraCheckCommand::driver(linePair* filePos, string outputFName, string filename){
447 m->openOutputFile(outputFName, out);
452 m->openInputFile(filename, inFASTA);
454 inFASTA.seekg(filePos->start);
461 if (m->control_pressed) { return 1; }
463 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
465 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
467 chimera->getChimeras(candidateSeq);
469 if (m->control_pressed) { delete candidateSeq; return 1; }
472 chimera->print(out, out2);
476 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
477 unsigned long int pos = inFASTA.tellg();
478 if ((pos == -1) || (pos >= filePos->end)) { break; }
480 if (inFASTA.eof()) { break; }
484 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
487 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
494 catch(exception& e) {
495 m->errorOut(e, "ChimeraCheckCommand", "driver");
499 //**********************************************************************************************************************
501 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos){
506 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
508 for(int i=0;i<num;i++){
510 if (m->control_pressed) { return 0; }
513 int length = MPIPos[start+i+1] - MPIPos[start+i];
515 char* buf4 = new char[length];
516 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
518 string tempBuf = buf4;
519 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
520 istringstream iss (tempBuf,istringstream::in);
523 Sequence* candidateSeq = new Sequence(iss); m->gobble(iss);
525 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
527 chimera->getChimeras(candidateSeq);
530 chimera->print(outMPI, outAccMPI);
535 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
538 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
542 catch(exception& e) {
543 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
549 /**************************************************************************************************/
551 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
553 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
557 //loop through and create all the processes you want
558 while (process != processors) {
562 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
565 num = driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
567 //pass numSeqs to parent
569 string tempFile = outputFileName + toString(getpid()) + ".num.temp";
570 m->openOutputFile(tempFile, out);
576 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
577 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
582 //force parent to wait until all the processes are done
583 for (int i=0;i<processors;i++) {
584 int temp = processIDS[i];
588 for (int i = 0; i < processIDS.size(); i++) {
590 string tempFile = outputFileName + toString(processIDS[i]) + ".num.temp";
591 m->openInputFile(tempFile, in);
592 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
593 in.close(); remove(tempFile.c_str());
599 catch(exception& e) {
600 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
604 /**************************************************************************************************/