使用随机模块管理DNA突变

时间:2014-05-28 18:13:38

标签: python python-3.x bioinformatics

我想输入DNA序列并制作某种产生具有一定突变频率的序列的发生器。例如,假设我有DNA链“ATGTCGTCACACACCGCAGATCCGTGTTTGAC”,并且我想创建T-> A频率为5%的突变。我将如何创建这个?我听说我可以使用随机模块,但我不是一个经验丰富的程序员,并且从哪里开始有点困惑。关于我能做什么的任何想法?谢谢。

1 个答案:

答案 0 :(得分:2)

非常简单:查看字符串,每当找到可能的突变点时,如果随机数表示变异,则进行变异:

import random
def mutate(string, mutation, threshold):
    dna = list(string)
    for index, char in enumerate(dna):
        if char in mutation:
            if random.random() < threshold:
                dna[index] = mutation[char]

    return ''.join(dna)

如果你想成为更好的人,你可以使用列表理解:

import random
def mutate(string, mutation, threshold):
    return ''.join([mutation[char] if random.random() < threshold 
                                       and char in mutation else char
                                       for char in string])