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
|
#' @include zzz.R
#' @include generics.R
#' @importFrom methods setClass slot slot<- new
#'
NULL
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Class definitions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' The SpatialImage class
#'
#' The \code{SpatialImage} class is a virtual class representing spatial
#' information for Seurat. All spatial image information must inherit from this
#' class for use with \code{Seurat} objects
#'
#' @slot assay Name of assay to associate image data with; will give this image
#' priority for visualization when the assay is set as the active/default assay
#' in a \code{Seurat} object
#' @template slot-key
#' @template slot-misc
#'
#' @name SpatialImage-class
#' @rdname SpatialImage-class
#' @exportClass SpatialImage
#'
#' @seealso \code{\link{SpatialImage-methods}} for a list of required and
#' provided methods
#'
#' @aliases SpatialImage
#'
setClass(
Class = 'SpatialImage',
contains = 'VIRTUAL',
slots = list(
'assay' = 'character',
'key' = 'character',
'misc' = 'OptionalList'
),
prototype = list(
'misc' = list()
)
)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Methods for Seurat-defined generics
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' \code{SpatialImage} methods
#'
#' Methods defined on the \code{\link{SpatialImage}} class. Some of these
#' methods must be overridden in order to ensure proper functionality of the
#' derived classes (see \strong{Required methods} below). Other methods are
#' designed to work across all \code{SpatialImage}-derived subclasses, and
#' should only be overridden if necessary
#'
#' @param x,object A \code{SpatialImage}-derived object
#' @param ... Arguments passed to other methods
#' @param value Depends on the method:
#' \describe{
#' \item{\code{DefaultAssay<-}}{Assay that the image should be
#' associated with}
#' \item{\code{Key<-}}{New key for the image}
#' }
#' @inheritParams RenameCells
#'
#' @section Provided methods:
#' These methods are defined on the \code{SpatialImage} object and should not
#' be overridden without careful thought
#' \itemize{
#' \item \code{\link{DefaultAssay}} and \code{\link{DefaultAssay<-}}
#' \item \code{\link{Key}} and \code{\link{Key<-}}
#' \item \code{\link{GetImage}}; this method \emph{can} be overridden to
#' provide image data, normally returns empty image data. If overridden,
#' should default to returning a \code{\link[grid]{grob}} object
#' \item \code{\link{IsGlobal}}
#' \item \code{\link{Radius}}; this method \emph{can} be overridden to
#' provide a spot radius for image objects
#' \item \code{\link[base:Extract]{[}}; this method \emph{can} be overridden
#' to change default subset behavior, normally returns
#' \code{subset(x = x, cells = i)}. If overridden, should only accept \code{i}
#' }
#'
#' @section Required methods:
#' All subclasses of the \code{SpatialImage} class must define the following
#' methods; simply relying on the \code{SpatialImage} method will result in
#' errors. For required parameters and their values, see the \code{Usage} and
#' \code{Arguments} sections
#' \describe{
#' \item{\code{\link{Cells}}}{
#' Return the cell/spot barcodes associated with each position
#' }
#' \item{\code{\link{dim}}}{
#' Return the dimensions of the image for plotting in \code{(Y, X)} format
#' }
#' \item{\code{\link{GetTissueCoordinates}}}{
#' Return tissue coordinates; by default, must return a two-column
#' \code{data.frame} with x-coordinates in the first column and
#' y-coordinates in the second
#' }
#' \item{\code{\link{Radius}}}{
#' Return the spot radius; returns \code{NULL} by default for use with
#' non-spot image technologies
#' }
#' \item{\code{\link{RenameCells}}}{
#' Rename the cell/spot barcodes for this image
#' }
#' \item{\code{\link{subset}}}{
#' Subset the image data by cells/spots
#' }
#' }
#' These methods are used throughout Seurat, so defining them and setting the
#' proper defaults will allow subclasses of \code{SpatialImage} to work
#' seamlessly
#'
#' @name SpatialImage-methods
#' @rdname SpatialImage-methods
#'
#' @concept spatialimage
#'
NULL
#' @describeIn SpatialImage-methods Get the cell names from an image
#' (\strong{[Override]})
#'
#' @return \strong{[Override]} \code{Cells}: should return cell names
#'
#' @method Cells SpatialImage
#' @export
#'
Cells.SpatialImage <- function(x, ...) {
stop(
"'Cells' must be implemented for all subclasses of 'SpatialImage'",
call. = FALSE
)
}
#' @describeIn SpatialImage-methods Get the associated assay of a
#' \code{SpatialImage}-derived object
#'
#' @return \code{DefaultAssay}: The associated assay of a
#' \code{SpatialImage}-derived object
#'
#' @method DefaultAssay SpatialImage
#' @export
#'
#' @seealso \code{\link{DefaultAssay}}
#'
DefaultAssay.SpatialImage <- function(object, ...) {
CheckDots(...)
return(slot(object = object, name = 'assay'))
}
#' @describeIn SpatialImage-methods Set the associated assay of a
#' \code{SpatialImage}-derived object
#'
#' @return \code{DefaultAssay<-}: \code{object} with the associated assay
#' updated
#'
#' @method DefaultAssay<- SpatialImage
#' @export
#'
"DefaultAssay<-.SpatialImage" <- function(object, ..., value) {
CheckDots(...)
slot(object = object, name = 'assay') <- value
return(object)
}
#' @describeIn SpatialImage-methods Get the image data from a
#' \code{SpatialImage}-derived object
#'
#' @inheritParams GetImage
#'
#' @return \strong{[Override]} \code{GetImage}: The image data from a
#' \code{SpatialImage}-derived object
#'
#' @method GetImage SpatialImage
#' @export
#'
#' @seealso \code{\link{GetImage}}
#'
GetImage.SpatialImage <- function(
object,
mode = c('grob', 'raster', 'plotly', 'raw'),
...
) {
return(NullImage(mode = mode))
}
#' @describeIn SpatialImage-methods Get tissue coordinates for a
#' \code{SpatialImage}-derived object (\strong{[Override]})
#'
#' @return \strong{[Override]} \code{GetTissueCoordinates}: ...
#'
#' @method GetTissueCoordinates SpatialImage
#' @export
#'
#' @seealso \code{\link{GetTissueCoordinates}}
#'
GetTissueCoordinates.SpatialImage <- function(object, ...) {
stop(
"'GetTissueCoordinates' must be implemented for all subclasses of 'SpatialImage'",
call. = FALSE
)
}
#' @describeIn SpatialImage-methods Globality test for
#' \code{SpatialImage}-derived object
#'
#' @return \code{IsGlobal}: returns \code{TRUE} as images are, by default,
#' global
#'
#' @method IsGlobal SpatialImage
#' @export
#'
#' @seealso \code{\link{IsGlobal}}
#'
IsGlobal.SpatialImage <- function(object, ...) {
return(TRUE)
}
#' @describeIn SpatialImage-methods Get the key for a
#' \code{SpatialImage}-derived object
#'
#' @return \code{Key}: The key for a \code{SpatialImage}-derived object
#'
#' @method Key SpatialImage
#' @export
#'
#' @seealso \code{\link{Key}}
#'
Key.SpatialImage <- function(object, ...) {
CheckDots(...)
# object <- UpdateSlots(object = object)
return(slot(object = object, name = 'key'))
}
#' @describeIn SpatialImage-methods Set the key for a
#' \code{SpatialImage}-derived object
#'
#' @return \code{Key<-}: \code{object} with the key set to \code{value}
#'
#' @method Key<- SpatialImage
#' @export
#'
"Key<-.SpatialImage" <- function(object, ..., value) {
CheckDots(...)
object <- UpdateSlots(object = object)
value <- UpdateKey(key = value)
slot(object = object, name = 'key') <- value
return(object)
}
#' @describeIn SpatialImage-methods Get the spot radius size
#'
#' @return \code{Radius}: The spot radius size; by default, returns \code{NULL}
#'
#' @method Radius SpatialImage
#' @export
#'
Radius.SpatialImage <- function(object, ...) {
return(NULL)
}
#' @describeIn SpatialImage-methods Rename cells in a
#' \code{SpatialImage}-derived object (\strong{[Override]})
#'
#' @return \strong{[Override]} \code{RenameCells}: \code{object} with the new
#' cell names
#'
#' @method RenameCells SpatialImage
#' @export
#'
#' @seealso \code{\link{RenameCells}}
#'
RenameCells.SpatialImage <- function(object, new.names = NULL, ...) {
stop(
"'RenameCells' must be implemented for all subclasses of 'SpatialImage'",
call. = FALSE
)
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Methods for R-defined generics
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' @describeIn SpatialImage-methods Subset a \code{SpatialImage}-derived object
#'
#' @param i,cells A vector of cells to keep
#'
#' @return \code{[}, \code{subset}: \code{x}/\code{object} for only the cells
#' requested
#'
#' @method [ SpatialImage
#' @export
#'
"[.SpatialImage" <- function(x, i, ...) {
return(subset(x = x, cells = i))
}
#' @describeIn SpatialImage-methods Get the plotting dimensions of an image
#' (\strong{[Override]})
#'
#' @return \strong{[Override]} \code{dim}: The dimensions of the image data in
#' (Y, X) format
#'
#' @method dim SpatialImage
#' @export
#'
dim.SpatialImage <- function(x) {
stop(
"'dim' must be implemented for all subclasses of 'SpatialImage'",
call. = FALSE
)
}
#' @describeIn SpatialImage-methods Subset a \code{SpatialImage}-derived object
#' (\strong{[Override]})
#'
#' @method subset SpatialImage
#' @export
#'
subset.SpatialImage <- function(x, cells, ...) {
stop("'subset' must be implemented for all subclasses of 'SpatialImage'")
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# S4 methods
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' @describeIn SpatialImage-methods Overview of a \code{SpatialImage}-derived
#' object
#'
#' @return \code{show}: Prints summary to \code{\link[base]{stdout}} and
#' invisibly returns \code{NULL}
#'
#' @importFrom methods show
#'
#' @export
#'
setMethod(
f = 'show',
signature = 'SpatialImage',
definition = function(object) {
object <- UpdateSlots(object = object)
cat(
"Spatial data from the",
class(x = object),
"technology for",
length(x = Cells(x = object)),
"samples\n"
)
cat("Associated assay:", DefaultAssay(object = object), "\n")
cat("Image key:", Key(object = object), "\n")
return(invisible(x = NULL))
}
)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Internal
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Return a null image
#'
#' @inheritParams GetImage
#'
#' @return Varies by value of \code{mode}:
#' \describe{
#' \item{\dQuote{grob}}{a \code{\link[grid]{nullGrob}}}
#' \item{\dQuote{raster}}{an empty \code{\link[grDevices:as.raster]{raster}}}
#' \item{\dQuote{plotly}}{a list with one named item: \code{value = FALSE}}
#' \item{\dQuote{raw}}{returns \code{NULL}}
#' }
#'
#' @importFrom grid nullGrob
#' @importFrom grDevices as.raster
#'
#' @keywords internal
#'
#' @noRd
#'
NullImage <- function(mode = c('grob', 'raster', 'plotly', 'raw')) {
mode <- mode[1]
mode <- match.arg(arg = mode)
image <- switch(
EXPR = mode,
'grob' = nullGrob(),
'raster' = as.raster(x = new(Class = 'matrix')),
'plotly' = list('visible' = FALSE),
'raw' = NULL,
stop("Unknown image mode: ", mode, call. = FALSE)
)
return(image)
}
|