| 1 |
#ifndef CLUSTER_H |
| 2 |
#define CLUSTER_H |
| 3 |
|
| 4 |
#include <boost/filesystem.hpp> |
| 5 |
#include <boost/algorithm/string.hpp> |
| 6 |
#include <boost/algorithm/string.hpp> |
| 7 |
#include <boost/iostreams/filtering_streambuf.hpp> |
| 8 |
#include <boost/iostreams/filtering_stream.hpp> |
| 9 |
#include <boost/iostreams/copy.hpp> |
| 10 |
#include <boost/iostreams/filter/gzip.hpp> |
| 11 |
#include <boost/iostreams/device/file.hpp> |
| 12 |
#include <boost/math/distributions/binomial.hpp> |
| 13 |
|
| 14 |
#include <htslib/sam.h> |
| 15 |
|
| 16 |
#include "util.h" |
| 17 |
#include "junction.h" |
| 18 |
|
| 19 |
namespace torali |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
struct BamAlignRecord { |
| 25 |
int32_t tid; |
| 26 |
int32_t pos; |
| 27 |
int32_t mtid; |
| 28 |
int32_t mpos; |
| 29 |
int32_t alen; |
| 30 |
int32_t malen; |
| 31 |
int32_t Median; |
| 32 |
int32_t Mad; |
| 33 |
int32_t maxNormalISize; |
| 34 |
uint32_t flag; |
| 35 |
uint8_t MapQuality; |
| 36 |
|
| 37 |
BamAlignRecord(bam1_t* rec, uint8_t pairQuality, uint16_t a, uint16_t ma, int32_t median, int32_t mad, int32_t maxISize) : tid(rec->core.tid), pos(rec->core.pos), mtid(rec->core.mtid), mpos(rec->core.mpos), alen(a), malen(ma), Median(median), Mad(mad), maxNormalISize(maxISize), flag(rec->core.flag), MapQuality(pairQuality) {} |
| 38 |
|
| 39 |
bool operator<(const BamAlignRecord& s2) const { |
| 40 |
if (tid==mtid) { |
| 41 |
return ((std::min(pos, mpos) < std::min(s2.pos, s2.mpos)) || |
| 42 |
((std::min(pos, mpos) == std::min(s2.pos, s2.mpos)) && (std::max(pos, mpos) < std::max(s2.pos, s2.mpos))) || |
| 43 |
((std::min(pos, mpos) == std::min(s2.pos, s2.mpos)) && (std::max(pos, mpos) == std::max(s2.pos, s2.mpos)) && (maxNormalISize < s2.maxNormalISize))); |
| 44 |
} else { |
| 45 |
return ((pos < s2.pos) || |
| 46 |
((pos == s2.pos) && (mpos < s2.mpos)) || |
| 47 |
((pos == s2.pos) && (mpos == s2.mpos) && (maxNormalISize < s2.maxNormalISize))); |
| 48 |
} |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
template<typename TWeight, typename TVertex> |
| 54 |
struct EdgeRecord { |
| 55 |
typedef TVertex TVertexType; |
| 56 |
TVertex source; |
| 57 |
TVertex target; |
| 58 |
TWeight weight; |
| 59 |
|
| 60 |
EdgeRecord(TVertex s, TVertex t, TWeight w) : source(s), target(t), weight(w) {} |
| 61 |
|
| 62 |
bool operator<(const EdgeRecord e2) const { |
| 63 |
return ((weight < e2.weight) || ((weight == e2.weight) && (source < e2.source)) || ((weight == e2.weight) && (source == e2.source) && (target < e2.target))); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
template<typename TBamRecord, typename TSize> |
| 69 |
inline void |
| 70 |
_initClique(TBamRecord const& el, TSize& svStart, TSize& svEnd, TSize& wiggle, int32_t const svt) { |
| 71 |
if (_translocation(svt)) { |
| 72 |
uint8_t ct = _getSpanOrientation(svt); |
| 73 |
if (ct%2==0) { |
| 74 |
svStart = el.pos + el.alen; |
| 75 |
if (ct>=2) svEnd = el.mpos; |
| 76 |
else svEnd = el.mpos + el.malen; |
| 77 |
} else { |
| 78 |
svStart = el.pos; |
| 79 |
if (ct>=2) svEnd = el.mpos + el.malen; |
| 80 |
else svEnd = el.mpos; |
| 81 |
} |
| 82 |
wiggle=el.maxNormalISize; |
| 83 |
} else { |
| 84 |
if (svt == 0) { |
| 85 |
svStart = el.mpos + el.malen; |
| 86 |
svEnd = el.pos + el.alen; |
| 87 |
wiggle = el.maxNormalISize - std::max(el.alen, el.malen); |
| 88 |
} else if (svt == 1) { |
| 89 |
svStart = el.mpos; |
| 90 |
svEnd = el.pos; |
| 91 |
wiggle = el.maxNormalISize - std::max(el.alen, el.malen); |
| 92 |
} else if (svt == 2) { |
| 93 |
svStart = el.mpos + el.malen; |
| 94 |
svEnd = el.pos; |
| 95 |
wiggle = -el.maxNormalISize; |
| 96 |
} else if (svt == 3) { |
| 97 |
svStart = el.mpos; |
| 98 |
svEnd = el.pos + el.alen; |
| 99 |
wiggle = el.maxNormalISize; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
template<typename TBamRecord, typename TSize> |
| 106 |
inline bool |
| 107 |
_updateClique(TBamRecord const& el, TSize& svStart, TSize& svEnd, TSize& wiggle, int32_t const svt) |
| 108 |
{ |
| 109 |
if (_translocation(svt)) { |
| 110 |
int ct = _getSpanOrientation(svt); |
| 111 |
TSize newSvStart; |
| 112 |
TSize newSvEnd; |
| 113 |
TSize newWiggle = wiggle; |
| 114 |
if (ct%2==0) { |
| 115 |
newSvStart = std::max(svStart, el.pos + el.alen); |
| 116 |
newWiggle -= (newSvStart - svStart); |
| 117 |
if (ct>=2) { |
| 118 |
newSvEnd = std::min(svEnd, el.mpos); |
| 119 |
newWiggle -= (svEnd - newSvEnd); |
| 120 |
} else { |
| 121 |
newSvEnd = std::max(svEnd, el.mpos + el.malen); |
| 122 |
newWiggle -= (newSvEnd - svEnd); |
| 123 |
} |
| 124 |
} else { |
| 125 |
newSvStart = std::min(svStart, el.pos); |
| 126 |
newWiggle -= (svStart - newSvStart); |
| 127 |
if (ct>=2) { |
| 128 |
newSvEnd = std::max(svEnd, el.mpos + el.malen); |
| 129 |
newWiggle -= (newSvEnd - svEnd); |
| 130 |
} else { |
| 131 |
newSvEnd = std::min(svEnd, el.mpos); |
| 132 |
newWiggle -= (svEnd - newSvEnd); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (newWiggle>0) { |
| 137 |
svStart = newSvStart; |
| 138 |
svEnd = newSvEnd; |
| 139 |
wiggle = newWiggle; |
| 140 |
return true; |
| 141 |
} |
| 142 |
return false; |
| 143 |
} else { |
| 144 |
if ((svt == 0) || (svt == 1)) { |
| 145 |
int ct = _getSpanOrientation(svt); |
| 146 |
TSize newSvStart; |
| 147 |
TSize newSvEnd; |
| 148 |
TSize newWiggle; |
| 149 |
TSize wiggleChange; |
| 150 |
if (!ct) { |
| 151 |
newSvStart = std::max(svStart, el.mpos + el.malen); |
| 152 |
newSvEnd = std::max(svEnd, el.pos + el.alen); |
| 153 |
newWiggle = std::min(el.maxNormalISize - (newSvStart - el.mpos), el.maxNormalISize - (newSvEnd - el.pos)); |
| 154 |
wiggleChange = wiggle - std::max(newSvStart - svStart, newSvEnd - svEnd); |
| 155 |
} else { |
| 156 |
newSvStart = std::min(svStart, el.mpos); |
| 157 |
newSvEnd = std::min(svEnd, el.pos); |
| 158 |
newWiggle = std::min(el.maxNormalISize - (el.mpos + el.malen - newSvStart), el.maxNormalISize - (el.pos + el.alen - newSvEnd)); |
| 159 |
wiggleChange = wiggle - std::max(svStart - newSvStart, svEnd - newSvEnd); |
| 160 |
} |
| 161 |
if (wiggleChange < newWiggle) newWiggle=wiggleChange; |
| 162 |
|
| 163 |
|
| 164 |
if ((newSvStart < newSvEnd) && (newWiggle>=0)) { |
| 165 |
svStart = newSvStart; |
| 166 |
svEnd = newSvEnd; |
| 167 |
wiggle = newWiggle; |
| 168 |
return true; |
| 169 |
} |
| 170 |
return false; |
| 171 |
} else if (svt == 2) { |
| 172 |
TSize newSvStart = std::max(svStart, el.mpos + el.malen); |
| 173 |
TSize newSvEnd = std::min(svEnd, el.pos); |
| 174 |
TSize newWiggle = el.pos + el.alen - el.mpos - el.maxNormalISize - (newSvEnd - newSvStart); |
| 175 |
TSize wiggleChange = wiggle + (svEnd-svStart) - (newSvEnd - newSvStart); |
| 176 |
if (wiggleChange > newWiggle) newWiggle=wiggleChange; |
| 177 |
|
| 178 |
|
| 179 |
if ((newSvStart < newSvEnd) && (newWiggle<=0)) { |
| 180 |
svStart = newSvStart; |
| 181 |
svEnd = newSvEnd; |
| 182 |
wiggle = newWiggle; |
| 183 |
return true; |
| 184 |
} |
| 185 |
return false; |
| 186 |
} else if (svt == 3) { |
| 187 |
TSize newSvStart = std::min(svStart, el.mpos); |
| 188 |
TSize newSvEnd = std::max(svEnd, el.pos + el.alen); |
| 189 |
TSize newWiggle = el.pos - (el.mpos + el.malen) + el.maxNormalISize - (newSvEnd - newSvStart); |
| 190 |
TSize wiggleChange = wiggle - ((newSvEnd - newSvStart) - (svEnd-svStart)); |
| 191 |
if (wiggleChange < newWiggle) newWiggle = wiggleChange; |
| 192 |
|
| 193 |
|
| 194 |
if ((newSvStart < newSvEnd) && (newWiggle>=0)) { |
| 195 |
svStart = newSvStart; |
| 196 |
svEnd = newSvEnd; |
| 197 |
wiggle = newWiggle; |
| 198 |
return true; |
| 199 |
} |
| 200 |
return false; |
| 201 |
} |
| 202 |
} |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
template<typename TConfig, typename TCompEdgeList> |
| 208 |
inline void |
| 209 |
_searchCliques(TConfig const& c, TCompEdgeList& compEdge, std::vector<SRBamRecord>& br, std::vector<StructuralVariantRecord>& sv, int32_t const svt) { |
| 210 |
typedef typename TCompEdgeList::mapped_type TEdgeList; |
| 211 |
typedef typename TEdgeList::value_type TEdgeRecord; |
| 212 |
typedef typename TEdgeRecord::TVertexType TVertex; |
| 213 |
|
| 214 |
|
| 215 |
for(typename TCompEdgeList::iterator compIt = compEdge.begin(); compIt != compEdge.end(); ++compIt) { |
| 216 |
|
| 217 |
std::sort(compIt->second.begin(), compIt->second.end()); |
| 218 |
|
| 219 |
|
| 220 |
typename TEdgeList::const_iterator itWEdge = compIt->second.begin(); |
| 221 |
typename TEdgeList::const_iterator itWEdgeEnd = compIt->second.end(); |
| 222 |
typedef std::set<TVertex> TCliqueMembers; |
| 223 |
typedef std::set<std::size_t> TSeeds; |
| 224 |
TCliqueMembers clique; |
| 225 |
TCliqueMembers incompatible; |
| 226 |
TSeeds seeds; |
| 227 |
|
| 228 |
|
| 229 |
clique.insert(itWEdge->source); |
| 230 |
seeds.insert(br[itWEdge->source].id); |
| 231 |
int32_t chr = br[itWEdge->source].chr; |
| 232 |
int32_t chr2 = br[itWEdge->source].chr2; |
| 233 |
int32_t ciposlow = br[itWEdge->source].pos; |
| 234 |
uint64_t pos = br[itWEdge->source].pos; |
| 235 |
int32_t ciposhigh = br[itWEdge->source].pos; |
| 236 |
int32_t ciendlow = br[itWEdge->source].pos2; |
| 237 |
uint64_t pos2 = br[itWEdge->source].pos2; |
| 238 |
int32_t ciendhigh = br[itWEdge->source].pos2; |
| 239 |
int32_t mapq = br[itWEdge->source].qual; |
| 240 |
int32_t inslen = br[itWEdge->source].inslen; |
| 241 |
|
| 242 |
|
| 243 |
uint32_t wiggle = c.maxReadSep; |
| 244 |
if (_translocation(svt)) wiggle = 2 * c.maxReadSep; |
| 245 |
else { |
| 246 |
|
| 247 |
uint32_t svvar = std::abs(0.1 * (pos2 - pos)); |
| 248 |
if (svt == 4) svvar = std::abs(0.1 * inslen); |
| 249 |
if (wiggle < svvar) wiggle = svvar; |
| 250 |
if (wiggle > 1000) wiggle = 1000; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
bool cliqueGrow = true; |
| 255 |
while (cliqueGrow) { |
| 256 |
itWEdge = compIt->second.begin(); |
| 257 |
cliqueGrow = false; |
| 258 |
|
| 259 |
for(;(!cliqueGrow) && (itWEdge != itWEdgeEnd);++itWEdge) { |
| 260 |
TVertex v; |
| 261 |
if ((clique.find(itWEdge->source) == clique.end()) && (clique.find(itWEdge->target) != clique.end())) v = itWEdge->source; |
| 262 |
else if ((clique.find(itWEdge->source) != clique.end()) && (clique.find(itWEdge->target) == clique.end())) v = itWEdge->target; |
| 263 |
else continue; |
| 264 |
if (incompatible.find(v) != incompatible.end()) continue; |
| 265 |
if (seeds.find(br[v].id) != seeds.end()) continue; |
| 266 |
|
| 267 |
int32_t newCiPosLow = std::min(br[v].pos, ciposlow); |
| 268 |
int32_t newCiPosHigh = std::max(br[v].pos, ciposhigh); |
| 269 |
int32_t newCiEndLow = std::min(br[v].pos2, ciendlow); |
| 270 |
int32_t newCiEndHigh = std::max(br[v].pos2, ciendhigh); |
| 271 |
if (((newCiPosHigh - newCiPosLow) < (int32_t) wiggle) && ((newCiEndHigh - newCiEndLow) < (int32_t) wiggle)) cliqueGrow = true; |
| 272 |
if (cliqueGrow) { |
| 273 |
|
| 274 |
clique.insert(v); |
| 275 |
seeds.insert(br[v].id); |
| 276 |
ciposlow = newCiPosLow; |
| 277 |
pos += br[v].pos; |
| 278 |
ciposhigh = newCiPosHigh; |
| 279 |
ciendlow = newCiEndLow; |
| 280 |
pos2 += br[v].pos2; |
| 281 |
ciendhigh = newCiEndHigh; |
| 282 |
mapq += br[v].qual; |
| 283 |
inslen += br[v].inslen; |
| 284 |
} else incompatible.insert(v); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
std::size_t prevSeed = 0; |
| 290 |
uint32_t cliqSize = 0; |
| 291 |
TSeeds selectedSeeds; |
| 292 |
for(typename TSeeds::const_iterator itS = seeds.begin(); itS != seeds.end(); ++itS) { |
| 293 |
if (prevSeed + 1 != *itS) { |
| 294 |
selectedSeeds.insert(*itS); |
| 295 |
++cliqSize; |
| 296 |
} |
| 297 |
prevSeed = *itS; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
if (cliqSize >= c.minCliqueSize) { |
| 302 |
int32_t svStart = (int32_t) (pos / (uint64_t) clique.size()); |
| 303 |
int32_t svEnd = (int32_t) (pos2 / (uint64_t) clique.size()); |
| 304 |
int32_t svInsLen = (int32_t) (inslen / (int32_t) clique.size()); |
| 305 |
if (_svSizeCheck(svStart, svEnd, svt, svInsLen)) { |
| 306 |
if ((ciposlow > svStart) || (ciposhigh < svStart) || (ciendlow > svEnd) || (ciendhigh < svEnd)) { |
| 307 |
std::cerr << "Warning: Confidence intervals out of bounds: " << ciposlow << ',' << svStart << ',' << ciposhigh << ':' << ciendlow << ',' << svEnd << ',' << ciendhigh << std::endl; |
| 308 |
} |
| 309 |
int32_t svid = sv.size(); |
| 310 |
sv.push_back(StructuralVariantRecord(chr, svStart, chr2, svEnd, (ciposlow - svStart), (ciposhigh - svStart), (ciendlow - svEnd), (ciendhigh - svEnd), cliqSize, mapq / clique.size(), mapq, svInsLen, svt, svid)); |
| 311 |
|
| 312 |
for(typename TCliqueMembers::iterator itC = clique.begin(); itC != clique.end(); ++itC) { |
| 313 |
if (selectedSeeds.find(br[*itC].id) != selectedSeeds.end()) { |
| 314 |
|
| 315 |
br[*itC].svid = svid; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
template<typename TConfig> |
| 325 |
inline void |
| 326 |
cluster(TConfig const& c, std::vector<SRBamRecord>& br, std::vector<StructuralVariantRecord>& sv, int32_t const svt) { |
| 327 |
uint32_t count = 0; |
| 328 |
for(int32_t refIdx = 0; refIdx < c.nchr; ++refIdx) { |
| 329 |
|
| 330 |
|
| 331 |
typedef std::vector<uint32_t> TComponent; |
| 332 |
TComponent comp; |
| 333 |
comp.resize(br.size(), 0); |
| 334 |
uint32_t numComp = 0; |
| 335 |
|
| 336 |
|
| 337 |
typedef uint32_t TWeightType; |
| 338 |
typedef uint32_t TVertex; |
| 339 |
typedef EdgeRecord<TWeightType, TVertex> TEdgeRecord; |
| 340 |
typedef std::vector<TEdgeRecord> TEdgeList; |
| 341 |
typedef std::map<uint32_t, TEdgeList> TCompEdgeList; |
| 342 |
TCompEdgeList compEdge; |
| 343 |
|
| 344 |
|
| 345 |
std::size_t lastConnectedNode = 0; |
| 346 |
std::size_t lastConnectedNodeStart = 0; |
| 347 |
for(uint32_t i = 0; i<br.size(); ++i) { |
| 348 |
if (br[i].chr == refIdx) { |
| 349 |
++count; |
| 350 |
|
| 351 |
if (i > lastConnectedNode) { |
| 352 |
|
| 353 |
if (!compEdge.empty()) { |
| 354 |
|
| 355 |
_searchCliques(c, compEdge, br, sv, svt); |
| 356 |
lastConnectedNodeStart = lastConnectedNode; |
| 357 |
compEdge.clear(); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
uint32_t varisize = c.maxReadSep; |
| 363 |
if (_translocation(svt)) varisize = 2 * c.maxReadSep; |
| 364 |
else { |
| 365 |
|
| 366 |
uint32_t svvar = std::abs(0.1 * (br[i].pos2 - br[i].pos)); |
| 367 |
if (svt == 4) svvar = std::abs(0.1 * br[i].inslen); |
| 368 |
if (varisize < svvar) varisize = svvar; |
| 369 |
if (varisize > 1000) varisize = 1000; |
| 370 |
} |
| 371 |
for(uint32_t j = i + 1; j<br.size(); ++j) { |
| 372 |
if (br[j].chr == refIdx) { |
| 373 |
if ( (uint32_t) (br[j].pos - br[i].pos) > varisize) break; |
| 374 |
if ((svt == 4) && (std::abs(br[j].inslen - br[i].inslen) > varisize)) continue; |
| 375 |
if ( (uint32_t) std::abs(br[j].pos2 - br[i].pos2) < varisize) { |
| 376 |
|
| 377 |
if (j > lastConnectedNode) lastConnectedNode = j; |
| 378 |
|
| 379 |
|
| 380 |
uint32_t compIndex = 0; |
| 381 |
if (!comp[i]) { |
| 382 |
if (!comp[j]) { |
| 383 |
|
| 384 |
compIndex = ++numComp; |
| 385 |
comp[i] = compIndex; |
| 386 |
comp[j] = compIndex; |
| 387 |
compEdge.insert(std::make_pair(compIndex, TEdgeList())); |
| 388 |
} else { |
| 389 |
compIndex = comp[j]; |
| 390 |
comp[i] = compIndex; |
| 391 |
} |
| 392 |
} else { |
| 393 |
if (!comp[j]) { |
| 394 |
compIndex = comp[i]; |
| 395 |
comp[j] = compIndex; |
| 396 |
} else { |
| 397 |
|
| 398 |
if (comp[j] == comp[i]) { |
| 399 |
compIndex = comp[j]; |
| 400 |
} else { |
| 401 |
|
| 402 |
compIndex = comp[i]; |
| 403 |
uint32_t otherIndex = comp[j]; |
| 404 |
if (otherIndex < compIndex) { |
| 405 |
compIndex = comp[j]; |
| 406 |
otherIndex = comp[i]; |
| 407 |
} |
| 408 |
|
| 409 |
for(uint32_t k = lastConnectedNodeStart; k <= lastConnectedNode; ++k) { |
| 410 |
if (otherIndex == comp[k]) comp[k] = compIndex; |
| 411 |
} |
| 412 |
|
| 413 |
TCompEdgeList::iterator compEdgeIt = compEdge.find(compIndex); |
| 414 |
TCompEdgeList::iterator compEdgeOtherIt = compEdge.find(otherIndex); |
| 415 |
compEdgeIt->second.insert(compEdgeIt->second.end(), compEdgeOtherIt->second.begin(), compEdgeOtherIt->second.end()); |
| 416 |
compEdge.erase(compEdgeOtherIt); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
TCompEdgeList::iterator compEdgeIt = compEdge.find(compIndex); |
| 423 |
if (compEdgeIt->second.size() < c.graphPruning) { |
| 424 |
|
| 425 |
TWeightType weight = std::abs(br[j].pos2 - br[i].pos2) + std::abs(br[j].pos - br[i].pos); |
| 426 |
compEdgeIt->second.push_back(TEdgeRecord(i, j, weight)); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
if (!compEdge.empty()) { |
| 435 |
_searchCliques(c, compEdge, br, sv, svt); |
| 436 |
compEdge.clear(); |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
template<typename TConfig, typename TCompEdgeList, typename TBamRecord, typename TSVs> |
| 443 |
inline void |
| 444 |
_searchCliques(TConfig const& c, TCompEdgeList& compEdge, TBamRecord const& bamRecord, TSVs& svs, int32_t const svt) { |
| 445 |
typedef typename TCompEdgeList::mapped_type TEdgeList; |
| 446 |
|
| 447 |
|
| 448 |
for(typename TCompEdgeList::iterator compIt = compEdge.begin(); compIt != compEdge.end(); ++compIt) { |
| 449 |
|
| 450 |
std::sort(compIt->second.begin(), compIt->second.end()); |
| 451 |
|
| 452 |
|
| 453 |
typename TEdgeList::const_iterator itWEdge = compIt->second.begin(); |
| 454 |
typename TEdgeList::const_iterator itWEdgeEnd = compIt->second.end(); |
| 455 |
typedef std::set<std::size_t> TCliqueMembers; |
| 456 |
|
| 457 |
TCliqueMembers clique; |
| 458 |
TCliqueMembers incompatible; |
| 459 |
int32_t svStart = -1; |
| 460 |
int32_t svEnd = -1; |
| 461 |
int32_t wiggle = 0; |
| 462 |
int32_t clusterRefID=bamRecord[itWEdge->source].tid; |
| 463 |
int32_t clusterMateRefID=bamRecord[itWEdge->source].mtid; |
| 464 |
_initClique(bamRecord[itWEdge->source], svStart, svEnd, wiggle, svt); |
| 465 |
if ((clusterRefID==clusterMateRefID) && (svStart >= svEnd)) continue; |
| 466 |
clique.insert(itWEdge->source); |
| 467 |
|
| 468 |
|
| 469 |
bool cliqueGrow=true; |
| 470 |
while (cliqueGrow) { |
| 471 |
itWEdge = compIt->second.begin(); |
| 472 |
cliqueGrow = false; |
| 473 |
for(;(!cliqueGrow) && (itWEdge != itWEdgeEnd);++itWEdge) { |
| 474 |
std::size_t v; |
| 475 |
if ((clique.find(itWEdge->source) == clique.end()) && (clique.find(itWEdge->target) != clique.end())) v = itWEdge->source; |
| 476 |
else if ((clique.find(itWEdge->source) != clique.end()) && (clique.find(itWEdge->target) == clique.end())) v = itWEdge->target; |
| 477 |
else continue; |
| 478 |
if (incompatible.find(v) != incompatible.end()) continue; |
| 479 |
cliqueGrow = _updateClique(bamRecord[v], svStart, svEnd, wiggle, svt); |
| 480 |
if (cliqueGrow) clique.insert(v); |
| 481 |
else incompatible.insert(v); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
if ((clique.size() >= c.minCliqueSize) && (_svSizeCheck(svStart, svEnd, svt))) { |
| 487 |
StructuralVariantRecord svRec; |
| 488 |
svRec.chr = clusterRefID; |
| 489 |
svRec.chr2 = clusterMateRefID; |
| 490 |
svRec.svStart = (uint32_t) svStart + 1; |
| 491 |
svRec.svEnd = (uint32_t) svEnd + 1; |
| 492 |
svRec.peSupport = clique.size(); |
| 493 |
int32_t ci_wiggle = std::max(abs(wiggle), 50); |
| 494 |
svRec.ciposlow = -ci_wiggle; |
| 495 |
svRec.ciposhigh = ci_wiggle; |
| 496 |
svRec.ciendlow = -ci_wiggle; |
| 497 |
svRec.ciendhigh = ci_wiggle; |
| 498 |
svRec.mapq = 0; |
| 499 |
std::vector<uint8_t> mapQV; |
| 500 |
for(typename TCliqueMembers::const_iterator itC = clique.begin(); itC!=clique.end(); ++itC) { |
| 501 |
mapQV.push_back(bamRecord[*itC].MapQuality); |
| 502 |
svRec.mapq += bamRecord[*itC].MapQuality; |
| 503 |
} |
| 504 |
std::sort(mapQV.begin(), mapQV.end()); |
| 505 |
svRec.peMapQuality = mapQV[mapQV.size()/2]; |
| 506 |
svRec.srSupport=0; |
| 507 |
svRec.srAlignQuality=0; |
| 508 |
svRec.precise=false; |
| 509 |
svRec.svt = svt; |
| 510 |
svRec.insLen = 0; |
| 511 |
svRec.homLen = 0; |
| 512 |
svs.push_back(svRec); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
template<typename TConfig> |
| 520 |
inline void |
| 521 |
cluster(TConfig const& c, std::vector<BamAlignRecord>& bamRecord, std::vector<StructuralVariantRecord>& svs, uint32_t const varisize, int32_t const svt) { |
| 522 |
typedef typename std::vector<BamAlignRecord> TBamRecord; |
| 523 |
|
| 524 |
typedef std::vector<uint32_t> TComponent; |
| 525 |
TComponent comp; |
| 526 |
comp.resize(bamRecord.size(), 0); |
| 527 |
uint32_t numComp = 0; |
| 528 |
|
| 529 |
|
| 530 |
typedef uint32_t TWeightType; |
| 531 |
typedef uint32_t TVertex; |
| 532 |
typedef EdgeRecord<TWeightType, TVertex> TEdgeRecord; |
| 533 |
typedef std::vector<TEdgeRecord> TEdgeList; |
| 534 |
typedef std::map<uint32_t, TEdgeList> TCompEdgeList; |
| 535 |
TCompEdgeList compEdge; |
| 536 |
|
| 537 |
|
| 538 |
std::size_t lastConnectedNode = 0; |
| 539 |
std::size_t lastConnectedNodeStart = 0; |
| 540 |
std::size_t bamItIndex = 0; |
| 541 |
for(TBamRecord::const_iterator bamIt = bamRecord.begin(); bamIt != bamRecord.end(); ++bamIt, ++bamItIndex) { |
| 542 |
|
| 543 |
if (bamItIndex > lastConnectedNode) { |
| 544 |
|
| 545 |
if (!compEdge.empty()) { |
| 546 |
_searchCliques(c, compEdge, bamRecord, svs, svt); |
| 547 |
lastConnectedNodeStart = lastConnectedNode; |
| 548 |
compEdge.clear(); |
| 549 |
} |
| 550 |
} |
| 551 |
int32_t const minCoord = _minCoord(bamIt->pos, bamIt->mpos, svt); |
| 552 |
int32_t const maxCoord = _maxCoord(bamIt->pos, bamIt->mpos, svt); |
| 553 |
TBamRecord::const_iterator bamItNext = bamIt; |
| 554 |
++bamItNext; |
| 555 |
std::size_t bamItIndexNext = bamItIndex + 1; |
| 556 |
for(; ((bamItNext != bamRecord.end()) && ((uint32_t) std::abs(_minCoord(bamItNext->pos, bamItNext->mpos, svt) + bamItNext->alen - minCoord) <= varisize)) ; ++bamItNext, ++bamItIndexNext) { |
| 557 |
|
| 558 |
if (bamIt->mtid != bamItNext->mtid) continue; |
| 559 |
|
| 560 |
|
| 561 |
if (_pairsDisagree(minCoord, maxCoord, bamIt->alen, bamIt->maxNormalISize, _minCoord(bamItNext->pos, bamItNext->mpos, svt), _maxCoord(bamItNext->pos, bamItNext->mpos, svt), bamItNext->alen, bamItNext->maxNormalISize, svt)) continue; |
| 562 |
|
| 563 |
|
| 564 |
if (bamItIndexNext > lastConnectedNode ) lastConnectedNode = bamItIndexNext; |
| 565 |
|
| 566 |
|
| 567 |
uint32_t compIndex = 0; |
| 568 |
if (!comp[bamItIndex]) { |
| 569 |
if (!comp[bamItIndexNext]) { |
| 570 |
|
| 571 |
compIndex = ++numComp; |
| 572 |
comp[bamItIndex] = compIndex; |
| 573 |
comp[bamItIndexNext] = compIndex; |
| 574 |
compEdge.insert(std::make_pair(compIndex, TEdgeList())); |
| 575 |
} else { |
| 576 |
compIndex = comp[bamItIndexNext]; |
| 577 |
comp[bamItIndex] = compIndex; |
| 578 |
} |
| 579 |
} else { |
| 580 |
if (!comp[bamItIndexNext]) { |
| 581 |
compIndex = comp[bamItIndex]; |
| 582 |
comp[bamItIndexNext] = compIndex; |
| 583 |
} else { |
| 584 |
|
| 585 |
if (comp[bamItIndexNext] == comp[bamItIndex]) { |
| 586 |
compIndex = comp[bamItIndexNext]; |
| 587 |
} else { |
| 588 |
|
| 589 |
compIndex = comp[bamItIndex]; |
| 590 |
uint32_t otherIndex = comp[bamItIndexNext]; |
| 591 |
if (otherIndex < compIndex) { |
| 592 |
compIndex = comp[bamItIndexNext]; |
| 593 |
otherIndex = comp[bamItIndex]; |
| 594 |
} |
| 595 |
|
| 596 |
for(std::size_t i = lastConnectedNodeStart; i <= lastConnectedNode; ++i) { |
| 597 |
if (otherIndex == comp[i]) comp[i] = compIndex; |
| 598 |
} |
| 599 |
|
| 600 |
TCompEdgeList::iterator compEdgeIt = compEdge.find(compIndex); |
| 601 |
TCompEdgeList::iterator compEdgeOtherIt = compEdge.find(otherIndex); |
| 602 |
compEdgeIt->second.insert(compEdgeIt->second.end(), compEdgeOtherIt->second.begin(), compEdgeOtherIt->second.end()); |
| 603 |
compEdge.erase(compEdgeOtherIt); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
TCompEdgeList::iterator compEdgeIt = compEdge.find(compIndex); |
| 610 |
if (compEdgeIt->second.size() < c.graphPruning) { |
| 611 |
TWeightType weight = (TWeightType) ( std::log2((double) abs( abs( (_minCoord(bamItNext->pos, bamItNext->mpos, svt) - minCoord) - (_maxCoord(bamItNext->pos, bamItNext->mpos, svt) - maxCoord) ) - abs(bamIt->Median - bamItNext->Median)) + 1) ); |
| 612 |
compEdgeIt->second.push_back(TEdgeRecord(bamItIndex, bamItIndexNext, weight)); |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
if (!compEdge.empty()) { |
| 617 |
_searchCliques(c, compEdge, bamRecord, svs, svt); |
| 618 |
compEdge.clear(); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
#endif |