5 * Created by westcott on 9/23/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
12 #include "blastdb.hpp"
14 /***********************************************************************/
15 Maligner::Maligner(vector<Sequence*> temp, int num, int match, int misMatch, float div, int ms, int minCov, string mode, Database* dataLeft, Database* dataRight) :
16 db(temp), numWanted(num), matchScore(match), misMatchPenalty(misMatch), minDivR(div), minSimilarity(ms), minCoverage(minCov), searchMethod(mode), databaseLeft(dataLeft), databaseRight(dataRight) { m = MothurOut::getInstance(); }
17 /***********************************************************************/
18 string Maligner::getResults(Sequence* q, DeCalculator* decalc) {
21 outputResults.clear();
23 //make copy so trimming doesn't destroy query from calling class - remember to deallocate
24 query = new Sequence(q->getName(), q->getAligned());
28 if (searchMethod == "distance") {
29 //find closest seqs to query in template - returns copies of seqs so trim does not destroy - remember to deallocate
30 refSeqs = decalc->findClosest(query, db, numWanted, indexes);
31 }else if (searchMethod == "blast") {
32 refSeqs = getBlastSeqs(query, numWanted); //fills indexes
33 }else if (searchMethod == "kmer") {
34 refSeqs = getKmerSeqs(query, numWanted); //fills indexes
35 }else { m->mothurOut("not valid search."); exit(1); } //should never get here
37 if (m->control_pressed) { return chimera; }
39 refSeqs = minCoverageFilter(refSeqs);
41 if (refSeqs.size() < 2) {
42 for (int i = 0; i < refSeqs.size(); i++) { delete refSeqs[i]; }
43 percentIdenticalQueryChimera = 0.0;
47 int chimeraPenalty = computeChimeraPenalty();
50 chimera = chimeraMaligner(chimeraPenalty, decalc);
52 if (m->control_pressed) { return chimera; }
57 for (int i = 0; i < refSeqs.size(); i++) { delete refSeqs[i]; }
62 m->errorOut(e, "Maligner", "getResults");
66 /***********************************************************************/
67 string Maligner::chimeraMaligner(int chimeraPenalty, DeCalculator* decalc) {
72 //trims seqs to first non gap char in all seqs and last non gap char in all seqs
73 spotMap = decalc->trimSeqs(query, refSeqs);
75 //you trimmed the whole sequence, skip
76 if (query->getAligned() == "") { return "no"; }
78 vector<Sequence*> temp = refSeqs;
79 temp.push_back(query);
82 //for (int i = 0; i < temp.size(); i++) { cout << temp[i]->getName() << '\n' << temp[i]->getAligned() << endl; } return "no";
84 vector< vector<score_struct> > matrix = buildScoreMatrix(query->getAligned().length(), refSeqs.size()); //builds and initializes
86 if (m->control_pressed) { return chimera; }
88 fillScoreMatrix(matrix, refSeqs, chimeraPenalty);
90 vector<score_struct> path = extractHighestPath(matrix);
92 if (m->control_pressed) { return chimera; }
94 vector<trace_struct> trace = mapTraceRegionsToAlignment(path, refSeqs);
96 if (trace.size() > 1) { chimera = "yes"; }
97 else { chimera = "no"; }
99 int traceStart = path[0].col;
100 int traceEnd = path[path.size()-1].col;
101 string queryInRange = query->getAligned();
102 queryInRange = queryInRange.substr(traceStart, (traceEnd-traceStart+1));
104 string chimeraSeq = constructChimericSeq(trace, refSeqs);
106 percentIdenticalQueryChimera = computePercentID(queryInRange, chimeraSeq);
108 if (m->control_pressed) { return chimera; }
110 //save output results
111 for (int i = 0; i < trace.size(); i++) {
112 int regionStart = trace[i].col;
113 int regionEnd = trace[i].oldCol;
114 int seqIndex = trace[i].row;
118 temp.parent = refSeqs[seqIndex]->getName();
119 temp.parentAligned = db[indexes[seqIndex]]->getAligned();
120 temp.nastRegionStart = spotMap[regionStart];
121 temp.nastRegionEnd = spotMap[regionEnd];
122 temp.regionStart = regionStart;
123 temp.regionEnd = regionEnd;
125 string parentInRange = refSeqs[seqIndex]->getAligned();
126 parentInRange = parentInRange.substr(traceStart, (traceEnd-traceStart+1));
128 temp.queryToParent = computePercentID(queryInRange, parentInRange);
129 temp.divR = (percentIdenticalQueryChimera / temp.queryToParent);
131 string queryInRegion = query->getAligned();
132 queryInRegion = queryInRegion.substr(regionStart, (regionEnd-regionStart+1));
134 string parentInRegion = refSeqs[seqIndex]->getAligned();
135 parentInRegion = parentInRegion.substr(regionStart, (regionEnd-regionStart+1));
137 temp.queryToParentLocal = computePercentID(queryInRegion, parentInRegion);
139 outputResults.push_back(temp);
144 catch(exception& e) {
145 m->errorOut(e, "Maligner", "chimeraMaligner");
149 /***********************************************************************/
150 //removes top matches that do not have minimum coverage with query.
151 vector<Sequence*> Maligner::minCoverageFilter(vector<Sequence*> ref){
153 vector<Sequence*> newRefs;
155 string queryAligned = query->getAligned();
157 for (int i = 0; i < ref.size(); i++) {
159 string refAligned = ref[i]->getAligned();
165 for (int j = 0; j < queryAligned.length(); j++) {
167 if (isalpha(queryAligned[j])) {
170 if (isalpha(refAligned[j])) {
176 int coverage = ((numCovered/(float)numBases)*100);
178 //if coverage above minimum
179 if (coverage > minCoverage) {
180 newRefs.push_back(ref[i]);
188 catch(exception& e) {
189 m->errorOut(e, "Maligner", "minCoverageFilter");
193 /***********************************************************************/
194 // a breakpoint should yield fewer mismatches than this number with respect to the best parent sequence.
195 int Maligner::computeChimeraPenalty() {
198 int numAllowable = ((1.0 - (1.0/minDivR)) * query->getNumBases());
200 int penalty = int(numAllowable + 1) * misMatchPenalty;
205 catch(exception& e) {
206 m->errorOut(e, "Maligner", "computeChimeraPenalty");
210 /***********************************************************************/
211 //this is a vertical filter
212 void Maligner::verticalFilter(vector<Sequence*> seqs) {
214 vector<int> gaps; gaps.resize(query->getAligned().length(), 0);
216 string filterString = (string(query->getAligned().length(), '1'));
219 for (int i = 0; i < seqs.size(); i++) {
221 string seqAligned = seqs[i]->getAligned();
223 for (int j = 0; j < seqAligned.length(); j++) {
224 //if this spot is a gap
225 if ((seqAligned[j] == '-') || (seqAligned[j] == '.')) { gaps[j]++; }
229 //zero out spot where all sequences have blanks
230 int numColRemoved = 0;
231 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
232 if(gaps[i] == seqs.size()) { filterString[i] = '0'; numColRemoved++; }
235 map<int, int> newMap;
237 for (int i = 0; i < seqs.size(); i++) {
239 string seqAligned = seqs[i]->getAligned();
240 string newAligned = "";
243 for (int j = 0; j < seqAligned.length(); j++) {
244 //if this spot is not a gap
245 if (filterString[j] == '1') {
246 newAligned += seqAligned[j];
247 newMap[count] = spotMap[j];
252 seqs[i]->setAligned(newAligned);
257 catch(exception& e) {
258 m->errorOut(e, "Maligner", "verticalFilter");
262 //***************************************************************************************************************
263 vector< vector<score_struct> > Maligner::buildScoreMatrix(int cols, int rows) {
266 vector< vector<score_struct> > m; m.resize(rows);
268 for (int i = 0; i < m.size(); i++) {
269 for (int j = 0; j < cols; j++) {
271 //initialize each cell
274 temp.score = -9999999;
278 m[i].push_back(temp);
284 catch(exception& e) {
285 m->errorOut(e, "Maligner", "buildScoreMatrix");
289 //***************************************************************************************************************
290 void Maligner::fillScoreMatrix(vector<vector<score_struct> >& ms, vector<Sequence*> seqs, int penalty) {
293 //get matrix dimensions
294 int numCols = query->getAligned().length();
295 int numRows = seqs.size();
297 //initialize first col
298 string queryAligned = query->getAligned();
299 for (int i = 0; i < numRows; i++) {
300 string subjectAligned = seqs[i]->getAligned();
303 if ((!isalpha(queryAligned[0])) && (!isalpha(subjectAligned[0]))) {
305 }else if (queryAligned[0] == subjectAligned[0]) {
306 ms[i][0].score = matchScore;
312 //fill rest of matrix
313 for (int j = 1; j < numCols; j++) { //iterate through matrix columns
315 for (int i = 0; i < numRows; i++) { //iterate through matrix rows
317 string subjectAligned = seqs[i]->getAligned();
319 int matchMisMatchScore = 0;
321 if ((!isalpha(queryAligned[j])) && (!isalpha(subjectAligned[j]))) {
323 }else if ((toupper(queryAligned[j]) == 'N') || (toupper(subjectAligned[j]) == 'N')) {
325 }else if (queryAligned[j] == subjectAligned[j]) {
326 matchMisMatchScore = matchScore;
327 }else if (queryAligned[j] != subjectAligned[j]) {
328 matchMisMatchScore = misMatchPenalty;
331 //compute score based on previous columns scores
332 for (int prevIndex = 0; prevIndex < numRows; prevIndex++) { //iterate through rows
334 int sumScore = matchMisMatchScore + ms[prevIndex][j-1].score;
336 //you are not at yourself
337 if (prevIndex != i) { sumScore += penalty; }
338 if (sumScore < 0) { sumScore = 0; }
340 if (sumScore > ms[i][j].score) {
341 ms[i][j].score = sumScore;
342 ms[i][j].prev = prevIndex;
349 catch(exception& e) {
350 m->errorOut(e, "Maligner", "fillScoreMatrix");
354 //***************************************************************************************************************
355 vector<score_struct> Maligner::extractHighestPath(vector<vector<score_struct> > ms) {
358 //get matrix dimensions
359 int numCols = query->getAligned().length();
360 int numRows = ms.size();
363 //find highest score scoring matrix
364 score_struct highestStruct;
365 int highestScore = 0;
367 for (int i = 0; i < numRows; i++) {
368 for (int j = 0; j < numCols; j++) {
369 if (ms[i][j].score > highestScore) {
370 highestScore = ms[i][j].score;
371 highestStruct = ms[i][j];
376 vector<score_struct> path;
378 int rowIndex = highestStruct.row;
379 int pos = highestStruct.col;
380 int score = highestStruct.score;
382 while (pos >= 0 && score > 0) {
383 score_struct temp = ms[rowIndex][pos];
386 if (score > 0) { path.push_back(temp); }
388 rowIndex = temp.prev;
392 reverse(path.begin(), path.end());
397 catch(exception& e) {
398 m->errorOut(e, "Maligner", "extractHighestPath");
402 //***************************************************************************************************************
403 vector<trace_struct> Maligner::mapTraceRegionsToAlignment(vector<score_struct> path, vector<Sequence*> seqs) {
405 vector<trace_struct> trace;
407 int region_index = path[0].row;
408 int region_start = path[0].col;
410 for (int i = 1; i < path.size(); i++) {
412 int next_region_index = path[i].row;
414 if (next_region_index != region_index) {
417 int col_index = path[i].col;
419 temp.col = region_start;
420 temp.oldCol = col_index-1;
421 temp.row = region_index;
423 trace.push_back(temp);
425 region_index = path[i].row;
426 region_start = col_index;
432 temp.col = region_start;
433 temp.oldCol = path[path.size()-1].col;
434 temp.row = region_index;
435 trace.push_back(temp);
440 catch(exception& e) {
441 m->errorOut(e, "Maligner", "mapTraceRegionsToAlignment");
445 //***************************************************************************************************************
446 string Maligner::constructChimericSeq(vector<trace_struct> trace, vector<Sequence*> seqs) {
450 for (int i = 0; i < trace.size(); i++) {
451 string seqAlign = seqs[trace[i].row]->getAligned();
452 seqAlign = seqAlign.substr(trace[i].col, (trace[i].oldCol-trace[i].col+1));
458 catch(exception& e) {
459 m->errorOut(e, "Maligner", "constructChimericSeq");
463 //***************************************************************************************************************
464 float Maligner::computePercentID(string queryAlign, string chimera) {
467 if (queryAlign.length() != chimera.length()) {
468 m->mothurOut("Error, alignment strings are of different lengths: "); m->mothurOutEndLine();
469 m->mothurOut(toString(queryAlign.length())); m->mothurOutEndLine(); m->mothurOutEndLine(); m->mothurOutEndLine(); m->mothurOutEndLine();
470 m->mothurOut(toString(chimera.length())); m->mothurOutEndLine();
476 int numIdentical = 0;
478 for (int i = 0; i < queryAlign.length(); i++) {
479 if ((isalpha(queryAlign[i])) || (isalpha(chimera[i]))) {
481 if (queryAlign[i] == chimera[i]) {
487 if (numBases == 0) { return 0; }
489 float percentIdentical = (numIdentical/(float)numBases) * 100;
491 return percentIdentical;
494 catch(exception& e) {
495 m->errorOut(e, "Maligner", "computePercentID");
499 //***************************************************************************************************************
500 vector<Sequence*> Maligner::getBlastSeqs(Sequence* q, int num) {
503 vector<Sequence*> refResults;
506 string queryUnAligned = q->getUnaligned();
507 string leftQuery = queryUnAligned.substr(0, int(queryUnAligned.length() * 0.33)); //first 1/3 of the sequence
508 string rightQuery = queryUnAligned.substr(int(queryUnAligned.length() * 0.66)); //last 1/3 of the sequence
510 Sequence* queryLeft = new Sequence(q->getName()+"left", leftQuery);
511 Sequence* queryRight = new Sequence(q->getName()+"right", rightQuery);
513 vector<int> tempIndexesRight = databaseLeft->findClosestMegaBlast(queryRight, num+1);
514 vector<int> tempIndexesLeft = databaseLeft->findClosestMegaBlast(queryLeft, num+1);
516 //if ((tempIndexesRight.size() == 0) && (tempIndexesLeft.size() == 0)) { m->mothurOut("megablast returned " + toString(tempIndexesRight.size()) + " results for the right end, and " + toString(tempIndexesLeft.size()) + " for the left end. Needed " + toString(num+1) + ". Unable to process sequence " + q->getName()); m->mothurOutEndLine(); return refResults; }
521 if (tempIndexesRight.size() < tempIndexesLeft.size()) { smaller = tempIndexesRight; larger = tempIndexesLeft; }
522 else { smaller = tempIndexesLeft; larger = tempIndexesRight; }
526 map<int, int>::iterator it;
528 vector<int> mergedResults;
529 for (int i = 0; i < smaller.size(); i++) {
530 //add left if you havent already
531 it = seen.find(smaller[i]);
532 if (it == seen.end()) {
533 mergedResults.push_back(smaller[i]);
534 seen[smaller[i]] = smaller[i];
537 //add right if you havent already
538 it = seen.find(larger[i]);
539 if (it == seen.end()) {
540 mergedResults.push_back(larger[i]);
541 seen[larger[i]] = larger[i];
545 for (int i = smaller.size(); i < larger.size(); i++) {
546 it = seen.find(larger[i]);
547 if (it == seen.end()) {
548 mergedResults.push_back(larger[i]);
549 seen[larger[i]] = larger[i];
553 if (mergedResults.size() < numWanted) { numWanted = mergedResults.size(); }
554 //cout << q->getName() << endl;
555 for (int i = 0; i < numWanted; i++) {
556 //cout << db[mergedResults[i]]->getName() << endl;
557 if (db[mergedResults[i]]->getName() != q->getName()) {
558 Sequence* temp = new Sequence(db[mergedResults[i]]->getName(), db[mergedResults[i]]->getAligned());
559 refResults.push_back(temp);
560 indexes.push_back(mergedResults[i]);
562 //cout << mergedResults[i] << endl;
570 catch(exception& e) {
571 m->errorOut(e, "Maligner", "getBlastSeqs");
575 //***************************************************************************************************************
576 vector<Sequence*> Maligner::getKmerSeqs(Sequence* q, int num) {
581 string queryUnAligned = q->getUnaligned();
582 string leftQuery = queryUnAligned.substr(0, int(queryUnAligned.length() * 0.33)); //first 1/3 of the sequence
583 string rightQuery = queryUnAligned.substr(int(queryUnAligned.length() * 0.66)); //last 1/3 of the sequence
585 Sequence* queryLeft = new Sequence(q->getName(), leftQuery);
586 Sequence* queryRight = new Sequence(q->getName(), rightQuery);
588 vector<int> tempIndexesLeft = databaseLeft->findClosestSequences(queryLeft, numWanted);
589 vector<int> tempIndexesRight = databaseRight->findClosestSequences(queryRight, numWanted);
593 map<int, int>::iterator it;
595 vector<int> mergedResults;
596 for (int i = 0; i < tempIndexesLeft.size(); i++) {
597 //add left if you havent already
598 it = seen.find(tempIndexesLeft[i]);
599 if (it == seen.end()) {
600 mergedResults.push_back(tempIndexesLeft[i]);
601 seen[tempIndexesLeft[i]] = tempIndexesLeft[i];
604 //add right if you havent already
605 it = seen.find(tempIndexesRight[i]);
606 if (it == seen.end()) {
607 mergedResults.push_back(tempIndexesRight[i]);
608 seen[tempIndexesRight[i]] = tempIndexesRight[i];
612 //cout << q->getName() << endl;
613 vector<Sequence*> refResults;
614 for (int i = 0; i < numWanted; i++) {
615 //cout << db[mergedResults[i]]->getName() << endl;
616 Sequence* temp = new Sequence(db[mergedResults[i]]->getName(), db[mergedResults[i]]->getAligned());
617 refResults.push_back(temp);
618 indexes.push_back(mergedResults[i]);
626 catch(exception& e) {
627 m->errorOut(e, "Maligner", "getKmerSeqs");
631 //***************************************************************************************************************