我目前正在学习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/