2 * classifyseqscommand.cpp
5 * Created by westcott on 11/2/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "classifyseqscommand.h"
11 #include "sequence.hpp"
13 #include "phylotree.h"
14 #include "phylosummary.h"
18 //**********************************************************************************************************************
19 vector<string> ClassifySeqsCommand::setParameters(){
21 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptaxonomy);
22 CommandParameter ptemplate("reference", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptemplate);
23 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
24 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
25 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
26 CommandParameter psearch("search", "Multiple", "kmer-blast-suffix-distance", "kmer", "", "", "",false,false); parameters.push_back(psearch);
27 CommandParameter pksize("ksize", "Number", "", "8", "", "", "",false,false); parameters.push_back(pksize);
28 CommandParameter pmethod("method", "Multiple", "bayesian-knn", "bayesian", "", "", "",false,false); parameters.push_back(pmethod);
29 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
30 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pmatch);
31 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pmismatch);
32 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "",false,false); parameters.push_back(pgapopen);
33 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pgapextend);
34 CommandParameter pcutoff("cutoff", "Number", "", "0", "", "", "",false,true); parameters.push_back(pcutoff);
35 CommandParameter pprobs("probs", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pprobs);
36 CommandParameter piters("iters", "Number", "", "100", "", "", "",false,true); parameters.push_back(piters);
37 CommandParameter pnumwanted("numwanted", "Number", "", "10", "", "", "",false,true); parameters.push_back(pnumwanted);
38 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
39 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
41 vector<string> myArray;
42 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
46 m->errorOut(e, "ClassifySeqsCommand", "setParameters");
50 //**********************************************************************************************************************
51 string ClassifySeqsCommand::getHelpString(){
53 string helpString = "";
54 helpString += "The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n";
55 helpString += "The classify.seqs command parameters are reference, fasta, name, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend, numwanted and probs.\n";
56 helpString += "The reference, fasta and taxonomy parameters are required. You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n";
57 helpString += "The search parameter allows you to specify the method to find most similar template. Your options are: suffix, kmer, blast and distance. The default is kmer.\n";
58 helpString += "The name parameter allows you add a names file with your fasta file, if you enter multiple fasta files, you must enter matching names files for them.\n";
59 helpString += "The group parameter allows you add a group file so you can have the summary totals broken up by group.\n";
60 helpString += "The method parameter allows you to specify classification method to use. Your options are: bayesian and knn. The default is bayesian.\n";
61 helpString += "The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n";
62 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
64 helpString += "When using MPI, the processors parameter is set to the number of MPI processes running. \n";
66 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
67 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
68 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
69 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
70 helpString += "The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n";
71 helpString += "The cutoff parameter allows you to specify a bootstrap confidence threshold for your taxonomy. The default is 0.\n";
72 helpString += "The probs parameter shuts off the bootstrapping results for the bayesian method. The default is true, meaning you want the bootstrapping to be shown.\n";
73 helpString += "The iters parameter allows you to specify how many iterations to do when calculating the bootstrap confidence score for your taxonomy with the bayesian method. The default is 100.\n";
74 helpString += "The classify.seqs command should be in the following format: \n";
75 helpString += "classify.seqs(reference=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n";
76 helpString += "Example classify.seqs(fasta=amazon.fasta, reference=core.filtered, method=knn, search=gotoh, ksize=8, processors=2)\n";
77 helpString += "The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n";
78 helpString += "The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n";
79 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
83 m->errorOut(e, "ClassifySeqsCommand", "getHelpString");
87 //**********************************************************************************************************************
88 ClassifySeqsCommand::ClassifySeqsCommand(){
90 abort = true; calledHelp = true;
92 vector<string> tempOutNames;
93 outputTypes["taxonomy"] = tempOutNames;
94 outputTypes["taxsummary"] = tempOutNames;
95 outputTypes["matchdist"] = tempOutNames;
98 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
102 //**********************************************************************************************************************
103 ClassifySeqsCommand::ClassifySeqsCommand(string option) {
105 abort = false; calledHelp = false;
107 //allow user to run help
108 if(option == "help") { help(); abort = true; calledHelp = true; }
109 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
112 vector<string> myArray = setParameters();
114 OptionParser parser(option);
115 map<string, string> parameters = parser.getParameters();
117 ValidParameters validParameter("classify.seqs");
118 map<string, string>::iterator it;
120 //check to make sure all parameters are valid for command
121 for (it = parameters.begin(); it != parameters.end(); it++) {
122 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
125 //initialize outputTypes
126 vector<string> tempOutNames;
127 outputTypes["taxonomy"] = tempOutNames;
128 outputTypes["taxsummary"] = tempOutNames;
129 outputTypes["matchdist"] = tempOutNames;
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
134 //if the user changes the input directory command factory will send this info to us in the output parameter
135 string inputDir = validParameter.validFile(parameters, "inputdir", false);
136 if (inputDir == "not found"){ inputDir = ""; }
139 it = parameters.find("reference");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["reference"] = inputDir + it->second; }
147 it = parameters.find("taxonomy");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
155 it = parameters.find("group");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["group"] = inputDir + it->second; }
164 //check for required parameters
165 templateFileName = validParameter.validFile(parameters, "reference", true);
166 if (templateFileName == "not found") {
167 m->mothurOut("reference is a required parameter for the classify.seqs command.");
168 m->mothurOutEndLine();
171 else if (templateFileName == "not open") { abort = true; }
174 fastaFileName = validParameter.validFile(parameters, "fasta", false);
175 if (fastaFileName == "not found") {
176 //if there is a current fasta file, use it
177 string filename = m->getFastaFile();
178 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
179 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
182 m->splitAtDash(fastaFileName, fastaFileNames);
184 //go through files and make sure they are good, if not, then disregard them
185 for (int i = 0; i < fastaFileNames.size(); i++) {
188 if (fastaFileNames[i] == "current") {
189 fastaFileNames[i] = m->getFastaFile();
190 if (fastaFileNames[i] != "") { m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
192 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
193 //erase from file list
194 fastaFileNames.erase(fastaFileNames.begin()+i);
201 if (inputDir != "") {
202 string path = m->hasPath(fastaFileNames[i]);
203 //if the user has not given a path then, add inputdir. else leave path alone.
204 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
210 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
212 //if you can't open it, try default location
213 if (ableToOpen == 1) {
214 if (m->getDefaultPath() != "") { //default path is set
215 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
216 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
218 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
220 fastaFileNames[i] = tryPath;
224 if (ableToOpen == 1) {
225 if (m->getOutputDir() != "") { //default path is set
226 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
227 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
229 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
231 fastaFileNames[i] = tryPath;
237 if (ableToOpen == 1) {
238 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
239 //erase from file list
240 fastaFileNames.erase(fastaFileNames.begin()+i);
247 //make sure there is at least one valid file left
248 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
252 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
253 if (taxonomyFileName == "not found") {
254 m->mothurOut("taxonomy is a required parameter for the classify.seqs command.");
255 m->mothurOutEndLine();
258 else if (taxonomyFileName == "not open") { abort = true; }
261 namefile = validParameter.validFile(parameters, "name", false);
262 if (namefile == "not found") { namefile = ""; }
265 m->splitAtDash(namefile, namefileNames);
267 //go through files and make sure they are good, if not, then disregard them
268 for (int i = 0; i < namefileNames.size(); i++) {
270 if (namefileNames[i] == "current") {
271 namefileNames[i] = m->getNameFile();
272 if (namefileNames[i] != "") { m->mothurOut("Using " + namefileNames[i] + " as input file for the name parameter where you had given current."); m->mothurOutEndLine(); }
274 m->mothurOut("You have no current namefile, ignoring current."); m->mothurOutEndLine(); ignore=true;
275 //erase from file list
276 namefileNames.erase(namefileNames.begin()+i);
283 if (inputDir != "") {
284 string path = m->hasPath(namefileNames[i]);
285 //if the user has not given a path then, add inputdir. else leave path alone.
286 if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; }
291 ableToOpen = m->openInputFile(namefileNames[i], in, "noerror");
293 //if you can't open it, try default location
294 if (ableToOpen == 1) {
295 if (m->getDefaultPath() != "") { //default path is set
296 string tryPath = m->getDefaultPath() + m->getSimpleName(namefileNames[i]);
297 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
299 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
301 namefileNames[i] = tryPath;
305 if (ableToOpen == 1) {
306 if (m->getOutputDir() != "") { //default path is set
307 string tryPath = m->getOutputDir() + m->getSimpleName(namefileNames[i]);
308 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
310 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
312 namefileNames[i] = tryPath;
317 if (ableToOpen == 1) {
318 m->mothurOut("Unable to open " + namefileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); abort = true;
319 //erase from file list
320 namefileNames.erase(namefileNames.begin()+i);
327 if (namefile != "") {
328 if (namefileNames.size() != fastaFileNames.size()) { abort = true; m->mothurOut("If you provide a name file, you must have one for each fasta file."); m->mothurOutEndLine(); }
331 groupfile = validParameter.validFile(parameters, "group", false);
332 if (groupfile == "not found") { groupfile = ""; }
334 m->splitAtDash(groupfile, groupfileNames);
336 //go through files and make sure they are good, if not, then disregard them
337 for (int i = 0; i < groupfileNames.size(); i++) {
338 if (inputDir != "") {
339 string path = m->hasPath(groupfileNames[i]);
340 //if the user has not given a path then, add inputdir. else leave path alone.
341 if (path == "") { groupfileNames[i] = inputDir + groupfileNames[i]; }
346 ableToOpen = m->openInputFile(groupfileNames[i], in, "noerror");
348 //if you can't open it, try default location
349 if (ableToOpen == 1) {
350 if (m->getDefaultPath() != "") { //default path is set
351 string tryPath = m->getDefaultPath() + m->getSimpleName(groupfileNames[i]);
352 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
354 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
356 groupfileNames[i] = tryPath;
360 if (ableToOpen == 1) {
361 if (m->getOutputDir() != "") { //default path is set
362 string tryPath = m->getOutputDir() + m->getSimpleName(groupfileNames[i]);
363 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
365 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
367 groupfileNames[i] = tryPath;
373 if (ableToOpen == 1) {
374 m->mothurOut("Unable to open " + groupfileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); groupfileNames[i] = "";
375 //erase from file list
376 groupfileNames.erase(groupfileNames.begin()+i);
382 if (groupfile != "") {
383 if (groupfileNames.size() != fastaFileNames.size()) { abort = true; m->mothurOut("If you provide a group file, you must have one for each fasta file."); m->mothurOutEndLine(); }
385 for (int i = 0; i < fastaFileNames.size(); i++) { groupfileNames.push_back(""); }
388 //check for optional parameter and set defaults
389 // ...at some point should added some additional type checking...
391 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
392 convert(temp, kmerSize);
394 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
395 m->setProcessors(temp);
396 convert(temp, processors);
398 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
400 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
402 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
403 convert(temp, match);
405 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
406 convert(temp, misMatch);
408 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
409 convert(temp, gapOpen);
411 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
412 convert(temp, gapExtend);
414 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
415 convert(temp, numWanted);
417 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
418 convert(temp, cutoff);
420 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
421 probs = m->isTrue(temp);
423 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
424 convert(temp, iters);
428 if ((method == "bayesian") && (search != "kmer")) {
429 m->mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); m->mothurOutEndLine();
435 catch(exception& e) {
436 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
441 //**********************************************************************************************************************
442 ClassifySeqsCommand::~ClassifySeqsCommand(){
443 if (abort == false) {
444 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
447 //**********************************************************************************************************************
449 int ClassifySeqsCommand::execute(){
451 if (abort == true) { if (calledHelp) { return 0; } return 2; }
453 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters); }
454 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted); }
456 m->mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
457 m->mothurOutEndLine();
458 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters);
461 if (m->control_pressed) { delete classify; return 0; }
464 for (int s = 0; s < fastaFileNames.size(); s++) {
466 m->mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
468 string RippedTaxName = m->getRootName(m->getSimpleName(taxonomyFileName));
469 RippedTaxName = m->getExtension(RippedTaxName.substr(0, RippedTaxName.length()-1));
470 if (RippedTaxName[0] == '.') { RippedTaxName = RippedTaxName.substr(1, RippedTaxName.length()); }
471 RippedTaxName += ".";
473 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
474 string newTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "taxonomy";
475 string tempTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
476 string taxSummary = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "tax.summary";
478 if ((method == "knn") && (search == "distance")) {
479 string DistName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "match.dist";
480 classify->setDistName(DistName); outputNames.push_back(DistName); outputTypes["matchdist"].push_back(DistName);
483 outputNames.push_back(newTaxonomyFile); outputTypes["taxonomy"].push_back(newTaxonomyFile);
484 outputNames.push_back(taxSummary); outputTypes["taxsummary"].push_back(taxSummary);
486 int start = time(NULL);
487 int numFastaSeqs = 0;
488 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
491 int pid, numSeqsPerProcessor;
493 vector<unsigned long int> MPIPos;
496 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
497 MPI_Comm_size(MPI_COMM_WORLD, &processors);
500 MPI_File outMPINewTax;
501 MPI_File outMPITempTax;
503 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
504 int inMode=MPI_MODE_RDONLY;
506 char outNewTax[1024];
507 strcpy(outNewTax, newTaxonomyFile.c_str());
509 char outTempTax[1024];
510 strcpy(outTempTax, tempTaxonomyFile.c_str());
512 char inFileName[1024];
513 strcpy(inFileName, fastaFileNames[s].c_str());
515 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
516 MPI_File_open(MPI_COMM_WORLD, outNewTax, outMode, MPI_INFO_NULL, &outMPINewTax);
517 MPI_File_open(MPI_COMM_WORLD, outTempTax, outMode, MPI_INFO_NULL, &outMPITempTax);
519 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
521 if (pid == 0) { //you are the root process
523 MPIPos = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
525 //send file positions to all processes
526 for(int i = 1; i < processors; i++) {
527 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
528 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
531 //figure out how many sequences you have to align
532 numSeqsPerProcessor = numFastaSeqs / processors;
533 int startIndex = pid * numSeqsPerProcessor;
534 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
538 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
540 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
542 for (int i = 1; i < processors; i++) {
544 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
546 }else{ //you are a child process
547 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
548 MPIPos.resize(numFastaSeqs+1);
549 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
551 //figure out how many sequences you have to align
552 numSeqsPerProcessor = numFastaSeqs / processors;
553 int startIndex = pid * numSeqsPerProcessor;
554 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
558 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
560 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
563 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
567 MPI_File_close(&inMPI);
568 MPI_File_close(&outMPINewTax);
569 MPI_File_close(&outMPITempTax);
570 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
574 vector<unsigned long int> positions = m->divideFile(fastaFileNames[s], processors);
576 for (int i = 0; i < (positions.size()-1); i++) {
577 lines.push_back(new linePair(positions[i], positions[(i+1)]));
580 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
582 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
585 processIDS.resize(0);
587 numFastaSeqs = createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
591 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
595 m->mothurOutEndLine();
596 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
601 if (pid == 0) { //this part does not need to be paralellized
603 if(namefile != "") { m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush(); MPIReadNamesFile(namefileNames[s]); m->mothurOut(" Done."); m->mothurOutEndLine(); }
608 m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush();
610 nameMap.clear(); //remove old names
613 m->openInputFile(namefileNames[s], inNames);
615 string firstCol, secondCol;
616 while(!inNames.eof()) {
617 inNames >> firstCol >> secondCol; m->gobble(inNames);
620 m->splitAtComma(secondCol, temp);
622 nameMap[firstCol] = temp;
626 m->mothurOut(" Done."); m->mothurOutEndLine();
631 if (groupfile != "") { group = groupfileNames[s]; }
633 PhyloSummary taxaSum(taxonomyFileName, group);
635 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
637 if (namefile == "") { taxaSum.summarize(tempTaxonomyFile); }
640 m->openInputFile(tempTaxonomyFile, in);
642 //read in users taxonomy file and add sequences to tree
646 in >> name >> taxon; m->gobble(in);
648 itNames = nameMap.find(name);
650 if (itNames == nameMap.end()) {
651 m->mothurOut(name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
653 for (int i = 0; i < itNames->second.size(); i++) {
654 taxaSum.addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
656 itNames->second.clear();
657 nameMap.erase(itNames->first);
662 remove(tempTaxonomyFile.c_str());
664 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
668 m->openOutputFile(taxSummary, outTaxTree);
669 taxaSum.print(outTaxTree);
672 //output taxonomy with the unclassified bins added
674 m->openInputFile(newTaxonomyFile, inTax);
677 string unclass = newTaxonomyFile + ".unclass.temp";
678 m->openOutputFile(unclass, outTax);
680 //get maxLevel from phylotree so you know how many 'unclassified's to add
681 int maxLevel = taxaSum.getMaxLevel();
683 //read taxfile - this reading and rewriting is done to preserve the confidence scores.
685 while (!inTax.eof()) {
686 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } remove(unclass.c_str()); delete classify; return 0; }
688 inTax >> name >> taxon; m->gobble(inTax);
690 string newTax = addUnclassifieds(taxon, maxLevel);
692 outTax << name << '\t' << newTax << endl;
697 remove(newTaxonomyFile.c_str());
698 rename(unclass.c_str(), newTaxonomyFile.c_str());
700 m->mothurOutEndLine();
701 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
707 m->mothurOutEndLine();
708 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
709 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
710 m->mothurOutEndLine();
713 //set taxonomy file as new current taxonomyfile
715 itTypes = outputTypes.find("taxonomy");
716 if (itTypes != outputTypes.end()) {
717 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
723 catch(exception& e) {
724 m->errorOut(e, "ClassifySeqsCommand", "execute");
729 /**************************************************************************************************/
730 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
732 string newTax, taxon;
735 //keep what you have counting the levels
736 while (tax.find_first_of(';') != -1) {
738 taxon = tax.substr(0,tax.find_first_of(';'))+';';
739 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
744 //add "unclassified" until you reach maxLevel
745 while (level < maxlevel) {
746 newTax += "unclassified;";
752 catch(exception& e) {
753 m->errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
758 /**************************************************************************************************/
760 int ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
762 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
766 //loop through and create all the processes you want
767 while (process != processors) {
771 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
774 num = driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
776 //pass numSeqs to parent
778 string tempFile = filename + toString(getpid()) + ".num.temp";
779 m->openOutputFile(tempFile, out);
785 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
786 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
791 //parent does its part
792 num = driver(lines[0], taxFileName, tempTaxFile, filename);
794 //force parent to wait until all the processes are done
795 for (int i=0;i<processIDS.size();i++) {
796 int temp = processIDS[i];
800 for (int i = 0; i < processIDS.size(); i++) {
802 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
803 m->openInputFile(tempFile, in);
804 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
805 in.close(); remove(tempFile.c_str());
808 for(int i=0;i<processIDS.size();i++){
809 appendTaxFiles((taxFileName + toString(processIDS[i]) + ".temp"), taxFileName);
810 appendTaxFiles((tempTaxFile + toString(processIDS[i]) + ".temp"), tempTaxFile);
811 remove((taxFileName + toString(processIDS[i]) + ".temp").c_str());
812 remove((tempTaxFile + toString(processIDS[i]) + ".temp").c_str());
818 catch(exception& e) {
819 m->errorOut(e, "ClassifySeqsCommand", "createProcesses");
823 /**************************************************************************************************/
825 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
830 m->openOutputFileAppend(filename, output);
831 m->openInputFile(temp, input);
833 while(char c = input.get()){
834 if(input.eof()) { break; }
835 else { output << c; }
841 catch(exception& e) {
842 m->errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
847 //**********************************************************************************************************************
849 int ClassifySeqsCommand::driver(linePair* filePos, string taxFName, string tempTFName, string filename){
852 m->openOutputFile(taxFName, outTax);
854 ofstream outTaxSimple;
855 m->openOutputFile(tempTFName, outTaxSimple);
858 m->openInputFile(filename, inFASTA);
862 inFASTA.seekg(filePos->start);
868 if (m->control_pressed) { return 0; }
870 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
872 if (candidateSeq->getName() != "") {
874 taxonomy = classify->getTaxonomy(candidateSeq);
876 if (m->control_pressed) { delete candidateSeq; return 0; }
878 if (taxonomy != "bad seq") {
879 //output confidence scores or not
881 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
883 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
886 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
892 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
893 unsigned long int pos = inFASTA.tellg();
894 if ((pos == -1) || (pos >= filePos->end)) { break; }
896 if (inFASTA.eof()) { break; }
900 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
904 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
908 outTaxSimple.close();
912 catch(exception& e) {
913 m->errorOut(e, "ClassifySeqsCommand", "driver");
917 //**********************************************************************************************************************
919 int ClassifySeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& newFile, MPI_File& tempFile, vector<unsigned long int>& MPIPos){
921 MPI_Status statusNew;
922 MPI_Status statusTemp;
926 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
931 for(int i=0;i<num;i++){
933 if (m->control_pressed) { return 0; }
936 int length = MPIPos[start+i+1] - MPIPos[start+i];
937 char* buf4 = new char[length];
938 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
940 string tempBuf = buf4;
941 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
942 istringstream iss (tempBuf,istringstream::in);
945 Sequence* candidateSeq = new Sequence(iss);
947 if (candidateSeq->getName() != "") {
948 taxonomy = classify->getTaxonomy(candidateSeq);
950 if (taxonomy != "bad seq") {
951 //output confidence scores or not
953 outputString = candidateSeq->getName() + "\t" + taxonomy + "\n";
955 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
958 int length = outputString.length();
959 char* buf2 = new char[length];
960 memcpy(buf2, outputString.c_str(), length);
962 MPI_File_write_shared(newFile, buf2, length, MPI_CHAR, &statusNew);
965 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
966 length = outputString.length();
967 char* buf = new char[length];
968 memcpy(buf, outputString.c_str(), length);
970 MPI_File_write_shared(tempFile, buf, length, MPI_CHAR, &statusTemp);
976 if((i+1) % 100 == 0){ cout << "Classifying sequence " << (i+1) << endl; }
979 if(num % 100 != 0){ cout << "Classifying sequence " << (num) << endl; }
984 catch(exception& e) {
985 m->errorOut(e, "ClassifySeqsCommand", "driverMPI");
990 //**********************************************************************************************************************
991 int ClassifySeqsCommand::MPIReadNamesFile(string nameFilename){
994 nameMap.clear(); //remove old names
1000 //char* inFileName = new char[nameFilename.length()];
1001 //memcpy(inFileName, nameFilename.c_str(), nameFilename.length());
1003 char inFileName[1024];
1004 strcpy(inFileName, nameFilename.c_str());
1006 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
1007 MPI_File_get_size(inMPI, &size);
1008 //delete inFileName;
1010 char* buffer = new char[size];
1011 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
1013 string tempBuf = buffer;
1014 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
1015 istringstream iss (tempBuf,istringstream::in);
1018 string firstCol, secondCol;
1020 iss >> firstCol >> secondCol; m->gobble(iss);
1022 vector<string> temp;
1023 m->splitAtComma(secondCol, temp);
1025 nameMap[firstCol] = temp;
1028 MPI_File_close(&inMPI);
1032 catch(exception& e) {
1033 m->errorOut(e, "ClassifySeqsCommand", "MPIReadNamesFile");
1038 /**************************************************************************************************/