X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bayesian.cpp;h=80518666cfe5436280f58117fe506dc132bd2754;hb=d4cd876bd09bfa1bcb137261700eae79f9e41fc0;hp=d0c0a16fe834b1a2e8a5c87698e0b02fba41fbf8;hpb=e0fbf58358a72f20352cf2a43922ab6b5bdf0cf8;p=mothur.git diff --git a/bayesian.cpp b/bayesian.cpp index d0c0a16..8051866 100644 --- a/bayesian.cpp +++ b/bayesian.cpp @@ -18,8 +18,9 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { /************calculate the probablity that each word will be in a specific taxonomy*************/ string tfileroot = tfile.substr(0,tfile.find_last_of(".")+1); - string tempfileroot = getRootName(getSimpleName(tempFile)); + string tempfileroot = m->getRootName(m->getSimpleName(tempFile)); string phyloTreeName = tfileroot + "tree.train"; + string phyloTreeSumName = tfileroot + "tree.sum"; string probFileName = tfileroot + tempfileroot + char('0'+ kmerSize) + "mer.prob"; string probFileName2 = tfileroot + tempfileroot + char('0'+ kmerSize) + "mer.numNonZero"; @@ -29,10 +30,17 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { ifstream phyloTreeTest(phyloTreeName.c_str()); ifstream probFileTest2(probFileName2.c_str()); ifstream probFileTest(probFileName.c_str()); + ifstream probFileTest3(phyloTreeSumName.c_str()); int start = time(NULL); - if(probFileTest && probFileTest2 && phyloTreeTest){ + //if they are there make sure they were created after this release date + bool FilesGood = false; + if(probFileTest && probFileTest2 && phyloTreeTest && probFileTest3){ + FilesGood = checkReleaseDate(probFileTest, probFileTest2, phyloTreeTest, probFileTest3); + } + + if(probFileTest && probFileTest2 && phyloTreeTest && probFileTest3 && FilesGood){ m->mothurOut("Reading template taxonomy... "); cout.flush(); phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName); @@ -68,9 +76,11 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { //initialze probabilities wordGenusProb.resize(numKmers); - + cout << numKmers << '\t' << genusNodes.size() << endl; for (int j = 0; j < wordGenusProb.size(); j++) { wordGenusProb[j].resize(genusNodes.size()); } - + cout << numKmers << '\t' << genusNodes.size() << endl; + ofstream out; + ofstream out2; #ifdef USE_MPI int pid; @@ -79,13 +89,18 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { if (pid == 0) { #endif - ofstream out; - openOutputFile(probFileName, out); + + m->openOutputFile(probFileName, out); + + //output mothur version + out << "#" << m->getVersion() << endl; out << numKmers << endl; - ofstream out2; - openOutputFile(probFileName2, out2); + m->openOutputFile(probFileName2, out2); + + //output mothur version + out2 << "#" << m->getVersion() << endl; #ifdef USE_MPI } @@ -94,7 +109,7 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { //for each word for (int i = 0; i < numKmers; i++) { - if (m->control_pressed) { break; } + if (m->control_pressed) { break; } #ifdef USE_MPI MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are @@ -128,7 +143,9 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { wordGenusProb[i][k] = log((count[genusNodes[k]] + probabilityInTemplate) / (float) (genusTotals[k] + 1)); if (count[genusNodes[k]] != 0) { #ifdef USE_MPI + int pid; MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are + if (pid == 0) { #endif @@ -144,6 +161,7 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { #ifdef USE_MPI MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are + if (pid == 0) { #endif @@ -157,6 +175,7 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) { #ifdef USE_MPI MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are + if (pid == 0) { #endif @@ -389,8 +408,8 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string MPI_File_open(MPI_COMM_WORLD, inFileName2, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI2); //comm, filename, mode, info, filepointer if (pid == 0) { - positions = setFilePosEachLine(inNumName, num); - positions2 = setFilePosEachLine(inName, num2); + positions = m->setFilePosEachLine(inNumName, num); + positions2 = m->setFilePosEachLine(inName, num2); for(int i = 1; i < processors; i++) { MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD); @@ -409,12 +428,19 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string positions2.resize(num2+1); MPI_Recv(&positions2[0], (num2+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status); } - - //read numKmers + + //read version int length = positions2[1] - positions2[0]; + char* buf5 = new char[length]; + + MPI_File_read_at(inMPI2, positions2[0], buf5, length, MPI_CHAR, &status); + delete buf5; + + //read numKmers + length = positions2[2] - positions2[1]; char* buf = new char[length]; - MPI_File_read_at(inMPI2, positions2[0], buf, length, MPI_CHAR, &status); + MPI_File_read_at(inMPI2, positions2[1], buf, length, MPI_CHAR, &status); string tempBuf = buf; if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); } @@ -431,10 +457,17 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string int kmer, name; vector numbers; numbers.resize(numKmers); float prob; - vector zeroCountProb; zeroCountProb.resize(numKmers); + vector zeroCountProb; zeroCountProb.resize(numKmers); + + //read version + length = positions[1] - positions[0]; + char* buf6 = new char[length]; + MPI_File_read_at(inMPI2, positions[0], buf6, length, MPI_CHAR, &status); + delete buf6; + //read file - for(int i=0;i> numKmers; gobble(in); + //read version + string line = m->getline(in); m->gobble(in); + + in >> numKmers; m->gobble(in); //initialze probabilities wordGenusProb.resize(numKmers); @@ -493,11 +528,14 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string vector num; num.resize(numKmers); float prob; vector zeroCountProb; zeroCountProb.resize(numKmers); - + + //read version + string line2 = m->getline(inNum); m->gobble(inNum); + while (inNum) { inNum >> zeroCountProb[count] >> num[count]; count++; - gobble(inNum); + m->gobble(inNum); } inNum.close(); @@ -515,7 +553,7 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string wordGenusProb[kmer][name] = prob; } - gobble(in); + m->gobble(in); } in.close(); @@ -527,6 +565,61 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string } } /**************************************************************************************************/ +bool Bayesian::checkReleaseDate(ifstream& file1, ifstream& file2, ifstream& file3, ifstream& file4) { + try { + + bool good = true; + + vector lines; + lines.push_back(m->getline(file1)); + lines.push_back(m->getline(file2)); + lines.push_back(m->getline(file3)); + lines.push_back(m->getline(file4)); + + //before we added this check + if ((lines[0][0] != '#') || (lines[1][0] != '#') || (lines[2][0] != '#') || (lines[3][0] != '#')) { good = false; } + else { + //rip off # + for (int i = 0; i < lines.size(); i++) { lines[i] = lines[i].substr(1); } + + //get mothurs current version + string version = m->getVersion(); + + vector versionVector; + m->splitAtChar(version, versionVector, '.'); + + //check each files version + for (int i = 0; i < lines.size(); i++) { + vector linesVector; + m->splitAtChar(lines[i], linesVector, '.'); + + if (versionVector.size() != linesVector.size()) { good = false; break; } + else { + for (int j = 0; j < versionVector.size(); j++) { + int num1, num2; + convert(versionVector[j], num1); + convert(linesVector[j], num2); + + //if mothurs version is newer than this files version, then we want to remake it + if (num1 > num2) { good = false; break; } + } + } + + if (!good) { break; } + } + } + + if (!good) { file1.close(); file2.close(); file3.close(); file4.close(); } + else { file1.seekg(0); file2.seekg(0); file3.seekg(0); file4.seekg(0); } + + return good; + } + catch(exception& e) { + m->errorOut(e, "Bayesian", "checkReleaseDate"); + exit(1); + } +} +/**************************************************************************************************/