Skip to content

Commit 67c0d34

Browse files
committed
Use package extensions when available instead of Requires.jl
1 parent 805ee3b commit 67c0d34

File tree

5 files changed

+79
-42
lines changed

5 files changed

+79
-42
lines changed

Project.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@ version = "0.2.1"
55

66
[deps]
77
BioStockholm = "eeb925a3-6f9d-43e6-829e-e0ea03b76ecf"
8-
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
98
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
109
Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc"
10+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1111
R2R_jll = "1e27f72a-4876-5c10-8678-3341fdb8ba55"
1212
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1313
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
14-
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1514
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
16-
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
1715
ViennaRNA = "05a721ad-238d-4945-8c85-8b5c1fff3465"
1816

1917
[compat]
2018
BioStockholm = "0.1"
2119
CairoMakie = "0.10"
2220
ColorSchemes = "3.18"
2321
Luxor = "3.3"
22+
PrecompileTools = "1"
2423
R2R_jll = "=1.0.6"
2524
Requires = "1.3"
2625
Scratch = "1.1"
27-
PrecompileTools = "1"
2826
UnicodePlots = "3"
2927
ViennaRNA = "0.9,0.10"
3028
julia = "1.6"
3129

30+
[extensions]
31+
CairoMakieExt = "CairoMakie"
32+
UnicodePlotsExt = "UnicodePlots"
33+
3234
[extras]
3335
BioStockholm = "eeb925a3-6f9d-43e6-829e-e0ea03b76ecf"
3436
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
@@ -37,4 +39,8 @@ UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
3739
ViennaRNA = "05a721ad-238d-4945-8c85-8b5c1fff3465"
3840

