[413088]: / R / sample.R

Download this file

550 lines (462 with data), 15.1 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
## vrSample ####
### subset ####
#' Methods for vrSample objects
#'
#' Methods for \code{\link{vrSample}} objects for generics defined in other
#' packages
#'
#' @param x A vrSample object
#' @param i the name of layer associated with the sample, see \link{SampleMetadata}
#' @param value a vrLayer object, see \link{vrLayer}
#'
#' @name vrSample-methods
#' @rdname vrSample-methods
#'
#' @concept vrsample
#'
NULL
#' @describeIn vrSample-methods Accessing vrLayer objects from \code{vrSample} objects
#'
#' @importFrom methods slot
setMethod(
f = '[[',
signature = c('vrSample', "character"),
definition = function(x, i){
# sample names
layer_names <- names(methods::slot(x, "layer"))
# check query sample name
if(!i %in% layer_names){
stop("There are no layers named ", i, " in this sample")
}
# return samples
return(x@layer[[i]])
}
)
#' @describeIn vrSample-methods Accessing vrLayer objects from \code{vrSample} objects
#'
#' @importFrom methods slot
setMethod(
f = '[[<-',
signature = c('vrSample', "character"),
definition = function(x, i, value){
# check if value if vrLayer
if(!inherits(value, "vrLayer")){
stop("The provided object is not of class vrLayer")
}
# sample names
layer_names <- names(methods::slot(x, "layer"))
# check query sample name
if(!i %in% layer_names){
stop("There are no layers named ", i, " in this sample")
}
# change layer
x@layer[[i]] <- value
# return
return(x)
}
)
## vrBlock ####
### subset ####
#' @describeIn vrSample-methods (deprecated) Accessing vrLayer objects from \code{vrBlock} objects
#'
#' @importFrom methods slot
setMethod(
f = '[[',
signature = c('vrBlock', "character"),
definition = function(x, i){
# sample names
layer_names <- names(methods::slot(x, "layer"))
# check query sample name
if(!i %in% layer_names){
stop("There are no layers named ", i, " in this sample")
}
# return samples
return(x@layer[[i]])
}
)
#' @describeIn vrSample-methods (deprecated) Overwriting vrLayer objects from \code{vrBlock} objects
#'
#' @importFrom methods slot
setMethod(
f = '[[<-',
signature = c('vrBlock', "character"),
definition = function(x, i, value){
# check if value if vrLayer
if(!inherits(value, "vrLayer")){
stop("The provided object is not of class vrLayer")
}
# sample names
layer_names <- names(methods::slot(x, "layer"))
# check query sample name
if(!i %in% layer_names){
stop("There are no layers named ", i, " in this sample")
}
# change layer
x@layer[[i]] <- value
# return
return(x)
}
)
## vrLayer ####
### subset ####
#' Methods for vrLayer objects
#'
#' Methods for \code{\link{vrLayer}} objects for generics defined in other
#' packages
#'
#' @param x A vrLayer object
#' @param i the name of assay associated with the layer, see \link{SampleMetadata}
#' @param value a vrAssayV2 object, see \link{vrAssayV2}
#'
#' @name vrLayer-methods
#' @rdname vrLayer-methods
#'
#' @concept vrlayer
#'
NULL
#' @describeIn vrLayer-methods Accessing vrAssay objects from \code{vrLayer} objects
#'
#' @importFrom methods slot
setMethod(
f = '[[',
signature = c('vrLayer', "character"),
definition = function(x, i){
# if no assay were found, check sample names
assay_names <- names(methods::slot(x, "assay"))
# check query sample name
if(!i %in% assay_names){
stop("There are no assays named ", i, " in this object")
} else {
return(x@assay[[i]])
}
}
)
#' @describeIn vrLayer-methods Overwriting vrAssay objects from \code{vrLayer} objects
#'
#' @importFrom methods slot
setMethod(
f = '[[<-',
signature = c('vrLayer', "character"),
definition = function(x, i, value){
# if no assay were found, check sample names
assay_names <- names(methods::slot(x, "assay"))
# check query sample name
if(!i %in% assay_names){
stop("There are no assays named ", i, " in this object")
}
x@assay[[i]] <- value
return(x)
}
)
####
# Methods ####
####
### vrSample Methods ####
mergevrSample <- function(x, y, samples = NULL){
# start
object <- x
object_list <- y
# combine all elements
if(!is.list(object_list))
object_list <- list(object_list)
object_list <- c(object, object_list)
names(object_list) <- samples
# set VoltRon class
return(object_list)
}
#' Merging vrSample objects
#'
#' Given a vrSample object, and a list of vrSample objects, merge all.
#'
#' @param x a vrSample object
#' @param y a list of vrSample objects
#' @param samples the sample names
#'
#' @method merge vrSample
setMethod("merge", "vrSample", mergevrSample)
#' Merging vrBlock objects
#'
#' Given a vrBlock object, and a list of vrSample objects, merge all.
#'
#' @param x a vrSample object
#' @param y a list of vrSample objects
#' @param samples the sample names
#'
#' @method merge vrBlock
setMethod("merge", "vrBlock", mergevrSample)
# merge.vrBlock <- function(object, object_list, samples = NULL){
# merge.vrSample(object, object_list = object_list, samples = samples)
# }
subsetvrSample <- function(x, subset, assays = NULL, spatialpoints = NULL, image = NULL) {
# start
object <- x
if (!missing(x = subset)) {
subset <- enquo(arg = subset)
}
# subseting on samples, layers and assays
layers <- object@layer
if(!is.null(assays)){
object@layer <- sapply(layers, function(lay) {
subsetvrLayer(lay, assays = assays)
}, USE.NAMES = TRUE, simplify = TRUE)
} else if(!is.null(spatialpoints)){
object@layer <- sapply(layers, function(lay) {
subsetvrLayer(lay, spatialpoints = spatialpoints)
}, USE.NAMES = TRUE, simplify = TRUE)
} else if(!is.null(image)){
object@layer <- sapply(layers, function(lay) {
subsetvrLayer(lay, image = image)
}, USE.NAMES = TRUE, simplify = TRUE)
}
# remove NULL assays
ind <- which(vapply(object@layer, function(x) !is.null(x), logical(1)))
object@layer <- object@layer[ind]
# check if there are layers
if(length(object@layer) > 0){
# get updated adjaceny and distance
catch_connect <- try(slot(object, name = "zlocation"), silent = TRUE)
if(!is(catch_connect, 'try-error') && !methods::is(catch_connect,'error')){
object@zlocation <- object@zlocation[ind]
object@adjacency <- object@adjacency[ind, ind, drop = FALSE]
}
# return object
return(object)
} else {
return(NULL)
}
}
#' Subsetting vrSample objects
#'
#' Given a vrSample object, subset the object given one of the attributes
#'
#' @param x a vrSample object
#' @param subset the subset statement
#' @param assays the set of assays to subset the object
#' @param spatialpoints the set of spatial points to subset the object
#' @param image the subseting string passed to \link{image_crop}
#'
#' @method subset vrSample
#' @order 6
#'
#' @importFrom rlang enquo
setMethod("subset", "vrSample", subsetvrSample)
#' Subsetting vrBlock objects
#'
#' Given a vrBlock object, subset the object given one of the attributes
#'
#' @param x a vrSample object
#' @param subset the subset statement
#' @param assays the set of assays to subset the object
#' @param spatialpoints the set of spatial points to subset the object
#' @param image the subseting string passed to \link{image_crop}
#'
#' @method subset vrBlock
#' @order 6
setMethod("subset", "vrBlock", subsetvrSample)
# subset.vrBlock <- function(object, subset, assays = NULL, spatialpoints = NULL, image = NULL){
# subset.vrSample(object, subset = subset, assays = assays, spatialpoints = spatialpoints, image = image)
# }
#' @rdname vrSpatialPoints
#' @order 5
#' @export
setMethod("vrSpatialPoints", "vrSample", function(object) {
do.call("c", lapply(object@layer, function(lay) {
vrSpatialPoints(lay)
}))
})
#' @rdname vrSpatialPoints
#' @order 5
#' @export
setMethod("vrSpatialPoints", "vrBlock", function(object) {
do.call("c", lapply(object@layer, function(lay) {
vrSpatialPoints(lay)
}))
})
changeAssayNamesvrSample <- function(object, sample.metadata = NULL){
if(is.null(sample.metadata))
stop("Please provide a sample.metadata")
if(!"NewAssayNames" %in% colnames(sample.metadata))
stop("Please provide a sample.metadata with NewAssayNames column which includes the new assay names")
# change the assay names of the layers
layer_names <- names(object@layer)
for(lyr in layer_names)
object[[lyr]] <- changeAssayNames(object[[lyr]], sample.metadata = sample.metadata[sample.metadata$Layer == lyr,])
# return
return(object)
}
#' changeAssayNames.vrSample
#'
#' Change the assay names of assays within a vrSample object
#'
#' @param sample.metadata the sample metadata with NewAssayNames column which includes the new assay names
#'
#' @rdname changeAssayNames
#'
#' @noRd
setMethod("changeAssayNames", "vrSample", changeAssayNamesvrSample)
changeAssayNamesvrBlock <- function(object, sample.metadata = NULL) {
object <- changeAssayNamesvrSample(object, sample.metadata = sample.metadata)
return(object)
}
#' changeAssayNames.vrBlock
#'
#' Change the assay names of assays within a vrBlock object
#'
#' @param sample.metadata the sample metadata with NewAssayNames column which includes the new assay names
#'
#' @rdname changeAssayNames
#'
#' @noRd
setMethod("changeAssayNames", "vrBlock", changeAssayNamesvrBlock)
### vrLayer Methods ####
subsetvrLayer <- function(x, subset, assays = NULL, spatialpoints = NULL, image = NULL) {
# start
object <- x
if (!missing(x = subset)) {
subset <- enquo(arg = subset)
}
# subseting on samples, layers and assays
if(!is.null(assays)){
# get assay names of all assays
assay_names <- vapply(object@assay, vrAssayNames, character(1))
if(any(assays %in% assay_names)) {
assays <- intersect(assays, assay_names)
object@assay <- object@assay[which(assay_names %in% assays)]
} else if(any(assays %in% names(object@assay))) {
object@assay <- object@assay[names(object@assay) %in% assays]
} else {
return(NULL)
}
} else if(!is.null(spatialpoints)){
# get points connected to queried spatialpoints
catch_connect <- try(slot(object, name = "connectivity"), silent = TRUE)
if(!is(catch_connect, 'try-error') && !methods::is(catch_connect,'error')){
if(igraph::vcount(object@connectivity) > 0){
spatialpoints <- getConnectedSpatialPoints(object, spatialpoints)
object@connectivity <- subset.Connectivity(object@connectivity, spatialpoints)
}
}
# subset assays
object@assay <- sapply(object@assay, function(assy) {
if(inherits(assy, "vrAssay")){
# return(subset.vrAssay(assy, spatialpoints = spatialpoints))
return(subsetvrAssay(assy, spatialpoints = spatialpoints))
} else {
# return(subset.vrAssayV2(assy, spatialpoints = spatialpoints))
return(subsetvrAssay(assy, spatialpoints = spatialpoints))
}
}, USE.NAMES = TRUE, simplify = TRUE)
} else if(!is.null(image)){
object@assay <- sapply(object@assay, function(assy) {
if(inherits(assy, "vrAssay")){
# return(subset.vrAssay(assy, image = image))
return(subsetvrAssay(assy, image = image))
} else {
return(subsetvrAssay(assy, image = image))
}
}, USE.NAMES = TRUE, simplify = TRUE)
}
# remove NULL assays
object@assay <- object@assay[which(vapply(object@assay, function(x) !is.null(x), logical(1)))]
# set VoltRon class
if(length(object@assay) > 0){
return(object)
} else {
return(NULL)
}
}
#' Subsetting vrLayer objects
#'
#' Given a vrLayer object, subset the object given one of the attributes
#'
#' @param x a vrLayer object
#' @param subset the subset statement
#' @param assays the set of assays to subset the object
#' @param spatialpoints the set of spatial points to subset the object
#' @param image the subseting string passed to \link{image_crop}
#'
#' @method subset vrLayer
#' @order 7
#'
#' @importFrom rlang enquo
#' @importFrom methods is
setMethod("subset", "vrLayer", subsetvrLayer)
#' @rdname vrSpatialPoints
#' @order 6
#' @export
setMethod("vrSpatialPoints", "vrLayer", function(object) {
do.call("c", lapply(object@assay, function(assy) {
vrSpatialPoints(assy)
}))
})
#' subset.Connectivity
#'
#' Subsetting the connectivity graph of vrLayer using spatial points
#'
#' @param object the connectivity graph of the vrLayer
#' @param spatialpoints the set of spatial points
#'
#' @importFrom igraph induced_subgraph
#'
#' @noRd
subset.Connectivity <- function(object, spatialpoints = NULL){
return(igraph::induced_subgraph(object, spatialpoints))
}
#' getConnectedSpatialPoints
#'
#' get spatial points connected to other spatial points in the connectivity graph of vrLayer
#'
#' @param object A vrLayer object
#' @param spatialpoints the set of spatial points
#'
#' @importFrom igraph neighborhood V vcount
#'
#' @noRd
getConnectedSpatialPoints <- function(object, spatialpoints = NULL){
if(igraph::vcount(object@connectivity) > 0){
spatialpoints <- intersect(spatialpoints, igraph::V(object@connectivity)$name)
return(names(unlist(igraph::neighborhood(object@connectivity, nodes = spatialpoints))))
} else {
return(spatialpoints)
}
}
changeAssayNamesvrLayer <- function(object, sample.metadata = NULL){
if(is.null(sample.metadata))
stop("Please provide a sample.metadata")
if(!"NewAssayNames" %in% colnames(sample.metadata))
stop("Please provide a sample.metadata with NewAssayNames column which includes the new assay names")
# change the assay names of the connectivity graph if exists
catch_connect <- try(slot(object, name = "connectivity"), silent = TRUE)
if(!is(catch_connect, 'try-error') && !methods::is(catch_connect,'error')){
if(igraph::vcount(object@connectivity) > 0){
spatialpoints <- igraph::V(object@connectivity)$name
old_assay_names <- vapply(object@assay, vrAssayNames, character(1))
new_assay_names <- sample.metadata$NewAssayNames
cur_spatialpoints <- spatialpoints
for(i in seq_len(length(old_assay_names))){
if(old_assay_names[i]!=new_assay_names[i]){
ind <- grepl(paste0(old_assay_names[i],"$"), spatialpoints)
cur_spatialpoints[ind] <- gsub(paste0(old_assay_names[i],"$"), new_assay_names[i], spatialpoints[ind])
}
}
igraph::V(object@connectivity)$name <- cur_spatialpoints
}
}
# change the assay names of vrAssays
assay_names <- names(object@assay)
for(assy in assay_names)
vrAssayNames(object[[assy]]) <- rownames(sample.metadata[sample.metadata$Assay == assy,])
# return
return(object)
}
#' changeAssayNamesvrLayer
#'
#' Change the assay names of assays within a vrSample object
#'
#' @rdname changeAssayNames
#'
#' @importFrom igraph V V<- vcount
#' @importFrom methods is
#'
#' @noRd
setMethod("changeAssayNames", "vrLayer", changeAssayNamesvrLayer)