from random import randint
def replace_base_randomly_using_names(base_seq):
"""Return a sequence with the base at a randomly selected position of base_seq
replaced by a base chosen randomly from the three bases that are not at that
position."""
position = randint(0, len(base_seq) - 1) # −1 because len is one past end
base = base_seq[position]
bases = 'TCAG'
bases.replace(base, '') # replace with empty string!
newbase = bases[randint(0,2)]
beginning = base_seq[0:position] # up to position
end = base_seq[position+1:] # omitting the base at position
return beginning + newbase + end
这是用来模拟突变的。我不明白如何选择一个不同的基(来自TCAG)来确保基确实发生了变化,正如doctype提到的那样。你知道吗
编辑:
上述代码的另一个版本执行相同的操作:
def replace_base_randomly(base_seq):
position = randint(0, len(base_seq) - 1)
bases = 'TCAG'.replace(base_seq[position], '')
return (base_seq[0:position] +
bases [randint(0,2)] +
base_seq[position+1:])
要清楚的是,我的问题是如何用一个不同的基地取代基地?你知道吗
考虑将print语句交错放入代码中,您可以看到它在做什么。算法如下:
它不编辑字符串,而是从旧字符串的两部分加上新的基创建一个新字符串,并返回该字符串。你知道吗
字符串在python中是不可变的,您应该将从
bases.replace(base, '')
返回的字符串重新分配给bases
。你知道吗行
bases.replace(base, '')
实际上并没有更改bases
字符串。要更改bases
字符串,必须设置bases = bases.replace(base, '')
。你自己测试一下从这里开始,现在可能的碱基列表已经减少到只有突变的碱基,函数随机选择一个带有
bases[randint(0, 2)]
的碱基并将其附加到新序列中。你知道吗相关问题 更多 >
编程相关推荐