Debian Codegraph @

R/bioconductor.R

raw
1
2 renv_bioconductor_manager <- function() {
3 if (getRversion() >= "3.5.0")
4 "BiocManager"
5 else
6 "BiocInstaller"
7 }
8
9 renv_bioconductor_versions <- function() {
10
11 # map versions of Bioconductor to the versions of R they can be used with
12 list(
13 "3.9" = "3.6",
14 "3.10" = "3.6",
15 "3.11" = "4.0",
16 "3.12" = "4.0",
17 "3.13" = "4.1",
18 "3.14" = "4.1",
19 "3.15" = "4.2",
20 "3.16" = "4.2",
21 "3.17" = "4.3",
22 "3.18" = "4.3",
23 "3.19" = "4.4",
24 "3.20" = "4.4",
25 "3.21" = "4.5", # speculative
26 "3.22" = "4.5" # speculative
27 )
28
29 }
30
31 renv_bioconductor_validate <- function(version, prompt = interactive()) {
32
33 # check for the requested Bioconductor version in our internal version map;
34 # if it doesn't exist, then just assume compatibility
35 #
36 # we previously used BiocManager for this, but because it makes web requests,
37 # this can be prohibitively slow for certain users
38 #
39 # https://github.com/rstudio/renv/issues/2091
40 biocversions <- renv_bioconductor_versions()
41 rversion <- biocversions[[version]]
42 if (is.null(rversion))
43 return(TRUE)
44
45 # check that the version of R in use matches what Bioconductor requires
46 ok <- renv_version_eq(rversion, getRversion(), n = 2L)
47 if (ok)
48 return(TRUE)
49
50 fmt <- lines(
51 "You are using Bioconductor %1$s, which is not compatible with R %2$s.",
52 "Use 'renv::init(bioconductor = TRUE)' to re-initialize this project with the appropriate Bioconductor release.",
53 if (renv_package_installed("BiocVersion"))
54 "Please uninstall the 'BiocVersion' package first, with `remove.packages(\"BiocVersion\")`."
55 )
56
57 caution(fmt, version, getRversion())
58
59 if (prompt) {
60 writef("")
61 response <- ask("Would you still like to use this version of Bioconductor?")
62 cancel_if(!response)
63 }
64
65 TRUE
66
67 }
68
69 renv_bioconductor_init <- function(library = NULL) {
70 renv_scope_options(renv.verbose = FALSE)
71
72 if (identical(renv_bioconductor_manager(), "BiocManager"))
73 renv_bioconductor_init_biocmanager(library)
74 else
75 renv_bioconductor_init_biocinstaller(library)
76 }
77
78 renv_bioconductor_init_biocmanager <- function(library = NULL) {
79
80 library <- library %||% renv_libpaths_active()
81 if (renv_package_installed("BiocManager", lib.loc = library))
82 return(TRUE)
83
84 ensure_directory(library)
85 install("BiocManager", library = library, prompt = FALSE)
86
87 TRUE
88
89 }
90
91 renv_bioconductor_init_biocinstaller <- function(library = NULL) {
92
93 library <- library %||% renv_libpaths_active()
94 if (renv_package_installed("BiocInstaller", lib.loc = library))
95 return(TRUE)
96
97 url <- "https://bioconductor.org/biocLite.R"
98 destfile <- renv_scope_tempfile("renv-bioclite-", fileext = ".R")
99 download(url, destfile = destfile, quiet = TRUE)
100
101 ensure_directory(library)
102 renv_scope_libpaths(library)
103 source(destfile)
104 TRUE
105
106 }
107
108 renv_bioconductor_version <- function(project, refresh = FALSE) {
109
110 # check and see if we have an override via option
111 version <- getOption("renv.bioconductor.version")
112 if (!is.null(version))
113 return(version)
114
115 # check and see if the project has been configured to use a specific
116 # Bioconductor release
117 if (!refresh) {
118 version <- settings$bioconductor.version(project = project)
119 if (length(version))
120 return(version)
121 }
122
123 # if BiocVersion is installed, use it
124 if (renv_package_available("BiocVersion"))
125 return(format(packageVersion("BiocVersion")[1, 1:2]))
126
127 # make sure the required bioc package is available
128 renv_bioconductor_init()
129
130 # otherwise, infer the Bioconductor version from installed packages
131 case(
132
133 renv_package_available("BiocManager") ~ {
134 BiocManager <- renv_scope_biocmanager()
135 format(BiocManager$version())
136 },
137
138 renv_package_available("BiocVersion") ~ {
139 BiocInstaller <- renv_namespace_load("BiocInstaller")
140 format(BiocInstaller$biocVersion())
141 }
142
143 )
144
145 }
146
147 # Returns the union of the inferred Bioconductor repositories, together with the
148 # current value of the 'repos' R option. The Bioconductor repositories are
149 # placed first in the repository list.
150 renv_bioconductor_repos <- function(project = NULL, version = NULL) {
151
152 # allow bioconductor repos override
153 repos <- getOption("renv.bioconductor.repos")
154 if (!is.null(repos))
155 return(repos)
156
157 # make sure the required bioc package is available
158 renv_bioconductor_init()
159
160 # read Bioconductor version (normally set during restore)
161 version <- version %||% renv_bioconductor_version(project = project)
162
163 # get current repositories
164 repos <- getOption("repos")
165
166 # read Bioconductor repositories (prefer BiocInstaller for older R)
167 biocrepos <- if (identical(renv_bioconductor_manager(), "BiocManager"))
168 renv_bioconductor_repos_biocmanager(version)
169 else
170 renv_bioconductor_repos_biocinstaller(version)
171
172 # overlay new repos on old repos (this helps preserve ordering)
173 # https://github.com/rstudio/renv/issues/2128
174 repos[names(biocrepos)] <- biocrepos
175 repos
176
177 }
178
179 renv_bioconductor_repos_biocmanager <- function(version) {
180
181 BiocManager <- renv_scope_biocmanager()
182 version <- version %||% BiocManager$version()
183
184 tryCatch(
185 BiocManager$.repositories(site_repository = character(), version = version),
186 error = function(e) {
187 BiocManager$repositories(version = version)
188 }
189 )
190
191 }
192
193 renv_bioconductor_repos_biocinstaller <- function(version) {
194 BiocInstaller <- asNamespace("BiocInstaller")
195 version <- version %||% BiocInstaller$biocVersion()
196 BiocInstaller$biocinstallRepos(version = version)
197 }
198
199 renv_bioconductor_required <- function(records) {
200
201 for (record in records)
202 if (identical(record$Source, "Bioconductor"))
203 return(TRUE)
204
205 FALSE
206
207 }