]> git.donarmstrong.com Git - mothur.git/blob - bayesian.cpp
added count file to get.groups and remove.groups. added shortcut parameter to classif...
[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 FilesGood = false;
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                                 
147                                 //for each word
148                                 for (int i = 0; i < numKmers; i++) {
149                                         if (m->control_pressed) {  break; }
150                                         
151                                         #ifdef USE_MPI
152                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
153
154                                                 if (pid == 0) {  
155                                         #endif
156
157                     if (shortcuts) {  out << i << '\t'; }
158                                         
159                                         #ifdef USE_MPI
160                                                 }
161                                         #endif
162                                         
163                                         vector<int> seqsWithWordi = database->getSequencesWithKmer(i);
164                                         
165                                         map<int, int> count;
166                                         for (int k = 0; k < genusNodes.size(); k++) {  count[genusNodes[k]] = 0;  }                     
167                                                         
168                                         //for each sequence with that word
169                                         for (int j = 0; j < seqsWithWordi.size(); j++) {
170                                                 int temp = phyloTree->getIndex(names[seqsWithWordi[j]]);
171                                                 count[temp]++;  //increment count of seq in this genus who have this word
172                                         }
173                                         
174                                         //probabilityInTemplate = (# of seqs with that word in template + 0.50) / (total number of seqs in template + 1);
175                                         float probabilityInTemplate = (seqsWithWordi.size() + 0.50) / (float) (names.size() + 1);
176                                         diffPair tempProb(log(probabilityInTemplate), 0.0);
177                                         WordPairDiffArr[i] = tempProb;
178                                                 
179                                         int numNotZero = 0;
180                                         for (int k = 0; k < genusNodes.size(); k++) {
181                                                 //probabilityInThisTaxonomy = (# of seqs with that word in this taxonomy + probabilityInTemplate) / (total number of seqs in this taxonomy + 1);
182                                                 
183                                                 
184                                                 wordGenusProb[i][k] = log((count[genusNodes[k]] + probabilityInTemplate) / (float) (genusTotals[k] + 1));  
185                                                                         
186                                                 if (count[genusNodes[k]] != 0) { 
187                                                         #ifdef USE_MPI
188                                                                 int pid;
189                                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
190                                                 
191                                                                 if (pid == 0) {  
192                                                         #endif
193
194                             if (shortcuts) { out << k << '\t' << wordGenusProb[i][k] << '\t' ; }
195                                                         
196                                                         #ifdef USE_MPI
197                                                                 }
198                                                         #endif
199
200                                                         numNotZero++;  
201                                                 }
202                                         }
203                                         
204                                         #ifdef USE_MPI
205                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
206                                 
207                                                 if (pid == 0) {  
208                                         #endif
209                                         
210                             if (shortcuts) { 
211                                 out << endl;
212                                 out2 << probabilityInTemplate << '\t' << numNotZero << '\t' << log(probabilityInTemplate) << endl;
213                             }
214                                         
215                                         #ifdef USE_MPI
216                                                 }
217                                         #endif
218                                 }
219                                 
220                                 #ifdef USE_MPI
221                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
222                                 
223                                         if (pid == 0) {  
224                                 #endif
225                                 
226                         if (shortcuts) { 
227                             out.close();
228                             out2.close();
229                         }
230                                 #ifdef USE_MPI
231                                         }
232                                 #endif
233                                 
234                                 //read in new phylotree with less info. - its faster
235                                 ifstream phyloTreeTest(phyloTreeName.c_str());
236                                 delete phyloTree;
237                                 
238                                 phyloTree = new PhyloTree(phyloTreeTest, phyloTreeName);
239                 
240                                 //save probabilities
241                                 if (rdb->save) { rdb->wordGenusProb = wordGenusProb; rdb->WordPairDiffArr = WordPairDiffArr; }
242                         }
243                 }
244                 
245                 generateWordPairDiffArr();
246                 
247                 //save probabilities
248                 if (rdb->save) { rdb->wordGenusProb = wordGenusProb; rdb->WordPairDiffArr = WordPairDiffArr; }
249                 
250                 m->mothurOut("DONE."); m->mothurOutEndLine();
251                 m->mothurOut("It took " + toString(time(NULL) - start) + " seconds get probabilities. "); m->mothurOutEndLine();
252         }
253         catch(exception& e) {
254                 m->errorOut(e, "Bayesian", "Bayesian");
255                 exit(1);
256         }
257 }
258 /**************************************************************************************************/
259 Bayesian::~Bayesian() {
260         try {
261                 
262                  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."); 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                 tax = bootstrapResults(queryKmers, index, numToSelect);
316                 
317                 return tax;     
318         }
319         catch(exception& e) {
320                 m->errorOut(e, "Bayesian", "getTaxonomy");
321                 exit(1);
322         }
323 }
324 /**************************************************************************************************/
325 string Bayesian::bootstrapResults(vector<int> kmers, int tax, int numToSelect) {
326         try {
327                                 
328                 map<int, int> confidenceScores; 
329                 
330                 //initialize confidences to 0 
331                 int seqIndex = tax;
332                 TaxNode seq = phyloTree->get(tax);
333                 confidenceScores[tax] = 0;
334                 
335                 while (seq.level != 0) { //while you are not at the root
336                         seqIndex = seq.parent;
337                         confidenceScores[seqIndex] = 0;
338                         seq = phyloTree->get(seq.parent);
339                 }
340                                 
341                 map<int, int>::iterator itBoot;
342                 map<int, int>::iterator itBoot2;
343                 map<int, int>::iterator itConvert;
344                         
345                 for (int i = 0; i < iters; i++) {
346                         if (m->control_pressed) { return "control"; }
347                         
348                         vector<int> temp;
349                         for (int j = 0; j < numToSelect; j++) {
350                                 int index = int(rand() % kmers.size());
351                                 
352                                 //add word to temp
353                                 temp.push_back(kmers[index]);
354                         }
355                         
356                         //get taxonomy
357                         int newTax = getMostProbableTaxonomy(temp);
358                         //int newTax = 1;
359                         TaxNode taxonomyTemp = phyloTree->get(newTax);
360                         
361                         //add to confidence results
362                         while (taxonomyTemp.level != 0) { //while you are not at the root
363                                 itBoot2 = confidenceScores.find(newTax); //is this a classification we already have a count on
364                                 
365                                 if (itBoot2 != confidenceScores.end()) { //this is a classification we need a confidence for
366                                         (itBoot2->second)++;
367                                 }
368                                 
369                                 newTax = taxonomyTemp.parent;
370                                 taxonomyTemp = phyloTree->get(newTax);
371                         }
372         
373                 }
374                 
375                 string confidenceTax = "";
376                 simpleTax = "";
377                 
378                 int seqTaxIndex = tax;
379                 TaxNode seqTax = phyloTree->get(tax);
380                 
381                 while (seqTax.level != 0) { //while you are not at the root
382                                         
383                                 itBoot2 = confidenceScores.find(seqTaxIndex); //is this a classification we already have a count on
384                                 
385                                 int confidence = 0;
386                                 if (itBoot2 != confidenceScores.end()) { //already in confidence scores
387                                         confidence = itBoot2->second;
388                                 }
389                                 
390                                 if (((confidence/(float)iters) * 100) >= confidenceThreshold) {
391                                         confidenceTax = seqTax.name + "(" + toString(((confidence/(float)iters) * 100)) + ");" + confidenceTax;
392                                         simpleTax = seqTax.name + ";" + simpleTax;
393                                 }
394                                 
395                                 seqTaxIndex = seqTax.parent;
396                                 seqTax = phyloTree->get(seqTax.parent);
397                 }
398                 
399                 if (confidenceTax == "") { confidenceTax = "unknown;"; simpleTax = "unknown;";  }
400         
401                 return confidenceTax;
402                 
403         }
404         catch(exception& e) {
405                 m->errorOut(e, "Bayesian", "bootstrapResults");
406                 exit(1);
407         }
408 }
409 /**************************************************************************************************/
410 int Bayesian::getMostProbableTaxonomy(vector<int> queryKmer) {
411         try {
412                 int indexofGenus = 0;
413                 
414                 double maxProbability = -1000000.0;
415                 //find taxonomy with highest probability that this sequence is from it
416                 
417                 
418 //              cout << genusNodes.size() << endl;
419                 
420                 
421                 for (int k = 0; k < genusNodes.size(); k++) {
422                         //for each taxonomy calc its probability
423                         
424                         double prob = 0.0000;
425                         for (int i = 0; i < queryKmer.size(); i++) {
426                                 prob += wordGenusProb[queryKmer[i]][k];
427                         }
428                         
429 //                      cout << phyloTree->get(genusNodes[k]).name << '\t' << prob << endl;
430
431                         //is this the taxonomy with the greatest probability?
432                         if (prob > maxProbability) { 
433                                 indexofGenus = genusNodes[k];
434                                 maxProbability = prob;
435                         }
436                 }
437                 
438                         
439                 return indexofGenus;
440         }
441         catch(exception& e) {
442                 m->errorOut(e, "Bayesian", "getMostProbableTaxonomy");
443                 exit(1);
444         }
445 }
446 //********************************************************************************************************************
447 //if it is more probable that the reverse compliment kmers are in the template, then we assume the sequence is reversed.
448 bool Bayesian::isReversed(vector<int>& queryKmers){
449         try{
450                 bool reversed = false;
451                 float prob = 0;
452                 float reverseProb = 0;
453                  
454         for (int i = 0; i < queryKmers.size(); i++){
455             int kmer = queryKmers[i];
456             if (kmer >= 0){
457                 prob += WordPairDiffArr[kmer].prob;
458                                 reverseProb += WordPairDiffArr[kmer].reverseProb;
459             }
460         }
461                 
462         if (reverseProb > prob){ reversed = true; }
463         
464                 return reversed;
465         }
466         catch(exception& e) {
467                 m->errorOut(e, "Bayesian", "isReversed");
468                 exit(1);
469         }
470 }
471 //********************************************************************************************************************
472 int Bayesian::generateWordPairDiffArr(){
473         try{
474                 Kmer kmer(kmerSize);
475                 for (int i = 0; i < WordPairDiffArr.size(); i++) {
476                         int reversedWord = kmer.getReverseKmerNumber(i);
477                         WordPairDiffArr[i].reverseProb = WordPairDiffArr[reversedWord].prob;
478                 }
479                 
480                 return 0;
481         }catch(exception& e) {
482                 m->errorOut(e, "Bayesian", "generateWordPairDiffArr");
483                 exit(1);
484         }
485 }
486 /*************************************************************************************************
487 map<string, int> Bayesian::parseTaxMap(string newTax) {
488         try{
489         
490                 map<string, int> parsed;
491                 
492                 newTax = newTax.substr(0, newTax.length()-1);  //get rid of last ';'
493         
494                 //parse taxonomy
495                 string individual;
496                 while (newTax.find_first_of(';') != -1) {
497                         individual = newTax.substr(0,newTax.find_first_of(';'));
498                         newTax = newTax.substr(newTax.find_first_of(';')+1, newTax.length());
499                         parsed[individual] = 1;
500                 }
501                 
502                 //get last one
503                 parsed[newTax] = 1;
504
505                 return parsed;
506                 
507         }
508         catch(exception& e) {
509                 m->errorOut(e, "Bayesian", "parseTax");
510                 exit(1);
511         }
512 }
513 **************************************************************************************************/
514 void Bayesian::readProbFile(ifstream& in, ifstream& inNum, string inName, string inNumName) {
515         try{
516                 
517                 #ifdef USE_MPI
518                         
519                         int pid, num, num2, processors;
520                         vector<unsigned long long> positions;
521                         vector<unsigned long long> positions2;
522                         
523                         MPI_Status status; 
524                         MPI_File inMPI;
525                         MPI_File inMPI2;
526                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
527                         MPI_Comm_size(MPI_COMM_WORLD, &processors);
528                         int tag = 2001;
529
530                         char inFileName[1024];
531                         strcpy(inFileName, inNumName.c_str());
532                         
533                         char inFileName2[1024];
534                         strcpy(inFileName2, inName.c_str());
535
536                         MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
537                         MPI_File_open(MPI_COMM_WORLD, inFileName2, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI2);  //comm, filename, mode, info, filepointer
538
539                         if (pid == 0) {
540                                 positions = m->setFilePosEachLine(inNumName, num);
541                                 positions2 = m->setFilePosEachLine(inName, num2);
542                                 
543                                 for(int i = 1; i < processors; i++) { 
544                                         MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
545                                         MPI_Send(&positions[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
546                                         
547                                         MPI_Send(&num2, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
548                                         MPI_Send(&positions2[0], (num2+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
549                                 }
550
551                         }else{
552                                 MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
553                                 positions.resize(num+1);
554                                 MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
555                                 
556                                 MPI_Recv(&num2, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
557                                 positions2.resize(num2+1);
558                                 MPI_Recv(&positions2[0], (num2+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
559                         }
560                         
561                         //read version
562                         int length = positions2[1] - positions2[0];
563                         char* buf5 = new char[length];
564
565                         MPI_File_read_at(inMPI2, positions2[0], buf5, length, MPI_CHAR, &status);
566                         delete buf5;
567
568                         //read numKmers
569                         length = positions2[2] - positions2[1];
570                         char* buf = new char[length];
571
572                         MPI_File_read_at(inMPI2, positions2[1], buf, length, MPI_CHAR, &status);
573
574                         string tempBuf = buf;
575                         if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
576                         delete buf;
577
578                         istringstream iss (tempBuf,istringstream::in);
579                         iss >> numKmers;  
580                         
581                         //initialze probabilities
582                         wordGenusProb.resize(numKmers);
583                         
584                         for (int j = 0; j < wordGenusProb.size(); j++) {        wordGenusProb[j].resize(genusNodes.size());             }
585                         
586                         int kmer, name;  
587                         vector<int> numbers; numbers.resize(numKmers);
588                         float prob;
589                         vector<float> zeroCountProb; zeroCountProb.resize(numKmers);
590                         WordPairDiffArr.resize(numKmers);
591                         
592                         //read version
593                         length = positions[1] - positions[0];
594                         char* buf6 = new char[length];
595
596                         MPI_File_read_at(inMPI2, positions[0], buf6, length, MPI_CHAR, &status);
597                         delete buf6;
598                         
599                         //read file 
600                         for(int i=1;i<num;i++){
601                                 //read next sequence
602                                 length = positions[i+1] - positions[i];
603                                 char* buf4 = new char[length];
604
605                                 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
606
607                                 tempBuf = buf4;
608                                 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
609                                 delete buf4;
610
611                                 istringstream iss (tempBuf,istringstream::in);
612                                 float probTemp;
613                                 iss >> zeroCountProb[i] >> numbers[i] >> probTemp; 
614                                 WordPairDiffArr[i].prob = probTemp;
615
616                         }
617                         
618                         MPI_File_close(&inMPI);
619                         
620                         for(int i=2;i<num2;i++){
621                                 //read next sequence
622                                 length = positions2[i+1] - positions2[i];
623                                 char* buf4 = new char[length];
624
625                                 MPI_File_read_at(inMPI2, positions2[i], buf4, length, MPI_CHAR, &status);
626
627                                 tempBuf = buf4;
628                                 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
629                                 delete buf4;
630
631                                 istringstream iss (tempBuf,istringstream::in);
632                                 
633                                 iss >> kmer;
634                                 
635                                 //set them all to zero value
636                                 for (int i = 0; i < genusNodes.size(); i++) {
637                                         wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
638                                 }
639                                 
640                                 //get probs for nonzero values
641                                 for (int i = 0; i < numbers[kmer]; i++) {
642                                         iss >> name >> prob;
643                                         wordGenusProb[kmer][name] = prob;
644                                 }
645                                 
646                         }
647                         MPI_File_close(&inMPI2);
648                         MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
649                 #else
650                         //read version
651                         string line = m->getline(in); m->gobble(in);
652                         
653                         in >> numKmers; m->gobble(in);
654                         //cout << threadID << '\t' << line << '\t' << numKmers << &in << '\t' << &inNum << '\t' << genusNodes.size() << endl;
655                         //initialze probabilities
656                         wordGenusProb.resize(numKmers);
657                         
658                         for (int j = 0; j < wordGenusProb.size(); j++) {        wordGenusProb[j].resize(genusNodes.size());             }
659                         
660                         int kmer, name, count;  count = 0;
661                         vector<int> num; num.resize(numKmers);
662                         float prob;
663                         vector<float> zeroCountProb; zeroCountProb.resize(numKmers);    
664                         WordPairDiffArr.resize(numKmers);
665                         
666                         //read version
667                         string line2 = m->getline(inNum); m->gobble(inNum);
668                         float probTemp;
669                 //cout << threadID << '\t' << line2 << '\t' << this << endl;    
670                         while (inNum) {
671                                 inNum >> zeroCountProb[count] >> num[count] >> probTemp; 
672                                 WordPairDiffArr[count].prob = probTemp;
673                                 count++;
674                                 m->gobble(inNum);
675                                 //cout << threadID << '\t' << count << endl;
676                         }
677                         inNum.close();
678                 //cout << threadID << '\t' << "here1 " << &wordGenusProb << '\t' << &num << endl; //
679                 //cout << threadID << '\t' << &genusTotals << '\t' << endl; 
680                 //cout << threadID << '\t' << genusNodes.size() << endl;
681                         while(in) {
682                                 in >> kmer;
683                         //cout << threadID << '\t' << kmer << endl;
684                                 //set them all to zero value
685                                 for (int i = 0; i < genusNodes.size(); i++) {
686                                         wordGenusProb[kmer][i] = log(zeroCountProb[kmer] / (float) (genusTotals[i]+1));
687                                 }
688                         //cout << threadID << '\t' << num[kmer] << "here" << endl;      
689                                 //get probs for nonzero values
690                                 for (int i = 0; i < num[kmer]; i++) {
691                                         in >> name >> prob;
692                                         wordGenusProb[kmer][name] = prob;
693                                 }
694                                 
695                                 m->gobble(in);
696                         }
697                         in.close();
698                 //cout << threadID << '\t' << "here" << endl;   
699                 #endif
700         }
701         catch(exception& e) {
702                 m->errorOut(e, "Bayesian", "readProbFile");
703                 exit(1);
704         }
705 }
706 /**************************************************************************************************/
707 bool Bayesian::checkReleaseDate(ifstream& file1, ifstream& file2, ifstream& file3, ifstream& file4) {
708         try {
709                 
710                 bool good = true;
711                 
712                 vector<string> lines;
713                 lines.push_back(m->getline(file1));  
714                 lines.push_back(m->getline(file2)); 
715                 lines.push_back(m->getline(file3)); 
716                 lines.push_back(m->getline(file4)); 
717
718                 //before we added this check
719                 if ((lines[0][0] != '#') || (lines[1][0] != '#') || (lines[2][0] != '#') || (lines[3][0] != '#')) {  good = false;  }
720                 else {
721                         //rip off #
722                         for (int i = 0; i < lines.size(); i++) { lines[i] = lines[i].substr(1);  }
723                         
724                         //get mothurs current version
725                         string version = m->getVersion();
726                         
727                         vector<string> versionVector;
728                         m->splitAtChar(version, versionVector, '.');
729                         
730                         //check each files version
731                         for (int i = 0; i < lines.size(); i++) { 
732                                 vector<string> linesVector;
733                                 m->splitAtChar(lines[i], linesVector, '.');
734                         
735                                 if (versionVector.size() != linesVector.size()) { good = false; break; }
736                                 else {
737                                         for (int j = 0; j < versionVector.size(); j++) {
738                                                 int num1, num2;
739                                                 convert(versionVector[j], num1);
740                                                 convert(linesVector[j], num2);
741                                                 
742                                                 //if mothurs version is newer than this files version, then we want to remake it
743                                                 if (num1 > num2) {  good = false; break;  }
744                                         }
745                                 }
746                                 
747                                 if (!good) { break; }
748                         }
749                 }
750                 
751                 if (!good) {  file1.close(); file2.close(); file3.close(); file4.close();  }
752                 else { file1.seekg(0);  file2.seekg(0);  file3.seekg(0);  file4.seekg(0);  }
753                 
754                 return good;
755         }
756         catch(exception& e) {
757                 m->errorOut(e, "Bayesian", "checkReleaseDate");
758                 exit(1);
759         }
760 }
761 /**************************************************************************************************/
762
763
764
765
766
767