我想知道在python中使用pairwise2匹配字符串的残差索引。
例如,我有两个字符串
A:' EEEEE HHH HHH EEEEE'和
B: 'EEE EEEE HHH'使用以下代码:
from Bio import pairwise2
from Bio.pairwise2 import format_alignment
alignment = pairwise2.align.localdc(A,B, matrix,gap_function_1,gap_function_2)我得到的对齐之一是:
EEE-------EE--- HHH HHH EEEEE
||| || |||||||||
EEE EEEE HHH--------------------------
Score=29.6我想要获取匹配的索引,即序列A中与序列B匹配的所有Es、Hs和' '的原始位置。
我该怎么做?
发布于 2018-06-07 17:06:56
我假设A中的第一个空格是一个打字错误?否则,对齐方式看起来会不一样。
因此,假设:
A = 'EEEEE HHH HHH EEEEE'
B = 'EEE EEEE HHH'
alignment = """EEE-------EE--- HHH HHH EEEEE
||| || |||||||||
EEE EEEE HHH--------------------------
Score=29.6"""我们可以编写一个函数compare()
def compare(align, matches, original):
result = []
index = -1
for char, match in zip(align, matches):
if char == '-':
index += 0
else:
index += 1
if match == '|':
assert original[index] == char
result.append(index)
return result然后
align_A, matches, align_B, score = alignment.splitlines()
print(compare(align_A, matches, A))提供[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]。快速的视觉检查确认了这一点:A的前14个字符是匹配的(5个Es,6个空格和3个Hs)。和
print(compare(align_B, matches, B))提供[0, 1, 2, 10, 11, 15, 16, 17, 18, 19, 20, 21, 22, 23]。
https://stackoverflow.com/questions/50687768
复制相似问题