Reputation:
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
Upvotes: 2
Views: 1090
Reputation: 12347
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)
Upvotes: 2
Reputation: 213
To return a list of sequences instead of just one, you simply have to call you function multiple times.
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)
And if it's easier to read for you:
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
Upvotes: 1