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