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 refSeqs = minCoverageFilter(refSeqs);
39 if (refSeqs.size() < 2) {
40 for (int i = 0; i < refSeqs.size(); i++) { delete refSeqs[i]; }
41 percentIdenticalQueryChimera = 0.0;
45 int chimeraPenalty = computeChimeraPenalty();
48 chimera = chimeraMaligner(chimeraPenalty, decalc);
54 for (int i = 0; i < refSeqs.size(); i++) { delete refSeqs[i]; }
59 m->errorOut(e, "Maligner", "getResults");
63 /***********************************************************************/
64 string Maligner::chimeraMaligner(int chimeraPenalty, DeCalculator* decalc) {
69 //trims seqs to first non gap char in all seqs and last non gap char in all seqs
70 spotMap = decalc->trimSeqs(query, refSeqs);
72 vector<Sequence*> temp = refSeqs;
73 temp.push_back(query);
76 //for (int i = 0; i < temp.size(); i++) { cout << temp[i]->getName() << '\n' << temp[i]->getAligned() << endl; } return "no";
78 vector< vector<score_struct> > matrix = buildScoreMatrix(query->getAligned().length(), refSeqs.size()); //builds and initializes
80 fillScoreMatrix(matrix, refSeqs, chimeraPenalty);
82 vector<score_struct> path = extractHighestPath(matrix);
84 vector<trace_struct> trace = mapTraceRegionsToAlignment(path, refSeqs);
86 if (trace.size() > 1) { chimera = "yes"; }
87 else { chimera = "no"; }
89 int traceStart = path[0].col;
90 int traceEnd = path[path.size()-1].col;
91 string queryInRange = query->getAligned();
92 queryInRange = queryInRange.substr(traceStart, (traceEnd-traceStart+1));
94 string chimeraSeq = constructChimericSeq(trace, refSeqs);
96 percentIdenticalQueryChimera = computePercentID(queryInRange, chimeraSeq);
99 for (int i = 0; i < trace.size(); i++) {
100 int regionStart = trace[i].col;
101 int regionEnd = trace[i].oldCol;
102 int seqIndex = trace[i].row;
106 temp.parent = refSeqs[seqIndex]->getName();
107 temp.parentAligned = db[indexes[seqIndex]]->getAligned();
108 temp.nastRegionStart = spotMap[regionStart];
109 temp.nastRegionEnd = spotMap[regionEnd];
110 temp.regionStart = regionStart;
111 temp.regionEnd = regionEnd;
113 string parentInRange = refSeqs[seqIndex]->getAligned();
114 parentInRange = parentInRange.substr(traceStart, (traceEnd-traceStart+1));
116 temp.queryToParent = computePercentID(queryInRange, parentInRange);
117 temp.divR = (percentIdenticalQueryChimera / temp.queryToParent);
119 string queryInRegion = query->getAligned();
120 queryInRegion = queryInRegion.substr(regionStart, (regionEnd-regionStart+1));
122 string parentInRegion = refSeqs[seqIndex]->getAligned();
123 parentInRegion = parentInRegion.substr(regionStart, (regionEnd-regionStart+1));
125 temp.queryToParentLocal = computePercentID(queryInRegion, parentInRegion);
127 outputResults.push_back(temp);
132 catch(exception& e) {
133 m->errorOut(e, "Maligner", "chimeraMaligner");
137 /***********************************************************************/
138 //removes top matches that do not have minimum coverage with query.
139 vector<Sequence*> Maligner::minCoverageFilter(vector<Sequence*> ref){
141 vector<Sequence*> newRefs;
143 string queryAligned = query->getAligned();
145 for (int i = 0; i < ref.size(); i++) {
147 string refAligned = ref[i]->getAligned();
153 for (int j = 0; j < queryAligned.length(); j++) {
155 if (isalpha(queryAligned[j])) {
158 if (isalpha(refAligned[j])) {
164 int coverage = ((numCovered/(float)numBases)*100);
166 //if coverage above minimum
167 if (coverage > minCoverage) {
168 newRefs.push_back(ref[i]);
174 catch(exception& e) {
175 m->errorOut(e, "Maligner", "minCoverageFilter");
179 /***********************************************************************/
180 // a breakpoint should yield fewer mismatches than this number with respect to the best parent sequence.
181 int Maligner::computeChimeraPenalty() {
184 int numAllowable = ((1.0 - (1.0/minDivR)) * query->getNumBases());
186 int penalty = int(numAllowable + 1) * misMatchPenalty;
191 catch(exception& e) {
192 m->errorOut(e, "Maligner", "computeChimeraPenalty");
196 /***********************************************************************/
197 //this is a vertical filter
198 void Maligner::verticalFilter(vector<Sequence*> seqs) {
200 vector<int> gaps; gaps.resize(query->getAligned().length(), 0);
202 string filterString = (string(query->getAligned().length(), '1'));
205 for (int i = 0; i < seqs.size(); i++) {
207 string seqAligned = seqs[i]->getAligned();
209 for (int j = 0; j < seqAligned.length(); j++) {
210 //if this spot is a gap
211 if ((seqAligned[j] == '-') || (seqAligned[j] == '.')) { gaps[j]++; }
215 //zero out spot where all sequences have blanks
216 int numColRemoved = 0;
217 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
218 if(gaps[i] == seqs.size()) { filterString[i] = '0'; numColRemoved++; }
221 map<int, int> newMap;
223 for (int i = 0; i < seqs.size(); i++) {
225 string seqAligned = seqs[i]->getAligned();
226 string newAligned = "";
229 for (int j = 0; j < seqAligned.length(); j++) {
230 //if this spot is not a gap
231 if (filterString[j] == '1') {
232 newAligned += seqAligned[j];
233 newMap[count] = spotMap[j];
238 seqs[i]->setAligned(newAligned);
243 catch(exception& e) {
244 m->errorOut(e, "Maligner", "verticalFilter");
248 //***************************************************************************************************************
249 vector< vector<score_struct> > Maligner::buildScoreMatrix(int cols, int rows) {
252 vector< vector<score_struct> > m; m.resize(rows);
254 for (int i = 0; i < m.size(); i++) {
255 for (int j = 0; j < cols; j++) {
257 //initialize each cell
260 temp.score = -9999999;
264 m[i].push_back(temp);
270 catch(exception& e) {
271 m->errorOut(e, "Maligner", "buildScoreMatrix");
275 //***************************************************************************************************************
276 void Maligner::fillScoreMatrix(vector<vector<score_struct> >& ms, vector<Sequence*> seqs, int penalty) {
279 //get matrix dimensions
280 int numCols = query->getAligned().length();
281 int numRows = seqs.size();
283 //initialize first col
284 string queryAligned = query->getAligned();
285 for (int i = 0; i < numRows; i++) {
286 string subjectAligned = seqs[i]->getAligned();
289 if ((!isalpha(queryAligned[0])) && (!isalpha(subjectAligned[0]))) {
291 }else if (queryAligned[0] == subjectAligned[0]) {
292 ms[i][0].score = matchScore;
298 //fill rest of matrix
299 for (int j = 1; j < numCols; j++) { //iterate through matrix columns
301 for (int i = 0; i < numRows; i++) { //iterate through matrix rows
303 string subjectAligned = seqs[i]->getAligned();
305 int matchMisMatchScore = 0;
307 if ((!isalpha(queryAligned[j])) && (!isalpha(subjectAligned[j]))) {
309 }else if ((toupper(queryAligned[j]) == 'N') || (toupper(subjectAligned[j]) == 'N')) {
311 }else if (queryAligned[j] == subjectAligned[j]) {
312 matchMisMatchScore = matchScore;
313 }else if (queryAligned[j] != subjectAligned[j]) {
314 matchMisMatchScore = misMatchPenalty;
317 //compute score based on previous columns scores
318 for (int prevIndex = 0; prevIndex < numRows; prevIndex++) { //iterate through rows
320 int sumScore = matchMisMatchScore + ms[prevIndex][j-1].score;
322 //you are not at yourself
323 if (prevIndex != i) { sumScore += penalty; }
324 if (sumScore < 0) { sumScore = 0; }
326 if (sumScore > ms[i][j].score) {
327 ms[i][j].score = sumScore;
328 ms[i][j].prev = prevIndex;
335 catch(exception& e) {
336 m->errorOut(e, "Maligner", "fillScoreMatrix");
340 //***************************************************************************************************************
341 vector<score_struct> Maligner::extractHighestPath(vector<vector<score_struct> > ms) {
344 //get matrix dimensions
345 int numCols = query->getAligned().length();
346 int numRows = ms.size();
349 //find highest score scoring matrix
350 score_struct highestStruct;
351 int highestScore = 0;
353 for (int i = 0; i < numRows; i++) {
354 for (int j = 0; j < numCols; j++) {
355 if (ms[i][j].score > highestScore) {
356 highestScore = ms[i][j].score;
357 highestStruct = ms[i][j];
362 vector<score_struct> path;
364 int rowIndex = highestStruct.row;
365 int pos = highestStruct.col;
366 int score = highestStruct.score;
368 while (pos >= 0 && score > 0) {
369 score_struct temp = ms[rowIndex][pos];
372 if (score > 0) { path.push_back(temp); }
374 rowIndex = temp.prev;
378 reverse(path.begin(), path.end());
383 catch(exception& e) {
384 m->errorOut(e, "Maligner", "extractHighestPath");
388 //***************************************************************************************************************
389 vector<trace_struct> Maligner::mapTraceRegionsToAlignment(vector<score_struct> path, vector<Sequence*> seqs) {
391 vector<trace_struct> trace;
393 int region_index = path[0].row;
394 int region_start = path[0].col;
396 for (int i = 1; i < path.size(); i++) {
398 int next_region_index = path[i].row;
400 if (next_region_index != region_index) {
403 int col_index = path[i].col;
405 temp.col = region_start;
406 temp.oldCol = col_index-1;
407 temp.row = region_index;
409 trace.push_back(temp);
411 region_index = path[i].row;
412 region_start = col_index;
418 temp.col = region_start;
419 temp.oldCol = path[path.size()-1].col;
420 temp.row = region_index;
421 trace.push_back(temp);
426 catch(exception& e) {
427 m->errorOut(e, "Maligner", "mapTraceRegionsToAlignment");
431 //***************************************************************************************************************
432 string Maligner::constructChimericSeq(vector<trace_struct> trace, vector<Sequence*> seqs) {
436 for (int i = 0; i < trace.size(); i++) {
437 string seqAlign = seqs[trace[i].row]->getAligned();
438 seqAlign = seqAlign.substr(trace[i].col, (trace[i].oldCol-trace[i].col+1));
444 catch(exception& e) {
445 m->errorOut(e, "Maligner", "constructChimericSeq");
449 //***************************************************************************************************************
450 float Maligner::computePercentID(string queryAlign, string chimera) {
453 if (queryAlign.length() != chimera.length()) {
454 m->mothurOut("Error, alignment strings are of different lengths: "); m->mothurOutEndLine();
455 m->mothurOut(toString(queryAlign.length())); m->mothurOutEndLine(); m->mothurOutEndLine(); m->mothurOutEndLine(); m->mothurOutEndLine();
456 m->mothurOut(toString(chimera.length())); m->mothurOutEndLine();
462 int numIdentical = 0;
464 for (int i = 0; i < queryAlign.length(); i++) {
465 if ((isalpha(queryAlign[i])) || (isalpha(chimera[i]))) {
467 if (queryAlign[i] == chimera[i]) {
473 if (numBases == 0) { return 0; }
475 float percentIdentical = (numIdentical/(float)numBases) * 100;
477 return percentIdentical;
480 catch(exception& e) {
481 m->errorOut(e, "Maligner", "computePercentID");
485 //***************************************************************************************************************
486 vector<Sequence*> Maligner::getBlastSeqs(Sequence* q, int num) {
489 vector<Sequence*> refResults;
491 Database* database = new BlastDB(-2.0, -1.0, matchScore, misMatchPenalty);
492 for (int i = 0; i < db.size(); i++) { database->addSequence(*db[i]); }
493 database->generateDB();
494 database->setNumSeqs(db.size());
497 string queryUnAligned = q->getUnaligned();
498 string leftQuery = queryUnAligned.substr(0, int(queryUnAligned.length() * 0.33)); //first 1/3 of the sequence
499 string rightQuery = queryUnAligned.substr(int(queryUnAligned.length() * 0.66)); //last 1/3 of the sequence
501 Sequence* queryLeft = new Sequence(q->getName(), leftQuery);
502 Sequence* queryRight = new Sequence(q->getName(), rightQuery);
504 vector<int> tempIndexesRight = database->findClosestMegaBlast(queryRight, num+1);
505 vector<int> tempIndexesLeft = database->findClosestMegaBlast(queryLeft, num+1);
507 //if ((tempIndexesRight.size() != (num+1)) || (tempIndexesLeft.size() != (num+1))) { 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 porcess sequence " + q->getName()); m->mothurOutEndLine(); return refResults; }
512 if (tempIndexesRight.size() < tempIndexesLeft.size()) { smaller = tempIndexesRight; larger = tempIndexesLeft; }
513 else { smaller = tempIndexesLeft; larger = tempIndexesRight; }
517 map<int, int>::iterator it;
519 vector<int> mergedResults;
520 for (int i = 0; i < smaller.size(); i++) {
521 //add left if you havent already
522 it = seen.find(smaller[i]);
523 if (it == seen.end()) {
524 mergedResults.push_back(smaller[i]);
525 seen[smaller[i]] = smaller[i];
528 //add right if you havent already
529 it = seen.find(larger[i]);
530 if (it == seen.end()) {
531 mergedResults.push_back(larger[i]);
532 seen[larger[i]] = larger[i];
536 for (int i = smaller.size(); i < larger.size(); i++) {
537 it = seen.find(larger[i]);
538 if (it == seen.end()) {
539 mergedResults.push_back(larger[i]);
540 seen[larger[i]] = larger[i];
544 if (mergedResults.size() < numWanted) { numWanted = mergedResults.size(); }
545 //cout << q->getName() << endl;
546 for (int i = 0; i < numWanted; i++) {
547 //cout << db[mergedResults[i]]->getName() << endl;
548 if (db[mergedResults[i]]->getName() != q->getName()) {
549 Sequence* temp = new Sequence(db[mergedResults[i]]->getName(), db[mergedResults[i]]->getAligned());
550 refResults.push_back(temp);
551 indexes.push_back(mergedResults[i]);
553 //cout << mergedResults[i] << endl;
562 catch(exception& e) {
563 m->errorOut(e, "Maligner", "getBlastSeqs");
567 //***************************************************************************************************************
568 vector<Sequence*> Maligner::getKmerSeqs(Sequence* q, int num) {
573 string queryUnAligned = q->getUnaligned();
574 string leftQuery = queryUnAligned.substr(0, int(queryUnAligned.length() * 0.33)); //first 1/3 of the sequence
575 string rightQuery = queryUnAligned.substr(int(queryUnAligned.length() * 0.66)); //last 1/3 of the sequence
577 Sequence* queryLeft = new Sequence(q->getName(), leftQuery);
578 Sequence* queryRight = new Sequence(q->getName(), rightQuery);
580 vector<int> tempIndexesLeft = databaseLeft->findClosestSequences(queryLeft, numWanted);
581 vector<int> tempIndexesRight = databaseRight->findClosestSequences(queryRight, numWanted);
585 map<int, int>::iterator it;
587 vector<int> mergedResults;
588 for (int i = 0; i < tempIndexesLeft.size(); i++) {
589 //add left if you havent already
590 it = seen.find(tempIndexesLeft[i]);
591 if (it == seen.end()) {
592 mergedResults.push_back(tempIndexesLeft[i]);
593 seen[tempIndexesLeft[i]] = tempIndexesLeft[i];
596 //add right if you havent already
597 it = seen.find(tempIndexesRight[i]);
598 if (it == seen.end()) {
599 mergedResults.push_back(tempIndexesRight[i]);
600 seen[tempIndexesRight[i]] = tempIndexesRight[i];
604 //cout << q->getName() << endl;
605 vector<Sequence*> refResults;
606 for (int i = 0; i < numWanted; i++) {
607 //cout << db[mergedResults[i]]->getName() << endl;
608 Sequence* temp = new Sequence(db[mergedResults[i]]->getName(), db[mergedResults[i]]->getAligned());
609 refResults.push_back(temp);
610 indexes.push_back(mergedResults[i]);
618 catch(exception& e) {
619 m->errorOut(e, "Maligner", "getBlastSeqs");
623 //***************************************************************************************************************