【问题标题】:index of the residue matched using pairwise2 in biopython在 biopython 中使用 pairwise2 匹配的残基索引
【发布时间】:2025-12-22 15:00:10
【问题描述】:

我有兴趣了解在 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

我想从 seq A 中获取与 seq B 匹配的所有 EsHs' ' 的匹配索引。

我该怎么做?

【问题讨论】:

    标签: python string alignment biopython pairwise


    【解决方案1】:

    我认为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]

    【讨论】: