#include "bayesian.h"
#include "kmer.hpp"
#include "phylosummary.h"
-
+#include "referencedb.h"
/**************************************************************************************************/
Bayesian::Bayesian(string tfile, string tempFile, string method, int ksize, int cutoff, int i) :
Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i) {
try {
+ ReferenceDB* rdb = ReferenceDB::getInstance();
+
+ string baseName = tempFile;
+ if (baseName == "saved") { baseName = rdb->getSavedReference(); }
+
+ string baseTName = tfile;
+ if (baseTName == "saved") { baseTName = rdb->getSavedTaxonomy(); }
/************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));
+ string tfileroot = baseTName.substr(0,baseTName.find_last_of(".")+1);
+ string tempfileroot = m->getRootName(m->getSimpleName(baseName));
string phyloTreeName = tfileroot + "tree.train";
string phyloTreeSumName = tfileroot + "tree.sum";
string probFileName = tfileroot + tempfileroot + char('0'+ kmerSize) + "mer.prob";
FilesGood = checkReleaseDate(probFileTest, probFileTest2, phyloTreeTest, probFileTest3);
}
+ //if you want to save, but you dont need to calculate then just read
+ if (rdb->save && probFileTest && probFileTest2 && phyloTreeTest && probFileTest3 && FilesGood) {
+ ifstream saveIn;
+ m->openInputFile(tempFile, saveIn);
+
+ while (!saveIn.eof()) {
+ Sequence temp(saveIn);
+ m->gobble(saveIn);
+
+ rdb->referenceSeqs.push_back(temp);
+ }
+ saveIn.close();
+ }
+
if(probFileTest && probFileTest2 && phyloTreeTest && probFileTest3 && FilesGood){
+ if (tempFile == "saved") { m->mothurOutEndLine(); m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory."); m->mothurOutEndLine(); }
+
m->mothurOut("Reading template taxonomy... "); cout.flush();
phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
genusNodes = phyloTree->getGenusNodes();
genusTotals = phyloTree->getGenusTotals();
-
- m->mothurOut("Reading template probabilities... "); cout.flush();
- readProbFile(probFileTest, probFileTest2, probFileName, probFileName2);
+ if (tfile == "saved") {
+ m->mothurOutEndLine(); m->mothurOut("Using probabilties from " + rdb->getSavedTaxonomy() + " that are saved in memory... "); cout.flush();;
+ wordGenusProb = rdb->wordGenusProb;
+ }else {
+ m->mothurOut("Reading template probabilities... "); cout.flush();
+ readProbFile(probFileTest, probFileTest2, probFileName, probFileName2);
+ }
+
+ //save probabilities
+ if (rdb->save) { rdb->wordGenusProb = wordGenusProb; }
}else{
//create search database and names vector
generateDatabaseAndNames(tfile, tempFile, method, ksize, 0.0, 0.0, 0.0, 0.0);
//prevents errors caused by creating shortcut files if you had an error in the sanity check.
- if (m->control_pressed) { remove(phyloTreeName.c_str()); remove(probFileName.c_str()); remove(probFileName2.c_str()); }
+ if (m->control_pressed) { m->mothurRemove(phyloTreeName); m->mothurRemove(probFileName); m->mothurRemove(probFileName2); }
else{
genusNodes = phyloTree->getGenusNodes();
genusTotals = phyloTree->getGenusTotals();
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;
delete phyloTree;
phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
+
+ //save probabilities
+ if (rdb->save) { rdb->wordGenusProb = wordGenusProb; }
}
}
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);
}
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"; }
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;
}
}
-// cout << phyloTree->get(indexofGenus).name << '\t' << maxProbability << endl;
+
+
return indexofGenus;
}
catch(exception& e) {