Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export("imgSource<-")
export("molecules<-")
export("spatialCoords<-")
export("spatialCoordsNames<-")
export("spatialData<-")
export("spatialDataNames<-")
export(SpatialExperiment)
export(SpatialImage)
export(addImg)
Expand All @@ -30,8 +28,6 @@ export(rotateObject)
export(scaleFactors)
export(spatialCoords)
export(spatialCoordsNames)
export(spatialData)
export(spatialDataNames)
export(toSpatialExperiment)
exportClasses(LoadedSpatialImage)
exportClasses(RemoteSpatialImage)
Expand All @@ -47,8 +43,6 @@ exportMethods("imgSource<-")
exportMethods("molecules<-")
exportMethods("spatialCoords<-")
exportMethods("spatialCoordsNames<-")
exportMethods("spatialData<-")
exportMethods("spatialDataNames<-")
exportMethods(addImg)
exportMethods(dim)
exportMethods(getImg)
Expand All @@ -66,16 +60,12 @@ exportMethods(rotateObject)
exportMethods(scaleFactors)
exportMethods(spatialCoords)
exportMethods(spatialCoordsNames)
exportMethods(spatialData)
exportMethods(spatialDataNames)
importClassesFrom(S4Vectors,Annotated)
importFrom(BiocGenerics,cbind)
importFrom(BiocGenerics,rbind)
importFrom(S4Vectors,DataFrame)
importFrom(S4Vectors,coolcat)
importFrom(S4Vectors,isEmpty)
importFrom(S4Vectors,make_zero_col_DFrame)
importFrom(S4Vectors,metadata)
importFrom(S4Vectors,setValidity2)
importFrom(SingleCellExperiment,"int_colData<-")
importFrom(SingleCellExperiment,"int_metadata<-")
Expand Down
12 changes: 0 additions & 12 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ setGeneric("mirrorImg", function(x, ...) standardGeneric("mirrorImg"))

# SpatialExperiment methods ----------------------------------------------------

#' @export
setGeneric("spatialData", function(x, ...) standardGeneric("spatialData"))

#' @export
setGeneric("spatialData<-", function(x, value) standardGeneric("spatialData<-"))

#' @export
setGeneric("spatialDataNames", function(x) standardGeneric("spatialDataNames"))

#' @export
setGeneric("spatialDataNames<-", function(x, value) standardGeneric("spatialDataNames<-"))

#' @export
setGeneric("spatialCoords", function(x, ...) standardGeneric("spatialCoords"))

