saketkc/pyseqlogo

Failure to plot logos when using pdf renderer

Closed this issue · 1 comments

  • pyseqlogo version: commit bda975f
  • Python version: Python 3.7.9
  • Operating System: MX Linux 17 (Debian-based distribution)

Description

I wanted to try draw_logo from within an automated pipeline, where, for some reason, I had to set a pdf renderer for matplotlib, and draw_logo crashed on line 127 of pyseqlogo.py:

    126             window_ext = txt.get_window_extent(
--> 127                 txt._renderer)  #(fig.canvas.renderer) #txt._renderer)

What I Did

Here is a minimum example to reproduce the crash:

>>> import matplotlib as mpl
>>> mpl.use("PDF")
>>> from pyseqlogo.pyseqlogo import draw_logo
>>> data = {"A": [5, 5, 5, 5], "C": [5, 5, 5, 5], "G": [5, 5, 5, 5], "T": [5, 5, 5, 5]}
>>> fig, axes = draw_logo(data, data_type="counts")
RuntimeError                              Traceback (most recent call last)
<ipython-input-5-5a59f0cf5a0b> in <module>
----> 1 fig, axes = draw_logo(data, data_type="counts")

/usr/local/lib/python3.7/site-packages/pyseqlogo/pyseqlogo.py in draw_logo(data, data_type, seq_type, yaxis, colorscheme, nrow, ncol, padding, draw_range, coordinate_type, draw_axis, fontfamily, debug, ax)
    364         else:
    365             xshifts_list = _draw_text_data_coord(
--> 366                 ic, ax, fontfamily, colorscheme, draw_axis=draw_axis)
    367     else:
    368         if yaxis == 'probability':

/usr/local/lib/python3.7/site-packages/pyseqlogo/pyseqlogo.py in _draw_text_data_coord(height_matrix, ax, fontfamily, colorscheme, scalex, draw_axis, debug)
    125             fig.canvas.draw()
    126             window_ext = txt.get_window_extent(
--> 127                 txt._renderer)  #(fig.canvas.renderer) #txt._renderer)
    128             if basescore > 0.3:
    129                 yshift = window_ext.height * basescore  #- fontsize/10# fontsize/4#/1.20 #*.85 #* fig.dpi/72.0

/usr/local/lib/python3.7/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi)
    895             self._renderer = self.figure._cachedRenderer
    896         if self._renderer is None:
--> 897             raise RuntimeError('Cannot get window extent w/o renderer')
    898 
    899         with cbook._setattr_cm(self.figure, dpi=dpi):

RuntimeError: Cannot get window extent w/o renderer

I haven't tested this with pdf backend. If you need a pdf, I would suggest giving dmslogo a shot. The corresponding citation is here. It is based on pyseqlogo, but has more features.