DNA 序列中的随机突变 - python 中的突变率

random mutation in a DNA sequence - mutation rate in python

已编辑

我创建了一个函数,它引入了具有确定突变率(示例 0.05)的随机突变。

input='ATCTAGGAT'


def mutate_v2(sequence, mutation_rate):
    dna_list = list(sequence)
    for i in range(len(sequence)):
        r = random.random()
        if r < mutation_rate:
            mutation_site = random.randint(0, len(dna_list) - 1)
            print(mutation_site)
            dna_list[mutation_site] = random.choice(list('ATCG'))
        return ''.join(dna_list)


## run the function
mutate_v2(input, 0.01)

现在我希望函数将序列列表(示例:list_sequences = ['ATTCTGTA', 'TTCGCTAA', 'ACCCGCTA'])和 return 每个突变序列(在列表中:output)作为输入。

请帮忙!

谢谢

要return一个序列列表而不是一个序列,你只需多次调用你的函数。

def multiple_draws(sequence, mutation_rate, n_draws=1):
    return [mutate_v2(sequence, mutation_rate) for _ in range (n_draws)]

print(multiple_draws(input, 0.01, 10)

如果您更容易阅读:

def multiple_draws(sequence, mutation_rate, n_draws=1):
    mutations = []
    for _ in range(n_draws):
       mutation = mutate_v2(sequence, mutation_rate)
       mutations.append(mutation)
    return mutations

我会保持 mutate_v2 的界面不变(我认为界面就这样没问题),但使用 list comprehension 调用它,就像这样:

input = [seq_1, seq_2, seq_n]
mutated_input = [mutate_v2(s) for s in input]

或者,您可以像这样将其包装到自己的方法中:

def mutate_multiple(sequences):
    return [mutate_v2(s) for s in sequences]

# call the method:
input = [seq_1, seq_2, seq_n]
mutated_input = mutate_multiple(input)