/
LoadH5Seurat.R
370 lines (363 loc) · 9.39 KB
/
LoadH5Seurat.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#' @include zzz.R
#' @include h5Seurat.R
#' @include GetObject.R
#' @include AssembleObject.R
#'
NULL
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Generics
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Load a saved \code{Seurat} object from an h5Seurat file
#'
#' @param file,x Name of h5Seurat or connected h5Seurat file to load
#' @param assays One of:
#' \itemize{
#' \item A character vector with names of assays
#' \item A character vector with one or more of \code{counts}, \code{data},
#' \code{scale.data} describing which slots of \strong{all assays} to load
#' \item A named list where each entry is either the name of an assay or a vector
#' describing which slots (described above) to take from which assay
#' \item \code{NULL} for all assays
#' }
#' @param reductions One of:
#' \itemize{
#' \item A character vector with names of reductions
#' \item \code{NULL} for all reductions
#' \item \code{NA} for \link[Seurat:IsGlobal]{global} reductions
#' \item \code{FALSE} for no reductions
#' }
#' \strong{Note}: Only reductions associated with an assay loaded in
#' \code{assays} or marked as \link[Seurat:IsGlobal]{global} will be loaded
#' @param graphs One of:
#' \itemize{
#' \item A character vector with names of graphs
#' \item \code{NULL} for all graphs
#' \item \code{FALSE} for no graphs
#' }
#' \strong{Note}: Only graphs associated with an assay loaded in \code{assays}
#' will be loaded
#' @param neighbors One of:
#' \itemize{
#' \item A character vector with the names of neighbors
#' \item \code{NULL} for all neighbors
#' \item \code{FALSE} for no neighbors
#' }
#' @param images One of:
#' \itemize{
#' \item A character vector with names of images
#' \item \code{NULL} for all images
#' \item \code{NA} for \link[Seurat:IsGlobal]{global} images
#' \item \code{FALSE} for no images
#' }
#' @param meta.data Load object metadata
#' @param commands Load command information \cr
#' \strong{Note}: only commands associated with an assay loaded in
#' \code{assays} will be loaded
#' @param misc Load miscellaneous data
#' @param tools Load tool-specific information
#' @param verbose Show progress updates
#' @param ... Arguments passed to other methods
#'
#' @return A \code{Seurat} object with the data requested
#'
#' @export
#'
LoadH5Seurat <- function(file, ...) {
UseMethod(generic = 'LoadH5Seurat', object = file)
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Methods
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' @rdname LoadH5Seurat
#' @method LoadH5Seurat character
#' @export
#'
LoadH5Seurat.character <- function(
file,
assays = NULL,
reductions = NULL,
graphs = NULL,
neighbors = NULL,
images = NULL,
meta.data = TRUE,
commands = TRUE,
misc = is.null(x = assays),
tools = is.null(x = assays),
verbose = TRUE,
...
) {
hfile <- h5Seurat$new(filename = file, mode = 'r')
on.exit(expr = hfile$close_all())
return(LoadH5Seurat(
file = hfile,
assays = assays,
reductions = reductions,
graphs = graphs,
neighbors = neighbors,
images = images,
meta.data = meta.data,
commands = commands,
misc = misc,
tools = tools,
verbose = verbose,
...
))
}
#' @rdname LoadH5Seurat
#' @method LoadH5Seurat H5File
#' @export
#'
LoadH5Seurat.H5File <- function(
file,
assays = NULL,
reductions = NULL,
graphs = NULL,
neighbors = NULL,
images = NULL,
meta.data = TRUE,
commands = TRUE,
misc = is.null(x = assays),
tools = is.null(x = assays),
verbose = TRUE,
...
) {
return(LoadH5Seurat(
file = as.h5Seurat(x = file),
assays = assays,
reductions = reductions,
graphs = graphs,
neighbors = neighbors,
images = images,
meta.data = meta.data,
commands = commands,
misc = misc,
tools = tools,
verbose = verbose,
...
))
}
#' @importFrom Seurat as.Seurat
#'
#' @rdname LoadH5Seurat
#' @method LoadH5Seurat h5Seurat
#' @export
#'
LoadH5Seurat.h5Seurat <- function(
file,
assays = NULL,
reductions = NULL,
graphs = NULL,
neighbors = NULL,
images = NULL,
meta.data = TRUE,
commands = TRUE,
misc = is.null(x = assays),
tools = is.null(x = assays),
verbose = TRUE,
...
) {
return(as.Seurat(
x = file,
assays = assays,
reductions = reductions,
graphs = graphs,
neighbors = neighbors,
images = images,
meta.data = meta.data,
commands = commands,
misc = misc,
tools = tools,
verbose = verbose,
...
))
}
#' @importClassesFrom Seurat Seurat
#' @importFrom methods slot<-
#' @importFrom Seurat as.Seurat DefaultAssay Cells
#' Idents<- Idents Project<- Project
#' AddMetaData
#'
#' @aliases as.Seurat
#'
#' @rdname LoadH5Seurat
#' @method as.Seurat h5Seurat
#' @export
#'
as.Seurat.h5Seurat <- function(
x,
assays = NULL,
reductions = NULL,
graphs = NULL,
neighbors = NULL,
images = NULL,
meta.data = TRUE,
commands = TRUE,
misc = TRUE,
tools = TRUE,
verbose = TRUE,
...
) {
index <- x$index()
obj.all <- all(vapply(
X = c(assays, reductions, graphs),
FUN = is.null,
FUN.VALUE = logical(length = 1L)
))
# Load Assays
assays <- GetAssays(assays = assays, index = index)
if (!DefaultAssay(object = index) %in% names(x = assays)) {
active.assay <- names(x = assays)[1]
warning(
"Default assay not requested, using ",
active.assay,
" instead",
call. = FALSE,
immediate. = TRUE
)
} else {
active.assay <- DefaultAssay(object = index)
}
assay.objects <- vector(mode = 'list', length = length(x = assays))
names(x = assay.objects) <- names(x = assays)
for (assay in names(x = assays)) {
assay.objects[[assay]] <- AssembleAssay(
assay = assay,
file = x,
slots = assays[[assay]],
verbose = verbose
)
}
default.assay <- list(assay.objects[[active.assay]])
names(x = default.assay) <- active.assay
object <- new(
Class = 'Seurat',
assays = default.assay,
active.assay = active.assay,
meta.data = data.frame(row.names = Cells(x = x)),
version = package_version(x = x$version())
)
for (assay in names(x = assay.objects)) {
if (assay != active.assay) {
object[[assay]] <- assay.objects[[assay]]
}
}
# Load DimReducs
reductions <- GetDimReducs(
reductions = reductions,
index = index,
assays = assays
)
for (reduc in reductions) {
if (verbose) {
message("Adding reduction ", reduc)
}
reduction <- AssembleDimReduc(
reduction = reduc,
file = x,
verbose = verbose
)
if (isTRUE(x = getOption(x = 'SeuratDisk.dimreducs.allglobal', default = FALSE))) {
slot(object = reduction, name = 'global') <- TRUE
}
object[[reduc]] <- reduction
}
# Load Graphs
graphs <- GetGraphs(graphs = graphs, index = index, assays = assays)
for (graph in graphs) {
if (verbose) {
message("Adding graph ", graph)
}
object[[graph]] <- AssembleGraph(graph = graph, file = x, verbose = verbose)
}
# Load Neighbors
neighbors <- GetNeighbors(neighbors = neighbors, index = index)
for (neighbor in neighbors) {
if (verbose) {
message("Adding neighbors ", neighbor)
}
object[[neighbor]] <- AssembleNeighbor(
neighbor = neighbor,
file = x,
verbose = verbose
)
}
# Load SpatialImages
if (packageVersion(pkg = 'Seurat') >= numeric_version(x = spatial.version)) {
images <- GetImages(images = images, index = index, assays = assays)
for (image in images) {
if (verbose) {
message("Adding image ", image)
}
object[[image]] <- AssembleImage(
image = image,
file = x,
verbose = verbose
)
}
}
# Load SeuratCommands
if (commands) {
if (verbose) {
message("Adding command information")
}
cmds <- GetCommands(index = index, assays = assays)
cmdlogs <- vector(mode = 'list', length = length(x = cmds))
names(x = cmdlogs) <- cmds
for (cmd in cmds) {
cmdlogs[[cmd]] <- AssembleSeuratCommand(
cmd = cmd,
file = x,
verbose = verbose
)
}
slot(object = object, name = 'commands') <- cmdlogs
}
# Load meta.data
if (meta.data) {
if (verbose) {
message("Adding cell-level metadata")
}
md <- as.data.frame(x = x[['meta.data']], row.names = Cells(x = x))
if (ncol(x = md)) {
object <- AddMetaData(object = object, metadata = md)
}
}
# Set cell identities and object project
Idents(object = object) <- Idents(object = x)
Project(object = object) <- Project(object = x)
# Load misc
if (misc) {
if (verbose) {
message("Adding miscellaneous information")
}
slot(object = object, name = 'misc') <- as.list(x = x[['misc']])
}
# Load tools
if (tools) {
if (verbose) {
message("Adding tool-specific results")
}
slot(object = object, name = 'tools') <- as.list(x = x[['tools']])
}
# Load no.assay information
if (obj.all && !is.null(x = index$no.assay)) {
if (verbose) {
message("Adding data that was not associated with an assay")
}
for (graph in index$no.assay$graphs) {
object[[graph]] <- AssembleGraph(
graph = graph,
file = x,
verbose = verbose
)
}
for (cmd in index$no.assay$commands) {
object[[cmd]] <- AssembleSeuratCommand(
cmd = cmd,
file = x,
verbose = verbose
)
}
}
return(object)
}