Skip to content

Commit fa6d7e3

Browse files
authored
Improve overriding energy parameter dirs via env vars (#5)
Improve overriding energy parameter dirs via env vars Always ensure the DATAPATH, CYCLEFOLD_DATAPATH env vars are pointing to the correct path from the `RNAstructure_jll` binary package. These paths can be overridden with the RNASTRUCTURE_JL_DATAPATH, RNASTRUCTURE_JLCYCLEFOLD_DATAPATH env vars if necessary. Don't warn if the DATAPATH, CYCLEFOLD_DATAPATH env vars are already set to the correct directories. This avoids unnecessary warnings when running the tests. Fixes #4.
1 parent 8a6c2aa commit fa6d7e3

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ for more details.
3939
Some programs make exceptions to these rules, check the manual pages
4040
of the RNAstructure programs for details on any differences.
4141

42+
### Note: Overriding energy parameter directories
43+
44+
The environment variables `RNASTRUCTURE_JL_DATAPATH` can be set to
45+
override the directory where energy parameters are read from. For the
46+
`cyclefold_*` functions the environment variable is called
47+
`RNASTRUCTURE_JL_CYCLEFOLD_DATAPATH`.
48+
49+
In the original RNAstructure program these environment variables are
50+
called `DATAPATH` and `CYCLEFOLD_DATAPATH`. `RNAstructure.jl` (this
51+
package) sets these environment variables automatically to the
52+
corresponding installation directory of the `RNAstructure_jll` binary
53+
package. The names of the env vars were changed to avoid clashes with
54+
possible settings you might already have in your shell startup files
55+
from a pre-existing manual RNAstructure installation, which could be a
56+
different version and have different parameters. In this way, you can
57+
be sure that this package uses the correct parameters, while still
58+
allowing to override them if necessary.
4259

4360
### Minimum free energy (MFE) and structure
4461

src/RNAstructure.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ include("pairtable-to-dbn.jl")
1818
include("plot.jl")
1919

2020
function __init__()
21-
if !haskey(ENV, "DATAPATH")
22-
ENV["DATAPATH"] = joinpath(RNAstructure_jll.artifact_dir, "data_tables")
23-
else
24-
@info "RNAstructure: energy params already set, DATAPATH=$(ENV["DATAPATH"])"
25-
end
26-
if !haskey(ENV, "CYCLEFOLD_DATAPATH")
27-
ENV["CYCLEFOLD_DATAPATH"] = joinpath(RNAstructure_jll.artifact_dir, "CycleFold", "datafiles")
28-
else
29-
@info "RNAstructure: CycleFold energy params already set, CYCLEFOLD_DATAPATH=$(ENV["CYCLEFOLD_DATAPATH"])"
21+
datapath = joinpath(RNAstructure_jll.artifact_dir, "data_tables")
22+
cyclefold_datapath = joinpath(RNAstructure_jll.artifact_dir, "CycleFold", "datafiles")
23+
24+
# set env vars DATAPATH, CYCLEFOLD_DATAPATH needed for RNAstructure_jll
25+
# these can be overridden with RNASTRUCTURE_JL_DATAPATH, RNASTRUCTURE_JL_CYCLEFOLD_DATAPATH
26+
for (env_varname, default_path) in [("DATAPATH", datapath), ("CYCLEFOLD_DATAPATH", cyclefold_datapath)]
27+
if haskey(ENV, "RNASTRUCTURE_JL_$(env_varname)")
28+
@info "Setting ENV[\"$(env_varname)\"] = ENV[\"RNASTRUCTURE_JL_$(env_varname)\"]"
29+
ENV[env_varname] = ENV["RNASTRUCTURE_JL_$(env_varname)"]
30+
else
31+
if haskey(ENV, env_varname) && ENV[env_varname] != default_path
32+
# only warn if env var is set to non-default path
33+
@warn ("RNAstructure: $(env_varname) env var set, replacing with $default_path\n"
34+
* "To override $(env_varname) used by RNAstructure, set the RNASTRUCTURE_JL_$(env_varname) env var")
35+
end
36+
ENV[env_varname] = default_path
37+
end
3038
end
39+
3140
# TODO: set OMP_NUM_THREADS env var for smp programs (number of threads to use)
3241
return nothing
3342
end

test/init-env-vars.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@testset "__init__ env vars" begin
2+
showtestset()
3+
4+
upstream_env_vars = [
5+
"DATAPATH",
6+
"CYCLEFOLD_DATAPATH",
7+
]
8+
our_env_vars = ["RNASTRUCTURE_JL_" * e for e in upstream_env_vars]
9+
env_var_mapping = Dict(e => "RNASTRUCTURE_JL_" * e for e in upstream_env_vars)
10+
all_env_vars = [upstream_env_vars..., our_env_vars...]
11+
12+
function delete_all_env_vars()
13+
for e in all_env_vars
14+
delete!(ENV, e);
15+
end
16+
end
17+
18+
# save relevant ENV vars so that we can change them here for
19+
# testing and then restore them later
20+
saved_env_vars = Dict{String,String}()
21+
for e in all_env_vars
22+
if haskey(ENV, e)
23+
saved_env_vars[e] = ENV[e]
24+
delete!(ENV, e)
25+
end
26+
end
27+
28+
@test RNAstructure.__init__() == nothing
29+
delete_all_env_vars()
30+
31+
# setting DATAPATH, etc
32+
for e in upstream_env_vars
33+
ENV[e] = "non-existent-dir-path"
34+
warn_msg = ("RNAstructure: $e env var set, replacing with $(saved_env_vars[e])\n"
35+
* "To override $e used by RNAstructure, set the RNASTRUCTURE_JL_$e env var")
36+
@test (@test_logs (:warn, (warn_msg)) RNAstructure.__init__()) == nothing
37+
delete_all_env_vars()
38+
end
39+
40+
# setting RNASTRUCTURE_JL_*
41+
for (upstream_env, our_env) in env_var_mapping
42+
ENV[our_env] = "non-existent-dir-path"
43+
info_msg = ("Setting ENV[\"$upstream_env\"] = ENV[\"$our_env\"]")
44+
@test (@test_logs (:info, (info_msg)) RNAstructure.__init__()) == nothing
45+
delete_all_env_vars()
46+
end
47+
48+
# setting both upstream_env_vars and our_env_vars, our_env_vars
49+
# should have precedence
50+
for (upstream_env, our_env) in env_var_mapping
51+
ENV[upstream_env] = "non-existent-dir-path"
52+
ENV[our_env] = "non-existent-dir-path"
53+
info_msg = ("Setting ENV[\"$upstream_env\"] = ENV[\"$our_env\"]")
54+
@test (@test_logs (:info, (info_msg)) RNAstructure.__init__()) == nothing
55+
delete_all_env_vars()
56+
end
57+
58+
# restore env vars that we previously unset
59+
for e in all_env_vars
60+
if haskey(saved_env_vars, e)
61+
ENV[e] = saved_env_vars[e]
62+
end
63+
end
64+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ showtestset() = println(" "^(2 * Test.get_testset_depth()), "testing ",
1414
@testset verbose=true "RNAstructure" begin
1515
showtestset()
1616
include("aqua.jl")
17+
include("init-env-vars.jl")
1718
include("ct-format.jl")
1819
include("plot.jl")
1920
include("RNAstructure.jl")

0 commit comments

Comments
 (0)