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 内部)以确保确实改变了基础。
编辑:
执行相同操作的上述代码的另一个版本:
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:])
需要明确的是,我的问题是如何用不同的基地替换基地?