def downsample_bam(in_name, out_name, downsample_rate, restrict_chrom=None):
""" Downsamples a bam. Optionally also restricts the output to a single chromosome.
"""
f = create_bam_infile(in_name)
g, tids = create_bam_outfile(out_name, None, None, template=f)
if restrict_chrom is None:
bam_iter = f
else:
bam_iter = f.fetch(restrict_chrom)
should_write = {}
for r in bam_iter:
if should_write.has_key(r.qname):
if should_write[r.qname]:
g.write(r)
else:
if random.random() < downsample_rate:
should_write[r.qname] = True
g.write(r)
else:
should_write[r.qname] = False
g.close()
评论列表
文章目录