如何使用 biopython 查询 fasta 文件中的区域?

How to query a region in a fasta file using biopython?

我有一个包含一些参考基因组的 fasta 文件。 给定染色体、起始索引和结束索引,我想将参考核苷酸作为字符串获取。

我正在寻找一个在代码中看起来像这样的函数:

from Bio import SeqIO
p = '/path/to/refernce.fa'
seqs = SeqIO.parse(p.open(), 'fasta')
string = seqs.query(id='chr7', start=10042, end=10252)

字符串应该是这样的:'GGCTACGAACT...'

我所发现的只是如何遍历序列,以及如何从 NCBI 中提取数据,这不是我要找的。 在 biopython 中执行此操作的正确方法是什么?

A​​FAIK,biopython does not currently have this functionality. For random lookups using an index (please see samtools faidx), you'll probably want either pysam or pyfaidx. Here's an example using the pysam.FastaFile class 允许您快速 'fetch' 区域中的序列:

import pysam
ref = pysam.FastaFile('/path/to/reference.fa')
seq = ref.fetch('chr7', 10042, 10252)
print(seq)

或使用pyfaidx和'get_seq'方法:

from pyfaidx import Fasta
ref = Fasta('/path/to/reference.fa')
seq = ref.get_seq('chr7', 10042, 10252)
print(seq)