2016-11-28 149 views
-1

這是我寫的將dna序列翻譯成蛋白質序列的代碼。該功能起作用,但是當我嘗試輸出蛋白質序列時,只有最後一個序列出現在文件中。寫入fasta文件,只創建最後一行

def translate(dna_seq): #create function 
    "this function translates a dna sequence into a single letter code amino acid sequence" 
    #create dictionary for genetic code codon:amino acid 
    gencode = {'TTT':'F', 'TTC':'F', 'TTA':'L', 'TTG':'L', 'CTT':'L', 'CTC':'L', 'CTA':'L', 'CTG':'L', 'ATT':'I', 'ATC':'I', 'ATA':'I', 'ATG':'M', 'GTT':'V', 'GTC':'V', 'GTA':'V', 'GTG':'V', 'TCT':'S', 'TCC':'S', 'TCA':'S', 'TCG':'S', 'CCT':'P', 'CCC':'P', 'CCA':'P', 'CCG':'P', 'ACT':'T', 'ACC':'T', 'ACA':'T', 'ACG':'T', 'GCT':'A', 'GCC':'A', 'GCA':'A', 'GCG':'A', 'TAT':'Y', 'TAC':'Y', 'CAT':'H', 'CAC':'H', 'CAA':'Q', 'CAG':'Q', 'AAT':'N', 'AAC':'N', 'AAA':'K', 'AAG':'K', 'GAT':'D', 'GAC':'D', 'GAA':'E', 'GAG':'E', 'TGT':'C', 'TGC':'C', 'TGG':'W', 'CGT':'R', 'CGC':'R', 'CGA':'R', 'CGG':'R', 'AGT':'S', 'AGC':'S', 'AGA':'R', 'AGG':'R', 'GGT':'G', 'GGC':'G', 'GGA':'G', 'GGG':'G', 'TAA':'_', 'TAG':'_', 'TGA':'_'} 
    protein_seq = "" #define blank string for protein sequence 
    for x in range(0,len(dna_seq),3): #loop to read the dna_seq from the beginning, 0, for entire length of dna_seq, counting by 3 
     codon = dna_seq[x:x+3] #estabilishing that each codon is at X base index to 3 indices over, so codon is every 3 letters 
     protein_seq += gencode[codon] #adding the amino acid, value, corresponding to the codon, key, from the dict gencode 
    return protein_seq 

with open('cDNA_sequences(2).csv', 'r') as file: #open file for reading, 
    for line in file: #create loop to read each line 
     data = line.rstrip("\n").split(",") #create list by removing newline and comma 
     seq_name = data[0] #sequence name is first column of data 
     dna_seq = data[1] #dna base sequence is second column 

     protein_seq = translate(dna_seq) #protein sequence function of dna sequence from file 

    with open ("protein_sequences.fasta", "w") as outfile: #open file for writing 
     outfile.write(">{}\n".format(seq_name)) #write > and sequence name 
     outfile.write("{}\n".format(protein_seq)) #write protein sequence 

所有這就是被導出到文件是最後1734個序列

+1

我很驚訝,你的代碼寫入_first_行「protein_sequences.fasta」中每行而不是文件。我期望它寫入_last_行,因爲直到'for line in file:'循環完成後纔開始寫入。 –

+0

使用'Biopython'爲此,也請參閱http://stackoverflow.com/questions/40834338/how-to-convert-a-set-of-dna-sequences-into-protein-sequences-using-python-progra –

回答

0

縮進打印的。
您在對外開放水平打印/關閉文件

with open('cDNA_sequences(2).csv', 'r') as file: #open file for reading, 
    for line in file: #create loop to read each line 
     data = line.rstrip("\n").split(",") #create list by removing newline and comma 
     seq_name = data[0] #sequence name is first column of data 
     dna_seq = data[1] #dna base sequence is second column 

     protein_seq = translate(dna_seq) #protein sequence function of dna sequence from file 

     with open ("protein_sequences.fasta", "w") as outfile: #open file for writing 
      outfile.write(">{}\n".format(seq_name)) #write > and sequence name 
      outfile.write("{}\n".format(protein_seq)) #write protein sequence