#include "bayesian.h"
#include "kmer.hpp"
+#include "phylosummary.h"
/**************************************************************************************************/
Bayesian::Bayesian(string tfile, string tempFile, string method, int ksize, int cutoff, int i) :
-Classify(tfile, tempFile, method, ksize, 0.0, 0.0, 0.0, 0.0), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) {
+Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) {
try {
- numKmers = database->getMaxKmer() + 1;
-
- //initialze probabilities
- wordGenusProb.resize(numKmers);
-
- genusNodes = phyloTree->getGenusNodes();
-
- for (int j = 0; j < wordGenusProb.size(); j++) { wordGenusProb[j].resize(genusNodes.size()); }
-
- //reset counts because we are on a new word
- for (int j = 0; j < genusNodes.size(); j++) {
- TaxNode temp = phyloTree->get(genusNodes[j]);
- genusTotals.push_back(temp.accessions.size());
- }
-
-
/************calculate the probablity that each word will be in a specific taxonomy*************/
- ofstream out;
+ string phyloTreeName = tfile.substr(0,tfile.find_last_of(".")+1) + "tree.train";
string probFileName = tfile.substr(0,tfile.find_last_of(".")+1) + tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer.prob";
- ifstream probFileTest(probFileName.c_str());
+ string probFileName2 = tfile.substr(0,tfile.find_last_of(".")+1) + tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer.numNonZero";
+ ofstream out;
ofstream out2;
- string probFileName2 = tfile.substr(0,tfile.find_last_of(".")+1) + tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer.numNonZero";
+
+ ifstream phyloTreeTest(phyloTreeName.c_str());
ifstream probFileTest2(probFileName2.c_str());
+ ifstream probFileTest(probFileName.c_str());
int start = time(NULL);
- if(probFileTest && probFileTest2){
+ if(probFileTest && probFileTest2 && phyloTreeTest){
+ m->mothurOut("Reading template taxonomy... "); cout.flush();
+
+ phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
+
+ m->mothurOut("DONE."); m->mothurOutEndLine();
+
+ genusNodes = phyloTree->getGenusNodes();
+ genusTotals = phyloTree->getGenusTotals();
+
m->mothurOut("Reading template probabilities... "); cout.flush();
- readProbFile(probFileTest, probFileTest2);
+ readProbFile(probFileTest, probFileTest2, probFileName, probFileName2);
+
}else{
+
+ //create search database and names vector
+ generateDatabaseAndNames(tfile, tempFile, method, ksize, 0.0, 0.0, 0.0, 0.0);
+
+ genusNodes = phyloTree->getGenusNodes();
+ genusTotals = phyloTree->getGenusTotals();
+
+ m->mothurOut("Calculating template taxonomy tree... "); cout.flush();
+
+ phyloTree->printTreeNodes(phyloTreeName);
+
+ m->mothurOut("DONE."); m->mothurOutEndLine();
+
m->mothurOut("Calculating template probabilities... "); cout.flush();
+
+ numKmers = database->getMaxKmer() + 1;
+
+ //initialze probabilities
+ wordGenusProb.resize(numKmers);
+
+ for (int j = 0; j < wordGenusProb.size(); j++) { wordGenusProb[j].resize(genusNodes.size()); }
+
+
+ #ifdef USE_MPI
+ int pid;
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+
+ if (pid == 0) {
+ #endif
ofstream out;
openOutputFile(probFileName, out);
+ out << numKmers << endl;
+
ofstream out2;
openOutputFile(probFileName2, out2);
+ #ifdef USE_MPI
+ }
+ #endif
+
+
//for each word
for (int i = 0; i < numKmers; i++) {
if (m->control_pressed) { break; }
+ #ifdef USE_MPI
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+
+ if (pid == 0) {
+ #endif
+
out << i << '\t';
+ #ifdef USE_MPI
+ }
+ #endif
+
vector<int> seqsWithWordi = database->getSequencesWithKmer(i);
map<int, int> count;
for (int k = 0; k < genusNodes.size(); k++) {
//probabilityInThisTaxonomy = (# of seqs with that word in this taxonomy + probabilityInTemplate) / (total number of seqs in this taxonomy + 1);
wordGenusProb[i][k] = log((count[genusNodes[k]] + probabilityInTemplate) / (float) (genusTotals[k] + 1));
- if (count[genusNodes[k]] != 0) { out << k << '\t' << wordGenusProb[i][k] << '\t'; numNotZero++; }
+ if (count[genusNodes[k]] != 0) {
+ #ifdef USE_MPI
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+ if (pid == 0) {
+ #endif
+
+ out << k << '\t' << wordGenusProb[i][k] << '\t';
+
+ #ifdef USE_MPI
+ }
+ #endif
+
+ numNotZero++;
+ }
}
+
+ #ifdef USE_MPI
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+ if (pid == 0) {
+ #endif
+
out << endl;
out2 << probabilityInTemplate << '\t' << numNotZero << endl;
+
+ #ifdef USE_MPI
+ }
+ #endif
}
+ #ifdef USE_MPI
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+ if (pid == 0) {
+ #endif
+
out.close();
out2.close();
+
+ #ifdef USE_MPI
+ }
+ #endif
+
+ //read in new phylotree with less info. - its faster
+ ifstream phyloTreeTest(phyloTreeName.c_str());
+ delete phyloTree;
+
+ phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
}
-
-
+
m->mothurOut("DONE."); m->mothurOutEndLine();
m->mothurOut("It took " + toString(time(NULL) - start) + " seconds get probabilities. "); m->mothurOutEndLine();
}
exit(1);
}
}
+/**************************************************************************************************/
+Bayesian::~Bayesian() {
+ try {
+ delete phyloTree;
+ if (database != NULL) { delete database; }
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Bayesian", "~Bayesian");
+ exit(1);
+ }
+}
+
/**************************************************************************************************/
string Bayesian::getTaxonomy(Sequence* seq) {
try {
for (int i = 0; i < queryKmerString.length(); i++) {
if (queryKmerString[i] != '!') { //this kmer is in the query
queryKmers.push_back(i);
-
}
}
if (queryKmers.size() == 0) { m->mothurOut(seq->getName() + "is bad."); m->mothurOutEndLine(); return "bad seq"; }
int index = getMostProbableTaxonomy(queryKmers);
-
if (m->control_pressed) { return tax; }
-
+//cout << seq->getName() << '\t' << index << endl;
//bootstrap - to set confidenceScore
int numToSelect = queryKmers.size() / 8;
tax = bootstrapResults(queryKmers, index, numToSelect);
/**************************************************************************************************/
string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
try {
-
- //taxConfidenceScore.clear(); //clear out previous seqs scores
- vector< map<string, int> > confidenceScores; //you need the added vector level of confusion to account for the level that name is seen since they can be the same
- //map of classification to confidence for all areas seen
- //ie. Bacteria;Alphaproteobacteria;Rhizobiales;Azorhizobium_et_rel.;Methylobacterium_et_rel.;Bosea;
- //ie. Bacteria -> 100, Alphaproteobacteria -> 100, Rhizobiales -> 87, Azorhizobium_et_rel. -> 78, Methylobacterium_et_rel. -> 70, Bosea -> 50
- confidenceScores.resize(100); //if you have more than 100 levels of classification...
-
- map<string, int>::iterator itBoot;
- map<string, int>::iterator itBoot2;
+ map<int, int> confidenceScores;
+
+ map<int, int>::iterator itBoot;
+ map<int, int>::iterator itBoot2;
map<int, int>::iterator itConvert;
for (int i = 0; i < iters; i++) {
//get taxonomy
int newTax = getMostProbableTaxonomy(temp);
- TaxNode taxonomy = phyloTree->get(newTax);
-
+ TaxNode taxonomyTemp = phyloTree->get(newTax);
+
//add to confidence results
- while (taxonomy.level != 0) { //while you are not at the root
+ while (taxonomyTemp.level != 0) { //while you are not at the root
- itBoot2 = confidenceScores[taxonomy.level].find(taxonomy.name); //is this a classification we already have a count on
+ itBoot2 = confidenceScores.find(newTax); //is this a classification we already have a count on
- if (itBoot2 == confidenceScores[taxonomy.level].end()) { //not already in confidence scores
- confidenceScores[taxonomy.level][taxonomy.name] = 1;
+ if (itBoot2 == confidenceScores.end()) { //not already in confidence scores
+ confidenceScores[newTax] = 1;
}else{
- confidenceScores[taxonomy.level][taxonomy.name]++;
+ confidenceScores[newTax]++;
}
-
- taxonomy = phyloTree->get(taxonomy.parent);
+
+ newTax = taxonomyTemp.parent;
+ taxonomyTemp = phyloTree->get(newTax);
}
}
string confidenceTax = "";
simpleTax = "";
+
+ int seqTaxIndex = tax;
TaxNode seqTax = phyloTree->get(tax);
while (seqTax.level != 0) { //while you are not at the root
- itBoot2 = confidenceScores[seqTax.level].find(seqTax.name); //is this a classification we already have a count on
+ itBoot2 = confidenceScores.find(seqTaxIndex); //is this a classification we already have a count on
int confidence = 0;
- if (itBoot2 != confidenceScores[seqTax.level].end()) { //not already in confidence scores
- confidence = confidenceScores[seqTax.level][seqTax.name];
+ if (itBoot2 != confidenceScores.end()) { //already in confidence scores
+ confidence = confidenceScores[seqTaxIndex];
}
- if (confidence >= confidenceThreshold) {
+ if (((confidence/(float)iters) * 100) >= confidenceThreshold) {
confidenceTax = seqTax.name + "(" + toString(((confidence/(float)iters) * 100)) + ");" + confidenceTax;
simpleTax = seqTax.name + ";" + simpleTax;
}
+ seqTaxIndex = seqTax.parent;
seqTax = phyloTree->get(seqTax.parent);
}
}
}
/**************************************************************************************************/
-void Bayesian::readProbFile(ifstream& in, ifstream& inNum) {
+void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string inNumName) {
try{
- int kmer, name, count; count = 0;
- vector<int> num; num.resize(numKmers);
- float prob;
- vector<float> zeroCountProb; zeroCountProb.resize(numKmers);
-
- while (inNum) {
- inNum >> zeroCountProb[count] >> num[count];
- count++;
- gobble(inNum);
- }
- inNum.close();
+ #ifdef USE_MPI
+
+ int pid, num, num2, processors;
+ vector<long> positions;
+ vector<long> positions2;
+
+ MPI_Status status;
+ MPI_File inMPI;
+ MPI_File inMPI2;
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
+ MPI_Comm_size(MPI_COMM_WORLD, &processors);
+ int tag = 2001;
+
+ char inFileName[1024];
+ strcpy(inFileName, inNumName.c_str());
+
+ char inFileName2[1024];
+ strcpy(inFileName2, inName.c_str());
+
+ MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
+ 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);
+
+ 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);
+
+ MPI_Send(&num2, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
+ MPI_Send(&positions2[0], (num2+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);
+
+ MPI_Recv(&num2, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
+ positions2.resize(num2+1);
+ MPI_Recv(&positions2[0], (num2+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
+ }
- while(in) {
- in >> kmer;
+ //read numKmers
+ int length = positions2[1] - positions2[0];
+ char* buf = new char[length];
+
+ MPI_File_read_at(inMPI2, positions2[0], buf, length, MPI_CHAR, &status);
+
+ string tempBuf = buf;
+ if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
+ delete buf;
+
+ istringstream iss (tempBuf,istringstream::in);
+ iss >> numKmers;
+
+ //initialze probabilities
+ wordGenusProb.resize(numKmers);
- //set them all to zero value
- for (int i = 0; i < genusNodes.size(); i++) {
- wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
+ for (int j = 0; j < wordGenusProb.size(); j++) { wordGenusProb[j].resize(genusNodes.size()); }
+
+ int kmer, name;
+ vector<int> numbers; numbers.resize(numKmers);
+ float prob;
+ vector<float> zeroCountProb; zeroCountProb.resize(numKmers);
+
+ //read file
+ for(int i=0;i<num;i++){
+ //read next sequence
+ length = positions[i+1] - positions[i];
+ char* buf4 = new char[length];
+
+ MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
+
+ tempBuf = buf4;
+ if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
+ delete buf4;
+
+ istringstream iss (tempBuf,istringstream::in);
+ iss >> zeroCountProb[i] >> numbers[i];
}
- //get probs for nonzero values
- for (int i = 0; i < num[kmer]; i++) {
- in >> name >> prob;
- wordGenusProb[kmer][name] = prob;
+ MPI_File_close(&inMPI);
+
+ for(int i=1;i<num2;i++){
+ //read next sequence
+ length = positions2[i+1] - positions2[i];
+ char* buf4 = new char[length];
+
+ MPI_File_read_at(inMPI2, positions2[i], buf4, length, MPI_CHAR, &status);
+
+ tempBuf = buf4;
+ if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
+ delete buf4;
+
+ istringstream iss (tempBuf,istringstream::in);
+
+ iss >> kmer;
+
+ //set them all to zero value
+ for (int i = 0; i < genusNodes.size(); i++) {
+ wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
+ }
+
+ //get probs for nonzero values
+ for (int i = 0; i < numbers[kmer]; i++) {
+ iss >> name >> prob;
+ wordGenusProb[kmer][name] = prob;
+ }
+
}
+ MPI_File_close(&inMPI2);
+ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
+ #else
+
+ in >> numKmers; gobble(in);
- gobble(in);
- }
- in.close();
+ //initialze probabilities
+ wordGenusProb.resize(numKmers);
+
+ for (int j = 0; j < wordGenusProb.size(); j++) { wordGenusProb[j].resize(genusNodes.size()); }
+
+ int kmer, name, count; count = 0;
+ vector<int> num; num.resize(numKmers);
+ float prob;
+ vector<float> zeroCountProb; zeroCountProb.resize(numKmers);
+
+ while (inNum) {
+ inNum >> zeroCountProb[count] >> num[count];
+ count++;
+ gobble(inNum);
+ }
+ inNum.close();
+
+ while(in) {
+ in >> kmer;
+
+ //set them all to zero value
+ for (int i = 0; i < genusNodes.size(); i++) {
+ wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
+ }
+
+ //get probs for nonzero values
+ for (int i = 0; i < num[kmer]; i++) {
+ in >> name >> prob;
+ wordGenusProb[kmer][name] = prob;
+ }
+
+ gobble(in);
+ }
+ in.close();
+
+ #endif
}
catch(exception& e) {
m->errorOut(e, "Bayesian", "readProbFile");