[413088]: / R / allclasses.R

Download this file

447 lines (393 with data), 11.5 kB

  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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#' @include zzz.R
#' @include allgenerics.R
#' @useDynLib VoltRon
NULL
## vrImage ####
# Set class union
suppressWarnings({
setClassUnion("image_matrix",
members = c("matrix",
"data.frame",
"dgRMatrix",
"dgeMatrix",
"Array",
if (requireNamespace("BPCells", quietly = TRUE)) "IterableMatrix" else NULL))
})
#' The vrImage (VoltRon Image) Class
#'
#' @slot coords spatial coordinates of the assay
#' @slot segments spatial coordinates of the segments, if available
#' @slot image image of the spatial assay, bitmap class
#' @slot main_channel the key of the main channel of vrImage object
#'
#' @name vrImage-class
#' @rdname vrImage-class
#' @exportClass vrImage
#'
vrImage <- setClass(
Class = 'vrImage',
slots = c(
coords = 'image_matrix',
segments = 'list',
image = "list",
main_channel = "character"
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrImage',
definition = function(object) {
# separate names
image_names <- names(object@image)
image_id <- seq_along(image_names)
image_names_split <- split(image_names, ceiling(image_id/10))
cat("vrImage (VoltRon Image) Object \n")
text <- "Channels:"
for(img in image_names_split){
cat(text, paste(img, collapse = ", "), "\n")
text <- " "
}
return(invisible(x = NULL))
}
)
## vrSpatial ####
#' The vrSpatial (VoltRon Spatial) Class
#'
#' @slot coords spatial coordinates of the assay
#' @slot segments spatial coordinates of the segments, if available
#' @slot image image of the spatial assay, bitmap class
#' @slot main_channel the key of the main channel of vrImage object
#'
#' @name vrSpatial-class
#' @rdname vrSpatial-class
#' @exportClass vrSpatial
#'
vrSpatial <- setClass(
Class = 'vrSpatial',
slots = c(
coords = 'image_matrix',
segments = 'list',
image = "list",
main_channel = "character"
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrSpatial',
definition = function(object) {
# separate names
image_names <- names(object@image)
image_id <- seq_along(image_names)
image_names_split <- split(image_names, ceiling(image_id/10))
cat("vrSpatial (VoltRon Spatial) Object \n")
text <- "Channels:"
for(img in image_names_split){
cat(text, paste(img, collapse = ", "), "\n")
text <- " "
}
return(invisible(x = NULL))
}
)
## vrAssay ####
# Set class union
suppressWarnings({
setClassUnion("data_matrix",
members = c("matrix",
"dgCMatrix", "dgRMatrix", "dgeMatrix",
"Array",
if (requireNamespace("BPCells", quietly = TRUE)) "IterableMatrix" else NULL))
})
#' The vrAssay (VoltRon Assay) Class
#'
#' @slot rawdata raw data
#' @slot normdata normalized data
#' @slot featuredata feature metadata
#' @slot embeddings list of embeddings
#' @slot image a list of vrImage objects
#' @slot params additional parameters used by different assay types
#' @slot type the type of the assay (tile, molecule, cell, spot, ROI)
#' @slot name the assay name
#' @slot main_image the key of the main image
#'
#' @name vrAssay-class
#' @rdname vrAssay-class
#' @exportClass vrAssay
vrAssay <- setClass(
Class = 'vrAssay',
slots = c(
rawdata = 'data_matrix',
normdata = 'data_matrix',
featuredata = 'data.frame',
embeddings = "list",
image = "list",
params = "list",
type = "character",
name = "character",
main_image = "character"
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrAssay',
definition = function(object) {
cat("vrAssay (VoltRon Assay) of", nrow(vrCoordinates(object)), "spatial points and", nrow(object@rawdata), "features. \n")
return(invisible(x = NULL))
}
)
## vrAssayV2 ####
#' The vrAssayV2 (VoltRon Assay) Class
#'
#' @slot data list of count/normalized datasets
#' @slot featuredata list of feature metadata
#' @slot embeddings list of embeddings
#' @slot image a list of vrImage objects
#' @slot params additional parameters used by different assay types
#' @slot type the type of the assay (tile, molecule, cell, spot, ROI)
#' @slot name the assay name
#' @slot main_image the key of the main image
#' @slot main_featureset the key of the main feature set
#'
#' @name vrAssayV2-class
#' @rdname vrAssayV2-class
#' @exportClass vrAssayV2
vrAssayV2 <- setClass(
Class = 'vrAssayV2',
slots = c(
data = "list",
featuredata = 'list',
embeddings = "list",
image = "list",
params = "list",
type = "character",
name = "character",
main_image = "character",
main_featureset = "character"
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrAssayV2',
definition = function(object) {
# check if there is a data or rawdata slot in assay object
cat(
paste0("vrAssayV2 (VoltRon Assay V2) of ",
nrow(vrCoordinates(object)), " spatial points and ",
nrow(object@data[[vrMainFeatureType(object)]]), " features (", vrMainFeatureType(object), "). \n")
)
return(invisible(x = NULL))
}
)
## vrLayer ####
# Set classes
setOldClass(Classes = c('igraph'))
#' The vrLayer (VoltRon Layer) Class
#'
#' @slot assay A list of assays (vrAssay)
#' @slot connectivity the connectivity graph
#'
#' @name vrLayer-class
#' @rdname vrLayer-class
#' @exportClass vrLayer
#'
vrLayer <- setClass(
Class = 'vrLayer',
slots = c(
assay = 'list',
connectivity = 'igraph'
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrLayer',
definition = function(object) {
cat(class(x = object), "(VoltRon Layer) Object \n")
layers <- names(unlist(object@assay))
cat("Assay(s):", paste(layers, collapse = " "), "\n")
return(invisible(x = NULL))
}
)
## vrSample ####
#' The vrSample (VoltRon Sample) Class
#'
#' @slot layer A list of layers (vrLayer)
#' @slot zlocation a vector of z coordinates of layers
#' @slot adjacency an adjacency matrix of connected layers within a block
#'
#' @name vrSample-class
#' @rdname vrSample-class
#' @exportClass vrSample
#'
vrSample <- setClass(
Class = 'vrSample',
slots = c(
layer = 'list',
zlocation = 'numeric',
adjacency = "matrix"
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrSample',
definition = function(object) {
cat(class(x = object), "(VoltRon Block) Object \n")
layers <- names(unlist(object@layer))
cat("Layer(s):", paste(layers, collapse = " "), "\n")
return(invisible(x = NULL))
}
)
## vrBlock ####
#' The vrBlock (VoltRon Block) Class
#'
#' @slot layer A list of layers (vrLayer)
#' @slot zlocation a vector of z coordinates of layers
#' @slot adjacency an adjacency matrix of connected layers within a block
#'
#' @name vrBlock-class
#' @rdname vrBlock-class
#' @exportClass vrBlock
#'
vrBlock <- setClass(
Class = 'vrBlock',
slots = c(
layer = 'list',
zlocation = 'numeric',
adjacency = "matrix"
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrBlock',
definition = function(object) {
cat(class(x = object), "(VoltRon Block) Object \n")
layers <- names(unlist(object@layer))
cat("Layer(s):", paste(layers, collapse = " "), "\n")
return(invisible(x = NULL))
}
)
## vrMetadata ####
suppressWarnings({
setClassUnion("metadata_data",
members = c("data.table",
"data.frame",
if (requireNamespace("S4Vectors", quietly = TRUE)) "DataFrame" else NULL,
if (requireNamespace("HDF5DataFrame", quietly = TRUE)) "HDF5DataFrame" else NULL,
if (requireNamespace("ZarrDataFrame", quietly = TRUE)) "ZarrDataFrame" else NULL))
})
#' The vrMetadata (VoltRon Metadata) Class
#'
#' @slot tile the metadata of tiles
#' @slot molecule the metadata of molecules
#' @slot cell the metadata of cells
#' @slot spot the metadata of spot
#' @slot ROI the metadata of ROI
#'
#' @name vrMetadata-class
#' @rdname vrMetadata-class
#' @exportClass vrMetadata
#'
vrMetadata <- setClass(
Class = 'vrMetadata',
slots = c(
molecule = 'metadata_data',
cell = 'metadata_data',
spot = 'metadata_data',
ROI = 'metadata_data',
tile = 'metadata_data'
)
)
### show ####
setMethod(
f = 'show',
signature = 'vrMetadata',
definition = function(object) {
cat("VoltRon Metadata Object \n")
cat("This object includes: \n")
lapply(methods::slotNames(object), function(x){
if(nrow(slot(object, name = x))){
cat(" ", nrow(slot(object, name = x)), paste0(x, "s"), "\n")
}
})
return(invisible(x = NULL))
}
)
## VoltRon ####
#' The VoltRon Class
#'
#' @slot samples A list of samples (vrSample)
#' @slot metadata A vrMetadata object that includes metadata of ROIs, spots, and cells
#' @slot sample.metadata Contains meta-information about each sample, layer and assay
#' @slot graph A igraph object
#' @slot main.assay The type of the main assay (i.e. Visium, Xenium, GeoMx etc.)
#' @slot project Name of the project
#'
#' @name VoltRon-class
#' @rdname VoltRon-class
#' @exportClass VoltRon
VoltRon <- setClass(
Class = 'VoltRon',
slots = c(
samples = 'list',
metadata = "vrMetadata",
sample.metadata = "data.frame",
graph = "list",
main.assay = "character",
project = 'character'
)
)
### show ####
setMethod(
f = 'show',
signature = 'VoltRon',
definition = function(object) {
# print class
cat(class(x = object), "Object \n")
# sample metadata
sample.metadata <- SampleMetadata(object)
# get sample and layer names
sample_names <- unique(sample.metadata$Sample)
show_length <- min(5,length(sample_names))
for(samp in sample_names[seq_len(show_length)]){
cat(samp, ": \n", sep = "")
layers <- unique(sample.metadata$Layer[sample.metadata$Sample == samp])
layers <- split(layers, ceiling(seq_along(layers)/5))
cat(" Layers:", paste(layers[[1]], collapse = " "), "\n")
if(length(layers) > 1){
for(i in 2:length(layers)){
cat(" ", paste(layers[[i]], collapse = " "), "\n")
}
}
}
# get assay names
unique_assays <- unique(sample.metadata$Assay)
# print
if(length(sample_names) > 5){
cat("...", "\n")
cat("There are", length(sample_names), "samples in total", "\n")
}
# print assays
main.assay <- vrMainAssay(object)
unique_assays <- unique_assays[c(which(unique_assays == main.assay),which(unique_assays != main.assay))]
unique_assays[1] <- paste0(unique_assays[1], "(Main)")
cat("Assays:", paste(unique_assays, collapse = " "), "\n")
# print features
main.feat <- vrMainFeatureType(object)
if(!is.null(main.feat)){
main.feat <- unique(vrMainFeatureType(object)$Feature)
unique_features <- vrFeatureTypeNames(object)
if(length(main.feat) == 1){
unique_features <- unique_features[c(which(unique_features == main.feat),which(unique_features != main.feat))]
unique_features[1] <- paste0(unique_features[1], "(Main)")
}
cat("Features:", paste(unique_features, collapse = " "), "\n")
}
# return invisible
return(invisible(x = NULL))
}
)