Python DNA翻译

2024-09-28 03:16:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须从本质上创造我自己的biopython。能够读取、转录和翻译DNA的东西。我已经做到了这一点,它可以做到所有这些,但我不知道如何让程序识别“atg”后面的3个密码子,直到它到达一个终止密码子。现在它只找到一个起始密码子,然后是最近的终止密码子,而不按3计算。有人能帮我弄清楚吗?如果这不合理,很抱歉

#locate start codons
startcodon=0
n=0
while(n < 1):
    startcodon=dataset.find("atg", startcodon, len(dataset)-startcodon)
    #locate stop codons
    taacodon=dataset.find("taa", startcodon+3, len(dataset)-startcodon)
    tagcodon=dataset.find("tag", startcodon+3, len(dataset)-startcodon)
    tgacodon=dataset.find("tga", startcodon+3, len(dataset)-startcodon)
    if(taacodon<tagcodon):
        if(taacodon<tgacodon):
            stopcodon=taacodon
            #print("taacodon", startcodon)
        else:
            stopcodon=tgacodon
            #print("tGacodon", startcodon)

    elif(tgacodon>tagcodon):
        stopcodon=tagcodon
        #print("taGcodon", startcodon)
    else:
        stopcodon=tgacodon
        #print("tGacodon", startcodon)
    #to add sequences to an array
    codon.append(dataset[startcodon:stopcodon+3])
    if(startcodon > len(dataset) or startcodon < 0):
        n = 2;
    startcodon=stopcodon

#reverse the string and swap the letters
n=0;
while(n < len(codon)):
        rcodon.append (codon[n][len(codon[n])::-1])
        #replace a with u
        rcodon[n] = re.sub('a', "u", rcodon[n])
        #replace t with a
        rcodon[n] = re.sub('t', "a", rcodon[n])
        #replace c with x
        rcodon[n] = re.sub('c', "x", rcodon[n])
        #replace g with c
        rcodon[n] = re.sub('g', "c", rcodon[n])
        #replace x with g
        rcodon[n] = re.sub('x', "g", rcodon[n])
        print("DNA sequence: ", codon[n] ,'\n', "RNA sequence:", rcodon[n])
        n=n+1
answer = 0
print("Total Sequences:  ", len(codon)-3)
while (int(answer) >=0):
        #str = "Please enter an integer from 0 to " + str(len(dataset)) + " or -1 to quit: "
        answer = int(input("Please enter a sequence you would like to see or -1 to quit:  "))
        if(int(answer) >= 0):
                print("DNA sequence: ", codon[int(answer)] ,'\n', "RNA sequence:", rcodon[int(answer)])
        dna = codon[int(answer)]
        #dna codon table
        protein = {"ttt" : "Phe-", "ctt" : "Leu-", "att" : "Ile-", "gtt" : "Val-",
           "ttc" : "Phe-", "ctc" : "Leu-", "atc" : "Ile-", "gtc" : "Val-",
           "tta" : "Leu-", "cta" : "Leu-", "ata" : "Ile-", "gta" : "Val-",
           "ttg" : "Leu-", "ctg" : "Leu-", "atg" : "Met-", "gtg" : "Val-",
           "tct" : "Ser-", "cct" : "Pro-", "act" : "Thr-", "gct" : "Ala-",
           "tcc" : "Ser-", "ccc" : "Pro-", "acc" : "Thr-", "gcc" : "Ala-",
           "tca" : "Ser-", "cca" : "Pro-", "aca" : "Thr-", "gca" : "Ala-",
           "tcg" : "Ser-", "ccg" : "Pro-", "acg" : "Thr-", "gcg" : "Ala-",
           "tat" : "Tyr-", "cat" : "His-", "aat" : "Asn-", "gat" : "Asp-",
           "tac" : "Tyr-", "cac" : "His-", "aac" : "Asn-", "gac" : "Asp-",
           "taa" : "STOP", "caa" : "Gin-", "aaa" : "Lys-", "gaa" : "Glu-",
           "tag" : "STOP", "cag" : "Gin-", "aag" : "Lys-", "gag" : "Glu-",
           "tgt" : "Cys-", "cgt" : "Arg-", "agt" : "Ser-", "ggt" : "Gly-",
           "tgc" : "Cys-", "cgc" : "Arg-", "agc" : "Ser-", "ggc" : "Gly-",
           "tga" : "STOP", "cga" : "Arg-", "aga" : "Arg-", "gga" : "Gly-",
           "tgg" : "Trp-", "cgg" : "Arg-", "agg" : "Arg-", "ggg" : "Gly-"  
           }
        protein_sequence = ""

        # Generate protein sequence
        for i in range(0, len(dna)-(3+len(dna)%3), 3):
                protein_sequence += protein[dna[i:i+3]]

        # Print the protein sequence
        print ("Protein Sequence: ", protein_sequence)

