2 * chimeracheckcommand.cpp
5 * Created by westcott on 3/31/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeracheckcommand.h"
12 //***************************************************************************************************************
14 ChimeraCheckCommand::ChimeraCheckCommand(string option) {
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string,string> parameters = parser.getParameters();
29 ValidParameters validParameter("chimera.check");
30 map<string,string>::iterator it;
32 //check to make sure all parameters are valid for command
33 for (it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //if the user changes the input directory command factory will send this info to us in the output parameter
38 string inputDir = validParameter.validFile(parameters, "inputdir", false);
39 if (inputDir == "not found"){ inputDir = ""; }
41 it = parameters.find("template");
42 //user has given a template file
43 if(it != parameters.end()){
44 string path = hasPath(it->second);
45 //if the user has not given a path then, add inputdir. else leave path alone.
46 if (path == "") { parameters["template"] = inputDir + it->second; }
50 //check for required parameters
51 fastafile = validParameter.validFile(parameters, "fasta", false);
52 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
54 splitAtDash(fastafile, fastaFileNames);
56 //go through files and make sure they are good, if not, then disregard them
57 for (int i = 0; i < fastaFileNames.size(); i++) {
59 string path = hasPath(fastaFileNames[i]);
60 //if the user has not given a path then, add inputdir. else leave path alone.
61 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
69 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
70 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
75 ableToOpen = openInputFile(fastaFileNames[i], in, "noerror");
77 //if you can't open it, try default location
78 if (ableToOpen == 1) {
79 if (m->getDefaultPath() != "") { //default path is set
80 string tryPath = m->getDefaultPath() + getSimpleName(fastaFileNames[i]);
81 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
82 ableToOpen = openInputFile(tryPath, in, "noerror");
83 fastaFileNames[i] = tryPath;
89 for (int j = 1; j < processors; j++) {
90 MPI_Send(&ableToOpen, 1, MPI_INT, j, 2001, MPI_COMM_WORLD);
94 MPI_Recv(&ableToOpen, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
99 if (ableToOpen == 1) {
100 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine();
101 //erase from file list
102 fastaFileNames.erase(fastaFileNames.begin()+i);
107 //make sure there is at least one valid file left
108 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
111 //if the user changes the output directory command factory will send this info to us in the output parameter
112 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
114 templatefile = validParameter.validFile(parameters, "template", true);
115 if (templatefile == "not open") { abort = true; }
116 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
118 namefile = validParameter.validFile(parameters, "name", false);
119 if (namefile == "not found") { namefile = ""; }
121 splitAtDash(namefile, nameFileNames);
123 //go through files and make sure they are good, if not, then disregard them
124 for (int i = 0; i < nameFileNames.size(); i++) {
125 if (inputDir != "") {
126 string path = hasPath(nameFileNames[i]);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
136 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
137 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
142 ableToOpen = openInputFile(nameFileNames[i], in, "noerror");
144 //if you can't open it, try default location
145 if (ableToOpen == 1) {
146 if (m->getDefaultPath() != "") { //default path is set
147 string tryPath = m->getDefaultPath() + getSimpleName(nameFileNames[i]);
148 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
149 ableToOpen = openInputFile(tryPath, in, "noerror");
150 nameFileNames[i] = tryPath;
156 for (int j = 1; j < processors; j++) {
157 MPI_Send(&ableToOpen, 1, MPI_INT, j, 2001, MPI_COMM_WORLD);
161 MPI_Recv(&ableToOpen, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
166 if (ableToOpen == 1) {
167 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
168 //erase from file list
169 nameFileNames.erase(nameFileNames.begin()+i);
175 //make sure there is at least one valid file left
176 if (nameFileNames.size() != 0) {
177 if (nameFileNames.size() != fastaFileNames.size()) {
178 m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine();
184 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
185 convert(temp, processors);
187 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
188 convert(temp, ksize);
190 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
192 if (nameFileNames.size() != 0) { svg = true; }
194 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
195 convert(temp, increment);
198 catch(exception& e) {
199 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
203 //**********************************************************************************************************************
205 void ChimeraCheckCommand::help(){
208 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
209 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
210 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
211 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
212 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
213 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
214 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
216 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
218 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
219 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
220 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");
221 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
222 m->mothurOut("You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n");
223 m->mothurOut("The chimera.check command should be in the following format: \n");
224 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
225 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
226 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
228 catch(exception& e) {
229 m->errorOut(e, "ChimeraCheckCommand", "help");
234 //***************************************************************************************************************
236 ChimeraCheckCommand::~ChimeraCheckCommand(){ /* do nothing */ }
238 //***************************************************************************************************************
240 int ChimeraCheckCommand::execute(){
243 if (abort == true) { return 0; }
245 for (int i = 0; i < fastaFileNames.size(); i++) {
247 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
249 int start = time(NULL);
251 string thisNameFile = "";
252 if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
254 chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);
256 if (m->control_pressed) { delete chimera; return 0; }
258 string outputFileName = outputDir + getRootName(getSimpleName(fastaFileNames[i])) + "chimeracheck.chimeras";
259 outputNames.push_back(outputFileName);
263 int pid, end, numSeqsPerProcessor;
265 vector<unsigned long int> MPIPos;
268 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
269 MPI_Comm_size(MPI_COMM_WORLD, &processors);
274 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
275 int inMode=MPI_MODE_RDONLY;
277 char outFilename[1024];
278 strcpy(outFilename, outputFileName.c_str());
280 char inFileName[1024];
281 strcpy(inFileName, fastaFileNames[i].c_str());
283 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
284 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
286 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; }
288 if (pid == 0) { //you are the root process
289 MPIPos = setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
291 //send file positions to all processes
292 for(int j = 1; j < processors; j++) {
293 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
294 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
297 //figure out how many sequences you have to align
298 numSeqsPerProcessor = numSeqs / processors;
299 int startIndex = pid * numSeqsPerProcessor;
300 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
304 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
306 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; }
309 for(int j = 1; j < processors; j++) {
311 MPI_Recv(buf, 4, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status);
313 }else{ //you are a child process
314 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
315 MPIPos.resize(numSeqs+1);
316 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
318 //figure out how many sequences you have to align
319 numSeqsPerProcessor = numSeqs / processors;
320 int startIndex = pid * numSeqsPerProcessor;
321 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
324 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
326 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; }
328 //tell parent you are done.
331 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
335 MPI_File_close(&inMPI);
336 MPI_File_close(&outMPI);
337 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
341 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
344 openInputFile(fastaFileNames[i], inFASTA);
345 getNumSeqs(inFASTA, numSeqs);
348 lines.push_back(new linePair(0, numSeqs));
350 driver(lines[0], outputFileName, fastaFileNames[i]);
352 if (m->control_pressed) {
353 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
354 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
360 vector<unsigned long int> positions;
361 processIDS.resize(0);
364 openInputFile(fastaFileNames[i], inFASTA);
367 while(!inFASTA.eof()){
368 input = getline(inFASTA);
369 if (input.length() != 0) {
370 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
375 numSeqs = positions.size();
377 int numSeqsPerProcessor = numSeqs / processors;
379 for (int j = 0; j < processors; j++) {
380 unsigned long int startPos = positions[ j * numSeqsPerProcessor ];
381 if(j == processors - 1){
382 numSeqsPerProcessor = numSeqs - j * numSeqsPerProcessor;
384 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
388 createProcesses(outputFileName, fastaFileNames[i]);
390 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
392 //append output files
393 for(int j=1;j<processors;j++){
394 appendFiles((outputFileName + toString(processIDS[j]) + ".temp"), outputFileName);
395 remove((outputFileName + toString(processIDS[j]) + ".temp").c_str());
398 if (m->control_pressed) {
399 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
400 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
408 openInputFile(fastaFileNames[i], inFASTA);
409 getNumSeqs(inFASTA, numSeqs);
411 lines.push_back(new linePair(0, numSeqs));
413 driver(lines[0], outputFileName, fastaFileNames[i]);
415 if (m->control_pressed) {
416 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
417 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
424 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
426 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();
427 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
431 m->mothurOutEndLine();
432 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
433 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
434 m->mothurOutEndLine();
439 catch(exception& e) {
440 m->errorOut(e, "ChimeraCheckCommand", "execute");
444 //**********************************************************************************************************************
446 int ChimeraCheckCommand::driver(linePair* line, string outputFName, string filename){
449 openOutputFile(outputFName, out);
454 openInputFile(filename, inFASTA);
456 inFASTA.seekg(line->start);
458 for(int i=0;i<line->numSeqs;i++){
460 if (m->control_pressed) { return 1; }
462 Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
464 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
466 chimera->getChimeras(candidateSeq);
468 if (m->control_pressed) { delete candidateSeq; return 1; }
471 chimera->print(out, out2);
476 if((i+1) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine(); }
479 if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine(); }
486 catch(exception& e) {
487 m->errorOut(e, "ChimeraCheckCommand", "driver");
491 //**********************************************************************************************************************
493 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos){
498 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
500 for(int i=0;i<num;i++){
502 if (m->control_pressed) { return 0; }
505 int length = MPIPos[start+i+1] - MPIPos[start+i];
507 char* buf4 = new char[length];
508 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
510 string tempBuf = buf4;
511 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
512 istringstream iss (tempBuf,istringstream::in);
515 Sequence* candidateSeq = new Sequence(iss); gobble(iss);
517 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
519 chimera->getChimeras(candidateSeq);
522 chimera->print(outMPI, outAccMPI);
527 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
530 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
534 catch(exception& e) {
535 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
541 /**************************************************************************************************/
543 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
545 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
547 // processIDS.resize(0);
549 //loop through and create all the processes you want
550 while (process != processors) {
554 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
557 driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
559 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
562 //force parent to wait until all the processes are done
563 for (int i=0;i<processors;i++) {
564 int temp = processIDS[i];
571 catch(exception& e) {
572 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
576 /**************************************************************************************************/