| 1 |
#ifndef CNV_H |
| 2 |
#define CNV_H |
| 3 |
|
| 4 |
#include <boost/filesystem.hpp> |
| 5 |
#include <boost/multi_array.hpp> |
| 6 |
#include <boost/date_time/posix_time/posix_time.hpp> |
| 7 |
#include <boost/unordered_map.hpp> |
| 8 |
#include <boost/algorithm/string.hpp> |
| 9 |
#include <boost/tokenizer.hpp> |
| 10 |
#include <boost/math/distributions/chi_squared.hpp> |
| 11 |
#include <boost/math/distributions/hypergeometric.hpp> |
| 12 |
#include <boost/iostreams/filtering_streambuf.hpp> |
| 13 |
#include <boost/iostreams/filtering_stream.hpp> |
| 14 |
#include <boost/iostreams/copy.hpp> |
| 15 |
#include <boost/iostreams/filter/gzip.hpp> |
| 16 |
#include <boost/iostreams/device/file.hpp> |
| 17 |
#include <boost/iostreams/filter/zlib.hpp> |
| 18 |
#include <boost/accumulators/accumulators.hpp> |
| 19 |
#include <boost/accumulators/statistics.hpp> |
| 20 |
|
| 21 |
#include <htslib/faidx.h> |
| 22 |
#include <htslib/vcf.h> |
| 23 |
#include <htslib/sam.h> |
| 24 |
#include "util.h" |
| 25 |
|
| 26 |
|
| 27 |
namespace torali |
| 28 |
{ |
| 29 |
|
| 30 |
struct SVBreakpoint { |
| 31 |
int32_t pos; |
| 32 |
int32_t cilow; |
| 33 |
int32_t cihigh; |
| 34 |
int32_t qual; |
| 35 |
|
| 36 |
explicit SVBreakpoint(int32_t const p) : pos(p), cilow(0), cihigh(0), qual(0) {} |
| 37 |
SVBreakpoint(int32_t const p, int32_t const cil, int32_t const cih, int32_t q) : pos(p), cilow(cil), cihigh(cih), qual(q) {} |
| 38 |
|
| 39 |
bool operator<(const SVBreakpoint& sv2) const { |
| 40 |
return ((pos<sv2.pos) || ((pos==sv2.pos) && (qual<sv2.qual))); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
struct BpCNV { |
| 46 |
int32_t start; |
| 47 |
int32_t end; |
| 48 |
double zscore; |
| 49 |
|
| 50 |
BpCNV(int32_t const s, int32_t const e, double const z) : start(s), end(e), zscore(z) {} |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
template<typename TConfig> |
| 55 |
inline void |
| 56 |
mergeCNVs(TConfig const& c, std::vector<CNV>& chrcnv, std::vector<CNV>& cnvs) { |
| 57 |
|
| 58 |
bool merged = true; |
| 59 |
std::vector<CNV> newcnv; |
| 60 |
while(merged) { |
| 61 |
int32_t k = -1; |
| 62 |
for(int32_t i = 0; i < (int32_t) chrcnv.size(); ++i) { |
| 63 |
if (i <= k) continue; |
| 64 |
k = i; |
| 65 |
for(int32_t j = i + 1; j < (int32_t) chrcnv.size(); ++j) { |
| 66 |
bool allValid = true; |
| 67 |
for(int32_t pre = i; pre < j; ++pre) { |
| 68 |
double diff = std::abs(chrcnv[pre].cn - chrcnv[j].cn); |
| 69 |
if (diff >= c.cn_offset) { |
| 70 |
allValid = false; |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
if (allValid) k = j; |
| 75 |
else break; |
| 76 |
} |
| 77 |
if (k > i) { |
| 78 |
|
| 79 |
double cn = (chrcnv[i].cn + chrcnv[k].cn) / 2.0; |
| 80 |
double mp = (chrcnv[i].mappable + chrcnv[k].mappable) / 2.0; |
| 81 |
newcnv.push_back(CNV(chrcnv[i].chr, chrcnv[i].start, chrcnv[k].end, chrcnv[i].ciposlow, chrcnv[i].ciposhigh, chrcnv[k].ciendlow, chrcnv[k].ciendhigh, cn, mp)); |
| 82 |
} else { |
| 83 |
newcnv.push_back(chrcnv[i]); |
| 84 |
} |
| 85 |
} |
| 86 |
if (newcnv.size() == chrcnv.size()) merged = false; |
| 87 |
else { |
| 88 |
chrcnv = newcnv; |
| 89 |
newcnv.clear(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
for(uint32_t i = 0; i < chrcnv.size(); ++i) { |
| 95 |
cnvs.push_back(chrcnv[i]); |
| 96 |
|
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
template<typename TConfig, typename TGcBias, typename TCoverage, typename TGenomicBreakpoints> |
| 102 |
inline void |
| 103 |
breakpointRefinement(TConfig const& c, std::pair<uint32_t, uint32_t> const& gcbound, std::vector<uint16_t> const& gcContent, std::vector<uint16_t> const& uniqContent, TGcBias const& gcbias, TCoverage const& cov, bam_hdr_t const* hdr, int32_t const refIndex, TGenomicBreakpoints const& svbp, std::vector<CNV>& cnvs) { |
| 104 |
typedef typename TGenomicBreakpoints::value_type TSVs; |
| 105 |
|
| 106 |
|
| 107 |
for(uint32_t n = 1; n < cnvs.size(); ++n) { |
| 108 |
if ((cnvs[n-1].chr != refIndex) || (cnvs[n].chr != refIndex)) continue; |
| 109 |
double precovsum = 0; |
| 110 |
double preexpcov = 0; |
| 111 |
double succovsum = 0; |
| 112 |
double sucexpcov = 0; |
| 113 |
int32_t pos = cnvs[n-1].start; |
| 114 |
while((pos < cnvs[n].end) && (pos < (int32_t) hdr->target_len[refIndex])) { |
| 115 |
if ((gcContent[pos] > gcbound.first) && (gcContent[pos] < gcbound.second) && (uniqContent[pos] >= c.fragmentUnique * c.meanisize)) { |
| 116 |
if (pos < cnvs[n-1].end) { |
| 117 |
precovsum += cov[pos]; |
| 118 |
preexpcov += gcbias[gcContent[pos]].coverage; |
| 119 |
} else { |
| 120 |
succovsum += cov[pos]; |
| 121 |
sucexpcov += gcbias[gcContent[pos]].coverage; |
| 122 |
} |
| 123 |
} |
| 124 |
++pos; |
| 125 |
} |
| 126 |
double precndiff = std::abs((c.ploidy * precovsum / preexpcov) - (c.ploidy * succovsum / sucexpcov)); |
| 127 |
|
| 128 |
|
| 129 |
typename TSVs::const_iterator itbest = svbp[refIndex].end(); |
| 130 |
int32_t searchStart = std::max(0, std::min(cnvs[n-1].ciendlow, cnvs[n-1].end - 1000)); |
| 131 |
int32_t searchEnd = std::max(cnvs[n].ciposhigh, cnvs[n].start + 1000); |
| 132 |
int32_t midpoint = (int32_t) ((cnvs[n-1].start + cnvs[n-1].end) / 2); |
| 133 |
if (searchStart < midpoint) searchStart = midpoint; |
| 134 |
midpoint = (int32_t) ((cnvs[n].start + cnvs[n].end) / 2); |
| 135 |
if (searchEnd > midpoint) searchEnd = midpoint; |
| 136 |
|
| 137 |
auto itsv = std::lower_bound(svbp[refIndex].begin(), svbp[refIndex].end(), SVBreakpoint(searchStart)); |
| 138 |
for(; itsv != svbp[refIndex].end(); ++itsv) { |
| 139 |
if (itsv->pos > searchEnd) break; |
| 140 |
if ((itbest == svbp[refIndex].end()) || (itsv->qual > itbest->qual)) itbest = itsv; |
| 141 |
} |
| 142 |
if ((itbest != svbp[refIndex].end()) && (itbest->qual >= 50)) { |
| 143 |
|
| 144 |
precovsum = 0; |
| 145 |
preexpcov = 0; |
| 146 |
succovsum = 0; |
| 147 |
sucexpcov = 0; |
| 148 |
pos = cnvs[n-1].start; |
| 149 |
while((pos < cnvs[n].end) && (pos < (int32_t) hdr->target_len[refIndex])) { |
| 150 |
if ((gcContent[pos] > gcbound.first) && (gcContent[pos] < gcbound.second) && (uniqContent[pos] >= c.fragmentUnique * c.meanisize)) { |
| 151 |
if (pos < itbest->pos) { |
| 152 |
precovsum += cov[pos]; |
| 153 |
preexpcov += gcbias[gcContent[pos]].coverage; |
| 154 |
} else { |
| 155 |
succovsum += cov[pos]; |
| 156 |
sucexpcov += gcbias[gcContent[pos]].coverage; |
| 157 |
} |
| 158 |
} |
| 159 |
++pos; |
| 160 |
} |
| 161 |
double postcndiff = std::abs((c.ploidy * precovsum / preexpcov) - (c.ploidy * succovsum / sucexpcov)); |
| 162 |
|
| 163 |
if ((precndiff < postcndiff + c.cn_offset) && (std::abs(cnvs[n].start - itbest->pos) < 50000)) { |
| 164 |
|
| 165 |
cnvs[n-1].end = itbest->pos; |
| 166 |
cnvs[n].start = itbest->pos; |
| 167 |
cnvs[n-1].ciendlow = itbest->pos + itbest->cilow; |
| 168 |
cnvs[n-1].ciendhigh = itbest->pos + itbest->cihigh; |
| 169 |
cnvs[n].ciposlow = itbest->pos + itbest->cilow; |
| 170 |
cnvs[n].ciposhigh = itbest->pos + itbest->cihigh; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
template<typename TConfig, typename TGcBias, typename TCoverage> |
| 178 |
inline void |
| 179 |
genotypeCNVs(TConfig const& c, std::pair<uint32_t, uint32_t> const& gcbound, std::vector<uint16_t> const& gcContent, std::vector<uint16_t> const& uniqContent, TGcBias const& gcbias, TCoverage const& cov, bam_hdr_t const* hdr, int32_t const refIndex, std::vector<CNV>& cnvs) { |
| 180 |
for(uint32_t n = 0; n < cnvs.size(); ++n) { |
| 181 |
if (cnvs[n].chr != refIndex) continue; |
| 182 |
double covsum = 0; |
| 183 |
double expcov = 0; |
| 184 |
int32_t winlen = 0; |
| 185 |
int32_t pos = cnvs[n].start; |
| 186 |
while((pos < cnvs[n].end) && (pos < (int32_t) hdr->target_len[refIndex])) { |
| 187 |
if ((gcContent[pos] > gcbound.first) && (gcContent[pos] < gcbound.second) && (uniqContent[pos] >= c.fragmentUnique * c.meanisize)) { |
| 188 |
covsum += cov[pos]; |
| 189 |
expcov += gcbias[gcContent[pos]].coverage; |
| 190 |
++winlen; |
| 191 |
} |
| 192 |
++pos; |
| 193 |
} |
| 194 |
double cn = c.ploidy; |
| 195 |
if (expcov > 0) cn = c.ploidy * covsum / expcov; |
| 196 |
double mp = (double) winlen / (double) (cnvs[n].end - cnvs[n].start); |
| 197 |
cnvs[n].cn = cn; |
| 198 |
cnvs[n].mappable = mp; |
| 199 |
|
| 200 |
|
| 201 |
boost::accumulators::accumulator_set<double, boost::accumulators::features<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > acc; |
| 202 |
uint32_t wsz = winlen / 10; |
| 203 |
if (wsz > 1) { |
| 204 |
covsum = 0; |
| 205 |
expcov = 0; |
| 206 |
winlen = 0; |
| 207 |
pos = cnvs[n].start; |
| 208 |
while((pos < cnvs[n].end) && (pos < (int32_t) hdr->target_len[refIndex])) { |
| 209 |
if ((gcContent[pos] > gcbound.first) && (gcContent[pos] < gcbound.second) && (uniqContent[pos] >= c.fragmentUnique * c.meanisize)) { |
| 210 |
covsum += cov[pos]; |
| 211 |
expcov += gcbias[gcContent[pos]].coverage; |
| 212 |
++winlen; |
| 213 |
if (winlen % wsz == 0) { |
| 214 |
double cn = c.ploidy; |
| 215 |
if (expcov > 0) cn = c.ploidy * covsum / expcov; |
| 216 |
acc(cn); |
| 217 |
covsum = 0; |
| 218 |
expcov = 0; |
| 219 |
} |
| 220 |
} |
| 221 |
++pos; |
| 222 |
} |
| 223 |
cnvs[n].sd = sqrt(boost::accumulators::variance(acc)); |
| 224 |
if (cnvs[n].sd < 0.025) cnvs[n].sd = 0.025; |
| 225 |
} else { |
| 226 |
|
| 227 |
cnvs[n].cn = -1; |
| 228 |
cnvs[n].sd = 0.025; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
template<typename TConfig, typename TGcBias, typename TCoverage> |
| 234 |
inline void |
| 235 |
callCNVs(TConfig const& c, std::pair<uint32_t, uint32_t> const& gcbound, std::vector<uint16_t> const& gcContent, std::vector<uint16_t> const& uniqContent, TGcBias const& gcbias, TCoverage const& cov, bam_hdr_t const* hdr, int32_t const refIndex, std::vector<CNV>& cnvs) { |
| 236 |
|
| 237 |
|
| 238 |
int32_t smallestWin = c.minCnvSize / 10; |
| 239 |
int32_t biggestWin = smallestWin * 200; |
| 240 |
uint32_t chain = 10; |
| 241 |
|
| 242 |
|
| 243 |
std::vector<BpCNV> bpmax; |
| 244 |
if (bpmax.empty()) { |
| 245 |
|
| 246 |
std::vector<int32_t> winsize; |
| 247 |
int32_t wsize = smallestWin; |
| 248 |
while (wsize < biggestWin) { |
| 249 |
winsize.push_back(wsize); |
| 250 |
wsize *= 2; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
typedef int32_t TCnVal; |
| 255 |
typedef std::vector<TCnVal> TCN; |
| 256 |
typedef std::vector<int32_t> TChrPos; |
| 257 |
std::vector<BpCNV> bpvec; |
| 258 |
for(uint32_t idx = 0; idx < winsize.size(); ++idx) { |
| 259 |
uint32_t idxOffset = winsize[idx] / winsize[0]; |
| 260 |
|
| 261 |
TCN cnvec; |
| 262 |
TChrPos wpos; |
| 263 |
uint32_t wstart = 0; |
| 264 |
while(wstart < hdr->target_len[refIndex]) { |
| 265 |
double covsum = 0; |
| 266 |
double expcov = 0; |
| 267 |
int32_t winlen = 0; |
| 268 |
uint32_t pos = wstart; |
| 269 |
while ((winlen < winsize[idx]) && (pos < hdr->target_len[refIndex])) { |
| 270 |
if ((gcContent[pos] > gcbound.first) && (gcContent[pos] < gcbound.second) && (uniqContent[pos] >= c.fragmentUnique * c.meanisize)) { |
| 271 |
covsum += cov[pos]; |
| 272 |
expcov += gcbias[gcContent[pos]].coverage; |
| 273 |
++winlen; |
| 274 |
} |
| 275 |
++pos; |
| 276 |
} |
| 277 |
if (winlen == winsize[idx]) { |
| 278 |
|
| 279 |
if (expcov > 0) cnvec.push_back((int32_t) boost::math::round(c.ploidy * covsum / expcov * 100.0)); |
| 280 |
else cnvec.push_back((int32_t) boost::math::round(c.ploidy * 100.0)); |
| 281 |
wpos.push_back(wstart); |
| 282 |
} |
| 283 |
wstart = pos; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
TCN pre(chain, -1); |
| 288 |
TCN suc(chain, -1); |
| 289 |
TChrPos prep(chain, 0); |
| 290 |
TChrPos sucp(chain, 0); |
| 291 |
uint32_t idxbp = 0; |
| 292 |
for(uint32_t k = 0; k < cnvec.size(); ++k) { |
| 293 |
if (k < chain) { |
| 294 |
pre[k % chain] = cnvec[k]; |
| 295 |
prep[k % chain] = wpos[k]; |
| 296 |
if (k + 1 < cnvec.size()) { |
| 297 |
if (idx == 0 ) bpvec.push_back(BpCNV(wpos[k], wpos[k+1], 0)); |
| 298 |
else idxbp += idxOffset; |
| 299 |
} |
| 300 |
} else if (k < 2 * chain) { |
| 301 |
suc[k % chain] = cnvec[k]; |
| 302 |
sucp[k % chain] = wpos[k]; |
| 303 |
} else { |
| 304 |
|
| 305 |
TCnVal val = suc[k%chain]; |
| 306 |
int32_t pos = sucp[k%chain]; |
| 307 |
int32_t posNext = sucp[(k+1)%chain]; |
| 308 |
suc[k%chain] = cnvec[k]; |
| 309 |
sucp[k%chain] = wpos[k]; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
boost::accumulators::accumulator_set<TCnVal, boost::accumulators::features<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > accpre; |
| 318 |
boost::accumulators::accumulator_set<TCnVal, boost::accumulators::features<boost::accumulators::tag::mean, boost::accumulators::tag::variance> > accsuc; |
| 319 |
for(uint32_t m = 0; m < pre.size(); ++m) accpre(pre[m]); |
| 320 |
for(uint32_t m = 0; m < suc.size(); ++m) accsuc(suc[m]); |
| 321 |
double diff = std::abs(boost::accumulators::mean(accsuc) - boost::accumulators::mean(accpre)); |
| 322 |
|
| 323 |
double zscore = 0; |
| 324 |
if ((diff > c.stringency * sqrt(boost::accumulators::variance(accpre))) && (diff > c.stringency * sqrt(boost::accumulators::variance(accsuc)))) { |
| 325 |
zscore = diff / std::max(sqrt(boost::accumulators::variance(accpre)), sqrt(boost::accumulators::variance(accsuc))); |
| 326 |
} |
| 327 |
if (idx == 0) bpvec.push_back(BpCNV(pos, posNext, zscore)); |
| 328 |
else { |
| 329 |
for(uint32_t sub = idxbp; sub < idxbp + idxOffset; ++sub) bpvec[sub].zscore += zscore; |
| 330 |
idxbp += idxOffset; |
| 331 |
} |
| 332 |
pre[k%chain] = val; |
| 333 |
prep[k%chain] = pos; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if (bpvec.size()) { |
| 340 |
int32_t pos = bpvec[0].start; |
| 341 |
int32_t posNext = bpvec[0].end; |
| 342 |
double bestDiff = bpvec[0].zscore; |
| 343 |
for(uint32_t n = 1; n < bpvec.size(); ++n) { |
| 344 |
|
| 345 |
if (bpvec[n].zscore == 0) { |
| 346 |
if (bestDiff != 0) { |
| 347 |
|
| 348 |
bpmax.push_back(BpCNV(pos, posNext, bestDiff)); |
| 349 |
pos = bpvec[n].start; |
| 350 |
posNext = bpvec[n].end; |
| 351 |
bestDiff = bpvec[n].zscore; |
| 352 |
} |
| 353 |
} else { |
| 354 |
if (bpvec[n].zscore > bestDiff) { |
| 355 |
|
| 356 |
pos = bpvec[n].start; |
| 357 |
posNext = bpvec[n].end; |
| 358 |
bestDiff = bpvec[n].zscore; |
| 359 |
} else if (bpvec[n].zscore == bestDiff) { |
| 360 |
|
| 361 |
posNext = bpvec[n].end; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
for(uint32_t n = 0; n <= bpmax.size(); ++n) { |
| 370 |
int32_t cil = 0; |
| 371 |
int32_t cih = 0; |
| 372 |
if (n > 0) { |
| 373 |
cil = bpmax[n-1].start; |
| 374 |
cih = bpmax[n-1].end; |
| 375 |
} |
| 376 |
int32_t cel = hdr->target_len[refIndex] - 1; |
| 377 |
int32_t ceh = hdr->target_len[refIndex] - 1; |
| 378 |
if (n < bpmax.size()) { |
| 379 |
cel = bpmax[n].start; |
| 380 |
ceh = bpmax[n].end; |
| 381 |
} |
| 382 |
|
| 383 |
int32_t cnvstart = (int32_t) ((cil + cih)/2); |
| 384 |
int32_t cnvend = (int32_t) ((cel + ceh)/2); |
| 385 |
int32_t estcnvstart = -1; |
| 386 |
int32_t estcnvend = -1; |
| 387 |
double covsum = 0; |
| 388 |
double expcov = 0; |
| 389 |
int32_t winlen = 0; |
| 390 |
int32_t pos = cnvstart; |
| 391 |
while((pos < cnvend) && (pos < (int32_t) hdr->target_len[refIndex])) { |
| 392 |
if ((gcContent[pos] > gcbound.first) && (gcContent[pos] < gcbound.second) && (uniqContent[pos] >= c.fragmentUnique * c.meanisize)) { |
| 393 |
if (estcnvstart == -1) estcnvstart = pos; |
| 394 |
estcnvend = pos; |
| 395 |
covsum += cov[pos]; |
| 396 |
expcov += gcbias[gcContent[pos]].coverage; |
| 397 |
++winlen; |
| 398 |
} |
| 399 |
++pos; |
| 400 |
} |
| 401 |
if ((estcnvstart != -1) && (estcnvend != -1) && (estcnvend - estcnvstart > 0)) { |
| 402 |
double cn = c.ploidy; |
| 403 |
if (expcov > 0) cn = c.ploidy * covsum / expcov; |
| 404 |
double mp = (double) winlen / (double) (estcnvend - estcnvstart); |
| 405 |
cnvs.push_back(CNV(refIndex, estcnvstart, estcnvend, cil, cih, cel, ceh, cn, mp)); |
| 406 |
|
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
template<typename TConfig> |
| 414 |
inline void |
| 415 |
parseVcfCNV(TConfig const& c, bam_hdr_t* hd, std::vector<CNV>& cnvs) { |
| 416 |
|
| 417 |
htsFile* ifile = bcf_open(c.genofile.string().c_str(), "r"); |
| 418 |
bcf_hdr_t* hdr = bcf_hdr_read(ifile); |
| 419 |
bcf1_t* rec = bcf_init(); |
| 420 |
|
| 421 |
|
| 422 |
int32_t nsvend = 0; |
| 423 |
int32_t* svend = NULL; |
| 424 |
int32_t ncipos = 0; |
| 425 |
int32_t* cipos = NULL; |
| 426 |
int32_t nmp = 0; |
| 427 |
float* mp = NULL; |
| 428 |
int32_t nsvt = 0; |
| 429 |
char* svt = NULL; |
| 430 |
int32_t nmethod = 0; |
| 431 |
char* method = NULL; |
| 432 |
uint16_t wimethod = 0; |
| 433 |
while (bcf_read(ifile, hdr, rec) == 0) { |
| 434 |
bcf_unpack(rec, BCF_UN_INFO); |
| 435 |
|
| 436 |
|
| 437 |
if (!wimethod) { |
| 438 |
wimethod = 2; |
| 439 |
if (bcf_get_info_string(hdr, rec, "SVMETHOD", &method, &nmethod) > 0) { |
| 440 |
std::string mstr = std::string(method); |
| 441 |
if ((mstr.size() >= 10) && (mstr.substr(0, 10) == "EMBL.DELLY")) wimethod = 1; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
if (wimethod == 1) { |
| 447 |
|
| 448 |
CNV cnv; |
| 449 |
std::string chrName = bcf_hdr_id2name(hdr, rec->rid); |
| 450 |
int32_t tid = bam_name2id(hd, chrName.c_str()); |
| 451 |
cnv.chr = tid; |
| 452 |
cnv.start = rec->pos - 1; |
| 453 |
cnv.qval = rec->qual; |
| 454 |
|
| 455 |
|
| 456 |
if (bcf_get_info_string(hdr, rec, "SVTYPE", &svt, &nsvt) > 0) { |
| 457 |
if (std::string(svt) != "CNV") continue; |
| 458 |
} else continue; |
| 459 |
|
| 460 |
|
| 461 |
if (bcf_get_info_int32(hdr, rec, "END", &svend, &nsvend) > 0) cnv.end = *svend; |
| 462 |
else continue; |
| 463 |
if (bcf_get_info_int32(hdr, rec, "CIPOS", &cipos, &ncipos) > 0) { |
| 464 |
cnv.ciposlow = cnv.start + cipos[0]; |
| 465 |
cnv.ciposhigh = cnv.start + cipos[1]; |
| 466 |
} else { |
| 467 |
cnv.ciposlow = cnv.start - 50; |
| 468 |
cnv.ciposhigh = cnv.start + 50; |
| 469 |
} |
| 470 |
if (bcf_get_info_int32(hdr, rec, "CIEND", &cipos, &ncipos) > 0) { |
| 471 |
cnv.ciendlow = cnv.end + cipos[0]; |
| 472 |
cnv.ciendhigh = cnv.end + cipos[1]; |
| 473 |
} else { |
| 474 |
cnv.ciendlow = cnv.end - 50; |
| 475 |
cnv.ciendhigh = cnv.end + 50; |
| 476 |
} |
| 477 |
if (bcf_get_info_float(hdr, rec, "MP", &mp, &nmp) > 0) cnv.mappable = (double) *mp; |
| 478 |
else cnv.mappable = 0; |
| 479 |
|
| 480 |
cnvs.push_back(cnv); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
free(svend); |
| 485 |
free(svt); |
| 486 |
free(method); |
| 487 |
free(cipos); |
| 488 |
free(mp); |
| 489 |
|
| 490 |
|
| 491 |
bcf_hdr_destroy(hdr); |
| 492 |
bcf_close(ifile); |
| 493 |
bcf_destroy(rec); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
template<typename TConfig> |
| 498 |
inline void |
| 499 |
cnvVCF(TConfig const& c, std::vector<CNV> const& cnvs) { |
| 500 |
|
| 501 |
samFile* samfile = sam_open(c.bamFile.string().c_str(), "r"); |
| 502 |
hts_set_fai_filename(samfile, c.genome.string().c_str()); |
| 503 |
bam_hdr_t* bamhd = sam_hdr_read(samfile); |
| 504 |
|
| 505 |
|
| 506 |
std::string fmtout = "wb"; |
| 507 |
if (c.outfile.string() == "-") fmtout = "w"; |
| 508 |
htsFile *fp = hts_open(c.outfile.string().c_str(), fmtout.c_str()); |
| 509 |
bcf_hdr_t *hdr = bcf_hdr_init("w"); |
| 510 |
|
| 511 |
|
| 512 |
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
| 513 |
boost::gregorian::date today = now.date(); |
| 514 |
std::string datestr("##fileDate="); |
| 515 |
datestr += boost::gregorian::to_iso_string(today); |
| 516 |
bcf_hdr_append(hdr, datestr.c_str()); |
| 517 |
bcf_hdr_append(hdr, "##ALT=<ID=CNV,Description=\"copy-number variants\">"); |
| 518 |
bcf_hdr_append(hdr, "##FILTER=<ID=LowQual,Description=\"Poor quality copy-number variant\">"); |
| 519 |
bcf_hdr_append(hdr, "##INFO=<ID=CIEND,Number=2,Type=Integer,Description=\"Confidence interval around END\">"); |
| 520 |
bcf_hdr_append(hdr, "##INFO=<ID=CIPOS,Number=2,Type=Integer,Description=\"Confidence interval around POS\">"); |
| 521 |
bcf_hdr_append(hdr, "##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the copy-number variant\">"); |
| 522 |
bcf_hdr_append(hdr, "##INFO=<ID=MP,Number=1,Type=Float,Description=\"Mappable fraction of CNV\">"); |
| 523 |
bcf_hdr_append(hdr, "##INFO=<ID=IMPRECISE,Number=0,Type=Flag,Description=\"Imprecise copy-number variant\">"); |
| 524 |
bcf_hdr_append(hdr, "##INFO=<ID=SVTYPE,Number=1,Type=String,Description=\"Type of structural variant\">"); |
| 525 |
bcf_hdr_append(hdr, "##INFO=<ID=SVMETHOD,Number=1,Type=String,Description=\"Type of approach used to detect CNV\">"); |
| 526 |
bcf_hdr_append(hdr, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">"); |
| 527 |
bcf_hdr_append(hdr, "##FORMAT=<ID=CN,Number=1,Type=Integer,Description=\"Integer copy-number\">"); |
| 528 |
bcf_hdr_append(hdr, "##FORMAT=<ID=CNL,Number=.,Type=Float,Description=\"Log10-scaled copy-number likelihoods\">"); |
| 529 |
bcf_hdr_append(hdr, "##FORMAT=<ID=GQ,Number=1,Type=Integer,Description=\"Genotype Quality\">"); |
| 530 |
bcf_hdr_append(hdr, "##FORMAT=<ID=FT,Number=1,Type=String,Description=\"Per-sample genotype filter\">"); |
| 531 |
|
| 532 |
bcf_hdr_append(hdr, "##FORMAT=<ID=RDCN,Number=1,Type=Float,Description=\"Read-depth based copy-number estimate\">"); |
| 533 |
bcf_hdr_append(hdr, "##FORMAT=<ID=RDSD,Number=1,Type=Float,Description=\"Read-depth standard deviation\">"); |
| 534 |
|
| 535 |
|
| 536 |
std::string refloc("##reference="); |
| 537 |
refloc += c.genome.string(); |
| 538 |
bcf_hdr_append(hdr, refloc.c_str()); |
| 539 |
for (int i = 0; i<bamhd->n_targets; ++i) { |
| 540 |
std::string refname("##contig=<ID="); |
| 541 |
refname += std::string(bamhd->target_name[i]) + ",length=" + boost::lexical_cast<std::string>(bamhd->target_len[i]) + ">"; |
| 542 |
bcf_hdr_append(hdr, refname.c_str()); |
| 543 |
} |
| 544 |
|
| 545 |
bcf_hdr_add_sample(hdr, c.sampleName.c_str()); |
| 546 |
bcf_hdr_add_sample(hdr, NULL); |
| 547 |
if (bcf_hdr_write(fp, hdr) != 0) std::cerr << "Error: Failed to write BCF header!" << std::endl; |
| 548 |
|
| 549 |
uint32_t cnvid = 0; |
| 550 |
if (!cnvs.empty()) { |
| 551 |
|
| 552 |
int32_t *gts = (int*) malloc(bcf_hdr_nsamples(hdr) * 2 * sizeof(int)); |
| 553 |
int32_t *gqval = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int)); |
| 554 |
int32_t *cnval = (int*) malloc(bcf_hdr_nsamples(hdr) * sizeof(int)); |
| 555 |
float *cnrdval = (float*) malloc(bcf_hdr_nsamples(hdr) * sizeof(float)); |
| 556 |
float *cnsdval = (float*) malloc(bcf_hdr_nsamples(hdr) * sizeof(float)); |
| 557 |
float *cnl = (float*) malloc(bcf_hdr_nsamples(hdr) * MAX_CN * sizeof(float)); |
| 558 |
|
| 559 |
std::vector<std::string> ftarr; |
| 560 |
ftarr.resize(bcf_hdr_nsamples(hdr)); |
| 561 |
|
| 562 |
|
| 563 |
now = boost::posix_time::second_clock::local_time(); |
| 564 |
std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "Genotyping" << std::endl; |
| 565 |
bcf1_t *rec = bcf_init(); |
| 566 |
for(uint32_t i = 0; i < cnvs.size(); ++i) { |
| 567 |
|
| 568 |
if ((!c.hasGenoFile) && (cnvs[i].cn == -1)) continue; |
| 569 |
|
| 570 |
|
| 571 |
int32_t absCN = (int32_t) boost::math::round(cnvs[i].cn); |
| 572 |
if ((!c.segmentation) && (absCN == c.ploidy)) continue; |
| 573 |
|
| 574 |
|
| 575 |
rec->rid = bcf_hdr_name2id(hdr, bamhd->target_name[cnvs[i].chr]); |
| 576 |
int32_t svStartPos = cnvs[i].start + 1; |
| 577 |
int32_t svEndPos = cnvs[i].end; |
| 578 |
if (svEndPos >= (int32_t) bamhd->target_len[cnvs[i].chr]) svEndPos = bamhd->target_len[cnvs[i].chr] - 1; |
| 579 |
rec->pos = svStartPos; |
| 580 |
std::string id("CNV"); |
| 581 |
std::string padNumber = boost::lexical_cast<std::string>(++cnvid); |
| 582 |
padNumber.insert(padNumber.begin(), 8 - padNumber.length(), '0'); |
| 583 |
id += padNumber; |
| 584 |
bcf_update_id(hdr, rec, id.c_str()); |
| 585 |
std::string svtype = "CNV"; |
| 586 |
std::string alleles = "N,<" + svtype + ">"; |
| 587 |
bcf_update_alleles_str(hdr, rec, alleles.c_str()); |
| 588 |
|
| 589 |
|
| 590 |
bcf_update_info_flag(hdr, rec, "IMPRECISE", NULL, 1); |
| 591 |
|
| 592 |
bcf_update_info_string(hdr, rec, "SVTYPE", svtype.c_str()); |
| 593 |
std::string dellyVersion("EMBL.DELLYv"); |
| 594 |
dellyVersion += dellyVersionNumber; |
| 595 |
bcf_update_info_string(hdr,rec, "SVMETHOD", dellyVersion.c_str()); |
| 596 |
int32_t tmpi = svEndPos; |
| 597 |
bcf_update_info_int32(hdr, rec, "END", &tmpi, 1); |
| 598 |
int32_t ciend[2]; |
| 599 |
ciend[0] = cnvs[i].ciendlow - cnvs[i].end; |
| 600 |
ciend[1] = cnvs[i].ciendhigh - cnvs[i].end; |
| 601 |
int32_t cipos[2]; |
| 602 |
cipos[0] = cnvs[i].ciposlow - cnvs[i].start; |
| 603 |
cipos[1] = cnvs[i].ciposhigh - cnvs[i].start; |
| 604 |
bcf_update_info_int32(hdr, rec, "CIPOS", cipos, 2); |
| 605 |
bcf_update_info_int32(hdr, rec, "CIEND", ciend, 2); |
| 606 |
float tmpf = cnvs[i].mappable; |
| 607 |
bcf_update_info_float(hdr, rec, "MP", &tmpf, 1); |
| 608 |
|
| 609 |
|
| 610 |
cnval[0] = absCN; |
| 611 |
cnrdval[0] = cnvs[i].cn; |
| 612 |
cnsdval[0] = cnvs[i].sd; |
| 613 |
gts[0] = bcf_gt_missing; |
| 614 |
gts[1] = bcf_gt_missing; |
| 615 |
int32_t qval = _computeCNLs(c, cnvs[i].cn, cnvs[i].sd, cnl, gqval); |
| 616 |
if (c.hasGenoFile) rec->qual = cnvs[i].qval; |
| 617 |
else rec->qual = qval; |
| 618 |
tmpi = bcf_hdr_id2int(hdr, BCF_DT_ID, "PASS"); |
| 619 |
if (rec->qual < 15) tmpi = bcf_hdr_id2int(hdr, BCF_DT_ID, "LowQual"); |
| 620 |
bcf_update_filter(hdr, rec, &tmpi, 1); |
| 621 |
|
| 622 |
if (gqval[0] < 15) ftarr[0] = "LowQual"; |
| 623 |
else ftarr[0] = "PASS"; |
| 624 |
std::vector<const char*> strp(bcf_hdr_nsamples(hdr)); |
| 625 |
std::transform(ftarr.begin(), ftarr.end(), strp.begin(), cstyle_str()); |
| 626 |
bcf_update_genotypes(hdr, rec, gts, bcf_hdr_nsamples(hdr) * 2); |
| 627 |
bcf_update_format_int32(hdr, rec, "CN", cnval, bcf_hdr_nsamples(hdr)); |
| 628 |
bcf_update_format_float(hdr, rec, "CNL", cnl, bcf_hdr_nsamples(hdr) * MAX_CN); |
| 629 |
bcf_update_format_int32(hdr, rec, "GQ", gqval, bcf_hdr_nsamples(hdr)); |
| 630 |
bcf_update_format_string(hdr, rec, "FT", &strp[0], bcf_hdr_nsamples(hdr)); |
| 631 |
bcf_update_format_float(hdr, rec, "RDCN", cnrdval, bcf_hdr_nsamples(hdr)); |
| 632 |
bcf_update_format_float(hdr, rec, "RDSD", cnsdval, bcf_hdr_nsamples(hdr)); |
| 633 |
bcf_write1(fp, hdr, rec); |
| 634 |
bcf_clear1(rec); |
| 635 |
} |
| 636 |
bcf_destroy1(rec); |
| 637 |
|
| 638 |
|
| 639 |
free(gts); |
| 640 |
free(gqval); |
| 641 |
free(cnval); |
| 642 |
free(cnrdval); |
| 643 |
free(cnsdval); |
| 644 |
free(cnl); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
bam_hdr_destroy(bamhd); |
| 649 |
sam_close(samfile); |
| 650 |
|
| 651 |
|
| 652 |
bcf_hdr_destroy(hdr); |
| 653 |
hts_close(fp); |
| 654 |
|
| 655 |
|
| 656 |
if (c.outfile.string() != "-") bcf_index_build(c.outfile.string().c_str(), 14); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
} |
| 661 |
|
| 662 |
#endif |