Python: Appending a to a list from a dictionary
Question: This is going to be long but I don't know how else to effectively explain
this.

So I have 2 files that I am reading in. The first one has a list of
characters.The second file is a list of 3 characters and then it's matching
identifier character(separated by a tab).

With the second file I made a dictionary with the 3 characters as the items
and the one character as the corresponding key. What I need to do is take 3
characters at a time from the first list and compare it with the dictionary.
If there is a match I need to take the corresponding key and append it to a
new list that I will print out. If the match is a '*' character I need to stop
not continue comparing the list to the dictionary.

I'm having trouble with the comparing and then making the new list by using
the append function.

Here is part of the first input file:

    
    
    Seq0
    ATGGAAGCGAGGATGtGa
    

Here is part the second:

    
    
    AUU     I
    AUC     I
    AUA     I
    CUU     L
    GUU     V
    UGA     *
    

Here is my code so far:

    
    
    input = open("input.fasta", "r")
    codons = open("codons.txt", "r")
    
    counts = 1
    amino_acids = {}
    
    for lines in codons:
            lines = lines.strip()
            codon, acid = lines.split("\t")
            amino_acids[codon] = acid
            counts += 1
    
    count = 1
    
    for line in input:
            if count%2 == 0:
                    line = line.upper()
                    line = line.strip()
                    line = line.replace(" ", "")
                    line = line.replace("T", "U")
    
                    import re
    
                    if not re.match("^[AUCG]*$", line):
                            print "Error!"
    
                    if re.match("^[AUCG]*$", line):
                            mrna = len(line)/3
                            first = 0
                            last = 3
    
                            while mrna != 0:
                                    codon = line[first:last]
                                    first += 3
                                    last += 3
                                    mrna -= 1
                                    list = []
    
                                    if codon == amino_acids[codon]:
                                            list.append(acid)
    
                                            if acid == "*":
                                                    mrna = 0
    
                                    for acid in list:
                                            print acid
    

So I want my output to look something like this:

    
    
    M    L    I    V    *
    

But I'm not getting even close to this. Please help!


Answer: The following is purely untested code. Check indentation, syntax and logic,
but should be closer to what you want.

    
    
    import re
    
    codons = open("codons.txt", "r")
    amino_acids = {}
    for lines in codons:
            lines = lines.strip()
            codon, acid = lines.split("\t")
            amino_acids[codon] = acid
    
    input = open("input.fasta", "r")
    count = 0
    list = []
    for line in input:
        count += 1
        if count%2 == 0:    #i.e. only care about even lines
            line = line.upper()
            line = line.strip()
             line = line.replace(" ", "")
             line = line.replace("T", "U")
    
            if not re.match("^[AUCG]*$", line):
                    print "Error!"
            else:
                mrna = len(line)/3
                  first = 0
                  while mrna != 0:
                      codon = line[first:first+3]
                      first += 3
                      mrna -= 1
                      if codon in amino_acids:
                          list.append(amino_acids[codon])
                          if acid == "*":
                              mrna = 0
    
    for acid in list:
        print acid
    

