本文整理汇总了Python中pybedtools.BedTool.groupby方法的典型用法代码示例。如果您正苦于以下问题:Python BedTool.groupby方法的具体用法?Python BedTool.groupby怎么用?Python BedTool.groupby使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybedtools.BedTool
的用法示例。
在下文中一共展示了BedTool.groupby方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gene_regions
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def gene_regions(vf, af):
v = BedTool(vf)
feats = BedTool(af)
# first establish all the columns in the annotation file
cols = set(f[4] for f in feats)
results = {}
intersection = v.intersect(feats, wb=True)
if len(intersection) > 0:
#sort_cmd1 = 'awk -F \'\t\' \'{print $1"\t"$2"\t"$3"\t"$4"\t"$9"\t"$5"_"$6"_"$7"_"$8"_"$9}\' %s 1<>%s' % (intersection.fn, intersection.fn)
#call(sort_cmd1, shell=True)
tempfile1 = tempfile.mktemp()
sort_cmd2 = 'awk -F \'\t\' \'{print $1"\t"$2"\t"$3"\t"$4"\t"$9"\t"$5"_"$6"_"$7"_"$8"_"$9}\' %s > %s' % (intersection.fn, tempfile1)
call(sort_cmd2, shell=True)
intersection = BedTool(tempfile1)
annots = intersection.groupby(g=[1,2,3,4,5], c=6, ops='collapse')
for entry in annots:
regions = {}
regions[entry[4]] = entry[5]
results[entry.name] = Series(regions)
df = DataFrame(results, index = cols)
return df.T.fillna(0)
示例2: repeats
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def repeats(vf, af):
v = BedTool(vf)
feats = BedTool(af)
intersection = v.intersect(feats, wb=True)
results = {}
if len(intersection) > 0:
tempfile1 = tempfile.mktemp()
sort_cmd2 = 'awk -F \'\t\' \'{print $1"\t"$2"\t"$3"\t"$4"\t"$5"_"$6"_"$7"_"$8"_"$9"_"$10}\' %s > %s' % (intersection.fn, tempfile1)
call(sort_cmd2, shell=True)
intersection = BedTool(tempfile1)
annots = intersection.groupby(g=[1,2,3,4], c=5, ops='collapse')
for entry in annots:
results[entry.name] = entry[4]
return Series(results, name='repeat')
示例3: motifs
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def motifs(vf, af):
v = BedTool(vf)
cpg = BedTool(af)
overlap = v.intersect(cpg, wb=True)
sort_cmd1 = 'sort -k1,1 -k2,2n -k3,3n -k4,4 %s -o %s' % (overlap.fn, overlap.fn)
tempfile1 = tempfile.mktemp()
call(sort_cmd1, shell=True)
sort_cmd2 = 'awk -F \'\t\' \'{print $1"\t"$2"\t"$3"\t"$4"\t"$5"__"$6"__"$7"__"$8"__"$9"__"$10"__"$11"__"$12"__"$13}\' %s > %s' % (overlap.fn, tempfile1)
call(sort_cmd2, shell=True)
intersection = BedTool(tempfile1)
annots = intersection.groupby(g=[1,2,3,4], c=5, ops='collapse')
results = {}
for entry in annots:
results[entry.name] = entry[4]
return Series(results, name="pwm")
示例4: encode_feats
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def encode_feats(vf, af):
results = {}
cols = open(af+'.cols', 'r').readline().strip().split(',')
#intersection = vs.intersect(feats, wb=True)#TRUE
tempfile1 = tempfile.mktemp()
sort_cmd1 = 'bedtools intersect -wb -a %s -b %s > %s' % (vf, af, tempfile1)
call(sort_cmd1, shell=True)
intersection = BedTool(tempfile1)
annots = intersection.groupby(g=[1,2,3,4], c=10, ops='freqdesc')
for entry in annots:
fs = entry[4].strip(',').split(',')
results[entry.name] = Series({e[0]: int(e[1]) for e in [f.split(':') for f in fs]})
df = DataFrame(results, index = cols)
# transpose to turn feature types into columns, and turn all the NAs in to 0s
return df.T.fillna(0)
示例5: juncs
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def juncs(args):
"""
%prog junctions junctions1.bed [junctions2.bed ...]
Given a TopHat junctions.bed file, trim the read overhang to get intron span
If more than one junction bed file is provided, uniq the junctions and
calculate cumulative (sum) junction support
"""
from tempfile import mkstemp
from pybedtools import BedTool
p = OptionParser(juncs.__doc__)
p.set_outfile()
opts, args = p.parse_args(args)
if len(args) < 1:
sys.exit(not p.print_help())
fh, trimbed = mkstemp(suffix = ".bed")
fw = must_open(trimbed, "w")
for i, juncbed in enumerate(args):
bed = Bed(juncbed, juncs=True)
for b in bed:
ovh = [int(x) for x in b.extra[-2].split(",")]
b.start += ovh[0]
b.end -= ovh[1]
b.accn = "{0}-{1}".format(b.accn, i)
b.extra = None
print >> fw, b
fw.close()
if len(args) > 1:
sh("sort -k1,1 -k2,2n {0} -o {0}".format(trimbed))
tbed = BedTool(trimbed)
grouptbed = tbed.groupby(g=[1,2,3,6], c=5, ops=['sum'])
cmd = """awk -F $'\t' 'BEGIN { OFS = FS } { ID = sprintf("mJUNC%07d", NR); print $1,$2,$3,ID,$5,$4; }'"""
infile = grouptbed.fn
sh(cmd, infile=infile, outfile=opts.outfile)
else:
sort([trimbed, "-o", opts.outfile])
os.unlink(trimbed)
示例6: bound_motifs
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def bound_motifs(vf, af):
v = BedTool(vf)
feats = BedTool(af)
#intersection = feats.intersect(v, wb=True,wa=True)
intersection = v.intersect(feats, wb=True)
results = {}
if len(intersection) > 0:
sort_cmd1 = 'sort -k1,1 -k2,2n -k3,3n %s -o %s' % (intersection.fn, intersection.fn)
call(sort_cmd1, shell=True)
tempfile1 = tempfile.mktemp()
sort_cmd2 = 'awk -F \'\t\' \'{print $1"\t"$2"\t"$3"\t"$4"\t"$5"__"$6"__"$7"__"$8"__"$9}\' %s > %s' % (intersection.fn, tempfile1)
call(sort_cmd2, shell=True)
intersection = BedTool(tempfile1)
annots = intersection.groupby(g=[1,2,3,4], c=5, ops='collapse')
for entry in annots:
results[entry.name] = entry[4]
return Series(results, name='bound_motifs')
示例7: encode_feats
# 需要导入模块: from pybedtools import BedTool [as 别名]
# 或者: from pybedtools.BedTool import groupby [as 别名]
def encode_feats(vf, af):
results = {}
cols = open(af+'.cols', 'r').readline().strip().split(',')
#intersection = vs.intersect(feats, wb=True)#TRUE
tempfile1 = tempfile.mktemp()
sort_cmd1 = 'bedtools intersect -wb -a %s -b %s > %s' % (vf, af, tempfile1)
call(sort_cmd1, shell=True)
tempfile2 = tempfile.mktemp()
sort_cmd2 = 'awk -F \'\t\' \'{print $1"\t"$2"\t"$3"\t"$4"\t"$10"\t"$5"_"$6"_"$7"_"$8"_"$9"_"$10"_"$11"_"$12}\' %s > %s' % (tempfile1, tempfile2)
call(sort_cmd2, shell=True)
intersection = BedTool(tempfile2)
annots = intersection.groupby(g=[1,2,3,4,5], c=6, ops='collapse')
for entry in annots:
#fs = entry[5].strip(',').split(',')
#results[entry.name] = Series({e[0]: int(e[1]) for e in [f.split(':') for f in fs]})
results[entry.name] = Series({entry[4]: entry[5]})
df = DataFrame(results, index = cols)
# transpose to turn feature types into columns, and turn all the NAs in to 0s
return df.T.fillna(0)