[英]How can I modify the following program so that I can make sure that each letter it replaces is unique?
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). 我编写了一个程序,该程序接收一个由3个字母组成的字符串列表(对于那些了解生物学的人也称为密码子),并且对于每个字符串,它将(随机地)选择3个字母中的任何一个并将该字母替换为eithe A, G,C或T(随机)。 For example: For the string GCT, it will pick any one of the 3 positions at random ie C, and then it will randomly change it to either A, G, C, or T ie T. So the new string (or codon) generated will be GTT, and so on for the next string on the list.
例如:对于字符串GCT,它将随机选择3个位置中的任意一个,即C,然后将其随机更改为A,G,C或T即T。因此,新字符串(或密码子)生成的将是GTT,依此类推,列表中的下一个字符串。
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 ie switching the C from GCT into a C again and producing GCT.
因此,如果程序随机选择将字母更改为与首字母相同的字母,那么它将偶然输出相同的字符串,即再次将C从GCT转换为C并生成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.
我试图通过在“ for”循环的第二行中使用list(A,G,T,C)-codon [index]来做到这一点,但是没有用。
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]
->如果密码子是由3个字母组成的字符串列表,认为您想要密码子,这将是3个字母代码[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. a
可以是您的密码子列表, b
可以是用随机索引(由不相同)替换的随机索引的密码子列表。
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. 假设您的字典称为
condon_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: 您可以使用while循环:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.