Skip to content

Commit

Permalink
Merge pull request #7 from marcom/pkgexts
Browse files Browse the repository at this point in the history
Use package extensions when available instead of Requires.jl
  • Loading branch information
marcom authored May 10, 2023
2 parents 805ee3b + 67c0d34 commit 09f24c6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 42 deletions.
16 changes: 11 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ version = "0.2.1"

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

[compat]
BioStockholm = "0.1"
CairoMakie = "0.10"
ColorSchemes = "3.18"
Luxor = "3.3"
PrecompileTools = "1"
R2R_jll = "=1.0.6"
Requires = "1.3"
Scratch = "1.1"
PrecompileTools = "1"
UnicodePlots = "3"
ViennaRNA = "0.9,0.10"
julia = "1.6"

[extensions]
CairoMakieExt = "CairoMakie"
UnicodePlotsExt = "UnicodePlots"

[extras]
BioStockholm = "eeb925a3-6f9d-43e6-829e-e0ea03b76ecf"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Expand All @@ -37,4 +39,8 @@ UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
ViennaRNA = "05a721ad-238d-4945-8c85-8b5c1fff3465"

[targets]
test = ["BioStockholm", "Test", "UnicodePlots", "ViennaRNA"]
test = ["BioStockholm", "CairoMakie", "Test", "UnicodePlots", "ViennaRNA"]

[weakdeps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ plot_structure(dbn; sequence=seq,
```

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

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


```julia
using UnicodePlots, PlotRNA
PlotRNA.uniplot("(((...)))")
Expand Down
29 changes: 23 additions & 6 deletions src/plot_structure_makie.jl → ext/CairoMakieExt.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# Note: we have to use the dot before CairoMakie as we are using
# Requires.jl to only compile this code if CairoMakie is available
using .CairoMakie: Makie, Axis, Colorbar, ColorTypes, DataAspect, Figure,
hidedecorations!, hidespines!, lines!, scatter!, text!,
xlims!, ylims!
module CairoMakieExt

function plot_structure_makie(
using PrecompileTools: @compile_workload
using PlotRNA
using ViennaRNA: FoldCompound, Pairtable, basepairs, partfn, plot_coords, prob_of_basepairs
if isdefined(Base, :get_extension)
using CairoMakie: Makie, Axis, Colorbar, ColorTypes, DataAspect,
Figure, hidedecorations!, hidespines!, lines!,
scatter!, text!, xlims!, ylims!
else
using ..CairoMakie: Makie, Axis, Colorbar, ColorTypes, DataAspect,
Figure, hidedecorations!, hidespines!, lines!,
scatter!, text!, xlims!, ylims!
end

# Note: docstring is in src/PlotRNA.jl
function PlotRNA.plot_structure_makie(
structure::AbstractString;
sequence::AbstractString=" "^length(structure),
savepath::String = "",
Expand Down Expand Up @@ -77,3 +87,10 @@ function plot_structure_makie(
end
return f
end

@compile_workload begin
PlotRNA.plot_structure_makie("(((...)))")
PlotRNA.plot_structure_makie("(((...)))"; sequence="GGGAAACCC")
end

end # module
26 changes: 19 additions & 7 deletions src/uniplot.jl → ext/UnicodePlotsExt.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
module UnicodePlotsExt

# Secondary structure plotting to terminal with UnicodePlots

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

# Note: we have to use the dot before UnicodePlots as we are using
# Requires.jl to only compile this code if UnicodePlots is available
using PrecompileTools: @compile_workload
using PlotRNA
import ViennaRNA
import .UnicodePlots
if isdefined(Base, :get_extension)
import UnicodePlots
else
import ..UnicodePlots
end

# the docstring is in PlotRNA.jl so it's always available, even if
# this file wasn't loaded because of @require
function uniplot(dbn; title="", width=:auto, height=:auto)
# Note: docstring is in src/PlotRNA.jl
function PlotRNA.uniplot(dbn::AbstractString; title::AbstractString="",
width::Symbol=:auto, height::Symbol=:auto)
n = length(dbn)
n > 0 || throw(ArgumentError("secondary structure dbn has length 0"))
x, y = ViennaRNA.plot_coords(ViennaRNA.Pairtable(dbn))
plot = UnicodePlots.lineplot(x, y; border=:none, labels=false, title, width, height)
pt = Pairtable(dbn)
pt = ViennaRNA.Pairtable(dbn)
for i = 1:n
j = pt[i]
if i < j
Expand All @@ -27,3 +33,9 @@ function uniplot(dbn; title="", width=:auto, height=:auto)
end
return plot
end

@compile_workload begin
PlotRNA.uniplot("(((...)))")
end

end # module
38 changes: 22 additions & 16 deletions src/PlotRNA.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
module PlotRNA

using PrecompileTools: @compile_workload
using Requires: @require

export VARNA, R2R, plot_structure

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

include("plot_structure.jl")
Expand All @@ -17,23 +25,22 @@ include("r2r.jl")
@compile_workload begin
plot_structure("(((...)))")
plot_structure("(((...)))"; sequence="GGGAAACCC")
# TODO: how to precompile but using @require from Requires.jl?
# measure: are these precompile statements improving TTFP?
#uniplot("(((...)))")
#plot_structure_makie("(((...)))")
#plot_structure_makie("(((...)))"; sequence="GGGAAACCC")
end

# We define the docstrings of plot_structure_makie and uniplot here so they're always
# available, even if they weren't loaded via @require in __init__().

# Predefine docstrings of functions that are only available when other
# packages are also loaded, either via package extensions (julia >=
# v1.9) or Requires.jl (julia < v1.9).
#
# - plot_structure_makie (needs CairoMakie)
# - uniplot (needs UnicodePlots)

"""
plot_structure_makie(structure; [sequence, savepath, layout_type, colorscheme])
Plot a secondary structure to a PNG image or PDF file depending on `savepath` ending.
Using this function requires CairoMakie to have been loaded before
PlotRNA with `using CairoMakie, PlotRNA`.
Using this function requires CairoMakie to be loaded.
"""
function plot_structure_makie end

Expand All @@ -43,8 +50,7 @@ function plot_structure_makie end
Plot secondary structure using UnicodePlots, usually to show inside a
terminal.
Using this function requires UnicodePlots to have been loaded before
PlotRNA with `using UnicodePlots, PlotRNA`.
Using this function requires UnicodePlots to be loaded.
"""
function uniplot end

Expand Down

0 comments on commit 09f24c6

Please sign in to comment.