【问题标题】:Generating DNA sequence excluding specific sequence生成不包括特定序列的 DNA 序列
【发布时间】:2018-11-13 02:34:27
【问题描述】:

我刚刚开始学习使用 python 编程。在课堂上,我们被要求生成一个不包含特定 6 字母序列 (AACGTT) 的随机 DNA 序列。关键是要创建一个始终返回合法序列的函数。目前,我的函数大约 78% 的时间会生成正确的序列。我怎样才能让它在 100% 的时间内返回合法的序列?任何帮助表示赞赏。

这是我的代码现在的样子:

from random import choice
def generate_seq(length, enzyme):
    list_dna = []
    nucleotides = ["A", "C", "T", "G"]
    i = 0
    while i < 1000:
        nucleotide = choice(nucleotides)
        list_dna.append(nucleotide)
        i = i + 1

    dna = ''.join(str(nucleotide) for nucleotide in list_dna)
    return(dna) 


seq = generate_seq(1000, "AACGTT")
if len(seq) == 1000 and seq.count("AACGTT") == 0:
    print(seq)

【问题讨论】:

  • 在您 22% 的不合法案例中,是什么导致数据不合法?
  • 如果唯一的目标是获得不含 6 个字母序列的 DNA 序列,只需更改 nucleotides = ["A"] 并在技术上正确(最好的正确)。如果您碰巧生成了该序列,您也可以删除它。或者您可以以不同的方式进行约束。这取决于你以后想用这个序列做什么。
  • 您的 DNA 序列可以合法地包含 AACGT,但是为了保持合法性,下一个选择的碱基只能从 [A, C, G] 中挑选,因为 T 是不允许的。

标签: python dna-sequence


【解决方案1】:

一种选择是检查循环中的最后几个条目,并且仅在尚未创建“坏”序列时才继续追加。但是,这可能导致出现“AACGT”序列的概率高于真实随机,只是用不同的字母代替最后一个“T”

from random import choice
def generate_seq(length, enzyme):
    list_dna = []
    nucleotides = ["A", "C", "T", "G"]
    i = 0
    while i < 1000:
        nucleotide = choice(nucleotides)
        list_dna.append(nucleotide)
        #check for invalid sequence. If found, remove last element and redraw
        if ''.join(list_dna[-6:]) == "AACGTT":
            list_dna.pop()
        else:
            i = i + 1

    dna = ''.join(str(nucleotide) for nucleotide in list_dna)
    return(dna) 


seq = generate_seq(1000, "AACGTT")
if len(seq) == 1000 and seq.count("AACGTT") == 0:
    print(seq)

【讨论】:

    【解决方案2】:

    一个想法是检查前 5 个核苷酸是否等于 AACGT,在这种情况下,仅从 ["A", "C", "G"] 中选择。

    from random import choice
    
    
    def generate_seq(length, enzyme, bad_prefix="AACGT"):
        list_dna = []
        nucleotides = ["A", "C", "T", "G"]
        i = 0
        while i < 1000:
            if list_dna[-5:] != bad_prefix:
                nucleotide = choice(nucleotides)
            else:
                nucleotide = choice(["A", "C", "G"])
            list_dna.append(nucleotide)
            i = i + 1
    
        dna = ''.join(str(nucleotide) for nucleotide in list_dna)
        return dna
    
    
    seq = generate_seq(1000, "AACGTT")
    if len(seq) == 1000 and seq.count("AACGTT") == 0:
        print(seq)
    

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多