ViennaRNA / forgi

An RNA manipulation library.
GNU General Public License v3.0
51 stars 30 forks source link

fvm.plot_rna(): nan values in annot_pos cause plotting errors #53

Open eugenechow opened 11 months ago

eugenechow commented 11 months ago

Hi, I encountered an issue when using the fvm.plot_rna() function.

The following code and RNA structure can trigger the issue consistently using forgi 2.2.2 on Ubuntu 22.04 / python 3.10 / matplotlib 3.8.0

import matplotlib.pyplot as plt
import forgi.visual.mplotlib as fvm
import forgi.graph.bulge_graph as fgb

seq = "GUACGAUCAAGAGAAGAGUCAUCAUGGACAUCGUCCUCGGGUUCUCCCUCGGGGGUGUCAUGGCCUCUUACUGGUGGUGGGGAUUCCACAUGGAUAAGAUUAACAAGAGAGAGAAGUUCUACGCAGAGCUAG"
mfe =  "....((((..(((..(((((......))).))...)))(((....)))....................................(((....)))...)))).............((((((....)))))).."
cg = fgb.BulgeGraph.from_fasta_text(f">test\n{seq}\n{mfe}")[0]
fvm.plot_rna(cg, text_kwargs={"fontweight":"black"}, lighten=0.7,
             backbone_kwargs={"linewidth":3})
plt.show()

Where python would produce the following error:

File "lib/python3.10/site-packages/matplotlib/bezier.py", line 351, in split_path_inout
    ctl_points, command = next(path_iter)
StopIteration

I tried debugging and found that the _find_annot_pos_on_circle() method returned [nan nan] for some of the base position annotations, which caused the plotting to fail.

A quick fix to omit such annotations can be done by changing line #329 in forgi/visual/mplotlib.py from:

if annot_pos is not None:

to:

if annot_pos is not None and not np.any(np.isnan(annot_pos)):