Search code examples
pythonpool

Matching codons


So I'm now learning about nested pools and I don't really get it. my task is to return all matching pairs in following format:

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

so this is my code:

def matching_codons(complements, poolA, poolB):
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
poolB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
final = []
 for i in poolA:
  for z in poolB:
    if i and z in complements:
      final.append()
      return(final)

It's not working and I don't know why, I don't quite get it. How can I make a statement so that the poolA and the poolB will match according to the dictionary provided?


Solution

  • Try to search for the complements in the other pool, instead of walking through every elements in poolA and poolB.

    def _complements(complements, str):
        return ''.join([complements[i] for i in str])
    
    def matching_codons(complements, poolA, poolB):
        final = []
        for i in poolA:
            if _complements(complements, i) in poolB:
                final.append((i, _complements(complements, i)))
        return final
    complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
    poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
    poolB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
    print matching_codons(complements, poolA, poolB)
    

    output

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