通過每個終止密碼子運行一個循環,並由此分裂將導致不正確的輸出,因爲我認爲這些終止密碼子可以以序列中的任何順序出現,並且對終止密碼子列表的迭代將要求停止位於相同的順序。
所以,如果我理解正確的話,您將需要掃描您的字符串由左到右,並搜索密碼子的方法:
class Strand:
def __init__(self, code):
self.code = code
self.endCodons = ["TAG", "TAA", "TGA"]
self.genes = []
def getGenes(self):
if (len(self.code) % 3 != 0):
print("Input sequence is not divisible by 3?")
# In this, we assume each stop codon is always 3 characters.
iteration = 0
lastGeneEnd = 0
while (iteration < len(self.code)):
# What is our current 3 character sequence? (Unless it's at the end)
currentSequence = self.code[iteration:iteration + 3]
# Check if our current 3 character sequence is an end codon
if (currentSequence in self.endCodons):
# What will our gene length be?
geneLength = (iteration + 3) - lastGeneEnd
# Make sure we only break into multiples of 3
overlap = 3 - (geneLength % 3)
# There is no overlap if our length is already a multiple of 3
if (overlap == 3): overlap = 0
# Modify the gene length to reflect our overlap into a multiple of 3
geneLength = geneLength + overlap
# Update the iteration so we don't process any more than we need
iteration = iteration + overlap + 3
# Grab the entire gene sequence, including the stop codon
gene = self.code[lastGeneEnd:iteration]
# If we have a 3-length gene and there's nothing left, just append to the last gene retrieved as it has
# got to be part of the last sequence
if (len(gene) == 3 and iteration >= len(self.code)):
lastIndex = len(self.genes) - 1
self.genes[lastIndex] = self.genes[lastIndex] + gene
break
# Make sure we update the last end index so we don't include portions of previous positives
lastGeneEnd = iteration
# Append the result to our genes and continue
self.genes.append(gene)
continue
iteration = iteration + 1
strand = Strand("ATCATGCACATAGAAACTGATACACACCACAGTGATCACATGAAGTACACATG")
strand.getGenes()
print("Got Genes: ")
print(strand.genes)
for gene in strand.genes:
print("Sequence '%s' is a multiple of 3: %u" % (gene, len(gene) % 3 == 0))
我不是一個真正的生物學家,所以我可能已經取得一些不正確的假設。
編輯:
的代碼,保證休息變成三的倍數,但我似乎還是不太明白所需的邏輯。它在給定的例子中工作,但我不確定它是否像其他情況下那樣工作。
你期望你的程序做什麼? 'self.endCodons'是一個空列表,所以'getGenes()'不會做任何事情。 – MattDMo
什麼是* STOP密碼子*?你需要把它們放在你的問題中! – Kasramvd
https://www.google.com/#q=stop+codons – MattDMo