<pre><code>from Bio.Seq import Seq
coding_dna = Seq(&quot;AAYGANAGYCARAGYAAR&quot;)
print(coding_dna.translate())
</code></pre>
<p>This gives</p>
<blockquote>
<p>NXSQSK</p>
</blockquote>
<p>So .translate() will read a degenerate code and provide the unambiguous amino acid, except where it is unable to do so e.g. GAN because this is a split between asp and glu. Thus GAR or GAY is fine, but GAN gives X</p>
<p>You would then use <code>itertools</code> by mapping (<code>map</code>) against the triplet code (<code>import Bio.Data.CodonTable</code>) to obtain all permutations. Doesn't work for X admittedly and stop codons a special case (3 possibilities).</p>
<p><code>Bio.Data.CodonTable.list_possible_proteins</code> would solve the <code>X</code> problem, but stop codons remain a special case.</p>
<hr />
<p>I seriously did like the Stackoverflow link in the comments, with the double map funtion ... very slick.</p>

CC BY-SA 4.0
2023-07-14T17:20:51.237Z
I am missing the SO link the one with :  the double map funtion ... , could you add it to your answer
pippo1980
CC BY-SA 4.0
2023-07-14T17:23:51.120Z
@pippo1980ONSTRIKE I can't do it now, but I'll look at it early next week. Please check the answer below from acvill
M__
CC BY-SA 4.0
2023-07-14T17:28:54.063Z
cheers, but R is like ancient greek to me, I am following this question though
pippo1980
CC BY-SA 4.0
2022-02-18T22:24:41.660Z
2022-02-19T03:17:43.647Z
2022-02-19T03:17:43.647Z
M__
<p>M__'s approach is good, but, as explained, some subset of the unambiguous DNA sequences will not be recovered because of the ambiguity in reverse-translation due to the degeneracy of the genetic code. I don't know a <code>python</code> approach, but here's an <code>R</code> function that returns a vector of all the possible unambiguous DNA strings:</p>
<pre><code>make_unambiguous &lt;- function(dna) {
  require(tidyverse)
  require(S4Vectors)
  iupac &lt;- tibble(code = c(&quot;A&quot;, &quot;C&quot;, &quot;G&quot;, &quot;T&quot;,
                           &quot;R&quot;, &quot;Y&quot;, &quot;S&quot;, &quot;W&quot;,
                           &quot;K&quot;, &quot;M&quot;, &quot;B&quot;, &quot;D&quot;,
                           &quot;H&quot;, &quot;V&quot;, &quot;N&quot;),
                  base = c(&quot;A&quot;, &quot;C&quot;, &quot;G&quot;, &quot;T&quot;,
                           &quot;AG&quot;, &quot;CT&quot;, &quot;GC&quot;, &quot;AT&quot;,
                           &quot;GT&quot;, &quot;AC&quot;, &quot;CGT&quot;, &quot;AGT&quot;,
                           &quot;ACT&quot;, &quot;ACG&quot;, &quot;ACGT&quot;))

  tibble(dna) |&gt;
    separate_rows(dna, sep = '(?&lt;=.)(?=.)') |&gt;
    left_join(iupac, by = c(&quot;dna&quot; = &quot;code&quot;)) |&gt;
    pull(base) |&gt;
    str_split(&quot;&quot;) |&gt;
    expand.grid(stringsAsFactors = FALSE) |&gt;
    unite(col = sequence, sep = &quot;&quot;) |&gt;
    as_vector()
}

make_unambiguous(dna = &quot;AAYGANAGYCARAGYAAR&quot;)
</code></pre>

CC BY-SA 4.0
2023-07-14T17:08:11.123Z
2023-08-16T16:26:04.057Z
2023-08-16T16:26:04.057Z
acvill
<p>I have a project to write in Python that requires to write a function that given  degenerate DNA sequence (for example: KKGTACACCAG) sequence and a molecular weight interval, returns a list of all unambiguous sequences represented by the given sequence (encoding based on IUPAC alphabet: <em><strong><a href="http://www.bioinformatics.org/sms/iupac.html" rel="nofollow noreferrer">http://www.bioinformatics.org/sms/iupac.html</a></strong></em>).</p>
<p>Is there any function in Biopython to convert a DNA sequence from degenerate to unambiguous base pairs?</p>
<p>Also, Is it possible to use the molecular weight function from Bio.SeqUtils library for an ambiguous DNA?</p>

<python><homework>
Is there any function in Biopython to convert a DNA sequence from ambiguous to unambiguous?
# Is there any function in Biopython to convert a DNA sequence from ambiguous to unambiguous?

(Asked by: username_0 on 2022-01-07)

I have a project to write in Python that requires to write a function that given degenerate DNA sequence (for example: KKGTACACCAG) sequence and a molecular weight interval, returns a list of all unambiguous sequences represented by the given sequence (encoding based on IUPAC alphabet: ***<http://www.bioinformatics.org/sms/iupac.html>***).

Is there any function in Biopython to convert a DNA sequence from degenerate to unambiguous base pairs?

Also, Is it possible to use the molecular weight function from Bio.SeqUtils library for an ambiguous DNA?

**Tags:** python, homework

---

## 2 answers

### Answer by username_1 on 2022-02-18. Score: 3

from Bio.Seq import Seq
coding_dna = Seq("AAYGANAGYCARAGYAAR")
print(coding_dna.translate())

This gives

NXSQSK

So .translate() will read a degenerate code and provide the unambiguous amino acid, except where it is unable to do so e.g. GAN because this is a split between asp and glu. Thus GAR or GAY is fine, but GAN gives X
You would then use itertools by mapping (map) against the triplet code (import Bio.Data.CodonTable) to obtain all permutations. Doesn't work for X admittedly and stop codons a special case (3 possibilities).
Bio.Data.CodonTable.list_possible_proteins would solve the X problem, but stop codons remain a special case.

I seriously did like the Stackoverflow link in the comments, with the double map funtion ... very slick.

> **Comments:**
> **username_2**: I am missing the SO link the one with :  the double map funtion ... , could you add it to your answer  
> **username_1**: @pippo1980ONSTRIKE I can't do it now, but I'll look at it early next week. Please check the answer below from acvill  
> **username_2**: cheers, but R is like ancient greek to me, I am following this question though  

---

### Answer by username_3 on 2023-07-14. Score: 3

M__'s approach is good, but, as explained, some subset of the unambiguous DNA sequences will not be recovered because of the ambiguity in reverse-translation due to the degeneracy of the genetic code. I don't know a python approach, but here's an R function that returns a vector of all the possible unambiguous DNA strings:
make_unambiguous <- function(dna) {
  require(tidyverse)
  require(S4Vectors)
  iupac <- tibble(code = c("A", "C", "G", "T",
                           "R", "Y", "S", "W",
                           "K", "M", "B", "D",
                           "H", "V", "N"),
                  base = c("A", "C", "G", "T",
                           "AG", "CT", "GC", "AT",
                           "GT", "AC", "CGT", "AGT",
                           "ACT", "ACG", "ACGT"))

  tibble(dna) |>
    separate_rows(dna, sep = '(?<=.)(?=.)') |>
    left_join(iupac, by = c("dna" = "code")) |>
    pull(base) |>
    str_split("") |>
    expand.grid(stringsAsFactors = FALSE) |>
    unite(col = sequence, sep = "") |>
    as_vector()
}

make_unambiguous(dna = "AAYGANAGYCARAGYAAR")


---

