]> git.donarmstrong.com Git - mothur.git/blobdiff - bayesian.cpp
1.18.0
[mothur.git] / bayesian.cpp
index e596f672ed45f8f9f977e5264417e6781b959c38..9ae0777912ba21ad231540eed5658c09298b4ece 100644 (file)
 Bayesian::Bayesian(string tfile, string tempFile, string method, int ksize, int cutoff, int i) : 
 Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
        try {
-                                       
+               
                /************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";
@@ -76,9 +76,9 @@ 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;
                                
@@ -90,14 +90,14 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
                                #endif
 
                                
-                               openOutputFile(probFileName, out);
+                               m->openOutputFile(probFileName, out);
                                
                                //output mothur version
                                out << "#" << m->getVersion() << endl;
                                
                                out << numKmers << endl;
                                
-                               openOutputFile(probFileName2, out2);
+                               m->openOutputFile(probFileName2, out2);
                                
                                //output mothur version
                                out2 << "#" << m->getVersion() << endl;
@@ -205,6 +205,7 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
 /**************************************************************************************************/
 Bayesian::~Bayesian() {
        try {
+               
                 delete phyloTree; 
                 if (database != NULL) {  delete database; }
        }
@@ -223,7 +224,7 @@ string Bayesian::getTaxonomy(Sequence* seq) {
                //get words contained in query
                //getKmerString returns a string where the index in the string is hte kmer number 
                //and the character at that index can be converted to be the number of times that kmer was seen
-
+               
                string queryKmerString = kmer.getKmerString(seq->getUnaligned()); 
 
                vector<int> queryKmers;
@@ -235,14 +236,16 @@ string Bayesian::getTaxonomy(Sequence* seq) {
                
                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);
-                                               
+                               
                return tax;     
        }
        catch(exception& e) {
@@ -255,6 +258,17 @@ string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
        try {
                                
                map<int, int> confidenceScores; 
+               
+               //initialize confidences to 0 
+               int seqIndex = tax;
+               TaxNode seq = phyloTree->get(tax);
+               confidenceScores[tax] = 0;
+               
+               while (seq.level != 0) { //while you are not at the root
+                       seqIndex = seq.parent;
+                       confidenceScores[seqIndex] = 0;
+                       seq = phyloTree->get(seq.parent);
+               }
                                
                map<int, int>::iterator itBoot;
                map<int, int>::iterator itBoot2;
@@ -264,7 +278,6 @@ string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
                        if (m->control_pressed) { return "control"; }
                        
                        vector<int> temp;
-                                               
                        for (int j = 0; j < numToSelect; j++) {
                                int index = int(rand() % kmers.size());
                                
@@ -274,17 +287,15 @@ string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
                        
                        //get taxonomy
                        int newTax = getMostProbableTaxonomy(temp);
+                       //int newTax = 1;
                        TaxNode taxonomyTemp = phyloTree->get(newTax);
-       
+                       
                        //add to confidence results
                        while (taxonomyTemp.level != 0) { //while you are not at the root
-                               
                                itBoot2 = confidenceScores.find(newTax); //is this a classification we already have a count on
                                
-                               if (itBoot2 == confidenceScores.end()) { //not already in confidence scores
-                                       confidenceScores[newTax] = 1;
-                               }else{
-                                       confidenceScores[newTax]++;
+                               if (itBoot2 != confidenceScores.end()) { //this is a classification we need a confidence for
+                                       (itBoot2->second)++;
                                }
                                
                                newTax = taxonomyTemp.parent;
@@ -305,7 +316,7 @@ string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
                                
                                int confidence = 0;
                                if (itBoot2 != confidenceScores.end()) { //already in confidence scores
-                                       confidence = confidenceScores[seqTaxIndex];
+                                       confidence = itBoot2->second;
                                }
                                
                                if (((confidence/(float)iters) * 100) >= confidenceThreshold) {
@@ -339,14 +350,14 @@ int Bayesian::getMostProbableTaxonomy(vector<int> queryKmer) {
                        for (int i = 0; i < queryKmer.size(); i++) {
                                prob += wordGenusProb[queryKmer[i]][k];
                        }
-               
+                       
                        //is this the taxonomy with the greatest probability?
                        if (prob > maxProbability) { 
                                indexofGenus = genusNodes[k];
                                maxProbability = prob;
                        }
                }
-
+//             cout << phyloTree->get(indexofGenus).name << '\t' << maxProbability << endl;
                return indexofGenus;
        }
        catch(exception& e) {
@@ -408,8 +419,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);
@@ -515,9 +526,9 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string
                        MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
                #else
                        //read version
-                       string line = getline(in); gobble(in);
+                       string line = m->getline(in); m->gobble(in);
                        
-                       in >> numKmers; gobble(in);
+                       in >> numKmers; m->gobble(in);
                        
                        //initialze probabilities
                        wordGenusProb.resize(numKmers);
@@ -530,12 +541,12 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string
                        vector<float> zeroCountProb; zeroCountProb.resize(numKmers);            
                        
                        //read version
-                       string line2 = getline(inNum); gobble(inNum);
+                       string line2 = m->getline(inNum); m->gobble(inNum);
                        
                        while (inNum) {
                                inNum >> zeroCountProb[count] >> num[count];  
                                count++;
-                               gobble(inNum);
+                               m->gobble(inNum);
                        }
                        inNum.close();
                
@@ -553,7 +564,7 @@ void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string
                                        wordGenusProb[kmer][name] = prob;
                                }
                                
-                               gobble(in);
+                               m->gobble(in);
                        }
                        in.close();
                        
@@ -571,10 +582,10 @@ bool Bayesian::checkReleaseDate(ifstream& file1, ifstream& file2, ifstream& file
                bool good = true;
                
                vector<string> lines;
-               lines.push_back(getline(file1));  
-               lines.push_back(getline(file2)); 
-               lines.push_back(getline(file3)); 
-               lines.push_back(getline(file4)); 
+               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;  }
@@ -586,12 +597,12 @@ bool Bayesian::checkReleaseDate(ifstream& file1, ifstream& file2, ifstream& file
                        string version = m->getVersion();
                        
                        vector<string> versionVector;
-                       splitAtChar(version, versionVector, '.');
+                       m->splitAtChar(version, versionVector, '.');
                        
                        //check each files version
                        for (int i = 0; i < lines.size(); i++) { 
                                vector<string> linesVector;
-                               splitAtChar(lines[i], linesVector, '.');
+                               m->splitAtChar(lines[i], linesVector, '.');
                        
                                if (versionVector.size() != linesVector.size()) { good = false; break; }
                                else {