<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Omar Wagih on Medium]]></title>
        <description><![CDATA[Stories by Omar Wagih on Medium]]></description>
        <link>https://medium.com/@omarwagih?source=rss-2b68123819a9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BQprz-NNgVIZmg5V.</url>
            <title>Stories by Omar Wagih on Medium</title>
            <link>https://medium.com/@omarwagih?source=rss-2b68123819a9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 07:38:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@omarwagih/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating publication-ready sequence logos in R]]></title>
            <link>https://medium.com/@omarwagih/creating-publication-ready-sequence-logos-in-r-785d747f550d?source=rss-2b68123819a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/785d747f550d</guid>
            <category><![CDATA[r]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[bioinformatics]]></category>
            <dc:creator><![CDATA[Omar Wagih]]></dc:creator>
            <pubDate>Sun, 19 Feb 2017 03:07:30 GMT</pubDate>
            <atom:updated>2017-02-19T10:08:55.350Z</atom:updated>
            <content:encoded><![CDATA[<p>Sequence logos are a great way of visualising sequence alignments, that have been <a href="https://www.ncbi.nlm.nih.gov/pubmed/2172928">around for decades</a>. The visualisation it it self is quite simple and similar in nature to bar charts, where the each bar or “stack” reflect how conserved that position in the alignment is. Each stack contains a set of stretched out letters that reflect the frequency of that nucleotide or amino acid in that position of the alignment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*O22aPsVBdJo99HEf9fjGtg.png" /><figcaption>An example of a sequence logo</figcaption></figure><p>Despite their popularity and there being <a href="https://en.wikipedia.org/wiki/Sequence_logo#Tools_for_creating_sequence_logos">numerous tools that generate sequence logos</a>, only a very few of them provide native sequence logos plotting in R. I personally found this quite frustrating given that the concept of the visualisation was quite simple. Most commonly used in R, is the <a href="https://www.bioconductor.org/packages/release/bioc/html/seqLogo.html">seqLogo package</a>, which comes with major limitations: it can only be used with DNA sequences, and lacks the flexibility of tweaking the plot. Another package is <a href="https://cran.r-project.org/web/packages/RWebLogo/index.html">RWebLogo</a>, a wrapper for the <a href="http://weblogo.berkeley.edu/">python WebLogo library</a> that I recently authored. However, it’s was quite buggy at times and is only a wrapper <em>i.e.</em> with no option for native plots.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*WpNl64w6IAww_eb-87mZrQ.png" /><figcaption>A sequence logo as generated by gglogo</figcaption></figure><p>With ggplot2 being one of the most versatile plotting libraries in R, it made sense that there was a package that made use of its capabilities to draw sequence logos. Integrated with ggplot2, this would allow a great amount of flexibility to users when it came to tweaking the visualisation. I came accross <a href="https://github.com/heike/gglogo">gglogo</a>, a package that attempts to do this by drawing letters as polygons. However I found the logos it generated just didn’t look that great (left).</p><p>I liked the the approach of using polygons - after all, it seemed like the only way to get letters to stretch vertically and horizontally in ggplot2. I adopted this <a href="https://github.com/omarwagih/ggseqlogo">into a new package of my own, called ggseqlogo</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pj3-j-ljo-Mes9zqsNJoAw.png" /></figure><p>The goal was to make something that was easy to use, highly versatile, while producing publication-ready logos. To do this, there were a few features I made sure to include:</p><ul><li>The ability to create DNA, RNA or amino acid logos, as well as the option to define define custom alphabets</li><li>Custom colour schemes</li><li>Easily allow for plots to be combined in a grid</li><li>Allow selection of different fonts</li></ul><p>On top of all this, users could tweak almost every aspect of the visualisation using ggplot2. Below is a short tutorial demoing ggseqlogo’s features. You can also find the package on github:</p><p><a href="https://github.com/omarwagih/ggseqlogo">GitHub - omarwagih/ggseqlogo: Beautiful and versatile sequence logos in R</a></p><p>I hope ggseqlogo can relieve some of the frustrations I’m sure many of you have when it comes to plotting sequence logos in R. If you have any ideas or suggestions , please send them my way!</p><p>To get started, first install the package:</p><pre>library(&quot;devtools&quot;)<br>install_github(&quot;omarwagih/ggseqlogo&quot;)</pre><p>Fire up and load some sample DNA sequence data. I’m using binding sites for transcription factors.</p><pre>require(ggplot2)<br>require(ggseqlogo)<br><br># Some example DNA sequences<br>f = system.file(&quot;extdata&quot;, &quot;sample_dna.rds&quot;, package = &quot;ggseqlogo&quot;)<br>seqs_list = readRDS(f)<br><br># Get first set of sequences<br>seqs_dna = seqs_list[[1]]<br><br># Plot a sequence logo<br>ggplot() + geom_logo(seqs_dna) + theme_logo()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V_Qw1JbJxdeEKpSWJutZBA.png" /><figcaption>A DNA sequence logo generated using ggseqlogo</figcaption></figure><p>Changing the colour scheme is also easy.</p><pre>ggplot() + geom_logo(seqs_dna, col_scheme=&#39;base_pairing&#39;) + theme_logo()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rfSYMDxMv5IGDcdfbEUNGA.png" /><figcaption>Applying the base_pairing colour scheme</figcaption></figure><p>If you’re not a fan of the preset schemes, you apply custom discrete or continuous colour schemes</p><pre># A discrete color scheme<br>cs1 = make_col_scheme(chars=c(&#39;A&#39;, &#39;T&#39;, &#39;C&#39;, &#39;G&#39;), <br>                      groups=c(&#39;gr1&#39;, &#39;gr1&#39;, &#39;gr2&#39;, &#39;gr2&#39;), <br>                      cols=c(&#39;purple&#39;, &#39;purple&#39;, &#39;blue&#39;, &#39;blue&#39;))</pre><pre># A continuous color scheme<br>cs2 = make_col_scheme(chars=c(&#39;A&#39;, &#39;T&#39;, &#39;C&#39;, &#39;G&#39;), values=1:4)</pre><pre># Plot<br>ggplot() + geom_logo(seqs_dna, col_scheme=cs1) + theme_logo()<br>ggplot() + geom_logo(seqs_dna, col_scheme=cs2) + theme_logo()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QRTrtdE8jCUIadk1ya5mGg.png" /><figcaption>Custom discrete colour schemes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I-pOP0naeo6Sq_yI9WV-Dg.png" /><figcaption>Custom continuous colour schemes</figcaption></figure><p>To combine more than one sequence logo you can use <a href="https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html">gridExtra’s grid.arrange</a> or the ggplot2 facet option. To do this all you need to do is pass a named list of sequences, and call facet_wrap.</p><pre>ggplot() + geom_logo(seqs_list) + theme_logo() + <br>  facet_wrap(~seq_group, ncol=1, scales=&#39;free_x&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JpLivb6RXWcwgjEdToO1tA.png" /><figcaption>Combining multiple sequence logos</figcaption></figure><p>You can change the font using the font parameter:</p><pre>ggplot() + geom_logo(seqs_dna, font=2) + theme_logo()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_4kFrFhxB-rYJ6SDQp5ZMg.png" /><figcaption>Sequence logos with different fonts</figcaption></figure><p>Finally, ggseqlogo also lets you use custom alphabets <em>i.e</em>. one that isn’t amino acids or nucleotides:</p><pre>seqs_other = c(&#39;10010&#39;, &#39;10100&#39;, &#39;01010&#39;, &#39;01001&#39;)<br>ggplot() + <br>    geom_logo(seqs_other, method=&#39;p&#39;, <br>              seq_type=&#39;other&#39;, namespace=0:1) + <br>    theme_logo()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NPe1jjgykXNilVX_Y9fJYw.png" /><figcaption>Sequence logos using custom namespaces</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=785d747f550d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>