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", "", "", "NameCount", "none", "none",false,false); parameters.push_back(pname);
21 CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none",false,false); parameters.push_back(pcount);
22 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none",false,false); parameters.push_back(pgroup);
24 CommandParameter psearch("search", "Multiple", "kmer-blast-suffix-distance-align", "kmer", "", "", "",false,false); parameters.push_back(psearch);
25 CommandParameter pksize("ksize", "Number", "", "8", "", "", "",false,false); parameters.push_back(pksize);
26 CommandParameter pmethod("method", "Multiple", "wang-knn-zap", "wang", "", "", "",false,false); parameters.push_back(pmethod);
27 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
28 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pmatch);
29 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pmismatch);
30 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "",false,false); parameters.push_back(pgapopen);
31 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pgapextend);
32 //CommandParameter pflip("flip", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pflip);
33 CommandParameter pcutoff("cutoff", "Number", "", "0", "", "", "",false,true); parameters.push_back(pcutoff);
34 CommandParameter pprobs("probs", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pprobs);
35 CommandParameter piters("iters", "Number", "", "100", "", "", "",false,true); parameters.push_back(piters);
36 CommandParameter psave("save", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psave);
37 CommandParameter pshortcuts("shortcuts", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshortcuts);
38 CommandParameter pnumwanted("numwanted", "Number", "", "10", "", "", "",false,true); parameters.push_back(pnumwanted);
39 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
40 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
42 vector<string> myArray;
43 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
47 m->errorOut(e, "ClassifySeqsCommand", "setParameters");
51 //**********************************************************************************************************************
52 string ClassifySeqsCommand::getHelpString(){
54 string helpString = "";
55 helpString += "The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n";
56 helpString += "The classify.seqs command parameters are reference, fasta, name, group, count, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend, numwanted and probs.\n";
57 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";
58 helpString += "The search parameter allows you to specify the method to find most similar template. Your options are: suffix, kmer, blast, align and distance. The default is kmer.\n";
59 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";
60 helpString += "The group parameter allows you add a group file so you can have the summary totals broken up by group.\n";
61 helpString += "The count parameter allows you add a count file so you can have the summary totals broken up by group.\n";
62 helpString += "The method parameter allows you to specify classification method to use. Your options are: wang, knn and zap. The default is wang.\n";
63 helpString += "The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n";
64 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
66 helpString += "When using MPI, the processors parameter is set to the number of MPI processes running. \n";
68 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.";
69 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
70 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
71 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
72 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
73 helpString += "The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n";
74 helpString += "The cutoff parameter allows you to specify a bootstrap confidence threshold for your taxonomy. The default is 0.\n";
75 helpString += "The probs parameter shuts off the bootstrapping results for the wang and zap method. The default is true, meaning you want the bootstrapping to be shown.\n";
76 helpString += "The iters parameter allows you to specify how many iterations to do when calculating the bootstrap confidence score for your taxonomy with the wang method. The default is 100.\n";
77 //helpString += "The flip parameter allows you shut off mothur's The default is T.\n";
78 helpString += "The classify.seqs command should be in the following format: \n";
79 helpString += "classify.seqs(reference=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n";
80 helpString += "Example classify.seqs(fasta=amazon.fasta, reference=core.filtered, method=knn, search=gotoh, ksize=8, processors=2)\n";
81 helpString += "The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n";
82 helpString += "The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n";
83 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
87 m->errorOut(e, "ClassifySeqsCommand", "getHelpString");
91 //**********************************************************************************************************************
92 string ClassifySeqsCommand::getOutputFileNameTag(string type, string inputName=""){
94 string outputFileName = "";
95 map<string, vector<string> >::iterator it;
97 //is this a type this command creates
98 it = outputTypes.find(type);
99 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
101 if (type == "taxonomy") { outputFileName = "taxonomy"; }
102 else if (type == "accnos") { outputFileName = "flip.accnos"; }
103 else if (type == "taxsummary") { outputFileName = "tax.summary"; }
104 else if (type == "matchdist") { outputFileName = "match.dist"; }
105 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
107 return outputFileName;
109 catch(exception& e) {
110 m->errorOut(e, "ClassifySeqsCommand", "getOutputFileNameTag");
114 //**********************************************************************************************************************
115 ClassifySeqsCommand::ClassifySeqsCommand(){
117 abort = true; calledHelp = true;
119 vector<string> tempOutNames;
120 outputTypes["taxonomy"] = tempOutNames;
121 outputTypes["accnos"] = tempOutNames;
122 outputTypes["taxsummary"] = tempOutNames;
123 outputTypes["matchdist"] = tempOutNames;
125 catch(exception& e) {
126 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
130 //**********************************************************************************************************************
131 ClassifySeqsCommand::ClassifySeqsCommand(string option) {
133 abort = false; calledHelp = false;
134 rdb = ReferenceDB::getInstance(); hasName = false; hasCount=false;
136 //allow user to run help
137 if(option == "help") { help(); abort = true; calledHelp = true; }
138 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
141 vector<string> myArray = setParameters();
143 OptionParser parser(option);
144 map<string, string> parameters = parser.getParameters();
146 ValidParameters validParameter("classify.seqs");
147 map<string, string>::iterator it;
149 //check to make sure all parameters are valid for command
150 for (it = parameters.begin(); it != parameters.end(); it++) {
151 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
154 //initialize outputTypes
155 vector<string> tempOutNames;
156 outputTypes["taxonomy"] = tempOutNames;
157 outputTypes["taxsummary"] = tempOutNames;
158 outputTypes["matchdist"] = tempOutNames;
159 outputTypes["accnos"] = tempOutNames;
161 //if the user changes the output directory command factory will send this info to us in the output parameter
162 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
164 //if the user changes the input directory command factory will send this info to us in the output parameter
165 string inputDir = validParameter.validFile(parameters, "inputdir", false);
166 if (inputDir == "not found"){ inputDir = ""; }
169 it = parameters.find("reference");
170 //user has given a template file
171 if(it != parameters.end()){
172 path = m->hasPath(it->second);
173 //if the user has not given a path then, add inputdir. else leave path alone.
174 if (path == "") { parameters["reference"] = inputDir + it->second; }
177 it = parameters.find("taxonomy");
178 //user has given a template file
179 if(it != parameters.end()){
180 path = m->hasPath(it->second);
181 //if the user has not given a path then, add inputdir. else leave path alone.
182 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
185 it = parameters.find("group");
186 //user has given a template file
187 if(it != parameters.end()){
188 path = m->hasPath(it->second);
189 //if the user has not given a path then, add inputdir. else leave path alone.
190 if (path == "") { parameters["group"] = inputDir + it->second; }
193 it = parameters.find("count");
194 //user has given a template file
195 if(it != parameters.end()){
196 path = m->hasPath(it->second);
197 //if the user has not given a path then, add inputdir. else leave path alone.
198 if (path == "") { parameters["count"] = inputDir + it->second; }
202 fastaFileName = validParameter.validFile(parameters, "fasta", false);
203 if (fastaFileName == "not found") {
204 //if there is a current fasta file, use it
205 string filename = m->getFastaFile();
206 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
207 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
210 m->splitAtDash(fastaFileName, fastaFileNames);
212 //go through files and make sure they are good, if not, then disregard them
213 for (int i = 0; i < fastaFileNames.size(); i++) {
216 if (fastaFileNames[i] == "current") {
217 fastaFileNames[i] = m->getFastaFile();
218 if (fastaFileNames[i] != "") { m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
220 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
221 //erase from file list
222 fastaFileNames.erase(fastaFileNames.begin()+i);
229 if (inputDir != "") {
230 string path = m->hasPath(fastaFileNames[i]);
231 //if the user has not given a path then, add inputdir. else leave path alone.
232 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
238 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
240 //if you can't open it, try default location
241 if (ableToOpen == 1) {
242 if (m->getDefaultPath() != "") { //default path is set
243 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
244 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
246 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
248 fastaFileNames[i] = tryPath;
252 if (ableToOpen == 1) {
253 if (m->getOutputDir() != "") { //default path is set
254 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
255 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
257 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
259 fastaFileNames[i] = tryPath;
265 if (ableToOpen == 1) {
266 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
267 //erase from file list
268 fastaFileNames.erase(fastaFileNames.begin()+i);
271 m->setFastaFile(fastaFileNames[i]);
277 //make sure there is at least one valid file left
278 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
281 namefile = validParameter.validFile(parameters, "name", false);
282 if (namefile == "not found") { namefile = ""; }
285 m->splitAtDash(namefile, namefileNames);
287 //go through files and make sure they are good, if not, then disregard them
288 for (int i = 0; i < namefileNames.size(); i++) {
290 if (namefileNames[i] == "current") {
291 namefileNames[i] = m->getNameFile();
292 if (namefileNames[i] != "") { m->mothurOut("Using " + namefileNames[i] + " as input file for the name parameter where you had given current."); m->mothurOutEndLine(); }
294 m->mothurOut("You have no current namefile, ignoring current."); m->mothurOutEndLine(); ignore=true;
295 //erase from file list
296 namefileNames.erase(namefileNames.begin()+i);
303 if (inputDir != "") {
304 string path = m->hasPath(namefileNames[i]);
305 //if the user has not given a path then, add inputdir. else leave path alone.
306 if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; }
311 ableToOpen = m->openInputFile(namefileNames[i], in, "noerror");
313 //if you can't open it, try default location
314 if (ableToOpen == 1) {
315 if (m->getDefaultPath() != "") { //default path is set
316 string tryPath = m->getDefaultPath() + m->getSimpleName(namefileNames[i]);
317 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
319 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
321 namefileNames[i] = tryPath;
325 if (ableToOpen == 1) {
326 if (m->getOutputDir() != "") { //default path is set
327 string tryPath = m->getOutputDir() + m->getSimpleName(namefileNames[i]);
328 m->mothurOut("Unable to open " + namefileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
330 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
332 namefileNames[i] = tryPath;
337 if (ableToOpen == 1) {
338 m->mothurOut("Unable to open " + namefileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); abort = true;
339 //erase from file list
340 namefileNames.erase(namefileNames.begin()+i);
343 m->setNameFile(namefileNames[i]);
349 if (namefileNames.size() != 0) { hasName = true; }
351 if (namefile != "") {
352 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(); }
355 //check for required parameters
356 countfile = validParameter.validFile(parameters, "count", false);
357 if (countfile == "not found") {
360 m->splitAtDash(countfile, countfileNames);
362 //go through files and make sure they are good, if not, then disregard them
363 for (int i = 0; i < countfileNames.size(); i++) {
366 if (countfileNames[i] == "current") {
367 countfileNames[i] = m->getCountTableFile();
368 if (countfileNames[i] != "") { m->mothurOut("Using " + countfileNames[i] + " as input file for the count parameter where you had given current."); m->mothurOutEndLine(); }
370 m->mothurOut("You have no current count file, ignoring current."); m->mothurOutEndLine(); ignore=true;
371 //erase from file list
372 countfileNames.erase(countfileNames.begin()+i);
379 if (inputDir != "") {
380 string path = m->hasPath(countfileNames[i]);
381 //if the user has not given a path then, add inputdir. else leave path alone.
382 if (path == "") { countfileNames[i] = inputDir + countfileNames[i]; }
388 ableToOpen = m->openInputFile(countfileNames[i], in, "noerror");
390 //if you can't open it, try default location
391 if (ableToOpen == 1) {
392 if (m->getDefaultPath() != "") { //default path is set
393 string tryPath = m->getDefaultPath() + m->getSimpleName(countfileNames[i]);
394 m->mothurOut("Unable to open " + countfileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
396 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
398 countfileNames[i] = tryPath;
402 if (ableToOpen == 1) {
403 if (m->getOutputDir() != "") { //default path is set
404 string tryPath = m->getOutputDir() + m->getSimpleName(countfileNames[i]);
405 m->mothurOut("Unable to open " + countfileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
407 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
409 countfileNames[i] = tryPath;
415 if (ableToOpen == 1) {
416 m->mothurOut("Unable to open " + countfileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
417 //erase from file list
418 countfileNames.erase(countfileNames.begin()+i);
421 m->setCountTableFile(countfileNames[i]);
427 if (countfileNames.size() != 0) { hasCount = true; if (countfileNames.size() != fastaFileNames.size()) {m->mothurOut("If you provide a count file, you must have one for each fasta file."); m->mothurOutEndLine(); } }
429 //make sure there is at least one valid file left
430 if (hasName && hasCount) { m->mothurOut("[ERROR]: You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
432 groupfile = validParameter.validFile(parameters, "group", false);
433 if (groupfile == "not found") { groupfile = ""; }
435 m->splitAtDash(groupfile, groupfileNames);
437 //go through files and make sure they are good, if not, then disregard them
438 for (int i = 0; i < groupfileNames.size(); i++) {
439 if (inputDir != "") {
440 string path = m->hasPath(groupfileNames[i]);
441 //if the user has not given a path then, add inputdir. else leave path alone.
442 if (path == "") { groupfileNames[i] = inputDir + groupfileNames[i]; }
447 ableToOpen = m->openInputFile(groupfileNames[i], in, "noerror");
449 //if you can't open it, try default location
450 if (ableToOpen == 1) {
451 if (m->getDefaultPath() != "") { //default path is set
452 string tryPath = m->getDefaultPath() + m->getSimpleName(groupfileNames[i]);
453 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
455 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
457 groupfileNames[i] = tryPath;
461 if (ableToOpen == 1) {
462 if (m->getOutputDir() != "") { //default path is set
463 string tryPath = m->getOutputDir() + m->getSimpleName(groupfileNames[i]);
464 m->mothurOut("Unable to open " + groupfileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
466 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
468 groupfileNames[i] = tryPath;
474 if (ableToOpen == 1) {
475 m->mothurOut("Unable to open " + groupfileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); groupfileNames[i] = "";
476 //erase from file list
477 groupfileNames.erase(groupfileNames.begin()+i);
480 m->setGroupFile(groupfileNames[i]);
485 if (groupfile != "") {
486 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(); }
487 if (hasCount) { m->mothurOut("[ERROR]: You must enter ONLY ONE of the following: count or group."); m->mothurOutEndLine(); abort = true; }
489 for (int i = 0; i < fastaFileNames.size(); i++) { groupfileNames.push_back(""); }
492 //check for optional parameter and set defaults
493 // ...at some point should added some additional type checking...
495 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
496 m->setProcessors(temp);
497 m->mothurConvert(temp, processors);
499 temp = validParameter.validFile(parameters, "save", false); if (temp == "not found"){ temp = "f"; }
500 save = m->isTrue(temp);
502 if (save) { //clear out old references
506 //this has to go after save so that if the user sets save=t and provides no reference we abort
507 templateFileName = validParameter.validFile(parameters, "reference", true);
508 if (templateFileName == "not found") {
509 //check for saved reference sequences
510 if (rdb->referenceSeqs.size() != 0) {
511 templateFileName = "saved";
513 m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required for the classify.seqs command.");
514 m->mothurOutEndLine();
517 }else if (templateFileName == "not open") { abort = true; }
518 else { if (save) { rdb->setSavedReference(templateFileName); } }
520 //this has to go after save so that if the user sets save=t and provides no reference we abort
521 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
522 if (taxonomyFileName == "not found") {
523 //check for saved reference sequences
524 if (rdb->wordGenusProb.size() != 0) {
525 taxonomyFileName = "saved";
527 m->mothurOut("[ERROR]: You don't have any saved taxonomy information and the taxonomy parameter is a required for the classify.seqs command.");
528 m->mothurOutEndLine();
531 }else if (taxonomyFileName == "not open") { abort = true; }
532 else { if (save) { rdb->setSavedTaxonomy(taxonomyFileName); } }
534 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
536 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "wang"; }
538 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){
540 if (method == "zap") { temp = "7"; }
542 m->mothurConvert(temp, kmerSize);
544 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
545 m->mothurConvert(temp, match);
547 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
548 m->mothurConvert(temp, misMatch);
550 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
551 m->mothurConvert(temp, gapOpen);
553 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
554 m->mothurConvert(temp, gapExtend);
556 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
557 m->mothurConvert(temp, numWanted);
559 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
560 m->mothurConvert(temp, cutoff);
562 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
563 probs = m->isTrue(temp);
565 temp = validParameter.validFile(parameters, "shortcuts", false); if (temp == "not found"){ temp = "true"; }
566 writeShortcuts = m->isTrue(temp);
568 //temp = validParameter.validFile(parameters, "flip", false); if (temp == "not found"){ temp = "T"; }
569 //flip = m->isTrue(temp);
572 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
573 m->mothurConvert(temp, iters);
576 if ((method == "wang") && (search != "kmer")) {
577 m->mothurOut("The wang method requires the kmer search. " + search + " will be disregarded, and kmer will be used." ); m->mothurOutEndLine();
581 if ((method == "zap") && ((search != "kmer") && (search != "align"))) {
582 m->mothurOut("The zap method requires the kmer or align search. " + search + " will be disregarded, and kmer will be used." ); m->mothurOutEndLine();
588 if (namefileNames.size() == 0){
589 if (fastaFileNames.size() != 0) {
590 vector<string> files; files.push_back(fastaFileNames[fastaFileNames.size()-1]);
591 parser.getNameFile(files);
598 catch(exception& e) {
599 m->errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
604 //**********************************************************************************************************************
605 ClassifySeqsCommand::~ClassifySeqsCommand(){
606 if (abort == false) {
607 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
610 //**********************************************************************************************************************
612 int ClassifySeqsCommand::execute(){
614 if (abort == true) { if (calledHelp) { return 0; } return 2; }
616 string outputMethodTag = method + ".";
617 if(method == "wang"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters, rand(), flip, writeShortcuts); }
618 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted, rand()); }
619 else if(method == "zap"){
620 outputMethodTag = search + "_" + outputMethodTag;
621 if (search == "kmer") { classify = new KmerTree(templateFileName, taxonomyFileName, kmerSize, cutoff); }
622 else { classify = new AlignTree(templateFileName, taxonomyFileName, cutoff); }
625 m->mothurOut(search + " is not a valid method option. I will run the command using wang.");
626 m->mothurOutEndLine();
627 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters, rand(), flip, writeShortcuts);
630 if (m->control_pressed) { delete classify; return 0; }
632 for (int s = 0; s < fastaFileNames.size(); s++) {
634 m->mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
636 string baseTName = m->getSimpleName(taxonomyFileName);
637 if (taxonomyFileName == "saved") {baseTName = rdb->getSavedTaxonomy(); }
639 //set rippedTaxName to
640 string RippedTaxName = "";
641 bool foundDot = false;
642 for (int i = baseTName.length()-1; i >= 0; i--) {
643 if (foundDot && (baseTName[i] != '.')) { RippedTaxName = baseTName[i] + RippedTaxName; }
644 else if (foundDot && (baseTName[i] == '.')) { break; }
645 else if (!foundDot && (baseTName[i] == '.')) { foundDot = true; }
647 if (RippedTaxName != "") { RippedTaxName += "."; }
649 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
650 string newTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + outputMethodTag + getOutputFileNameTag("taxonomy");
651 string newaccnosFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + outputMethodTag +getOutputFileNameTag("accnos");
652 string tempTaxonomyFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
653 string taxSummary = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + RippedTaxName + outputMethodTag + getOutputFileNameTag("taxsummary");
655 if ((method == "knn") && (search == "distance")) {
656 string DistName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + getOutputFileNameTag("matchdist");
657 classify->setDistName(DistName); outputNames.push_back(DistName); outputTypes["matchdist"].push_back(DistName);
660 outputNames.push_back(newTaxonomyFile); outputTypes["taxonomy"].push_back(newTaxonomyFile);
661 outputNames.push_back(newaccnosFile); outputTypes["accnos"].push_back(newaccnosFile);
662 outputNames.push_back(taxSummary); outputTypes["taxsummary"].push_back(taxSummary);
664 int start = time(NULL);
665 int numFastaSeqs = 0;
666 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
669 int pid, numSeqsPerProcessor;
671 vector<unsigned long long> MPIPos;
674 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
675 MPI_Comm_size(MPI_COMM_WORLD, &processors);
678 MPI_File outMPINewTax;
679 MPI_File outMPITempTax;
682 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
683 int inMode=MPI_MODE_RDONLY;
685 char outNewTax[1024];
686 strcpy(outNewTax, newTaxonomyFile.c_str());
688 char outTempTax[1024];
689 strcpy(outTempTax, tempTaxonomyFile.c_str());
692 strcpy(outAcc, newaccnosFile.c_str());
694 char inFileName[1024];
695 strcpy(inFileName, fastaFileNames[s].c_str());
697 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
698 MPI_File_open(MPI_COMM_WORLD, outNewTax, outMode, MPI_INFO_NULL, &outMPINewTax);
699 MPI_File_open(MPI_COMM_WORLD, outTempTax, outMode, MPI_INFO_NULL, &outMPITempTax);
700 MPI_File_open(MPI_COMM_WORLD, outAcc, outMode, MPI_INFO_NULL, &outMPIAcc);
702 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPIAcc); MPI_File_close(&outMPITempTax); delete classify; return 0; }
704 if (pid == 0) { //you are the root process
706 MPIPos = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs); //fills MPIPos, returns numSeqs
708 //send file positions to all processes
709 for(int i = 1; i < processors; i++) {
710 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
711 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
714 //figure out how many sequences you have to align
715 numSeqsPerProcessor = numFastaSeqs / processors;
716 int startIndex = pid * numSeqsPerProcessor;
717 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
721 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, outMPIAcc, MPIPos);
723 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPIAcc); MPI_File_close(&outMPITempTax); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
725 for (int i = 1; i < processors; i++) {
727 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
729 }else{ //you are a child process
730 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
731 MPIPos.resize(numFastaSeqs+1);
732 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
734 //figure out how many sequences you have to align
735 numSeqsPerProcessor = numFastaSeqs / processors;
736 int startIndex = pid * numSeqsPerProcessor;
737 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
741 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPINewTax, outMPITempTax, outMPIAcc, MPIPos);
743 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&inMPI); MPI_File_close(&outMPINewTax); MPI_File_close(&outMPIAcc); MPI_File_close(&outMPITempTax); delete classify; return 0; }
746 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
750 MPI_File_close(&inMPI);
751 MPI_File_close(&outMPINewTax);
752 MPI_File_close(&outMPITempTax);
753 MPI_File_close(&outMPIAcc);
754 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
758 vector<unsigned long long> positions;
759 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
760 positions = m->divideFile(fastaFileNames[s], processors);
761 for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(new linePair(positions[i], positions[(i+1)])); }
763 if (processors == 1) {
764 lines.push_back(new linePair(0, 1000));
766 positions = m->setFilePosFasta(fastaFileNames[s], numFastaSeqs);
767 if (positions.size() < processors) { processors = positions.size(); }
769 //figure out how many sequences you have to process
770 int numSeqsPerProcessor = numFastaSeqs / processors;
771 for (int i = 0; i < processors; i++) {
772 int startIndex = i * numSeqsPerProcessor;
773 if(i == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; }
774 lines.push_back(new linePair(positions[startIndex], numSeqsPerProcessor));
779 numFastaSeqs = driver(lines[0], newTaxonomyFile, tempTaxonomyFile, newaccnosFile, fastaFileNames[s]);
781 numFastaSeqs = createProcesses(newTaxonomyFile, tempTaxonomyFile, newaccnosFile, fastaFileNames[s]);
785 if (!m->isBlank(newaccnosFile)) { m->mothurOutEndLine(); m->mothurOut("[WARNING]: mothur suspects some of your sequences may be reversed, please check " + newaccnosFile + " for the list of the sequences."); m->mothurOutEndLine(); }
787 m->mothurOutEndLine();
788 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
794 if (pid == 0) { //this part does not need to be paralellized
796 if(namefile != "") { m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush(); MPIReadNamesFile(namefileNames[s]); m->mothurOut(" Done."); m->mothurOutEndLine(); }
801 m->mothurOut("Reading " + namefileNames[s] + "..."); cout.flush();
802 nameMap.clear(); //remove old names
803 m->readNames(namefileNames[s], nameMap);
804 m->mothurOut(" Done."); m->mothurOutEndLine();
809 GroupMap* groupMap = NULL;
810 CountTable* ct = NULL;
811 PhyloSummary* taxaSum;
813 ct = new CountTable();
814 ct->readTable(countfileNames[s]);
815 taxaSum = new PhyloSummary(taxonomyFileName, ct);
816 taxaSum->summarize(tempTaxonomyFile);
818 if (groupfile != "") { group = groupfileNames[s]; groupMap = new GroupMap(group); groupMap->readMap(); }
820 taxaSum = new PhyloSummary(taxonomyFileName, groupMap);
822 if (m->control_pressed) { outputTypes.clear(); if (ct != NULL) { delete ct; } if (groupMap != NULL) { delete groupMap; } delete taxaSum; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
824 if (namefile == "") { taxaSum->summarize(tempTaxonomyFile); }
827 m->openInputFile(tempTaxonomyFile, in);
829 //read in users taxonomy file and add sequences to tree
833 if (m->control_pressed) { outputTypes.clear(); if (ct != NULL) { delete ct; } if (groupMap != NULL) { delete groupMap; } delete taxaSum; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
835 in >> name >> taxon; m->gobble(in);
837 itNames = nameMap.find(name);
839 if (itNames == nameMap.end()) {
840 m->mothurOut(name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
842 for (int i = 0; i < itNames->second.size(); i++) {
843 taxaSum->addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
845 itNames->second.clear();
846 nameMap.erase(itNames->first);
852 m->mothurRemove(tempTaxonomyFile);
854 if (m->control_pressed) { outputTypes.clear(); if (ct != NULL) { delete ct; } if (groupMap != NULL) { delete groupMap; } for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete classify; return 0; }
858 m->openOutputFile(taxSummary, outTaxTree);
859 taxaSum->print(outTaxTree);
862 //output taxonomy with the unclassified bins added
864 m->openInputFile(newTaxonomyFile, inTax);
867 string unclass = newTaxonomyFile + ".unclass.temp";
868 m->openOutputFile(unclass, outTax);
870 //get maxLevel from phylotree so you know how many 'unclassified's to add
871 int maxLevel = taxaSum->getMaxLevel();
873 //read taxfile - this reading and rewriting is done to preserve the confidence scores.
875 while (!inTax.eof()) {
876 if (m->control_pressed) { outputTypes.clear(); if (ct != NULL) { delete ct; } if (groupMap != NULL) { delete groupMap; } delete taxaSum; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->mothurRemove(unclass); delete classify; return 0; }
878 inTax >> name >> taxon; m->gobble(inTax);
880 string newTax = addUnclassifieds(taxon, maxLevel);
882 outTax << name << '\t' << newTax << endl;
887 if (ct != NULL) { delete ct; }
888 if (groupMap != NULL) { delete groupMap; } delete taxaSum;
889 m->mothurRemove(newTaxonomyFile);
890 rename(unclass.c_str(), newTaxonomyFile.c_str());
892 m->mothurOutEndLine();
893 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
899 m->mothurOutEndLine();
900 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
901 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
902 m->mothurOutEndLine();
905 //set taxonomy file as new current taxonomyfile
907 itTypes = outputTypes.find("taxonomy");
908 if (itTypes != outputTypes.end()) {
909 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
913 itTypes = outputTypes.find("accnos");
914 if (itTypes != outputTypes.end()) {
915 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
922 catch(exception& e) {
923 m->errorOut(e, "ClassifySeqsCommand", "execute");
928 /**************************************************************************************************/
929 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
931 string newTax, taxon;
934 //keep what you have counting the levels
935 while (tax.find_first_of(';') != -1) {
937 taxon = tax.substr(0,tax.find_first_of(';'))+';';
938 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
943 //add "unclassified" until you reach maxLevel
944 while (level < maxlevel) {
945 newTax += "unclassified;";
951 catch(exception& e) {
952 m->errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
957 /**************************************************************************************************/
959 int ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string accnos, string filename) {
965 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
968 //loop through and create all the processes you want
969 while (process != processors) {
973 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
976 num = driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", accnos + toString(getpid()) + ".temp", filename);
978 //pass numSeqs to parent
980 string tempFile = filename + toString(getpid()) + ".num.temp";
981 m->openOutputFile(tempFile, out);
987 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
988 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
993 //parent does its part
994 num = driver(lines[0], taxFileName, tempTaxFile, accnos, filename);
996 //force parent to wait until all the processes are done
997 for (int i=0;i<processIDS.size();i++) {
998 int temp = processIDS[i];
1002 for (int i = 0; i < processIDS.size(); i++) {
1004 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
1005 m->openInputFile(tempFile, in);
1006 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
1007 in.close(); m->mothurRemove(m->getFullPathName(tempFile));
1010 //////////////////////////////////////////////////////////////////////////////////////////////////////
1011 //Windows version shared memory, so be careful when passing variables through the alignData struct.
1012 //Above fork() will clone, so memory is separate, but that's not the case with windows,
1013 //////////////////////////////////////////////////////////////////////////////////////////////////////
1015 vector<classifyData*> pDataArray;
1016 DWORD dwThreadIdArray[processors-1];
1017 HANDLE hThreadArray[processors-1];
1019 //Create processor worker threads.
1020 for( int i=0; i<processors-1; i++ ){
1021 // Allocate memory for thread data.
1022 string extension = "";
1023 if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
1025 classifyData* tempclass = new classifyData((accnos + extension), 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, flip, writeShortcuts);
1026 pDataArray.push_back(tempclass);
1028 //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
1029 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
1030 hThreadArray[i] = CreateThread(NULL, 0, MyClassThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
1034 //parent does its part
1035 num = driver(lines[processors-1], taxFileName + toString(processors-1) + ".temp", tempTaxFile + toString(processors-1) + ".temp", accnos + toString(processors-1) + ".temp", filename);
1036 processIDS.push_back((processors-1));
1038 //Wait until all threads have terminated.
1039 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
1041 //Close all thread handles and free memory allocations.
1042 for(int i=0; i < pDataArray.size(); i++){
1043 num += pDataArray[i]->count;
1044 CloseHandle(hThreadArray[i]);
1045 delete pDataArray[i];
1049 vector<string> nonBlankAccnosFiles;
1050 if (!(m->isBlank(accnos))) { nonBlankAccnosFiles.push_back(accnos); }
1051 else { m->mothurRemove(accnos); } //remove so other files can be renamed to it
1053 for(int i=0;i<processIDS.size();i++){
1054 m->appendFiles((taxFileName + toString(processIDS[i]) + ".temp"), taxFileName);
1055 m->appendFiles((tempTaxFile + toString(processIDS[i]) + ".temp"), tempTaxFile);
1056 if (!(m->isBlank(accnos + toString(processIDS[i]) + ".temp"))) {
1057 nonBlankAccnosFiles.push_back(accnos + toString(processIDS[i]) + ".temp");
1058 }else { m->mothurRemove((accnos + toString(processIDS[i]) + ".temp")); }
1060 m->mothurRemove((m->getFullPathName(taxFileName) + toString(processIDS[i]) + ".temp"));
1061 m->mothurRemove((m->getFullPathName(tempTaxFile) + toString(processIDS[i]) + ".temp"));
1064 //append accnos files
1065 if (nonBlankAccnosFiles.size() != 0) {
1066 rename(nonBlankAccnosFiles[0].c_str(), accnos.c_str());
1068 for (int h=1; h < nonBlankAccnosFiles.size(); h++) {
1069 m->appendFiles(nonBlankAccnosFiles[h], accnos);
1070 m->mothurRemove(nonBlankAccnosFiles[h]);
1072 }else { //recreate the accnosfile if needed
1074 m->openOutputFile(accnos, out);
1081 catch(exception& e) {
1082 m->errorOut(e, "ClassifySeqsCommand", "createProcesses");
1086 //**********************************************************************************************************************
1088 int ClassifySeqsCommand::driver(linePair* filePos, string taxFName, string tempTFName, string accnos, string filename){
1091 m->openOutputFile(taxFName, outTax);
1093 ofstream outTaxSimple;
1094 m->openOutputFile(tempTFName, outTaxSimple);
1097 m->openOutputFile(accnos, outAcc);
1100 m->openInputFile(filename, inFASTA);
1104 inFASTA.seekg(filePos->start);
1110 if (m->control_pressed) {
1113 outTaxSimple.close();
1114 outAcc.close(); return 0; }
1116 Sequence* candidateSeq = new Sequence(inFASTA); m->gobble(inFASTA);
1118 if (candidateSeq->getName() != "") {
1120 taxonomy = classify->getTaxonomy(candidateSeq);
1122 if (m->control_pressed) { delete candidateSeq; return 0; }
1124 if (taxonomy == "unknown;") { m->mothurOut("[WARNING]: " + candidateSeq->getName() + " could not be classified. You can use the remove.lineage command with taxon=unknown; to remove such sequences."); m->mothurOutEndLine(); }
1126 //output confidence scores or not
1128 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
1130 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
1133 if (classify->getFlipped()) { outAcc << candidateSeq->getName() << endl; }
1135 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
1139 delete candidateSeq;
1141 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1142 unsigned long long pos = inFASTA.tellg();
1143 if ((pos == -1) || (pos >= filePos->end)) { break; }
1145 if (inFASTA.eof()) { break; }
1149 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
1153 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
1157 outTaxSimple.close();
1162 catch(exception& e) {
1163 m->errorOut(e, "ClassifySeqsCommand", "driver");
1167 //**********************************************************************************************************************
1169 int ClassifySeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& newFile, MPI_File& tempFile, MPI_File& accFile, vector<unsigned long long>& MPIPos){
1171 MPI_Status statusNew;
1172 MPI_Status statusTemp;
1173 MPI_Status statusAcc;
1177 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
1180 string outputString;
1182 for(int i=0;i<num;i++){
1184 if (m->control_pressed) { return 0; }
1186 //read next sequence
1187 int length = MPIPos[start+i+1] - MPIPos[start+i];
1188 char* buf4 = new char[length];
1189 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
1191 string tempBuf = buf4;
1192 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
1193 istringstream iss (tempBuf,istringstream::in);
1196 Sequence* candidateSeq = new Sequence(iss);
1198 if (candidateSeq->getName() != "") {
1199 taxonomy = classify->getTaxonomy(candidateSeq);
1201 if (taxonomy == "unknown;") { m->mothurOut("[WARNING]: " + candidateSeq->getName() + " could not be classified. You can use the remove.lineage command with taxon=unknown; to remove such sequences."); m->mothurOutEndLine(); }
1203 //output confidence scores or not
1205 outputString = candidateSeq->getName() + "\t" + taxonomy + "\n";
1207 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
1210 int length = outputString.length();
1211 char* buf2 = new char[length];
1212 memcpy(buf2, outputString.c_str(), length);
1214 MPI_File_write_shared(newFile, buf2, length, MPI_CHAR, &statusNew);
1217 outputString = candidateSeq->getName() + "\t" + classify->getSimpleTax() + "\n";
1218 length = outputString.length();
1219 char* buf = new char[length];
1220 memcpy(buf, outputString.c_str(), length);
1222 MPI_File_write_shared(tempFile, buf, length, MPI_CHAR, &statusTemp);
1225 if (classify->getFlipped()) {
1226 outputString = candidateSeq->getName() + "\n";
1227 length = outputString.length();
1228 char* buf3 = new char[length];
1229 memcpy(buf3, outputString.c_str(), length);
1231 MPI_File_write_shared(accFile, buf3, length, MPI_CHAR, &statusAcc);
1236 delete candidateSeq;
1238 if((i+1) % 100 == 0){ cout << "Classifying sequence " << (i+1) << endl; }
1241 if(num % 100 != 0){ cout << "Classifying sequence " << (num) << endl; }
1246 catch(exception& e) {
1247 m->errorOut(e, "ClassifySeqsCommand", "driverMPI");
1252 //**********************************************************************************************************************
1253 int ClassifySeqsCommand::MPIReadNamesFile(string nameFilename){
1256 nameMap.clear(); //remove old names
1262 //char* inFileName = new char[nameFilename.length()];
1263 //memcpy(inFileName, nameFilename.c_str(), nameFilename.length());
1265 char inFileName[1024];
1266 strcpy(inFileName, nameFilename.c_str());
1268 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
1269 MPI_File_get_size(inMPI, &size);
1270 //delete inFileName;
1272 char* buffer = new char[size];
1273 MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
1275 string tempBuf = buffer;
1276 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
1277 istringstream iss (tempBuf,istringstream::in);
1280 string firstCol, secondCol;
1282 iss >> firstCol >> secondCol; m->gobble(iss);
1284 vector<string> temp;
1285 m->splitAtComma(secondCol, temp);
1287 nameMap[firstCol] = temp;
1290 MPI_File_close(&inMPI);
1294 catch(exception& e) {
1295 m->errorOut(e, "ClassifySeqsCommand", "MPIReadNamesFile");
1300 /**************************************************************************************************/