【问题标题】:Protein sequence pattern-matching python蛋白质序列模式匹配python
【发布时间】:2012-06-13 15:49:31
【问题描述】:

我正在研究蛋白质序列的匹配算法。我从一个对齐的蛋白质序列开始,我试图将一个未对齐的序列转换为正确对齐的序列。这是一个例子:

原始对齐序列:----AB--C-D-----

错位序列:--A--BC---D-

预期的输出应如下所示:

原始对齐序列:----AB--C-D-----

错位序列:----AB--C-D-----(现在都一样了)

我被告知要非常具体地说明我的问题,但我尝试匹配的序列长度超过 4000 个字符,粘贴到此处时看起来很荒谬。不过,我将发布两个代表我的问题的序列,这应该可以。

seq="---A-A--AA---A--"
newseq="AA---A--A-----A-----"
seq=list(seq) #changing maaster sequence from string to list
newseq=list(newseq) #changing new sequence from string to list
n=len(seq) #obtaining length of master sequence
newseq.extend('.') #adding a tag to end of new sequence to account for terminal gaps

print(seq, newseq,n) #verification of sequences in list form and length

for i in range(n)
    if seq[i]!=newseq[i]:
        if seq[i] != '-': #gap deletion
            del newseq[i]

        elif newseq[i] != '-':
            newseq.insert(i,'-') #gap insertion


        elif newseq[i] == '-':
            del newseq[i]


old=''.join(seq) #changing list to string
new=''.join(newseq) #changing list to string
new=new.strip('.') #removing tag

print(old) #verification of master-sequence fidelity
print(new) #verification of matching sequence

我从这个特定的代码和序列集得到的输出是:

---A-A--AA---A--

---A-A--A----A-----A-----

我似乎无法让循环多次正确删除字母之间不需要的破折号,因为其余的循环迭代用于添加破折号/删除破折号对。
这是解决问题的良好开端。

我怎样才能成功地编写这个循环以获得我想要的输出(两个相同的序列)

【问题讨论】:

  • 此代码示例中没有循环
  • 感谢您指出这一点!我想我在随机播放中丢失了循环命令

标签: python algorithm for-loop pattern-matching sequence


【解决方案1】:

序列比对问题是众所周知的,它的解决方案也有很好的描述。有关介绍性文字,请参阅Wikipedia。我知道的最好的解决方案是动态编程,您可以在this site 看到 Java 的示例实现。

【讨论】:

    【解决方案2】:

    我编辑了你的代码,现在它给出了正确的输出:

    seq="----AB--C-D-----"
    newseq="--A--BC---D-"
    seq=list(seq) #changing maaster sequence from string to list
    newseq=list(newseq) #changing new sequence from string to list
    n=len(seq) #obtaining length of master sequence
    newseq.extend('.') #adding a tag to end of new sequence to account for terminal gaps
    
    print(seq, newseq,n) #verification of sequences in list form and length
    for i in range(len(seq)):
        if seq[i]!=newseq[i]:
           if seq[i]=='-':
               newseq.insert(i,'-')
    
           elif newseq[i]=='-':
               newseq.insert(i,seq[i])
           else:
               newseq.insert(i,seq[i])
    
    else:
        newseq=newseq[0:len(seq)]
    
    old=''.join(seq) #changing list to string
    new=''.join(newseq) #changing list to string
    new=new.strip('.') #removing tag
    
    print(old) #verification of master-sequence fidelity
    print(new) #verification of matching sequence
    

    输出:

    ----AB--C-D-----
    ----AB--C-D-----
    

    对于AA---A--A-----A-----

    ---A-A--AA---A--
    ---A-A--AA---A--
    

    【讨论】:

    • 这个算法和上一个一样,没有考虑特定位置、不同大小的字符串之间可能存在的不匹配,并且如果之后出现更好的解决方案,也没有回溯。请考虑研究动态规划。
    • 以后的工作我一定会追求动态规划。不过,此代码通常可用于我的直接目的(序列始终是相同的顺序,只有一个解决方案,并且此代码适用于不同大小的字符串)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 2013-01-16
    • 1970-01-01
    • 2022-10-08
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多