X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=classify.cpp;h=7f451261550a949b3a1cbb7ab72fd374122597bb;hb=62568a297aaf939ab753334f17abde82ec8e8531;hp=346c76478eefdf896fe6f04659174321bc721063;hpb=c3f0a9c8f932b923f3a6fbbf143e8f4b85fd6f5f;p=mothur.git diff --git a/classify.cpp b/classify.cpp index 346c764..7f45126 100644 --- a/classify.cpp +++ b/classify.cpp @@ -15,13 +15,84 @@ #include "distancedb.hpp" /**************************************************************************************************/ -Classify::Classify(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch) : taxFile(tfile), templateFile(tempFile) { +void Classify::generateDatabaseAndNames(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch) { try { - + taxFile = tfile; readTaxonomy(taxFile); + templateFile = tempFile; + int start = time(NULL); int numSeqs = 0; + + m->mothurOut("Generating search database... "); cout.flush(); +#ifdef USE_MPI + int pid, processors; + vector positions; + int tag = 2001; + + MPI_Status status; + MPI_File inMPI; + MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are + MPI_Comm_size(MPI_COMM_WORLD, &processors); + + //char* inFileName = new char[tempFile.length()]; + //memcpy(inFileName, tempFile.c_str(), tempFile.length()); + + char inFileName[1024]; + strcpy(inFileName, tempFile.c_str()); + + MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer + //delete inFileName; + + if (pid == 0) { //only one process needs to scan file + positions = setFilePosFasta(tempFile, numSeqs); //fills MPIPos, returns numSeqs + + //send file positions to all processes + for(int i = 1; i < processors; i++) { + MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD); + MPI_Send(&positions[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD); + } + }else{ + MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); + positions.resize(numSeqs+1); + MPI_Recv(&positions[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status); + } + + //create database + if(method == "kmer") { database = new KmerDB(tempFile, kmerSize); } + else if(method == "suffix") { database = new SuffixDB(numSeqs); } + else if(method == "blast") { database = new BlastDB(gapOpen, gapExtend, match, misMatch); } + else if(method == "distance") { database = new DistanceDB(); } + else { + m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8."); m->mothurOutEndLine(); + database = new KmerDB(tempFile, 8); + } + + //read file + for(int i=0;i length) { tempBuf = tempBuf.substr(0, length); } + delete buf4; + istringstream iss (tempBuf,istringstream::in); + + Sequence temp(iss); + if (temp.getName() != "") { + names.push_back(temp.getName()); + database->addSequence(temp); + } + } + + database->generateDB(); + MPI_File_close(&inMPI); + MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case + #else + //need to know number of template seqs for suffixdb if (method == "suffix") { ifstream inFASTA; @@ -30,8 +101,6 @@ Classify::Classify(string tfile, string tempFile, string method, int kmerSize, f inFASTA.close(); } - mothurOut("Generating search database... "); cout.flush(); - bool needToGenerate = true; string kmerDBName; if(method == "kmer") { @@ -45,8 +114,8 @@ Classify::Classify(string tfile, string tempFile, string method, int kmerSize, f else if(method == "blast") { database = new BlastDB(gapOpen, gapExtend, match, misMatch); } else if(method == "distance") { database = new DistanceDB(); } else { - mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8."); - mothurOutEndLine(); + m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8."); + m->mothurOutEndLine(); database = new KmerDB(tempFile, 8); } @@ -59,7 +128,7 @@ Classify::Classify(string tfile, string tempFile, string method, int kmerSize, f gobble(fastaFile); names.push_back(temp.getName()); - + database->addSequence(temp); } fastaFile.close(); @@ -76,37 +145,98 @@ Classify::Classify(string tfile, string tempFile, string method, int kmerSize, f while (!fastaFile.eof()) { Sequence temp(fastaFile); gobble(fastaFile); - + names.push_back(temp.getName()); } fastaFile.close(); } - +#endif database->setNumSeqs(names.size()); - mothurOut("DONE."); mothurOutEndLine(); - mothurOut("It took " + toString(time(NULL) - start) + " seconds generate search database. "); mothurOutEndLine(); + //sanity check + bool okay = phyloTree->ErrorCheck(names); + + if (!okay) { m->control_pressed = true; } + + m->mothurOut("DONE."); m->mothurOutEndLine(); + m->mothurOut("It took " + toString(time(NULL) - start) + " seconds generate search database. "); m->mothurOutEndLine(); } catch(exception& e) { - errorOut(e, "Classify", "Classify"); + m->errorOut(e, "Classify", "generateDatabaseAndNames"); exit(1); } } /**************************************************************************************************/ +Classify::Classify() { m = MothurOut::getInstance(); database = NULL; } +/**************************************************************************************************/ -void Classify::readTaxonomy(string file) { +int Classify::readTaxonomy(string file) { try { phyloTree = new PhyloTree(); + string name, taxInfo; + m->mothurOutEndLine(); + m->mothurOut("Reading in the " + file + " taxonomy...\t"); cout.flush(); + +#ifdef USE_MPI + int pid, num, processors; + vector positions; + int tag = 2001; + + MPI_Status status; + MPI_File inMPI; + MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are + MPI_Comm_size(MPI_COMM_WORLD, &processors); + + //char* inFileName = new char[file.length()]; + //memcpy(inFileName, file.c_str(), file.length()); + + char inFileName[1024]; + strcpy(inFileName, file.c_str()); + + MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer + //delete inFileName; + + if (pid == 0) { + positions = setFilePosEachLine(file, num); + + //send file positions to all processes + for(int i = 1; i < processors; i++) { + MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD); + MPI_Send(&positions[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD); + } + }else{ + MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); + positions.resize(num+1); + MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status); + } + + //read file + for(int i=0;i length) { tempBuf = tempBuf.substr(0, length); } + delete buf4; + + istringstream iss (tempBuf,istringstream::in); + iss >> name >> taxInfo; + taxonomy[name] = taxInfo; + phyloTree->addSeqToTree(name, taxInfo); + } + + MPI_File_close(&inMPI); + MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case +#else ifstream inTax; openInputFile(file, inTax); - mothurOutEndLine(); - mothurOut("Reading in the " + file + " taxonomy...\t"); cout.flush(); - - string name, taxInfo; //read template seqs and save while (!inTax.eof()) { inTax >> name >> taxInfo; @@ -117,16 +247,21 @@ void Classify::readTaxonomy(string file) { gobble(inTax); } - - phyloTree->assignHeirarchyIDs(0); inTax.close(); +#endif - mothurOut("DONE."); - mothurOutEndLine(); cout.flush(); + phyloTree->assignHeirarchyIDs(0); + + phyloTree->setUp(file); + + m->mothurOut("DONE."); + m->mothurOutEndLine(); cout.flush(); + + return phyloTree->getNumSeqs(); } catch(exception& e) { - errorOut(e, "Classify", "readTaxonomy"); + m->errorOut(e, "Classify", "readTaxonomy"); exit(1); } } @@ -152,7 +287,7 @@ vector Classify::parseTax(string tax) { return taxons; } catch(exception& e) { - errorOut(e, "Classify", "parseTax"); + m->errorOut(e, "Classify", "parseTax"); exit(1); } }