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
|
#' @name SpatialExperiment-methods
#'
#' @title Methods for spatial attributes
#'
#' @aliases
#' spatialData spatialData<-
#' spatialDataNames spatialDataNames<-
#' spatialCoords spatialCoords<-
#' spatialCoordsNames spatialCoordsNames<-
#' imgData imgData<-
#' scaleFactors
#'
#' @description
#' The \code{\link{SpatialExperiment}} class provides a family of methods to get
#' and set spatial data attributes in \code{\link{SpatialExperiment}} objects.
#' Spatial attributes include \code{spatialCoords}, \code{imgData}, and
#' \code{scaleFactors}, as well as methods to rotate and mirror
#' SpatialExperiment objects and their spatial coordinates.
#'
#' @param x A \code{\link{SpatialExperiment}} object.
#' @param value Replacement value for replacement methods.
#' @param sample_id Logical value or character vector specifying sample
#' identifier(s) for \code{scaleFactors}. Default = \code{TRUE} (all samples).
#' @param image_id Logical value or character vector specifying image
#' identifier(s) for \code{scaleFactors}. Default = \code{TRUE} (all images).
#' @param name The name of the \code{colData} column to extract.
#'
#' @details
#' Additional details for each type of data attribute are provided below.
#'
#' Note: \code{\link{spatialData}} and \code{\link{spatialDataNames}}
#' (previously used to store a subset of columns from \code{\link{colData}})
#' have been deprecated. All columns should be stored in either
#' \code{\link{spatialCoords}} (numeric matrix containing spatial coordinates)
#' or \code{\link{colData}} (all other columns). The
#' \code{spatialData}/\code{spatialDataNames} functionality has been retained
#' for backward compatibility but may be removed in the future.
#'
#' See \code{\link{rotateCoords}}, \code{\link{mirrorCoords}},
#' \code{\link{rotateObject}}, or \code{\link{mirrorObject}} for details on
#' methods to rotate and mirror SpatialExperiment objects and their
#' \code{spatialCoords}.
#'
#' @section spatialData and spatialCoords methods:
#' \describe{
#' \item{\code{spatialData(x) <- value}: }{
#' The \code{spatialData} setter expects a \code{DataFrame}.
#' If the input does not contain an \code{in_tissue} column,
#' this will be included with a default value of \code{1}.}
#' \item{\code{spatialCoords(x)}: }{
#' Getter for numeric matrix of spatial coordinates.}
#' \item{\code{spatialCoords(x) <- value}: }{
#' Setter for numeric matrix of spatial coordinates.}
#' }
#'
#' @section spatialDataNames and spatialCoordsNames methods:
#' \describe{
#' \item{\code{spatialDataNames(x)}: }{
#' Returns the names of the \code{colData} associated with the
#' spatial information, which are stored in the \code{int_metadata}.}
#' \item{\code{spatialDataNames(x) <- value}: }{
#' Setter to replace column names
#' in the \code{spatialData} \code{DataFrame}.}
#' \item{\code{spatialCoordsNames(x)}: }{
#' Returns the defined names of the
#' spatial coordinates (e.g. \code{c("x", "y")}).}
#' \item{\code{spatialCoordsNames(x) <- value}: }{
#' Setter to define the names of the spatial coordinate columns.}
#' }
#'
#' @section imgData methods:
#' \describe{
#' \item{\code{imgData(x)}: }{
#' Getter to return the \code{imgData} \code{DataFrame}.}
#' \item{\code{imgData(x) <- value}: }{
#' Setter to provide a \code{DataFrame} object as
#' \code{imgData} of the \code{SpatialExperiment} object.}
#' }
#'
#' @section Other methods:
#' \describe{
#' \item{\code{scaleFactors(x, sample_id, image_id)}: }{
#' Getter to return the scale factors associated with the
#' \code{sample_id}(s) and \code{image_id}(s) provided.
#' This is related to the stored image(s) in the \code{SpatialExperiment}
#' \code{imgData} structure. See argument descriptions for further details.}
#' }
#'
#' @return Return value varies depending on method, as described below.
#'
#' @examples
#' example(read10xVisium)
#'
#' # spatialCoords returns a numeric matrix
#' head(spatialCoords(spe))
#'
#' # change spatial coordinate names
#' spatialCoordsNames(spe)
#' spatialCoordsNames(spe) <- c("x", "y")
#' head(spatialCoords(spe))
#'
#' # imgData and scale factors
#' imgData(spe)
#' scaleFactors(spe)
#'
#' # tabulate number of spots mapped to tissue
#' cd <- colData(spe)
#' table(
#' in_tissue = cd$in_tissue,
#' sample_id = cd$sample_id)
NULL
# spatialData ------------------------------------------------------------------
#' @rdname SpatialExperiment-methods
#' @importFrom SummarizedExperiment colData
#' @export
setMethod("spatialData",
"SpatialExperiment",
function(x) {
colData(x)[spatialDataNames(x)]
}
)
#' @rdname SpatialExperiment-methods
#' @importFrom SummarizedExperiment colData colData<-
#' @export
setReplaceMethod("spatialData",
c("SpatialExperiment", "DFrame"),
function(x, value) {
stopifnot(nrow(value) == ncol(x))
out <- colData(x)
old <- names(out)
new <- names(value)
dup <- intersect(old, new)
if (length(dup) > 0) {
out[dup] <- value
value <- value[setdiff(new, dup)]
}
out <- cbind(out, value)
colData(x) <- out
spatialDataNames(x) <- new
return(x)
}
)
#' @rdname SpatialExperiment-methods
#' @importFrom S4Vectors make_zero_col_DFrame
#' @export
setReplaceMethod("spatialData",
c("SpatialExperiment", "NULL"),
function(x, value) {
`spatialDataNames<-`(x, value)
}
)
# spatialDataNames -------------------------------------------------------------
#' @rdname SpatialExperiment-methods
#' @importFrom S4Vectors metadata
#' @export
setMethod("spatialDataNames",
"SpatialExperiment",
function(x) {
.msg_spatialData()
int_metadata(x)$spatialDataNames
}
)
#' @rdname SpatialExperiment-methods
#' @importFrom SummarizedExperiment colData
#' @importFrom SingleCellExperiment int_metadata<-
#' @export
setReplaceMethod("spatialDataNames",
c("SpatialExperiment", "character"),
function(x, value) {
stopifnot(value %in% names(colData(x)))
int_metadata(x)$spatialDataNames <- value
return(x)
}
)
#' @rdname SpatialExperiment-methods
#' @export
setReplaceMethod("spatialDataNames",
c("SpatialExperiment", "NULL"),
function(x, value) {
value <- character()
`spatialDataNames<-`(x, value)
}
)
# spatialCoords ----------------------------------------------------------------
#' @rdname SpatialExperiment-methods
#' @importFrom SingleCellExperiment int_colData<-
#' @export
setMethod("spatialCoords",
"SpatialExperiment",
function(x) int_colData(x)$spatialCoords)
#' @rdname SpatialExperiment-methods
#' @importFrom SingleCellExperiment int_colData<-
#' @export
setReplaceMethod("spatialCoords",
c("SpatialExperiment", "matrix"),
function(x, value) {
stopifnot(
is.numeric(value),
nrow(value) == ncol(x))
int_colData(x)$spatialCoords <- value
return(x)
}
)
#' @rdname SpatialExperiment-methods
#' @export
setReplaceMethod("spatialCoords",
c("SpatialExperiment", "NULL"),
function(x, value) {
value <- matrix(numeric(), ncol(x), 0)
`spatialCoords<-`(x, value)
}
)
# spatialCoordsNames -----------------------------------------------------------
#' @rdname SpatialExperiment-methods
#' @importFrom SingleCellExperiment int_colData
#' @export
setMethod("spatialCoordsNames",
"SpatialExperiment",
function(x) colnames(int_colData(x)$spatialCoords))
#' @rdname SpatialExperiment-methods
#' @importFrom SingleCellExperiment int_colData<-
#' @export
setReplaceMethod("spatialCoordsNames",
c("SpatialExperiment", "character"),
function(x, value) {
colnames(int_colData(x)$spatialCoords) <- value
return(x)
}
)
#' @rdname SpatialExperiment-methods
#' @export
setReplaceMethod("spatialCoordsNames",
c("SpatialExperiment", "NULL"),
function(x, value) {
value <- character()
`spatialCoordsNames<-`(x, value)
}
)
# scaleFactors -----------------------------------------------------------------
#' @rdname SpatialExperiment-methods
#' @export
setMethod("scaleFactors",
"SpatialExperiment",
function(x, sample_id=TRUE, image_id=TRUE) {
stopifnot(!is.null(imgData(x)))
idx <- .get_img_idx(x, sample_id, image_id)
imgData(x)$scaleFactor[idx]
}
)
# utils ------------------------------------------------------------------------
# message for deprecation of spatialData/Names
.msg_spatialData <- function() {
message(paste0(
"Note: spatialData and spatialDataNames have been deprecated; all ",
"columns should be stored in colData and spatialCoords"))
}
#' @export
#' @importFrom utils .DollarNames
.DollarNames.SpatialExperiment <- function(x, pattern = "")
grep(pattern, names(colData(x)), value = TRUE)
#' @rdname SpatialExperiment-methods
#' @aliases $,SpatialExperiment-method
#' @exportMethod $
setMethod("$", "SpatialExperiment", function(x, name) {
colData(x)[[name]]
})
|