本文整理汇总了Python中pysam.Samfile.count方法的典型用法代码示例。如果您正苦于以下问题:Python Samfile.count方法的具体用法?Python Samfile.count怎么用?Python Samfile.count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysam.Samfile
的用法示例。
在下文中一共展示了Samfile.count方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_bam_differential
# 需要导入模块: from pysam import Samfile [as 别名]
# 或者: from pysam.Samfile import count [as 别名]
def parse_bam_differential(afn, bfn, regs, step):
"""(internal) Parses bam file in absolute mode. Proceeds by counting reads mapping
onto a segment (chr, start, end). No normalization is done at this step.
"""
abam = Samfile(str(afn), "rb")
bbam = Samfile(str(bfn), "rb")
acount = []
bcount = []
oldchr = "chr1"
for reg in regs:
chr, start, end = reg[:3]
if chr != oldchr:
log("files: %s - %s : %s counted" % (afn, bfn, oldchr))
oldchr = chr
# this could be improved
for s in xrange(start, end, step):
e = s + step
an = abam.count(chr, s, e)
bn = bbam.count(chr, s, e)
acount.append(an)
bcount.append(bn)
acount.append(-1)
bcount.append(-1)
log("files: %s - %s : %s counted (finished)" % (afn, bfn, oldchr))
return acount, bcount
示例2: parse_bam_absolute
# 需要导入模块: from pysam import Samfile [as 别名]
# 或者: from pysam.Samfile import count [as 别名]
def parse_bam_absolute(fn, regs):
"""(internal) Parses bam file in absolute mode. Proceeds by counting reads mapping
onto a segment (chr, start, end) and normalizes the count by the segment's length.
"""
bam = Samfile(str(fn), "rb")
count = []
for reg in regs:
chr, start, end = reg[:3]
n = bam.count(chr, start, end)
count.append(float(n) / (end - start))
return count
示例3: ace
# 需要导入模块: from pysam import Samfile [as 别名]
# 或者: from pysam.Samfile import count [as 别名]
def ace(args):
"""
%prog ace bamfile fastafile
convert bam format to ace format. This often allows the remapping to be
assessed as a denovo assembly format. bam file needs to be indexed. also
creates a .mates file to be used in amos/bambus, and .astat file to mark
whether the contig is unique or repetitive based on A-statistics in Celera
assembler.
"""
p = OptionParser(ace.__doc__)
p.add_option("--splitdir", dest="splitdir", default="outRoot",
help="split the ace per contig to dir [default: %default]")
p.add_option("--unpaired", dest="unpaired", default=False,
help="remove read pairs on the same contig [default: %default]")
p.add_option("--minreadno", dest="minreadno", default=3, type="int",
help="minimum read numbers per contig [default: %default]")
p.add_option("--minctgsize", dest="minctgsize", default=100, type="int",
help="minimum contig size per contig [default: %default]")
p.add_option("--astat", default=False, action="store_true",
help="create .astat to list repetitiveness [default: %default]")
p.add_option("--readids", default=False, action="store_true",
help="create file of mapped and unmapped ids [default: %default]")
from pysam import Samfile
opts, args = p.parse_args(args)
if len(args) != 2:
sys.exit(not p.print_help())
bamfile, fastafile = args
astat = opts.astat
readids = opts.readids
f = Fasta(fastafile)
prefix = bamfile.split(".")[0]
acefile = prefix + ".ace"
readsfile = prefix + ".reads"
astatfile = prefix + ".astat"
logging.debug("Load {0}".format(bamfile))
s = Samfile(bamfile, "rb")
ncontigs = s.nreferences
genomesize = sum(x for a, x in f.itersizes())
logging.debug("Total {0} contigs with size {1} base".format(ncontigs,
genomesize))
qual = "20" # default qual
totalreads = sum(s.count(x) for x in s.references)
logging.debug("Total {0} reads mapped".format(totalreads))
fw = open(acefile, "w")
if astat:
astatfw = open(astatfile, "w")
if readids:
readsfw = open(readsfile, "w")
print >> fw, "AS {0} {1}".format(ncontigs, totalreads)
print >> fw
for i, contig in enumerate(s.references):
cseq = f[contig]
nbases = len(cseq)
mapped_reads = [x for x in s.fetch(contig) if not x.is_unmapped]
nreads = len(mapped_reads)
nsegments = 0
print >> fw, "CO {0} {1} {2} {3} U".format(contig, nbases, nreads,
nsegments)
print >> fw, fill(str(cseq.seq))
print >> fw
if astat:
astat = Astat(nbases, nreads, genomesize, totalreads)
print >> astatfw, "{0}\t{1:.1f}".format(contig, astat)
text = fill([qual] * nbases, delimiter=" ", width=30)
print >> fw, "BQ\n{0}".format(text)
print >> fw
rnames = []
for a in mapped_reads:
readname = a.qname
rname = readname
if readids:
print >> readsfw, readname
rnames.append(rname)
strand = "C" if a.is_reverse else "U"
paddedstart = a.pos + 1 # 0-based to 1-based
af = "AF {0} {1} {2}".format(rname, strand, paddedstart)
print >> fw, af
print >> fw
for a, rname in zip(mapped_reads, rnames):
#.........这里部分代码省略.........