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 m->mothurConvert(temp, kmerSize);
379 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
380 m->setProcessors(temp);
381 m->mothurConvert(temp, processors);
383 temp = validParameter.validFile(parameters, "save", false); if (temp == "not found"){ temp = "f"; }
384 save = m->isTrue(temp);
386 if (save) { //clear out old references
390 //this has to go after save so that if the user sets save=t and provides no reference we abort
391 templateFileName = validParameter.validFile(parameters, "reference", true);
392 if (templateFileName == "not found") {
393 //check for saved reference sequences
394 if (rdb->referenceSeqs.size() != 0) {
395 templateFileName = "saved";
397 m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required for the classify.seqs command.");
398 m->mothurOutEndLine();
401 }else if (templateFileName == "not open") { abort = true; }
402 else { if (save) { rdb->setSavedReference(templateFileName); } }
404 //this has to go after save so that if the user sets save=t and provides no reference we abort
405 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
406 if (taxonomyFileName == "not found") {
407 //check for saved reference sequences
408 if (rdb->wordGenusProb.size() != 0) {
409 taxonomyFileName = "saved";
411 m->mothurOut("[ERROR]: You don't have any saved taxonomy information and the taxonomy parameter is a required for the classify.seqs command.");
412 m->mothurOutEndLine();
415 }else if (taxonomyFileName == "not open") { abort = true; }
416 else { if (save) { rdb->setSavedTaxonomy(taxonomyFileName); } }
418 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
420 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
422 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
423 m->mothurConvert(temp, match);
425 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
426 m->mothurConvert(temp, misMatch);
428 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
429 m->mothurConvert(temp, gapOpen);
431 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
432 m->mothurConvert(temp, gapExtend);
434 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
435 m->mothurConvert(temp, numWanted);
437 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
438 m->mothurConvert(temp, cutoff);
440 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
441 probs = m->isTrue(temp);
443 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
444 m->mothurConvert(temp, iters);
448 if ((method == "bayesian") && (search != "kmer")) {
449 m->mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); m->mothurOutEndLine();
455 catch(exception& e) {
456 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
461 //**********************************************************************************************************************
462 ClassifySeqsCommand::~ClassifySeqsCommand(){
463 if (abort == false) {
464 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
467 //**********************************************************************************************************************
469 int ClassifySeqsCommand::execute(){
471 if (abort == true) { if (calledHelp) { return 0; } return 2; }
473 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters, rand()); }
474 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted, rand()); }
476 m->mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
477 m->mothurOutEndLine();
478 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters, rand());
481 if (m->control_pressed) { delete classify; return 0; }
483 for (int s = 0; s < fastaFileNames.size(); s++) {
485 m->mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
487 string baseTName = taxonomyFileName;
488 if (taxonomyFileName == "saved") {baseTName = rdb->getSavedTaxonomy(); }
490 string RippedTaxName = m->getRootName(m->getSimpleName(baseTName));
491 RippedTaxName = m->getExtension(RippedTaxName.substr(0, RippedTaxName.length()-1));
492 if (RippedTaxName[0] == '.') { RippedTaxName = RippedTaxName.substr(1, RippedTaxName.length()); }
493 RippedTaxName += ".";
495 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
496 string newTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "taxonomy";
497 string tempTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
498 string taxSummary = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "tax.summary";
500 if ((method == "knn") && (search == "distance")) {
501 string DistName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "match.dist";
502 classify->setDistName(DistName); outputNames.push_back(DistName); outputTypes["matchdist"].push_back(DistName);
505 outputNames.push_back(newTaxonomyFile); outputTypes["taxonomy"].push_back(newTaxonomyFile);
506 outputNames.push_back(taxSummary); outputTypes["taxsummary"].push_back(taxSummary);
508 int start = time(NULL);
509 int numFastaSeqs = 0;
510 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
513 int pid, numSeqsPerProcessor;
515 vector<unsigned long long> MPIPos;
518 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
519 MPI_Comm_size(MPI_COMM_WORLD, &processors);
522 MPI_File outMPINewTax;
523 MPI_File outMPITempTax;
525 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
526 int inMode=MPI_MODE_RDONLY;
528 char outNewTax[1024];
529 strcpy(outNewTax, newTaxonomyFile.c_str());
531 char outTempTax[1024];
532 strcpy(outTempTax, tempTaxonomyFile.c_str());
534 char inFileName[1024];
535 strcpy(inFileName, fastaFileNames[s].c_str());
537 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
538 MPI_File_open(MPI_COMM_WORLD, outNewTax, outMode, MPI_INFO_NULL, &outMPINewTax);
539 MPI_File_open(MPI_COMM_WORLD, outTempTax, outMode, MPI_INFO_NULL, &outMPITempTax);
541 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
543 if (pid == 0) { //you are the root process
545 MPIPos = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
547 //send file positions to all processes
548 for(int i = 1; i < processors; i++) {
549 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
550 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
553 //figure out how many sequences you have to align
554 numSeqsPerProcessor = numFastaSeqs / processors;
555 int startIndex = pid * numSeqsPerProcessor;
556 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
560 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
562 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; }
564 for (int i = 1; i < processors; i++) {
566 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
568 }else{ //you are a child process
569 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
570 MPIPos.resize(numFastaSeqs+1);
571 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
573 //figure out how many sequences you have to align
574 numSeqsPerProcessor = numFastaSeqs / processors;
575 int startIndex = pid * numSeqsPerProcessor;
576 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
580 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
582 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
585 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
589 MPI_File_close(&inMPI);
590 MPI_File_close(&outMPINewTax);
591 MPI_File_close(&outMPITempTax);
592 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
596 vector<unsigned long long> positions;
597 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
598 positions = m->divideFile(fastaFileNames[s], processors);
599 for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(new linePair(positions[i], positions[(i+1)])); }
601 if (processors == 1) {
602 lines.push_back(new linePair(0, 1000));
604 positions = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs);
606 //figure out how many sequences you have to process
607 int numSeqsPerProcessor = numFastaSeqs / processors;
608 for (int i = 0; i < processors; i++) {
609 int startIndex = i * numSeqsPerProcessor;
610 if(i == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; }
611 lines.push_back(new linePair(positions[startIndex], numSeqsPerProcessor));
616 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
618 numFastaSeqs = createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
622 m->mothurOutEndLine();
623 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
628 if (pid == 0) { //this part does not need to be paralellized
630 if(namefile != "") { m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush(); MPIReadNamesFile(namefileNames[s]); m->mothurOut(" Done."); m->mothurOutEndLine(); }
635 m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush();
637 nameMap.clear(); //remove old names
640 m->openInputFile(namefileNames[s], inNames);
642 string firstCol, secondCol;
643 while(!inNames.eof()) {
644 inNames >> firstCol >> secondCol; m->gobble(inNames);
647 m->splitAtComma(secondCol, temp);
649 nameMap[firstCol] = temp;
653 m->mothurOut(" Done."); m->mothurOutEndLine();
658 if (groupfile != "") { group = groupfileNames[s]; }
660 PhyloSummary taxaSum(baseTName, group);
662 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
664 if (namefile == "") { taxaSum.summarize(tempTaxonomyFile); }
667 m->openInputFile(tempTaxonomyFile, in);
669 //read in users taxonomy file and add sequences to tree
673 in >> name >> taxon; m->gobble(in);
675 itNames = nameMap.find(name);
677 if (itNames == nameMap.end()) {
678 m->mothurOut(name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
680 for (int i = 0; i < itNames->second.size(); i++) {
681 taxaSum.addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
683 itNames->second.clear();
684 nameMap.erase(itNames->first);
689 m->mothurRemove(tempTaxonomyFile);
691 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
695 m->openOutputFile(taxSummary, outTaxTree);
696 taxaSum.print(outTaxTree);
699 //output taxonomy with the unclassified bins added
701 m->openInputFile(newTaxonomyFile, inTax);
704 string unclass = newTaxonomyFile + ".unclass.temp";
705 m->openOutputFile(unclass, outTax);
707 //get maxLevel from phylotree so you know how many 'unclassified's to add
708 int maxLevel = taxaSum.getMaxLevel();
710 //read taxfile - this reading and rewriting is done to preserve the confidence scores.
712 while (!inTax.eof()) {
713 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; }
715 inTax >> name >> taxon; m->gobble(inTax);
717 string newTax = addUnclassifieds(taxon, maxLevel);
719 outTax << name << '\t' << newTax << endl;
724 m->mothurRemove(newTaxonomyFile);
725 rename(unclass.c_str(), newTaxonomyFile.c_str());
727 m->mothurOutEndLine();
728 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
734 m->mothurOutEndLine();
735 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
736 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
737 m->mothurOutEndLine();
740 //set taxonomy file as new current taxonomyfile
742 itTypes = outputTypes.find("taxonomy");
743 if (itTypes != outputTypes.end()) {
744 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
751 catch(exception& e) {
752 m->errorOut(e, "ClassifySeqsCommand", "execute");
757 /**************************************************************************************************/
758 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
760 string newTax, taxon;
763 //keep what you have counting the levels
764 while (tax.find_first_of(';') != -1) {
766 taxon = tax.substr(0,tax.find_first_of(';'))+';';
767 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
772 //add "unclassified" until you reach maxLevel
773 while (level < maxlevel) {
774 newTax += "unclassified;";
780 catch(exception& e) {
781 m->errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
786 /**************************************************************************************************/
788 int ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
794 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
797 //loop through and create all the processes you want
798 while (process != processors) {
802 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
805 num = driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
807 //pass numSeqs to parent
809 string tempFile = filename + toString(getpid()) + ".num.temp";
810 m->openOutputFile(tempFile, out);
816 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
817 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
822 //parent does its part
823 num = driver(lines[0], taxFileName, tempTaxFile, filename);
825 //force parent to wait until all the processes are done
826 for (int i=0;i<processIDS.size();i++) {
827 int temp = processIDS[i];
831 for (int i = 0; i < processIDS.size(); i++) {
833 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
834 m->openInputFile(tempFile, in);
835 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
836 in.close(); m->mothurRemove(m->getFullPathName(tempFile));
839 //////////////////////////////////////////////////////////////////////////////////////////////////////
840 //Windows version shared memory, so be careful when passing variables through the alignData struct.
841 //Above fork() will clone, so memory is separate, but that's not the case with windows,
842 //////////////////////////////////////////////////////////////////////////////////////////////////////
844 vector<classifyData*> pDataArray;
845 DWORD dwThreadIdArray[processors-1];
846 HANDLE hThreadArray[processors-1];
848 //Create processor worker threads.
849 for( int i=0; i<processors-1; i++ ){
850 // Allocate memory for thread data.
851 string extension = "";
852 if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
854 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);
855 pDataArray.push_back(tempclass);
857 //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
858 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
859 hThreadArray[i] = CreateThread(NULL, 0, MyClassThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
863 //parent does its part
864 num = driver(lines[processors-1], taxFileName + toString(processors-1) + ".temp", tempTaxFile + toString(processors-1) + ".temp", filename);
865 processIDS.push_back((processors-1));
867 //Wait until all threads have terminated.
868 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
870 //Close all thread handles and free memory allocations.
871 for(int i=0; i < pDataArray.size(); i++){
872 num += pDataArray[i]->count;
873 CloseHandle(hThreadArray[i]);
874 delete pDataArray[i];
879 for(int i=0;i<processIDS.size();i++){
880 appendTaxFiles((taxFileName + toString(processIDS[i]) + ".temp"), taxFileName);
881 appendTaxFiles((tempTaxFile + toString(processIDS[i]) + ".temp"), tempTaxFile);
882 m->mothurRemove((m->getFullPathName(taxFileName) + toString(processIDS[i]) + ".temp"));
883 m->mothurRemove((m->getFullPathName(tempTaxFile) + toString(processIDS[i]) + ".temp"));
889 catch(exception& e) {
890 m->errorOut(e, "ClassifySeqsCommand", "createProcesses");
894 /**************************************************************************************************/
896 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
901 m->openOutputFileAppend(filename, output);
902 m->openInputFile(temp, input);
904 while(char c = input.get()){
905 if(input.eof()) { break; }
906 else { output << c; }
912 catch(exception& e) {
913 m->errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
918 //**********************************************************************************************************************
920 int ClassifySeqsCommand::driver(linePair* filePos, string taxFName, string tempTFName, string filename){
923 m->openOutputFile(taxFName, outTax);
925 ofstream outTaxSimple;
926 m->openOutputFile(tempTFName, outTaxSimple);
929 m->openInputFile(filename, inFASTA);
933 inFASTA.seekg(filePos->start);
939 if (m->control_pressed) { return 0; }
941 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
943 if (candidateSeq->getName() != "") {
945 taxonomy = classify->getTaxonomy(candidateSeq);
947 if (m->control_pressed) { delete candidateSeq; return 0; }
949 if (taxonomy != "bad seq") {
950 //output confidence scores or not
952 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
954 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
957 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
963 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
964 unsigned long long pos = inFASTA.tellg();
965 if ((pos == -1) || (pos >= filePos->end)) { break; }
967 if (inFASTA.eof()) { break; }
971 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
975 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
979 outTaxSimple.close();
983 catch(exception& e) {
984 m->errorOut(e, "ClassifySeqsCommand", "driver");
988 //**********************************************************************************************************************
990 int ClassifySeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& newFile, MPI_File& tempFile, vector<unsigned long long>& MPIPos){
992 MPI_Status statusNew;
993 MPI_Status statusTemp;
997 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
1000 string outputString;
1002 for(int i=0;i<num;i++){
1004 if (m->control_pressed) { return 0; }
1006 //read next sequence
1007 int length = MPIPos[start+i+1] - MPIPos[start+i];
1008 char* buf4 = new char[length];
1009 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
1011 string tempBuf = buf4;
1012 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
1013 istringstream iss (tempBuf,istringstream::in);
1016 Sequence* candidateSeq = new Sequence(iss);
1018 if (candidateSeq->getName() != "") {
1019 taxonomy = classify->getTaxonomy(candidateSeq);
1021 if (taxonomy != "bad seq") {
1022 //output confidence scores or not
1024 outputString = candidateSeq->getName() + "\t" + taxonomy + "\n";
1026 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
1029 int length = outputString.length();
1030 char* buf2 = new char[length];
1031 memcpy(buf2, outputString.c_str(), length);
1033 MPI_File_write_shared(newFile, buf2, length, MPI_CHAR, &statusNew);
1036 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
1037 length = outputString.length();
1038 char* buf = new char[length];
1039 memcpy(buf, outputString.c_str(), length);
1041 MPI_File_write_shared(tempFile, buf, length, MPI_CHAR, &statusTemp);
1045 delete candidateSeq;
1047 if((i+1) % 100 == 0){ cout << "Classifying sequence " << (i+1) << endl; }
1050 if(num % 100 != 0){ cout << "Classifying sequence " << (num) << endl; }
1055 catch(exception& e) {
1056 m->errorOut(e, "ClassifySeqsCommand", "driverMPI");
1061 //**********************************************************************************************************************
1062 int ClassifySeqsCommand::MPIReadNamesFile(string nameFilename){
1065 nameMap.clear(); //remove old names
1071 //char* inFileName = new char[nameFilename.length()];
1072 //memcpy(inFileName, nameFilename.c_str(), nameFilename.length());
1074 char inFileName[1024];
1075 strcpy(inFileName, nameFilename.c_str());
1077 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
1078 MPI_File_get_size(inMPI, &size);
1079 //delete inFileName;
1081 char* buffer = new char[size];
1082 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
1084 string tempBuf = buffer;
1085 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
1086 istringstream iss (tempBuf,istringstream::in);
1089 string firstCol, secondCol;
1091 iss >> firstCol >> secondCol; m->gobble(iss);
1093 vector<string> temp;
1094 m->splitAtComma(secondCol, temp);
1096 nameMap[firstCol] = temp;
1099 MPI_File_close(&inMPI);
1103 catch(exception& e) {
1104 m->errorOut(e, "ClassifySeqsCommand", "MPIReadNamesFile");
1109 /**************************************************************************************************/