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; }
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 //check for required parameters
164 templateFileName = validParameter.validFile(parameters, "reference", true);
165 if (templateFileName == "not found") {
166 m->mothurOut("reference is a required parameter for the classify.seqs command.");
167 m->mothurOutEndLine();
170 else if (templateFileName == "not open") { abort = true; }
173 fastaFileName = validParameter.validFile(parameters, "fasta", false);
174 if (fastaFileName == "not found") {
175 //if there is a current fasta file, use it
176 string filename = m->getFastaFile();
177 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
178 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
181 m->splitAtDash(fastaFileName, fastaFileNames);
183 //go through files and make sure they are good, if not, then disregard them
184 for (int i = 0; i < fastaFileNames.size(); i++) {
185 if (inputDir != "") {
186 string path = m->hasPath(fastaFileNames[i]);
187 //if the user has not given a path then, add inputdir. else leave path alone.
188 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
194 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
196 //if you can't open it, try default location
197 if (ableToOpen == 1) {
198 if (m->getDefaultPath() != "") { //default path is set
199 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
200 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
202 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
204 fastaFileNames[i] = tryPath;
208 if (ableToOpen == 1) {
209 if (m->getOutputDir() != "") { //default path is set
210 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
211 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
213 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
215 fastaFileNames[i] = tryPath;
221 if (ableToOpen == 1) {
222 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
223 //erase from file list
224 fastaFileNames.erase(fastaFileNames.begin()+i);
230 //make sure there is at least one valid file left
231 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
235 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
236 if (taxonomyFileName == "not found") {
237 m->mothurOut("taxonomy is a required parameter for the classify.seqs command.");
238 m->mothurOutEndLine();
241 else if (taxonomyFileName == "not open") { abort = true; }
244 namefile = validParameter.validFile(parameters, "name", false);
245 if (namefile == "not found") { namefile = ""; }
248 m->splitAtDash(namefile, namefileNames);
250 //go through files and make sure they are good, if not, then disregard them
251 for (int i = 0; i < namefileNames.size(); i++) {
252 if (inputDir != "") {
253 string path = m->hasPath(namefileNames[i]);
254 //if the user has not given a path then, add inputdir. else leave path alone.
255 if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; }
260 ableToOpen = m->openInputFile(namefileNames[i], in, "noerror");
262 //if you can't open it, try default location
263 if (ableToOpen == 1) {
264 if (m->getDefaultPath() != "") { //default path is set
265 string tryPath = m->getDefaultPath() + m->getSimpleName(namefileNames[i]);
266 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
268 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
270 namefileNames[i] = tryPath;
274 if (ableToOpen == 1) {
275 if (m->getOutputDir() != "") { //default path is set
276 string tryPath = m->getOutputDir() + m->getSimpleName(namefileNames[i]);
277 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
279 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
281 namefileNames[i] = tryPath;
286 if (ableToOpen == 1) {
287 m->mothurOut("Unable to open " + namefileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); abort = true;
288 //erase from file list
289 namefileNames.erase(namefileNames.begin()+i);
296 if (namefile != "") {
297 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(); }
300 groupfile = validParameter.validFile(parameters, "group", false);
301 if (groupfile == "not found") { groupfile = ""; }
303 m->splitAtDash(groupfile, groupfileNames);
305 //go through files and make sure they are good, if not, then disregard them
306 for (int i = 0; i < groupfileNames.size(); i++) {
307 if (inputDir != "") {
308 string path = m->hasPath(groupfileNames[i]);
309 //if the user has not given a path then, add inputdir. else leave path alone.
310 if (path == "") { groupfileNames[i] = inputDir + groupfileNames[i]; }
315 ableToOpen = m->openInputFile(groupfileNames[i], in, "noerror");
317 //if you can't open it, try default location
318 if (ableToOpen == 1) {
319 if (m->getDefaultPath() != "") { //default path is set
320 string tryPath = m->getDefaultPath() + m->getSimpleName(groupfileNames[i]);
321 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
323 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
325 groupfileNames[i] = tryPath;
329 if (ableToOpen == 1) {
330 if (m->getOutputDir() != "") { //default path is set
331 string tryPath = m->getOutputDir() + m->getSimpleName(groupfileNames[i]);
332 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
334 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
336 groupfileNames[i] = tryPath;
342 if (ableToOpen == 1) {
343 m->mothurOut("Unable to open " + groupfileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); groupfileNames[i] = "";
344 //erase from file list
345 groupfileNames.erase(groupfileNames.begin()+i);
351 if (groupfile != "") {
352 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(); }
354 for (int i = 0; i < fastaFileNames.size(); i++) { groupfileNames.push_back(""); }
357 //check for optional parameter and set defaults
358 // ...at some point should added some additional type checking...
360 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
361 convert(temp, kmerSize);
363 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
364 m->setProcessors(temp);
365 convert(temp, processors);
367 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
369 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
371 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
372 convert(temp, match);
374 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
375 convert(temp, misMatch);
377 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
378 convert(temp, gapOpen);
380 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
381 convert(temp, gapExtend);
383 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
384 convert(temp, numWanted);
386 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
387 convert(temp, cutoff);
389 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
390 probs = m->isTrue(temp);
392 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
393 convert(temp, iters);
397 if ((method == "bayesian") && (search != "kmer")) {
398 m->mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); m->mothurOutEndLine();
404 catch(exception& e) {
405 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
410 //**********************************************************************************************************************
411 ClassifySeqsCommand::~ClassifySeqsCommand(){
412 if (abort == false) {
413 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
416 //**********************************************************************************************************************
418 int ClassifySeqsCommand::execute(){
420 if (abort == true) { if (calledHelp) { return 0; } return 2; }
422 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters); }
423 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted); }
425 m->mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
426 m->mothurOutEndLine();
427 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters);
430 if (m->control_pressed) { delete classify; return 0; }
433 for (int s = 0; s < fastaFileNames.size(); s++) {
435 m->mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
437 string RippedTaxName = m->getRootName(m->getSimpleName(taxonomyFileName));
438 RippedTaxName = m->getExtension(RippedTaxName.substr(0, RippedTaxName.length()-1));
439 if (RippedTaxName[0] == '.') { RippedTaxName = RippedTaxName.substr(1, RippedTaxName.length()); }
440 RippedTaxName += ".";
442 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
443 string newTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "taxonomy";
444 string tempTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
445 string taxSummary = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + "tax.summary";
447 if ((method == "knn") && (search == "distance")) {
448 string DistName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "match.dist";
449 classify->setDistName(DistName); outputNames.push_back(DistName); outputTypes["matchdist"].push_back(DistName);
452 outputNames.push_back(newTaxonomyFile); outputTypes["taxonomy"].push_back(newTaxonomyFile);
453 outputNames.push_back(taxSummary); outputTypes["taxsummary"].push_back(taxSummary);
455 int start = time(NULL);
456 int numFastaSeqs = 0;
457 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
460 int pid, numSeqsPerProcessor;
462 vector<unsigned long int> MPIPos;
465 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
466 MPI_Comm_size(MPI_COMM_WORLD, &processors);
469 MPI_File outMPINewTax;
470 MPI_File outMPITempTax;
472 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
473 int inMode=MPI_MODE_RDONLY;
475 char outNewTax[1024];
476 strcpy(outNewTax, newTaxonomyFile.c_str());
478 char outTempTax[1024];
479 strcpy(outTempTax, tempTaxonomyFile.c_str());
481 char inFileName[1024];
482 strcpy(inFileName, fastaFileNames[s].c_str());
484 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
485 MPI_File_open(MPI_COMM_WORLD, outNewTax, outMode, MPI_INFO_NULL, &outMPINewTax);
486 MPI_File_open(MPI_COMM_WORLD, outTempTax, outMode, MPI_INFO_NULL, &outMPITempTax);
488 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
490 if (pid == 0) { //you are the root process
492 MPIPos = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
494 //send file positions to all processes
495 for(int i = 1; i < processors; i++) {
496 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
497 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
500 //figure out how many sequences you have to align
501 numSeqsPerProcessor = numFastaSeqs / processors;
502 int startIndex = pid * numSeqsPerProcessor;
503 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
507 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
509 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; }
511 for (int i = 1; i < processors; i++) {
513 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
515 }else{ //you are a child process
516 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
517 MPIPos.resize(numFastaSeqs+1);
518 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
520 //figure out how many sequences you have to align
521 numSeqsPerProcessor = numFastaSeqs / processors;
522 int startIndex = pid * numSeqsPerProcessor;
523 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
527 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
529 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
532 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
536 MPI_File_close(&inMPI);
537 MPI_File_close(&outMPINewTax);
538 MPI_File_close(&outMPITempTax);
539 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
543 vector<unsigned long int> positions = m->divideFile(fastaFileNames[s], processors);
545 for (int i = 0; i < (positions.size()-1); i++) {
546 lines.push_back(new linePair(positions[i], positions[(i+1)]));
549 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
551 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
554 processIDS.resize(0);
556 numFastaSeqs = createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
560 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
564 m->mothurOutEndLine();
565 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
570 if (pid == 0) { //this part does not need to be paralellized
572 if(namefile != "") { m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush(); MPIReadNamesFile(namefileNames[s]); m->mothurOut(" Done."); m->mothurOutEndLine(); }
577 m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush();
579 nameMap.clear(); //remove old names
582 m->openInputFile(namefileNames[s], inNames);
584 string firstCol, secondCol;
585 while(!inNames.eof()) {
586 inNames >> firstCol >> secondCol; m->gobble(inNames);
589 m->splitAtComma(secondCol, temp);
591 nameMap[firstCol] = temp;
595 m->mothurOut(" Done."); m->mothurOutEndLine();
600 if (groupfile != "") { group = groupfileNames[s]; }
602 PhyloSummary taxaSum(taxonomyFileName, group);
604 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
606 if (namefile == "") { taxaSum.summarize(tempTaxonomyFile); }
609 m->openInputFile(tempTaxonomyFile, in);
611 //read in users taxonomy file and add sequences to tree
615 in >> name >> taxon; m->gobble(in);
617 itNames = nameMap.find(name);
619 if (itNames == nameMap.end()) {
620 m->mothurOut(name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
622 for (int i = 0; i < itNames->second.size(); i++) {
623 taxaSum.addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
625 itNames->second.clear();
626 nameMap.erase(itNames->first);
631 remove(tempTaxonomyFile.c_str());
633 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
637 m->openOutputFile(taxSummary, outTaxTree);
638 taxaSum.print(outTaxTree);
641 //output taxonomy with the unclassified bins added
643 m->openInputFile(newTaxonomyFile, inTax);
646 string unclass = newTaxonomyFile + ".unclass.temp";
647 m->openOutputFile(unclass, outTax);
649 //get maxLevel from phylotree so you know how many 'unclassified's to add
650 int maxLevel = taxaSum.getMaxLevel();
652 //read taxfile - this reading and rewriting is done to preserve the confidence scores.
654 while (!inTax.eof()) {
655 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; }
657 inTax >> name >> taxon; m->gobble(inTax);
659 string newTax = addUnclassifieds(taxon, maxLevel);
661 outTax << name << '\t' << newTax << endl;
666 remove(newTaxonomyFile.c_str());
667 rename(unclass.c_str(), newTaxonomyFile.c_str());
669 m->mothurOutEndLine();
670 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
676 m->mothurOutEndLine();
677 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
678 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
679 m->mothurOutEndLine();
682 //set taxonomy file as new current taxonomyfile
684 itTypes = outputTypes.find("taxonomy");
685 if (itTypes != outputTypes.end()) {
686 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
692 catch(exception& e) {
693 m->errorOut(e, "ClassifySeqsCommand", "execute");
698 /**************************************************************************************************/
699 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
701 string newTax, taxon;
704 //keep what you have counting the levels
705 while (tax.find_first_of(';') != -1) {
707 taxon = tax.substr(0,tax.find_first_of(';'))+';';
708 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
713 //add "unclassified" until you reach maxLevel
714 while (level < maxlevel) {
715 newTax += "unclassified;";
721 catch(exception& e) {
722 m->errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
727 /**************************************************************************************************/
729 int ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
731 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
735 //loop through and create all the processes you want
736 while (process != processors) {
740 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
743 num = driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
745 //pass numSeqs to parent
747 string tempFile = filename + toString(getpid()) + ".num.temp";
748 m->openOutputFile(tempFile, out);
754 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
755 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
760 //parent does its part
761 num = driver(lines[0], taxFileName, tempTaxFile, filename);
763 //force parent to wait until all the processes are done
764 for (int i=0;i<processIDS.size();i++) {
765 int temp = processIDS[i];
769 for (int i = 0; i < processIDS.size(); i++) {
771 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
772 m->openInputFile(tempFile, in);
773 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
774 in.close(); remove(tempFile.c_str());
777 for(int i=0;i<processIDS.size();i++){
778 appendTaxFiles((taxFileName + toString(processIDS[i]) + ".temp"), taxFileName);
779 appendTaxFiles((tempTaxFile + toString(processIDS[i]) + ".temp"), tempTaxFile);
780 remove((taxFileName + toString(processIDS[i]) + ".temp").c_str());
781 remove((tempTaxFile + toString(processIDS[i]) + ".temp").c_str());
787 catch(exception& e) {
788 m->errorOut(e, "ClassifySeqsCommand", "createProcesses");
792 /**************************************************************************************************/
794 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
799 m->openOutputFileAppend(filename, output);
800 m->openInputFile(temp, input);
802 while(char c = input.get()){
803 if(input.eof()) { break; }
804 else { output << c; }
810 catch(exception& e) {
811 m->errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
816 //**********************************************************************************************************************
818 int ClassifySeqsCommand::driver(linePair* filePos, string taxFName, string tempTFName, string filename){
821 m->openOutputFile(taxFName, outTax);
823 ofstream outTaxSimple;
824 m->openOutputFile(tempTFName, outTaxSimple);
827 m->openInputFile(filename, inFASTA);
831 inFASTA.seekg(filePos->start);
837 if (m->control_pressed) { return 0; }
839 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
841 if (candidateSeq->getName() != "") {
843 taxonomy = classify->getTaxonomy(candidateSeq);
845 if (m->control_pressed) { delete candidateSeq; return 0; }
847 if (taxonomy != "bad seq") {
848 //output confidence scores or not
850 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
852 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
855 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
861 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
862 unsigned long int pos = inFASTA.tellg();
863 if ((pos == -1) || (pos >= filePos->end)) { break; }
865 if (inFASTA.eof()) { break; }
869 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
873 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
877 outTaxSimple.close();
881 catch(exception& e) {
882 m->errorOut(e, "ClassifySeqsCommand", "driver");
886 //**********************************************************************************************************************
888 int ClassifySeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& newFile, MPI_File& tempFile, vector<unsigned long int>& MPIPos){
890 MPI_Status statusNew;
891 MPI_Status statusTemp;
895 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
900 for(int i=0;i<num;i++){
902 if (m->control_pressed) { return 0; }
905 int length = MPIPos[start+i+1] - MPIPos[start+i];
906 char* buf4 = new char[length];
907 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
909 string tempBuf = buf4;
910 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
911 istringstream iss (tempBuf,istringstream::in);
914 Sequence* candidateSeq = new Sequence(iss);
916 if (candidateSeq->getName() != "") {
917 taxonomy = classify->getTaxonomy(candidateSeq);
919 if (taxonomy != "bad seq") {
920 //output confidence scores or not
922 outputString = candidateSeq->getName() + "\t" + taxonomy + "\n";
924 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
927 int length = outputString.length();
928 char* buf2 = new char[length];
929 memcpy(buf2, outputString.c_str(), length);
931 MPI_File_write_shared(newFile, buf2, length, MPI_CHAR, &statusNew);
934 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
935 length = outputString.length();
936 char* buf = new char[length];
937 memcpy(buf, outputString.c_str(), length);
939 MPI_File_write_shared(tempFile, buf, length, MPI_CHAR, &statusTemp);
945 if((i+1) % 100 == 0){ cout << "Classifying sequence " << (i+1) << endl; }
948 if(num % 100 != 0){ cout << "Classifying sequence " << (num) << endl; }
953 catch(exception& e) {
954 m->errorOut(e, "ClassifySeqsCommand", "driverMPI");
959 //**********************************************************************************************************************
960 int ClassifySeqsCommand::MPIReadNamesFile(string nameFilename){
963 nameMap.clear(); //remove old names
969 //char* inFileName = new char[nameFilename.length()];
970 //memcpy(inFileName, nameFilename.c_str(), nameFilename.length());
972 char inFileName[1024];
973 strcpy(inFileName, nameFilename.c_str());
975 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
976 MPI_File_get_size(inMPI, &size);
979 char* buffer = new char[size];
980 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
982 string tempBuf = buffer;
983 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
984 istringstream iss (tempBuf,istringstream::in);
987 string firstCol, secondCol;
989 iss >> firstCol >> secondCol; m->gobble(iss);
992 m->splitAtComma(secondCol, temp);
994 nameMap[firstCol] = temp;
997 MPI_File_close(&inMPI);
1001 catch(exception& e) {
1002 m->errorOut(e, "ClassifySeqsCommand", "MPIReadNamesFile");
1007 /**************************************************************************************************/