| 1 |
// Author: David Alexander |
| 2 |
|
| 3 |
// |
| 4 |
// This is the basic Hirschberg algo, not the affine variation by Myers and |
| 5 |
// Miller. |
| 6 |
// The tricky part of this code is that we use the a single array to store |
| 7 |
// portions |
| 8 |
// of two rows in the alignment matrices. |
| 9 |
// |
| 10 |
// Straightforward notes here: http://globin.cse.psu.edu/courses/fall2001/DP.pdf |
| 11 |
// ; |
| 12 |
// taken from "Recent Developments in Linear-Space Alignment Methods: A Survey". |
| 13 |
// I follow them pretty closely except for the semiglobal alignment mode |
| 14 |
// implemented |
| 15 |
// here. |
| 16 |
// |
| 17 |
// For the affine (Gotoh) variation, not yet implemented, see Myers & Miller |
| 18 |
// 1988 |
| 19 |
// |
| 20 |
|
| 21 |
#include <ConsensusCore/Align/LinearAlignment.hpp> |
| 22 |
#include <ConsensusCore/Align/PairwiseAlignment.hpp> |
| 23 |
#include <ConsensusCore/Utils.hpp> |
| 24 |
|
| 25 |
#include <boost/numeric/ublas/io.hpp> |
| 26 |
#include <boost/numeric/ublas/matrix.hpp> |
| 27 |
#include <boost/numeric/ublas/vector.hpp> |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <cassert> |
| 31 |
#include <string> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
using ConsensusCore::PairwiseAlignment; |
| 35 |
using ConsensusCore::AlignParams; |
| 36 |
using ConsensusCore::AlignConfig; |
| 37 |
using ConsensusCore::Align; |
| 38 |
using ConsensusCore::GLOBAL; |
| 39 |
using ConsensusCore::SEMIGLOBAL; |
| 40 |
|
| 41 |
//#define DEBUG_LINEAR_ALIGNMENT |
| 42 |
|
| 43 |
#ifdef DEBUG_LINEAR_ALIGNMENT |
| 44 |
#include <iostream> |
| 45 |
using std::cout; |
| 46 |
using std::endl; |
| 47 |
#endif // DEBUG_LINEAR_ALIGNMENT |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
using ConsensusCore::NotYetImplementedException; |
| 52 |
|
| 53 |
namespace ublas = boost::numeric::ublas; |
| 54 |
|
| 55 |
int INSERT_SCORE = -2; |
| 56 |
int DELETE_SCORE = -2; |
| 57 |
int MISMATCH_SCORE = -1; |
| 58 |
int MATCH_SCORE = +2; |
| 59 |
|
| 60 |
const AlignParams params(MATCH_SCORE, MISMATCH_SCORE, INSERT_SCORE, DELETE_SCORE); |
| 61 |
const AlignConfig config(params, GLOBAL); |
| 62 |
|
| 63 |
// |
| 64 |
// Get transcript of NW alignment taking |
| 65 |
// target[j1..j2] into query[i1..i2] (one-based indexing) |
| 66 |
// used for trivial base cases. |
| 67 |
// |
| 68 |
std::string NWTranscript(const std::string& target, int j1, int j2, const std::string& query, |
| 69 |
int i1, int i2, int* score) |
| 70 |
{ |
| 71 |
assert((i1 <= i2) && (j1 <= j2)); |
| 72 |
// implement this inline later |
| 73 |
std::string T = target.substr(j1 - 1, j2 - j1 + 1); |
| 74 |
std::string Q = query.substr(i1 - 1, i2 - i1 + 1); |
| 75 |
PairwiseAlignment* aln = Align(T, Q, score, config); |
| 76 |
std::string X = aln->Transcript(); |
| 77 |
delete aln; |
| 78 |
return X; |
| 79 |
} |
| 80 |
|
| 81 |
#ifndef NDEBUG |
| 82 |
bool CheckTranscript(const std::string& transcript, const std::string& unalnTarget, |
| 83 |
const std::string& unalnQuery) |
| 84 |
{ |
| 85 |
PairwiseAlignment* pa = PairwiseAlignment::FromTranscript(transcript, unalnTarget, unalnQuery); |
| 86 |
if (pa == NULL) { |
| 87 |
return false; |
| 88 |
} else { |
| 89 |
delete pa; |
| 90 |
return true; |
| 91 |
} |
| 92 |
} |
| 93 |
#endif // NDEBUG |
| 94 |
|
| 95 |
// |
| 96 |
// Hirschberg recursion: |
| 97 |
// Find optimal transcript taking target[j1..j2] into query[i1..i2] (one-based |
| 98 |
// indices) |
| 99 |
// Operates by divide-and-conquer, finding midpoint (m, j*) and recursing on |
| 100 |
// halves, then joining. |
| 101 |
// Notes: |
| 102 |
// |
| 103 |
// | Alignment | L | L_1 | L_2 | |
| 104 |
// | Path | (0,0) ~> (I,J) | (0,0) ~> (m, j*) | (m, j*) ~> (I, J) | |
| 105 |
// | T, Q | T[1..J], Q[1..I] | T[1..j*], Q[1..m] | T[j*+1..J], |
| 106 |
// Q[m+1..I] | |
| 107 |
// | Transcript | X | X_1 | X_2 | |
| 108 |
// |
| 109 |
// target on horizontal, query on vertical |
| 110 |
// i refers to query; j refers to target |
| 111 |
// this gives better balanced recursion in the (common) semiglobal case |
| 112 |
// |
| 113 |
std::string OptimalTranscript(const std::string& target, int j1, int j2, const std::string& query, |
| 114 |
int i1, int i2, ublas::vector<int>& buf1, ublas::vector<int>& buf2, |
| 115 |
int* score = NULL) |
| 116 |
{ |
| 117 |
DEBUG_ONLY(std::string subtarget = target.substr(j1 - 1, j2 - j1 + 1); |
| 118 |
std::string subquery = query.substr(i1 - 1, i2 - i1 + 1);) |
| 119 |
|
| 120 |
#ifdef DEBUG_LINEAR_ALIGNMENT |
| 121 |
cout << "Called OptimalTranscript(" |
| 122 |
<< "T[" << j1 << ".." << j2 << "], " |
| 123 |
<< "Q[" << i1 << ".." << i2 << "]) " |
| 124 |
<< " " << target << " " << query << " " |
| 125 |
<< "(" << subtarget << ", " << subquery << ")" << endl; |
| 126 |
#endif |
| 127 |
|
| 128 |
std::string x, x1, x2; |
| 129 |
int segmentScore; |
| 130 |
const AlignParams& configParams = config.Params; |
| 131 |
|
| 132 |
// |
| 133 |
// Base case |
| 134 |
// |
| 135 |
if ((j2 - j1 <= 1) || (i2 - i1 <= 1)) { |
| 136 |
x = NWTranscript(target, j1, j2, query, i1, i2, &segmentScore); |
| 137 |
} |
| 138 |
|
| 139 |
// |
| 140 |
// Recursive case |
| 141 |
// |
| 142 |
else { |
| 143 |
assert(buf1.size() == target.size() + 1); |
| 144 |
assert(buf2.size() == target.size() + 1); |
| 145 |
|
| 146 |
ublas::vector<int>& Sm = buf1; // S- |
| 147 |
ublas::vector<int>& Sp = buf2; // S+ |
| 148 |
|
| 149 |
int mid = (i1 + i2) / 2; |
| 150 |
|
| 151 |
// |
| 152 |
// Score forward, i1 upto mid |
| 153 |
// ( T[j1..j2] vs Q[i1..m] ) |
| 154 |
// |
| 155 |
Sm(j1 - 1) = 0; |
| 156 |
for (int j = j1; j <= j2; j++) { |
| 157 |
Sm(j) = Sm(j - 1) + configParams.Delete; |
| 158 |
} |
| 159 |
for (int i = i1; i <= mid; i++) { |
| 160 |
int s, c; |
| 161 |
s = Sm(j1 - 1); |
| 162 |
c = Sm(j1 - 1) + configParams.Insert; |
| 163 |
Sm(j1 - 1) = c; |
| 164 |
for (int j = j1; j <= j2; j++) { |
| 165 |
char t = target[j - 1]; |
| 166 |
char q = query[i - 1]; |
| 167 |
c = Max3(Sm(j) + configParams.Insert, |
| 168 |
s + (t == q ? configParams.Match : configParams.Mismatch), |
| 169 |
c + configParams.Delete); |
| 170 |
s = Sm(j); |
| 171 |
Sm(j) = c; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// |
| 176 |
// Score backwards, i2 downto mid |
| 177 |
// ( T[j1..j2] vs Q[m+1..i2] ) |
| 178 |
// |
| 179 |
Sp(j2) = 0; |
| 180 |
for (int j = j2 - 1; j >= j1 - 1; j--) { |
| 181 |
Sp(j) = Sp(j + 1) + configParams.Delete; |
| 182 |
} |
| 183 |
for (int i = i2 - 1; i >= mid; i--) { |
| 184 |
int s, c; |
| 185 |
s = Sp(j2); |
| 186 |
c = Sp(j2) + configParams.Delete; |
| 187 |
Sp(j2) = c; |
| 188 |
for (int j = j2 - 1; j >= j1 - 1; j--) { |
| 189 |
char t = target[j]; // j + 1 - 1 |
| 190 |
char q = query[i]; // i + 1 - 1 |
| 191 |
c = Max3(Sp(j) + configParams.Insert, |
| 192 |
s + (t == q ? configParams.Match : configParams.Mismatch), |
| 193 |
c + configParams.Delete); |
| 194 |
s = Sp(j); |
| 195 |
Sp(j) = c; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// |
| 200 |
// Find where optimal path crosses the mid row |
| 201 |
// |
| 202 |
ublas::vector<int> sum = Sm + Sp; |
| 203 |
int j = std::max_element(sum.begin() + j1, sum.begin() + j2 + 1) - sum.begin(); |
| 204 |
segmentScore = sum[j]; |
| 205 |
|
| 206 |
int segment1Score, segment2Score; |
| 207 |
x1 = OptimalTranscript(target, j1, j, query, i1, mid, buf1, buf2, &segment1Score); |
| 208 |
x2 = OptimalTranscript(target, j + 1, j2, query, mid + 1, i2, buf1, buf2, &segment2Score); |
| 209 |
assert(segmentScore == segment1Score + segment2Score); |
| 210 |
x = x1 + x2; |
| 211 |
} |
| 212 |
|
| 213 |
// Check 1: transcript has to take target[j1..j2] into query[i1..i2] |
| 214 |
assert(CheckTranscript(x, subtarget, subquery)); |
| 215 |
|
| 216 |
// Check 2: same score as basic N/W? |
| 217 |
DEBUG_ONLY(int peerScore; |
| 218 |
PairwiseAlignment* peerAlignment = Align(subtarget, subquery, &peerScore, config); |
| 219 |
assert(peerScore == segmentScore); delete peerAlignment;) |
| 220 |
|
| 221 |
if (score != NULL) { |
| 222 |
*score = segmentScore; |
| 223 |
} |
| 224 |
return x; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
PairwiseAlignment* ConsensusCore::AlignLinear(const std::string& target, const std::string& query, |
| 229 |
int* score, AlignConfig) |
| 230 |
{ |
| 231 |
int J = target.length(); |
| 232 |
ublas::vector<int> buf1(J + 1), buf2(J + 1); |
| 233 |
std::string x = |
| 234 |
OptimalTranscript(target, 1, target.length(), query, 1, query.length(), buf1, buf2, score); |
| 235 |
return PairwiseAlignment::FromTranscript(x, target, query); |
| 236 |
} |
| 237 |
|
| 238 |
PairwiseAlignment* ConsensusCore::AlignLinear(const std::string& target, const std::string& query, |
| 239 |
AlignConfig config_) |
| 240 |
{ |
| 241 |
return AlignLinear(target, query, NULL, config_); |
| 242 |
} |