3941
[targets]
40-
test = ["BioStockholm", "Test", "UnicodePlots", "ViennaRNA"]
42+
test = ["BioStockholm", "CairoMakie", "Test", "UnicodePlots", "ViennaRNA"]
43+
44+
[weakdeps]
45+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
46+
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ plot_structure(dbn; sequence=seq,
5959
```
6060

6161
There is also an experimental `PlotRNA.plot_structure_makie` which
62-
looks a bit nicer but currently has a rather large time to first plot
63-
(on julia-1.9: about 10 seconds for `using CairoMakie, PlotRNA` and 10
64-
seconds for `PlotRNA.plot_structure_makie("(((...)))")`). Subsequent
65-
plots are very fast though. To use this function, you must load
66-
CairoMakie before PlotRNA. Code using CairoMakie is only loaded
67-
optionally with Requires.jl, as otherwise PlotRNA's package load time
68-
is a lot higher.
62+
looks a bit nicer but has a larger time to first plot (on julia-1.9:
63+
about 9 seconds for `using CairoMakie, PlotRNA` and 7 seconds for
64+
`PlotRNA.plot_structure_makie("(((...)))")`). Subsequent plots are
65+
very fast though.
6966

7067
```julia
7168
using CairoMakie, PlotRNA
@@ -77,7 +74,6 @@ PlotRNA.plot_structure_makie("(((...)))")
7774
Plotting in the terminal is supported via UnicodePlots with
7875
coordinates generated by ViennaRNA.
7976

80-
8177
```julia
8278
using UnicodePlots, PlotRNA
8379
PlotRNA.uniplot("(((...)))")

src/plot_structure_makie.jl renamed to ext/CairoMakieExt.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
# Note: we have to use the dot before CairoMakie as we are using
2-
# Requires.jl to only compile this code if CairoMakie is available
3-
using .CairoMakie: Makie, Axis, Colorbar, ColorTypes, DataAspect, Figure,
4-
hidedecorations!, hidespines!, lines!, scatter!, text!,
5-
xlims!, ylims!
1+
module CairoMakieExt
62

7-
function plot_structure_makie(
3+
using PrecompileTools: @compile_workload
4+
using PlotRNA
5+
using ViennaRNA: FoldCompound, Pairtable, basepairs, partfn, plot_coords, prob_of_basepairs
6+
if isdefined(Base, :get_extension)
7+
using CairoMakie: Makie, Axis, Colorbar, ColorTypes, DataAspect,
8+
Figure, hidedecorations!, hidespines!, lines!,
9+
scatter!, text!, xlims!, ylims!
10+
else
11+
using ..CairoMakie: Makie, Axis, Colorbar, ColorTypes, DataAspect,
12+
Figure, hidedecorations!, hidespines!, lines!,
13+
scatter!, text!, xlims!, ylims!
14+
end
15+
16+
# Note: docstring is in src/PlotRNA.jl
17+
function PlotRNA.plot_structure_makie(
818
structure::AbstractString;
919
sequence::AbstractString=" "^length(structure),
1020
savepath::String = "",
@@ -77,3 +87,10 @@ function plot_structure_makie(
7787
end
7888
return f
7989
end
90+
91+
@compile_workload begin
92+
PlotRNA.plot_structure_makie("(((...)))")
93+
PlotRNA.plot_structure_makie("(((...)))"; sequence="GGGAAACCC")
94+
end
95+
96+
end # module
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
module UnicodePlotsExt
2+
13
# Secondary structure plotting to terminal with UnicodePlots
24

35
# TODO
46
# - smaller plots for smaller structures, e.g. (((...))), (((((....)))))
57
# - bad aspect ratio for some structure, e.g. "Gladio" from Eterna100
68
# "((....))..(...(...(..(.(..(...(((.(((...((((....)))).((((((.((((((.((((((.((((((.((((((((((.((((((.(((((.((((.((((..((((...)))).))).)))))).))))).)))))).)))))))).)))))).)))))).)))))).))))))).)))...(((.(((((.(..((((.(..((((.(..((((.(..(((((((.((((((.(((((.((((((.(((.((((((....))))))..)))).)))).))))).)))))).)))))))).)..)))).)..)))).)..)))).)..))))).((((....))))...))).)))...)..).)..)...)...)..((....))"
79

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+
using PrecompileTools: @compile_workload
11+
using PlotRNA
1012
import ViennaRNA
11-
import .UnicodePlots
13+
if isdefined(Base, :get_extension)
14+
import UnicodePlots
15+
else
16+
import ..UnicodePlots
17+
end
1218

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)
19+
# Note: docstring is in src/PlotRNA.jl
20+
function PlotRNA.uniplot(dbn::AbstractString; title::AbstractString="",
21+
width::Symbol=:auto, height::Symbol=:auto)
1622
n = length(dbn)
1723
n > 0 || throw(ArgumentError("secondary structure dbn has length 0"))
1824
x, y = ViennaRNA.plot_coords(ViennaRNA.Pairtable(dbn))
1925
plot = UnicodePlots.lineplot(x, y; border=:none, labels=false, title, width, height)
20-
pt = Pairtable(dbn)
26+
pt = ViennaRNA.Pairtable(dbn)
2127
for i = 1:n
2228
j = pt[i]
2329
if i < j
@@ -27,3 +33,9 @@ function uniplot(dbn; title="", width=:auto, height=:auto)
2733
end
2834
return plot
2935
end
36+
37+
@compile_workload begin
38+
PlotRNA.uniplot("(((...)))")
39+
end
40+
41+
end # module

src/PlotRNA.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
module PlotRNA
22

33
using PrecompileTools: @compile_workload
4-
using Requires: @require
5-
64
export VARNA, R2R, plot_structure
75

8-
function __init__()
9-
@require CairoMakie="13f3f980-e62b-5c42-98c6-ff1f3baf88f0" include("plot_structure_makie.jl")
10-
@require UnicodePlots="b8865327-cd53-5732-bb35-84acbb429228" include("uniplot.jl")
6+
# for julia < v1.9 we use Requires.jl instead of package extensions
7+
@static if !isdefined(Base, :get_extension)
8+
using Requires: @require
9+
end
10+
@static if !isdefined(Base, :get_extension)
11+
function __init__()
12+
@require CairoMakie="13f3f980-e62b-5c42-98c6-ff1f3baf88f0" include(
13+
normpath(@__DIR__, "..", "ext", "CairoMakieExt.jl")
14+
)
15+
@require UnicodePlots="b8865327-cd53-5732-bb35-84acbb429228" include(
16+
normpath(@__DIR__, "..", "ext", "UnicodePlotsExt.jl")
17+
)
18+
end
1119
end
1220

1321
include("plot_structure.jl")
@@ -17,23 +25,22 @@ include("r2r.jl")
1725
@compile_workload begin
1826
plot_structure("(((...)))")
1927
plot_structure("(((...)))"; sequence="GGGAAACCC")
20-
# TODO: how to precompile but using @require from Requires.jl?
21-
# measure: are these precompile statements improving TTFP?
22-
#uniplot("(((...)))")
23-
#plot_structure_makie("(((...)))")
24-
#plot_structure_makie("(((...)))"; sequence="GGGAAACCC")
2528
end
2629

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__().
30+
31+
# Predefine docstrings of functions that are only available when other
32+
# packages are also loaded, either via package extensions (julia >=
33+
# v1.9) or Requires.jl (julia < v1.9).
34+
#
35+
# - plot_structure_makie (needs CairoMakie)
36+
# - uniplot (needs UnicodePlots)
2937

3038
"""
3139
plot_structure_makie(structure; [sequence, savepath, layout_type, colorscheme])
3240
3341
Plot a secondary structure to a PNG image or PDF file depending on `savepath` ending.
3442
35-
Using this function requires CairoMakie to have been loaded before
36-
PlotRNA with `using CairoMakie, PlotRNA`.
43+
Using this function requires CairoMakie to be loaded.
3744
"""
3845
function plot_structure_makie end
3946

@@ -43,8 +50,7 @@ function plot_structure_makie end
4350
Plot secondary structure using UnicodePlots, usually to show inside a
4451
terminal.
4552
46-
Using this function requires UnicodePlots to have been loaded before
47-
PlotRNA with `using UnicodePlots, PlotRNA`.
53+
Using this function requires UnicodePlots to be loaded.
4854
"""
4955
function uniplot end
5056

0 commit comments

Comments
 (0)