2 * chimeracheckcommand.cpp
5 * Created by westcott on 3/31/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeracheckcommand.h"
11 #include "chimeracheckrdp.h"
13 //***************************************************************************************************************
15 ChimeraCheckCommand::ChimeraCheckCommand(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","processors","increment","template","ksize","svg", "name","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 = ""; }
42 it = parameters.find("template");
43 //user has given a template file
44 if(it != parameters.end()){
45 string path = hasPath(it->second);
46 //if the user has not given a path then, add inputdir. else leave path alone.
47 if (path == "") { parameters["template"] = inputDir + it->second; }
51 //check for required parameters
52 fastafile = validParameter.validFile(parameters, "fasta", false);
53 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
55 splitAtDash(fastafile, fastaFileNames);
57 //go through files and make sure they are good, if not, then disregard them
58 for (int i = 0; i < fastaFileNames.size(); i++) {
60 string path = hasPath(fastaFileNames[i]);
61 //if the user has not given a path then, add inputdir. else leave path alone.
62 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
70 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
71 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
76 ableToOpen = openInputFile(fastaFileNames[i], in, "noerror");
78 //if you can't open it, try default location
79 if (ableToOpen == 1) {
80 if (m->getDefaultPath() != "") { //default path is set
81 string tryPath = m->getDefaultPath() + getSimpleName(fastaFileNames[i]);
82 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
83 ableToOpen = openInputFile(tryPath, in, "noerror");
84 fastaFileNames[i] = tryPath;
90 for (int j = 1; j < processors; j++) {
91 MPI_Send(&ableToOpen, 1, MPI_INT, j, 2001, MPI_COMM_WORLD);
95 MPI_Recv(&ableToOpen, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
100 if (ableToOpen == 1) {
101 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine();
102 //erase from file list
103 fastaFileNames.erase(fastaFileNames.begin()+i);
108 //make sure there is at least one valid file left
109 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
112 //if the user changes the output directory command factory will send this info to us in the output parameter
113 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
115 templatefile = validParameter.validFile(parameters, "template", true);
116 if (templatefile == "not open") { abort = true; }
117 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
119 namefile = validParameter.validFile(parameters, "name", false);
120 if (namefile == "not found") { namefile = ""; }
122 splitAtDash(namefile, nameFileNames);
124 //go through files and make sure they are good, if not, then disregard them
125 for (int i = 0; i < nameFileNames.size(); i++) {
126 if (inputDir != "") {
127 string path = hasPath(nameFileNames[i]);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
137 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
138 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
143 ableToOpen = openInputFile(nameFileNames[i], in, "noerror");
145 //if you can't open it, try default location
146 if (ableToOpen == 1) {
147 if (m->getDefaultPath() != "") { //default path is set
148 string tryPath = m->getDefaultPath() + getSimpleName(nameFileNames[i]);
149 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
150 ableToOpen = openInputFile(tryPath, in, "noerror");
151 nameFileNames[i] = tryPath;
157 for (int j = 1; j < processors; j++) {
158 MPI_Send(&ableToOpen, 1, MPI_INT, j, 2001, MPI_COMM_WORLD);
162 MPI_Recv(&ableToOpen, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
167 if (ableToOpen == 1) {
168 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
169 //erase from file list
170 nameFileNames.erase(nameFileNames.begin()+i);
176 //make sure there is at least one valid file left
177 if (nameFileNames.size() != 0) {
178 if (nameFileNames.size() != fastaFileNames.size()) {
179 m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine();
185 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
186 convert(temp, processors);
188 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
189 convert(temp, ksize);
191 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
193 if (nameFileNames.size() != 0) { svg = true; }
195 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
196 convert(temp, increment);
199 catch(exception& e) {
200 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
204 //**********************************************************************************************************************
206 void ChimeraCheckCommand::help(){
209 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
210 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
211 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
212 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
213 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
214 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
215 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
217 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
219 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
220 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
221 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");
222 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
223 m->mothurOut("You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n");
224 m->mothurOut("The chimera.check command should be in the following format: \n");
225 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
226 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
227 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
229 catch(exception& e) {
230 m->errorOut(e, "ChimeraCheckCommand", "help");
235 //***************************************************************************************************************
237 ChimeraCheckCommand::~ChimeraCheckCommand(){ /* do nothing */ }
239 //***************************************************************************************************************
241 int ChimeraCheckCommand::execute(){
244 if (abort == true) { return 0; }
246 for (int i = 0; i < fastaFileNames.size(); i++) {
248 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
250 int start = time(NULL);
252 string thisNameFile = "";
253 if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
255 chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);
257 if (m->control_pressed) { delete chimera; return 0; }
259 string outputFileName = outputDir + getRootName(getSimpleName(fastaFileNames[i])) + "chimeracheck.chimeras";
260 outputNames.push_back(outputFileName);
264 int pid, end, numSeqsPerProcessor;
269 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
270 MPI_Comm_size(MPI_COMM_WORLD, &processors);
275 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
276 int inMode=MPI_MODE_RDONLY;
278 char outFilename[1024];
279 strcpy(outFilename, outputFileName.c_str());
281 char inFileName[1024];
282 strcpy(inFileName, fastaFileNames[i].c_str());
284 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
285 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
287 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()); } delete chimera; return 0; }
289 if (pid == 0) { //you are the root process
290 MPIPos = setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
292 //send file positions to all processes
293 for(int j = 1; j < processors; j++) {
294 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
295 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
298 //figure out how many sequences you have to align
299 numSeqsPerProcessor = numSeqs / processors;
300 int startIndex = pid * numSeqsPerProcessor;
301 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
305 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
307 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()); } delete chimera; return 0; }
310 for(int j = 1; j < processors; j++) {
312 MPI_Recv(buf, 4, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status);
314 }else{ //you are a child process
315 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
316 MPIPos.resize(numSeqs+1);
317 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
319 //figure out how many sequences you have to align
320 numSeqsPerProcessor = numSeqs / processors;
321 int startIndex = pid * numSeqsPerProcessor;
322 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
325 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
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()); } delete chimera; return 0; }
329 //tell parent you are done.
332 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
336 MPI_File_close(&inMPI);
337 MPI_File_close(&outMPI);
338 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
342 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
345 openInputFile(fastaFileNames[i], inFASTA);
346 getNumSeqs(inFASTA, numSeqs);
349 lines.push_back(new linePair(0, numSeqs));
351 driver(lines[0], outputFileName, fastaFileNames[i]);
353 if (m->control_pressed) {
354 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
355 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
361 vector<unsigned long int> positions;
362 processIDS.resize(0);
365 openInputFile(fastaFileNames[i], inFASTA);
368 while(!inFASTA.eof()){
369 input = getline(inFASTA);
370 if (input.length() != 0) {
371 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
376 numSeqs = positions.size();
378 int numSeqsPerProcessor = numSeqs / processors;
380 for (int j = 0; j < processors; j++) {
381 unsigned long int startPos = positions[ j * numSeqsPerProcessor ];
382 if(j == processors - 1){
383 numSeqsPerProcessor = numSeqs - j * numSeqsPerProcessor;
385 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
389 createProcesses(outputFileName, fastaFileNames[i]);
391 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
393 //append output files
394 for(int j=1;j<processors;j++){
395 appendFiles((outputFileName + toString(processIDS[j]) + ".temp"), outputFileName);
396 remove((outputFileName + toString(processIDS[j]) + ".temp").c_str());
399 if (m->control_pressed) {
400 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
401 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
409 openInputFile(fastaFileNames[i], inFASTA);
410 getNumSeqs(inFASTA, numSeqs);
412 lines.push_back(new linePair(0, numSeqs));
414 driver(lines[0], outputFileName, fastaFileNames[i]);
416 if (m->control_pressed) {
417 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
418 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
425 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
427 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();
428 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
432 m->mothurOutEndLine();
433 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
434 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
435 m->mothurOutEndLine();
440 catch(exception& e) {
441 m->errorOut(e, "ChimeraCheckCommand", "execute");
445 //**********************************************************************************************************************
447 int ChimeraCheckCommand::driver(linePair* line, string outputFName, string filename){
450 openOutputFile(outputFName, out);
455 openInputFile(filename, inFASTA);
457 inFASTA.seekg(line->start);
459 for(int i=0;i<line->numSeqs;i++){
461 if (m->control_pressed) { return 1; }
463 Sequence* candidateSeq = new Sequence(inFASTA); 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);
477 if((i+1) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine(); }
480 if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine(); }
487 catch(exception& e) {
488 m->errorOut(e, "ChimeraCheckCommand", "driver");
492 //**********************************************************************************************************************
494 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<long>& MPIPos){
499 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
501 for(int i=0;i<num;i++){
503 if (m->control_pressed) { return 0; }
506 int length = MPIPos[start+i+1] - MPIPos[start+i];
508 char* buf4 = new char[length];
509 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
511 string tempBuf = buf4;
512 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
513 istringstream iss (tempBuf,istringstream::in);
516 Sequence* candidateSeq = new Sequence(iss); gobble(iss);
518 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
520 chimera->getChimeras(candidateSeq);
523 chimera->print(outMPI, outAccMPI);
528 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
531 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
535 catch(exception& e) {
536 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
542 /**************************************************************************************************/
544 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
546 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
548 // processIDS.resize(0);
550 //loop through and create all the processes you want
551 while (process != processors) {
555 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
558 driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
560 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
563 //force parent to wait until all the processes are done
564 for (int i=0;i<processors;i++) {
565 int temp = processIDS[i];
572 catch(exception& e) {
573 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
577 /**************************************************************************************************/