| 1 | #include "seatac.H" |
| 2 | |
| 3 | |
| 4 | hitMatrix::hitMatrix(uint32 qsLen, uint32 qsIdx) { |
| 5 | _qsLen = qsLen; |
| 6 | _qsIdx = qsIdx; |
| 7 | |
| 8 | // Because this is doing scaffolds or chromosomes against more than |
| 9 | // 1/4 a genome, we expect a LOT of hits. Start off with a good |
| 10 | // amount of memory. |
| 11 | // |
| 12 | // At 8 bytes per diagonalLine, 128M of these is 1GB. Which works |
| 13 | // great for aligning mamalian chromosomes and stinks for microbes. |
| 14 | // |
| 15 | _hitsLen = 0; |
| 16 | _hitsMax = 32 * 1024 * 1024; |
| 17 | _hits = new diagonalLine [_hitsMax]; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | hitMatrix::~hitMatrix() { |
| 22 | delete [] _hits; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | // Utility for sorting the diagonal lines in the hitMatrix |
| 27 | // |
| 28 | // The two comparison functions return true if the first line |
| 29 | // is less than the second line. |
| 30 | |
| 31 | #ifdef WITHOUT_DIAGONALID |
| 32 | |
| 33 | inline |
| 34 | int |
| 35 | compareLines(diagonalLine *A, diagonalLine *B, uint32 qsLen) { |
| 36 | uint32 a = qsLen - A->_qsPos - 1 + A->_dsPos; |
| 37 | uint32 b = qsLen - B->_qsPos - 1 + B->_dsPos; |
| 38 | |
| 39 | return(((a < b)) || |
| 40 | ((a == b) && (A->_qsPos < B->_qsPos))); |
| 41 | } |
| 42 | |
| 43 | inline |
| 44 | int |
| 45 | compareLines(uint32 l, uint32 q, diagonalLine *B, uint32 qsLen) { |
| 46 | uint32 b = qsLen - B->_qsPos - 1 + B->_dsPos; |
| 47 | |
| 48 | return(((l < b)) || |
| 49 | ((l == b) && (q < B->_qsPos))); |
| 50 | } |
| 51 | |
| 52 | inline |
| 53 | void |
| 54 | adjustHeap(diagonalLine *L, int32 p, int32 n, uint32 qsLen) { |
| 55 | uint32 q = L[p]._qsPos; |
| 56 | uint32 d = L[p]._dsPos; |
| 57 | uint32 l = qsLen - q - 1 + d; |
| 58 | int32 c = (p << 1) + 1; // let c be the left child of p |
| 59 | |
| 60 | while (c < n) { |
| 61 | |
| 62 | // Find the larger of the two children |
| 63 | // |
| 64 | if ((c+1 < n) && compareLines(L+c, L+c+1, qsLen)) |
| 65 | c++; |
| 66 | |
| 67 | // Does the node in question fit here? |
| 68 | // |
| 69 | if (compareLines(l, q, L+c, qsLen) == false) |
| 70 | break; |
| 71 | |
| 72 | // Else, swap the parent and the child |
| 73 | // |
| 74 | L[p]._qsPos = L[c]._qsPos; |
| 75 | L[p]._dsPos = L[c]._dsPos; |
| 76 | |
| 77 | // Move down the tree |
| 78 | // |
| 79 | p = c; |
| 80 | c = (p << 1) + 1; |
| 81 | } |
| 82 | |
| 83 | L[p]._qsPos = q; |
| 84 | L[p]._dsPos = d; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | #else // WITH_DIAGONALID |
| 89 | |
| 90 | |
| 91 | inline |
| 92 | int |
| 93 | compareLines(diagonalLine *A, diagonalLine *B) { |
| 94 | return(((A->_diagonalID < B->_diagonalID)) || |
| 95 | ((A->_diagonalID == B->_diagonalID) && (A->_qsPos < B->_qsPos))); |
| 96 | } |
| 97 | |
| 98 | inline |
| 99 | int |
| 100 | compareLines(uint32 l, uint32 q, diagonalLine *B) { |
| 101 | return(((l < B->_diagonalID)) || |
| 102 | ((l == B->_diagonalID) && (q < B->_qsPos))); |
| 103 | } |
| 104 | |
| 105 | inline |
| 106 | void |
| 107 | adjustHeap(diagonalLine *L, int32 p, int32 n) { |
| 108 | uint32 q = L[p]._qsPos; |
| 109 | uint32 d = L[p]._dsPos; |
| 110 | uint32 l = L[p]._diagonalID; |
| 111 | int32 c = (p << 1) + 1; // let c be the left child of p |
| 112 | |
| 113 | while (c < n) { |
| 114 | |
| 115 | // Find the larger of the two children |
| 116 | // |
| 117 | if ((c+1 < n) && compareLines(L+c, L+c+1)) |
| 118 | c++; |
| 119 | |
| 120 | // Does the node in question fit here? |
| 121 | // |
| 122 | if (compareLines(l, q, L+c) == false) |
| 123 | break; |
| 124 | |
| 125 | // Else, swap the parent and the child |
| 126 | // |
| 127 | L[p]._qsPos = L[c]._qsPos; |
| 128 | L[p]._dsPos = L[c]._dsPos; |
| 129 | L[p]._diagonalID = L[c]._diagonalID; |
| 130 | |
| 131 | // Move down the tree |
| 132 | // |
| 133 | p = c; |
| 134 | c = (p << 1) + 1; |
| 135 | } |
| 136 | |
| 137 | L[p]._qsPos = q; |
| 138 | L[p]._dsPos = d; |
| 139 | L[p]._diagonalID = l; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | #endif |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | void |
| 151 | hitMatrix::processMatrix(char direction, filterObj *FO) { |
| 152 | |
| 153 | if (_hitsLen == 0) |
| 154 | return; |
| 155 | |
| 156 | // First, sort by the dsPos. This is done so that we can find all the hits for |
| 157 | // a specific scaffold. |
| 158 | // |
| 159 | sort_dsPos(); |
| 160 | |
| 161 | |
| 162 | merCovering IL(config._merSize); |
| 163 | uint32 ILlength = 0; |
| 164 | |
| 165 | // Now, while there are hits left.... |
| 166 | // |
| 167 | uint32 firstHit = 0; |
| 168 | uint32 lastHit = 0; |
| 169 | uint32 currentSeq = 0; |
| 170 | |
| 171 | while (firstHit < _hitsLen) { |
| 172 | |
| 173 | // Move the currentSeq until the firstHit is below it. |
| 174 | // |
| 175 | while ((currentSeq < config._genome->numberOfSequences()) && |
| 176 | (config._genome->startOf(currentSeq) <= _hits[firstHit]._dsPos)) |
| 177 | currentSeq++; |
| 178 | |
| 179 | // |
| 180 | // currentSeq is now the sequence AFTER the one that we want hits in. |
| 181 | // |
| 182 | |
| 183 | // Find the first hit that is in currentSeq. If this is the last sequence, |
| 184 | // then, of course, all remaining hits are in it. |
| 185 | // |
| 186 | if (currentSeq < config._genome->numberOfSequences()) { |
| 187 | lastHit = firstHit + 1; |
| 188 | while ((lastHit < _hitsLen) && |
| 189 | (_hits[lastHit]._dsPos < config._genome->startOf(currentSeq))) |
| 190 | lastHit++; |
| 191 | } else { |
| 192 | lastHit = _hitsLen; |
| 193 | } |
| 194 | |
| 195 | // Drop back one sequence; this is the sequence the hits are in. |
| 196 | // |
| 197 | currentSeq--; |
| 198 | |
| 199 | |
| 200 | // Adjust the hits to be relative to the start of this sequence |
| 201 | // |
| 202 | for (uint32 i=firstHit; i<lastHit; i++) |
| 203 | _hits[i]._dsPos -= config._genome->startOf(currentSeq); |
| 204 | |
| 205 | // Sort them, if needed. |
| 206 | // |
| 207 | if (lastHit - firstHit > 1) { |
| 208 | |
| 209 | // We cheat; heapsort isn't too friendly to sorting the middle of |
| 210 | // an array, so we make a new array in the middle! |
| 211 | // |
| 212 | diagonalLine *hitsToSort = _hits + firstHit; |
| 213 | |
| 214 | // Build the heap. I initially thought this could be done at the |
| 215 | // same time as the scan for the last hit, but it can't (easily) |
| 216 | // |
| 217 | for (int32 i=(lastHit - firstHit)/2 - 1; i>=0; i--) |
| 218 | #ifdef WITHOUT_DIAGONALID |
| 219 | adjustHeap(hitsToSort, i, lastHit - firstHit, _qsLen); |
| 220 | #else |
| 221 | adjustHeap(hitsToSort, i, lastHit - firstHit); |
| 222 | #endif |
| 223 | |
| 224 | // Sort the hits be diagonal. This is the second part of |
| 225 | // heap sort -- Interchange the new maximum with the element |
| 226 | // at the end of the tree |
| 227 | // |
| 228 | for (uint32 i=lastHit - firstHit - 1; i>0; i--) { |
| 229 | uint32 q = hitsToSort[i]._qsPos; |
| 230 | uint32 d = hitsToSort[i]._dsPos; |
| 231 | #ifndef WITHOUT_DIAGONALID |
| 232 | uint32 l = hitsToSort[i]._diagonalID; |
| 233 | #endif |
| 234 | |
| 235 | hitsToSort[i]._qsPos = hitsToSort[0]._qsPos; |
| 236 | hitsToSort[i]._dsPos = hitsToSort[0]._dsPos; |
| 237 | #ifndef WITHOUT_DIAGONALID |
| 238 | hitsToSort[i]._diagonalID = hitsToSort[0]._diagonalID; |
| 239 | #endif |
| 240 | |
| 241 | hitsToSort[0]._qsPos = q; |
| 242 | hitsToSort[0]._dsPos = d; |
| 243 | #ifndef WITHOUT_DIAGONALID |
| 244 | hitsToSort[0]._diagonalID = l; |
| 245 | #endif |
| 246 | |
| 247 | #ifdef WITHOUT_DIAGONALID |
| 248 | adjustHeap(hitsToSort, 0, i, _qsLen); |
| 249 | #else |
| 250 | adjustHeap(hitsToSort, 0, i); |
| 251 | #endif |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | // Filter them |
| 257 | // |
| 258 | #ifdef WITHOUT_DIAGONALID |
| 259 | uint32 lastDiagonal = _qsLen - _hits[firstHit]._qsPos - 1 + _hits[firstHit]._dsPos; |
| 260 | #else |
| 261 | uint32 lastDiagonal = _hits[firstHit]._diagonalID; |
| 262 | #endif |
| 263 | uint32 qsLow = _hits[firstHit]._qsPos; |
| 264 | uint32 qsHigh = _hits[firstHit]._qsPos; |
| 265 | uint32 dsLow = _hits[firstHit]._dsPos; |
| 266 | uint32 dsHigh = _hits[firstHit]._dsPos; |
| 267 | |
| 268 | IL.clear(); |
| 269 | |
| 270 | for (uint32 i=firstHit; i<lastHit; i++) { |
| 271 | //fprintf(stdout, "hit[%6u] seq=%8u qs=%5u ds=%5u\n", i, currentSeq, _hits[i]._qsPos, _hits[i]._dsPos); |
| 272 | |
| 273 | // |
| 274 | // Extend if on the same diagonal, and consecutive sequence. |
| 275 | // |
| 276 | if ((lastDiagonal == |
| 277 | #ifdef WITHOUT_DIAGONALID |
| 278 | (_qsLen - _hits[i]._qsPos - 1 + _hits[i]._dsPos) |
| 279 | #else |
| 280 | _hits[i]._diagonalID |
| 281 | #endif |
| 282 | ) && |
| 283 | (qsLow <= _hits[i]._qsPos) && |
| 284 | (_hits[i]._qsPos <= qsHigh + config._merSize + config._maxGap)) { |
| 285 | if (qsLow > _hits[i]._qsPos) qsLow = _hits[i]._qsPos; |
| 286 | if (qsHigh < _hits[i]._qsPos) qsHigh = _hits[i]._qsPos; |
| 287 | if (dsLow > _hits[i]._dsPos) dsLow = _hits[i]._dsPos; |
| 288 | if (dsHigh < _hits[i]._dsPos) dsHigh = _hits[i]._dsPos; |
| 289 | IL.addMer(_hits[i]._qsPos); |
| 290 | } else { |
| 291 | |
| 292 | // |
| 293 | // Save the match. cut-n-paste with below. |
| 294 | // |
| 295 | |
| 296 | ILlength = IL.sumOfLengths(); |
| 297 | IL.clear(); |
| 298 | |
| 299 | if (ILlength >= config._minLength) { |
| 300 | if (direction == 'r') { |
| 301 | FO->addHit(direction, |
| 302 | config._genome->IIDOf(currentSeq), |
| 303 | dsLow, |
| 304 | dsHigh - dsLow + config._merSize, |
| 305 | _qsIdx, |
| 306 | _qsLen - qsHigh - config._merSize, |
| 307 | qsHigh - qsLow + config._merSize, |
| 308 | ILlength); |
| 309 | } else { |
| 310 | FO->addHit(direction, |
| 311 | config._genome->IIDOf(currentSeq), |
| 312 | dsLow, |
| 313 | dsHigh - dsLow + config._merSize, |
| 314 | _qsIdx, |
| 315 | qsLow, |
| 316 | qsHigh - qsLow + config._merSize, |
| 317 | ILlength); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | #ifdef WITHOUT_DIAGONALID |
| 322 | lastDiagonal = _qsLen - _hits[i]._qsPos - 1 + _hits[i]._dsPos; |
| 323 | #else |
| 324 | lastDiagonal = _hits[i]._diagonalID; |
| 325 | #endif |
| 326 | qsLow = _hits[i]._qsPos; |
| 327 | qsHigh = _hits[i]._qsPos; |
| 328 | dsLow = _hits[i]._dsPos; |
| 329 | dsHigh = _hits[i]._dsPos; |
| 330 | IL.addMer(_hits[i]._qsPos); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Save the final cluster? (cut-n-paste from above) |
| 335 | // |
| 336 | ILlength = IL.sumOfLengths(); |
| 337 | IL.clear(); |
| 338 | |
| 339 | if (ILlength >= config._minLength) { |
| 340 | if (direction == 'r') { |
| 341 | FO->addHit(direction, |
| 342 | config._genome->IIDOf(currentSeq), |
| 343 | dsLow, |
| 344 | dsHigh - dsLow + config._merSize, |
| 345 | _qsIdx, |
| 346 | _qsLen - qsHigh - config._merSize, |
| 347 | qsHigh - qsLow + config._merSize, |
| 348 | ILlength); |
| 349 | } else { |
| 350 | FO->addHit(direction, |
| 351 | config._genome->IIDOf(currentSeq), |
| 352 | dsLow, |
| 353 | dsHigh - dsLow + config._merSize, |
| 354 | _qsIdx, |
| 355 | qsLow, |
| 356 | qsHigh - qsLow + config._merSize, |
| 357 | ILlength); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // All done with these hits. Move to the next set. |
| 362 | // |
| 363 | firstHit = lastHit; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 |