pythonbioinformaticsdna-sequencegenetics

random mutation in a DNA sequence - mutation rate in python


EDITED

I created a function that introduces random mutations with a determined mutation rate (example 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)

Now I want the function to take as input a list of sequences (example: list_sequences = ['ATTCTGTA', 'TTCGCTAA', 'ACCCGCTA']) and return each mutated sequence (in a list: output).

Any help please!

Thanks


Solution

  • I would keep the interface of mutate_v2 the same (I think the interface is OK the way it is), but call it using list comprehension, like so:

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

    Alternatively, you can wrap it into its own method like so:

    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)