This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Replacing codons using codon table on python

Hi there! I am trying to pull out a particular codon out of my DNA sequence and run it through a codon table/dictionary to get it to replace with another degenerate codon. I have managed to slice the particular codon out but I am not sure how to run it through the codon table and make it replace accordingly so that the amino acid is still the same.

Eg. I do not want UCA as my codon, hence I run it through a dictionary so that it will replace it with either UCC, UCA, UCG for the amino acid serine.

Much appreciated!

dna python

The general recipe (with a couple hints) for this is:

  1. Change your sequence into a list of characters seq = list(seq)

  2. Fill a dictionary with the replacements that you want to do, e.g.

    repl = {"UCA": ["UCU", "UCG", "UCC"] , ... }

  3. Iterate over your sequence in substrings of 3 (=codons). Hint: the range function can take 3 parameters, one of those is step size.

  4. IF the current codon is in your replacements dictionary:

    a. choose the replacement codon (random?)

    b. use slicing to replace the current codon with one replacement codon: seq[i:i+3] = replacement

  5. Turn your sequence back into a string "".join(seq)

edit: formatting

0 answers

No answers yet.

Log in to answer this question.