Diff of /R/allclasses.R [000000] .. [413088]

Switch to unified view

a b/R/allclasses.R
1
#' @include zzz.R
2
#' @include allgenerics.R
3
#' @useDynLib VoltRon
4
NULL
5
6
## vrImage ####
7
8
# Set class union
9
suppressWarnings({
10
  setClassUnion("image_matrix", 
11
                members = c("matrix", 
12
                            "data.frame",
13
                            "dgRMatrix", 
14
                            "dgeMatrix", 
15
                            "Array", 
16
                            if (requireNamespace("BPCells", quietly = TRUE)) "IterableMatrix" else NULL))
17
})
18
19
#' The vrImage (VoltRon Image) Class
20
#'
21
#' @slot coords spatial coordinates of the assay
22
#' @slot segments spatial coordinates of the segments, if available
23
#' @slot image image of the spatial assay, bitmap class
24
#' @slot main_channel the key of the main channel of vrImage object
25
#'
26
#' @name vrImage-class
27
#' @rdname vrImage-class
28
#' @exportClass vrImage
29
#'
30
vrImage <- setClass(
31
  Class = 'vrImage',
32
  slots = c(
33
    coords = 'image_matrix',
34
    segments = 'list',
35
    image = "list",
36
    main_channel = "character"
37
  )
38
)
39
40
### show ####
41
42
setMethod(
43
  f = 'show',
44
  signature = 'vrImage',
45
  definition = function(object) {
46
    
47
    # separate names
48
    image_names <- names(object@image)
49
    image_id <- seq_along(image_names)
50
    image_names_split <- split(image_names, ceiling(image_id/10))
51
    
52
    cat("vrImage (VoltRon Image) Object \n")
53
    text <- "Channels:"
54
    for(img in image_names_split){
55
      cat(text, paste(img, collapse = ", "), "\n")
56
      text <- "         "
57
    }
58
    return(invisible(x = NULL))
59
  }
60
)
61
62
## vrSpatial ####
63
64
#' The vrSpatial (VoltRon Spatial) Class
65
#'
66
#' @slot coords spatial coordinates of the assay
67
#' @slot segments spatial coordinates of the segments, if available
68
#' @slot image image of the spatial assay, bitmap class
69
#' @slot main_channel the key of the main channel of vrImage object
70
#'
71
#' @name vrSpatial-class
72
#' @rdname vrSpatial-class
73
#' @exportClass vrSpatial
74
#'
75
vrSpatial <- setClass(
76
  Class = 'vrSpatial',
77
  slots = c(
78
    coords = 'image_matrix',
79
    segments = 'list',
80
    image = "list",
81
    main_channel = "character"
82
  )
83
)
84
85
### show ####
86
87
setMethod(
88
  f = 'show',
89
  signature = 'vrSpatial',
90
  definition = function(object) {
91
    
92
    # separate names
93
    image_names <- names(object@image)
94
    image_id <- seq_along(image_names)
95
    image_names_split <- split(image_names, ceiling(image_id/10))
96
    
97
    cat("vrSpatial (VoltRon Spatial) Object \n")
98
    text <- "Channels:"
99
    for(img in image_names_split){
100
      cat(text, paste(img, collapse = ", "), "\n")
101
      text <- "         "
102
    }
103
    return(invisible(x = NULL))
104
  }
105
)
106
107
## vrAssay ####
108
109
# Set class union
110
suppressWarnings({
111
  setClassUnion("data_matrix", 
112
                members = c("matrix", 
113
                            "dgCMatrix", "dgRMatrix", "dgeMatrix", 
114
                            "Array", 
115
                            if (requireNamespace("BPCells", quietly = TRUE)) "IterableMatrix" else NULL))
116
})
117
118
#' The vrAssay (VoltRon Assay) Class
119
#'
120
#' @slot rawdata raw data
121
#' @slot normdata normalized data
122
#' @slot featuredata feature metadata
123
#' @slot embeddings list of embeddings
124
#' @slot image a list of vrImage objects
125
#' @slot params additional parameters used by different assay types
126
#' @slot type the type of the assay (tile, molecule, cell, spot, ROI)
127
#' @slot name the assay name
128
#' @slot main_image the key of the main image
129
#'
130
#' @name vrAssay-class
131
#' @rdname vrAssay-class
132
#' @exportClass vrAssay
133
vrAssay <- setClass(
134
  Class = 'vrAssay',
135
  slots = c(
136
    rawdata = 'data_matrix',
137
    normdata = 'data_matrix',
138
    featuredata = 'data.frame',
139
    embeddings = "list",
140
    image = "list",
141
    params = "list",
142
    type = "character",
143
    name = "character",
144
    main_image = "character"
145
  )
146
)
147
148
### show ####
149
150
setMethod(
151
  f = 'show',
152
  signature = 'vrAssay',
153
  definition = function(object) {
154
    cat("vrAssay (VoltRon Assay) of", nrow(vrCoordinates(object)), "spatial points and", nrow(object@rawdata), "features. \n")
155
    return(invisible(x = NULL))
156
  }
157
)
158
159
## vrAssayV2 ####
160
161
#' The vrAssayV2 (VoltRon Assay) Class
162
#'
163
#' @slot data list of count/normalized datasets
164
#' @slot featuredata list of feature metadata
165
#' @slot embeddings list of embeddings
166
#' @slot image a list of vrImage objects
167
#' @slot params additional parameters used by different assay types
168
#' @slot type the type of the assay (tile, molecule, cell, spot, ROI)
169
#' @slot name the assay name
170
#' @slot main_image the key of the main image
171
#' @slot main_featureset the key of the main feature set
172
#'
173
#' @name vrAssayV2-class
174
#' @rdname vrAssayV2-class
175
#' @exportClass vrAssayV2
176
vrAssayV2 <- setClass(
177
  Class = 'vrAssayV2',
178
  slots = c(
179
    data = "list",
180
    featuredata = 'list',
181
    embeddings = "list",
182
    image = "list",
183
    params = "list",
184
    type = "character",
185
    name = "character",
186
    main_image = "character",
187
    main_featureset = "character"
188
  )
189
)
190
191
### show ####
192
193
setMethod(
194
  f = 'show',
195
  signature = 'vrAssayV2',
196
  definition = function(object) {
197
    
198
    # check if there is a data or rawdata slot in assay object
199
    cat(
200
      paste0("vrAssayV2 (VoltRon Assay V2) of ", 
201
             nrow(vrCoordinates(object)), " spatial points and ", 
202
             nrow(object@data[[vrMainFeatureType(object)]]), " features (", vrMainFeatureType(object), "). \n")
203
    )
204
    
205
    return(invisible(x = NULL))
206
  }
207
)
208
209
## vrLayer ####
210
211
# Set classes
212
setOldClass(Classes = c('igraph'))
213
214
#' The vrLayer (VoltRon Layer) Class
215
#'
216
#' @slot assay A list of assays (vrAssay)
217
#' @slot connectivity the connectivity graph
218
#'
219
#' @name vrLayer-class
220
#' @rdname vrLayer-class
221
#' @exportClass vrLayer
222
#' 
223
vrLayer <- setClass(
224
  Class = 'vrLayer',
225
  slots = c(
226
    assay = 'list',
227
    connectivity = 'igraph'
228
  )
229
)
230
231
### show ####
232
233
setMethod(
234
  f = 'show',
235
  signature = 'vrLayer',
236
  definition = function(object) {
237
    cat(class(x = object), "(VoltRon Layer) Object \n")
238
    layers <- names(unlist(object@assay))
239
    cat("Assay(s):", paste(layers, collapse = " "), "\n")
240
    return(invisible(x = NULL))
241
  }
242
)
243
244
## vrSample ####
245
246
#' The vrSample (VoltRon Sample) Class
247
#'
248
#' @slot layer A list of layers (vrLayer)
249
#' @slot zlocation a vector of z coordinates of layers
250
#' @slot adjacency an adjacency matrix of connected layers within a block
251
#'
252
#' @name vrSample-class
253
#' @rdname vrSample-class
254
#' @exportClass vrSample
255
#'
256
vrSample <- setClass(
257
  Class = 'vrSample',
258
  slots = c(
259
    layer = 'list',
260
    zlocation = 'numeric',
261
    adjacency = "matrix"
262
  )
263
)
264
265
### show ####
266
267
setMethod(
268
  f = 'show',
269
  signature = 'vrSample',
270
  definition = function(object) {
271
    cat(class(x = object), "(VoltRon Block) Object \n")
272
    layers <- names(unlist(object@layer))
273
    cat("Layer(s):", paste(layers, collapse = " "), "\n")
274
    return(invisible(x = NULL))
275
  }
276
)
277
278
## vrBlock ####
279
280
#' The vrBlock (VoltRon Block) Class
281
#'
282
#' @slot layer A list of layers (vrLayer)
283
#' @slot zlocation a vector of z coordinates of layers
284
#' @slot adjacency an adjacency matrix of connected layers within a block
285
#'
286
#' @name vrBlock-class
287
#' @rdname vrBlock-class
288
#' @exportClass vrBlock
289
#'
290
vrBlock <- setClass(
291
  Class = 'vrBlock',
292
  slots = c(
293
    layer = 'list',
294
    zlocation = 'numeric', 
295
    adjacency = "matrix"
296
  )
297
)
298
299
### show ####
300
301
setMethod(
302
  f = 'show',
303
  signature = 'vrBlock',
304
  definition = function(object) {
305
    cat(class(x = object), "(VoltRon Block) Object \n")
306
    layers <- names(unlist(object@layer))
307
    cat("Layer(s):", paste(layers, collapse = " "), "\n")
308
    return(invisible(x = NULL))
309
  }
310
)
311
312
## vrMetadata ####
313
314
suppressWarnings({
315
  setClassUnion("metadata_data",
316
                members = c("data.table", 
317
                            "data.frame",
318
                            if (requireNamespace("S4Vectors", quietly = TRUE)) "DataFrame" else NULL,
319
                            if (requireNamespace("HDF5DataFrame", quietly = TRUE)) "HDF5DataFrame" else NULL, 
320
                            if (requireNamespace("ZarrDataFrame", quietly = TRUE)) "ZarrDataFrame" else NULL))
321
})
322
323
#' The vrMetadata (VoltRon Metadata) Class
324
#'
325
#' @slot tile the metadata of tiles
326
#' @slot molecule the metadata of molecules
327
#' @slot cell the metadata of cells
328
#' @slot spot the metadata of spot
329
#' @slot ROI the metadata of ROI
330
#'
331
#' @name vrMetadata-class
332
#' @rdname vrMetadata-class
333
#' @exportClass vrMetadata
334
#'
335
vrMetadata <- setClass(
336
  Class = 'vrMetadata',
337
  slots = c(
338
    molecule = 'metadata_data',
339
    cell = 'metadata_data',
340
    spot = 'metadata_data',
341
    ROI = 'metadata_data',
342
    tile = 'metadata_data'
343
  )
344
)
345
346
### show ####
347
348
setMethod(
349
  f = 'show',
350
  signature = 'vrMetadata',
351
  definition = function(object) {
352
    cat("VoltRon Metadata Object \n")
353
    cat("This object includes: \n")
354
    lapply(methods::slotNames(object), function(x){
355
      if(nrow(slot(object, name = x))){
356
        cat("  ", nrow(slot(object, name = x)), paste0(x, "s"), "\n")
357
      }
358
    })
359
    return(invisible(x = NULL))
360
  }
361
)
362
363
## VoltRon ####
364
365
#' The VoltRon Class
366
#'
367
#' @slot samples A list of samples (vrSample)
368
#' @slot metadata A vrMetadata object that includes metadata of ROIs, spots, and cells
369
#' @slot sample.metadata Contains meta-information about each sample, layer and assay
370
#' @slot graph A igraph object
371
#' @slot main.assay The type of the main assay (i.e. Visium, Xenium, GeoMx etc.)
372
#' @slot project Name of the project
373
#'
374
#' @name VoltRon-class
375
#' @rdname VoltRon-class
376
#' @exportClass VoltRon
377
VoltRon <- setClass(
378
  Class = 'VoltRon',
379
  slots = c(
380
    samples = 'list',
381
    metadata = "vrMetadata",
382
    sample.metadata = "data.frame",
383
    graph = "list",
384
    main.assay = "character",
385
    project = 'character'
386
  )
387
)
388
389
### show ####
390
391
setMethod(
392
  f = 'show',
393
  signature = 'VoltRon',
394
  definition = function(object) {
395
    
396
    # print class
397
    cat(class(x = object), "Object \n")
398
    
399
    # sample metadata
400
    sample.metadata <- SampleMetadata(object)
401
    
402
    # get sample and layer names
403
    sample_names <- unique(sample.metadata$Sample)
404
    show_length <- min(5,length(sample_names))
405
    for(samp in sample_names[seq_len(show_length)]){
406
      cat(samp, ": \n", sep = "")
407
      layers <- unique(sample.metadata$Layer[sample.metadata$Sample == samp])
408
      layers <- split(layers, ceiling(seq_along(layers)/5))
409
      cat("  Layers:", paste(layers[[1]], collapse = " "), "\n")
410
      if(length(layers) > 1){
411
        for(i in 2:length(layers)){
412
          cat("         ", paste(layers[[i]], collapse = " "), "\n")
413
        } 
414
      }
415
    }
416
    
417
    # get assay names
418
    unique_assays <- unique(sample.metadata$Assay)
419
    
420
    # print
421
    if(length(sample_names) > 5){
422
      cat("...", "\n")
423
      cat("There are", length(sample_names), "samples in total", "\n")
424
    }
425
    
426
    # print assays
427
    main.assay <- vrMainAssay(object)
428
    unique_assays <- unique_assays[c(which(unique_assays == main.assay),which(unique_assays != main.assay))]
429
    unique_assays[1] <- paste0(unique_assays[1], "(Main)")
430
    cat("Assays:", paste(unique_assays, collapse = " "), "\n")
431
    
432
    # print features
433
    main.feat <- vrMainFeatureType(object)
434
    if(!is.null(main.feat)){
435
      main.feat <- unique(vrMainFeatureType(object)$Feature)
436
      unique_features <- vrFeatureTypeNames(object)
437
      if(length(main.feat) == 1){
438
        unique_features <- unique_features[c(which(unique_features == main.feat),which(unique_features != main.feat))]
439
        unique_features[1] <- paste0(unique_features[1], "(Main)") 
440
      }
441
      cat("Features:", paste(unique_features, collapse = " "), "\n") 
442
    }
443
    
444
    # return invisible
445
    return(invisible(x = NULL))
446
  }
447
)