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