Skip to content Skip to sidebar Skip to footer

Converting Letters Using A Dictionary

I'm trying to write a program that converts letters matching a key in a dictionary to the value associated with that key e.g. if the dictionary is {'A':'T', 'C':'G', 'T':'A', 'G':'

Solution 1:

another solution is using maketrans from string

fromstring import maketrans
complementTrans = maketrans("ACTG", "TGAC")

poolA =  ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
[codon.translate(complementTrans) for codon in poolA]

you get:

['TTC', 'ATG', 'GCC', 'CTA', 'AAC', 'CAC', 'GTA', 'CCG', 'TAA', 'AGA']

Bonus

It is always better to use biopython library, for example

poolA =  ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']

from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

[str(Seq(seq, IUPAC.unambiguous_dna).complement()) for seq in poolA]

you get the same result

Bonus 2

Fixing your code, I remove unnecessary variable counter

poolA =  ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'} 

defmatching_codons(complements, poolA):
    answer = []
    for i in poolA:
        codon = ''# inside of for, codon reset in each iterationfor a in i:
            codon+= complements[a]
        answer.append(codon)  # outside of secondary for, codon have three leters to end iterationsreturn answer

matching_codons(complements, poolA)

Solution 2:

Define your dictionary:

>>>d = {'A':'T', 'C':'G', 'T':'A', 'G':'C'} 

Now, using that dictionary, AAG can be translated as follows:

>>> ''.join(d[c] for c in'AAG')
'TTC'

Solution 3:

So, to take your example here:

d = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}

x = "AAG"

To get your result to output "TTC", you can do this:

s = ""for i in x:
    s += d.get(i)

A one liner for this:

y = "".join([d.get(i) for i in x])

Solution 4:

Assumptions: complements is the dictionary {'A':'T', 'C':'G', 'T':'A', 'G':'C'}

You've got a pretty good gameplan so far, you're only missing a few ideas: 1) complements.values() will give you a view of all the values in the dict (see this). You want to instead look up the particular value for the character you are on. As you have already checked if the key is in the dictionary with "if a in complements:", you can look it up with "compliments[a]". 2) Once you've built a complemented codon, you can add it to your list of answers and clear the codon you are building at the moment.

You should note that there are cleaner answers posted here, but I wanted to keep as much of your code as possible. Please read through the other answers here and try to understand the better methods of achieving your goal as well.

If you have any more questions, please feel free to ask!

Solution 5:

Consider making a translation table:

>>> from string import maketrans
>>> transtab = maketrans("ACTG", "TGAC")
>>> [s.translate(transtab) for s in ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']]
['TTC', 'ATG', 'GCC', 'CTA', 'AAC', 'CAC', 'GTA', 'CCG', 'TAA', 'AGA']

Post a Comment for "Converting Letters Using A Dictionary"