5 * Created by Sarah Westcott on 7/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "bellerophon.h"
11 #include "eachgapdist.h"
12 #include "ignoregaps.h"
13 #include "onegapdist.h"
16 /***************************************************************************************************************/
18 Bellerophon::Bellerophon(string name, bool filterSeqs, bool c, int win, int inc, int p, string o) : Chimera() {
28 seqs = readSeqs(fastafile);
29 numSeqs = seqs.size();
30 if (numSeqs == 0) { m->mothurOut("Error in reading you sequences."); m->mothurOutEndLine(); exit(1); }
34 createFilter(seqs, 0.5);
35 for (int i = 0; i < seqs.size(); i++) { runFilter(seqs[i]); }
38 distCalculator = new eachGapDist();
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);
48 if (increment > (seqs[0]->getAlignLength() - (2*window))) {
49 if (increment != 10) {
51 m->mothurOut("You have selected a increment that is too large. I will use the default."); m->mothurOutEndLine();
53 if (increment > (seqs[0]->getAlignLength() - (2*window))) { increment = 0; }
55 }else{ increment = 0; }
58 if (increment == 0) { iters = 1; }
59 else { iters = ((seqs[0]->getAlignLength() - (2*window)) / increment); }
63 for (int i = 0; i < iters; i++) {
65 for (int j = 0; j < numSeqs; j++) {
66 pref[i].push_back(temp);
72 m->errorOut(e, "Bellerophon", "Bellerophon");
77 //***************************************************************************************************************
78 int Bellerophon::print(ostream& out, ostream& outAcc) {
82 //sorted "best" preference scores for all seqs
83 vector<Preference> best = getBestPref();
85 if (m->control_pressed) { return numSeqs; }
87 out << "Name\tScore\tLeft\tRight\t" << endl;
88 //output prefenence structure to .chimeras file
89 for (int i = 0; i < best.size(); i++) {
91 if (m->control_pressed) { return numSeqs; }
93 out << best[i].name << '\t' << setprecision(3) << best[i].score << '\t' << best[i].leftParent << '\t' << best[i].rightParent << endl;
95 //calc # of seqs with preference above 1.0
96 if (best[i].score > 1.0) {
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();
104 //output results to screen
105 m->mothurOutEndLine();
106 m->mothurOut("Sequence with preference score above 1.0: " + toString(above1)); m->mothurOutEndLine();
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();
121 m->mothurOut("Maximum:\t" + toString(best[spot].score)); m->mothurOutEndLine();
126 catch(exception& e) {
127 m->errorOut(e, "Bellerophon", "print");
132 //***************************************************************************************************************
133 int Bellerophon::print(MPI_File& out, MPI_File& outAcc) {
137 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
140 string outString = "";
142 //sorted "best" preference scores for all seqs
143 vector<Preference> best = getBestPref();
146 int ninetyfive = best.size() * 0.05;
147 float cutoffScore = best[ninetyfive].score;
149 if (m->control_pressed) { return numSeqs; }
151 outString += "Name\tScore\tLeft\tRight\n";
152 //output prefenence structure to .chimeras file
153 for (int i = 0; i < best.size(); i++) {
155 if (m->control_pressed) { return numSeqs; }
157 outString += best[i].name + "\t" + toString(best[i].score) + "\t" + best[i].leftParent + "\t" + best[i].rightParent + "\n";
160 int length = outString.length();
162 strcpy(buf2, outString.c_str());
164 MPI_File_write_shared(out, buf2, length, MPI_CHAR, &status);
167 //calc # of seqs with preference above 95%tile
168 if (best[i].score >= cutoffScore) {
171 outAccString += best[i].name + "\n";
173 MPI_Status statusAcc;
174 length = outAccString.length();
176 strcpy(buf, outAccString.c_str());
178 MPI_File_write_shared(outAcc, buf, length, MPI_CHAR, &statusAcc);
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;
185 //output results to screen
186 m->mothurOutEndLine();
187 m->mothurOut("Sequence with preference score above " + toString(cutoffScore) + ": " + toString(above1)); m->mothurOutEndLine();
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();
202 m->mothurOut("Maximum:\t" + toString(best[spot].score)); m->mothurOutEndLine();
209 catch(exception& e) {
210 m->errorOut(e, "Bellerophon", "print");
215 //********************************************************************************************************************
216 //sorts highest score to lowest
217 inline bool comparePref(Preference left, Preference right){
218 return (left.score > right.score);
220 //***************************************************************************************************************
221 int Bellerophon::getChimeras() {
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; }
229 int pid, numSeqsPerProcessor;
231 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
232 MPI_Comm_size(MPI_COMM_WORLD, &processors);
234 numSeqsPerProcessor = iters / processors;
236 //each process hits this only once
237 int startPos = pid * numSeqsPerProcessor;
238 if(pid == processors - 1){
239 numSeqsPerProcessor = iters - pid * numSeqsPerProcessor;
241 lines.push_back(linePair(startPos, numSeqsPerProcessor));
243 //fill pref with scores
244 driverChimeras(midpoints, lines[0]);
246 if (m->control_pressed) { return 0; }
248 //each process must send its parts back to pid 0
252 for (int j = 1; j < processors; j++) {
254 vector<string> MPIBestSend;
255 for (int i = 0; i < numSeqs; i++) {
257 if (m->control_pressed) { return 0; }
262 MPI_Recv(&length, 1, MPI_INT, j, 2001, MPI_COMM_WORLD, &status);
265 MPI_Recv(&buf, length, MPI_CHAR, j, 2001, MPI_COMM_WORLD, &status);
268 if (temp.length() > length) { temp = temp.substr(0, length); }
270 MPIBestSend.push_back(temp);
273 fillPref(j, MPIBestSend);
275 if (m->control_pressed) { return 0; }
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]);
284 //send your result to parent
285 for (int i = 0; i < numSeqs; i++) {
287 if (m->control_pressed) { return 0; }
289 int bestLength = MPIBestSend[i].length();
290 char buf[bestLength];
291 strcpy(buf, MPIBestSend[i].c_str());
293 MPI_Send(&bestLength, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD);
294 MPI_Send(buf, bestLength, MPI_CHAR, 0, 2001, MPI_COMM_WORLD);
302 //divide breakpoints between processors
303 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
305 lines.push_back(linePair(0, iters));
307 //fill pref with scores
308 driverChimeras(midpoints, lines[0]);
312 int numSeqsPerProcessor = iters / processors;
314 for (int i = 0; i < processors; i++) {
315 int startPos = i * numSeqsPerProcessor;
316 if(i == processors - 1){
317 numSeqsPerProcessor = iters - i * numSeqsPerProcessor;
319 lines.push_back(linePair(startPos, numSeqsPerProcessor));
322 createProcesses(midpoints);
325 lines.push_back(linePair(0, iters));
327 ///fill pref with scores
328 driverChimeras(midpoints, lines[0]);
336 catch(exception& e) {
337 m->errorOut(e, "Bellerophon", "getChimeras");
341 /**************************************************************************************************/
343 int Bellerophon::createProcesses(vector<int> mid) {
345 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
348 vector<int> processIDS;
350 //loop through and create all the processes you want
351 while (process != processors) {
355 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
358 exitCommand = driverChimeras(mid, lines[process]);
359 string tempOut = outputDir + toString(getpid()) + ".temp";
360 writePrefs(tempOut, lines[process]);
362 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
365 //force parent to wait until all the processes are done
366 for (int i=0;i<processors;i++) {
367 int temp = processIDS[i];
371 //get info that other processes created
372 for (int i = 0; i < processIDS.size(); i++) {
373 string tempIn = outputDir + toString(processIDS[i]) + ".temp";
380 catch(exception& e) {
381 m->errorOut(e, "AlignCommand", "createProcesses");
385 //***************************************************************************************************************
386 int Bellerophon::driverChimeras(vector<int> midpoints, linePair line) {
389 for (int h = line.start; h < (line.start + line.num); h++) {
391 int midpoint = midpoints[h];
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;
399 if (m->control_pressed) { return 0; }
401 //create 2 vectors of sequences, 1 for left side and one for right side
402 vector<Sequence> left; vector<Sequence> right;
404 for (int i = 0; i < seqs.size(); i++) {
406 if (m->control_pressed) { return 0; }
408 //cout << "midpoint = " << midpoint << "\twindow = " << window << endl;
409 //cout << "whole = " << seqs[i]->getAligned().length() << endl;
411 string seqLeft = seqs[i]->getAligned().substr(midpoint-window, window);
413 tempLeft.setName(seqs[i]->getName());
414 tempLeft.setAligned(seqLeft);
415 left.push_back(tempLeft);
416 //cout << "left = " << tempLeft.getAligned().length() << endl;
418 string seqRight = seqs[i]->getAligned().substr(midpoint, window);
420 tempRight.setName(seqs[i]->getName());
421 tempRight.setAligned(seqRight);
422 right.push_back(tempRight);
423 //cout << "right = " << seqRight.length() << endl;
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();
433 createSparseMatrix(0, left.size(), SparseLeft, left);
435 if (m->control_pressed) { delete SparseLeft; delete SparseRight; return 0; }
437 createSparseMatrix(0, right.size(), SparseRight, right);
439 if (m->control_pressed) { delete SparseLeft; delete SparseRight; return 0; }
441 left.clear(); right.clear();
442 vector<SeqMap> distMapRight;
443 vector<SeqMap> distMapLeft;
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;
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;
468 //fill preference structure
469 generatePreferences(distMapLeft, distMapRight, midpoint);
471 if (m->control_pressed) { return 0; }
474 if((h+1) % 10 == 0){ cout << "Processing sliding window: " << toString(h+1) << "\n"; m->mothurOutJustToLog("Processing sliding window: " + toString(h+1) + "\n") ; }
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") ; }
484 catch(exception& e) {
485 m->errorOut(e, "Bellerophon", "driverChimeras");
490 /***************************************************************************************************************/
491 int Bellerophon::createSparseMatrix(int startSeq, int endSeq, SparseMatrix* sparse, vector<Sequence> s){
494 for(int i=startSeq; i<endSeq; i++){
496 for(int j=0;j<i;j++){
498 if (m->control_pressed) { return 0; }
500 distCalculator->calcDist(s[i], s[j]);
501 float dist = distCalculator->getDist();
503 PCell temp(i, j, dist);
504 sparse->addCell(temp);
511 catch(exception& e) {
512 m->errorOut(e, "Bellerophon", "createSparseMatrix");
516 /***************************************************************************************************************/
517 int Bellerophon::generatePreferences(vector<SeqMap> left, vector<SeqMap> right, int mid){
521 SeqMap::iterator itR;
522 SeqMap::iterator itL;
524 for (int i = 0; i < left.size(); i++) {
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.
531 for (int j = 0; j < i; j++) {
533 if (m->control_pressed) { return 0; }
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;
540 //if you can find this entry update the preferences
541 if ((itL != currentLeft.end()) && (itR != currentRight.end())) {
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;
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;
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) {
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;
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;
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();
578 if (itR->second < pref[count][j].closestRight) {
579 pref[count][j].closestRight = itR->second;
580 pref[count][j].rightParent = seqs[i]->getName();
592 catch(exception& e) {
593 m->errorOut(e, "Bellerophon", "generatePreferences");
597 /**************************************************************************************************/
598 vector<Preference> Bellerophon::getBestPref() {
601 vector<Preference> best;
604 for (int i = 0; i < numSeqs; i++) {
606 //set best pref score to first one
607 Preference temp = pref[0][i];
609 if (m->control_pressed) { return best; }
612 for (int j = 1; j < pref.size(); j++) {
614 //is this a better score
615 if (pref[j][i].score > temp.score) { temp = pref[j][i]; }
618 best.push_back(temp);
621 //rank preference score to eachother
623 float expectedPercent = 1 / (float) (best.size());
625 for (int i = 0; i < best.size(); i++) { dme += best[i].score; }
627 for (int i = 0; i < best.size(); i++) {
629 if (m->control_pressed) { return best; }
631 //gives the actual percentage of the dme this seq adds
632 best[i].score = best[i].score / dme;
634 //how much higher or lower is this than expected
635 best[i].score = best[i].score / expectedPercent;
639 //sort Preferences highest to lowest
640 sort(best.begin(), best.end(), comparePref);
644 catch(exception& e) {
645 m->errorOut(e, "Bellerophon", "getBestPref");
649 /**************************************************************************************************/
650 int Bellerophon::writePrefs(string file, linePair tempLine) {
654 openOutputFile(file, outTemp);
656 //lets you know what part of the pref matrix you are writing
657 outTemp << tempLine.start << '\t' << tempLine.num << endl;
659 for (int i = tempLine.start; i < (tempLine.start + tempLine.num); i++) {
661 for (int j = 0; j < numSeqs; j++) {
663 if (m->control_pressed) { outTemp.close(); remove(file.c_str()); return 0; }
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;
674 catch(exception& e) {
675 m->errorOut(e, "Bellerophon", "writePrefs");
679 /**************************************************************************************************/
680 int Bellerophon::readPrefs(string file) {
684 openInputFile(file, inTemp);
688 //lets you know what part of the pref matrix you are writing
689 inTemp >> start >> num; gobble(inTemp);
691 for (int i = start; i < num; i++) {
693 for (int j = 0; j < numSeqs; j++) {
695 if (m->control_pressed) { inTemp.close(); remove(file.c_str()); return 0; }
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;
705 remove(file.c_str());
709 catch(exception& e) {
710 m->errorOut(e, "Bellerophon", "writePrefs");
714 /**************************************************************************************************/
715 vector<string> Bellerophon::getBestWindow(linePair line) {
721 for (int i = 0; i < numSeqs; i++) {
723 //set best pref score to first one
724 Preference temp = pref[line.start][i];
726 if (m->control_pressed) { return best; }
729 for (int j = (line.start+1); j < (line.start+line.num); j++) {
731 //is this a better score
732 if (pref[j][i].score > temp.score) { temp = pref[j][i]; }
735 string tempString = temp.name + '\t' + temp.leftParent + '\t' + temp.rightParent + '\t' + toString(temp.score);
736 best.push_back(tempString);
742 catch(exception& e) {
743 m->errorOut(e, "Bellerophon", "getBestWindow");
747 /**************************************************************************************************/
748 int Bellerophon::fillPref(int process, vector<string>& best) {
750 //figure out where you start so you can put the best scores there
751 int numSeqsPerProcessor = iters / processors;
752 int start = process * numSeqsPerProcessor;
754 for (int i = 0; i < best.size(); i++) {
756 if (m->control_pressed) { return 0; }
758 istringstream iss (best[i],istringstream::in);
761 iss >> pref[start][i].name >> pref[start][i].leftParent >> pref[start][i].rightParent >> tempScore;
762 convert(tempScore, pref[start][i].score);
767 catch(exception& e) {
768 m->errorOut(e, "Bellerophon", "fillPref");
773 /**************************************************************************************************/