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();
122 ableToOpen = m->openInputFile(tryPath, in, "noerror");
123 fastaFileNames[i] = tryPath;
127 //if you can't open it, try default location
128 if (ableToOpen == 1) {
129 if (m->getOutputDir() != "") { //default path is set
130 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
131 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
132 ableToOpen = m->openInputFile(tryPath, in, "noerror");
133 fastaFileNames[i] = tryPath;
139 if (ableToOpen == 1) {
140 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine();
141 //erase from file list
142 fastaFileNames.erase(fastaFileNames.begin()+i);
147 //make sure there is at least one valid file left
148 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
151 //if the user changes the output directory command factory will send this info to us in the output parameter
152 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
154 templatefile = validParameter.validFile(parameters, "template", true);
155 if (templatefile == "not open") { abort = true; }
156 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
158 namefile = validParameter.validFile(parameters, "name", false);
159 if (namefile == "not found") { namefile = ""; }
161 m->splitAtDash(namefile, nameFileNames);
163 //go through files and make sure they are good, if not, then disregard them
164 for (int i = 0; i < nameFileNames.size(); i++) {
165 if (inputDir != "") {
166 string path = m->hasPath(nameFileNames[i]);
167 //if the user has not given a path then, add inputdir. else leave path alone.
168 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
174 ableToOpen = m->openInputFile(nameFileNames[i], in, "noerror");
176 //if you can't open it, try default location
177 if (ableToOpen == 1) {
178 if (m->getDefaultPath() != "") { //default path is set
179 string tryPath = m->getDefaultPath() + m->getSimpleName(nameFileNames[i]);
180 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
181 ableToOpen = m->openInputFile(tryPath, in, "noerror");
182 nameFileNames[i] = tryPath;
186 //if you can't open it, try default location
187 if (ableToOpen == 1) {
188 if (m->getOutputDir() != "") { //default path is set
189 string tryPath = m->getOutputDir() + m->getSimpleName(nameFileNames[i]);
190 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
191 ableToOpen = m->openInputFile(tryPath, in, "noerror");
192 nameFileNames[i] = tryPath;
198 if (ableToOpen == 1) {
199 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
200 //erase from file list
201 nameFileNames.erase(nameFileNames.begin()+i);
207 //make sure there is at least one valid file left
208 if (nameFileNames.size() != 0) {
209 if (nameFileNames.size() != fastaFileNames.size()) {
210 m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine();
216 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
217 convert(temp, processors);
219 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
220 convert(temp, ksize);
222 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
223 svg = m->isTrue(temp);
224 if (nameFileNames.size() != 0) { svg = true; }
226 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
227 convert(temp, increment);
230 catch(exception& e) {
231 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
235 //**********************************************************************************************************************
237 void ChimeraCheckCommand::help(){
240 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
241 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
242 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
243 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
244 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
245 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
246 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
248 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
250 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
251 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
252 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");
253 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
254 m->mothurOut("You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n");
255 m->mothurOut("The chimera.check command should be in the following format: \n");
256 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
257 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
258 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
260 catch(exception& e) {
261 m->errorOut(e, "ChimeraCheckCommand", "help");
266 //***************************************************************************************************************
268 ChimeraCheckCommand::~ChimeraCheckCommand(){ /* do nothing */ }
270 //***************************************************************************************************************
272 int ChimeraCheckCommand::execute(){
275 if (abort == true) { return 0; }
277 for (int i = 0; i < fastaFileNames.size(); i++) {
279 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
281 int start = time(NULL);
283 string thisNameFile = "";
284 if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
286 chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);
288 if (m->control_pressed) { delete chimera; return 0; }
290 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]); }//if user entered a file with a path then preserve it
291 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i])) + "chimeracheck.chimeras";
292 outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
296 int pid, end, numSeqsPerProcessor;
298 vector<unsigned long int> MPIPos;
301 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
302 MPI_Comm_size(MPI_COMM_WORLD, &processors);
307 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
308 int inMode=MPI_MODE_RDONLY;
310 char outFilename[1024];
311 strcpy(outFilename, outputFileName.c_str());
313 char inFileName[1024];
314 strcpy(inFileName, fastaFileNames[i].c_str());
316 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
317 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
319 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; }
321 if (pid == 0) { //you are the root process
322 MPIPos = m->setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
324 //send file positions to all processes
325 for(int j = 1; j < processors; j++) {
326 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
327 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
330 //figure out how many sequences you have to align
331 numSeqsPerProcessor = numSeqs / processors;
332 int startIndex = pid * numSeqsPerProcessor;
333 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
337 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
339 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; }
342 for(int j = 1; j < processors; j++) {
344 MPI_Recv(buf, 5, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status);
346 }else{ //you are a child process
347 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
348 MPIPos.resize(numSeqs+1);
349 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
351 //figure out how many sequences you have to align
352 numSeqsPerProcessor = numSeqs / processors;
353 int startIndex = pid * numSeqsPerProcessor;
354 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
357 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
359 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; }
361 //tell parent you are done.
364 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
368 MPI_File_close(&inMPI);
369 MPI_File_close(&outMPI);
370 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
373 vector<unsigned long int> positions = m->divideFile(fastaFileNames[i], processors);
375 for (int s = 0; s < (positions.size()-1); s++) {
376 lines.push_back(new linePair(positions[s], positions[(s+1)]));
380 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
382 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
384 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; }
387 processIDS.resize(0);
389 numSeqs = 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 m->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()); } outputTypes.clear();
401 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
408 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
410 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; }
414 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
416 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();
417 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
421 m->mothurOutEndLine();
422 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
423 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
424 m->mothurOutEndLine();
429 catch(exception& e) {
430 m->errorOut(e, "ChimeraCheckCommand", "execute");
434 //**********************************************************************************************************************
436 int ChimeraCheckCommand::driver(linePair* filePos, string outputFName, string filename){
439 m->openOutputFile(outputFName, out);
444 m->openInputFile(filename, inFASTA);
446 inFASTA.seekg(filePos->start);
453 if (m->control_pressed) { return 1; }
455 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
457 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
459 chimera->getChimeras(candidateSeq);
461 if (m->control_pressed) { delete candidateSeq; return 1; }
464 chimera->print(out, out2);
468 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
469 unsigned long int pos = inFASTA.tellg();
470 if ((pos == -1) || (pos >= filePos->end)) { break; }
472 if (inFASTA.eof()) { break; }
476 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
479 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); 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); m->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)
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 num = driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
559 //pass numSeqs to parent
561 string tempFile = outputFileName + toString(getpid()) + ".num.temp";
562 m->openOutputFile(tempFile, out);
567 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
570 //force parent to wait until all the processes are done
571 for (int i=0;i<processors;i++) {
572 int temp = processIDS[i];
576 for (int i = 0; i < processIDS.size(); i++) {
578 string tempFile = outputFileName + toString(processIDS[i]) + ".num.temp";
579 m->openInputFile(tempFile, in);
580 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
581 in.close(); remove(tempFile.c_str());
587 catch(exception& e) {
588 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
592 /**************************************************************************************************/