<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>REBELSCIENCE</title><link>https://rebelscience.club/</link><description>Bioinformatics, Programming and Open-Source Science</description><item><title>DNA Toolkit Part 2: Transcription and Reverse Complement</title><link>https://rebelscience.club/2020/03/bioinformatics-in-python-dna-toolkit-part-2-transcription-and-reverse-complement/</link><guid isPermaLink="true">https://rebelscience.club/2020/03/bioinformatics-in-python-dna-toolkit-part-2-transcription-and-reverse-complement/</guid><pubDate>Sat, 28 Mar 2020 15:19:18 GMT</pubDate><description>Welcome back! Today we continue working on our DNA Toolkit project. In our last article, we created the first two functions: validate_seq and nucleotide_frequency. We
</description><content:encoded><![CDATA[
<p class="wp-block-paragraph">Welcome back! Today we continue working on our DNA Toolkit project. In our last article, we created the first two functions: <strong><em>validate_seq</em></strong> and <strong><em>nucleotide_frequency</em></strong>.
 We are not going to change the file structure, though we will add two 
more functions and one data structure. The functions we add today will 
finally replicate a real biological process. DNA into RNA <a href="https://www.khanacademy.org/science/biology/gene-expression-central-dogma/transcription-of-dna-into-rna/a/overview-of-transcription" target="_blank" rel="noreferrer noopener">Transcription</a> and <a href="https://en.m.wikipedia.org/wiki/Complementary_DNA" target="_blank" rel="noreferrer noopener">Complement</a> Generation (<a href="http://wifi.garden/splash-promotion/8513/158041333723769684703941" target="_blank" rel="noreferrer noopener">Reverse Complement</a> for computational purpose).</p>



<p class="wp-block-paragraph"><strong>DNA</strong>
 is an amazing way for nature to store instructions, it is biological 
code. It is efficient for computational use in two ways. Firstly, by 
having just a single strand of DNA we can generate another strand using a
 complement rule, and secondly, that data is very compressible. We will 
look into DNA data compression in our future articles/videos.</p>



<p class="wp-block-paragraph">In the two images below, we can see these three steps:</p>



<ul class="wp-block-list"><li>DNA Complement generation.</li><li>DNA -&gt; RNA Transcription.</li><li>RNA -&gt; Polypeptide -&gt; Protein Translation.</li></ul>



<p class="wp-block-paragraph">In this article, we will implement the first two steps, marked 1 and 2 in the second image.</p>



<figure class="wp-block-image"><img decoding="async" src="https://miro.medium.com/max/1367/1*UdRZW7ArY0oUOYPmOd5usg.jpeg" alt=""/></figure>



<hr class="wp-block-separator"/>



<figure class="wp-block-image"><img decoding="async" src="https://miro.medium.com/max/1921/1*AG2mTZmn8JCVMDqd6EHlsA.jpeg" alt=""/></figure>



<p class="wp-block-paragraph">We start by adding one new structure <strong><em>DNA_ReverseComplement</em></strong> to our <strong><em>structures.py</em></strong>  file as a Python dictionary. This dictionary will be used when we go  through a DNA string, nucleotide by nucleotide. The dictionary will then  return a complementary nucleotide. This approach is easy to understand  as we just used a dictionary and a <em>for</em> loop. There is a more <a rel="noreferrer noopener" href="https://docs.python-guide.org/writing/style/" target="_blank">Pythonic</a> way of generating a complement string, which is discussed later,</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">DNA_Nucleotides = ['A', 'C', 'G', 'T']
DNA_ReverseComplement = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}</pre></div>


<p class="wp-block-paragraph">Now, let’s add a function that will use this dictionary to give us a complementary DNA strand and reverse it.</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">def reverse_complement(seq):
    &quot;&quot;&quot;
    Swapping adenine with thymine and guanine with cytosine.
    Reversing newly generated string
    &quot;&quot;&quot;
    return ''.join([DNA_ReverseComplement[nuc] for nuc in seq])[::-1]</pre></div>


<p class="wp-block-paragraph">Here we use a <em>list comprehension</em> to loop through every character in the string, matching it with a <em>Key</em> in <em>DNA_ReverseComplement</em> <em>dictionary</em> to get a <em>Value</em> from that <em>dictionary</em>. When we have a new list of complementary nucleotides generated, we use <em>‘’.join</em> method to glue all the characters into a string and reverse it with <em>[::-1].</em></p>



<p class="wp-block-paragraph">So
 this approach is easy to read and understand. The code looks more 
structured as we used our predefined structures file. It is also 
translatable to other programming languages that way.</p>



<p class="wp-block-paragraph">Let’s try using Pythonic, <strong><em><a href="https://www.tutorialspoint.com/python/string_maketrans.htm">maketrans</a></em></strong> method to solve this problem without even using a dictionary.</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">def reverse_complement(seq):
    &quot;&quot;&quot;
    Swapping adenine with thymine and guanine with cytosine.
    Reversing newly generated string
    &quot;&quot;&quot;
    # Pythonic approach. A little bit faster solution.
    mapping = str.maketrans('ATCG', 'TAGC')
    return seq.translate(mapping)[::-1]</pre></div>


<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Python string method <strong>maketrans()</strong>
 returns a translation table that maps each character in the intabstring
 into the character at the same position in the outtab string. Then this
 table is passed to the translate() function.<br><strong>Note</strong> − Both intab and outtab must have the same length.</p></blockquote>



<p class="wp-block-paragraph">You can learn more about this method here: <a href="https://www.tutorialspoint.com/python/string_maketrans.htm" target="_blank" rel="noreferrer noopener">Python String maketrans() Method</a></p>



<p class="wp-block-paragraph">Next, we define the Transcription function. It is a very simple, one-liner in Python:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">def transcription(seq):
    &quot;&quot;&quot;
    DNA -&gt; RNA Transcription.
    Replacing Thymine with Uracil
    &quot;&quot;&quot;
    return seq.replace(&quot;T&quot;, &quot;U&quot;)</pre></div>


<p class="wp-block-paragraph">This  code is self-explanatory, as we are using standard Python language  functionality so far. Here, we just find every occurrence of Thymine and replace it with Uracil. We also return a new RNA string, without effecting the original DNA string, passed through seq.</p>



<p class="wp-block-paragraph">You might have noticed we started using an interesting code commenting approach. We wrap our comments into:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">&quot;&quot;&quot; comment &quot;&quot;&quot;
instead of
# comment</pre></div>


<p class="wp-block-paragraph">This is called a <a href="https://www.geeksforgeeks.org/python-docstrings/" target="_blank" rel="noreferrer noopener">Dockstring</a>.  It is super useful when you have to write a complex algorithm, or you  have many parameters your function accepts. By adding a  description in a form of a <a href="https://www.geeksforgeeks.org/python-docstrings/" target="_blank" rel="noreferrer noopener">Dockstring</a>,  the code editor will show you that information when you call that  function. For a demonstration of how this works, check out this <a href="https://youtu.be/h1aP9HCFu6Y?t=54" target="_blank" rel="noreferrer noopener">Video</a>.</p>



<p class="wp-block-paragraph">This is it. We are done implementing our two new functions. Let’s test them by adding the output from both to our <em><strong>main.py</strong></em> file. We will use <a rel="noreferrer noopener" href="https://realpython.com/python-f-strings/" target="_blank">f-strings</a> again to nicely format the output.</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">print(f'[3] + DNA/RNA Transcription: {transcription(DNAStr)}\n')

print(f&quot;[4] + DNA String + Complement + Reverse Complement:\n5' {DNAStr} 3'&quot;)
print(f&quot;   {''.join(['|' for c in range(len(DNAStr))])}&quot;)
print(f&quot;3' {reverse_complement(DNAStr)[::-1]} 5' [Complement]&quot;)
print(f&quot;5' {reverse_complement(DNAStr)} 3' [Rev. Complement]\n&quot;)</pre></div>


<p class="wp-block-paragraph">So here is the output for all 4 functions we implemented so far:</p>


<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">Sequence: CCGTTGGATGGGCTAGTTCGTCCTTCCTACACCAGTGCAGGTGCGGTTTA

[1] + Sequence Length: 50

[2] + Nucleotide Frequency: {'C': 13, 'G': 15, 'T': 15, 'A': 7}

[3] + DNA/RNA Transcription: CCGUUGGAUGGGCUAGUUCGUCCUUCCUACACCAGUGCAGGUGCGGUUUA

[4] + DNA String + Complement + Reverse Complement:
5' CCGTTGGATGGGCTAGTTCGTCCTTCCTACACCAGTGCAGGTGCGGTTTA 3'
   ||||||||||||||||||||||||||||||||||||||||||||||||||
3' GGCAACCTACCCGATCAAGCAGGAAGGATGTGGTCACGTCCACGCCAAAT 5' [Complement]
5' TAAACCGCACCTGCACTGGTGTAGGAAGGACGAACTAGCCCATCCAACGG 3' [Rev. Complement]</pre></div>


<p class="wp-block-paragraph">As a bonus, I have added coloring to our code, into a new file: <strong><em>utilites.py</em></strong>.  This is not Bioinformatics related, but if you want to practice your  Python, and add a function like that, you can view a video version of <a rel="noreferrer noopener" href="https://youtu.be/h1aP9HCFu6Y?t=628" target="_blank"><strong>DNA Toolkit. Part 2</strong></a> to see how this is done. We will add many more helper functions to <strong><em>utilites.py</em> </strong>in the future. Functions for reading/writing files, reading/writing databases, etc.</p>



<figure class="wp-block-image size-large"><a href="https://rebelscience.club/wp-content/uploads/2020/04/image.png"><img decoding="async" width="812" height="290" src="https://rebelscience.club/wp-content/uploads/2020/04/image.png" alt="" class="wp-image-275" srcset="https://rebelscience.club/wp-content/uploads/2020/04/image.png 812w, https://rebelscience.club/wp-content/uploads/2020/04/image-256x91.png 256w, https://rebelscience.club/wp-content/uploads/2020/04/image-512x183.png 512w, https://rebelscience.club/wp-content/uploads/2020/04/image-768x274.png 768w" sizes="(max-width: 812px) 100vw, 812px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<p class="wp-block-paragraph">Here is a GitHub Link for this article: <a href="https://github.com/rebelC0der/DNA_Toolkit" target="_blank" rel="noreferrer noopener">Link</a></p>



<p class="wp-block-paragraph">A video version of this article can be viewed here:</p>



<figure class="wp-block-embed aligncenter is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Bioinformatics in Python: DNA Toolkit. Part 2: Transcription, Reverse Complement" width="640" height="360" src="https://www.youtube.com/embed/h1aP9HCFu6Y?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">This is it for now. See you in the next article.</p>
]]></content:encoded></item></channel></rss>