| 1 |
#ifndef ASSEMBLE_H |
| 2 |
#define ASSEMBLE_H |
| 3 |
|
| 4 |
#include <iostream> |
| 5 |
|
| 6 |
#include "edlib.h" |
| 7 |
#include "msa.h" |
| 8 |
#include "split.h" |
| 9 |
#include "gotoh.h" |
| 10 |
#include "needle.h" |
| 11 |
|
| 12 |
namespace torali |
| 13 |
{ |
| 14 |
struct SeqSlice { |
| 15 |
int32_t svid; |
| 16 |
int32_t sstart; |
| 17 |
int32_t inslen; |
| 18 |
int32_t qual; |
| 19 |
|
| 20 |
SeqSlice() : svid(-1), sstart(-1), inslen(-1), qual(-1) {} |
| 21 |
SeqSlice(int32_t const sv, int32_t const sst, int32_t const il, int32_t q) : svid(sv), sstart(sst), inslen(il), qual(q) {} |
| 22 |
}; |
| 23 |
|
| 24 |
template<typename TAlign> |
| 25 |
inline void |
| 26 |
convertAlignment(std::string const& query, TAlign& align, EdlibAlignMode const modeCode, EdlibAlignResult const& cigar) { |
| 27 |
|
| 28 |
TAlign alignIn; |
| 29 |
alignIn.resize(boost::extents[align.shape()[0]][align.shape()[1]]); |
| 30 |
for(uint32_t i = 0; i < align.shape()[0]; ++i) { |
| 31 |
for(uint32_t j = 0; j < align.shape()[1]; ++j) { |
| 32 |
alignIn[i][j] = align[i][j]; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
uint32_t seqPos = alignIn.shape()[0]; |
| 38 |
int32_t tIdx = -1; |
| 39 |
int32_t qIdx = -1; |
| 40 |
uint32_t missingEnd = 0; |
| 41 |
uint32_t missingStart = 0; |
| 42 |
if (modeCode == EDLIB_MODE_HW) { |
| 43 |
tIdx = cigar.endLocations[0]; |
| 44 |
if (tIdx < (int32_t) alignIn.shape()[1]) missingEnd = alignIn.shape()[1] - tIdx - 1; |
| 45 |
for (int32_t i = 0; i < cigar.alignmentLength; i++) { |
| 46 |
if (cigar.alignment[i] != EDLIB_EDOP_INSERT) tIdx--; |
| 47 |
} |
| 48 |
if (tIdx >= 0) missingStart = tIdx + 1; |
| 49 |
} |
| 50 |
align.resize(boost::extents[alignIn.shape()[0]+1][missingStart + cigar.alignmentLength + missingEnd]); |
| 51 |
|
| 52 |
|
| 53 |
if (modeCode == EDLIB_MODE_HW) { |
| 54 |
if (missingStart) { |
| 55 |
for (uint32_t j = 0; j < missingStart; ++j) { |
| 56 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j] = alignIn[seqIdx][j]; |
| 57 |
align[seqPos][j] = '-'; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
for (int32_t j = 0; j < cigar.alignmentLength; ++j) { |
| 64 |
if (cigar.alignment[j] == EDLIB_EDOP_INSERT) { |
| 65 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j + missingStart] = '-'; |
| 66 |
} else { |
| 67 |
++tIdx; |
| 68 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j + missingStart] = alignIn[seqIdx][tIdx]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
for (int32_t j = 0; j < cigar.alignmentLength; ++j) { |
| 74 |
if (cigar.alignment[j] == EDLIB_EDOP_DELETE) align[seqPos][j + missingStart] = '-'; |
| 75 |
else align[seqPos][j + missingStart] = query[++qIdx]; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
if (modeCode == EDLIB_MODE_HW) { |
| 80 |
if (missingEnd) { |
| 81 |
for (uint32_t j = cigar.alignmentLength + missingStart; j < cigar.alignmentLength + missingStart + missingEnd; ++j) { |
| 82 |
++tIdx; |
| 83 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j] = alignIn[seqIdx][tIdx]; |
| 84 |
align[seqPos][j] = '-'; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
inline void |
| 91 |
buildSuperstring(std::string const& seqI, std::string const& seqJ, std::string& outStr, EdlibAlignResult const& cigar, uint32_t const preI, uint32_t const postI, uint32_t const preJ, uint32_t const postJ) { |
| 92 |
int32_t iIdx = 0; |
| 93 |
int32_t jIdx = 0; |
| 94 |
|
| 95 |
bool firstSeq = false; |
| 96 |
if (preI > preJ) { |
| 97 |
firstSeq = true; |
| 98 |
for(uint32_t j = 0; j < preI; ++j) outStr += seqI[iIdx++]; |
| 99 |
for(uint32_t j = 0; j < preJ; ++j) ++jIdx; |
| 100 |
} else { |
| 101 |
for(uint32_t j = 0; j < preI; ++j) ++iIdx; |
| 102 |
for(uint32_t j = 0; j < preJ; ++j) outStr += seqJ[jIdx++]; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
int32_t bp = cigar.alignmentLength/2; |
| 108 |
for (int32_t j = 0; j < cigar.alignmentLength; ++j) { |
| 109 |
if (bp == j) { |
| 110 |
firstSeq = !firstSeq; |
| 111 |
|
| 112 |
} |
| 113 |
if (cigar.alignment[j] == EDLIB_EDOP_DELETE) { |
| 114 |
if (!firstSeq) outStr += seqJ[jIdx]; |
| 115 |
++jIdx; |
| 116 |
} else if (cigar.alignment[j] == EDLIB_EDOP_INSERT) { |
| 117 |
if (firstSeq) outStr += seqI[iIdx]; |
| 118 |
++iIdx; |
| 119 |
} else { |
| 120 |
if (firstSeq) outStr += seqI[iIdx]; |
| 121 |
else outStr += seqJ[jIdx]; |
| 122 |
++iIdx; |
| 123 |
++jIdx; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if (postI > postJ) { |
| 129 |
for(uint32_t j = 0; j < postI; ++j) outStr += seqI[iIdx++]; |
| 130 |
} else { |
| 131 |
for(uint32_t j = 0; j < postJ; ++j) outStr += seqJ[jIdx++]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
template<typename TAlign> |
| 137 |
inline void |
| 138 |
convertAlignment(std::string const& query, TAlign& align, EdlibAlignMode const modeCode, EdlibAlignResult const& cigar, uint32_t const preI, uint32_t const postI, uint32_t const preJ, uint32_t const postJ) { |
| 139 |
|
| 140 |
TAlign alignIn; |
| 141 |
alignIn.resize(boost::extents[align.shape()[0]][align.shape()[1]]); |
| 142 |
for(uint32_t i = 0; i < align.shape()[0]; ++i) { |
| 143 |
for(uint32_t j = 0; j < align.shape()[1]; ++j) { |
| 144 |
alignIn[i][j] = align[i][j]; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
uint32_t seqPos = alignIn.shape()[0]; |
| 150 |
int32_t tIdx = 0; |
| 151 |
int32_t qIdx = 0; |
| 152 |
align.resize(boost::extents[alignIn.shape()[0]+1][cigar.alignmentLength + preI + postI + preJ + postJ]); |
| 153 |
if (preI) { |
| 154 |
for(uint32_t j = 0; j < preI; ++j) { |
| 155 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j] = alignIn[seqIdx][tIdx]; |
| 156 |
++tIdx; |
| 157 |
align[seqPos][j] = '-'; |
| 158 |
} |
| 159 |
} |
| 160 |
if (preJ) { |
| 161 |
for(uint32_t j = 0; j < preJ; ++j) { |
| 162 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j+preI] = '-'; |
| 163 |
align[seqPos][j+preI] = query[qIdx++]; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
for (int32_t j = 0; j < cigar.alignmentLength; ++j) { |
| 169 |
if (cigar.alignment[j] == EDLIB_EDOP_DELETE) { |
| 170 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j+preI+preJ] = '-'; |
| 171 |
} else { |
| 172 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j+preI+preJ] = alignIn[seqIdx][tIdx]; |
| 173 |
++tIdx; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
for (int32_t j = 0; j < cigar.alignmentLength; ++j) { |
| 179 |
if (cigar.alignment[j] == EDLIB_EDOP_INSERT) align[seqPos][j+preI+preJ] = '-'; |
| 180 |
else align[seqPos][j+preI+preJ] = query[qIdx++]; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if (postI) { |
| 185 |
for(uint32_t j = 0; j < postI; ++j) { |
| 186 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j+preI+preJ+cigar.alignmentLength] = alignIn[seqIdx][tIdx]; |
| 187 |
tIdx++; |
| 188 |
align[seqPos][j+preI+preJ+cigar.alignmentLength] = '-'; |
| 189 |
} |
| 190 |
} |
| 191 |
if (postJ) { |
| 192 |
for(uint32_t j = 0; j < postJ; ++j) { |
| 193 |
for(uint32_t seqIdx = 0; seqIdx < seqPos; ++seqIdx) align[seqIdx][j+preI+preJ+cigar.alignmentLength+postI] = '-'; |
| 194 |
align[seqPos][j+preI+preJ+cigar.alignmentLength+postI] = query[qIdx++]; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
template<typename TAlign> |
| 201 |
inline void |
| 202 |
consensusEdlib(TAlign const& align, std::string& cons) { |
| 203 |
typedef typename TAlign::index TAIndex; |
| 204 |
|
| 205 |
cons.resize(align.shape()[1]); |
| 206 |
for(TAIndex j = 0; j < (TAIndex) align.shape()[1]; ++j) { |
| 207 |
std::vector<int32_t> count(5, 0); |
| 208 |
for(TAIndex i = 0; i < (TAIndex) align.shape()[0]; ++i) { |
| 209 |
if ((align[i][j] == 'A') || (align[i][j] == 'a')) ++count[0]; |
| 210 |
else if ((align[i][j] == 'C') || (align[i][j] == 'c')) ++count[1]; |
| 211 |
else if ((align[i][j] == 'G') || (align[i][j] == 'g')) ++count[2]; |
| 212 |
else if ((align[i][j] == 'T') || (align[i][j] == 't')) ++count[3]; |
| 213 |
else ++count[4]; |
| 214 |
} |
| 215 |
uint32_t maxIdx = 0; |
| 216 |
uint32_t sndIdx = 1; |
| 217 |
if (count[maxIdx] < count[sndIdx]) { |
| 218 |
maxIdx = 1; |
| 219 |
sndIdx = 0; |
| 220 |
} |
| 221 |
for(uint32_t i = 2; i<5; ++i) { |
| 222 |
if (count[i] > count[maxIdx]) { |
| 223 |
sndIdx = maxIdx; |
| 224 |
maxIdx = i; |
| 225 |
} |
| 226 |
else if (count[i] > count[sndIdx]) { |
| 227 |
sndIdx = i; |
| 228 |
} |
| 229 |
} |
| 230 |
if (2 * count[sndIdx] < count[maxIdx]) { |
| 231 |
switch (maxIdx) { |
| 232 |
case 0: cons[j] = 'A'; break; |
| 233 |
case 1: cons[j] = 'C'; break; |
| 234 |
case 2: cons[j] = 'G'; break; |
| 235 |
case 3: cons[j] = 'T'; break; |
| 236 |
default: cons[j] = '-'; break; |
| 237 |
} |
| 238 |
} else { |
| 239 |
uint32_t k1 = maxIdx; |
| 240 |
uint32_t k2 = sndIdx; |
| 241 |
if (k1 > k2) { |
| 242 |
k1 = sndIdx; |
| 243 |
k2 = maxIdx; |
| 244 |
} |
| 245 |
|
| 246 |
if ((k1 == 0) && (k2 == 1)) cons[j] = 'M'; |
| 247 |
else if ((k1 == 0) && (k2 == 2)) cons[j] = 'R'; |
| 248 |
else if ((k1 == 0) && (k2 == 3)) cons[j] = 'W'; |
| 249 |
else if ((k1 == 0) && (k2 == 4)) cons[j] = 'B'; |
| 250 |
else if ((k1 == 1) && (k2 == 2)) cons[j] = 'S'; |
| 251 |
else if ((k1 == 1) && (k2 == 3)) cons[j] = 'Y'; |
| 252 |
else if ((k1 == 1) && (k2 == 4)) cons[j] = 'D'; |
| 253 |
else if ((k1 == 2) && (k2 == 3)) cons[j] = 'K'; |
| 254 |
else if ((k1 == 2) && (k2 == 4)) cons[j] = 'E'; |
| 255 |
else if ((k1 == 3) && (k2 == 4)) cons[j] = 'F'; |
| 256 |
else cons[j] = '-'; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
template<typename TAlign> |
| 263 |
inline void |
| 264 |
consensusWfa(TAlign const& align, std::string& cons) { |
| 265 |
typedef typename TAlign::index TAIndex; |
| 266 |
|
| 267 |
|
| 268 |
std::vector<uint32_t> readStart(align.shape()[0], align.shape()[1]); |
| 269 |
std::vector<uint32_t> readEnd(align.shape()[0], 0); |
| 270 |
for(TAIndex i = 0; i < (TAIndex) align.shape()[0]; ++i) { |
| 271 |
for(TAIndex j = 0; j < (TAIndex) align.shape()[1]; ++j) { |
| 272 |
if (align[i][j] != '-') { |
| 273 |
if (j < readStart[i]) readStart[i] = j; |
| 274 |
if (j > readEnd[i]) readEnd[i] = j; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
cons.resize(align.shape()[1]); |
| 281 |
for(TAIndex j = 0; j < (TAIndex) align.shape()[1]; ++j) { |
| 282 |
std::vector<int32_t> count(5, 0); |
| 283 |
for(TAIndex i = 0; i < (TAIndex) align.shape()[0]; ++i) { |
| 284 |
if ((j >= readStart[i]) && (j <= readEnd[i])) { |
| 285 |
if ((align[i][j] == 'A') || (align[i][j] == 'a')) ++count[0]; |
| 286 |
else if ((align[i][j] == 'C') || (align[i][j] == 'c')) ++count[1]; |
| 287 |
else if ((align[i][j] == 'G') || (align[i][j] == 'g')) ++count[2]; |
| 288 |
else if ((align[i][j] == 'T') || (align[i][j] == 't')) ++count[3]; |
| 289 |
else ++count[4]; |
| 290 |
} |
| 291 |
} |
| 292 |
uint32_t maxIdx = 0; |
| 293 |
uint32_t sndIdx = 1; |
| 294 |
if (count[maxIdx] < count[sndIdx]) { |
| 295 |
maxIdx = 1; |
| 296 |
sndIdx = 0; |
| 297 |
} |
| 298 |
for(uint32_t i = 2; i<5; ++i) { |
| 299 |
if (count[i] > count[maxIdx]) { |
| 300 |
sndIdx = maxIdx; |
| 301 |
maxIdx = i; |
| 302 |
} |
| 303 |
else if (count[i] > count[sndIdx]) { |
| 304 |
sndIdx = i; |
| 305 |
} |
| 306 |
} |
| 307 |
if (2 * count[sndIdx] < count[maxIdx]) { |
| 308 |
switch (maxIdx) { |
| 309 |
case 0: cons[j] = 'A'; break; |
| 310 |
case 1: cons[j] = 'C'; break; |
| 311 |
case 2: cons[j] = 'G'; break; |
| 312 |
case 3: cons[j] = 'T'; break; |
| 313 |
default: cons[j] = '-'; break; |
| 314 |
} |
| 315 |
} else { |
| 316 |
uint32_t k1 = maxIdx; |
| 317 |
uint32_t k2 = sndIdx; |
| 318 |
if (k1 > k2) { |
| 319 |
k1 = sndIdx; |
| 320 |
k2 = maxIdx; |
| 321 |
} |
| 322 |
|
| 323 |
if ((k1 == 0) && (k2 == 1)) cons[j] = 'M'; |
| 324 |
else if ((k1 == 0) && (k2 == 2)) cons[j] = 'R'; |
| 325 |
else if ((k1 == 0) && (k2 == 3)) cons[j] = 'W'; |
| 326 |
else if ((k1 == 0) && (k2 == 4)) cons[j] = 'B'; |
| 327 |
else if ((k1 == 1) && (k2 == 2)) cons[j] = 'S'; |
| 328 |
else if ((k1 == 1) && (k2 == 3)) cons[j] = 'Y'; |
| 329 |
else if ((k1 == 1) && (k2 == 4)) cons[j] = 'D'; |
| 330 |
else if ((k1 == 2) && (k2 == 3)) cons[j] = 'K'; |
| 331 |
else if ((k1 == 2) && (k2 == 4)) cons[j] = 'E'; |
| 332 |
else if ((k1 == 3) && (k2 == 4)) cons[j] = 'F'; |
| 333 |
else cons[j] = '-'; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
inline void |
| 339 |
_trimConsensus(std::string const& prefix, std::string const& suffix, std::string& cs) { |
| 340 |
std::string prefixRev = prefix; |
| 341 |
reverseComplement(prefixRev); |
| 342 |
EdlibAlignResult cigarFwd = edlibAlign(prefix.c_str(), prefix.size(), cs.c_str(), cs.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 343 |
int32_t scoreFwd = cigarFwd.editDistance; |
| 344 |
edlibFreeAlignResult(cigarFwd); |
| 345 |
EdlibAlignResult cigarRev = edlibAlign(prefixRev.c_str(), prefixRev.size(), cs.c_str(), cs.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 346 |
int32_t scoreRev = cigarRev.editDistance; |
| 347 |
edlibFreeAlignResult(cigarRev); |
| 348 |
if (scoreFwd > scoreRev) reverseComplement(cs); |
| 349 |
|
| 350 |
|
| 351 |
EdlibAlignResult cigarPrefix = edlibAlign(prefix.c_str(), prefix.size(), cs.c_str(), cs.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0)); |
| 352 |
uint32_t csStart = infixStart(cigarPrefix); |
| 353 |
|
| 354 |
|
| 355 |
edlibFreeAlignResult(cigarPrefix); |
| 356 |
EdlibAlignResult cigarSuffix = edlibAlign(suffix.c_str(), suffix.size(), cs.c_str(), cs.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0)); |
| 357 |
uint32_t csEnd = infixEnd(cigarSuffix); |
| 358 |
|
| 359 |
|
| 360 |
edlibFreeAlignResult(cigarSuffix); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
if ((csStart < csEnd) && (csEnd < cs.size())) cs = cs.substr(csStart, (csEnd - csStart)); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
template<typename TConfig, typename TSplitReadSet> |
| 369 |
inline int |
| 370 |
msaEdlib(TConfig const& c, TSplitReadSet& sps, std::string& cs) { |
| 371 |
|
| 372 |
std::vector<int32_t> edit(sps.size() * sps.size(), 0); |
| 373 |
for(uint32_t i = 0; i < sps.size(); ++i) { |
| 374 |
for(uint32_t j = i + 1; j < sps.size(); ++j) { |
| 375 |
EdlibAlignResult align = edlibAlign(sps[i].c_str(), sps[i].size(), sps[j].c_str(), sps[j].size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 376 |
edit[i * sps.size() + j] = align.editDistance; |
| 377 |
edit[j * sps.size() + i] = align.editDistance; |
| 378 |
edlibFreeAlignResult(align); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
uint32_t bestIdx = 0; |
| 384 |
int32_t bestVal = sps[0].size(); |
| 385 |
for(uint32_t i = 0; i < sps.size(); ++i) { |
| 386 |
std::vector<int32_t> dist(sps.size()); |
| 387 |
for(uint32_t j = 0; j < sps.size(); ++j) dist[j] = edit[i * sps.size() + j]; |
| 388 |
std::sort(dist.begin(), dist.end()); |
| 389 |
if (dist[sps.size()/2] < bestVal) { |
| 390 |
bestVal = dist[sps.size()/2]; |
| 391 |
bestIdx = i; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
std::vector<std::pair<int32_t, int32_t> > qscores; |
| 397 |
qscores.push_back(std::make_pair(0, bestIdx)); |
| 398 |
std::string revc = sps[bestIdx]; |
| 399 |
reverseComplement(revc); |
| 400 |
for(uint32_t j = 0; j < sps.size(); ++j) { |
| 401 |
if (j != bestIdx) { |
| 402 |
EdlibAlignResult align = edlibAlign(revc.c_str(), revc.size(), sps[j].c_str(), sps[j].size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 403 |
if (align.editDistance < edit[bestIdx * sps.size() + j]) { |
| 404 |
reverseComplement(sps[j]); |
| 405 |
qscores.push_back(std::make_pair(align.editDistance, j)); |
| 406 |
} else qscores.push_back(std::make_pair(edit[bestIdx * sps.size() + j], j)); |
| 407 |
edlibFreeAlignResult(align); |
| 408 |
} |
| 409 |
} |
| 410 |
std::sort(qscores.begin(), qscores.end()); |
| 411 |
|
| 412 |
|
| 413 |
std::vector<uint32_t> selectedIdx; |
| 414 |
uint32_t lastIdx = (uint32_t) (0.8 * qscores.size()); |
| 415 |
if (lastIdx < 3) lastIdx = 3; |
| 416 |
for(uint32_t i = 0; ((i < qscores.size()) && (i < lastIdx)); ++i) selectedIdx.push_back(qscores[i].second); |
| 417 |
|
| 418 |
|
| 419 |
EdlibEqualityPair additionalEqualities[20] = {{'M', 'A'}, {'M', 'C'}, {'R', 'A'}, {'R', 'G'}, {'W', 'A'}, {'W', 'T'}, {'B', 'A'}, {'B', '-'}, {'S', 'C'}, {'S', 'G'}, {'Y', 'C'}, {'Y', 'T'}, {'D', 'C'}, {'D', '-'}, {'K', 'G'}, {'K', 'T'}, {'E', 'G'}, {'E', '-'}, {'F', 'T'}, {'F', '-'}}; |
| 420 |
|
| 421 |
|
| 422 |
typedef boost::multi_array<char, 2> TAlign; |
| 423 |
TAlign align; |
| 424 |
align.resize(boost::extents[1][sps[selectedIdx[0]].size()]); |
| 425 |
uint32_t ind = 0; |
| 426 |
for(typename std::string::const_iterator str = sps[selectedIdx[0]].begin(); str != sps[selectedIdx[0]].end(); ++str) align[0][ind++] = *str; |
| 427 |
for(uint32_t i = 1; i < selectedIdx.size(); ++i) { |
| 428 |
|
| 429 |
std::string alignStr; |
| 430 |
consensusEdlib(align, alignStr); |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
EdlibAlignResult cigar = edlibAlign(sps[selectedIdx[i]].c_str(), sps[selectedIdx[i]].size(), alignStr.c_str(), alignStr.size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_PATH, additionalEqualities, 20)); |
| 442 |
convertAlignment(sps[selectedIdx[i]], align, EDLIB_MODE_NW, cigar); |
| 443 |
edlibFreeAlignResult(cigar); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
std::string gapped; |
| 455 |
consensus(c, align, gapped, cs); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
int32_t trim = (int32_t) (0.05 * cs.size()); |
| 461 |
if (trim > 50) trim = 50; |
| 462 |
int32_t len = (int32_t) (cs.size()) - 2 * trim; |
| 463 |
if (len > 100) cs = cs.substr(trim, len); |
| 464 |
|
| 465 |
|
| 466 |
return align.shape()[0]; |
| 467 |
} |
| 468 |
|
| 469 |
inline uint32_t |
| 470 |
charToInt(char c) { |
| 471 |
switch(c) |
| 472 |
{ |
| 473 |
case 'A': |
| 474 |
return 0; |
| 475 |
case 'C': |
| 476 |
return 1; |
| 477 |
case 'G': |
| 478 |
return 2; |
| 479 |
case 'T': |
| 480 |
return 3; |
| 481 |
case 'B': |
| 482 |
return 0; |
| 483 |
case 'D': |
| 484 |
return 1; |
| 485 |
case 'E': |
| 486 |
return 2; |
| 487 |
case 'F': |
| 488 |
return 3; |
| 489 |
} |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
inline void |
| 496 |
fillKmerTable(std::string const& s, std::vector<uint32_t>& kmerpos) { |
| 497 |
uint32_t len = s.size(); |
| 498 |
kmerpos.resize(std::pow(4, DELLY_KMER + 1)); |
| 499 |
std::fill(kmerpos.begin(), kmerpos.end(), 0); |
| 500 |
uint32_t hash = 0; |
| 501 |
for(uint32_t ki = 0; ((ki < len) && (ki < DELLY_KMER)); ++ki) { |
| 502 |
hash *= 4; |
| 503 |
hash += charToInt(s[ki]); |
| 504 |
} |
| 505 |
for(uint32_t ki = DELLY_KMER; ki < len; ++ki) { |
| 506 |
if (kmerpos[hash]) kmerpos[hash] = DELLY_DUPLICATE; |
| 507 |
else kmerpos[hash] = (ki - DELLY_KMER + 1); |
| 508 |
hash -= (charToInt(s[ki - DELLY_KMER]) * 4 * 4 * 4 * 4 * 4 * 4); |
| 509 |
hash *= 4; |
| 510 |
hash += charToInt(s[ki]); |
| 511 |
} |
| 512 |
if (kmerpos[hash]) kmerpos[hash] = DELLY_DUPLICATE; |
| 513 |
else kmerpos[hash] = (len - DELLY_KMER + 1); |
| 514 |
} |
| 515 |
|
| 516 |
inline int32_t |
| 517 |
bestDiagonal(std::vector<uint32_t> const& kmerHitI, std::vector<uint32_t> const& kmerHitJ, uint32_t const lenI, uint32_t const lenJ) { |
| 518 |
|
| 519 |
std::vector<uint32_t> diag(lenI + lenJ, 0); |
| 520 |
for(uint32_t k = 0; k < std::pow(4, DELLY_KMER + 1); ++k) { |
| 521 |
if ((kmerHitI[k]) && (kmerHitJ[k]) && (kmerHitI[k] != DELLY_DUPLICATE) && (kmerHitJ[k] != DELLY_DUPLICATE)) ++diag[lenJ + kmerHitI[k] - kmerHitJ[k]]; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
uint32_t window = 20; |
| 526 |
uint32_t windowVal = 0; |
| 527 |
for(uint32_t d = 0; ((d < diag.size()) && (d < window)); ++d) windowVal += diag[d]; |
| 528 |
uint32_t bestDiag = window / 2; |
| 529 |
uint32_t bestWindowVal = windowVal; |
| 530 |
for(uint32_t d = window; d < diag.size(); ++d) { |
| 531 |
windowVal -= diag[d - window]; |
| 532 |
windowVal += diag[d]; |
| 533 |
if (windowVal > bestWindowVal) { |
| 534 |
bestWindowVal = windowVal; |
| 535 |
bestDiag = d - window / 2; |
| 536 |
} |
| 537 |
} |
| 538 |
return ((int) (bestDiag) - (int) (lenJ)); |
| 539 |
} |
| 540 |
|
| 541 |
template<typename TConfig, typename TSplitReadSet> |
| 542 |
inline int |
| 543 |
msaWfa(TConfig const& c, TSplitReadSet& sps, std::string& cs, std::string const& prefix, std::string const& suffix) { |
| 544 |
|
| 545 |
std::vector<int32_t> edit(sps.size() * sps.size(), 0); |
| 546 |
std::vector<uint32_t> kmerHitI; |
| 547 |
std::vector<uint32_t> kmerHitJ; |
| 548 |
for(uint32_t i = 0; i < sps.size(); ++i) { |
| 549 |
uint32_t lenI = sps[i].size(); |
| 550 |
fillKmerTable(sps[i], kmerHitI); |
| 551 |
for(uint32_t j = i + 1; j < sps.size(); ++j) { |
| 552 |
uint32_t lenJ = sps[j].size(); |
| 553 |
fillKmerTable(sps[j], kmerHitJ); |
| 554 |
int32_t bestDiag = bestDiagonal(kmerHitI, kmerHitJ, lenI, lenJ); |
| 555 |
std::string seqI; |
| 556 |
std::string seqJ; |
| 557 |
if (bestDiag >= 0) { |
| 558 |
uint32_t seqlen = std::min(lenI - bestDiag, lenJ); |
| 559 |
seqI = sps[i].substr(bestDiag, seqlen); |
| 560 |
seqJ = sps[j].substr(0, seqlen); |
| 561 |
} else { |
| 562 |
uint32_t seqlen = std::min(lenJ + bestDiag, lenI); |
| 563 |
seqI = sps[i].substr(0, seqlen); |
| 564 |
seqJ = sps[j].substr(-1 * bestDiag, seqlen); |
| 565 |
} |
| 566 |
EdlibAlignResult align = edlibAlign(seqI.c_str(), seqI.size(), seqJ.c_str(), seqJ.size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 567 |
int32_t score = (align.editDistance * 1000) / std::max(seqI.size(), seqJ.size()); |
| 568 |
edit[i * sps.size() + j] = score; |
| 569 |
edit[j * sps.size() + i] = score; |
| 570 |
|
| 571 |
edlibFreeAlignResult(align); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
uint32_t bestIdx = 0; |
| 577 |
int32_t bestVal = sps[0].size(); |
| 578 |
for(uint32_t i = 0; i < sps.size(); ++i) { |
| 579 |
std::vector<int32_t> dist(sps.size()); |
| 580 |
for(uint32_t j = 0; j < sps.size(); ++j) dist[j] = edit[i * sps.size() + j]; |
| 581 |
std::sort(dist.begin(), dist.end()); |
| 582 |
if (dist[sps.size()/2] < bestVal) { |
| 583 |
bestVal = dist[sps.size()/2]; |
| 584 |
bestIdx = i; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
std::vector<std::pair<int32_t, int32_t> > qscores; |
| 590 |
qscores.push_back(std::make_pair(0, bestIdx)); |
| 591 |
std::string revc = sps[bestIdx]; |
| 592 |
reverseComplement(revc); |
| 593 |
uint32_t lenI = revc.size(); |
| 594 |
fillKmerTable(revc, kmerHitI); |
| 595 |
for(uint32_t j = 0; j < sps.size(); ++j) { |
| 596 |
if (j != bestIdx) { |
| 597 |
uint32_t lenJ = sps[j].size(); |
| 598 |
fillKmerTable(sps[j], kmerHitJ); |
| 599 |
int32_t bestDiag = bestDiagonal(kmerHitI, kmerHitJ, lenI, lenJ); |
| 600 |
std::string seqI; |
| 601 |
std::string seqJ; |
| 602 |
if (bestDiag >= 0) { |
| 603 |
uint32_t seqlen = std::min(lenI - bestDiag, lenJ); |
| 604 |
seqI = revc.substr(bestDiag, seqlen); |
| 605 |
seqJ = sps[j].substr(0, seqlen); |
| 606 |
} else { |
| 607 |
uint32_t seqlen = std::min(lenJ + bestDiag, lenI); |
| 608 |
seqI = revc.substr(0, seqlen); |
| 609 |
seqJ = sps[j].substr(-1 * bestDiag, seqlen); |
| 610 |
} |
| 611 |
|
| 612 |
EdlibAlignResult align = edlibAlign(seqI.c_str(), seqI.size(), seqJ.c_str(), seqJ.size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 613 |
int32_t score = (align.editDistance * 1000) / std::max(seqI.size(), seqJ.size()); |
| 614 |
if (score < edit[bestIdx * sps.size() + j]) { |
| 615 |
reverseComplement(sps[j]); |
| 616 |
qscores.push_back(std::make_pair(score, j)); |
| 617 |
} else qscores.push_back(std::make_pair(edit[bestIdx * sps.size() + j], j)); |
| 618 |
edlibFreeAlignResult(align); |
| 619 |
} |
| 620 |
} |
| 621 |
std::sort(qscores.begin(), qscores.end()); |
| 622 |
|
| 623 |
|
| 624 |
std::vector<uint32_t> selectedIdx; |
| 625 |
uint32_t lastIdx = (uint32_t) (0.8 * qscores.size()); |
| 626 |
if (lastIdx < 3) lastIdx = 3; |
| 627 |
for(uint32_t i = 0; ((i < qscores.size()) && (i < lastIdx)); ++i) selectedIdx.push_back(qscores[i].second); |
| 628 |
|
| 629 |
|
| 630 |
std::string superStr = sps[selectedIdx[0]]; |
| 631 |
for(uint32_t i = 1; i < selectedIdx.size(); ++i) { |
| 632 |
|
| 633 |
uint32_t lenI = superStr.size(); |
| 634 |
fillKmerTable(superStr, kmerHitI); |
| 635 |
uint32_t lenJ = sps[selectedIdx[i]].size(); |
| 636 |
fillKmerTable(sps[selectedIdx[i]], kmerHitJ); |
| 637 |
int32_t bestDiag = bestDiagonal(kmerHitI, kmerHitJ, lenI, lenJ); |
| 638 |
std::string seqI; |
| 639 |
std::string seqJ; |
| 640 |
uint32_t preI = 0; |
| 641 |
uint32_t postI = 0; |
| 642 |
uint32_t preJ = 0; |
| 643 |
uint32_t postJ = 0; |
| 644 |
uint32_t seqlen = 0; |
| 645 |
if (bestDiag >= 0) { |
| 646 |
seqlen = std::min(lenI - bestDiag, lenJ); |
| 647 |
preI = bestDiag; |
| 648 |
postI = lenI - (bestDiag + seqlen); |
| 649 |
preJ = 0; |
| 650 |
postJ = lenJ - seqlen; |
| 651 |
} else { |
| 652 |
seqlen = std::min(lenJ + bestDiag, lenI); |
| 653 |
preI = 0; |
| 654 |
postI = lenI - seqlen; |
| 655 |
preJ = -1 * bestDiag; |
| 656 |
postJ = lenJ - (-1 * bestDiag + seqlen); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
if ((preI > preJ) && (postI > postJ)) { |
| 662 |
|
| 663 |
} else if ((preJ > preI) && (postJ > postI)) { |
| 664 |
|
| 665 |
superStr = sps[selectedIdx[i]]; |
| 666 |
} else { |
| 667 |
|
| 668 |
if (bestDiag >= 0) { |
| 669 |
seqI = superStr.substr(bestDiag, seqlen); |
| 670 |
seqJ = sps[selectedIdx[i]].substr(0, seqlen); |
| 671 |
} else { |
| 672 |
seqI = superStr.substr(0, seqlen); |
| 673 |
seqJ = sps[selectedIdx[i]].substr(-1 * bestDiag, seqlen); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
EdlibAlignResult cigar = edlibAlign(seqI.c_str(), seqI.size(), seqJ.c_str(), seqJ.size(), edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_PATH, NULL, 0)); |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
std::string outStr; |
| 683 |
buildSuperstring(superStr, sps[selectedIdx[i]], outStr, cigar, preI, postI, preJ, postJ); |
| 684 |
edlibFreeAlignResult(cigar); |
| 685 |
|
| 686 |
|
| 687 |
superStr = outStr; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
EdlibEqualityPair additionalEqualities[20] = {{'M', 'A'}, {'M', 'C'}, {'R', 'A'}, {'R', 'G'}, {'W', 'A'}, {'W', 'T'}, {'B', 'A'}, {'B', '-'}, {'S', 'C'}, {'S', 'G'}, {'Y', 'C'}, {'Y', 'T'}, {'D', 'C'}, {'D', '-'}, {'K', 'G'}, {'K', 'T'}, {'E', 'G'}, {'E', '-'}, {'F', 'T'}, {'F', '-'}}; |
| 693 |
|
| 694 |
|
| 695 |
typedef boost::multi_array<char, 2> TAlign; |
| 696 |
TAlign align; |
| 697 |
align.resize(boost::extents[1][superStr.size()]); |
| 698 |
uint32_t ind = 0; |
| 699 |
for(typename std::string::const_iterator str = superStr.begin(); str != superStr.end(); ++str) align[0][ind++] = *str; |
| 700 |
for(uint32_t i = 0; i < selectedIdx.size(); ++i) { |
| 701 |
|
| 702 |
std::string alignStr; |
| 703 |
consensusWfa(align, alignStr); |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
EdlibAlignResult cigar = edlibAlign(sps[selectedIdx[i]].c_str(), sps[selectedIdx[i]].size(), alignStr.c_str(), alignStr.size(), edlibNewAlignConfig(-1, EDLIB_MODE_HW, EDLIB_TASK_PATH, additionalEqualities, 20)); |
| 715 |
convertAlignment(sps[selectedIdx[i]], align, EDLIB_MODE_HW, cigar); |
| 716 |
edlibFreeAlignResult(cigar); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
std::string gapped; |
| 728 |
consensus(c, align, gapped, cs); |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
if ((!prefix.empty()) && (!suffix.empty())) { |
| 735 |
_trimConsensus(prefix, suffix, cs); |
| 736 |
} else { |
| 737 |
|
| 738 |
int32_t trim = (int32_t) (0.05 * cs.size()); |
| 739 |
if (trim > 50) trim = 50; |
| 740 |
int32_t len = (int32_t) (cs.size()) - 2 * trim; |
| 741 |
if (len > 100) cs = cs.substr(trim, len); |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
return selectedIdx.size(); |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
template<typename TConfig, typename TSplitReadSet> |
| 750 |
inline int |
| 751 |
msaWfa(TConfig const& c, TSplitReadSet& sps, std::string& cs) { |
| 752 |
return msaWfa(c, sps, cs, "", ""); |
| 753 |
} |
| 754 |
|
| 755 |
template<typename TConfig, typename TValidRegion, typename TSRStore> |
| 756 |
inline void |
| 757 |
assemble(TConfig const& c, TValidRegion const& validRegions, std::vector<StructuralVariantRecord>& svs, TSRStore& srStore) { |
| 758 |
typedef typename TSRStore::value_type TPosReadSV; |
| 759 |
|
| 760 |
|
| 761 |
typedef std::vector<std::string> TSequences; |
| 762 |
typedef std::vector<TSequences> TSVSequences; |
| 763 |
TSVSequences seqStore(svs.size(), TSequences()); |
| 764 |
|
| 765 |
|
| 766 |
std::vector<bool> svcons(svs.size(), false); |
| 767 |
|
| 768 |
|
| 769 |
typedef std::vector<samFile*> TSamFile; |
| 770 |
typedef std::vector<hts_idx_t*> TIndex; |
| 771 |
TSamFile samfile(c.files.size()); |
| 772 |
TIndex idx(c.files.size()); |
| 773 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 774 |
samfile[file_c] = sam_open(c.files[file_c].string().c_str(), "r"); |
| 775 |
hts_set_fai_filename(samfile[file_c], c.genome.string().c_str()); |
| 776 |
idx[file_c] = sam_index_load(samfile[file_c], c.files[file_c].string().c_str()); |
| 777 |
} |
| 778 |
bam_hdr_t* hdr = sam_hdr_read(samfile[0]); |
| 779 |
|
| 780 |
|
| 781 |
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
| 782 |
std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "Split-read assembly" << std::endl; |
| 783 |
|
| 784 |
faidx_t* fai = fai_load(c.genome.string().c_str()); |
| 785 |
for(int32_t refIndex = 0; refIndex < hdr->n_targets; ++refIndex) { |
| 786 |
if (validRegions[refIndex].empty()) continue; |
| 787 |
if (srStore[refIndex].empty()) continue; |
| 788 |
|
| 789 |
|
| 790 |
int32_t seqlen = -1; |
| 791 |
std::string tname(hdr->target_name[refIndex]); |
| 792 |
char* seq = faidx_fetch_seq(fai, tname.c_str(), 0, hdr->target_len[refIndex], &seqlen); |
| 793 |
|
| 794 |
|
| 795 |
typedef boost::dynamic_bitset<> TBitSet; |
| 796 |
TBitSet hits(hdr->target_len[refIndex]); |
| 797 |
for(typename TPosReadSV::const_iterator it = srStore[refIndex].begin(); it != srStore[refIndex].end(); ++it) hits[it->first.first] = 1; |
| 798 |
|
| 799 |
|
| 800 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 801 |
|
| 802 |
hts_itr_t* iter = sam_itr_queryi(idx[file_c], refIndex, 0, hdr->target_len[refIndex]); |
| 803 |
bam1_t* rec = bam_init1(); |
| 804 |
while (sam_itr_next(samfile[file_c], iter, rec) >= 0) { |
| 805 |
|
| 806 |
if (rec->core.flag & (BAM_FQCFAIL | BAM_FDUP | BAM_FUNMAP | BAM_FSECONDARY | BAM_FSUPPLEMENTARY)) continue; |
| 807 |
if (!hits[rec->core.pos]) continue; |
| 808 |
|
| 809 |
std::size_t seed = hash_lr(rec); |
| 810 |
if (srStore[refIndex].find(std::make_pair(rec->core.pos, seed)) != srStore[refIndex].end()) { |
| 811 |
|
| 812 |
std::string sequence; |
| 813 |
sequence.resize(rec->core.l_qseq); |
| 814 |
const uint8_t* seqptr = bam_get_seq(rec); |
| 815 |
for (int i = 0; i < rec->core.l_qseq; ++i) sequence[i] = "=ACMGRSVTWYHKDBN"[bam_seqi(seqptr, i)]; |
| 816 |
int32_t readlen = sequence.size(); |
| 817 |
|
| 818 |
|
| 819 |
for(uint32_t ri = 0; ri < srStore[refIndex][std::make_pair(rec->core.pos, seed)].size(); ++ri) { |
| 820 |
SeqSlice seqsl = srStore[refIndex][std::make_pair(rec->core.pos, seed)][ri]; |
| 821 |
int32_t svid = seqsl.svid; |
| 822 |
if ((!svcons[svid]) && (seqStore[svid].size() < c.maxReadPerSV)) { |
| 823 |
|
| 824 |
int32_t window = c.minConsWindow; |
| 825 |
|
| 826 |
window += std::max(svs[svid].ciposhigh - svs[svid].ciposlow, svs[svid].ciendhigh - svs[svid].ciendlow); |
| 827 |
window += seqsl.inslen; |
| 828 |
int32_t sPos = seqsl.sstart - window; |
| 829 |
int32_t ePos = seqsl.sstart + window; |
| 830 |
if (rec->core.flag & BAM_FREVERSE) { |
| 831 |
sPos = (readlen - seqsl.sstart) - window; |
| 832 |
ePos = (readlen - seqsl.sstart) + window; |
| 833 |
} |
| 834 |
if (sPos < 0) sPos = 0; |
| 835 |
if (ePos > (int32_t) readlen) ePos = readlen; |
| 836 |
|
| 837 |
|
| 838 |
if ((ePos - sPos) > window) { |
| 839 |
seqStore[svid].push_back(sequence.substr(sPos, (ePos - sPos))); |
| 840 |
|
| 841 |
|
| 842 |
if ((!_translocation(svs[svid].svt)) && (svs[svid].chr == refIndex)) { |
| 843 |
if ((seqStore[svid].size() == c.maxReadPerSV) || ((int32_t) seqStore[svid].size() == svs[svid].srSupport)) { |
| 844 |
bool msaSuccess = false; |
| 845 |
if (seqStore[svid].size() > 1) { |
| 846 |
|
| 847 |
if (svs[svid].svt != 4) { |
| 848 |
msaEdlib(c, seqStore[svid], svs[svid].consensus); |
| 849 |
|
| 850 |
std::string tmpCons; |
| 851 |
int32_t offsetTmpCons = 0; |
| 852 |
int32_t svSize = svs[svid].svEnd - svs[svid].svStart; |
| 853 |
if (((svs[svid].svt == 0) || (svs[svid].svt == 1)) && (svSize < (int32_t) svs[svid].consensus.size())) { |
| 854 |
offsetTmpCons = (svs[svid].consensus.size() - svSize) / 2; |
| 855 |
tmpCons = svs[svid].consensus; |
| 856 |
svs[svid].consensus = svs[svid].consensus.substr(offsetTmpCons, svSize); |
| 857 |
} |
| 858 |
if (alignConsensus(c, hdr, seq, NULL, svs[svid], true)) msaSuccess = true; |
| 859 |
if (!tmpCons.empty()) { |
| 860 |
svs[svid].consensus = tmpCons; |
| 861 |
svs[svid].consBp += offsetTmpCons; |
| 862 |
} |
| 863 |
} else { |
| 864 |
std::string prefix = boost::to_upper_copy(std::string(seq + std::max(svs[svid].svStart - (int32_t) c.minConsWindow, 0), seq + svs[svid].svStart)); |
| 865 |
std::string suffix = boost::to_upper_copy(std::string(seq + svs[svid].svStart, seq + std::min(seqlen, svs[svid].svStart + c.minConsWindow))); |
| 866 |
msaWfa(c, seqStore[svid], svs[svid].consensus, prefix, suffix); |
| 867 |
if ((int32_t) svs[svid].consensus.size() < svs[svid].insLen + 4 * c.minConsWindow) { |
| 868 |
if (alignConsensus(c, hdr, seq, NULL, svs[svid], false)) msaSuccess = true; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
} |
| 873 |
if (!msaSuccess) { |
| 874 |
svs[svid].consensus = ""; |
| 875 |
svs[svid].srSupport = 0; |
| 876 |
svs[svid].srAlignQuality = 0; |
| 877 |
} |
| 878 |
seqStore[svid].clear(); |
| 879 |
svcons[svid] = true; |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
bam_destroy1(rec); |
| 888 |
hts_itr_destroy(iter); |
| 889 |
} |
| 890 |
|
| 891 |
for(int32_t refIndex2 = 0; refIndex2 <= refIndex; ++refIndex2) { |
| 892 |
char* sndSeq = NULL; |
| 893 |
for(uint32_t svid = 0; svid < svcons.size(); ++svid) { |
| 894 |
if (!svcons[svid]) { |
| 895 |
if (seqStore[svid].size() > 1) { |
| 896 |
bool computeMSA = false; |
| 897 |
if (_translocation(svs[svid].svt)) { |
| 898 |
if ((refIndex2 != refIndex) && (svs[svid].chr == refIndex) && (svs[svid].chr2 == refIndex2)) { |
| 899 |
computeMSA = true; |
| 900 |
|
| 901 |
if (sndSeq == NULL) { |
| 902 |
int32_t seqlen2 = -1; |
| 903 |
std::string tname2(hdr->target_name[refIndex2]); |
| 904 |
sndSeq = faidx_fetch_seq(fai, tname2.c_str(), 0, hdr->target_len[refIndex2], &seqlen2); |
| 905 |
} |
| 906 |
} |
| 907 |
} else { |
| 908 |
if ((refIndex2 == refIndex) && (svs[svid].chr == refIndex) && (svs[svid].chr2 == refIndex2)) computeMSA = true; |
| 909 |
} |
| 910 |
if (computeMSA) { |
| 911 |
bool msaSuccess = false; |
| 912 |
|
| 913 |
if (svs[svid].svt != 4) { |
| 914 |
msaEdlib(c, seqStore[svid], svs[svid].consensus); |
| 915 |
|
| 916 |
std::string tmpCons; |
| 917 |
int32_t offsetTmpCons = 0; |
| 918 |
int32_t svSize = svs[svid].svEnd - svs[svid].svStart; |
| 919 |
if (((svs[svid].svt == 0) || (svs[svid].svt == 1)) && (svSize < (int32_t) svs[svid].consensus.size())) { |
| 920 |
offsetTmpCons = (svs[svid].consensus.size() - svSize) / 2; |
| 921 |
tmpCons = svs[svid].consensus; |
| 922 |
svs[svid].consensus = svs[svid].consensus.substr(offsetTmpCons, svSize); |
| 923 |
} |
| 924 |
if (alignConsensus(c, hdr, seq, sndSeq, svs[svid], true)) msaSuccess = true; |
| 925 |
if (!tmpCons.empty()) { |
| 926 |
svs[svid].consensus = tmpCons; |
| 927 |
svs[svid].consBp += offsetTmpCons; |
| 928 |
} |
| 929 |
} else { |
| 930 |
std::string prefix = boost::to_upper_copy(std::string(seq + std::max(svs[svid].svStart - (int32_t) c.minConsWindow, 0), seq + svs[svid].svStart)); |
| 931 |
std::string suffix = boost::to_upper_copy(std::string(seq + svs[svid].svStart, seq + std::min(seqlen, svs[svid].svStart + c.minConsWindow))); |
| 932 |
msaWfa(c, seqStore[svid], svs[svid].consensus, prefix, suffix); |
| 933 |
if ((int32_t) svs[svid].consensus.size() < svs[svid].insLen + 4 * c.minConsWindow) { |
| 934 |
if (alignConsensus(c, hdr, seq, NULL, svs[svid], false)) msaSuccess = true; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
if (!msaSuccess) { |
| 939 |
svs[svid].consensus = ""; |
| 940 |
svs[svid].srSupport = 0; |
| 941 |
svs[svid].srAlignQuality = 0; |
| 942 |
} |
| 943 |
seqStore[svid].clear(); |
| 944 |
svcons[svid] = true; |
| 945 |
} |
| 946 |
} |
| 947 |
} |
| 948 |
} |
| 949 |
if (sndSeq != NULL) free(sndSeq); |
| 950 |
} |
| 951 |
|
| 952 |
if (seq != NULL) free(seq); |
| 953 |
} |
| 954 |
|
| 955 |
fai_destroy(fai); |
| 956 |
bam_hdr_destroy(hdr); |
| 957 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 958 |
hts_idx_destroy(idx[file_c]); |
| 959 |
sam_close(samfile[file_c]); |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
for(uint32_t svid = 0; svid < svcons.size(); ++svid) { |
| 964 |
if (!svcons[svid]) { |
| 965 |
|
| 966 |
svs[svid].consensus = ""; |
| 967 |
svs[svid].srSupport = 0; |
| 968 |
svs[svid].srAlignQuality = 0; |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
} |
| 975 |
|
| 976 |
#endif |