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)
--> this will be a 3 letter code if codon is a list of 3 letter strings think you want codon[x][index]
How about something like this?
So I was bored and decided to code golf it (could be shorter but was satisfied here).
a
can be your list of codons andb
will be a list of codons with a random index replaced by a random (never the same) letter.Ok to answer your new question:
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:
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