繁体   English   中英

如何修改以下程序,以确保替换的每个字母都是唯一的?

[英]How can I modify the following program so that I can make sure that each letter it replaces is unique?

我编写了一个程序,该程序接收一个由3个字母组成的字符串列表(对于那些了解生物学的人也称为密码子),并且对于每个字符串,它将(随机地)选择3个字母中的任何一个并将该字母替换为eithe A, G,C或T(随机)。 例如:对于字符串GCT,它将随机选择3个位置中的任意一个,即C,然后将其随机更改为A,G,C或T即T。因此,新字符串(或密码子)生成的将是GTT,依此类推,列表中的下一个字符串。

但是,有一个问题。 我编写它的方式不会检查以确保它生成的新字符串与旧字符串不相同。 因此,如果程序随机选择将字母更改为与首字母相同的字母,那么它将偶然输出相同的字符串,即再次将C从GCT转换为C并生成GCT。 我想确保不会发生这种情况,以使程序不会生成相同的字符串,因为在分析成千上万个此类密码子/字符串时,这确实是偶然发生的。 我试图通过在“ for”循环的第二行中使用list(A,G,T,C)-codon [index]来做到这一点,但是没有用。

我不会为整个代码烦恼,但是最初,我只是打开了列出我的密码子/字符串(在列中)的文件,并将它们全部添加到列表中并命名为“密码子”。 这是剩余的:

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]

编辑功能,让密码子集在那里,而不是向下,给索引替换在那里,我不知道如何创建密码子列表,但是这里有一个示例

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:]

所以我很无聊,决定为它打高尔夫球(可能会短一些,但在这里很满意)。

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可以是您的密码子列表, b可以是用随机索引(由不相同)替换的随机索引的密码子列表。

可以回答您的新问题:

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

假设您的字典称为condon_lookup ,它将打印每个突变的密码子,然后是其氨基酸查找。 您的旧代码是遍历每个突变密码子中的字母,而不是遍历您想要的密码子列表。

您可以使用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     

这样的事情怎么样?

#!/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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM