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