Expand Down
1 change: 0 additions & 1 deletion R/SpatialExperiment-colData.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ setReplaceMethod("colData",
setReplaceMethod("colData",
c("SpatialExperiment", "NULL"),
function(x, value) {
spatialDataNames(x) <- NULL
value <- colData(x)["sample_id"]
colData(x) <- value
return(x)
Expand Down
111 changes: 2 additions & 109 deletions R/SpatialExperiment-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#' @title Methods for spatial attributes
#'
#' @aliases
#' spatialData spatialData<-
#' spatialDataNames spatialDataNames<-
#' spatialCoords spatialCoords<-
#' spatialCoordsNames spatialCoordsNames<-
#' imgData imgData<-
Expand All @@ -28,39 +26,21 @@
#' @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:
#' @section 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:
#' @section 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")}).}
Expand Down Expand Up @@ -110,86 +90,6 @@
#' 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
Expand Down Expand Up @@ -268,13 +168,6 @@ setMethod("scaleFactors",

# 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 = "")
Expand Down
69 changes: 8 additions & 61 deletions R/SpatialExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@
#' \code{imageSources}) containing unique image identifiers.
#' @param loadImage Logical indicating whether to load image into memory.
#' Default = \code{FALSE}.
#' @param spatialDataNames (Deprecated) A \code{character} vector of column
#' names from \code{\link{colData}} to include in \code{\link{spatialData}}.
#' Alternatively, the \code{spatialData} argument may be provided. If both are
#' provided, \code{spatialDataNames} is given precedence, and a warning is
#' returned. (Note: \code{spatialData} and \code{spatialDataNames} have been
#' deprecated; \code{colData} and \code{spatialCoords} should be used for all
#' columns. The arguments have been retained for backward compatibility but
#' may be removed in the future.)
#' @param spatialData (Deprecated) A \code{\link{DataFrame}} containing columns
#' to store in \code{\link{spatialData}}, which must contain at least the
#' columns of spatial coordinates. Alternatively, \code{spatialDataNames} may
#' be provided. If both are provided, \code{spatialDataNames} is given
#' precedence, and a warning is returned. (Note: \code{spatialData} and
#' \code{spatialDataNames} have been deprecated; \code{colData} and
#' \code{spatialCoords} should be used for all columns. The arguments have
#' been retained for backward compatibility but may be removed in the future.)
#'
#' @details
#' In this class, rows represent genes, and columns represent spots (for
Expand Down Expand Up @@ -217,9 +201,7 @@ SpatialExperiment <- function(...,
imageSources=NULL,
image_id=NULL,
loadImage=FALSE,
imgData=NULL,
spatialDataNames=NULL,
spatialData=NULL) {
imgData=NULL) {

sce <- SingleCellExperiment(...)
spe <- .sce_to_spe(sce=sce,
Expand All @@ -230,9 +212,7 @@ SpatialExperiment <- function(...,
imageSources=imageSources,
image_id=image_id,
loadImage=loadImage,
imgData=imgData,
spatialDataNames=spatialDataNames,
spatialData=spatialData)
imgData=imgData)
return(spe)
}

Expand All @@ -247,9 +227,7 @@ SpatialExperiment <- function(...,
imageSources=NULL,
image_id=NULL,
loadImage=TRUE,
imgData=NULL,
spatialDataNames=NULL,
spatialData=NULL) {
imgData=NULL) {

old <- S4Vectors:::disableValidity()
if (!isTRUE(old)) {
Expand All @@ -270,40 +248,29 @@ SpatialExperiment <- function(...,
sample_id <- unique(sce$sample_id)
}

if (!is.null(spatialData) || !is.null(spatialDataNames)) {
.msg_spatialData()
}

spe <- new("SpatialExperiment", sce)

# in the following code chunk, we give precedence
# to spatialData/CoordsNames over spatialData/Coords
# where spatialDataNames should be in colData,
# and spatialCoordsNames can be in both colData and spatialData
# if both spatialData/Coords and -Names are supplied
# to spatialCoordsNames over spatialCoords
# where spatialCoordsNames should be in colData
# if both spatialCoords and -Names are supplied
# we give an informative warning notifying the user
# that spatialData/CoordsNames will be used
# that spatialCoordsNames will be used

msg <- function(.) message(sprintf(
"Both '%s' and '%sNames' have been supplied;\nusing '%s'. ", ., ., .),
"Set either to NULL to suppress this message.")

if (!is.null(spatialCoordsNames)) {
stopifnot(is.character(spatialCoordsNames),
all(spatialCoordsNames %in% names(colData(spe)))
|| all(spatialCoordsNames %in% names(spatialData)))
all(spatialCoordsNames %in% names(colData(spe))))
if (!is.null(spatialCoords))
msg("spatialCoords")
if (all(spatialCoordsNames %in% names(colData(spe)))) {
i <- spatialCoordsNames
j <- setdiff(names(colData(spe)), i)
spatialCoords(spe) <- as.matrix(colData(spe)[i])
colData(spe) <- colData(spe)[j]
} else {
i <- spatialCoordsNames
j <- setdiff(names(spatialData), i)
spatialCoords(spe) <- as.matrix(spatialData[i])
spatialData <- spatialData[j]
}
} else if (!is.null(spatialCoords)) {
stopifnot(
Expand All @@ -315,26 +282,6 @@ SpatialExperiment <- function(...,
spatialCoords(spe) <- NULL
}

if (!is.null(spatialDataNames)) {
stopifnot(
is.character(spatialDataNames),
spatialDataNames %in% names(colData(spe)))
if (!is.null(spatialData))
msg("spatialData")
i <- spatialDataNames
j <- setdiff(names(colData(spe)), i)
spd <- colData(spe)[i]
colData(spe) <- colData(spe)[j]
spatialData(spe) <- spd
} else if (!is.null(spatialData)) {
stopifnot(
is(spatialData, "DFrame"),
nrow(spatialData) == ncol(spe))
spatialData(spe) <- spatialData
} else {
spatialData(spe) <- NULL
}

if (!is.null(imgData)) {
stopifnot(imgData$sample_id %in% spe$sample_id)
imgData(spe) <- imgData
Expand Down
12 changes: 0 additions & 12 deletions R/Validity.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
return(msg)
}

.spatialData_validity <- function(x, msg=NULL) {
y <- colData(x)[int_metadata(x)$spatialDataNames]
if (!is.null(y) && !is(y, "DFrame")) {
msg <- c(msg, paste(
"'spatialData' field in 'int_colData'",
"should be a 'DFrame'"))
}
return(msg)
}

.colData_validity <- function(obj, msg=NULL) {
df <- colData(obj)
if (is.null(df$sample_id)) {
Expand Down Expand Up @@ -86,9 +76,7 @@
msg <- NULL
msg <- .colData_validity(object, msg)
msg <- .imgData_validity(object, msg)
msg <- .spatialData_validity(object, msg)
msg <- .spatialCoords_validity(object, msg)
#msg <- .spatialDataNames_validity(object, msg)
if (length(msg)) return(msg)
return(TRUE)
}
Expand Down
Loading