我使用的DNA序列以“GGTCAGAAAGCCTCTCATGTCTCACCATCACCATCACCATCACCATCACCATCACCAGAGAGAGAGAGGGCTTTTCATCATCATCATCTTGCTTTGCCAGTTTTTTGGGGTGGACTTTTGCCATATTTC”开头,因此它不是以atg开头,而是必须搜索它。提前谢谢你的建议


Tags: toanswerlenargdatasetserintprint
2条回答

Right now it just finds a start codon and then the nearest stop codon without counting by 3's.

如果要搜索与特定帧对齐的子字符串(即可被3整除的索引),可以先将字符串拆分为相等的块,然后在结果列表中搜索匹配的块

乙二醇

dataset_codons = [dataset[i:i+3] for i in range(0, len(dataset), 3)]
# ggtcagaaaaagccctctcca becomes [ggt cag aaa aag ccc tct cca]

try:
    startcodon = dataset_codons.index('atg', startcodon, len(dataset_codons) - startcodon)
except ValueError:
    break # no more start codons found

(注意startcodon将是匹配atg的块的索引,它正好是对应字符串索引的1/3)

编辑:如果终止密码子只需要和起始密码子在同一帧上,但起始密码子可以在任何地方,这就变得有点棘手了。在这种情况下,您可以继续搜索终止密码子,直到找到索引良好的终止密码子:

def find_codon(codon, string, start):
    i = start + 3
    while i < len(string):
        i = string.find(codon, i) # find the next substring
        if (i - start) % 3 == 0:  # check that it's a multiple of 3 after start
            return i
    return None



startcodon=dataset.find("atg", startcodon)
#locate stop codons
taacodon=find_codon("taa", dataset, startcodon)
tagcodon=find_codon("tag", dataset, startcodon)
tgacodon=find_codon("tga", dataset, startcodon)

stopcodon = min(taacodon, tagcodon, tgacodon)

顺便说一下,我不确定我是否正确理解参数len(dataset)-startcodon的用途。str.find()的第三个参数指定字符串内搜索范围的结束,这意味着随着startcodon的增加,搜索将在数据集实际结束之前停止

如果你想在起始密码子后找到3的终止密码子,你可以在起始密码子后拆分DNA字符串,并找出密码子列表中是否有终止密码子

sequence =  "ggtcagaaaaagccctctccatgtctactcacgatacatccctgaaaaccactgaggaagtggcttttcagatcatcttgctttgccagtttggggttgggacttttgccaatgtatttc"
startcodon = 0
length = len(sequence)
startcodon = sequence.find("atg", startcodon)
# list comprehension. could get codon_list in a for cycle
codon_list = [sequence[i:i+3] for i in range(startcodon+3, length-3, 3)]

# use list.index() to find out whether there is a stop codon in the codon_list
# list.index() would throw error if value is not in the list
try:
    taacodon = startcodon + 3 * codon_list.index('taa')
    print('taa codon is at {}'.format(taacodon))
except:
    print('taa codon is not in the list')

我认为分割DNA序列比使用str.find更有效,因为你需要通过3搜索终止密码子计数,如果使用str.find,你需要判断找到的终止密码子是否在3的距离


编辑:目前我不知道创建新字符串列表是否比搜索原始字符串花费更多

相关问题 更多 >

    热门问题