I wrote a program that takes a list of 3-letter strings (also called codons, for those who know biology) and for each string, it will pick any one of the 3 letters (randomly) and will replace that letter with eithe A, G, C, or T (at random). For example: For the string GCT, it will pick any one of the 3 positions at random i.e. C, and then it will randomly change it to either A, G, C, or T i.e. T. So the new string (or codon) generated will be GTT, and so on for the next string on the list.
However, there is one problem. The way I have written it doesn't check to make sure that the new string that it's generating is not the same as the old one. So if the program randomly chooses to change a letter to the same one as initial, then it will output the same string by chance i.e. switching the C from GCT into a C again and producing GCT. I want to make sure this doesn't happen so that the program isn't generating the same string, because this does happen by random chance when analyzing hundreds of thousands of these codons/strings. I tried to do this by using list(A, G, T, C) - codon[index] in the second line of my 'for' loop, but it didn't work.
I won't bother you with the entire code, but initially I just opened the file where my codons/strings are listed (in a column) and appended all of them into a list and named it 'codon'. Here's the remainder:
import random
def string_replace(s,index,char):
return s[:index] + char + s[index+1:]
for x in range(1,10): # I set the range to 10 so that I can manually check if the program worked properly
index = random.randrange(3)
letter_to_replace = random.choice(list({"A", "G", "T", "C"} - {codon[index]}))
mutated_codon = [string_replace(codon[x], index, letter_to_replace)]
print mutated_codon)
- {codon[index]
--> this will be a 3 letter code if codon is a list of 3 letter strings
think you want codon[x][index]
edit function, to have your codonset there, rather then down, give indexes to replace there,
I don't know how you will create list of codons, but here I have one example
listofcodons=["ATC", "AGT", "ACC"]
for s in listofcodons:
index=random.randrange(3)
mutated=string_replace(s,index)
print mutated
def string_replace(s,index):
codonset=set(["A","C","G","T"])
toreplace=s[index]
#codonset.pop(toreplace)
codonset.remove(toreplace)
char=random.choice(codonset)
return s[:index] + char + s[index+1:]
So I was bored and decided to code golf it (could be shorter but was satisfied here).
from random import choice as c
a=['ACT','ATT','GCT'] # as many as you want
f=lambda s,i:s[:i]+c(list(set(['A','G','C','T'])-set(s[i])))+s[i+1:]
b=[f(s,c([0,1,2]))for s in a]
print b
a
can be your list of codons and b
will be a list of codons with a random index replaced by a random (never the same) letter.
Ok to answer your new question:
from random import choice as c
codons = ['ACT','ATT','GCT']
f=lambda s,i:s[:i]+c(list(set(['A','G','C','T'])-set(s[i])))+s[i+1:]
mutated_codons = [f(s,c([0,1,2]))for s in codons]
for codon in mutated_codons:
try:
print codon, codon_lookup[codon]
except KeyError, e:
print e
Assuming your dictionary is called condon_lookup
, this will print each mutated codon followed by its amino acid lookup. Your old code was looping over the letters in each mutated codon instead of looping through a list of codons like you intended.
You could use a while loop:
import random
mutated_codon=codon='GCT'
while mutated_codon==codon:
li=list(mutated_codon)
li[random.choice([0,1,2])]=random.choice(["A", "G", "T", "C"])
mutated_codon = ''.join(li)
print codon, mutated_codon
How about something like this?
#!/usr/local/cpython-3.3/bin/python
import random
def yield_mutated_codons(codon):
possible_set = set({"A", "G", "T", "C"})
for x in range(1, 10):
index = random.randrange(3)
letter_to_replace = codon[index]
current_set = possible_set - set(letter_to_replace)
codon[index] = random.choice(list(current_set))
yield ''.join(codon)
def main():
codon = list('GAT')
for mutated_codon in yield_mutated_codons(codon):
print(mutated_codon)
main()