-1

This is the code I wrote to translate a the dna sequence to the protein sequence. The function works, but when I try to output the protein sequence only the last sequence appears in the file.

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 

all thats being exported to the file is the last of 1734 sequences

kitkat56
  • 1
  • 1
  • 1
    I'm surprised that your code writes the _first_ line to "protein_sequences.fasta". I'd expect it to write the _last_ line, since you don't start writing until after the `for line in file:` loop is finished. – PM 2Ring Nov 28 '16 at 10:31
  • Use `Biopython` for this, also see http://stackoverflow.com/questions/40834338/how-to-convert-a-set-of-dna-sequences-into-protein-sequences-using-python-progra – Chris_Rands Nov 28 '16 at 10:33

1 Answers1

0

Indent the print.
You are printing at the level of opening/closing the file not per line within the file

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 
tomc
  • 1,146
  • 6
  • 10