【问题标题】:Find longest palindrome substring of a piece of DNA找到一段 DNA 的最长回文子串
【发布时间】:2020-04-25 10:05:13
【问题描述】:

我必须创建一个函数来打印一段 DNA 的最长回文子串。我已经写了一个函数来检查一段 DNA 本身是否是回文。请参阅下面的函数。

def make_complement_strand(DNA):
    complement=[]
    rules_for_complement={"A":"T","T":"A","C":"G","G":"C"}
    for letter in DNA:
        complement.append(rules_for_complement[letter])
    return(complement)

def is_this_a_palindrome(DNA): 
        DNA=list(DNA)
        if DNA!=(make_complement_strand(DNA)[::-1]):     
            print("false")                  
            return False
        else:                             
            print("true")
            return True

is_this_a_palindrome("GGGCCC") 

但是现在:如何制作一个函数来打印 DNA 字符串的最长回文子串?

回文在遗传学背景下的含义与用于单词和句子的定义略有不同。由于双螺旋是由两条成对的核苷酸链形成的,它们在 5' 到 3' 方向上以相反的方向运行,并且核苷酸总是以相同的方式配对(对于 DNA,腺嘌呤 (A) 与胸腺嘧啶 (T),与尿嘧啶 (U) 代表 RNA;胞嘧啶 (C) 与鸟嘌呤 (G)),如果(单链)核苷酸序列与其反向互补序列相等,则称为回文序列。例如,DNA序列ACCTAGGT是回文的,因为它的核苷酸互补序列是TGGATCCA,将互补序列中的核苷酸顺序颠倒得到原始序列。

【问题讨论】:

  • 更清楚地解释你对回文的定义。顺便说一句,您在is_this_a_palindrome() 函数的开头将DNA 参数设置为一个空列表??
  • 你只需要检查一半的字符串对着后半部反转
  • @muyustan,我添加了一个定义!现在清楚了吗?
  • @stark,我明白你的意思,但我是 python 新手,不知道该怎么做。
  • 好多了,现在,任何人都可以在没有 DNA 序列信息的情况下解决这个问题 :)

标签: python python-3.7 palindrome dna-sequence


【解决方案1】:

在这里,这应该是获取最长回文子串的不错起点。

def make_complement_strand(DNA):
    complement=[]
    rules_for_complement={"A":"T","T":"A","C":"G","G":"C"}
    for letter in DNA:
        complement.append(rules_for_complement[letter])
    return(complement)

def is_this_a_palindrome(DNA): 
        DNA=list(DNA)
        if DNA!=(make_complement_strand(DNA)[::-1]):     
            #print("false")                  
            return False
        else:                             
            #print("true")
            return True


def longest_palindrome_ss(org_dna, palindrone_func):
    '''
    Naive implementation-

    We start with 2 pointers.
    i starts at start of current subsqeunce and j starts from i+1 to end
    increment i with every loop

    Uses palindrome function provided by user

    Further improvements- 
    1. Start with longest sequence instead of starting with smallest. i.e. start with i=0 and j=final_i and decrement.
    '''
    longest_palin=""
    i=j=0
    last_i=len(org_dna)
    while i < last_i:
        j=i+1
        while j < last_i:
            current_subsequence = org_dna[i:j+1]
            if palindrone_func(current_subsequence):
                if len(current_subsequence)>len(longest_palin):
                    longest_palin=current_subsequence
            j+=1
        i+=1
    print(org_dna, longest_palin)
    return longest_palin


longest_palindrome_ss("GGGCCC", is_this_a_palindrome)
longest_palindrome_ss("GAGCTT", is_this_a_palindrome)
longest_palindrome_ss("GGAATTCGA", is_this_a_palindrome)

这里有一些处决 -

mahorir@mahorir-Vostro-3446:~/Desktop$ python3 dna_paln.py 
GGGCCC GGGCCC
GAGCTT AGCT
GGAATTCGA GAATTC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2020-03-28
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多