| 1 |
#ifndef COVERAGE_H |
| 2 |
#define COVERAGE_H |
| 3 |
|
| 4 |
#include <boost/container/flat_set.hpp> |
| 5 |
#include <boost/dynamic_bitset.hpp> |
| 6 |
#include <boost/iostreams/stream.hpp> |
| 7 |
#include <boost/iostreams/stream_buffer.hpp> |
| 8 |
#include <boost/iostreams/device/file.hpp> |
| 9 |
#include <boost/iostreams/filtering_stream.hpp> |
| 10 |
#include <boost/iostreams/filter/zlib.hpp> |
| 11 |
#include <boost/iostreams/filter/gzip.hpp> |
| 12 |
|
| 13 |
#include <thread> |
| 14 |
#include <mutex> |
| 15 |
#include <future> |
| 16 |
#include <queue> |
| 17 |
#include <functional> |
| 18 |
#include <condition_variable> |
| 19 |
#include <unordered_map> |
| 20 |
#include <unordered_set> |
| 21 |
#include <memory> |
| 22 |
#include <atomic> |
| 23 |
|
| 24 |
#include <htslib/sam.h> |
| 25 |
|
| 26 |
#include "tags.h" |
| 27 |
#include "util.h" |
| 28 |
#include "msa.h" |
| 29 |
#include "split.h" |
| 30 |
|
| 31 |
|
| 32 |
namespace torali { |
| 33 |
|
| 34 |
struct SpanPoint { |
| 35 |
int32_t bppos; |
| 36 |
int32_t svt; |
| 37 |
uint32_t id; |
| 38 |
int32_t chr2; |
| 39 |
int32_t otherBppos; |
| 40 |
|
| 41 |
SpanPoint() : bppos(0), svt(0), id(0), chr2(0), otherBppos(0) {} |
| 42 |
explicit SpanPoint(int32_t const bp) : bppos(bp), svt(0), id(0), chr2(0), otherBppos(0) {} |
| 43 |
SpanPoint(int32_t const bp, int32_t const s, uint32_t const identifier, int32_t const tid, int32_t const obp) : bppos(bp), svt(s), id(identifier), chr2(tid), otherBppos(obp) {} |
| 44 |
|
| 45 |
bool operator<(const SpanPoint& s2) const { |
| 46 |
return (bppos < s2.bppos); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
struct BpRegion { |
| 51 |
int32_t regionStart; |
| 52 |
int32_t regionEnd; |
| 53 |
int32_t bppos; |
| 54 |
int32_t homLeft; |
| 55 |
int32_t homRight; |
| 56 |
int32_t svt; |
| 57 |
uint32_t id; |
| 58 |
uint8_t bpPoint; |
| 59 |
|
| 60 |
BpRegion() : regionStart(0), regionEnd(0), bppos(0), homLeft(0), homRight(0), svt(0), id(0), bpPoint(0) {} |
| 61 |
explicit BpRegion(int32_t bp) : regionStart(0), regionEnd(0), bppos(bp), homLeft(0), homRight(0), svt(0), id(0), bpPoint(0) {} |
| 62 |
BpRegion(int32_t rs, int32_t re, int32_t bpos, int32_t hl, int32_t hr, int32_t s, uint32_t identifier, uint8_t bpp) : regionStart(rs), regionEnd(re), bppos(bpos), homLeft(hl), homRight(hr), svt(s), id(identifier), bpPoint(bpp) {} |
| 63 |
|
| 64 |
bool operator<(const BpRegion& s2) const { |
| 65 |
return (bppos < s2.bppos); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
struct SpanningCount { |
| 70 |
std::vector<uint8_t> ref; |
| 71 |
std::vector<uint8_t> alt; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
struct JunctionCount { |
| 76 |
std::vector<uint8_t> ref; |
| 77 |
std::vector<uint8_t> alt; |
| 78 |
}; |
| 79 |
|
| 80 |
struct AlignJob { |
| 81 |
std::string consProbe; |
| 82 |
std::string refProbe; |
| 83 |
std::string sequence; |
| 84 |
uint32_t fileIndex; |
| 85 |
uint32_t svId; |
| 86 |
uint8_t qual; |
| 87 |
|
| 88 |
AlignJob(std::string const& cons, std::string const& ref, std::string const& seq, uint32_t const f, uint32_t const s, uint8_t const q) : consProbe(cons), refProbe(ref), sequence(seq), fileIndex(f), svId(s), qual(q) {} |
| 89 |
}; |
| 90 |
|
| 91 |
struct AlignResult { |
| 92 |
uint32_t fileIndex; |
| 93 |
uint32_t svId; |
| 94 |
char type; |
| 95 |
uint8_t qual; |
| 96 |
|
| 97 |
AlignResult() : fileIndex(0), svId(0), type('N'), qual(0) {} |
| 98 |
}; |
| 99 |
|
| 100 |
template<typename TConfig> |
| 101 |
inline double |
| 102 |
_editDistanceHW(TConfig const& c, std::string const& query, std::string const& target) { |
| 103 |
double score = 0; |
| 104 |
EdlibAlignResult align = edlibAlign(query.c_str(), query.size(), target.c_str(), target.size(), edlibNewAlignConfig(2 * c.flankQuality * query.size(), EDLIB_MODE_HW, EDLIB_TASK_DISTANCE, NULL, 0)); |
| 105 |
if (align.editDistance != -1) score = ((1.0 - c.flankQuality) * (double) query.size()) / (double) (align.editDistance + 1); |
| 106 |
edlibFreeAlignResult(align); |
| 107 |
return score; |
| 108 |
} |
| 109 |
|
| 110 |
template<typename TPos> |
| 111 |
inline int32_t |
| 112 |
_cutRefStart(TPos const rStart, TPos const rEnd, TPos const offset, unsigned int bpPoint, int32_t const svt) { |
| 113 |
if (_translocation(svt)) { |
| 114 |
uint8_t ct = _getSpanOrientation(svt); |
| 115 |
if (ct == 3) { |
| 116 |
if (!bpPoint) return rEnd - offset; |
| 117 |
else return rStart - offset; |
| 118 |
} else { |
| 119 |
if (bpPoint) return rEnd - offset; |
| 120 |
else return rStart - offset; |
| 121 |
} |
| 122 |
} else { |
| 123 |
if (svt == 3) { |
| 124 |
if (!bpPoint) return rEnd - offset; |
| 125 |
else return rStart - offset; |
| 126 |
} else { |
| 127 |
if (bpPoint) return rEnd - offset; |
| 128 |
else return rStart - offset; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
template<typename TPos> |
| 134 |
inline int32_t |
| 135 |
_cutRefEnd(TPos const rStart, TPos const rEnd, TPos const offset, unsigned int bpPoint, int32_t const svt) { |
| 136 |
if (_translocation(svt)) { |
| 137 |
uint8_t ct = _getSpanOrientation(svt); |
| 138 |
if (ct == 3) { |
| 139 |
if (!bpPoint) return rEnd + offset; |
| 140 |
else return rStart + offset; |
| 141 |
} else { |
| 142 |
if (bpPoint) return rEnd + offset; |
| 143 |
else return rStart + offset; |
| 144 |
} |
| 145 |
} else { |
| 146 |
if (svt == 3) { |
| 147 |
if (!bpPoint) return rEnd + offset; |
| 148 |
else return rStart + offset; |
| 149 |
} else { |
| 150 |
if (bpPoint) return rEnd + offset; |
| 151 |
else return rStart + offset; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
template<typename TConfig, typename TSVs, typename TBreakProbes, typename TGenomicBpRegion> |
| 158 |
inline void |
| 159 |
_generateProbes(TConfig const& c, bam_hdr_t* hdr, TSVs& svs, TBreakProbes& refProbeArr, TBreakProbes& consProbeArr, TGenomicBpRegion& bpRegion, std::vector<bool>& svOnChr) { |
| 160 |
typedef typename TBreakProbes::value_type TProbes; |
| 161 |
|
| 162 |
|
| 163 |
boost::posix_time::ptime noww = boost::posix_time::second_clock::local_time(); |
| 164 |
std::cerr << '[' << boost::posix_time::to_simple_string(noww) << "] " << "Generate REF and ALT probes" << std::endl; |
| 165 |
|
| 166 |
TProbes refProbes(svs.size()); |
| 167 |
faidx_t* fai = fai_load(c.genome.string().c_str()); |
| 168 |
for(int32_t refIndex=0; refIndex < (int32_t) hdr->n_targets; ++refIndex) { |
| 169 |
char* seq = NULL; |
| 170 |
|
| 171 |
|
| 172 |
for(typename TSVs::iterator itSV = svs.begin(); itSV != svs.end(); ++itSV) { |
| 173 |
if ((itSV->chr != refIndex) && (itSV->chr2 != refIndex)) continue; |
| 174 |
svOnChr[refIndex] = true; |
| 175 |
|
| 176 |
|
| 177 |
if (seq == NULL) { |
| 178 |
int32_t seqlen = -1; |
| 179 |
std::string tname(hdr->target_name[refIndex]); |
| 180 |
seq = faidx_fetch_seq(fai, tname.c_str(), 0, hdr->target_len[refIndex], &seqlen); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if (itSV->chr == refIndex) { |
| 185 |
if (itSV->alleles.empty()) itSV->alleles = _addAlleles(boost::to_upper_copy(std::string(seq + itSV->svStart - 1, seq + itSV->svStart)), std::string(hdr->target_name[itSV->chr2]), *itSV, itSV->svt); |
| 186 |
} |
| 187 |
if (!itSV->precise) continue; |
| 188 |
|
| 189 |
|
| 190 |
if ((itSV->chr != itSV->chr2) && (itSV->chr2 == refIndex)) { |
| 191 |
Breakpoint bp(*itSV); |
| 192 |
_initBreakpoint(hdr, bp, (int32_t) itSV->consensus.size(), itSV->svt); |
| 193 |
refProbes[itSV->id] = _getSVRef(c, seq, bp, refIndex, itSV->svt); |
| 194 |
} |
| 195 |
if (itSV->chr == refIndex) { |
| 196 |
Breakpoint bp(*itSV); |
| 197 |
if (_translocation(itSV->svt)) bp.part1 = refProbes[itSV->id]; |
| 198 |
if (itSV->svt ==4) { |
| 199 |
int32_t bufferSpace = std::max((int32_t) ((itSV->consensus.size() - itSV->insLen) / 3), c.minimumFlankSize); |
| 200 |
_initBreakpoint(hdr, bp, bufferSpace, itSV->svt); |
| 201 |
} else _initBreakpoint(hdr, bp, (int32_t) itSV->consensus.size(), itSV->svt); |
| 202 |
std::string svRefStr = _getSVRef(c, seq, bp, refIndex, itSV->svt); |
| 203 |
|
| 204 |
|
| 205 |
typedef boost::multi_array<char, 2> TAlign; |
| 206 |
TAlign align; |
| 207 |
if (!_consRefAlignment(itSV->consensus, svRefStr, align, itSV->svt)) continue; |
| 208 |
|
| 209 |
AlignDescriptor ad; |
| 210 |
if (!_findSplit(c, itSV->consensus, svRefStr, align, ad, itSV->svt)) continue; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
for (unsigned int bpPoint = 0; bpPoint<2; ++bpPoint) { |
| 222 |
int32_t regionChr, regionStart, regionEnd, cutConsStart, cutConsEnd, cutRefStart, cutRefEnd, bppos; |
| 223 |
if (bpPoint) { |
| 224 |
regionChr = itSV->chr2; |
| 225 |
regionStart = std::max(0, itSV->svEnd - c.minimumFlankSize); |
| 226 |
regionEnd = std::min((uint32_t) (itSV->svEnd + c.minimumFlankSize), hdr->target_len[itSV->chr2]); |
| 227 |
cutConsStart = ad.cEnd - ad.homLeft - c.minimumFlankSize; |
| 228 |
cutConsEnd = ad.cEnd + ad.homRight + c.minimumFlankSize; |
| 229 |
cutRefStart = _cutRefStart(ad.rStart, ad.rEnd, ad.homLeft + c.minimumFlankSize, bpPoint, itSV->svt); |
| 230 |
cutRefEnd = _cutRefEnd(ad.rStart, ad.rEnd, ad.homRight + c.minimumFlankSize, bpPoint, itSV->svt); |
| 231 |
bppos = itSV->svEnd; |
| 232 |
} else { |
| 233 |
regionChr = itSV->chr; |
| 234 |
regionStart = std::max(0, itSV->svStart - c.minimumFlankSize); |
| 235 |
regionEnd = std::min((uint32_t) (itSV->svStart + c.minimumFlankSize), hdr->target_len[itSV->chr]); |
| 236 |
cutConsStart = ad.cStart - ad.homLeft - c.minimumFlankSize; |
| 237 |
cutConsEnd = ad.cStart + ad.homRight + c.minimumFlankSize; |
| 238 |
cutRefStart = _cutRefStart(ad.rStart, ad.rEnd, ad.homLeft + c.minimumFlankSize, bpPoint, itSV->svt); |
| 239 |
cutRefEnd = _cutRefEnd(ad.rStart, ad.rEnd, ad.homRight + c.minimumFlankSize, bpPoint, itSV->svt); |
| 240 |
bppos = itSV->svStart; |
| 241 |
} |
| 242 |
consProbeArr[bpPoint][itSV->id] = itSV->consensus.substr(cutConsStart, (cutConsEnd - cutConsStart)); |
| 243 |
refProbeArr[bpPoint][itSV->id] = svRefStr.substr(cutRefStart, (cutRefEnd - cutRefStart)); |
| 244 |
bpRegion[regionChr].push_back(BpRegion(regionStart, regionEnd, bppos, ad.homLeft, ad.homRight, itSV->svt, itSV->id, bpPoint)); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
if (seq != NULL) free(seq); |
| 249 |
} |
| 250 |
|
| 251 |
fai_destroy(fai); |
| 252 |
for(int32_t refIndex=0; refIndex < (int32_t) hdr->n_targets; ++refIndex) { |
| 253 |
|
| 254 |
std::sort(bpRegion[refIndex].begin(), bpRegion[refIndex].end()); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
template<typename TConfig, typename TSampleLibrary, typename TSVs, typename TCoverageCount, typename TCountMap, typename TSpanMap> |
| 259 |
inline void |
| 260 |
annotateCoverage(TConfig& c, TSampleLibrary& sampleLib, TSVs& svs, TCoverageCount& covCount, TCountMap& countMap, TSpanMap& spanMap) |
| 261 |
{ |
| 262 |
typedef typename TSpanMap::value_type::value_type TSpanPair; |
| 263 |
typedef typename TCountMap::value_type::value_type TCountPair; |
| 264 |
uint32_t batchSize = 131072 * c.maxThreads; |
| 265 |
|
| 266 |
|
| 267 |
typedef std::vector<samFile*> TSamFile; |
| 268 |
typedef std::vector<hts_idx_t*> TIndex; |
| 269 |
typedef std::vector<bam_hdr_t*> THeader; |
| 270 |
TSamFile samfile(c.files.size()); |
| 271 |
TIndex idx(c.files.size()); |
| 272 |
THeader hdr(c.files.size()); |
| 273 |
int32_t totalTarget = 0; |
| 274 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 275 |
samfile[file_c] = sam_open(c.files[file_c].string().c_str(), "r"); |
| 276 |
hts_set_fai_filename(samfile[file_c], c.genome.string().c_str()); |
| 277 |
idx[file_c] = sam_index_load(samfile[file_c], c.files[file_c].string().c_str()); |
| 278 |
hdr[file_c] = sam_hdr_read(samfile[file_c]); |
| 279 |
totalTarget += hdr[file_c]->n_targets; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
covCount.resize(c.files.size()); |
| 284 |
countMap.resize(c.files.size()); |
| 285 |
spanMap.resize(c.files.size()); |
| 286 |
for(uint32_t file_c = 0; file_c < c.files.size(); ++file_c) { |
| 287 |
covCount[file_c].resize(svs.size()); |
| 288 |
countMap[file_c].resize(svs.size(), TCountPair()); |
| 289 |
spanMap[file_c].resize(svs.size(), TSpanPair()); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
typedef std::vector<std::string> TProbes; |
| 294 |
typedef std::vector<TProbes> TBreakProbes; |
| 295 |
TBreakProbes refProbeArr(2, TProbes()); |
| 296 |
TBreakProbes consProbeArr(2, TProbes()); |
| 297 |
for(uint32_t k = 0; k < 2; ++k) { |
| 298 |
refProbeArr[k].resize(svs.size()); |
| 299 |
consProbeArr[k].resize(svs.size()); |
| 300 |
} |
| 301 |
typedef std::vector<BpRegion> TBpRegion; |
| 302 |
typedef std::vector<TBpRegion> TGenomicBpRegion; |
| 303 |
TGenomicBpRegion bpRegion(hdr[0]->n_targets, TBpRegion()); |
| 304 |
std::vector<bool> svOnChr(hdr[0]->n_targets, false); |
| 305 |
|
| 306 |
|
| 307 |
_generateProbes(c, hdr[0], svs, refProbeArr, consProbeArr, bpRegion, svOnChr); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
| 318 |
std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "SV annotation" << std::endl; |
| 319 |
|
| 320 |
typedef std::vector<uint32_t> TRefAlignCount; |
| 321 |
typedef std::vector<TRefAlignCount> TFileRefAlignCount; |
| 322 |
TFileRefAlignCount refAlignedReadCount(c.files.size(), TRefAlignCount()); |
| 323 |
TFileRefAlignCount refAlignedSpanCount(c.files.size(), TRefAlignCount()); |
| 324 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 325 |
refAlignedReadCount[file_c].resize(svs.size(), 0); |
| 326 |
refAlignedSpanCount[file_c].resize(svs.size(), 0); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
boost::iostreams::filtering_ostream dumpOut; |
| 331 |
if (c.hasDumpFile) { |
| 332 |
dumpOut.push(boost::iostreams::gzip_compressor()); |
| 333 |
dumpOut.push(boost::iostreams::file_sink(c.dumpfile.string().c_str(), std::ios_base::out | std::ios_base::binary)); |
| 334 |
dumpOut << "#svid\tbam\tqname\tchr\tpos\tmatechr\tmatepos\tmapq\ttype" << std::endl; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
ThreadPool pool(c.maxThreads); |
| 339 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 340 |
|
| 341 |
typedef boost::unordered_map<std::size_t, std::pair<bool, uint8_t>> TClipQual; |
| 342 |
TClipQual cliptra; |
| 343 |
|
| 344 |
|
| 345 |
for(int32_t refIndex=0; refIndex < (int32_t) hdr[file_c]->n_targets; ++refIndex) { |
| 346 |
TClipQual clip; |
| 347 |
|
| 348 |
|
| 349 |
if (!svOnChr[refIndex]) continue; |
| 350 |
|
| 351 |
|
| 352 |
bool nodata = true; |
| 353 |
std::string suffix("cram"); |
| 354 |
std::string str(c.files[file_c].string()); |
| 355 |
if ((str.size() >= suffix.size()) && (str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0)) nodata = false; |
| 356 |
uint64_t mapped = 0; |
| 357 |
uint64_t unmapped = 0; |
| 358 |
hts_idx_get_stat(idx[file_c], refIndex, &mapped, &unmapped); |
| 359 |
if (mapped) nodata = false; |
| 360 |
if (nodata) continue; |
| 361 |
|
| 362 |
|
| 363 |
typedef uint16_t TCount; |
| 364 |
uint32_t maxCoverage = std::numeric_limits<TCount>::max(); |
| 365 |
typedef std::vector<TCount> TCoverage; |
| 366 |
TCoverage covFragment(hdr[file_c]->target_len[refIndex], 0); |
| 367 |
TCoverage covBases(hdr[file_c]->target_len[refIndex], 0); |
| 368 |
|
| 369 |
|
| 370 |
typedef boost::dynamic_bitset<> TBitSet; |
| 371 |
TBitSet bpOccupied(hdr[file_c]->target_len[refIndex]); |
| 372 |
for(uint32_t i = 0; i < bpRegion[refIndex].size(); ++i) { |
| 373 |
for(int32_t k = bpRegion[refIndex][i].regionStart; k < bpRegion[refIndex][i].regionEnd; ++k) { |
| 374 |
bpOccupied[k] = 1; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
typedef std::vector<SpanPoint> TSpanPoint; |
| 380 |
TSpanPoint spanPoint; |
| 381 |
typedef boost::dynamic_bitset<> TBitSet; |
| 382 |
TBitSet spanBp(hdr[file_c]->target_len[refIndex]); |
| 383 |
for(typename TSVs::iterator itSV = svs.begin(); itSV != svs.end(); ++itSV) { |
| 384 |
if (itSV->peSupport == 0) continue; |
| 385 |
if ((itSV->chr == refIndex) && (itSV->svStart < (int32_t) hdr[file_c]->target_len[refIndex])) { |
| 386 |
spanBp[itSV->svStart] = 1; |
| 387 |
spanPoint.push_back(SpanPoint(itSV->svStart, itSV->svt, itSV->id, itSV->chr2, itSV->svEnd)); |
| 388 |
} |
| 389 |
if ((itSV->chr2 == refIndex) && (itSV->svEnd < (int32_t) hdr[file_c]->target_len[refIndex])) { |
| 390 |
spanBp[itSV->svEnd] = 1; |
| 391 |
spanPoint.push_back(SpanPoint(itSV->svEnd, itSV->svt, itSV->id, itSV->chr, itSV->svStart)); |
| 392 |
} |
| 393 |
} |
| 394 |
std::sort(spanPoint.begin(), spanPoint.end()); |
| 395 |
|
| 396 |
|
| 397 |
hts_itr_t* iter = sam_itr_queryi(idx[file_c], refIndex, 0, hdr[file_c]->target_len[refIndex]); |
| 398 |
bam1_t* rec = bam_init1(); |
| 399 |
int32_t lastAlignedPos = 0; |
| 400 |
std::unordered_set<std::size_t> lastAlignedPosReads; |
| 401 |
|
| 402 |
|
| 403 |
std::vector<AlignJob> jobBuf; |
| 404 |
jobBuf.reserve(batchSize); |
| 405 |
auto process_batch = [&](std::vector<AlignJob> &jobs) { |
| 406 |
if (jobs.empty()) return; |
| 407 |
const std::size_t J = jobs.size(); |
| 408 |
std::vector<AlignResult> results(J, AlignResult()); |
| 409 |
std::atomic<std::size_t> next(0); |
| 410 |
std::vector<std::future<void>> futures; |
| 411 |
futures.reserve(c.maxThreads); |
| 412 |
for (unsigned int t = 0; t < c.maxThreads; ++t) { |
| 413 |
futures.push_back(pool.enqueue([&]() { |
| 414 |
for(;;) { |
| 415 |
std::size_t i = next.fetch_add(1, std::memory_order_relaxed); |
| 416 |
if (i >= J) break; |
| 417 |
AlignJob &job = jobs[i]; |
| 418 |
double scoreAlt = _editDistanceHW(c, job.consProbe, job.sequence); |
| 419 |
double scoreRef = _editDistanceHW(c, job.refProbe, job.sequence); |
| 420 |
if ((scoreRef > 0.7) || (scoreAlt > 0.7)) { |
| 421 |
results[i].svId = job.svId; |
| 422 |
results[i].fileIndex = job.fileIndex; |
| 423 |
if (scoreRef > scoreAlt) { |
| 424 |
results[i].type = 'R'; |
| 425 |
results[i].qual = (uint8_t) std::min(255, std::min((int) (scoreRef * 35), (int) job.qual)); |
| 426 |
} else { |
| 427 |
results[i].type = 'A'; |
| 428 |
results[i].qual = (uint8_t) std::min(255, std::min((int) (scoreAlt * 35), (int) job.qual)); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
})); |
| 433 |
} |
| 434 |
for (auto &f : futures) f.get(); |
| 435 |
|
| 436 |
for(std::size_t i = 0; i < J; ++i) { |
| 437 |
AlignResult &ar = results[i]; |
| 438 |
if (ar.type == 'N') continue; |
| 439 |
if ((countMap[ar.fileIndex][ar.svId].ref.size() + countMap[ar.fileIndex][ar.svId].alt.size()) >= c.maxGenoReadCount) continue; |
| 440 |
if (ar.type == 'A') countMap[ar.fileIndex][ar.svId].alt.push_back(ar.qual); |
| 441 |
else if (ar.type == 'R') { |
| 442 |
if (++refAlignedReadCount[ar.fileIndex][ar.svId] % 2) { |
| 443 |
countMap[ar.fileIndex][ar.svId].ref.push_back(ar.qual); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
while (sam_itr_next(samfile[file_c], iter, rec) >= 0) { |
| 450 |
if (rec->core.flag & (BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP | BAM_FSUPPLEMENTARY | BAM_FUNMAP | BAM_FMUNMAP)) continue; |
| 451 |
if (rec->core.qual < c.minGenoQual) continue; |
| 452 |
|
| 453 |
|
| 454 |
{ |
| 455 |
uint32_t rp = 0; |
| 456 |
const uint32_t* cigar = bam_get_cigar(rec); |
| 457 |
for (std::size_t i = 0; i < rec->core.n_cigar; ++i) { |
| 458 |
if (bam_cigar_op(cigar[i]) == BAM_CMATCH) { |
| 459 |
for(std::size_t k = 0; k<bam_cigar_oplen(cigar[i]);++k) { |
| 460 |
if ((rec->core.pos + rp < hdr[file_c]->target_len[refIndex]) && (covBases[rec->core.pos + rp] < maxCoverage - 1)) ++covBases[rec->core.pos + rp]; |
| 461 |
++rp; |
| 462 |
} |
| 463 |
} else if (bam_cigar_op(cigar[i]) == BAM_CDEL) { |
| 464 |
rp += bam_cigar_oplen(cigar[i]); |
| 465 |
} else if (bam_cigar_op(cigar[i]) == BAM_CREF_SKIP) { |
| 466 |
rp += bam_cigar_oplen(cigar[i]); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
bool hasSoftClip = false; |
| 473 |
bool hasClip = false; |
| 474 |
int32_t leadingSC = 0; |
| 475 |
const uint32_t* cigar = bam_get_cigar(rec); |
| 476 |
for (std::size_t i = 0; i < rec->core.n_cigar; ++i) { |
| 477 |
if (bam_cigar_op(cigar[i]) == BAM_CSOFT_CLIP) { |
| 478 |
hasClip = true; |
| 479 |
hasSoftClip = true; |
| 480 |
if (i == 0) leadingSC = bam_cigar_oplen(cigar[i]); |
| 481 |
} else if (bam_cigar_op(cigar[i]) == BAM_CHARD_CLIP) hasClip = true; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
if (rec->core.l_qseq >= (2 * c.minimumFlankSize)) { |
| 486 |
bool bpvalid = false; |
| 487 |
int32_t rbegin = std::max(0, (int32_t) rec->core.pos - leadingSC); |
| 488 |
for(int32_t k = rbegin; ((k < (rec->core.pos + rec->core.l_qseq)) && (k < (int32_t) hdr[file_c]->target_len[refIndex])); ++k) { |
| 489 |
if (bpOccupied[k]) { |
| 490 |
bpvalid = true; |
| 491 |
break; |
| 492 |
} |
| 493 |
} |
| 494 |
if (bpvalid) { |
| 495 |
|
| 496 |
typename TBpRegion::iterator itBp = std::lower_bound(bpRegion[refIndex].begin(), bpRegion[refIndex].end(), BpRegion(rbegin)); |
| 497 |
for(; ((itBp != bpRegion[refIndex].end()) && (rec->core.pos + rec->core.l_qseq >= itBp->bppos)); ++itBp) { |
| 498 |
if ((countMap[file_c][itBp->id].ref.size() + countMap[file_c][itBp->id].alt.size()) >= c.maxGenoReadCount) continue; |
| 499 |
|
| 500 |
if ((hasSoftClip) || ((!hasClip) && (rec->core.pos + c.minimumFlankSize + itBp->homLeft <= itBp->bppos) && (rec->core.pos + rec->core.l_qseq >= itBp->bppos + c.minimumFlankSize + itBp->homRight))) { |
| 501 |
std::string consProbe = consProbeArr[itBp->bpPoint][itBp->id]; |
| 502 |
std::string refProbe = refProbeArr[itBp->bpPoint][itBp->id]; |
| 503 |
|
| 504 |
|
| 505 |
std::string sequence; |
| 506 |
sequence.resize(rec->core.l_qseq); |
| 507 |
const uint8_t* seqptr = bam_get_seq(rec); |
| 508 |
for (int i = 0; i < rec->core.l_qseq; ++i) sequence[i] = "=ACMGRSVTWYHKDBN"[bam_seqi(seqptr, i)]; |
| 509 |
_adjustOrientation(sequence, itBp->bpPoint, itBp->svt); |
| 510 |
|
| 511 |
|
| 512 |
if (c.hasDumpFile) { |
| 513 |
double scoreAlt = _editDistanceHW(c, consProbe, sequence); |
| 514 |
double scoreRef = _editDistanceHW(c, refProbe, sequence); |
| 515 |
if ((scoreRef > 0.7) || (scoreAlt > 0.7)) { |
| 516 |
if (scoreRef > scoreAlt) { |
| 517 |
|
| 518 |
if (++refAlignedReadCount[file_c][itBp->id] % 2) { |
| 519 |
countMap[file_c][itBp->id].ref.push_back((uint8_t) std::min(255, std::min((int) (scoreRef * 35), (int) rec->core.qual))); |
| 520 |
} |
| 521 |
} else { |
| 522 |
countMap[file_c][itBp->id].alt.push_back((uint8_t) std::min(255, std::min((int) (scoreAlt * 35), (int) rec->core.qual))); |
| 523 |
|
| 524 |
std::string svid(_addID(itBp->svt)); |
| 525 |
std::string padNumber = boost::lexical_cast<std::string>(itBp->id); |
| 526 |
padNumber.insert(padNumber.begin(), 8 - padNumber.length(), '0'); |
| 527 |
svid += padNumber; |
| 528 |
dumpOut << svid << "\t" << c.files[file_c].string() << "\t" << bam_get_qname(rec) << "\t" << hdr[file_c]->target_name[rec->core.tid] << "\t" << rec->core.pos << "\t" << hdr[file_c]->target_name[rec->core.mtid] << "\t" << rec->core.mpos << "\t" << (int32_t) rec->core.qual << "\tSR" << std::endl; |
| 529 |
} |
| 530 |
} |
| 531 |
} else { |
| 532 |
|
| 533 |
jobBuf.push_back(AlignJob(consProbe, refProbe, sequence, file_c, itBp->id, rec->core.qual)); |
| 534 |
if (jobBuf.size() >= batchSize) { |
| 535 |
process_batch(jobBuf); |
| 536 |
jobBuf.clear(); |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
if ((!(rec->core.flag & BAM_FPAIRED)) || (!svOnChr[rec->core.mtid])) continue; |
| 546 |
|
| 547 |
|
| 548 |
if (rec->core.pos > lastAlignedPos) { |
| 549 |
lastAlignedPosReads.clear(); |
| 550 |
lastAlignedPos = rec->core.pos; |
| 551 |
} |
| 552 |
|
| 553 |
if (_firstPairObs(rec, lastAlignedPosReads)) { |
| 554 |
|
| 555 |
lastAlignedPosReads.insert(hash_string(bam_get_qname(rec))); |
| 556 |
std::size_t hv = hash_pair(rec); |
| 557 |
if (rec->core.tid == rec->core.mtid) clip[hv] = std::make_pair(hasSoftClip, rec->core.qual); |
| 558 |
else cliptra[hv] = std::make_pair(hasSoftClip, rec->core.qual); |
| 559 |
} else { |
| 560 |
|
| 561 |
std::size_t hv = hash_pair_mate(rec); |
| 562 |
uint8_t pairQuality = 0; |
| 563 |
bool pairClip = false; |
| 564 |
if (rec->core.tid == rec->core.mtid) { |
| 565 |
auto itCM = clip.find(hv); |
| 566 |
if (itCM == clip.end()) continue; |
| 567 |
pairQuality = std::min((uint8_t) itCM->second.second, (uint8_t) rec->core.qual); |
| 568 |
if ((itCM->second.first) || (hasSoftClip)) pairClip = true; |
| 569 |
clip.erase(itCM); |
| 570 |
} else { |
| 571 |
auto itCM = cliptra.find(hv); |
| 572 |
if (itCM == cliptra.end()) continue; |
| 573 |
pairQuality = std::min((uint8_t) itCM->second.second, (uint8_t) rec->core.qual); |
| 574 |
if ((itCM->second.first) || (hasSoftClip)) pairClip = true; |
| 575 |
cliptra.erase(itCM); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
if (pairQuality < c.minGenoQual) continue; |
| 580 |
|
| 581 |
|
| 582 |
if (rec->core.tid == rec->core.mtid) { |
| 583 |
|
| 584 |
int32_t midPoint = rec->core.pos + halfAlignmentLength(rec); |
| 585 |
if ((midPoint < (int32_t) hdr[file_c]->target_len[refIndex]) && (covFragment[midPoint] < maxCoverage - 1)) ++covFragment[midPoint]; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
int32_t outerISize = 0; |
| 590 |
if (rec->core.pos < rec->core.mpos) outerISize = rec->core.mpos + rec->core.l_qseq - rec->core.pos; |
| 591 |
else outerISize = rec->core.pos + rec->core.l_qseq - rec->core.mpos; |
| 592 |
|
| 593 |
|
| 594 |
if (sampleLib[file_c].median == 0) continue; |
| 595 |
|
| 596 |
|
| 597 |
if ((!pairClip) && (getSVType(rec) == 2) && (outerISize >= sampleLib[file_c].minNormalISize) && (outerISize <= sampleLib[file_c].maxNormalISize) && (rec->core.tid==rec->core.mtid)) { |
| 598 |
|
| 599 |
int32_t spanlen = 0.8 * outerISize; |
| 600 |
int32_t pbegin = std::min((int32_t) rec->core.pos, (int32_t) rec->core.mpos); |
| 601 |
int32_t st = pbegin + (outerISize - spanlen) / 2; |
| 602 |
bool spanvalid = false; |
| 603 |
for(int32_t i = st; ((i < (st + spanlen)) && (i < (int32_t) hdr[file_c]->target_len[refIndex])); ++i) { |
| 604 |
if (spanBp[i]) { |
| 605 |
spanvalid = true; |
| 606 |
break; |
| 607 |
} |
| 608 |
} |
| 609 |
if (spanvalid) { |
| 610 |
|
| 611 |
typename TSpanPoint::iterator itSpan = std::lower_bound(spanPoint.begin(), spanPoint.end(), SpanPoint(st)); |
| 612 |
for(; ((itSpan != spanPoint.end()) && (st + spanlen >= itSpan->bppos)); ++itSpan) { |
| 613 |
|
| 614 |
if (++refAlignedSpanCount[file_c][itSpan->id] % 2) { |
| 615 |
spanMap[file_c][itSpan->id].ref.push_back(pairQuality); |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
if ((getSVType(rec) != 2) || (outerISize < sampleLib[file_c].minNormalISize) || (outerISize > sampleLib[file_c].maxNormalISize) || (rec->core.tid!=rec->core.mtid)) { |
| 623 |
|
| 624 |
int32_t svt = _isizeMappingPos(rec, sampleLib[file_c].maxISizeCutoff); |
| 625 |
if (svt == -1) continue; |
| 626 |
|
| 627 |
|
| 628 |
bool spanvalid = false; |
| 629 |
int32_t pbegin = rec->core.pos; |
| 630 |
int32_t pend = std::min((int32_t) rec->core.pos + sampleLib[file_c].maxNormalISize, (int32_t) hdr[file_c]->target_len[refIndex]); |
| 631 |
if (rec->core.flag & BAM_FREVERSE) { |
| 632 |
pbegin = std::max(0, (int32_t) rec->core.pos + rec->core.l_qseq - sampleLib[file_c].maxNormalISize); |
| 633 |
pend = std::min((int32_t) rec->core.pos + rec->core.l_qseq, (int32_t) hdr[file_c]->target_len[refIndex]); |
| 634 |
} |
| 635 |
for(int32_t i = pbegin; i < pend; ++i) { |
| 636 |
if (spanBp[i]) { |
| 637 |
spanvalid = true; |
| 638 |
break; |
| 639 |
} |
| 640 |
} |
| 641 |
if (spanvalid) { |
| 642 |
|
| 643 |
typename TSpanPoint::iterator itSpan = std::lower_bound(spanPoint.begin(), spanPoint.end(), SpanPoint(pbegin)); |
| 644 |
for(; ((itSpan != spanPoint.end()) && (pend >= itSpan->bppos)); ++itSpan) { |
| 645 |
if (svt == itSpan->svt) { |
| 646 |
|
| 647 |
if (rec->core.mtid == itSpan->chr2) { |
| 648 |
if (std::abs((int32_t) rec->core.mpos - itSpan->otherBppos) < sampleLib[file_c].maxNormalISize) { |
| 649 |
if (c.hasDumpFile) { |
| 650 |
std::string svid(_addID(itSpan->svt)); |
| 651 |
std::string padNumber = boost::lexical_cast<std::string>(itSpan->id); |
| 652 |
padNumber.insert(padNumber.begin(), 8 - padNumber.length(), '0'); |
| 653 |
svid += padNumber; |
| 654 |
dumpOut << svid << "\t" << c.files[file_c].string() << "\t" << bam_get_qname(rec) << "\t" << hdr[file_c]->target_name[rec->core.tid] << "\t" << rec->core.pos << "\t" << hdr[file_c]->target_name[rec->core.mtid] << "\t" << rec->core.mpos << "\t" << (int32_t) rec->core.qual << "\tPE" << std::endl; |
| 655 |
} |
| 656 |
spanMap[file_c][itSpan->id].alt.push_back(pairQuality); |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
if (!jobBuf.empty()) { |
| 667 |
process_batch(jobBuf); |
| 668 |
jobBuf.clear(); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
bam_destroy1(rec); |
| 673 |
hts_itr_destroy(iter); |
| 674 |
clip.clear(); |
| 675 |
|
| 676 |
|
| 677 |
for(uint32_t i = 0; i < svs.size(); ++i) { |
| 678 |
if (svs[i].chr == refIndex) { |
| 679 |
|
| 680 |
bool smallSV = false; |
| 681 |
int32_t halfSize = (svs[i].svEnd - svs[i].svStart)/2; |
| 682 |
if ((_translocation(svs[i].svt)) || (svs[i].svt == 4)) { |
| 683 |
halfSize = 500; |
| 684 |
smallSV = true; |
| 685 |
} else { |
| 686 |
if ((svs[i].svEnd - svs[i].svStart) <= c.indelsize) smallSV = true; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
int32_t lstart = std::max(svs[i].svStart - halfSize, 0); |
| 691 |
int32_t lend = svs[i].svStart; |
| 692 |
int32_t covbase = 0; |
| 693 |
for(uint32_t k = lstart; ((k < (uint32_t) lend) && (k < hdr[0]->target_len[refIndex])); ++k) { |
| 694 |
if (smallSV) covbase += covBases[k]; |
| 695 |
else covbase += covFragment[k]; |
| 696 |
} |
| 697 |
covCount[file_c][svs[i].id].leftRC = covbase; |
| 698 |
|
| 699 |
|
| 700 |
covbase = 0; |
| 701 |
int32_t mstart = svs[i].svStart; |
| 702 |
int32_t mend = svs[i].svEnd; |
| 703 |
if ((_translocation(svs[i].svt)) || (svs[i].svt == 4)) { |
| 704 |
mstart = std::max(svs[i].svStart - halfSize, 0); |
| 705 |
mend = std::min(svs[i].svStart + halfSize, (int32_t) hdr[0]->target_len[refIndex]); |
| 706 |
} |
| 707 |
for(uint32_t k = mstart; ((k < (uint32_t) mend) && (k < hdr[0]->target_len[refIndex])); ++k) { |
| 708 |
if (smallSV) covbase += covBases[k]; |
| 709 |
else covbase += covFragment[k]; |
| 710 |
} |
| 711 |
covCount[file_c][svs[i].id].rc = covbase; |
| 712 |
|
| 713 |
|
| 714 |
covbase = 0; |
| 715 |
int32_t rstart = svs[i].svEnd; |
| 716 |
int32_t rend = std::min(svs[i].svEnd + halfSize, (int32_t) hdr[0]->target_len[refIndex]); |
| 717 |
if ((_translocation(svs[i].svt)) || (svs[i].svt == 4)) { |
| 718 |
rstart = svs[i].svStart; |
| 719 |
rend = std::min(svs[i].svStart + halfSize, (int32_t) hdr[0]->target_len[refIndex]); |
| 720 |
} |
| 721 |
for(uint32_t k = rstart; ((k < (uint32_t) rend) && (k < hdr[0]->target_len[refIndex])); ++k) { |
| 722 |
if (smallSV) covbase += covBases[k]; |
| 723 |
else covbase += covFragment[k]; |
| 724 |
} |
| 725 |
covCount[file_c][svs[i].id].rightRC = covbase; |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) { |
| 732 |
bam_hdr_destroy(hdr[file_c]); |
| 733 |
hts_idx_destroy(idx[file_c]); |
| 734 |
sam_close(samfile[file_c]); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
} |
| 739 |
|
| 740 |
#endif |