| 1 | #include "muscle.h" |
|---|
| 2 | #include <math.h> |
|---|
| 3 | #include "pwpath.h" |
|---|
| 4 | #include "profile.h" |
|---|
| 5 | #include <stdio.h> |
|---|
| 6 | |
|---|
| 7 | // Textbook Smith-Waterman affine gap implementation. |
|---|
| 8 | |
|---|
| 9 | #define TRACE 0 |
|---|
| 10 | |
|---|
| 11 | static const char *LocalScoreToStr(SCORE s) |
|---|
| 12 | { |
|---|
| 13 | static char str[16]; |
|---|
| 14 | if (MINUS_INFINITY == s) |
|---|
| 15 | return " *"; |
|---|
| 16 | sprintf(str, "%6.2f", s); |
|---|
| 17 | return str; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | static void ListDP(const SCORE *DPM_, const ProfPos *PA, const ProfPos *PB, |
|---|
| 21 | unsigned uPrefixCountA, unsigned uPrefixCountB) |
|---|
| 22 | { |
|---|
| 23 | Log(" "); |
|---|
| 24 | for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB) |
|---|
| 25 | { |
|---|
| 26 | char c = ' '; |
|---|
| 27 | if (uPrefixLengthB > 0) |
|---|
| 28 | c = ConsensusChar(PB[uPrefixLengthB - 1]); |
|---|
| 29 | Log(" %4u:%c", uPrefixLengthB, c); |
|---|
| 30 | } |
|---|
| 31 | Log("\n"); |
|---|
| 32 | for (unsigned uPrefixLengthA = 0; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA) |
|---|
| 33 | { |
|---|
| 34 | char c = ' '; |
|---|
| 35 | if (uPrefixLengthA > 0) |
|---|
| 36 | c = ConsensusChar(PA[uPrefixLengthA - 1]); |
|---|
| 37 | Log("%4u:%c ", uPrefixLengthA, c); |
|---|
| 38 | for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB) |
|---|
| 39 | Log(" %s", LocalScoreToStr(DPM(uPrefixLengthA, uPrefixLengthB))); |
|---|
| 40 | Log("\n"); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | SCORE SW(const ProfPos *PA, unsigned uLengthA, const ProfPos *PB, |
|---|
| 45 | unsigned uLengthB, PWPath &Path) |
|---|
| 46 | { |
|---|
| 47 | assert(uLengthB > 0 && uLengthA > 0); |
|---|
| 48 | |
|---|
| 49 | const unsigned uPrefixCountA = uLengthA + 1; |
|---|
| 50 | const unsigned uPrefixCountB = uLengthB + 1; |
|---|
| 51 | |
|---|
| 52 | // Allocate DP matrices |
|---|
| 53 | const size_t LM = uPrefixCountA*uPrefixCountB; |
|---|
| 54 | SCORE *DPM_ = new SCORE[LM]; |
|---|
| 55 | SCORE *DPD_ = new SCORE[LM]; |
|---|
| 56 | SCORE *DPI_ = new SCORE[LM]; |
|---|
| 57 | |
|---|
| 58 | DPM(0, 0) = 0; |
|---|
| 59 | DPD(0, 0) = MINUS_INFINITY; |
|---|
| 60 | DPI(0, 0) = MINUS_INFINITY; |
|---|
| 61 | |
|---|
| 62 | DPM(1, 0) = MINUS_INFINITY; |
|---|
| 63 | DPD(1, 0) = MINUS_INFINITY; |
|---|
| 64 | DPI(1, 0) = MINUS_INFINITY; |
|---|
| 65 | |
|---|
| 66 | DPM(0, 1) = MINUS_INFINITY; |
|---|
| 67 | DPD(0, 1) = MINUS_INFINITY; |
|---|
| 68 | DPI(0, 1) = MINUS_INFINITY; |
|---|
| 69 | |
|---|
| 70 | // Empty prefix of B is special case |
|---|
| 71 | for (unsigned uPrefixLengthA = 2; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA) |
|---|
| 72 | { |
|---|
| 73 | // M=LetterA+LetterB, impossible with empty prefix |
|---|
| 74 | DPM(uPrefixLengthA, 0) = MINUS_INFINITY; |
|---|
| 75 | |
|---|
| 76 | // D=LetterA+GapB, never optimal in local alignment with gap penalties |
|---|
| 77 | DPD(uPrefixLengthA, 0) = MINUS_INFINITY; |
|---|
| 78 | |
|---|
| 79 | // I=GapA+LetterB, impossible with empty prefix |
|---|
| 80 | DPI(uPrefixLengthA, 0) = MINUS_INFINITY; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // Empty prefix of A is special case |
|---|
| 84 | for (unsigned uPrefixLengthB = 2; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB) |
|---|
| 85 | { |
|---|
| 86 | // M=LetterA+LetterB, impossible with empty prefix |
|---|
| 87 | DPM(0, uPrefixLengthB) = MINUS_INFINITY; |
|---|
| 88 | |
|---|
| 89 | // D=LetterA+GapB, impossible with empty prefix |
|---|
| 90 | DPD(0, uPrefixLengthB) = MINUS_INFINITY; |
|---|
| 91 | |
|---|
| 92 | // I=GapA+LetterB, never optimal in local alignment with gap penalties |
|---|
| 93 | DPI(0, uPrefixLengthB) = MINUS_INFINITY; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | SCORE scoreMax = MINUS_INFINITY; |
|---|
| 97 | unsigned uPrefixLengthAMax = uInsane; |
|---|
| 98 | unsigned uPrefixLengthBMax = uInsane; |
|---|
| 99 | |
|---|
| 100 | // ============ |
|---|
| 101 | // Main DP loop |
|---|
| 102 | // ============ |
|---|
| 103 | SCORE scoreGapCloseB = MINUS_INFINITY; |
|---|
| 104 | for (unsigned uPrefixLengthB = 1; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB) |
|---|
| 105 | { |
|---|
| 106 | const ProfPos &PPB = PB[uPrefixLengthB - 1]; |
|---|
| 107 | |
|---|
| 108 | SCORE scoreGapCloseA = MINUS_INFINITY; |
|---|
| 109 | for (unsigned uPrefixLengthA = 1; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA) |
|---|
| 110 | { |
|---|
| 111 | const ProfPos &PPA = PA[uPrefixLengthA - 1]; |
|---|
| 112 | |
|---|
| 113 | { |
|---|
| 114 | // Match M=LetterA+LetterB |
|---|
| 115 | SCORE scoreLL = ScoreProfPos2(PPA, PPB); |
|---|
| 116 | |
|---|
| 117 | SCORE scoreMM = DPM(uPrefixLengthA-1, uPrefixLengthB-1); |
|---|
| 118 | SCORE scoreDM = DPD(uPrefixLengthA-1, uPrefixLengthB-1) + scoreGapCloseA; |
|---|
| 119 | SCORE scoreIM = DPI(uPrefixLengthA-1, uPrefixLengthB-1) + scoreGapCloseB; |
|---|
| 120 | |
|---|
| 121 | SCORE scoreBest; |
|---|
| 122 | if (scoreMM >= scoreDM && scoreMM >= scoreIM) |
|---|
| 123 | scoreBest = scoreMM; |
|---|
| 124 | else if (scoreDM >= scoreMM && scoreDM >= scoreIM) |
|---|
| 125 | scoreBest = scoreDM; |
|---|
| 126 | else |
|---|
| 127 | { |
|---|
| 128 | assert(scoreIM >= scoreMM && scoreIM >= scoreDM); |
|---|
| 129 | scoreBest = scoreIM; |
|---|
| 130 | } |
|---|
| 131 | if (scoreBest < 0) |
|---|
| 132 | scoreBest = 0; |
|---|
| 133 | scoreBest += scoreLL; |
|---|
| 134 | if (scoreBest > scoreMax) |
|---|
| 135 | { |
|---|
| 136 | scoreMax = scoreBest; |
|---|
| 137 | uPrefixLengthAMax = uPrefixLengthA; |
|---|
| 138 | uPrefixLengthBMax = uPrefixLengthB; |
|---|
| 139 | } |
|---|
| 140 | DPM(uPrefixLengthA, uPrefixLengthB) = scoreBest; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | { |
|---|
| 144 | // Delete D=LetterA+GapB |
|---|
| 145 | SCORE scoreMD = DPM(uPrefixLengthA-1, uPrefixLengthB) + |
|---|
| 146 | PA[uPrefixLengthA-1].m_scoreGapOpen; |
|---|
| 147 | SCORE scoreDD = DPD(uPrefixLengthA-1, uPrefixLengthB); |
|---|
| 148 | |
|---|
| 149 | SCORE scoreBest; |
|---|
| 150 | if (scoreMD >= scoreDD) |
|---|
| 151 | scoreBest = scoreMD; |
|---|
| 152 | else |
|---|
| 153 | { |
|---|
| 154 | assert(scoreDD >= scoreMD); |
|---|
| 155 | scoreBest = scoreDD; |
|---|
| 156 | } |
|---|
| 157 | DPD(uPrefixLengthA, uPrefixLengthB) = scoreBest; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // Insert I=GapA+LetterB |
|---|
| 161 | { |
|---|
| 162 | SCORE scoreMI = DPM(uPrefixLengthA, uPrefixLengthB-1) + |
|---|
| 163 | PB[uPrefixLengthB - 1].m_scoreGapOpen; |
|---|
| 164 | SCORE scoreII = DPI(uPrefixLengthA, uPrefixLengthB-1); |
|---|
| 165 | |
|---|
| 166 | SCORE scoreBest; |
|---|
| 167 | if (scoreMI >= scoreII) |
|---|
| 168 | scoreBest = scoreMI; |
|---|
| 169 | else |
|---|
| 170 | { |
|---|
| 171 | assert(scoreII > scoreMI); |
|---|
| 172 | scoreBest = scoreII; |
|---|
| 173 | } |
|---|
| 174 | DPI(uPrefixLengthA, uPrefixLengthB) = scoreBest; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | scoreGapCloseA = PPA.m_scoreGapClose; |
|---|
| 178 | } |
|---|
| 179 | scoreGapCloseB = PPB.m_scoreGapClose; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | #if TRACE |
|---|
| 183 | Log("DPM:\n"); |
|---|
| 184 | ListDP(DPM_, PA, PB, uPrefixLengthA, uPrefixLengthB); |
|---|
| 185 | Log("DPD:\n"); |
|---|
| 186 | ListDP(DPD_, PA, PB, uPrefixLengthA, uPrefixLengthB); |
|---|
| 187 | Log("DPI:\n"); |
|---|
| 188 | ListDP(DPI_, PA, PB, uPrefixLengthA, uPrefixLengthB); |
|---|
| 189 | #endif |
|---|
| 190 | |
|---|
| 191 | assert(scoreMax == DPM(uPrefixLengthAMax, uPrefixLengthBMax)); |
|---|
| 192 | TraceBackSW(PA, uLengthA, PB, uLengthB, DPM_, DPD_, DPI_, |
|---|
| 193 | uPrefixLengthAMax, uPrefixLengthBMax, Path); |
|---|
| 194 | |
|---|
| 195 | #if TRACE |
|---|
| 196 | SCORE scorePath = FastScorePath2(PA, uLengthA, PB, uLengthB, Path); |
|---|
| 197 | Path.LogMe(); |
|---|
| 198 | Log("Score = %s Path = %s\n", LocalScoreToStr(scoreMax), LocalScoreToStr(scorePath)); |
|---|
| 199 | #endif |
|---|
| 200 | |
|---|
| 201 | delete[] DPM_; |
|---|
| 202 | delete[] DPD_; |
|---|
| 203 | delete[] DPI_; |
|---|
| 204 | |
|---|
| 205 | return scoreMax; |
|---|
| 206 | } |
|---|