]> git.donarmstrong.com Git - mothur.git/blobdiff - bayesian.cpp
paralellized the indicator command
[mothur.git] / bayesian.cpp
index 80518666cfe5436280f58117fe506dc132bd2754..cf55d5c585ee2c7c2ca6534f7192996f2ef34d02 100644 (file)
@@ -15,7 +15,7 @@
 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 = m->getRootName(m->getSimpleName(tempFile));
@@ -76,9 +76,9 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
                        
                                //initialze probabilities
                                wordGenusProb.resize(numKmers);
-                       cout << numKmers << '\t' << genusNodes.size() << endl;
+                       //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;  
+                       //cout << numKmers << '\t' << genusNodes.size() << endl;        
                                ofstream out;
                                ofstream out2;
                                
@@ -134,13 +134,16 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
                                                count[temp]++;  //increment count of seq in this genus who have this word
                                        }
                                        
-                                       //probabilityInTemplate = (# of seqs with that word in template + 0.05) / (total number of seqs in template + 1);
+                                       //probabilityInTemplate = (# of seqs with that word in template + 0.50) / (total number of seqs in template + 1);
                                        float probabilityInTemplate = (seqsWithWordi.size() + 0.50) / (float) (names.size() + 1);
                                        
                                        int numNotZero = 0;
                                        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) { 
                                                        #ifdef USE_MPI
                                                                int pid;
@@ -205,6 +208,7 @@ Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
 /**************************************************************************************************/
 Bayesian::~Bayesian() {
        try {
+               
                 delete phyloTree; 
                 if (database != NULL) {  delete database; }
        }
@@ -223,11 +227,11 @@ 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;
-               for (int i = 0; i < queryKmerString.length(); i++) {
+               for (int i = 0; i < queryKmerString.length()-1; i++) {  // the -1 is to ignore any kmer with an N in it
                        if (queryKmerString[i] != '!') { //this kmer is in the query
                                queryKmers.push_back(i);
                        }
@@ -235,14 +239,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,16 +261,26 @@ 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;
                map<int, int>::iterator itConvert;
-               
+                       
                for (int i = 0; i < iters; i++) {
                        if (m->control_pressed) { return "control"; }
                        
                        vector<int> temp;
-                                               
                        for (int j = 0; j < numToSelect; j++) {
                                int index = int(rand() % kmers.size());
                                
@@ -274,17 +290,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 +319,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) {
@@ -333,20 +347,29 @@ int Bayesian::getMostProbableTaxonomy(vector<int> queryKmer) {
                
                double maxProbability = -1000000.0;
                //find taxonomy with highest probability that this sequence is from it
+               
+               
+//             cout << genusNodes.size() << endl;
+               
+               
                for (int k = 0; k < genusNodes.size(); k++) {
                        //for each taxonomy calc its probability
-                       double prob = 1.0;
+                       
+                       double prob = 0.0000;
                        for (int i = 0; i < queryKmer.size(); i++) {
                                prob += wordGenusProb[queryKmer[i]][k];
                        }
-               
+                       
+//                     cout << phyloTree->get(genusNodes[k]).name << '\t' << prob << endl;
+
                        //is this the taxonomy with the greatest probability?
                        if (prob > maxProbability) { 
                                indexofGenus = genusNodes[k];
                                maxProbability = prob;
                        }
                }
-
+               
+                       
                return indexofGenus;
        }
        catch(exception& e) {