##读取bed文件
>>> import pybedtools
>>> # use a BED file that ships with pybedtools...
>>> a = pybedtools.example_bedtool('a.bed')
>>> # ...or use your own by passing a filename
>>> a = pybedtools.BedTool('peaks.bed')
>>> a = pybedtools.example_bedtool('a.bed')
>>> b = pybedtools.example_bedtool('b.bed')
>>> a_and_b = a.intersect(b)
##使用loj参数
a_b=a.intersect(b,loj=True)
## 遍历结果文件
a_b.head()
3.保存结果文件saveas
a_b.saveas('intersection-of-a-and-b.bed')
4.过滤初始的输入bed文件
$\color{red}{ filter生成的迭代器只能被使用一次}$
You’ll need to be careful when using BedTool objects as generators, since any operation that reads all the features of a BedTool will consume the iterable.
#过滤起始和终止位点相差超过100的;
>>> a = pybedtools.example_bedtool('a.bed')
>>> b = a.filter(lambda x: len(x) > 100)
>>> print(b)
#chr1 150 500 feature3 0 -
##过滤指定染色体编号的行
b = a.filter(lambda x: x[0]=="chr1")
5.根据bed文件提取序列
>>> a = pybedtools.BedTool("""
... chr1 1 10
... chr1 50 55""", from_string=True)
>>> fasta = pybedtools.example_filename('test.fa')
>>> a = a.sequence(fi=fasta)
##保存seq信息
a.save_seqs("提取的序列信息")
##通过open打开存有序列信息的文件
>>> print(open(a.seqfn).read())