Skip to content

Commit c5c95e6

Browse files
committed
Add uniplot for plotting to terminal with UnicodePlots
1 parent bf9851b commit c5c95e6

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1313
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
1414
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
1515
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
16+
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
1617
ViennaRNA = "05a721ad-238d-4945-8c85-8b5c1fff3465"
1718

1819
[compat]

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ using CairoMakie, PlotRNA
6161
PlotRNA.plot_structure_makie("(((...)))")
6262
```
6363

64+
### Terminal plotting with UnicodePlots
65+
66+
Plotting in the terminal is supported via UnicodePlots.
67+
68+
```julia
69+
using UnicodePlots, PlotRNA
70+
PlotRNA.uniplot("(((...)))")
71+
```
72+
6473

6574
### Plot structures with VARNA
6675

src/PlotRNA.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export VARNA, R2R, plot_structure
77

88
function __init__()
99
@require CairoMakie="13f3f980-e62b-5c42-98c6-ff1f3baf88f0" include("plot_structure_makie.jl")
10+
@require UnicodePlots="b8865327-cd53-5732-bb35-84acbb429228" include("uniplot.jl")
1011
end
1112

1213
include("plot_structure.jl")
@@ -18,13 +19,14 @@ include("r2r.jl")
1819
plot_structure("(((...)))"; sequence="GGGAAACCC")
1920
# TODO: how to precompile but using @require from Requires.jl?
2021
# measure: are these precompile statements improving TTFP?
22+
#uniplot("(((...)))")
2123
#plot_structure_makie("(((...)))")
2224
#plot_structure_makie("(((...)))"; sequence="GGGAAACCC")
2325
end
2426

25-
# We define the docstring of plot_structure_makie here so it's always
26-
# available, even if the plot_structure_makie implementation wasn't
27-
# included because CairoMakie was not loaded.
27+
# We define the docstrings of plot_structure_makie and uniplot here so they're always
28+
# available, even if they weren't loaded via @require in __init__().
29+
2830
"""
2931
plot_structure_makie(structure; [sequence, savepath, layout_type, colorscheme])
3032
@@ -35,4 +37,16 @@ PlotRNA with `using CairoMakie, PlotRNA`.
3537
"""
3638
function plot_structure_makie end
3739

40+
"""
41+
uniplot(dbn; title, width, height)
42+
43+
Plot secondary structure using UnicodePlots, usually to show inside a
44+
terminal.
45+
46+
Using this function requires UnicodePlots to have been loaded before
47+
PlotRNA with `using UnicodePlots, PlotRNA`.
48+
"""
49+
function uniplot end
50+
51+
3852
end # module PlotRNA

src/uniplot.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Secondary structure plotting to terminal with UnicodePlots
2+
3+
# TODO
4+
# - smaller plots for smaller structures, e.g. (((...))), (((((....)))))
5+
# - bad aspect ratio for some structure, e.g. "Gladio" from Eterna100
6+
# "((....))..(...(...(..(.(..(...(((.(((...((((....)))).((((((.((((((.((((((.((((((.((((((((((.((((((.(((((.((((.((((..((((...)))).))).)))))).))))).)))))).)))))))).)))))).)))))).)))))).))))))).)))...(((.(((((.(..((((.(..((((.(..((((.(..(((((((.((((((.(((((.((((((.(((.((((((....))))))..)))).)))).))))).)))))).)))))))).)..)))).)..)))).)..)))).)..))))).((((....))))...))).)))...)..).)..)...)...)..((....))"
7+
8+
# Note: we have to use the dot before UnicodePlots as we are using
9+
# Requires.jl to only compile this code if UnicodePlots is available
10+
import ViennaRNA
11+
import .UnicodePlots
12+
13+
# the docstring is in PlotRNA.jl so it's always available, even if
14+
# this file wasn't loaded because of @require
15+
function uniplot(dbn; title="", width=:auto, height=:auto)
16+
n = length(dbn)
17+
n > 0 || throw(ArgumentError("secondary structure dbn has length 0"))
18+
x, y = ViennaRNA.plot_coords(ViennaRNA.Pairtable(dbn))
19+
plot = UnicodePlots.lineplot(x, y; border=:none, labels=false, title, width, height)
20+
pt = Pairtable(dbn)
21+
for i = 1:n
22+
j = pt[i]
23+
if i < j
24+
# i,j form a base pair
25+
UnicodePlots.lines!(plot, x[i], y[i], x[j], y[j])
26+
end
27+
end
28+
return plot
29+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ showtestset() = println(" "^(2 * Test.get_testset_depth()), "testing ",
99
include("plot_structure.jl")
1010
include("varna.jl")
1111
include("r2r.jl")
12+
include("uniplot.jl")
1213
include("plot_structure_makie.jl")
1314
end

test/uniplot.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using Test
2+
import UnicodePlots
3+
4+
@testset "uniplot" begin
5+
showtestset()
6+
@test PlotRNA.uniplot("(((...)))") isa UnicodePlots.Plot
7+
end

0 commit comments

Comments
 (0)