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':'C'} and the string is 'AAG' the output should be 'TTC'.
EDIT: This is what I've got now thanks to some of your answers:
def matching_codons(complements, poolA):
answer = []
codon = ''
counter = 0
for i in poolA:
for a in i:
codon+= complements[a]
counter += 1
if counter == 3:
answer.append(codon)
Unfortunately this only translates the first 3 letters - how can I make it keep running through the loop?
Note: poolA is a list of strings e.g. ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
Note2: I can't hard-code anything like a translation table because technically the dictionary input can be changed
another solution is using maketrans from string
from string 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'}
def matching_codons(complements, poolA):
answer = []
for i in poolA:
codon = '' # inside of for, codon reset in each iteration
for a in i:
codon+= complements[a]
answer.append(codon) # outside of secondary for, codon have three leters to end iterations
return answer
matching_codons(complements, poolA)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With