diff --git a/Project.toml b/Project.toml index 8d7384b..3608fea 100644 --- a/Project.toml +++ b/Project.toml @@ -5,15 +5,13 @@ 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] @@ -21,14 +19,18 @@ 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" @@ -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" diff --git a/README.md b/README.md index c9e7d0a..c88dd99 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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("(((...)))") diff --git a/src/plot_structure_makie.jl b/ext/CairoMakieExt.jl similarity index 70% rename from src/plot_structure_makie.jl rename to ext/CairoMakieExt.jl index f7fae96..ecf5db4 100644 --- a/src/plot_structure_makie.jl +++ b/ext/CairoMakieExt.jl @@ -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 = "", @@ -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 diff --git a/src/uniplot.jl b/ext/UnicodePlotsExt.jl similarity index 69% rename from src/uniplot.jl rename to ext/UnicodePlotsExt.jl index c507408..915f883 100644 --- a/src/uniplot.jl +++ b/ext/UnicodePlotsExt.jl @@ -1,3 +1,5 @@ +module UnicodePlotsExt + # Secondary structure plotting to terminal with UnicodePlots # TODO @@ -5,19 +7,23 @@ # - 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 @@ -27,3 +33,9 @@ function uniplot(dbn; title="", width=:auto, height=:auto) end return plot end + +@compile_workload begin + PlotRNA.uniplot("(((...)))") +end + +end # module diff --git a/src/PlotRNA.jl b/src/PlotRNA.jl index 001220b..9320dd1 100644 --- a/src/PlotRNA.jl +++ b/src/PlotRNA.jl @@ -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") @@ -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 @@ -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