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

Switch to unified view

a b/R/sample.R
1
## vrSample ####
2
3
### subset ####
4
5
#' Methods for vrSample objects
6
#'
7
#' Methods for \code{\link{vrSample}} objects for generics defined in other
8
#' packages
9
#'
10
#' @param x A vrSample object
11
#' @param i the name of layer associated with the sample, see \link{SampleMetadata}
12
#' @param value a vrLayer object, see \link{vrLayer}
13
#' 
14
#' @name vrSample-methods
15
#' @rdname vrSample-methods
16
#'
17
#' @concept vrsample
18
#'
19
NULL
20
21
#' @describeIn vrSample-methods Accessing vrLayer objects from \code{vrSample} objects
22
#' 
23
#' @importFrom methods slot
24
setMethod(
25
  f = '[[',
26
  signature = c('vrSample', "character"),
27
  definition = function(x, i){
28
29
    # sample names
30
    layer_names <- names(methods::slot(x, "layer"))
31
32
    # check query sample name
33
    if(!i %in% layer_names){
34
      stop("There are no layers named ", i, " in this sample")
35
    }
36
37
    # return samples
38
    return(x@layer[[i]])
39
  }
40
)
41
42
43
#' @describeIn vrSample-methods Accessing vrLayer objects from \code{vrSample} objects
44
#' 
45
#' @importFrom methods slot
46
setMethod(
47
  f = '[[<-',
48
  signature = c('vrSample', "character"),
49
  definition = function(x, i, value){
50
51
    # check if value if vrLayer
52
    if(!inherits(value, "vrLayer")){
53
      stop("The provided object is not of class vrLayer")
54
    }
55
56
    # sample names
57
    layer_names <- names(methods::slot(x, "layer"))
58
59
    # check query sample name
60
    if(!i %in% layer_names){
61
      stop("There are no layers named ", i, " in this sample")
62
    }
63
64
    # change layer
65
    x@layer[[i]] <- value
66
67
    # return
68
    return(x)
69
  }
70
)
71
72
## vrBlock ####
73
74
### subset ####
75
76
#' @describeIn vrSample-methods (deprecated) Accessing vrLayer objects from \code{vrBlock} objects
77
#' 
78
#' @importFrom methods slot
79
setMethod(
80
  f = '[[',
81
  signature = c('vrBlock', "character"),
82
  definition = function(x, i){
83
    
84
    # sample names
85
    layer_names <- names(methods::slot(x, "layer"))
86
    
87
    # check query sample name
88
    if(!i %in% layer_names){
89
      stop("There are no layers named ", i, " in this sample")
90
    }
91
    
92
    # return samples
93
    return(x@layer[[i]])
94
  }
95
)
96
97
#' @describeIn vrSample-methods (deprecated) Overwriting vrLayer objects from \code{vrBlock} objects
98
#' 
99
#' @importFrom methods slot
100
setMethod(
101
  f = '[[<-',
102
  signature = c('vrBlock', "character"),
103
  definition = function(x, i, value){
104
    
105
    # check if value if vrLayer
106
    if(!inherits(value, "vrLayer")){
107
      stop("The provided object is not of class vrLayer")
108
    }
109
    
110
    # sample names
111
    layer_names <- names(methods::slot(x, "layer"))
112
    
113
    # check query sample name
114
    if(!i %in% layer_names){
115
      stop("There are no layers named ", i, " in this sample")
116
    }
117
    
118
    # change layer
119
    x@layer[[i]] <- value
120
    
121
    # return
122
    return(x)
123
  }
124
)
125
126
## vrLayer ####
127
128
### subset ####
129
130
#' Methods for vrLayer objects
131
#'
132
#' Methods for \code{\link{vrLayer}} objects for generics defined in other
133
#' packages
134
#'
135
#' @param x A vrLayer object
136
#' @param i the name of assay associated with the layer, see \link{SampleMetadata}
137
#' @param value a vrAssayV2 object, see \link{vrAssayV2}
138
#' 
139
#' @name vrLayer-methods
140
#' @rdname vrLayer-methods
141
#'
142
#' @concept vrlayer
143
#'
144
NULL
145
146
#' @describeIn vrLayer-methods Accessing vrAssay objects from \code{vrLayer} objects
147
#' 
148
#' @importFrom methods slot
149
setMethod(
150
  f = '[[',
151
  signature = c('vrLayer', "character"),
152
  definition = function(x, i){
153
154
    # if no assay were found, check sample names
155
    assay_names <- names(methods::slot(x, "assay"))
156
157
    # check query sample name
158
    if(!i %in% assay_names){
159
      stop("There are no assays named ", i, " in this object")
160
    } else {
161
      return(x@assay[[i]])
162
    }
163
  }
164
)
165
166
#' @describeIn vrLayer-methods Overwriting vrAssay objects from \code{vrLayer} objects
167
#' 
168
#' @importFrom methods slot
169
setMethod(
170
  f = '[[<-',
171
  signature = c('vrLayer', "character"),
172
  definition = function(x, i, value){
173
174
    # if no assay were found, check sample names
175
    assay_names <- names(methods::slot(x, "assay"))
176
177
    # check query sample name
178
    if(!i %in% assay_names){
179
      stop("There are no assays named ", i, " in this object")
180
    }
181
182
    x@assay[[i]] <- value
183
    return(x)
184
  }
185
)
186
187
####
188
# Methods ####
189
####
190
191
### vrSample Methods ####
192
193
mergevrSample <- function(x, y, samples = NULL){
194
  
195
  # start
196
  object <- x
197
  object_list <- y
198
   
199
  # combine all elements
200
  if(!is.list(object_list))
201
    object_list <- list(object_list)
202
  object_list <- c(object, object_list)
203
  names(object_list) <- samples
204
  
205
  # set VoltRon class
206
  return(object_list)
207
}
208
209
#' Merging vrSample objects
210
#'
211
#' Given a vrSample object, and a list of vrSample objects, merge all.
212
#'
213
#' @param x a vrSample object
214
#' @param y a list of vrSample objects
215
#' @param samples the sample names
216
#'
217
#' @method merge vrSample
218
setMethod("merge", "vrSample", mergevrSample)
219
220
#' Merging vrBlock objects
221
#'
222
#' Given a vrBlock object, and a list of vrSample objects, merge all.
223
#' 
224
#' @param x a vrSample object
225
#' @param y a list of vrSample objects
226
#' @param samples the sample names
227
#' 
228
#' @method merge vrBlock
229
setMethod("merge", "vrBlock", mergevrSample)
230
# merge.vrBlock <- function(object, object_list, samples = NULL){
231
#   merge.vrSample(object, object_list = object_list, samples = samples)
232
# }
233
234
subsetvrSample <- function(x, subset, assays = NULL, spatialpoints = NULL, image = NULL) {
235
  
236
  # start
237
  object <- x
238
  
239
  if (!missing(x = subset)) {
240
    subset <- enquo(arg = subset)
241
  }
242
  
243
  # subseting on samples, layers and assays
244
  layers <- object@layer
245
  if(!is.null(assays)){
246
    object@layer <- sapply(layers, function(lay) {
247
      subsetvrLayer(lay, assays = assays)
248
    }, USE.NAMES = TRUE, simplify = TRUE)
249
  } else if(!is.null(spatialpoints)){
250
    object@layer <- sapply(layers, function(lay) {
251
      subsetvrLayer(lay, spatialpoints = spatialpoints)
252
    }, USE.NAMES = TRUE, simplify = TRUE)
253
  } else if(!is.null(image)){
254
    object@layer <- sapply(layers, function(lay) {
255
      subsetvrLayer(lay, image = image)
256
    }, USE.NAMES = TRUE, simplify = TRUE)
257
  }
258
  
259
  # remove NULL assays
260
  ind <- which(vapply(object@layer, function(x) !is.null(x), logical(1)))
261
  object@layer <- object@layer[ind]
262
  
263
  # check if there are layers
264
  if(length(object@layer) > 0){
265
    
266
    # get updated adjaceny and distance
267
    catch_connect <- try(slot(object, name = "zlocation"), silent = TRUE)
268
    if(!is(catch_connect, 'try-error') && !methods::is(catch_connect,'error')){
269
      object@zlocation <- object@zlocation[ind]
270
      object@adjacency <- object@adjacency[ind, ind, drop = FALSE]
271
    }
272
    
273
    # return object
274
    return(object)
275
  } else {
276
    return(NULL)
277
  }
278
}
279
280
#' Subsetting vrSample objects
281
#'
282
#' Given a vrSample object, subset the object given one of the attributes
283
#'
284
#' @param x a vrSample object
285
#' @param subset the subset statement
286
#' @param assays the set of assays to subset the object
287
#' @param spatialpoints the set of spatial points to subset the object
288
#' @param image the subseting string passed to \link{image_crop}
289
#'
290
#' @method subset vrSample
291
#' @order 6
292
#'
293
#' @importFrom rlang enquo
294
setMethod("subset", "vrSample", subsetvrSample)
295
296
#' Subsetting vrBlock objects
297
#'
298
#' Given a vrBlock object, subset the object given one of the attributes
299
#' 
300
#' @param x a vrSample object
301
#' @param subset the subset statement
302
#' @param assays the set of assays to subset the object
303
#' @param spatialpoints the set of spatial points to subset the object
304
#' @param image the subseting string passed to \link{image_crop}
305
#'
306
#' @method subset vrBlock
307
#' @order 6
308
setMethod("subset", "vrBlock", subsetvrSample)
309
310
# subset.vrBlock <- function(object, subset, assays = NULL, spatialpoints = NULL, image = NULL){
311
#   subset.vrSample(object, subset = subset, assays = assays, spatialpoints = spatialpoints, image = image)
312
# }
313
314
#' @rdname vrSpatialPoints
315
#' @order 5
316
#' @export
317
setMethod("vrSpatialPoints", "vrSample", function(object) {
318
  do.call("c", lapply(object@layer, function(lay) {
319
    vrSpatialPoints(lay)
320
  }))
321
})
322
323
#' @rdname vrSpatialPoints
324
#' @order 5
325
#' @export
326
setMethod("vrSpatialPoints", "vrBlock", function(object) {
327
  do.call("c", lapply(object@layer, function(lay) {
328
    vrSpatialPoints(lay)
329
  }))
330
})
331
332
changeAssayNamesvrSample <- function(object, sample.metadata = NULL){
333
  
334
  if(is.null(sample.metadata))
335
    stop("Please provide a sample.metadata")
336
  
337
  if(!"NewAssayNames" %in% colnames(sample.metadata))
338
    stop("Please provide a sample.metadata with NewAssayNames column which includes the new assay names")
339
  
340
  # change the assay names of the layers
341
  layer_names <- names(object@layer)
342
  for(lyr in layer_names)
343
    object[[lyr]] <- changeAssayNames(object[[lyr]], sample.metadata = sample.metadata[sample.metadata$Layer == lyr,])
344
  
345
  # return
346
  return(object)
347
}
348
349
#' changeAssayNames.vrSample
350
#'
351
#' Change the assay names of assays within a vrSample object
352
#'
353
#' @param sample.metadata the sample metadata with NewAssayNames column which includes the new assay names
354
#' 
355
#' @rdname changeAssayNames
356
#'
357
#' @noRd
358
setMethod("changeAssayNames", "vrSample", changeAssayNamesvrSample)
359
360
changeAssayNamesvrBlock <- function(object, sample.metadata = NULL) {
361
  object <- changeAssayNamesvrSample(object, sample.metadata = sample.metadata)
362
  return(object)
363
}
364
365
#' changeAssayNames.vrBlock
366
#'
367
#' Change the assay names of assays within a vrBlock object
368
#' 
369
#' @param sample.metadata the sample metadata with NewAssayNames column which includes the new assay names
370
#' 
371
#' @rdname changeAssayNames
372
#'
373
#' @noRd
374
setMethod("changeAssayNames", "vrBlock", changeAssayNamesvrBlock)
375
376
### vrLayer Methods ####
377
378
subsetvrLayer <- function(x, subset, assays = NULL, spatialpoints = NULL, image = NULL) {
379
  
380
  # start
381
  object <- x
382
  
383
  if (!missing(x = subset)) {
384
    subset <- enquo(arg = subset)
385
  }
386
  
387
  # subseting on samples, layers and assays
388
  if(!is.null(assays)){
389
    
390
    # get assay names of all assays
391
    assay_names <- vapply(object@assay, vrAssayNames, character(1))
392
    if(any(assays %in% assay_names)) {
393
      assays <- intersect(assays, assay_names)
394
      object@assay  <- object@assay[which(assay_names %in% assays)]
395
    } else if(any(assays %in% names(object@assay))) {
396
      object@assay  <- object@assay[names(object@assay) %in% assays]
397
    } else {
398
      return(NULL)
399
    }
400
    
401
  } else if(!is.null(spatialpoints)){
402
    
403
    # get points connected to queried spatialpoints
404
    catch_connect <- try(slot(object, name = "connectivity"), silent = TRUE)
405
    if(!is(catch_connect, 'try-error') && !methods::is(catch_connect,'error')){
406
      if(igraph::vcount(object@connectivity) > 0){
407
        spatialpoints <- getConnectedSpatialPoints(object, spatialpoints)
408
        object@connectivity <- subset.Connectivity(object@connectivity, spatialpoints)
409
      }
410
    }
411
    
412
    # subset assays
413
    object@assay <- sapply(object@assay, function(assy) {
414
      if(inherits(assy, "vrAssay")){
415
        # return(subset.vrAssay(assy, spatialpoints = spatialpoints))
416
        return(subsetvrAssay(assy, spatialpoints = spatialpoints))
417
      } else {
418
        # return(subset.vrAssayV2(assy, spatialpoints = spatialpoints))
419
        return(subsetvrAssay(assy, spatialpoints = spatialpoints))
420
      }
421
    }, USE.NAMES = TRUE, simplify = TRUE)
422
    
423
  } else if(!is.null(image)){
424
    object@assay <- sapply(object@assay, function(assy) {
425
      if(inherits(assy, "vrAssay")){
426
        # return(subset.vrAssay(assy, image = image))
427
        return(subsetvrAssay(assy, image = image))
428
      } else {
429
        return(subsetvrAssay(assy, image = image))
430
      }
431
    }, USE.NAMES = TRUE, simplify = TRUE)
432
  }
433
  
434
  # remove NULL assays
435
  object@assay <- object@assay[which(vapply(object@assay, function(x) !is.null(x), logical(1)))]
436
  
437
  # set VoltRon class
438
  if(length(object@assay) > 0){
439
    return(object)
440
  } else {
441
    return(NULL)
442
  }
443
}
444
445
#' Subsetting vrLayer objects
446
#'
447
#' Given a vrLayer object, subset the object given one of the attributes
448
#'
449
#' @param x a vrLayer object
450
#' @param subset the subset statement
451
#' @param assays the set of assays to subset the object
452
#' @param spatialpoints the set of spatial points to subset the object
453
#' @param image the subseting string passed to \link{image_crop}
454
#'
455
#' @method subset vrLayer
456
#' @order 7
457
#'
458
#' @importFrom rlang enquo
459
#' @importFrom methods is
460
setMethod("subset", "vrLayer", subsetvrLayer)
461
462
#' @rdname vrSpatialPoints
463
#' @order 6
464
#' @export
465
setMethod("vrSpatialPoints", "vrLayer", function(object) {
466
  do.call("c", lapply(object@assay, function(assy) {
467
      vrSpatialPoints(assy)
468
  }))
469
})
470
471
#' subset.Connectivity
472
#'
473
#' Subsetting the connectivity graph of vrLayer using spatial points
474
#'
475
#' @param object the connectivity graph of the vrLayer
476
#' @param spatialpoints the set of spatial points
477
#'
478
#' @importFrom igraph induced_subgraph
479
#'
480
#' @noRd
481
subset.Connectivity <- function(object, spatialpoints = NULL){
482
  return(igraph::induced_subgraph(object, spatialpoints))
483
}
484
485
#' getConnectedSpatialPoints
486
#'
487
#' get spatial points connected to other spatial points in the connectivity graph of vrLayer
488
#'
489
#' @param object A vrLayer object
490
#' @param spatialpoints the set of spatial points
491
#'
492
#' @importFrom igraph neighborhood V vcount
493
#'
494
#' @noRd
495
getConnectedSpatialPoints <- function(object, spatialpoints = NULL){
496
  if(igraph::vcount(object@connectivity) > 0){
497
    spatialpoints <- intersect(spatialpoints, igraph::V(object@connectivity)$name)
498
    return(names(unlist(igraph::neighborhood(object@connectivity, nodes = spatialpoints))))
499
  } else {
500
    return(spatialpoints)
501
  }
502
}
503
504
changeAssayNamesvrLayer <- function(object, sample.metadata = NULL){
505
506
  if(is.null(sample.metadata))
507
    stop("Please provide a sample.metadata")
508
509
  if(!"NewAssayNames" %in% colnames(sample.metadata))
510
    stop("Please provide a sample.metadata with NewAssayNames column which includes the new assay names")
511
512
  # change the assay names of the connectivity graph if exists
513
  catch_connect <- try(slot(object, name = "connectivity"), silent = TRUE)
514
  if(!is(catch_connect, 'try-error') && !methods::is(catch_connect,'error')){
515
    if(igraph::vcount(object@connectivity) > 0){
516
      spatialpoints <- igraph::V(object@connectivity)$name
517
      old_assay_names <- vapply(object@assay, vrAssayNames, character(1))
518
      new_assay_names <- sample.metadata$NewAssayNames
519
      cur_spatialpoints <- spatialpoints
520
      for(i in seq_len(length(old_assay_names))){
521
        if(old_assay_names[i]!=new_assay_names[i]){
522
          ind <- grepl(paste0(old_assay_names[i],"$"), spatialpoints)
523
          cur_spatialpoints[ind] <- gsub(paste0(old_assay_names[i],"$"), new_assay_names[i], spatialpoints[ind])
524
        }
525
      }
526
      igraph::V(object@connectivity)$name <- cur_spatialpoints
527
    }
528
  }
529
530
  # change the assay names of vrAssays
531
  assay_names <- names(object@assay)
532
  for(assy in assay_names)
533
    vrAssayNames(object[[assy]]) <- rownames(sample.metadata[sample.metadata$Assay == assy,])
534
535
  # return
536
  return(object)
537
}
538
539
#' changeAssayNamesvrLayer
540
#'
541
#' Change the assay names of assays within a vrSample object
542
#'
543
#' @rdname changeAssayNames
544
#'
545
#' @importFrom igraph V V<- vcount
546
#' @importFrom methods is
547
#'
548
#' @noRd
549
setMethod("changeAssayNames", "vrLayer", changeAssayNamesvrLayer)