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"
17 //**********************************************************************************************************************
19 ClassifySeqsCommand::ClassifySeqsCommand(string option) {
23 //allow user to run help
24 if(option == "help") { help(); abort = true; }
28 //valid paramters for this command
29 string AlignArray[] = {"template","fasta","name","group","search","ksize","method","processors","taxonomy","match","mismatch","gapopen","gapextend","numwanted","cutoff","probs","iters", "outputdir","inputdir"};
30 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
32 OptionParser parser(option);
33 map<string, string> parameters = parser.getParameters();
35 ValidParameters validParameter("classify.seqs");
36 map<string, string>::iterator it;
38 //check to make sure all parameters are valid for command
39 for (it = parameters.begin(); it != parameters.end(); it++) {
40 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
43 //if the user changes the output directory command factory will send this info to us in the output parameter
44 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
46 //if the user changes the input directory command factory will send this info to us in the output parameter
47 string inputDir = validParameter.validFile(parameters, "inputdir", false);
48 if (inputDir == "not found"){ inputDir = ""; }
51 it = parameters.find("template");
52 //user has given a template file
53 if(it != parameters.end()){
54 path = hasPath(it->second);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { parameters["template"] = inputDir + it->second; }
59 it = parameters.find("taxonomy");
60 //user has given a template file
61 if(it != parameters.end()){
62 path = hasPath(it->second);
63 //if the user has not given a path then, add inputdir. else leave path alone.
64 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
67 it = parameters.find("group");
68 //user has given a template file
69 if(it != parameters.end()){
70 path = hasPath(it->second);
71 //if the user has not given a path then, add inputdir. else leave path alone.
72 if (path == "") { parameters["group"] = inputDir + it->second; }
76 //check for required parameters
77 templateFileName = validParameter.validFile(parameters, "template", true);
78 if (templateFileName == "not found") {
79 m->mothurOut("template is a required parameter for the classify.seqs command.");
80 m->mothurOutEndLine();
83 else if (templateFileName == "not open") { abort = true; }
86 fastaFileName = validParameter.validFile(parameters, "fasta", false);
87 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the classify.seqs command."); m->mothurOutEndLine(); abort = true; }
89 splitAtDash(fastaFileName, fastaFileNames);
91 //go through files and make sure they are good, if not, then disregard them
92 for (int i = 0; i < fastaFileNames.size(); i++) {
94 string path = hasPath(fastaFileNames[i]);
95 //if the user has not given a path then, add inputdir. else leave path alone.
96 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
102 ableToOpen = openInputFile(fastaFileNames[i], in, "noerror");
104 //if you can't open it, try default location
105 if (ableToOpen == 1) {
106 if (m->getDefaultPath() != "") { //default path is set
107 string tryPath = m->getDefaultPath() + getSimpleName(fastaFileNames[i]);
108 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
109 ableToOpen = openInputFile(tryPath, in, "noerror");
110 fastaFileNames[i] = tryPath;
115 if (ableToOpen == 1) {
116 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
117 //erase from file list
118 fastaFileNames.erase(fastaFileNames.begin()+i);
124 //make sure there is at least one valid file left
125 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
129 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
130 if (taxonomyFileName == "not found") {
131 m->mothurOut("taxonomy is a required parameter for the classify.seqs command.");
132 m->mothurOutEndLine();
135 else if (taxonomyFileName == "not open") { abort = true; }
138 namefile = validParameter.validFile(parameters, "name", false);
139 if (namefile == "not found") { namefile = ""; }
142 splitAtDash(namefile, namefileNames);
144 //go through files and make sure they are good, if not, then disregard them
145 for (int i = 0; i < namefileNames.size(); i++) {
146 if (inputDir != "") {
147 string path = hasPath(namefileNames[i]);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; }
154 ableToOpen = openInputFile(namefileNames[i], in, "noerror");
156 //if you can't open it, try default location
157 if (ableToOpen == 1) {
158 if (m->getDefaultPath() != "") { //default path is set
159 string tryPath = m->getDefaultPath() + getSimpleName(namefileNames[i]);
160 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
161 ableToOpen = openInputFile(tryPath, in, "noerror");
162 namefileNames[i] = tryPath;
167 if (ableToOpen == 1) {
168 m->mothurOut("Unable to open " + namefileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); abort = true;
169 //erase from file list
170 namefileNames.erase(namefileNames.begin()+i);
177 if (namefile != "") {
178 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(); }
181 groupfile = validParameter.validFile(parameters, "group", false);
182 if (groupfile == "not found") { groupfile = ""; }
184 splitAtDash(groupfile, groupfileNames);
186 //go through files and make sure they are good, if not, then disregard them
187 for (int i = 0; i < groupfileNames.size(); i++) {
188 if (inputDir != "") {
189 string path = hasPath(groupfileNames[i]);
190 //if the user has not given a path then, add inputdir. else leave path alone.
191 if (path == "") { groupfileNames[i] = inputDir + groupfileNames[i]; }
196 ableToOpen = openInputFile(groupfileNames[i], in, "noerror");
198 //if you can't open it, try default location
199 if (ableToOpen == 1) {
200 if (m->getDefaultPath() != "") { //default path is set
201 string tryPath = m->getDefaultPath() + getSimpleName(groupfileNames[i]);
202 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
203 ableToOpen = openInputFile(tryPath, in, "noerror");
204 groupfileNames[i] = tryPath;
209 if (ableToOpen == 1) {
210 m->mothurOut("Unable to open " + groupfileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); groupfileNames[i] = "";
211 //erase from file list
212 groupfileNames.erase(groupfileNames.begin()+i);
218 if (groupfile != "") {
219 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(); }
221 for (int i = 0; i < fastaFileNames.size(); i++) { groupfileNames.push_back(""); }
224 //check for optional parameter and set defaults
225 // ...at some point should added some additional type checking...
227 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
228 convert(temp, kmerSize);
230 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
231 convert(temp, processors);
233 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
235 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
237 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
238 convert(temp, match);
240 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
241 convert(temp, misMatch);
243 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
244 convert(temp, gapOpen);
246 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
247 convert(temp, gapExtend);
249 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
250 convert(temp, numWanted);
252 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
253 convert(temp, cutoff);
255 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
256 probs = isTrue(temp);
258 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
259 convert(temp, iters);
263 if ((method == "bayesian") && (search != "kmer")) {
264 m->mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); m->mothurOutEndLine();
270 catch(exception& e) {
271 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
276 //**********************************************************************************************************************
278 ClassifySeqsCommand::~ClassifySeqsCommand(){
280 if (abort == false) {
281 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
285 //**********************************************************************************************************************
287 void ClassifySeqsCommand::help(){
289 m->mothurOut("The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n");
290 m->mothurOut("The classify.seqs command parameters are template, fasta, name, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend, numwanted and probs.\n");
291 m->mothurOut("The template, 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");
292 m->mothurOut("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");
293 m->mothurOut("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");
294 m->mothurOut("The group parameter allows you add a group file so you can have the summary totals broken up by group.\n");
295 m->mothurOut("The method parameter allows you to specify classification method to use. Your options are: bayesian and knn. The default is bayesian.\n");
296 m->mothurOut("The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n");
297 m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
299 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
301 m->mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
302 m->mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
303 m->mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n");
304 m->mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n");
305 m->mothurOut("The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n");
306 m->mothurOut("The cutoff parameter allows you to specify a bootstrap confidence threshold for your taxonomy. The default is 0.\n");
307 m->mothurOut("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");
308 m->mothurOut("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");
309 m->mothurOut("The classify.seqs command should be in the following format: \n");
310 m->mothurOut("classify.seqs(template=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n");
311 m->mothurOut("Example classify.seqs(fasta=amazon.fasta, template=core.filtered, method=knn, search=gotoh, ksize=8, processors=2)\n");
312 m->mothurOut("The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n");
313 m->mothurOut("The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n");
314 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
316 catch(exception& e) {
317 m->errorOut(e, "ClassifySeqsCommand", "help");
323 //**********************************************************************************************************************
325 int ClassifySeqsCommand::execute(){
327 if (abort == true) { return 0; }
329 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters); }
330 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted); }
332 m->mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
333 m->mothurOutEndLine();
334 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters);
337 if (m->control_pressed) { delete classify; return 0; }
339 vector<string> outputNames;
341 for (int s = 0; s < fastaFileNames.size(); s++) {
343 m->mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
345 string RippedTaxName = getRootName(getSimpleName(taxonomyFileName));
346 RippedTaxName = getExtension(RippedTaxName.substr(0, RippedTaxName.length()-1));
347 if (RippedTaxName[0] == '.') { RippedTaxName = RippedTaxName.substr(1, RippedTaxName.length()); }
348 RippedTaxName += ".";
350 if (outputDir == "") { outputDir += hasPath(fastaFileNames[s]); }
351 string newTaxonomyFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + RippedTaxName + "taxonomy";
352 string tempTaxonomyFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
353 string taxSummary = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + RippedTaxName + "tax.summary";
355 if ((method == "knn") && (search == "distance")) {
356 string DistName = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "match.dist";
357 classify->setDistName(DistName); outputNames.push_back(DistName);
360 outputNames.push_back(newTaxonomyFile);
361 outputNames.push_back(taxSummary);
363 int start = time(NULL);
364 int numFastaSeqs = 0;
365 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
368 int pid, end, numSeqsPerProcessor;
370 vector<unsigned long int> MPIPos;
373 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
374 MPI_Comm_size(MPI_COMM_WORLD, &processors);
377 MPI_File outMPINewTax;
378 MPI_File outMPITempTax;
380 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
381 int inMode=MPI_MODE_RDONLY;
383 //char* outNewTax = new char[newTaxonomyFile.length()];
384 //memcpy(outNewTax, newTaxonomyFile.c_str(), newTaxonomyFile.length());
386 char outNewTax[1024];
387 strcpy(outNewTax, newTaxonomyFile.c_str());
389 //char* outTempTax = new char[tempTaxonomyFile.length()];
390 //memcpy(outTempTax, tempTaxonomyFile.c_str(), tempTaxonomyFile.length());
392 char outTempTax[1024];
393 strcpy(outTempTax, tempTaxonomyFile.c_str());
395 //char* inFileName = new char[fastaFileNames[s].length()];
396 //memcpy(inFileName, fastaFileNames[s].c_str(), fastaFileNames[s].length());
398 char inFileName[1024];
399 strcpy(inFileName, fastaFileNames[s].c_str());
401 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
402 MPI_File_open(MPI_COMM_WORLD, outNewTax, outMode, MPI_INFO_NULL, &outMPINewTax);
403 MPI_File_open(MPI_COMM_WORLD, outTempTax, outMode, MPI_INFO_NULL, &outMPITempTax);
409 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
411 if (pid == 0) { //you are the root process
413 MPIPos = setFilePosFasta(fastaFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
415 //send file positions to all processes
416 for(int i = 1; i < processors; i++) {
417 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
418 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
421 //figure out how many sequences you have to align
422 numSeqsPerProcessor = numFastaSeqs / processors;
423 int startIndex = pid * numSeqsPerProcessor;
424 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
428 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
430 if (m->control_pressed) { 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; }
432 for (int i = 1; i < processors; i++) {
434 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
436 }else{ //you are a child process
437 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
438 MPIPos.resize(numFastaSeqs+1);
439 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
441 //figure out how many sequences you have to align
442 numSeqsPerProcessor = numFastaSeqs / processors;
443 int startIndex = pid * numSeqsPerProcessor;
444 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
448 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, MPIPos);
450 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPITempTax); delete classify; return 0; }
453 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
457 MPI_File_close(&inMPI);
458 MPI_File_close(&outMPINewTax);
459 MPI_File_close(&outMPITempTax);
460 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
464 vector<unsigned long int> positions = divideFile(fastaFileNames[s], processors);
466 for (int i = 0; i < (positions.size()-1); i++) {
467 lines.push_back(new linePair(positions[i], positions[(i+1)]));
470 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
472 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
475 processIDS.resize(0);
477 numFastaSeqs = createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
479 rename((newTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), newTaxonomyFile.c_str());
480 rename((tempTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), tempTaxonomyFile.c_str());
482 for(int i=1;i<processors;i++){
483 appendTaxFiles((newTaxonomyFile + toString(processIDS[i]) + ".temp"), newTaxonomyFile);
484 appendTaxFiles((tempTaxonomyFile + toString(processIDS[i]) + ".temp"), tempTaxonomyFile);
485 remove((newTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
486 remove((tempTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
491 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
495 m->mothurOutEndLine();
496 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
501 if (pid == 0) { //this part does not need to be paralellized
503 if(namefile != "") { m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush(); MPIReadNamesFile(namefileNames[s]); m->mothurOut(" Done."); m->mothurOutEndLine(); }
508 m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush();
510 nameMap.clear(); //remove old names
513 openInputFile(namefileNames[s], inNames);
515 string firstCol, secondCol;
516 while(!inNames.eof()) {
517 inNames >> firstCol >> secondCol; gobble(inNames);
520 splitAtComma(secondCol, temp);
522 nameMap[firstCol] = temp;
526 m->mothurOut(" Done."); m->mothurOutEndLine();
531 if (groupfile != "") { group = groupfileNames[s]; }
533 PhyloSummary taxaSum(taxonomyFileName, group);
535 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
537 if (namefile == "") { taxaSum.summarize(tempTaxonomyFile); }
540 openInputFile(tempTaxonomyFile, in);
542 //read in users taxonomy file and add sequences to tree
546 in >> name >> taxon; gobble(in);
548 itNames = nameMap.find(name);
550 if (itNames == nameMap.end()) {
551 m->mothurOut(name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
553 for (int i = 0; i < itNames->second.size(); i++) {
554 taxaSum.addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
556 itNames->second.clear();
557 nameMap.erase(itNames->first);
562 remove(tempTaxonomyFile.c_str());
564 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete classify; return 0; }
568 openOutputFile(taxSummary, outTaxTree);
569 taxaSum.print(outTaxTree);
572 //output taxonomy with the unclassified bins added
574 openInputFile(newTaxonomyFile, inTax);
577 string unclass = newTaxonomyFile + ".unclass.temp";
578 openOutputFile(unclass, outTax);
580 //get maxLevel from phylotree so you know how many 'unclassified's to add
581 int maxLevel = taxaSum.getMaxLevel();
583 //read taxfile - this reading and rewriting is done to preserve the confidence scores.
585 while (!inTax.eof()) {
586 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } remove(unclass.c_str()); delete classify; return 0; }
588 inTax >> name >> taxon; gobble(inTax);
590 string newTax = addUnclassifieds(taxon, maxLevel);
592 outTax << name << '\t' << newTax << endl;
597 remove(newTaxonomyFile.c_str());
598 rename(unclass.c_str(), newTaxonomyFile.c_str());
600 m->mothurOutEndLine();
601 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
607 m->mothurOutEndLine();
608 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
609 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
610 m->mothurOutEndLine();
616 catch(exception& e) {
617 m->errorOut(e, "ClassifySeqsCommand", "execute");
622 /**************************************************************************************************/
623 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
625 string newTax, taxon;
628 //keep what you have counting the levels
629 while (tax.find_first_of(';') != -1) {
631 taxon = tax.substr(0,tax.find_first_of(';'))+';';
632 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
637 //add "unclassified" until you reach maxLevel
638 while (level < maxlevel) {
639 newTax += "unclassified;";
645 catch(exception& e) {
646 m->errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
651 /**************************************************************************************************/
653 int ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
655 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
659 //loop through and create all the processes you want
660 while (process != processors) {
664 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
667 num = driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
669 //pass numSeqs to parent
671 string tempFile = toString(getpid()) + ".temp";
672 openOutputFile(tempFile, out);
677 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
680 //force parent to wait until all the processes are done
681 for (int i=0;i<processors;i++) {
682 int temp = processIDS[i];
686 for (int i = 0; i < processIDS.size(); i++) {
688 string tempFile = toString(processIDS[i]) + ".temp";
689 openInputFile(tempFile, in);
690 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
691 in.close(); remove(tempFile.c_str());
697 catch(exception& e) {
698 m->errorOut(e, "ClassifySeqsCommand", "createProcesses");
702 /**************************************************************************************************/
704 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
709 openOutputFileAppend(filename, output);
710 openInputFile(temp, input);
712 while(char c = input.get()){
713 if(input.eof()) { break; }
714 else { output << c; }
720 catch(exception& e) {
721 m->errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
726 //**********************************************************************************************************************
728 int ClassifySeqsCommand::driver(linePair* filePos, string taxFName, string tempTFName, string filename){
731 openOutputFile(taxFName, outTax);
733 ofstream outTaxSimple;
734 openOutputFile(tempTFName, outTaxSimple);
737 openInputFile(filename, inFASTA);
741 inFASTA.seekg(filePos->start);
747 if (m->control_pressed) { return 0; }
749 Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
751 if (candidateSeq->getName() != "") {
752 taxonomy = classify->getTaxonomy(candidateSeq);
754 if (m->control_pressed) { delete candidateSeq; return 0; }
756 if (taxonomy != "bad seq") {
757 //output confidence scores or not
759 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
761 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
764 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
770 unsigned long int pos = inFASTA.tellg();
771 if ((pos == -1) || (pos >= filePos->end)) { break; }
774 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
777 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
781 outTaxSimple.close();
785 catch(exception& e) {
786 m->errorOut(e, "ClassifySeqsCommand", "driver");
790 //**********************************************************************************************************************
792 int ClassifySeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& newFile, MPI_File& tempFile, vector<unsigned long int>& MPIPos){
794 MPI_Status statusNew;
795 MPI_Status statusTemp;
799 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
804 for(int i=0;i<num;i++){
806 if (m->control_pressed) { return 0; }
809 int length = MPIPos[start+i+1] - MPIPos[start+i];
810 char* buf4 = new char[length];
811 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
813 string tempBuf = buf4;
814 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
815 istringstream iss (tempBuf,istringstream::in);
818 Sequence* candidateSeq = new Sequence(iss);
820 if (candidateSeq->getName() != "") {
821 taxonomy = classify->getTaxonomy(candidateSeq);
823 if (taxonomy != "bad seq") {
824 //output confidence scores or not
826 outputString = candidateSeq->getName() + "\t" + taxonomy + "\n";
828 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
831 int length = outputString.length();
832 char* buf2 = new char[length];
833 memcpy(buf2, outputString.c_str(), length);
835 MPI_File_write_shared(newFile, buf2, length, MPI_CHAR, &statusNew);
838 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
839 length = outputString.length();
840 char* buf = new char[length];
841 memcpy(buf, outputString.c_str(), length);
843 MPI_File_write_shared(tempFile, buf, length, MPI_CHAR, &statusTemp);
849 if((i+1) % 100 == 0){ cout << "Classifying sequence " << (i+1) << endl; }
852 if(num % 100 != 0){ cout << "Classifying sequence " << (num) << endl; }
857 catch(exception& e) {
858 m->errorOut(e, "ClassifySeqsCommand", "driverMPI");
863 //**********************************************************************************************************************
864 int ClassifySeqsCommand::MPIReadNamesFile(string nameFilename){
867 nameMap.clear(); //remove old names
873 //char* inFileName = new char[nameFilename.length()];
874 //memcpy(inFileName, nameFilename.c_str(), nameFilename.length());
876 char inFileName[1024];
877 strcpy(inFileName, nameFilename.c_str());
879 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
880 MPI_File_get_size(inMPI, &size);
883 char* buffer = new char[size];
884 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
886 string tempBuf = buffer;
887 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
888 istringstream iss (tempBuf,istringstream::in);
891 string firstCol, secondCol;
893 iss >> firstCol >> secondCol; gobble(iss);
896 splitAtComma(secondCol, temp);
898 nameMap[firstCol] = temp;
901 MPI_File_close(&inMPI);
905 catch(exception& e) {
906 m->errorOut(e, "ClassifySeqsCommand", "MPIReadNamesFile");
911 /**************************************************************************************************/