Opticgenius - 1 year ago 113
Python Question

# Codon Pairs Dictionary

I am trying to make a function

codon_pairs(pairs, codonsA, codonsB)
that takes in three arguments; a dictionary
pairs
and two lists,
codonsA
and
codonsB
. The dictionary contains the base pairs and the codon lists contain codon sequences. I am trying to find the complementary codon sequence in
codonsB
for each codon sequence in
codonsA
, and return matching pairs like the following:

pairs = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']

print(codons_pairs(pairs, condonsA, codonsB))

[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]

The first item in the 2-tuple is a codon from
codonsA
and the second item is a matching codon from
codonsB
. For example, the sequences AAG (
codonsA[0]
) and TCC (
codonsB[3]
) are matching pairs, as the base pair of A is T, and the base pair of G is C, highlighted in the
pairs
dictionary.

On the flip side, if a matching pair cannot be found, it will be omitted from the final result.

This is what I have so far:

pairs = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']

def codons_pairs(pairs, codonsA, codonsB):

for A in codonsA:
for B in codonsB:
for i in A:
for j in B:

I'm just not sure how to check pairs between
codonsA
and
codonsB
, with regard to a dictionary. Any help would be greatly appreciated.

Convert codonsB to a set() for fast checking (O(1) membership tests, no need for nested loops), then map each codon from A through your pairs mapping and test the result against the set:

def matching_codons(pairs, codonsA, codonsB):
codonsB = set(codonsB)
for codon in codonsA:
complement = ''.join([pairs[base] for base in codon])
if complement in codonsB:
yield (codon, complement)

The above is a generator function; it'll yield each match as it finds them. You could convert the resulting generator to a list with the list() function, or just iterate over the function.

Demo:

>>> pairs = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
>>> codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
>>> codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
>>> list(matching_codons(pairs, codonsA, codonsB))
[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download