如何翻译多个fasta序列?

时间:2016-03-29 19:39:07

标签: bioinformatics fasta

所以我有一个FASTA文件,我需要翻译多个序列(cDNA,所以我不需要搜索起始密码子)。我可以将它清理干净并让它只打印出序列,但我似乎无法将它们转换为蛋白质序列(我不想使用BioPython)。任何建议都非常感谢。

#Open the file for reading
FASTA=open('mRNA_database.fasta', 'r')


def readSeq(FASTA):
    for line in FASTA:
        if line.startswith('>'):
            continue
        line = line.strip()
        #print(line)

g_code=dict()
g_code = {'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
    'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
    'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
    'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
    'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
    'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
    'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
    'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
    'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
    'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
    'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
    'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
    'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
    'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
    'TAC':'Y', 'TAT':'Y', 'TAA':'stop', 'TAG':'stop',
    'TGC':'C', 'TGT':'C', 'TGA':'stop', 'TGG':'W'}

seq = readSeq(FASTA)

def aa_to_prt(seq, g_code):
    prt = ''
    for i in range(0, len(seq), 3):
        codon = seq[i:i+3]
        prt+= g_code[codon]
    return prt
aa_to_prt(seq, g_code)

1 个答案:

答案 0 :(得分:0)

您的readSeq函数不会返回任何值。此外,它没有正确连接行或正确处理多个序列。您现在正在做的所有事情(即使您返回线路)正在翻译每一行。

这是您脚本的改进版本,但我仍然不建议使用此版本;它还是很天真。例如,这个版本(和你的一样)不会寻找开始或终止密码子,所以如果你没有一套策划的核苷酸输入,这可能会变成乱码。

如果这是一个真正的问题,而不是家庭作业,那么就没有理由避免使用BioPython来处理这些问题和其他问题,并且已经针对现实问题进行了广泛的测试。

# No need to define g_code as a dict first
g_code = {'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
    'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
    'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
    'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
    'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
    'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
    'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
    'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
    'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
    'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
    'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
    'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
    'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
    'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
    'TAC':'Y', 'TAT':'Y', 'TAA':'stop', 'TAG':'stop',
    'TGC':'C', 'TGT':'C', 'TGA':'stop', 'TGG':'W'}

def readSeq(fastaF):
    fasta_nts = {}  # We can use a dict to keep the header information 
    fasta_description = '' 
    with open(fastaF, 'r') as f:  # Use the with ... as f syntax to ensure proper file handling
        for line in f.readlines():  # readlines, not read
            if line.startswith('>'): 
                fasta_description = line[1:].strip() # Skip the first character (">") and remove newline
                # Store the sequence, line by line, in a list; much faster and more efficient than building a string
                fasta_nts[fasta_description] = []  
            else:
                fasta_nts[fasta_description].append(line.strip()) # Append each line to the list 
        return fasta_nts  # Remember to return something from the function

def aa_to_prt(seq):   # Not necessary to pass g_code dict each time
    prt = []  # Again, build lists, not strings
    for i in range(0, len(seq), 3):
        codon = seq[i:i+3].upper()  # In case fasta sequence is lowercase - make sure matches your keys
        if len(codon) == 3:  # If it's not a 3-character codon it will throw a KeyError
            prt.append( g_code[codon] ) 
    return ''.join(prt) # Join the list at the end to make a string

fastaF = input_file_path
fasta_nts = readSeq(fastaF) 

fasta_prts = {}  # Final output will save the fasta description as dict key
for (description, sequence_list) in fasta_nts.items():
    fasta_prts[description] = aa_to_prt(''.join(sequence_list))