python - 使用 Python 查找序列比对中间隙 (indel) 的位置和长度

标签 python

我目前正在学习Python。我不想使用 Biopython,或者任何导入的模块,除了正则表达式,这样我就可以理解代码在做什么。

从基因序列比对中,我想找到我的序列中彼此相邻的间隙/插入缺失“-”的起始位置和结束位置、间隙区域的数量,并计算间隙区域。例如:

>Seq1
ATC----GCTGTA--A-----T

我想要一个看起来像这样的输出:

Number of gaps = 3

Index Position of Gap region 1 = 3 to 6
Length of Gap region 1 = 4

Index Position of Gap region 2 = 13 to 14
Length of Gap region 2 = 2

Index Position of Gap region 3 = 16 to 20
Length of Gap region 3 = 5

我试图在更大的序列比对上解决这个问题,但我什至无法远程弄清楚如何做到这一点。

最佳答案

您想要的是使用正则表达式来查找间隙(一个或多个破折号,翻译为“-+”,加号表示一个或多个):

import re

seq = 'ATC----GCTGTA--A-----T'
matches = list(re.finditer('-+', seq))

print 'Number of gaps =', len(matches)
print

for region_number, match in enumerate(matches, 1):
    print 'Index Position of Gap region {} = {} to {}'.format(
            region_number,
            match.start(),
            match.end() - 1)
    print 'Length of Gap region {} = {}'.format(
            region_number,
            match.end() - match.start())
    print

注释

  • matches 是匹配对象的列表
  • 为了获取区域编号,我使用了函数enumerate。您可以查一下它是如何工作的。
  • 匹配对象有很多方法,但我们感兴趣的是返回开始索引的 .start() 和返回结束索引的 .end() 。请注意,这里的结束索引比您想要的多了一个,因此我从中减去了1。

关于python - 使用 Python 查找序列比对中间隙 (indel) 的位置和长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303791/

相关文章:

python mysqldb 无法插入用引号填充的长字符串

python - 将文件包含在 Python 发行版中的 2 种技术 : which is better?

python - 为什么 ruby​​ 无法使用 wget 将文件保存到特殊路径?

python - 如何知道 GRPC 服务器是否可用

python - 在将数据传递给插值例程之前存储数据

Python:索引制表符分隔的文件

python - 我可以在多处理池星图方法中传递队列对象吗

具有服务器端证书问题的Python Paho-mqtt客户端

python - Pandas 合并数据帧并覆盖原始 df 中的数据

python - 持久内存/缓存