-
Notifications
You must be signed in to change notification settings - Fork 46
/
seurat_data.R
329 lines (322 loc) · 9.29 KB
/
seurat_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#' @include zzz.R
#'
NULL
#' Get list of available datasets
#'
#' @return A dataframe with available Seurat datasets. Rownames of the dataframe are the actual package names
#' \describe{
#' \item{Dataset}{Name of dataset, usable for other functions in SeuratData (eg. \code{\link{InstallData}})}
#' \item{Version}{Version of dataset, generally corresponds to version of Seurat dataset was built with}
#' \item{Installed}{Is the dataset installed?}
#' \item{InstalledVersion}{Version of dataset installed, \code{NA} if not installed}
#' }
#'
#' Other columns are extra metadata, and may change as new datasets are made available
#'
#' @export
#'
#' @seealso \code{\link{InstallData}} \code{\link{InstalledData}}
#' \code{\link{RemoveData}} \code{\link{UpdateData}}
#'
#' @examples
#' AvailableData()
#'
AvailableData <- function() {
UpdateManifest()
return(pkg.env$manifest)
}
#' Install a dataset
#'
#' @param ds List of datasets to install
#' @param force.reinstall Force reinstall already installed packages
#' @param ... Extra parameters passed to \code{\link[utils]{install.packages}}
#'
#' @return Invisible \code{NULL}
#'
#' @importFrom utils install.packages
#'
#' @export
#'
#' @seealso \code{\link{AvailableData}} \code{\link{InstalledData}}
#' \code{\link{RemoveData}} \code{\link{UpdateData}}
#'
#' @examples
#' \dontrun{
#' InstallData('pbmc3k')
#' }
#'
InstallData <- function(ds, force.reinstall = FALSE, ...) {
UpdateManifest()
if (pkg.env$source != 'remote') {
stop(
"No access to remote SeuratData repository, unable to install new datasets",
call. = FALSE
)
}
pkgs <- NameToPackage(ds = ds)
if (!force.reinstall) {
installed <- intersect(x = pkgs, y = rownames(x = InstalledData()))
if (length(x = installed) > 0) {
warning(
"The following packages are already installed and will not be reinstalled: ",
paste(
gsub(pattern = pkg.key, replacement = '', x = installed),
collapse = ', '
),
call. = FALSE,
immediate. = TRUE
)
pkgs <- setdiff(x = pkgs, y = installed)
if (!as.logical(x = length(x = pkgs))) {
UpdateManifest()
return(invisible(x = NULL))
}
}
}
pkgs2 <- paste0('package:', pkgs)
for (p in pkgs2[pkgs2 %in% search()]) {
detach(name = p, unload = TRUE, character.only = TRUE)
}
install.packages(
pkgs = pkgs,
repos = getOption(x = "SeuratData.repo.use"),
type = 'source',
...
)
for (pkg in pkgs) {
attachNamespace(ns = pkg)
pkg.env$attached <- c(pkg.env$attached, pkg)
}
UpdateManifest()
invisible(x = NULL)
}
#' Get a list of installed datasets
#'
#' @return A dataframe with installed Seurat datasets. Rownames of the dataframe are the actual package names
#' \describe{
#' \item{Dataset}{Name of dataset, usable for other functions in SeuratData (eg. \code{\link{InstallData}})}
#' \item{Version}{Version of dataset, generally corresponds to version of Seurat dataset was built with}
#' \item{Installed}{Is the dataset installed?}
#' \item{InstalledVersion}{Version of dataset installed}
#' }
#'
#' Other columns are extra metadata, and may change as new datasets are made available
#'
#' @export
#'
#' @seealso \code{\link{AvailableData}} \code{\link{InstallData}}
#' \code{\link{RemoveData}} \code{\link{UpdateData}}
#'
#' @examples
#' InstalledData()
#'
InstalledData <- function() {
dat <- AvailableData()
return(dat[which(x = dat$Installed, ), , drop = FALSE])
}
#' Modularly load a dataset
#'
# @inheritParams LoadH5Seurat
#' @param ds Optional name of dataset to load
#' @param type How to load the \code{Seurat} object; choose from either
#' 'default' for the default dataset or any dataset listed in the
#' \code{other.datasets} section of the data manifest
# \describe{
# \item{info}{Information about the object and what's stored in it}
# \item{raw}{The raw form of the dataset, no other options are evaluated}
# \item{azimuth}{Load the dataset as an azimuth reference}
# \item{processed}{The proccessed data, modular loading avaible by setting other parameters}
# }
#'
# @inherit LoadH5Seurat return
#' @return A \code{Seurat} object with the dataset asked for
#'
#' @importFrom utils data
#'
#' @export
#'
#' @seealso \code{\link[utils]{data}}
#'
LoadData <- function(
ds,
type = 'default'
# assays = NULL,
# reductions = NULL,
# graphs = NULL,
# verbose = TRUE
) {
installed <- InstalledData()
if (!NameToPackage(ds = ds) %in% rownames(x = installed)) {
stop("Cannot find dataset ", ds, call. = FALSE)
}
ds <- NameToPackage(ds = ds)
datasets <- c(
installed[ds, 'default.dataset', drop = TRUE],
trimws(x = unlist(x = strsplit(
x = installed[ds, 'other.datasets', drop = TRUE], split = ','
)))
)
type <- match.arg(
arg = tolower(x = type),
choices = c('raw', 'default', 'azimuth', datasets)
)
if (type == 'azimuth') {
return(LoadReference(file.path(system.file(package=ds), type)))
} else if (type %in% c('raw', 'default')) {
type <- gsub(pattern = pkg.key, replacement = '', x = ds)
} else if (type == 'final') {
type <- paste0(gsub(pattern = pkg.key, replacement = '', x = ds), '.final')
}
if (type %in% data(package = ds)$results[, 'Item', drop = TRUE]) {
e <- new.env()
data(list = type, package = ds, envir = e)
# ds <- gsub(pattern = '\\.SeuratData', replacement = '', x = ds)
# data(list = ds, envir = e)
return(e[[type]])
}
stop(
"Could not find dataset '",
type,
"', please check manifest and try again",
call. = FALSE
)
.NotYetImplemented()
# type <- match.arg(arg = type, choices = c('info', 'processed'))
# return(LoadH5Seurat(
# file = system.file(
# file.path('extdata', 'processed.h5Seurat'),
# package = ds,
# mustWork = TRUE
# ),
# type = ifelse(test = type == 'processed', yes = 'object', no = type),
# assays = assays,
# reductions = reductions,
# graphs = graphs,
# verbose = verbose
# ))
}
#' Remove a dataset
#'
#' @inheritParams utils::remove.packages
#' @param ds List of datasets to remove
#'
#' @importFrom utils remove.packages
#'
#' @export
#'
#' @seealso \code{\link{AvailableData}} \code{\link{InstallData}}
#' \code{\link{InstalledData}} \code{\link{UpdateData}}
#'
#' @examples
#' \dontrun{
#' RemoveData('pbmc3k')
#' }
#'
RemoveData <- function(ds, lib) {
UpdateManifest()
pkgs <- NameToPackage(ds = ds)
pkgs2 <- paste0('package:', pkgs)
for (p in pkgs2[pkgs2 %in% search()]) {
detach(name = p, unload = TRUE, character.only = TRUE)
}
remove.packages(pkgs = pkgs, lib = lib)
UpdateManifest()
}
#' Update datasets
#'
#' @inheritParams utils::update.packages
#'
#' @return Invisible \code{NULL}
#'
#' @importFrom utils update.packages
#'
#' @export
#'
#' @seealso \code{\link{AvailableData}} \code{\link{InstallData}}
#' \code{\link{InstalledData}} \code{\link{RemoveData}}
#'
#' @examples
#' \dontrun{
#' UpdateData(ask = FALSE)
#' }
#'
UpdateData <- function(ask = TRUE, lib.loc = NULL) {
UpdateManifest()
if (pkg.env$source != 'remote') {
stop(
"No access to remote SeuratData repository, unable to update datasets",
call. = FALSE
)
}
update.packages(lib.loc = lib.loc, repos = getOption(x = "SeuratData.repo.use"), ask = ask, type = 'source')
UpdateManifest()
invisible(x = NULL)
}
#' Load the reference RDS files
#'
#' Read in a reference \code{\link[Seurat]{Seurat}} object and annoy index. This
#' function can read from a file path. In order to read properly,
#' there must be the following files:
#' \itemize{
#' \item \dQuote{ref.Rds} for the downsampled reference \code{Seurat}
#' object (for mapping)
#' \item \dQuote{idx.annoy} for the nearest-neighbor index object
#' }
#'
#' @param path Pathto the two RDS files
#'
#' @return A list with two entries:
#' \describe{
#' \item{\code{map}}{
#' The downsampled reference \code{\link[Seurat]{Seurat}}
#' object (for mapping)
#' }
#' \item{\code{plot}}{The reference \code{Seurat} object (for plotting)}
#' }
#'
#' @importFrom SeuratObject Idents<- Tool Cells Misc AddMetaData CreateSeuratObject
#' @importFrom Seurat LoadAnnoyIndex
#' @importFrom utils download.file
#' @importFrom Matrix sparseMatrix
#'
LoadReference <- function(path) {
ref.names <- list(
map = 'ref.Rds',
ann = 'idx.annoy'
)
mapref <- file.path(path, ref.names$map)
annref <- file.path(path, ref.names$ann)
exists <- file.exists(c(mapref, annref))
if (!all(exists)) {
stop(
"Missing the following files from the directory provided: ",
unlist(x = ref.names)[!exists]
)
}
# Load the map reference
map <- readRDS(file = mapref)
# Load the annoy index into the Neighbor object in the neighbors slot
map[["refdr.annoy.neighbors"]] <- LoadAnnoyIndex(
object = map[["refdr.annoy.neighbors"]],
file = annref
)
# Create plotref
ad <- Tool(object = map, slot = "AzimuthReference")
# plotref.dr <- GetPlotRef(object = ad)
print(ad)
plotref.dr <- slot(object = ad, name = "plotref")
cm <- sparseMatrix(
i = 1, j = 1, x = 0, dims = c(1, nrow(x = plotref.dr)),
dimnames = list("placeholder", Cells(x = plotref.dr))
)
plot <- CreateSeuratObject(
counts = cm
)
plot[["refUMAP"]] <- plotref.dr
plot <- AddMetaData(object = plot, metadata = Misc(object = plotref.dr, slot = "plot.metadata"))
gc(verbose = FALSE)
return(list(
map = map,
plot = plot
))
}