]> git.donarmstrong.com Git - mothur.git/blob - bellerophon.cpp
743c6d779c8042ea2d47690cdc77ec839bf21bab
[mothur.git] / bellerophon.cpp
1 /*
2  *  bellerophon.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "bellerophon.h"
11 #include "eachgapdist.h"
12 #include "ignoregaps.h"
13 #include "onegapdist.h"
14
15
16 /***************************************************************************************************************/
17
18 Bellerophon::Bellerophon(string name, bool filterSeqs,  bool c, int win, int inc, int p, string o) : Chimera() {
19         try {
20                 fastafile = name;
21                 correction = c;
22                 outputDir = o;
23                 window = win;
24                 increment = inc;
25                 processors = p;
26                 
27                 //read in sequences
28                 seqs = readSeqs(fastafile);
29                 numSeqs = seqs.size();
30                 if (numSeqs == 0) { m->mothurOut("Error in reading you sequences."); m->mothurOutEndLine(); exit(1); }
31         
32                 //do soft filter
33                 if (filterSeqs)  {
34                         createFilter(seqs, 0.5);
35                         for (int i = 0; i < seqs.size(); i++) {  runFilter(seqs[i]);  }
36                 }
37                 
38                 distCalculator = new eachGapDist();
39                 
40                 //set default window to 25% of sequence length
41                 string seq0 = seqs[0]->getAligned();
42                 if (window == 0) { window = seq0.length() / 4;  }
43                 else if (window > (seq0.length() / 2)) {  
44                         m->mothurOut("Your sequence length is = " + toString(seq0.length()) + ". You have selected a window size greater than the length of half your aligned sequence. I will run it with a window size of " + toString((seq0.length() / 2))); m->mothurOutEndLine();
45                         window = (seq0.length() / 2);
46                 }
47                 
48                 if (increment > (seqs[0]->getAlignLength() - (2*window))) { 
49                         if (increment != 10) {
50                         
51                                 m->mothurOut("You have selected a increment that is too large. I will use the default."); m->mothurOutEndLine();
52                                 increment = 10;
53                                 if (increment > (seqs[0]->getAlignLength() - (2*window))) {  increment = 0;  }
54                                 
55                         }else{ increment = 0; }
56                 }
57                 
58                 if (increment == 0) { iters = 1; }
59                 else { iters = ((seqs[0]->getAlignLength() - (2*window)) / increment); }
60                 
61                 //initialize pref
62                 pref.resize(iters);
63                 for (int i = 0; i < iters; i++) { 
64                         Preference temp;
65                         for (int j = 0; j < numSeqs; j++) {  
66                                 pref[i].push_back(temp); 
67                         }
68                 } 
69
70         }
71         catch(exception& e) {
72                 m->errorOut(e, "Bellerophon", "Bellerophon");
73                 exit(1);
74         }
75 }
76
77 //***************************************************************************************************************
78 int Bellerophon::print(ostream& out, ostream& outAcc) {
79         try {
80                 int above1 = 0;
81                 
82                 //sorted "best" preference scores for all seqs
83                 vector<Preference> best = getBestPref();
84                 
85                 if (m->control_pressed) { return numSeqs; }
86                 
87                 out << "Name\tScore\tLeft\tRight\t" << endl;
88                 //output prefenence structure to .chimeras file
89                 for (int i = 0; i < best.size(); i++) {
90                         
91                         if (m->control_pressed) {  return numSeqs; }
92                         
93                         out << best[i].name << '\t' << setprecision(3) << best[i].score << '\t' << best[i].leftParent << '\t' << best[i].rightParent << endl;
94                         
95                         //calc # of seqs with preference above 1.0
96                         if (best[i].score > 1.0) { 
97                                 above1++; 
98                                 outAcc << best[i].name << endl;
99                                 m->mothurOut(best[i].name + " is a suspected chimera at breakpoint " + toString(best[i].midpoint)); m->mothurOutEndLine();
100                                 m->mothurOut("It's score is " + toString(best[i].score) + " with suspected left parent " + best[i].leftParent + " and right parent " + best[i].rightParent); m->mothurOutEndLine();
101                         }
102                 }
103                 
104                 //output results to screen
105                 m->mothurOutEndLine();
106                 m->mothurOut("Sequence with preference score above 1.0: " + toString(above1)); m->mothurOutEndLine();
107                 int spot;
108                 spot = best.size()-1;
109                 m->mothurOut("Minimum:\t" + toString(best[spot].score)); m->mothurOutEndLine();
110                 spot = best.size() * 0.975;
111                 m->mothurOut("2.5%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
112                 spot = best.size() * 0.75;
113                 m->mothurOut("25%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
114                 spot = best.size() * 0.50;
115                 m->mothurOut("Median: \t" + toString(best[spot].score)); m->mothurOutEndLine();
116                 spot = best.size() * 0.25;
117                 m->mothurOut("75%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
118                 spot = best.size() * 0.025;
119                 m->mothurOut("97.5%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
120                 spot = 0;
121                 m->mothurOut("Maximum:\t" + toString(best[spot].score)); m->mothurOutEndLine();
122                 
123                 return numSeqs;
124
125         }
126         catch(exception& e) {
127                 m->errorOut(e, "Bellerophon", "print");
128                 exit(1);
129         }
130 }
131 #ifdef USE_MPI
132 //***************************************************************************************************************
133 int Bellerophon::print(MPI_File& out, MPI_File& outAcc) {
134         try {
135         
136                 int pid;
137                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
138                 
139                 if (pid == 0) {
140                         string outString = "";
141                                                 
142                         //sorted "best" preference scores for all seqs
143                         vector<Preference> best = getBestPref();
144                         
145                         int above1 = 0;
146                         int ninetyfive = best.size() * 0.05;
147                         float cutoffScore = best[ninetyfive].score;
148
149                         if (m->control_pressed) { return numSeqs; }
150                         
151                         outString = "Name\tScore\tLeft\tRight\n";
152                         MPI_Status status;
153                         int olength = outString.length();
154                         char* buf5 = new char[olength];
155                         memcpy(buf5, outString.c_str(), olength);
156                                         
157                         MPI_File_write_shared(out, buf5, olength, MPI_CHAR, &status);
158                         
159                         delete buf5;
160
161                         //output prefenence structure to .chimeras file
162                         for (int i = 0; i < best.size(); i++) {
163                                 
164                                 if (m->control_pressed) {  return numSeqs; }
165                                 
166                                 outString = best[i].name + "\t" +  toString(best[i].score) + "\t" + best[i].leftParent + "\t" + best[i].rightParent + "\n";
167                         
168                                 MPI_Status status;
169                                 int length = outString.length();
170                                 char* buf2 = new char[length];
171                                 memcpy(buf2, outString.c_str(), length);
172                                         
173                                 MPI_File_write_shared(out, buf2, length, MPI_CHAR, &status);
174                                 
175                                 delete buf2;
176                                 
177                                 //calc # of seqs with preference above 95%tile
178                                 if (best[i].score >= cutoffScore) { 
179                                         above1++; 
180                                         string outAccString = "";
181                                          outAccString += best[i].name + "\n";
182                                         
183                                         MPI_Status statusAcc;
184                                         length = outAccString.length();
185                                         char* buf = new char[length];
186                                         memcpy(buf, outAccString.c_str(), length);
187                                         
188                                         MPI_File_write_shared(outAcc, buf, length, MPI_CHAR, &statusAcc);
189                                         
190                                         delete buf;
191
192                                         cout << best[i].name << " is a suspected chimera at breakpoint " << toString(best[i].midpoint) << endl;
193                                         cout << "It's score is " << toString(best[i].score) << " with suspected left parent " << best[i].leftParent << " and right parent " << best[i].rightParent << endl;
194                                 }
195                         }
196                         
197                         //output results to screen
198                         m->mothurOutEndLine();
199                         m->mothurOut("Sequence with preference score above " + toString(cutoffScore) +  ": " + toString(above1)); m->mothurOutEndLine();
200                         int spot;
201                         spot = best.size()-1;
202                         m->mothurOut("Minimum:\t" + toString(best[spot].score)); m->mothurOutEndLine();
203                         spot = best.size() * 0.975;
204                         m->mothurOut("2.5%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
205                         spot = best.size() * 0.75;
206                         m->mothurOut("25%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
207                         spot = best.size() * 0.50;
208                         m->mothurOut("Median: \t" + toString(best[spot].score)); m->mothurOutEndLine();
209                         spot = best.size() * 0.25;
210                         m->mothurOut("75%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
211                         spot = best.size() * 0.025;
212                         m->mothurOut("97.5%-tile:\t" + toString(best[spot].score)); m->mothurOutEndLine();
213                         spot = 0;
214                         m->mothurOut("Maximum:\t" + toString(best[spot].score)); m->mothurOutEndLine();
215                         
216                 }
217                 
218                 return numSeqs;
219                 
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "Bellerophon", "print");
223                 exit(1);
224         }
225 }
226 #endif
227 //********************************************************************************************************************
228 //sorts highest score to lowest
229 inline bool comparePref(Preference left, Preference right){
230         return (left.score > right.score);      
231 }
232 //***************************************************************************************************************
233 int Bellerophon::getChimeras() {
234         try {
235                 
236                 //create breaking points
237                 vector<int> midpoints;   midpoints.resize(iters, window);
238                 for (int i = 1; i < iters; i++) {  midpoints[i] = midpoints[i-1] + increment;  }
239         
240         #ifdef USE_MPI
241                 int pid, numSeqsPerProcessor; 
242         
243                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
244                 MPI_Comm_size(MPI_COMM_WORLD, &processors); 
245                 
246                 numSeqsPerProcessor = iters / processors;
247                 
248                 //each process hits this only once
249                 unsigned long int startPos = pid * numSeqsPerProcessor;
250                 if(pid == processors - 1){
251                                 numSeqsPerProcessor = iters - pid * numSeqsPerProcessor;
252                 }
253                 lines.push_back(linePair(startPos, numSeqsPerProcessor));
254                 
255                 //fill pref with scores
256                 driverChimeras(midpoints, lines[0]);
257                 
258                 if (m->control_pressed) { return 0; }
259                                 
260                 //each process must send its parts back to pid 0
261                 if (pid == 0) {
262                         
263                         //receive results 
264                         for (int j = 1; j < processors; j++) {
265                                 
266                                 vector<string>  MPIBestSend; 
267                                 for (int i = 0; i < numSeqs; i++) {
268                                 
269                                         if (m->control_pressed) { return 0; }
270
271                                         MPI_Status status;
272                                         //receive string
273                                         int length;
274                                         MPI_Recv(&length, 1, MPI_INT, j, 2001, MPI_COMM_WORLD, &status);
275                                         
276                                         char* buf = new char[length];
277                                         MPI_Recv(&buf, length, MPI_CHAR, j, 2001, MPI_COMM_WORLD, &status);
278                                         
279                                         string temp = buf;
280                                         if (temp.length() > length) { temp = temp.substr(0, length); }
281                                         delete buf;
282
283                                         MPIBestSend.push_back(temp);
284                                 }
285                                 
286                                 fillPref(j, MPIBestSend);
287                                 
288                                 if (m->control_pressed) { return 0; }
289                         }
290
291                 }else {
292                         //takes best window for each sequence and turns Preference to string that can be parsed by pid 0.
293                         //played with this a bit, but it may be better to try user-defined datatypes with set string lengths??
294                         vector<string> MPIBestSend = getBestWindow(lines[0]);
295                         pref.clear();
296                         
297                         //send your result to parent
298                         for (int i = 0; i < numSeqs; i++) {
299                                 
300                                 if (m->control_pressed) { return 0; }
301                                 
302                                 int bestLength = MPIBestSend[i].length();
303                                 char* buf = new char[bestLength];
304                                 memcpy(buf, MPIBestSend[i].c_str(), bestLength);
305                                 
306                                 MPI_Send(&bestLength, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD);
307                                 MPI_Send(buf, bestLength, MPI_CHAR, 0, 2001, MPI_COMM_WORLD);
308                                 delete buf;
309                         }
310                         
311                         MPIBestSend.clear();
312                 }
313                 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
314         #else
315         
316                 //divide breakpoints between processors
317                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
318                         if(processors == 1){ 
319                                 lines.push_back(linePair(0, iters));    
320                                 
321                                 //fill pref with scores
322                                 driverChimeras(midpoints, lines[0]);
323         
324                         }else{
325                         
326                                 int numSeqsPerProcessor = iters / processors;
327                                 
328                                 for (int i = 0; i < processors; i++) {
329                                         unsigned long int startPos = i * numSeqsPerProcessor;
330                                         if(i == processors - 1){
331                                                 numSeqsPerProcessor = iters - i * numSeqsPerProcessor;
332                                         }
333                                         lines.push_back(linePair(startPos, numSeqsPerProcessor));
334                                 }
335                                 
336                                 createProcesses(midpoints);
337                         }
338                 #else
339                         lines.push_back(linePair(0, iters));    
340                         
341                         ///fill pref with scores
342                         driverChimeras(midpoints, lines[0]);
343                 #endif
344         
345         #endif
346         
347                 return 0;
348                 
349         }
350         catch(exception& e) {
351                 m->errorOut(e, "Bellerophon", "getChimeras");
352                 exit(1);
353         }
354 }
355 /**************************************************************************************************/
356
357 int Bellerophon::createProcesses(vector<int> mid) {
358         try {
359 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
360                 int process = 0;
361                 int exitCommand = 1;
362                 vector<int> processIDS;
363                                 
364                 //loop through and create all the processes you want
365                 while (process != processors) {
366                         int pid = fork();
367                         
368                         if (pid > 0) {
369                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
370                                 process++;
371                         }else if (pid == 0){
372                                 exitCommand = driverChimeras(mid, lines[process]);
373                                 string tempOut = outputDir + toString(getpid()) + ".temp";
374                                 writePrefs(tempOut, lines[process]);
375                                 exit(0);
376                         }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
377                 }
378                 
379                 //force parent to wait until all the processes are done
380                 for (int i=0;i<processors;i++) { 
381                         int temp = processIDS[i];
382                         wait(&temp);
383                 }
384                 
385                 //get info that other processes created
386                 for (int i = 0; i < processIDS.size(); i++) {
387                         string tempIn = outputDir + toString(processIDS[i]) + ".temp";
388                         readPrefs(tempIn);
389                 }
390                 
391                 return exitCommand;
392 #endif          
393         }
394         catch(exception& e) {
395                 m->errorOut(e, "AlignCommand", "createProcesses");
396                 exit(1);
397         }
398 }
399 //***************************************************************************************************************
400 int Bellerophon::driverChimeras(vector<int> midpoints, linePair line) {
401         try {
402                 
403                 for (int h = line.start; h < (line.start + line.num); h++) {
404                         count = h;
405                         int midpoint = midpoints[h];
406                 
407                         //initialize pref[count]                
408                         for (int i = 0; i < numSeqs; i++ ) { 
409                                 pref[count][i].name = seqs[i]->getName();
410                                 pref[count][i].midpoint = midpoint;  
411                         }
412                         
413                         if (m->control_pressed) { return 0; }
414                         
415                         //create 2 vectors of sequences, 1 for left side and one for right side
416                         vector<Sequence> left;  vector<Sequence> right;
417                         
418                         for (int i = 0; i < seqs.size(); i++) {
419                                 
420                                 if (m->control_pressed) { return 0; }
421                                 
422                                 //cout << "midpoint = " << midpoint << "\twindow = " << window << endl;
423                                 //cout << "whole = " << seqs[i]->getAligned().length() << endl;
424                                 //save left side
425                                 string seqLeft = seqs[i]->getAligned().substr(midpoint-window, window);
426                                 Sequence tempLeft;
427                                 tempLeft.setName(seqs[i]->getName());
428                                 tempLeft.setAligned(seqLeft);
429                                 left.push_back(tempLeft);
430                                 //cout << "left = " << tempLeft.getAligned().length() << endl;                  
431                                 //save right side
432                                 string seqRight = seqs[i]->getAligned().substr(midpoint, window);
433                                 Sequence tempRight;
434                                 tempRight.setName(seqs[i]->getName());
435                                 tempRight.setAligned(seqRight);
436                                 right.push_back(tempRight);
437                                 //cout << "right = " << seqRight.length() << endl;      
438                         }
439                         
440                         //this should be parallelized
441                         //perference = sum of (| distance of my left to sequence j's left - distance of my right to sequence j's right | )
442                         //create a matrix containing the distance from left to left and right to right
443                         //calculate distances
444                         SparseMatrix* SparseLeft = new SparseMatrix();
445                         SparseMatrix* SparseRight = new SparseMatrix();
446                         
447                         createSparseMatrix(0, left.size(), SparseLeft, left);
448                         
449                         if (m->control_pressed) { delete SparseLeft; delete SparseRight; return 0; }
450                         
451                         createSparseMatrix(0, right.size(), SparseRight, right);
452                         
453                         if (m->control_pressed) { delete SparseLeft; delete SparseRight; return 0; }
454                         
455                         left.clear(); right.clear();
456                         vector<SeqMap> distMapRight;
457                         vector<SeqMap> distMapLeft;
458                         
459                         // Create a data structure to quickly access the distance information.
460                         //this is from thallingers reimplementation on get.oturep
461                         // It consists of a vector of distance maps, where each map contains
462                         // all distances of a certain sequence. Vector and maps are accessed
463                         // via the index of a sequence in the distance matrix
464                         distMapRight = vector<SeqMap>(numSeqs); 
465                         distMapLeft = vector<SeqMap>(numSeqs); 
466                         //cout << "left" << endl << endl;
467                         for (MatData currentCell = SparseLeft->begin(); currentCell != SparseLeft->end(); currentCell++) {
468                                 distMapLeft[currentCell->row][currentCell->column] = currentCell->dist;
469                                 if (m->control_pressed) { delete SparseLeft; delete SparseRight; return 0; }
470                                 //cout << " i = " << currentCell->row << " j = " << currentCell->column << " dist = " << currentCell->dist << endl;
471                         }
472                         //cout << "right" << endl << endl;
473                         for (MatData currentCell = SparseRight->begin(); currentCell != SparseRight->end(); currentCell++) {
474                                 distMapRight[currentCell->row][currentCell->column] = currentCell->dist;
475                                 if (m->control_pressed) { delete SparseLeft; delete SparseRight; return 0; }
476                                 //cout << " i = " << currentCell->row << " j = " << currentCell->column << " dist = " << currentCell->dist << endl;
477                         }
478                         
479                         delete SparseLeft;
480                         delete SparseRight;
481                         
482                         //fill preference structure
483                         generatePreferences(distMapLeft, distMapRight, midpoint);
484                         
485                         if (m->control_pressed) { return 0; }
486                         
487                         //report progress
488                         if((h+1) % 10 == 0){    cout << "Processing sliding window: " << toString(h+1) <<  "\n";  m->mothurOutJustToLog("Processing sliding window: " + toString(h+1) + "\n") ;         }
489                         
490                 }
491                 
492                 //report progress
493                 if((line.start + line.num) % 10 != 0){  cout << "Processing sliding window: " << toString(line.start + line.num) <<  "\n";  m->mothurOutJustToLog("Processing sliding window: " + toString(line.start + line.num) + "\n") ;             }
494
495                 return 0;
496                 
497         }
498         catch(exception& e) {
499                 m->errorOut(e, "Bellerophon", "driverChimeras");
500                 exit(1);
501         }
502 }
503
504 /***************************************************************************************************************/
505 int Bellerophon::createSparseMatrix(int startSeq, int endSeq, SparseMatrix* sparse, vector<Sequence> s){
506         try {
507
508                 for(int i=startSeq; i<endSeq; i++){
509                         
510                         for(int j=0;j<i;j++){
511                                 
512                                 if (m->control_pressed) { return 0; }
513                         
514                                 distCalculator->calcDist(s[i], s[j]);
515                                 float dist = distCalculator->getDist();
516                         
517                                 PCell temp(i, j, dist);
518                                 sparse->addCell(temp);
519                                 
520                         }
521                 }
522                 
523                 return 1;
524         }
525         catch(exception& e) {
526                 m->errorOut(e, "Bellerophon", "createSparseMatrix");
527                 exit(1);
528         }
529 }
530 /***************************************************************************************************************/
531 int Bellerophon::generatePreferences(vector<SeqMap> left, vector<SeqMap> right, int mid){
532         try {
533                 
534                 float dme = 0.0;
535                 SeqMap::iterator itR;
536                 SeqMap::iterator itL;
537                 
538                 for (int i = 0; i < left.size(); i++) {
539                         
540                         SeqMap currentLeft = left[i];    //example i = 3;   currentLeft is a map of 0 to the distance of sequence 3 to sequence 0,
541                                                                                                 //                                                                              1 to the distance of sequence 3 to sequence 1,
542                                                                                                 //                                                                              2 to the distance of sequence 3 to sequence 2.
543                         SeqMap currentRight = right[i];         // same as left but with distances on the right side.
544                         
545                         for (int j = 0; j < i; j++) {
546                         
547                                 if (m->control_pressed) {  return 0; }
548                                 
549                                 itL = currentLeft.find(j);
550                                 itR = currentRight.find(j);
551 //cout << " i = " << i << " j = " << j << " distLeft = " << itL->second << endl;
552 //cout << " i = " << i << " j = " << j << " distright = " << itR->second << endl;
553                                 
554                                 //if you can find this entry update the preferences
555                                 if ((itL != currentLeft.end()) && (itR != currentRight.end())) {
556                                 
557                                         if (!correction) {
558                                                 pref[count][i].score += abs((itL->second - itR->second));
559                                                 pref[count][j].score += abs((itL->second - itR->second));
560 //cout << "left " << i << " " << j << " = " << itL->second << " right " << i << " " << j << " = " << itR->second << endl;
561 //cout << "abs = " << abs((itL->second - itR->second)) << endl;
562 //cout << i << " score = " << pref[i].score[1] << endl;
563 //cout << j << " score = " << pref[j].score[1] << endl;
564                                         }else {
565                                                 pref[count][i].score += abs((sqrt(itL->second) - sqrt(itR->second)));
566                                                 pref[count][j].score += abs((sqrt(itL->second) - sqrt(itR->second)));
567 //cout << "left " << i << " " << j << " = " << itL->second << " right " << i << " " << j << " = " << itR->second << endl;
568 //cout << "abs = " << abs((sqrt(itL->second) - sqrt(itR->second))) << endl;
569 //cout << i << " score = " << pref[i].score[1] << endl;
570 //cout << j << " score = " << pref[j].score[1] << endl;
571                                         }
572 //cout << "pref[" << i << "].closestLeft[1] = " <<      pref[i].closestLeft[1] << " parent = " << pref[i].leftParent[1] << endl;                        
573                                         //are you the closest left sequence
574                                         if (itL->second < pref[count][i].closestLeft) {  
575
576                                                 pref[count][i].closestLeft = itL->second;
577                                                 pref[count][i].leftParent = seqs[j]->getName();
578 //cout << "updating closest left to " << pref[i].leftParent[1] << endl;
579                                         }
580 //cout << "pref[" << j << "].closestLeft[1] = " <<      pref[j].closestLeft[1] << " parent = " << pref[j].leftParent[1] << endl;        
581                                         if (itL->second < pref[count][j].closestLeft) { 
582                                                 pref[count][j].closestLeft = itL->second;
583                                                 pref[count][j].leftParent = seqs[i]->getName();
584 //cout << "updating closest left to " << pref[j].leftParent[1] << endl;
585                                         }
586                                         
587                                         //are you the closest right sequence
588                                         if (itR->second < pref[count][i].closestRight) {   
589                                                 pref[count][i].closestRight = itR->second;
590                                                 pref[count][i].rightParent = seqs[j]->getName();
591                                         }
592                                         if (itR->second < pref[count][j].closestRight) {   
593                                                 pref[count][j].closestRight = itR->second;
594                                                 pref[count][j].rightParent = seqs[i]->getName();
595                                         }
596                                         
597                                 }
598                         }
599                 
600                 }
601                 
602                                 
603                 return 1;
604
605         }
606         catch(exception& e) {
607                 m->errorOut(e, "Bellerophon", "generatePreferences");
608                 exit(1);
609         }
610 }
611 /**************************************************************************************************/
612 vector<Preference> Bellerophon::getBestPref() {
613         try {
614                 
615                 vector<Preference> best;
616                 
617                 //for each sequence
618                 for (int i = 0; i < numSeqs; i++) {
619                         
620                         //set best pref score to first one
621                         Preference temp = pref[0][i];
622                         
623                         if (m->control_pressed) { return best;  }
624                         
625                         //for each window
626                         for (int j = 1; j < pref.size(); j++) {
627                                 
628                                 //is this a better score
629                                 if (pref[j][i].score > temp.score) {    temp = pref[j][i];              }
630                         }
631                         
632                         best.push_back(temp);
633                 }
634                 
635                 //rank preference score to eachother
636                 float dme = 0.0;
637                 float expectedPercent = 1 / (float) (best.size());
638                 
639                 for (int i = 0; i < best.size(); i++) {  dme += best[i].score;  }
640         
641                 for (int i = 0; i < best.size(); i++) {
642
643                         if (m->control_pressed) { return best; }
644                         
645                         //gives the actual percentage of the dme this seq adds
646                         best[i].score = best[i].score / dme;
647                         
648                         //how much higher or lower is this than expected
649                         best[i].score = best[i].score / expectedPercent;
650                 
651                 }
652                 
653                 //sort Preferences highest to lowest
654                 sort(best.begin(), best.end(), comparePref);
655
656                 return best;
657         }
658         catch(exception& e) {
659                 m->errorOut(e, "Bellerophon", "getBestPref");
660                 exit(1);
661         }
662 }
663 /**************************************************************************************************/
664 int Bellerophon::writePrefs(string file, linePair tempLine) {
665         try {
666         
667                 ofstream outTemp;
668                 openOutputFile(file, outTemp);
669                 
670                 //lets you know what part of the pref matrix you are writing
671                 outTemp << tempLine.start << '\t' << tempLine.num << endl;
672                 
673                 for (int i = tempLine.start; i < (tempLine.start + tempLine.num); i++) {
674                         
675                         for (int j = 0; j < numSeqs; j++) {
676                                 
677                                 if (m->control_pressed) { outTemp.close(); remove(file.c_str()); return 0; }
678                                 
679                                 outTemp << pref[i][j].name << '\t' << pref[i][j].leftParent << '\t' << pref[i][j].rightParent << '\t';
680                                 outTemp << pref[i][j].score << '\t' << pref[i][j].closestLeft << '\t' << pref[i][j].closestRight << '\t' << pref[i][j].midpoint <<  endl;
681                         }
682                 }
683                 
684                 outTemp.close();
685                 
686                 return 0;
687         }
688         catch(exception& e) {
689                 m->errorOut(e, "Bellerophon", "writePrefs");
690                 exit(1);
691         }
692 }
693 /**************************************************************************************************/
694 int Bellerophon::readPrefs(string file) {
695         try {
696         
697                 ifstream inTemp;
698                 openInputFile(file, inTemp);
699                 
700                 int start, num;
701                 
702                 //lets you know what part of the pref matrix you are writing
703                 inTemp >> start >> num;  gobble(inTemp);
704                 
705                 for (int i = start; i < num; i++) {
706                         
707                         for (int j = 0; j < numSeqs; j++) {
708                                 
709                                 if (m->control_pressed) { inTemp.close(); remove(file.c_str()); return 0; }
710                         
711                                 inTemp >> pref[i][j].name >> pref[i][j].leftParent >> pref[i][j].rightParent;
712                                 inTemp >> pref[i][j].score >> pref[i][j].closestLeft >> pref[i][j].closestRight >> pref[i][j].midpoint;
713                                 gobble(inTemp);
714                         }
715                 }
716                 
717                 inTemp.close();
718                 
719                 remove(file.c_str());
720                 
721                 return 0;
722         }
723         catch(exception& e) {
724                 m->errorOut(e, "Bellerophon", "writePrefs");
725                 exit(1);
726         }
727 }
728 /**************************************************************************************************/
729 vector<string> Bellerophon::getBestWindow(linePair line) {
730         try {
731         
732                 vector<string> best;
733                         
734                 //for each sequence
735                 for (int i = 0; i < numSeqs; i++) {
736                         
737                         //set best pref score to first one
738                         Preference temp = pref[line.start][i];
739                         
740                         if (m->control_pressed) { return best;  }
741                         
742                         //for each window
743                         for (int j = (line.start+1); j < (line.start+line.num); j++) {
744                                 
745                                 //is this a better score
746                                 if (pref[j][i].score > temp.score) {    temp = pref[j][i];              }
747                         }
748                         
749                         string tempString = temp.name + '\t' + temp.leftParent + '\t' + temp.rightParent + '\t' + toString(temp.score);
750                         best.push_back(tempString);
751                 }
752
753                 return best;
754         
755         }
756         catch(exception& e) {
757                 m->errorOut(e, "Bellerophon", "getBestWindow");
758                 exit(1);
759         }
760 }
761 /**************************************************************************************************/
762 int Bellerophon::fillPref(int process, vector<string>& best) {
763         try {
764                 //figure out where you start so you can put the best scores there
765                 int numSeqsPerProcessor = iters / processors;
766                 int start = process * numSeqsPerProcessor;
767                 
768                 for (int i = 0; i < best.size(); i++) {
769                 
770                         if (m->control_pressed) { return 0;  }
771                         
772                         istringstream iss (best[i],istringstream::in);
773                         
774                         string tempScore;
775                         iss >> pref[start][i].name >> pref[start][i].leftParent >> pref[start][i].rightParent >> tempScore;
776                         convert(tempScore, pref[start][i].score); 
777                 }
778
779                 return 0;
780         }
781         catch(exception& e) {
782                 m->errorOut(e, "Bellerophon", "fillPref");
783                 exit(1);
784         }
785 }
786
787 /**************************************************************************************************/
788