2 * chimeraslayercommand.cpp
5 * Created by westcott on 3/31/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeraslayercommand.h"
11 #include "chimeraslayer.h"
12 #include "deconvolutecommand.h"
14 //**********************************************************************************************************************
15 vector<string> ChimeraSlayerCommand::getValidParameters(){
17 string AlignArray[] = {"fasta", "processors", "name","window", "template","numwanted", "ksize", "match","mismatch",
18 "divergence", "minsim","mincov","minbs", "minsnp","parents", "iters","outputdir","inputdir", "search","realign" };
19 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
23 m->errorOut(e, "ChimeraSlayerCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 ChimeraSlayerCommand::ChimeraSlayerCommand(){
30 vector<string> tempOutNames;
31 outputTypes["chimera"] = tempOutNames;
32 outputTypes["accnos"] = tempOutNames;
35 m->errorOut(e, "ChimeraSlayerCommand", "ChimeraSlayerCommand");
39 //**********************************************************************************************************************
40 vector<string> ChimeraSlayerCommand::getRequiredParameters(){
42 string AlignArray[] = {"template","fasta"};
43 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
47 m->errorOut(e, "ChimeraSlayerCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> ChimeraSlayerCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "ChimeraSlayerCommand", "getRequiredFiles");
62 //***************************************************************************************************************
63 ChimeraSlayerCommand::ChimeraSlayerCommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"fasta", "processors","name", "window", "template","numwanted", "ksize", "match","mismatch",
73 "divergence", "minsim","mincov","minbs", "minsnp","parents", "iters","outputdir","inputdir", "search","realign" };
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter("chimera.slayer");
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 vector<string> tempOutNames;
88 outputTypes["chimera"] = tempOutNames;
89 outputTypes["accnos"] = tempOutNames;
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
95 //check for required parameters
96 fastafile = validParameter.validFile(parameters, "fasta", false);
97 if (fastafile == "not found") { fastafile = ""; m->mothurOut("[ERROR]: fasta is a required parameter for the chimera.slayer command."); m->mothurOutEndLine(); abort = true; }
99 m->splitAtDash(fastafile, fastaFileNames);
101 //go through files and make sure they are good, if not, then disregard them
102 for (int i = 0; i < fastaFileNames.size(); i++) {
103 if (inputDir != "") {
104 string path = m->hasPath(fastaFileNames[i]);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
112 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
114 //if you can't open it, try default location
115 if (ableToOpen == 1) {
116 if (m->getDefaultPath() != "") { //default path is set
117 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
118 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
120 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
122 fastaFileNames[i] = tryPath;
126 if (ableToOpen == 1) {
127 if (m->getOutputDir() != "") { //default path is set
128 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
129 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
131 ableToOpen = m->openInputFile(tryPath, in2, "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("[ERROR]: no valid files."); m->mothurOutEndLine(); abort = true; }
152 //check for required parameters
154 namefile = validParameter.validFile(parameters, "name", false);
155 if (namefile == "not found") { namefile = ""; hasName = false; }
157 m->splitAtDash(namefile, nameFileNames);
159 //go through files and make sure they are good, if not, then disregard them
160 for (int i = 0; i < nameFileNames.size(); i++) {
161 if (inputDir != "") {
162 string path = m->hasPath(nameFileNames[i]);
163 //if the user has not given a path then, add inputdir. else leave path alone.
164 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
170 ableToOpen = m->openInputFile(nameFileNames[i], in, "noerror");
172 //if you can't open it, try default location
173 if (ableToOpen == 1) {
174 if (m->getDefaultPath() != "") { //default path is set
175 string tryPath = m->getDefaultPath() + m->getSimpleName(nameFileNames[i]);
176 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
178 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
180 nameFileNames[i] = tryPath;
184 if (ableToOpen == 1) {
185 if (m->getOutputDir() != "") { //default path is set
186 string tryPath = m->getOutputDir() + m->getSimpleName(nameFileNames[i]);
187 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
189 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
191 nameFileNames[i] = tryPath;
197 if (ableToOpen == 1) {
198 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
199 //erase from file list
200 nameFileNames.erase(nameFileNames.begin()+i);
205 //make sure there is at least one valid file left
206 if (nameFileNames.size() == 0) { m->mothurOut("[ERROR]: no valid name files."); m->mothurOutEndLine(); abort = true; }
209 if (hasName && (nameFileNames.size() != fastaFileNames.size())) { m->mothurOut("[ERROR]: The number of namefiles does not match the number of fastafiles, please correct."); m->mothurOutEndLine(); abort=true; }
211 //if the user changes the output directory command factory will send this info to us in the output parameter
212 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
216 it = parameters.find("template");
217 //user has given a template file
218 if(it != parameters.end()){
219 if (it->second == "self") { templatefile = "self"; }
221 path = m->hasPath(it->second);
222 //if the user has not given a path then, add inputdir. else leave path alone.
223 if (path == "") { parameters["template"] = inputDir + it->second; }
225 templatefile = validParameter.validFile(parameters, "template", true);
226 if (templatefile == "not open") { abort = true; }
227 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.slayer command."); m->mothurOutEndLine(); abort = true; }
231 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
232 convert(temp, processors);
234 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
235 convert(temp, ksize);
237 temp = validParameter.validFile(parameters, "window", false); if (temp == "not found") { temp = "50"; }
238 convert(temp, window);
240 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found") { temp = "5"; }
241 convert(temp, match);
243 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found") { temp = "-4"; }
244 convert(temp, mismatch);
246 temp = validParameter.validFile(parameters, "divergence", false); if (temp == "not found") { temp = "1.007"; }
249 temp = validParameter.validFile(parameters, "minsim", false); if (temp == "not found") { temp = "90"; }
250 convert(temp, minSimilarity);
252 temp = validParameter.validFile(parameters, "mincov", false); if (temp == "not found") { temp = "70"; }
253 convert(temp, minCoverage);
255 temp = validParameter.validFile(parameters, "minbs", false); if (temp == "not found") { temp = "90"; }
256 convert(temp, minBS);
258 temp = validParameter.validFile(parameters, "minsnp", false); if (temp == "not found") { temp = "100"; }
259 convert(temp, minSNP);
261 temp = validParameter.validFile(parameters, "parents", false); if (temp == "not found") { temp = "3"; }
262 convert(temp, parents);
264 temp = validParameter.validFile(parameters, "realign", false); if (temp == "not found") { temp = "f"; }
265 realign = m->isTrue(temp);
267 search = validParameter.validFile(parameters, "search", false); if (search == "not found") { search = "distance"; }
269 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
270 convert(temp, iters);
272 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "5"; }
273 convert(temp, increment);
275 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found") { temp = "15"; }
276 convert(temp, numwanted);
278 if ((search != "distance") && (search != "blast") && (search != "kmer")) { m->mothurOut(search + " is not a valid search."); m->mothurOutEndLine(); abort = true; }
281 catch(exception& e) {
282 m->errorOut(e, "ChimeraSlayerCommand", "ChimeraSlayerCommand");
286 //**********************************************************************************************************************
288 void ChimeraSlayerCommand::help(){
291 m->mothurOut("The chimera.slayer command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
292 m->mothurOut("This command was modeled after the chimeraSlayer written by the Broad Institute.\n");
293 m->mothurOut("The chimera.slayer command parameters are fasta, name, template, processors, ksize, window, match, mismatch, divergence. minsim, mincov, minbs, minsnp, parents, search, iters, increment and numwanted.\n"); //realign,
294 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
295 m->mothurOut("The name parameter allows you to provide a name file, if you are using template=self. \n");
296 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amazon.fasta \n");
297 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. You may also set template=self, in this case the abundant sequences will be used as potential parents. \n");
298 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
300 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
302 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras, default=50. \n");
303 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default=5.\n");
304 m->mothurOut("The numwanted parameter allows you to specify how many sequences you would each query sequence compared with, default=15.\n");
305 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7, used if search is kmer. \n");
306 m->mothurOut("The match parameter allows you to reward matched bases in blast search, default is 5. \n");
307 m->mothurOut("The parents parameter allows you to select the number of potential parents to investigate from the numwanted best matches after rating them, default is 3. \n");
308 m->mothurOut("The mismatch parameter allows you to penalize mismatched bases in blast search, default is -4. \n");
309 m->mothurOut("The divergence parameter allows you to set a cutoff for chimera determination, default is 1.007. \n");
310 m->mothurOut("The iters parameter allows you to specify the number of bootstrap iters to do with the chimeraslayer method, default=100.\n");
311 m->mothurOut("The minsim parameter allows you to specify a minimum similarity with the parent fragments, default=90. \n");
312 m->mothurOut("The mincov parameter allows you to specify minimum coverage by closest matches found in template. Default is 70, meaning 70%. \n");
313 m->mothurOut("The minbs parameter allows you to specify minimum bootstrap support for calling a sequence chimeric. Default is 90, meaning 90%. \n");
314 m->mothurOut("The minsnp parameter allows you to specify percent of SNPs to sample on each side of breakpoint for computing bootstrap support (default: 100) \n");
315 m->mothurOut("The search parameter allows you to specify search method for finding the closest parent. Choices are distance, blast, and kmer, default distance. \n");
316 m->mothurOut("The realign parameter allows you to realign the query to the potential parents. Choices are true or false, default false. \n");
317 m->mothurOut("The chimera.slayer command should be in the following format: \n");
318 m->mothurOut("chimera.slayer(fasta=yourFastaFile, template=yourTemplate, search=yourSearch) \n");
319 m->mothurOut("Example: chimera.slayer(fasta=AD.align, template=core_set_aligned.imputed.fasta, search=kmer) \n");
320 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
322 catch(exception& e) {
323 m->errorOut(e, "ChimeraSlayerCommand", "help");
328 //***************************************************************************************************************
330 ChimeraSlayerCommand::~ChimeraSlayerCommand(){ /* do nothing */ }
332 //***************************************************************************************************************
334 int ChimeraSlayerCommand::execute(){
337 if (abort == true) { return 0; }
339 for (int s = 0; s < fastaFileNames.size(); s++) {
341 m->mothurOut("Checking sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
343 int start = time(NULL);
345 if (templatefile != "self") { //you want to run slayer with a refernce template
346 chimera = new ChimeraSlayer(fastaFileNames[s], templatefile, search, ksize, match, mismatch, window, divR, minSimilarity, minCoverage, minBS, minSNP, parents, iters, increment, numwanted, realign);
348 if (nameFileNames.size() != 0) { //you provided a namefile and we don't need to create one
349 chimera = new ChimeraSlayer(fastaFileNames[s], templatefile, nameFileNames[s], search, ksize, match, mismatch, window, divR, minSimilarity, minCoverage, minBS, minSNP, parents, iters, increment, numwanted, realign);
352 m->mothurOutEndLine(); m->mothurOut("No namesfile given, running unique.seqs command to generate one."); m->mothurOutEndLine(); m->mothurOutEndLine();
354 //use unique.seqs to create new name and fastafile
355 string inputString = "fasta=" + fastaFileNames[s];
356 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
357 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
359 Command* uniqueCommand = new DeconvoluteCommand(inputString);
360 uniqueCommand->execute();
362 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
364 delete uniqueCommand;
366 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
368 string nameFile = filenames["name"][0];
369 fastaFileNames[s] = filenames["fasta"][0];
371 chimera = new ChimeraSlayer(fastaFileNames[s], templatefile, nameFile, search, ksize, match, mismatch, window, divR, minSimilarity, minCoverage, minBS, minSNP, parents, iters, increment, numwanted, realign);
375 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[s]); }//if user entered a file with a path then preserve it
376 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "slayer.chimeras";
377 string accnosFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "slayer.accnos";
379 if (m->control_pressed) { delete chimera; for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
381 if (chimera->getUnaligned()) {
382 m->mothurOut("Your template sequences are different lengths, please correct."); m->mothurOutEndLine();
386 templateSeqsLength = chimera->getLength();
389 int pid, end, numSeqsPerProcessor;
391 vector<unsigned long int> MPIPos;
394 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
395 MPI_Comm_size(MPI_COMM_WORLD, &processors);
399 MPI_File outMPIAccnos;
401 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
402 int inMode=MPI_MODE_RDONLY;
404 char outFilename[1024];
405 strcpy(outFilename, outputFileName.c_str());
407 char outAccnosFilename[1024];
408 strcpy(outAccnosFilename, accnosFileName.c_str());
410 char inFileName[1024];
411 strcpy(inFileName, fastaFileNames[s].c_str());
413 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
414 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
415 MPI_File_open(MPI_COMM_WORLD, outAccnosFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
417 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
419 if (pid == 0) { //you are the root process
420 m->mothurOutEndLine();
421 m->mothurOut("Only reporting sequence supported by " + toString(minBS) + "% of bootstrapped results.");
422 m->mothurOutEndLine();
424 string outTemp = "Name\tLeftParent\tRightParent\tDivQLAQRB\tPerIDQLAQRB\tBootStrapA\tDivQLBQRA\tPerIDQLBQRA\tBootStrapB\tFlag\tLeftWindow\tRightWindow\n";
427 int length = outTemp.length();
428 char* buf2 = new char[length];
429 memcpy(buf2, outTemp.c_str(), length);
431 MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &status);
434 MPIPos = m->setFilePosFasta(fastaFileNames[s], numSeqs); //fills MPIPos, returns numSeqs
436 //send file positions to all processes
437 for(int i = 1; i < processors; i++) {
438 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
439 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
442 //figure out how many sequences you have to align
443 numSeqsPerProcessor = numSeqs / processors;
444 int startIndex = pid * numSeqsPerProcessor;
445 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
448 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
450 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } remove(outputFileName.c_str()); remove(accnosFileName.c_str()); delete chimera; return 0; }
452 }else{ //you are a child process
453 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
454 MPIPos.resize(numSeqs+1);
455 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
457 //figure out how many sequences you have to align
458 numSeqsPerProcessor = numSeqs / processors;
459 int startIndex = pid * numSeqsPerProcessor;
460 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
463 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
465 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPI); MPI_File_close(&outMPIAccnos); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
469 MPI_File_close(&inMPI);
470 MPI_File_close(&outMPI);
471 MPI_File_close(&outMPIAccnos);
472 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
476 string tempHeader = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "slayer.chimeras.tempHeader";
477 m->openOutputFile(tempHeader, outHeader);
479 chimera->printHeader(outHeader);
482 vector<unsigned long int> positions = m->divideFile(fastaFileNames[s], processors);
484 for (int i = 0; i < (positions.size()-1); i++) {
485 lines.push_back(new linePair(positions[i], positions[(i+1)]));
489 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
491 numSeqs = driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
493 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); remove(tempHeader.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear(); delete chimera; return 0; }
496 processIDS.resize(0);
498 numSeqs = createProcesses(outputFileName, fastaFileNames[s], accnosFileName);
500 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
501 rename((accnosFileName + toString(processIDS[0]) + ".temp").c_str(), accnosFileName.c_str());
503 //append output files
504 for(int i=1;i<processors;i++){
505 m->appendFiles((outputFileName + toString(processIDS[i]) + ".temp"), outputFileName);
506 remove((outputFileName + toString(processIDS[i]) + ".temp").c_str());
509 //append output files
510 for(int i=1;i<processors;i++){
511 m->appendFiles((accnosFileName + toString(processIDS[i]) + ".temp"), accnosFileName);
512 remove((accnosFileName + toString(processIDS[i]) + ".temp").c_str());
515 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear(); delete chimera; return 0; }
519 numSeqs = driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
521 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); remove(tempHeader.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear(); delete chimera; return 0; }
525 m->appendFiles(outputFileName, tempHeader);
527 remove(outputFileName.c_str());
528 rename(tempHeader.c_str(), outputFileName.c_str());
534 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
536 outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
537 outputNames.push_back(accnosFileName); outputTypes["accnos"].push_back(accnosFileName);
539 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
542 m->mothurOutEndLine();
543 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
544 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
545 m->mothurOutEndLine();
550 catch(exception& e) {
551 m->errorOut(e, "ChimeraSlayerCommand", "execute");
555 //**********************************************************************************************************************
557 int ChimeraSlayerCommand::driver(linePair* filePos, string outputFName, string filename, string accnos){
560 m->openOutputFile(outputFName, out);
563 m->openOutputFile(accnos, out2);
566 m->openInputFile(filename, inFASTA);
568 inFASTA.seekg(filePos->start);
575 if (m->control_pressed) { return 1; }
577 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
579 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
581 if (candidateSeq->getAligned().length() != templateSeqsLength) {
582 m->mothurOut(candidateSeq->getName() + " is not the same length as the template sequences. Skipping."); m->mothurOutEndLine();
585 chimera->getChimeras(candidateSeq);
587 if (m->control_pressed) { delete candidateSeq; return 1; }
590 chimera->print(out, out2);
596 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
597 unsigned long int pos = inFASTA.tellg();
598 if ((pos == -1) || (pos >= filePos->end)) { break; }
600 if (inFASTA.eof()) { break; }
604 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
607 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
615 catch(exception& e) {
616 m->errorOut(e, "ChimeraSlayerCommand", "driver");
620 //**********************************************************************************************************************
622 int ChimeraSlayerCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, MPI_File& outAccMPI, vector<unsigned long int>& MPIPos){
626 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
628 for(int i=0;i<num;i++){
630 if (m->control_pressed) { return 1; }
633 int length = MPIPos[start+i+1] - MPIPos[start+i];
635 char* buf4 = new char[length];
636 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
638 string tempBuf = buf4;
639 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
640 istringstream iss (tempBuf,istringstream::in);
644 Sequence* candidateSeq = new Sequence(iss); m->gobble(iss);
646 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
648 if (candidateSeq->getAligned().length() != templateSeqsLength) {
649 m->mothurOut(candidateSeq->getName() + " is not the same length as the template sequences. Skipping."); m->mothurOutEndLine();
653 chimera->getChimeras(candidateSeq);
655 if (m->control_pressed) { delete candidateSeq; return 1; }
656 //cout << "about to print" << endl;
658 bool isChimeric = chimera->print(outMPI, outAccMPI);
664 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
667 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
672 catch(exception& e) {
673 m->errorOut(e, "ChimeraSlayerCommand", "driverMPI");
679 /**************************************************************************************************/
681 int ChimeraSlayerCommand::createProcesses(string outputFileName, string filename, string accnos) {
683 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
687 //loop through and create all the processes you want
688 while (process != processors) {
692 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
695 num = driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename, accnos + toString(getpid()) + ".temp");
697 //pass numSeqs to parent
699 string tempFile = outputFileName + toString(getpid()) + ".num.temp";
700 m->openOutputFile(tempFile, out);
706 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
707 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
712 //force parent to wait until all the processes are done
713 for (int i=0;i<processors;i++) {
714 int temp = processIDS[i];
718 for (int i = 0; i < processIDS.size(); i++) {
720 string tempFile = outputFileName + toString(processIDS[i]) + ".num.temp";
721 m->openInputFile(tempFile, in);
722 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
723 in.close(); remove(tempFile.c_str());
729 catch(exception& e) {
730 m->errorOut(e, "ChimeraSlayerCommand", "createProcesses");
735 /**************************************************************************************************/