Biopython: Native Motif logo plotter?

Created on 12 Jun 2016  路  10Comments  路  Source: biopython/biopython

Currently Biopython relies on Weblogo for plotting motif logos.
I have a implementation that can be tweaked to be more Biopython compatible here

There are few scaling issues besides too much redundant code, but it seems to work fine otherwise and was made as a proof of concept.

If there is sufficient interest, I would like to contribute it to Biopython.

P.S. If Github rendering fails, here is nbviewer link: http://nbviewer.jupyter.org/github/saketkc/motif-logos-matplotlib/blob/master/Motif%20Logos%20using%20matplotlib.ipynb

Most helpful comment

Thanks to @MarkusPiotrowski, I was able to scale it up. It now works with multiple fonts:
http://nbviewer.jupyter.org/github/saketkc/notebooks/blob/master/python/Sequence%20Logo%20Python%20%20--%20Any%20font.ipynb?flush=true

All 10 comments

I like the idea, in fact I was also playing around a little bit with matplotlib with the idea of implementing a seqlogo module in Biopython.
I think plotting your letters as graphics limits your approach to DNA/RNA; when you want to use a seqlogo for amino acid sequences you would have to define another 16 letters.
I investigated myself a little bit into this and found in a German Python forum that patheffects of matplotlib can be used to scale text. For this you have to define your own Scale class as follows:

import matplotlib.patheffects
import matplotlib.pyplot as plt

class Scale(matplotlib.patheffects._Base):
    def __init__(self, sx, sy=None):
        self._sx = sx
        self._sy = sy

    def draw_path(self, renderer, gc, tpath, affine, rgbFace):
        affine=affine.identity().scale(self._sx, self._sy)+affine
        renderer.draw_path(gc, tpath, affine, rgbFace)

So you can plot and scale 'normal' strings ('A', 'C', etc.) like this:

txt = plt.text(x,y, a_normal_letter, ....)
txt.set_path_effects([Scale(x_scale, y_scale)])

I also found the following colors to better match the weblogo colors:'forestgreen', 'mediumblue', and 'crimson'.

@MarkusPiotrowski I like your approach, it's way more scalable! I have updated my notebook reflecting your example. It's not complete yet, as evident from the missing gaps. But, I think this is the approach we should take going forward.

See the last example on the notebook

It would be nice if BioPython also would support to use an installed version of weblogo (as module)
instead of only supporting the weblogo webservice.

A while back I PEP8-ified the sources and fixed some Python3 issues:
https://github.com/WebLogo/weblogo

If support for the weblogo module is added, it probably will be useful for WebLogo to be able to use/support BioPython Motifs. This also would allow WebLogo to support all motif formats supported by BioPython. https://github.com/WebLogo/weblogo/issues/38

@ghuls You mean as long as the user has installed it (e.g. via pip install weblogo), then Biopython would be able to use it (via import weblogo)? That seems sensible - we have a lot of similar soft-dependencies likes networkx or reportlab.

@peterjc Yes that is what I mean.

I think the actual module name is weblogolib.

Using this module also allows a lot of control over the output (LogoOptions):
https://github.com/WebLogo/weblogo/blob/master/weblogolib/__init__.py

@MarkusPiotrowski How can I set the font size so that a letter has its height (before applying set_path_effects) equal to one unit on the vertical axis ?

Thanks to @MarkusPiotrowski, I was able to scale it up. It now works with multiple fonts:
http://nbviewer.jupyter.org/github/saketkc/notebooks/blob/master/python/Sequence%20Logo%20Python%20%20--%20Any%20font.ipynb?flush=true

That looks very nice 馃憤

@saketkc
Hi, how I can choose the color for different amino acids? I see in your example, you have chose the four different colors for DNA.
Thank you very much!

@beyondpie

The original version of my logo plotter is here: https://github.com/saketkc/pyseqlogo which has some examples using Amino acids.

Better still, I would recommend you use dmslogo developed by Dr. Bloom: https://jbloomlab.github.io/dmslogo/ part of which is based on my code but is more versatile. (If you do, don't forget to cite our paper)

I am closing this issue, since I have found my solution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ardsatcg picture ardsatcg  路  9Comments

benfulton picture benfulton  路  11Comments

peterjc picture peterjc  路  8Comments

peterjc picture peterjc  路  11Comments

satta picture satta  路  12Comments