2 * classifyseqscommand.cpp
5 * Created by westcott on 11/2/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "classifyseqscommand.h"
14 //**********************************************************************************************************************
15 vector<string> ClassifySeqsCommand::setParameters(){
17 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptaxonomy);
18 CommandParameter ptemplate("reference", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptemplate);
19 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
20 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
21 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
22 CommandParameter psearch("search", "Multiple", "kmer-blast-suffix-distance", "kmer", "", "", "",false,false); parameters.push_back(psearch);
23 CommandParameter pksize("ksize", "Number", "", "8", "", "", "",false,false); parameters.push_back(pksize);
24 CommandParameter pmethod("method", "Multiple", "bayesian-knn", "bayesian", "", "", "",false,false); parameters.push_back(pmethod);
25 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
26 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pmatch);
27 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pmismatch);
28 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "",false,false); parameters.push_back(pgapopen);
29 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pgapextend);
30 CommandParameter pcutoff("cutoff", "Number", "", "0", "", "", "",false,true); parameters.push_back(pcutoff);
31 CommandParameter pprobs("probs", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pprobs);
32 CommandParameter piters("iters", "Number", "", "100", "", "", "",false,true); parameters.push_back(piters);
33 CommandParameter psave("save", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psave);
34 CommandParameter pnumwanted("numwanted", "Number", "", "10", "", "", "",false,true); parameters.push_back(pnumwanted);
35 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
36 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
38 vector<string> myArray;
39 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
43 m->errorOut(e, "ClassifySeqsCommand", "setParameters");
47 //**********************************************************************************************************************
48 string ClassifySeqsCommand::getHelpString(){
50 string helpString = "";
51 helpString += "The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n";
52 helpString += "The classify.seqs command parameters are reference, fasta, name, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend, numwanted and probs.\n";
53 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";
54 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";
55 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";
56 helpString += "The group parameter allows you add a group file so you can have the summary totals broken up by group.\n";
57 helpString += "The method parameter allows you to specify classification method to use. Your options are: bayesian and knn. The default is bayesian.\n";
58 helpString += "The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n";
59 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
61 helpString += "When using MPI, the processors parameter is set to the number of MPI processes running. \n";
63 helpString += "If the save parameter is set to true the reference sequences will be saved in memory, to clear them later you can use the clear.memory command. Default=f.";
64 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
65 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
66 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
67 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
68 helpString += "The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n";
69 helpString += "The cutoff parameter allows you to specify a bootstrap confidence threshold for your taxonomy. The default is 0.\n";
70 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";
71 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";
72 helpString += "The classify.seqs command should be in the following format: \n";
73 helpString += "classify.seqs(reference=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n";
74 helpString += "Example classify.seqs(fasta=amazon.fasta, reference=core.filtered, method=knn, search=gotoh, ksize=8, processors=2)\n";
75 helpString += "The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n";
76 helpString += "The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n";
77 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
81 m->errorOut(e, "ClassifySeqsCommand", "getHelpString");
85 //**********************************************************************************************************************
86 ClassifySeqsCommand::ClassifySeqsCommand(){
88 abort = true; calledHelp = true;
90 vector<string> tempOutNames;
91 outputTypes["taxonomy"] = tempOutNames;
92 outputTypes["taxsummary"] = tempOutNames;
93 outputTypes["matchdist"] = tempOutNames;
96 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
100 //**********************************************************************************************************************
101 ClassifySeqsCommand::ClassifySeqsCommand(string option) {
103 abort = false; calledHelp = false;
104 rdb = ReferenceDB::getInstance();
106 //allow user to run help
107 if(option == "help") { help(); abort = true; calledHelp = true; }
108 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
111 vector<string> myArray = setParameters();
113 OptionParser parser(option);
114 map<string, string> parameters = parser.getParameters();
116 ValidParameters validParameter("classify.seqs");
117 map<string, string>::iterator it;
119 //check to make sure all parameters are valid for command
120 for (it = parameters.begin(); it != parameters.end(); it++) {
121 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
124 //initialize outputTypes
125 vector<string> tempOutNames;
126 outputTypes["taxonomy"] = tempOutNames;
127 outputTypes["taxsummary"] = tempOutNames;
128 outputTypes["matchdist"] = tempOutNames;
130 //if the user changes the output directory command factory will send this info to us in the output parameter
131 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
133 //if the user changes the input directory command factory will send this info to us in the output parameter
134 string inputDir = validParameter.validFile(parameters, "inputdir", false);
135 if (inputDir == "not found"){ inputDir = ""; }
138 it = parameters.find("reference");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["reference"] = inputDir + it->second; }
146 it = parameters.find("taxonomy");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
154 it = parameters.find("group");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["group"] = inputDir + it->second; }
163 fastaFileName = validParameter.validFile(parameters, "fasta", false);
164 if (fastaFileName == "not found") {
165 //if there is a current fasta file, use it
166 string filename = m->getFastaFile();
167 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
168 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
171 m->splitAtDash(fastaFileName, fastaFileNames);
173 //go through files and make sure they are good, if not, then disregard them
174 for (int i = 0; i < fastaFileNames.size(); i++) {
177 if (fastaFileNames[i] == "current") {
178 fastaFileNames[i] = m->getFastaFile();
179 if (fastaFileNames[i] != "") { m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
181 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
182 //erase from file list
183 fastaFileNames.erase(fastaFileNames.begin()+i);
190 if (inputDir != "") {
191 string path = m->hasPath(fastaFileNames[i]);
192 //if the user has not given a path then, add inputdir. else leave path alone.
193 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
199 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
201 //if you can't open it, try default location
202 if (ableToOpen == 1) {
203 if (m->getDefaultPath() != "") { //default path is set
204 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
205 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
207 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
209 fastaFileNames[i] = tryPath;
213 if (ableToOpen == 1) {
214 if (m->getOutputDir() != "") { //default path is set
215 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
216 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
218 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
220 fastaFileNames[i] = tryPath;
226 if (ableToOpen == 1) {
227 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
228 //erase from file list
229 fastaFileNames.erase(fastaFileNames.begin()+i);
232 m->setFastaFile(fastaFileNames[i]);
238 //make sure there is at least one valid file left
239 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
242 namefile = validParameter.validFile(parameters, "name", false);
243 if (namefile == "not found") { namefile = ""; }
246 m->splitAtDash(namefile, namefileNames);
248 //go through files and make sure they are good, if not, then disregard them
249 for (int i = 0; i < namefileNames.size(); i++) {
251 if (namefileNames[i] == "current") {
252 namefileNames[i] = m->getNameFile();
253 if (namefileNames[i] != "") { m->mothurOut("Using " + namefileNames[i] + " as input file for the name parameter where you had given current."); m->mothurOutEndLine(); }
255 m->mothurOut("You have no current namefile, ignoring current."); m->mothurOutEndLine(); ignore=true;
256 //erase from file list
257 namefileNames.erase(namefileNames.begin()+i);
264 if (inputDir != "") {
265 string path = m->hasPath(namefileNames[i]);
266 //if the user has not given a path then, add inputdir. else leave path alone.
267 if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; }
272 ableToOpen = m->openInputFile(namefileNames[i], in, "noerror");
274 //if you can't open it, try default location
275 if (ableToOpen == 1) {
276 if (m->getDefaultPath() != "") { //default path is set
277 string tryPath = m->getDefaultPath() + m->getSimpleName(namefileNames[i]);
278 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
280 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
282 namefileNames[i] = tryPath;
286 if (ableToOpen == 1) {
287 if (m->getOutputDir() != "") { //default path is set
288 string tryPath = m->getOutputDir() + m->getSimpleName(namefileNames[i]);
289 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
291 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
293 namefileNames[i] = tryPath;
298 if (ableToOpen == 1) {
299 m->mothurOut("Unable to open " + namefileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); abort = true;
300 //erase from file list
301 namefileNames.erase(namefileNames.begin()+i);
304 m->setNameFile(namefileNames[i]);
310 if (namefile != "") {
311 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(); }
314 groupfile = validParameter.validFile(parameters, "group", false);
315 if (groupfile == "not found") { groupfile = ""; }
317 m->splitAtDash(groupfile, groupfileNames);
319 //go through files and make sure they are good, if not, then disregard them
320 for (int i = 0; i < groupfileNames.size(); i++) {
321 if (inputDir != "") {
322 string path = m->hasPath(groupfileNames[i]);
323 //if the user has not given a path then, add inputdir. else leave path alone.
324 if (path == "") { groupfileNames[i] = inputDir + groupfileNames[i]; }
329 ableToOpen = m->openInputFile(groupfileNames[i], in, "noerror");
331 //if you can't open it, try default location
332 if (ableToOpen == 1) {
333 if (m->getDefaultPath() != "") { //default path is set
334 string tryPath = m->getDefaultPath() + m->getSimpleName(groupfileNames[i]);
335 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
337 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
339 groupfileNames[i] = tryPath;
343 if (ableToOpen == 1) {
344 if (m->getOutputDir() != "") { //default path is set
345 string tryPath = m->getOutputDir() + m->getSimpleName(groupfileNames[i]);
346 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
348 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
350 groupfileNames[i] = tryPath;
356 if (ableToOpen == 1) {
357 m->mothurOut("Unable to open " + groupfileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); groupfileNames[i] = "";
358 //erase from file list
359 groupfileNames.erase(groupfileNames.begin()+i);
362 m->setGroupFile(groupfileNames[i]);
367 if (groupfile != "") {
368 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(); }
370 for (int i = 0; i < fastaFileNames.size(); i++) { groupfileNames.push_back(""); }
373 //check for optional parameter and set defaults
374 // ...at some point should added some additional type checking...
376 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
377 convert(temp, kmerSize);
379 temp = validParameter.validFile(parameters, "save", false); if (temp == "not found"){ temp = "f"; }
380 save = m->isTrue(temp);
381 //this is so the threads can quickly load the reference data
382 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
384 if (processors != 1) { save = true; }
387 if (save) { //clear out old references
391 //this has to go after save so that if the user sets save=t and provides no reference we abort
392 templateFileName = validParameter.validFile(parameters, "reference", true);
393 if (templateFileName == "not found") {
394 //check for saved reference sequences
395 if (rdb->referenceSeqs.size() != 0) {
396 templateFileName = "saved";
398 m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required for the classify.seqs command.");
399 m->mothurOutEndLine();
402 }else if (templateFileName == "not open") { abort = true; }
403 else { if (save) { rdb->setSavedReference(templateFileName); } }
405 //this has to go after save so that if the user sets save=t and provides no reference we abort
406 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
407 if (taxonomyFileName == "not found") {
408 //check for saved reference sequences
409 if (rdb->wordGenusProb.size() != 0) {
410 taxonomyFileName = "saved";
412 m->mothurOut("[ERROR]: You don't have any saved taxonomy information and the taxonomy parameter is a required for the classify.seqs command.");
413 m->mothurOutEndLine();
416 }else if (taxonomyFileName == "not open") { abort = true; }
417 else { if (save) { rdb->setSavedTaxonomy(taxonomyFileName); } }
419 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
420 m->setProcessors(temp);
421 convert(temp, processors);
423 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
425 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
427 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
428 convert(temp, match);
430 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
431 convert(temp, misMatch);
433 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
434 convert(temp, gapOpen);
436 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
437 convert(temp, gapExtend);
439 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
440 convert(temp, numWanted);
442 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
443 convert(temp, cutoff);
445 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
446 probs = m->isTrue(temp);
448 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
449 convert(temp, iters);
453 if ((method == "bayesian") && (search != "kmer")) {
454 m->mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); m->mothurOutEndLine();
460 catch(exception& e) {
461 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
466 //**********************************************************************************************************************
467 ClassifySeqsCommand::~ClassifySeqsCommand(){
468 if (abort == false) {
469 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
472 //**********************************************************************************************************************
474 int ClassifySeqsCommand::execute(){
476 if (abort == true) { if (calledHelp) { return 0; } return 2; }
478 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters, rand()); }
479 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted, rand()); }
481 m->mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
482 m->mothurOutEndLine();
483 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters, rand());
486 if (m->control_pressed) { delete classify; return 0; }
488 for (int s = 0; s < fastaFileNames.size(); s++) {
490 m->mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
492 string baseTName = taxonomyFileName;
493 if (taxonomyFileName == "saved") {baseTName = rdb->getSavedTaxonomy(); }
495 string RippedTaxName = m->getRootName(m->getSimpleName(baseTName));
496 RippedTaxName = m->getExtension(RippedTaxName.substr(0, RippedTaxName.length()-1));
497 if (RippedTaxName[0] == '.') { RippedTaxName = RippedTaxName.substr(1, RippedTaxName.length()); }
498 RippedTaxName += ".";
500 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
501 string newTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "taxonomy";
502 string tempTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
503 string taxSummary = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "tax.summary";
505 if ((method == "knn") && (search == "distance")) {
506 string DistName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "match.dist";
507 classify->setDistName(DistName); outputNames.push_back(DistName); outputTypes["matchdist"].push_back(DistName);
510 outputNames.push_back(newTaxonomyFile); outputTypes["taxonomy"].push_back(newTaxonomyFile);
511 outputNames.push_back(taxSummary); outputTypes["taxsummary"].push_back(taxSummary);
513 int start = time(NULL);
514 int numFastaSeqs = 0;
515 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
518 int pid, numSeqsPerProcessor;
520 vector<unsigned long long> MPIPos;
523 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
524 MPI_Comm_size(MPI_COMM_WORLD, &processors);
527 MPI_File outMPINewTax;
528 MPI_File outMPITempTax;
530 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
531 int inMode=MPI_MODE_RDONLY;
533 char outNewTax[1024];
534 strcpy(outNewTax, newTaxonomyFile.c_str());
536 char outTempTax[1024];
537 strcpy(outTempTax, tempTaxonomyFile.c_str());
539 char inFileName[1024];
540 strcpy(inFileName, fastaFileNames[s].c_str());
542 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
543 MPI_File_open(MPI_COMM_WORLD, outNewTax, outMode, MPI_INFO_NULL, &outMPINewTax);
544 MPI_File_open(MPI_COMM_WORLD, outTempTax, outMode, MPI_INFO_NULL, &outMPITempTax);
546 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
548 if (pid == 0) { //you are the root process
550 MPIPos = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
552 //send file positions to all processes
553 for(int i = 1; i < processors; i++) {
554 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
555 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
558 //figure out how many sequences you have to align
559 numSeqsPerProcessor = numFastaSeqs / processors;
560 int startIndex = pid * numSeqsPerProcessor;
561 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
565 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
567 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++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
569 for (int i = 1; i < processors; i++) {
571 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
573 }else{ //you are a child process
574 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
575 MPIPos.resize(numFastaSeqs+1);
576 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
578 //figure out how many sequences you have to align
579 numSeqsPerProcessor = numFastaSeqs / processors;
580 int startIndex = pid * numSeqsPerProcessor;
581 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
585 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
587 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
590 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
594 MPI_File_close(&inMPI);
595 MPI_File_close(&outMPINewTax);
596 MPI_File_close(&outMPITempTax);
597 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
601 vector<unsigned long long> positions;
602 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
603 positions = m->divideFile(fastaFileNames[s], processors);
604 for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(new linePair(positions[i], positions[(i+1)])); }
606 if (processors == 1) {
607 lines.push_back(new linePair(0, 1000));
609 positions = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs);
611 //figure out how many sequences you have to process
612 int numSeqsPerProcessor = numFastaSeqs / processors;
613 for (int i = 0; i < processors; i++) {
614 int startIndex = i * numSeqsPerProcessor;
615 if(i == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; }
616 lines.push_back(new linePair(positions[startIndex], numSeqsPerProcessor));
621 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
623 numFastaSeqs = createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
627 m->mothurOutEndLine();
628 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
633 if (pid == 0) { //this part does not need to be paralellized
635 if(namefile != "") { m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush(); MPIReadNamesFile(namefileNames[s]); m->mothurOut(" Done."); m->mothurOutEndLine(); }
640 m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush();
642 nameMap.clear(); //remove old names
645 m->openInputFile(namefileNames[s], inNames);
647 string firstCol, secondCol;
648 while(!inNames.eof()) {
649 inNames >> firstCol >> secondCol; m->gobble(inNames);
652 m->splitAtComma(secondCol, temp);
654 nameMap[firstCol] = temp;
658 m->mothurOut(" Done."); m->mothurOutEndLine();
663 if (groupfile != "") { group = groupfileNames[s]; }
665 PhyloSummary taxaSum(baseTName, group);
667 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
669 if (namefile == "") { taxaSum.summarize(tempTaxonomyFile); }
672 m->openInputFile(tempTaxonomyFile, in);
674 //read in users taxonomy file and add sequences to tree
678 in >> name >> taxon; m->gobble(in);
680 itNames = nameMap.find(name);
682 if (itNames == nameMap.end()) {
683 m->mothurOut(name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
685 for (int i = 0; i < itNames->second.size(); i++) {
686 taxaSum.addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
688 itNames->second.clear();
689 nameMap.erase(itNames->first);
694 m->mothurRemove(tempTaxonomyFile);
696 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
700 m->openOutputFile(taxSummary, outTaxTree);
701 taxaSum.print(outTaxTree);
704 //output taxonomy with the unclassified bins added
706 m->openInputFile(newTaxonomyFile, inTax);
709 string unclass = newTaxonomyFile + ".unclass.temp";
710 m->openOutputFile(unclass, outTax);
712 //get maxLevel from phylotree so you know how many 'unclassified's to add
713 int maxLevel = taxaSum.getMaxLevel();
715 //read taxfile - this reading and rewriting is done to preserve the confidence scores.
717 while (!inTax.eof()) {
718 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->mothurRemove(unclass); delete classify; return 0; }
720 inTax >> name >> taxon; m->gobble(inTax);
722 string newTax = addUnclassifieds(taxon, maxLevel);
724 outTax << name << '\t' << newTax << endl;
729 m->mothurRemove(newTaxonomyFile);
730 rename(unclass.c_str(), newTaxonomyFile.c_str());
732 m->mothurOutEndLine();
733 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
739 m->mothurOutEndLine();
740 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
741 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
742 m->mothurOutEndLine();
745 //set taxonomy file as new current taxonomyfile
747 itTypes = outputTypes.find("taxonomy");
748 if (itTypes != outputTypes.end()) {
749 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
756 catch(exception& e) {
757 m->errorOut(e, "ClassifySeqsCommand", "execute");
762 /**************************************************************************************************/
763 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
765 string newTax, taxon;
768 //keep what you have counting the levels
769 while (tax.find_first_of(';') != -1) {
771 taxon = tax.substr(0,tax.find_first_of(';'))+';';
772 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
777 //add "unclassified" until you reach maxLevel
778 while (level < maxlevel) {
779 newTax += "unclassified;";
785 catch(exception& e) {
786 m->errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
791 /**************************************************************************************************/
793 int ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
799 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
802 //loop through and create all the processes you want
803 while (process != processors) {
807 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
810 num = driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
812 //pass numSeqs to parent
814 string tempFile = filename + toString(getpid()) + ".num.temp";
815 m->openOutputFile(tempFile, out);
821 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
822 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
827 //parent does its part
828 num = driver(lines[0], taxFileName, tempTaxFile, filename);
830 //force parent to wait until all the processes are done
831 for (int i=0;i<processIDS.size();i++) {
832 int temp = processIDS[i];
836 for (int i = 0; i < processIDS.size(); i++) {
838 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
839 m->openInputFile(tempFile, in);
840 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
841 in.close(); m->mothurRemove(m->getFullPathName(tempFile));
844 //////////////////////////////////////////////////////////////////////////////////////////////////////
845 //Windows version shared memory, so be careful when passing variables through the alignData struct.
846 //Above fork() will clone, so memory is separate, but that's not the case with windows,
847 //////////////////////////////////////////////////////////////////////////////////////////////////////
849 vector<classifyData*> pDataArray;
850 DWORD dwThreadIdArray[processors-1];
851 HANDLE hThreadArray[processors-1];
853 //Create processor worker threads.
854 for( int i=0; i<processors-1; i++ ){
855 // Allocate memory for thread data.
856 string extension = "";
857 if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
859 classifyData* tempclass = new classifyData(probs, method, templateFileName, taxonomyFileName, (taxFileName + extension), (tempTaxFile + extension), filename, search, kmerSize, iters, numWanted, m, lines[i]->start, lines[i]->end, match, misMatch, gapOpen, gapExtend, cutoff, i);
860 pDataArray.push_back(tempclass);
862 //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
863 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
864 hThreadArray[i] = CreateThread(NULL, 0, MyClassThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
868 //parent does its part
869 num = driver(lines[processors-1], taxFileName + toString(processors-1) + ".temp", tempTaxFile + toString(processors-1) + ".temp", filename);
870 processIDS.push_back((processors-1));
872 //Wait until all threads have terminated.
873 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
875 //Close all thread handles and free memory allocations.
876 for(int i=0; i < pDataArray.size(); i++){
877 num += pDataArray[i]->count;
878 CloseHandle(hThreadArray[i]);
879 delete pDataArray[i];
884 for(int i=0;i<processIDS.size();i++){
885 appendTaxFiles((taxFileName + toString(processIDS[i]) + ".temp"), taxFileName);
886 appendTaxFiles((tempTaxFile + toString(processIDS[i]) + ".temp"), tempTaxFile);
887 m->mothurRemove((m->getFullPathName(taxFileName) + toString(processIDS[i]) + ".temp"));
888 m->mothurRemove((m->getFullPathName(tempTaxFile) + toString(processIDS[i]) + ".temp"));
894 catch(exception& e) {
895 m->errorOut(e, "ClassifySeqsCommand", "createProcesses");
899 /**************************************************************************************************/
901 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
906 m->openOutputFileAppend(filename, output);
907 m->openInputFile(temp, input);
909 while(char c = input.get()){
910 if(input.eof()) { break; }
911 else { output << c; }
917 catch(exception& e) {
918 m->errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
923 //**********************************************************************************************************************
925 int ClassifySeqsCommand::driver(linePair* filePos, string taxFName, string tempTFName, string filename){
928 m->openOutputFile(taxFName, outTax);
930 ofstream outTaxSimple;
931 m->openOutputFile(tempTFName, outTaxSimple);
934 m->openInputFile(filename, inFASTA);
938 inFASTA.seekg(filePos->start);
944 if (m->control_pressed) { return 0; }
946 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
948 if (candidateSeq->getName() != "") {
950 taxonomy = classify->getTaxonomy(candidateSeq);
952 if (m->control_pressed) { delete candidateSeq; return 0; }
954 if (taxonomy != "bad seq") {
955 //output confidence scores or not
957 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
959 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
962 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
968 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
969 unsigned long long pos = inFASTA.tellg();
970 if ((pos == -1) || (pos >= filePos->end)) { break; }
972 if (inFASTA.eof()) { break; }
976 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
980 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
984 outTaxSimple.close();
988 catch(exception& e) {
989 m->errorOut(e, "ClassifySeqsCommand", "driver");
993 //**********************************************************************************************************************
995 int ClassifySeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& newFile, MPI_File& tempFile, vector<unsigned long long>& MPIPos){
997 MPI_Status statusNew;
998 MPI_Status statusTemp;
1002 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
1005 string outputString;
1007 for(int i=0;i<num;i++){
1009 if (m->control_pressed) { return 0; }
1011 //read next sequence
1012 int length = MPIPos[start+i+1] - MPIPos[start+i];
1013 char* buf4 = new char[length];
1014 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
1016 string tempBuf = buf4;
1017 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
1018 istringstream iss (tempBuf,istringstream::in);
1021 Sequence* candidateSeq = new Sequence(iss);
1023 if (candidateSeq->getName() != "") {
1024 taxonomy = classify->getTaxonomy(candidateSeq);
1026 if (taxonomy != "bad seq") {
1027 //output confidence scores or not
1029 outputString = candidateSeq->getName() + "\t" + taxonomy + "\n";
1031 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
1034 int length = outputString.length();
1035 char* buf2 = new char[length];
1036 memcpy(buf2, outputString.c_str(), length);
1038 MPI_File_write_shared(newFile, buf2, length, MPI_CHAR, &statusNew);
1041 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
1042 length = outputString.length();
1043 char* buf = new char[length];
1044 memcpy(buf, outputString.c_str(), length);
1046 MPI_File_write_shared(tempFile, buf, length, MPI_CHAR, &statusTemp);
1050 delete candidateSeq;
1052 if((i+1) % 100 == 0){ cout << "Classifying sequence " << (i+1) << endl; }
1055 if(num % 100 != 0){ cout << "Classifying sequence " << (num) << endl; }
1060 catch(exception& e) {
1061 m->errorOut(e, "ClassifySeqsCommand", "driverMPI");
1066 //**********************************************************************************************************************
1067 int ClassifySeqsCommand::MPIReadNamesFile(string nameFilename){
1070 nameMap.clear(); //remove old names
1076 //char* inFileName = new char[nameFilename.length()];
1077 //memcpy(inFileName, nameFilename.c_str(), nameFilename.length());
1079 char inFileName[1024];
1080 strcpy(inFileName, nameFilename.c_str());
1082 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
1083 MPI_File_get_size(inMPI, &size);
1084 //delete inFileName;
1086 char* buffer = new char[size];
1087 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
1089 string tempBuf = buffer;
1090 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
1091 istringstream iss (tempBuf,istringstream::in);
1094 string firstCol, secondCol;
1096 iss >> firstCol >> secondCol; m->gobble(iss);
1098 vector<string> temp;
1099 m->splitAtComma(secondCol, temp);
1101 nameMap[firstCol] = temp;
1104 MPI_File_close(&inMPI);
1108 catch(exception& e) {
1109 m->errorOut(e, "ClassifySeqsCommand", "MPIReadNamesFile");
1114 /**************************************************************************************************/