]> git.donarmstrong.com Git - mothur.git/blob - bayesian.cpp
*** empty log message ***
[mothur.git] / bayesian.cpp
1 /*
2  *  bayesian.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/3/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "bayesian.h"
11 #include "kmer.hpp"
12 #include "phylosummary.h"
13 #include "referencedb.h"
14 /**************************************************************************************************/
15 Bayesian::Bayesian(string tfile, string tempFile, string method, int ksize, int cutoff, int i, int tid) : 
16 Classify(), kmerSize(ksize), confidenceThreshold(cutoff), iters(i)  {
17         try {
18                 ReferenceDB* rdb = ReferenceDB::getInstance();
19                 
20                 threadID = tid;
21                 string baseName = tempFile;
22                         
23                 if (baseName == "saved") { baseName = rdb->getSavedReference(); }
24                 
25                 string baseTName = tfile;
26                 if (baseTName == "saved") { baseTName = rdb->getSavedTaxonomy(); }
27                 
28                 /************calculate the probablity that each word will be in a specific taxonomy*************/
29                 string tfileroot = baseTName.substr(0,baseTName.find_last_of(".")+1);
30                 string tempfileroot = m->getRootName(m->getSimpleName(baseName));
31                 string phyloTreeName = tfileroot + "tree.train";
32                 string phyloTreeSumName = tfileroot + "tree.sum";
33                 string probFileName = tfileroot + tempfileroot + char('0'+ kmerSize) + "mer.prob";
34                 string probFileName2 = tfileroot + tempfileroot + char('0'+ kmerSize) + "mer.numNonZero";
35                 
36                 ofstream out;
37                 ofstream out2;
38                 
39                 ifstream phyloTreeTest(phyloTreeName.c_str());
40                 ifstream probFileTest2(probFileName2.c_str());
41                 ifstream probFileTest(probFileName.c_str());
42                 ifstream probFileTest3(phyloTreeSumName.c_str());
43                 
44                 int start = time(NULL);
45                 
46                 //if they are there make sure they were created after this release date
47                 bool FilesGood = false;
48                 if(probFileTest && probFileTest2 && phyloTreeTest && probFileTest3){
49                         FilesGood = checkReleaseDate(probFileTest, probFileTest2, phyloTreeTest, probFileTest3);
50                 }
51                 
52                 //if you want to save, but you dont need to calculate then just read
53                 if (rdb->save && probFileTest && probFileTest2 && phyloTreeTest && probFileTest3 && FilesGood && (tempFile != "saved")) {  
54                         ifstream saveIn;
55                         m->openInputFile(tempFile, saveIn);
56                         
57                         while (!saveIn.eof()) {
58                                 Sequence temp(saveIn);
59                                 m->gobble(saveIn);
60                                 
61                                 rdb->referenceSeqs.push_back(temp); 
62                         }
63                         saveIn.close();                 
64                 }
65                 
66                 if(probFileTest && probFileTest2 && phyloTreeTest && probFileTest3 && FilesGood){       
67                         if (tempFile == "saved") { m->mothurOutEndLine();  m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory.");     m->mothurOutEndLine(); }
68                         
69                         m->mothurOut("Reading template taxonomy...     "); cout.flush();
70                         
71                         phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
72                         
73                         m->mothurOut("DONE."); m->mothurOutEndLine();
74                         
75                         genusNodes = phyloTree->getGenusNodes(); 
76                         genusTotals = phyloTree->getGenusTotals();
77                         
78                         if (tfile == "saved") { 
79                                 m->mothurOutEndLine();  m->mothurOut("Using probabilties from " + rdb->getSavedTaxonomy() + " that are saved in memory...    ");        cout.flush();; 
80                                 wordGenusProb = rdb->wordGenusProb;
81                         }else {
82                                 m->mothurOut("Reading template probabilities...     "); cout.flush();
83                                 readProbFile(probFileTest, probFileTest2, probFileName, probFileName2);
84                         }       
85                         
86                         //save probabilities
87                         if (rdb->save) { rdb->wordGenusProb = wordGenusProb; }
88                 }else{
89                 
90                         //create search database and names vector
91                         generateDatabaseAndNames(tfile, tempFile, method, ksize, 0.0, 0.0, 0.0, 0.0);
92                         
93                         //prevents errors caused by creating shortcut files if you had an error in the sanity check.
94                         if (m->control_pressed) {  m->mothurRemove(phyloTreeName);  m->mothurRemove(probFileName); m->mothurRemove(probFileName2); }
95                         else{ 
96                                 genusNodes = phyloTree->getGenusNodes(); 
97                                 genusTotals = phyloTree->getGenusTotals();
98                                 
99                                 m->mothurOut("Calculating template taxonomy tree...     "); cout.flush();
100                                 
101                                 phyloTree->printTreeNodes(phyloTreeName);
102                                                         
103                                 m->mothurOut("DONE."); m->mothurOutEndLine();
104                                 
105                                 m->mothurOut("Calculating template probabilities...     "); cout.flush();
106                                 
107                                 numKmers = database->getMaxKmer() + 1;
108                         
109                                 //initialze probabilities
110                                 wordGenusProb.resize(numKmers);
111                         //cout << numKmers << '\t' << genusNodes.size() << endl;
112                                 for (int j = 0; j < wordGenusProb.size(); j++) {        wordGenusProb[j].resize(genusNodes.size());             }
113                         //cout << numKmers << '\t' << genusNodes.size() << endl;        
114                                 //ofstream out;
115                                 //ofstream out2;
116                                 
117                                 #ifdef USE_MPI
118                                         int pid;
119                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
120
121                                         if (pid == 0) {  
122                                 #endif
123
124                                 
125                                 //m->openOutputFile(probFileName, out);
126                                 
127                                 //output mothur version
128                                 //out << "#" << m->getVersion() << endl;
129                                 
130                                 //out << numKmers << endl;
131                                 
132                                 //m->openOutputFile(probFileName2, out2);
133                                 
134                                 //output mothur version
135                                 //out2 << "#" << m->getVersion() << endl;
136                                 
137                                 #ifdef USE_MPI
138                                         }
139                                 #endif
140
141                                 
142                                 //for each word
143                                 for (int i = 0; i < numKmers; i++) {
144                                         if (m->control_pressed) {  break; }
145                                         
146                                         #ifdef USE_MPI
147                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
148
149                                                 if (pid == 0) {  
150                                         #endif
151
152                                         //out << i << '\t';
153                                         
154                                         #ifdef USE_MPI
155                                                 }
156                                         #endif
157                                         
158                                         vector<int> seqsWithWordi = database->getSequencesWithKmer(i);
159                                         
160                                         map<int, int> count;
161                                         for (int k = 0; k < genusNodes.size(); k++) {  count[genusNodes[k]] = 0;  }                     
162                                                         
163                                         //for each sequence with that word
164                                         for (int j = 0; j < seqsWithWordi.size(); j++) {
165                                                 int temp = phyloTree->getIndex(names[seqsWithWordi[j]]);
166                                                 count[temp]++;  //increment count of seq in this genus who have this word
167                                         }
168                                         
169                                         //probabilityInTemplate = (# of seqs with that word in template + 0.50) / (total number of seqs in template + 1);
170                                         float probabilityInTemplate = (seqsWithWordi.size() + 0.50) / (float) (names.size() + 1);
171                                         
172                                         int numNotZero = 0;
173                                         for (int k = 0; k < genusNodes.size(); k++) {
174                                                 //probabilityInThisTaxonomy = (# of seqs with that word in this taxonomy + probabilityInTemplate) / (total number of seqs in this taxonomy + 1);
175                                                 
176                                                 
177                                                 wordGenusProb[i][k] = log((count[genusNodes[k]] + probabilityInTemplate) / (float) (genusTotals[k] + 1));  
178                                                                         
179                                                 if (count[genusNodes[k]] != 0) { 
180                                                         #ifdef USE_MPI
181                                                                 int pid;
182                                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
183                                                 
184                                                                 if (pid == 0) {  
185                                                         #endif
186
187                                                         //out << k << '\t' << wordGenusProb[i][k] << '\t'; 
188                                                         
189                                                         #ifdef USE_MPI
190                                                                 }
191                                                         #endif
192
193                                                         numNotZero++;  
194                                                 }
195                                         }
196                                         
197                                         #ifdef USE_MPI
198                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
199                                 
200                                                 if (pid == 0) {  
201                                         #endif
202                                         
203                                         //out << endl;
204                                         //out2 << probabilityInTemplate << '\t' << numNotZero << endl;
205                                         
206                                         #ifdef USE_MPI
207                                                 }
208                                         #endif
209                                 }
210                                 
211                                 #ifdef USE_MPI
212                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
213                                 
214                                         if (pid == 0) {  
215                                 #endif
216                                 
217                                 //out.close();
218                                 //out2.close();
219                                 
220                                 #ifdef USE_MPI
221                                         }
222                                 #endif
223                                 
224                                 //read in new phylotree with less info. - its faster
225                                 ifstream phyloTreeTest(phyloTreeName.c_str());
226                                 delete phyloTree;
227                                 
228                                 phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
229                                 
230                                 //save probabilities
231                                 if (rdb->save) { rdb->wordGenusProb = wordGenusProb; }
232                         }
233                 }
234         
235                 m->mothurOut("DONE."); m->mothurOutEndLine();
236                 m->mothurOut("It took " + toString(time(NULL) - start) + " seconds get probabilities. "); m->mothurOutEndLine();
237         }
238         catch(exception& e) {
239                 m->errorOut(e, "Bayesian", "Bayesian");
240                 exit(1);
241         }
242 }
243 /**************************************************************************************************/
244 Bayesian::~Bayesian() {
245         try {
246                 
247                  delete phyloTree; 
248                  if (database != NULL) {  delete database; }
249         }
250         catch(exception& e) {
251                 m->errorOut(e, "Bayesian", "~Bayesian");
252                 exit(1);
253         }
254 }
255
256 /**************************************************************************************************/
257 string Bayesian::getTaxonomy(Sequence* seq) {
258         try {
259                 string tax = "";
260                 Kmer kmer(kmerSize);
261                 
262                 //get words contained in query
263                 //getKmerString returns a string where the index in the string is hte kmer number 
264                 //and the character at that index can be converted to be the number of times that kmer was seen
265                 
266                 string queryKmerString = kmer.getKmerString(seq->getUnaligned()); 
267
268                 vector<int> queryKmers;
269                 for (int i = 0; i < queryKmerString.length()-1; i++) {  // the -1 is to ignore any kmer with an N in it
270                         if (queryKmerString[i] != '!') { //this kmer is in the query
271                                 queryKmers.push_back(i);
272                         }
273                 }
274                 
275                 if (queryKmers.size() == 0) {  m->mothurOut(seq->getName() + "is bad."); m->mothurOutEndLine(); return "bad seq"; }
276                 
277                 
278                 int index = getMostProbableTaxonomy(queryKmers);
279                 
280                 if (m->control_pressed) { return tax; }
281                                         
282                 //bootstrap - to set confidenceScore
283                 int numToSelect = queryKmers.size() / 8;
284         
285                 tax = bootstrapResults(queryKmers, index, numToSelect);
286                                 
287                 return tax;     
288         }
289         catch(exception& e) {
290                 m->errorOut(e, "Bayesian", "getTaxonomy");
291                 exit(1);
292         }
293 }
294 /**************************************************************************************************/
295 string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
296         try {
297                                 
298                 map<int, int> confidenceScores; 
299                 
300                 //initialize confidences to 0 
301                 int seqIndex = tax;
302                 TaxNode seq = phyloTree->get(tax);
303                 confidenceScores[tax] = 0;
304                 
305                 while (seq.level != 0) { //while you are not at the root
306                         seqIndex = seq.parent;
307                         confidenceScores[seqIndex] = 0;
308                         seq = phyloTree->get(seq.parent);
309                 }
310                                 
311                 map<int, int>::iterator itBoot;
312                 map<int, int>::iterator itBoot2;
313                 map<int, int>::iterator itConvert;
314                         
315                 for (int i = 0; i < iters; i++) {
316                         if (m->control_pressed) { return "control"; }
317                         
318                         vector<int> temp;
319                         for (int j = 0; j < numToSelect; j++) {
320                                 int index = int(rand() % kmers.size());
321                                 
322                                 //add word to temp
323                                 temp.push_back(kmers[index]);
324                         }
325                         
326                         //get taxonomy
327                         int newTax = getMostProbableTaxonomy(temp);
328                         //int newTax = 1;
329                         TaxNode taxonomyTemp = phyloTree->get(newTax);
330                         
331                         //add to confidence results
332                         while (taxonomyTemp.level != 0) { //while you are not at the root
333                                 itBoot2 = confidenceScores.find(newTax); //is this a classification we already have a count on
334                                 
335                                 if (itBoot2 != confidenceScores.end()) { //this is a classification we need a confidence for
336                                         (itBoot2->second)++;
337                                 }
338                                 
339                                 newTax = taxonomyTemp.parent;
340                                 taxonomyTemp = phyloTree->get(newTax);
341                         }
342         
343                 }
344                 
345                 string confidenceTax = "";
346                 simpleTax = "";
347                 
348                 int seqTaxIndex = tax;
349                 TaxNode seqTax = phyloTree->get(tax);
350                 
351                 while (seqTax.level != 0) { //while you are not at the root
352                                         
353                                 itBoot2 = confidenceScores.find(seqTaxIndex); //is this a classification we already have a count on
354                                 
355                                 int confidence = 0;
356                                 if (itBoot2 != confidenceScores.end()) { //already in confidence scores
357                                         confidence = itBoot2->second;
358                                 }
359                                 
360                                 if (((confidence/(float)iters) * 100) >= confidenceThreshold) {
361                                         confidenceTax = seqTax.name + "(" + toString(((confidence/(float)iters) * 100)) + ");" + confidenceTax;
362                                         simpleTax = seqTax.name + ";" + simpleTax;
363                                 }
364                                 
365                                 seqTaxIndex = seqTax.parent;
366                                 seqTax = phyloTree->get(seqTax.parent);
367                 }
368                 
369                 if (confidenceTax == "") { confidenceTax = "unclassified;"; simpleTax = "unclassified;"; }
370                 return confidenceTax;
371                 
372         }
373         catch(exception& e) {
374                 m->errorOut(e, "Bayesian", "bootstrapResults");
375                 exit(1);
376         }
377 }
378 /**************************************************************************************************/
379 int Bayesian::getMostProbableTaxonomy(vector<int> queryKmer) {
380         try {
381                 int indexofGenus = 0;
382                 
383                 double maxProbability = -1000000.0;
384                 //find taxonomy with highest probability that this sequence is from it
385                 
386                 
387 //              cout << genusNodes.size() << endl;
388                 
389                 
390                 for (int k = 0; k < genusNodes.size(); k++) {
391                         //for each taxonomy calc its probability
392                         
393                         double prob = 0.0000;
394                         for (int i = 0; i < queryKmer.size(); i++) {
395                                 prob += wordGenusProb[queryKmer[i]][k];
396                         }
397                         
398 //                      cout << phyloTree->get(genusNodes[k]).name << '\t' << prob << endl;
399
400                         //is this the taxonomy with the greatest probability?
401                         if (prob > maxProbability) { 
402                                 indexofGenus = genusNodes[k];
403                                 maxProbability = prob;
404                         }
405                 }
406                 
407                         
408                 return indexofGenus;
409         }
410         catch(exception& e) {
411                 m->errorOut(e, "Bayesian", "getMostProbableTaxonomy");
412                 exit(1);
413         }
414 }
415 /*************************************************************************************************
416 map<string, int> Bayesian::parseTaxMap(string newTax) {
417         try{
418         
419                 map<string, int> parsed;
420                 
421                 newTax = newTax.substr(0, newTax.length()-1);  //get rid of last ';'
422         
423                 //parse taxonomy
424                 string individual;
425                 while (newTax.find_first_of(';') != -1) {
426                         individual = newTax.substr(0,newTax.find_first_of(';'));
427                         newTax = newTax.substr(newTax.find_first_of(';')+1, newTax.length());
428                         parsed[individual] = 1;
429                 }
430                 
431                 //get last one
432                 parsed[newTax] = 1;
433
434                 return parsed;
435                 
436         }
437         catch(exception& e) {
438                 m->errorOut(e, "Bayesian", "parseTax");
439                 exit(1);
440         }
441 }
442 /**************************************************************************************************/
443 void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string inNumName) {
444         try{
445                 
446                 #ifdef USE_MPI
447                         
448                         int pid, num, num2, processors;
449                         vector<unsigned long long> positions;
450                         vector<unsigned long long> positions2;
451                         
452                         MPI_Status status; 
453                         MPI_File inMPI;
454                         MPI_File inMPI2;
455                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
456                         MPI_Comm_size(MPI_COMM_WORLD, &processors);
457                         int tag = 2001;
458
459                         char inFileName[1024];
460                         strcpy(inFileName, inNumName.c_str());
461                         
462                         char inFileName2[1024];
463                         strcpy(inFileName2, inName.c_str());
464
465                         MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
466                         MPI_File_open(MPI_COMM_WORLD, inFileName2, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI2);  //comm, filename, mode, info, filepointer
467
468                         if (pid == 0) {
469                                 positions = m->setFilePosEachLine(inNumName, num);
470                                 positions2 = m->setFilePosEachLine(inName, num2);
471                                 
472                                 for(int i = 1; i < processors; i++) { 
473                                         MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
474                                         MPI_Send(&positions[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
475                                         
476                                         MPI_Send(&num2, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
477                                         MPI_Send(&positions2[0], (num2+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
478                                 }
479
480                         }else{
481                                 MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
482                                 positions.resize(num+1);
483                                 MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
484                                 
485                                 MPI_Recv(&num2, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
486                                 positions2.resize(num2+1);
487                                 MPI_Recv(&positions2[0], (num2+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
488                         }
489                         
490                         //read version
491                         int length = positions2[1] - positions2[0];
492                         char* buf5 = new char[length];
493
494                         MPI_File_read_at(inMPI2, positions2[0], buf5, length, MPI_CHAR, &status);
495                         delete buf5;
496
497                         //read numKmers
498                         length = positions2[2] - positions2[1];
499                         char* buf = new char[length];
500
501                         MPI_File_read_at(inMPI2, positions2[1], buf, length, MPI_CHAR, &status);
502
503                         string tempBuf = buf;
504                         if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
505                         delete buf;
506
507                         istringstream iss (tempBuf,istringstream::in);
508                         iss >> numKmers;  
509                         
510                         //initialze probabilities
511                         wordGenusProb.resize(numKmers);
512                         
513                         for (int j = 0; j < wordGenusProb.size(); j++) {        wordGenusProb[j].resize(genusNodes.size());             }
514                         
515                         int kmer, name;  
516                         vector<int> numbers; numbers.resize(numKmers);
517                         float prob;
518                         vector<float> zeroCountProb; zeroCountProb.resize(numKmers);    
519                         
520                         //read version
521                         length = positions[1] - positions[0];
522                         char* buf6 = new char[length];
523
524                         MPI_File_read_at(inMPI2, positions[0], buf6, length, MPI_CHAR, &status);
525                         delete buf6;
526                         
527                         //read file 
528                         for(int i=1;i<num;i++){
529                                 //read next sequence
530                                 length = positions[i+1] - positions[i];
531                                 char* buf4 = new char[length];
532
533                                 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
534
535                                 tempBuf = buf4;
536                                 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
537                                 delete buf4;
538
539                                 istringstream iss (tempBuf,istringstream::in);
540                                 iss >> zeroCountProb[i] >> numbers[i];  
541                         }
542                         
543                         MPI_File_close(&inMPI);
544                         
545                         for(int i=2;i<num2;i++){
546                                 //read next sequence
547                                 length = positions2[i+1] - positions2[i];
548                                 char* buf4 = new char[length];
549
550                                 MPI_File_read_at(inMPI2, positions2[i], buf4, length, MPI_CHAR, &status);
551
552                                 tempBuf = buf4;
553                                 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
554                                 delete buf4;
555
556                                 istringstream iss (tempBuf,istringstream::in);
557                                 
558                                 iss >> kmer;
559                                 
560                                 //set them all to zero value
561                                 for (int i = 0; i < genusNodes.size(); i++) {
562                                         wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
563                                 }
564                                 
565                                 //get probs for nonzero values
566                                 for (int i = 0; i < numbers[kmer]; i++) {
567                                         iss >> name >> prob;
568                                         wordGenusProb[kmer][name] = prob;
569                                 }
570                                 
571                         }
572                         MPI_File_close(&inMPI2);
573                         MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
574                 #else
575                         //read version
576                         string line = m->getline(in); m->gobble(in);
577                         
578                         in >> numKmers; m->gobble(in);
579                         //cout << threadID << '\t' << line << '\t' << numKmers << &in << '\t' << &inNum << '\t' << genusNodes.size() << endl;
580                         //initialze probabilities
581                         wordGenusProb.resize(numKmers);
582                         
583                         for (int j = 0; j < wordGenusProb.size(); j++) {        wordGenusProb[j].resize(genusNodes.size());             }
584                         
585                         int kmer, name, count;  count = 0;
586                         vector<int> num; num.resize(numKmers);
587                         float prob;
588                         vector<float> zeroCountProb; zeroCountProb.resize(numKmers);            
589                         
590                         //read version
591                         string line2 = m->getline(inNum); m->gobble(inNum);
592                 //cout << threadID << '\t' << line2 << '\t' << this << endl;    
593                         while (inNum) {
594                                 inNum >> zeroCountProb[count] >> num[count];  
595                                 count++;
596                                 m->gobble(inNum);
597                                 //cout << threadID << '\t' << count << endl;
598                         }
599                         inNum.close();
600                 //cout << threadID << '\t' << "here1 " << &wordGenusProb << '\t' << &num << endl; //
601                 //cout << threadID << '\t' << &genusTotals << '\t' << endl; 
602                 //cout << threadID << '\t' << genusNodes.size() << endl;
603                         while(in) {
604                                 in >> kmer;
605                         //cout << threadID << '\t' << kmer << endl;
606                                 //set them all to zero value
607                                 for (int i = 0; i < genusNodes.size(); i++) {
608                                         wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
609                                 }
610                         //cout << threadID << '\t' << num[kmer] << "here" << endl;      
611                                 //get probs for nonzero values
612                                 for (int i = 0; i < num[kmer]; i++) {
613                                         in >> name >> prob;
614                                         wordGenusProb[kmer][name] = prob;
615                                 }
616                                 
617                                 m->gobble(in);
618                         }
619                         in.close();
620                 //cout << threadID << '\t' << "here" << endl;   
621                 #endif
622         }
623         catch(exception& e) {
624                 m->errorOut(e, "Bayesian", "readProbFile");
625                 exit(1);
626         }
627 }
628 /**************************************************************************************************/
629 bool Bayesian::checkReleaseDate(ifstream& file1, ifstream& file2, ifstream& file3, ifstream& file4) {
630         try {
631                 
632                 bool good = true;
633                 
634                 vector<string> lines;
635                 lines.push_back(m->getline(file1));  
636                 lines.push_back(m->getline(file2)); 
637                 lines.push_back(m->getline(file3)); 
638                 lines.push_back(m->getline(file4)); 
639
640                 //before we added this check
641                 if ((lines[0][0] != '#') || (lines[1][0] != '#') || (lines[2][0] != '#') || (lines[3][0] != '#')) {  good = false;  }
642                 else {
643                         //rip off #
644                         for (int i = 0; i < lines.size(); i++) { lines[i] = lines[i].substr(1);  }
645                         
646                         //get mothurs current version
647                         string version = m->getVersion();
648                         
649                         vector<string> versionVector;
650                         m->splitAtChar(version, versionVector, '.');
651                         
652                         //check each files version
653                         for (int i = 0; i < lines.size(); i++) { 
654                                 vector<string> linesVector;
655                                 m->splitAtChar(lines[i], linesVector, '.');
656                         
657                                 if (versionVector.size() != linesVector.size()) { good = false; break; }
658                                 else {
659                                         for (int j = 0; j < versionVector.size(); j++) {
660                                                 int num1, num2;
661                                                 convert(versionVector[j], num1);
662                                                 convert(linesVector[j], num2);
663                                                 
664                                                 //if mothurs version is newer than this files version, then we want to remake it
665                                                 if (num1 > num2) {  good = false; break;  }
666                                         }
667                                 }
668                                 
669                                 if (!good) { break; }
670                         }
671                 }
672                 
673                 if (!good) {  file1.close(); file2.close(); file3.close(); file4.close();  }
674                 else { file1.seekg(0);  file2.seekg(0);  file3.seekg(0);  file4.seekg(0);  }
675                 
676                 return good;
677         }
678         catch(exception& e) {
679                 m->errorOut(e, "Bayesian", "checkReleaseDate");
680                 exit(1);
681         }
682 }
683 /**************************************************************************************************/
684
685
686
687
688
689