Skip to content

Commit 07eed59

Browse files
committed
splitComponent #73
1 parent 39228fb commit 07eed59

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export(spatialGraph)
149149
export(spatialGraphNames)
150150
export(spatialGraphs)
151151
export(splitByCol)
152+
export(splitComponent)
152153
export(splitContiguity)
153154
export(splitSamples)
154155
export(spotPoly)
@@ -265,6 +266,7 @@ importFrom(S4Vectors,mcols)
265266
importFrom(S4Vectors,metadata)
266267
importFrom(S4Vectors,setValidity2)
267268
importFrom(S4Vectors,showAsCell)
269+
importFrom(S4Vectors,split)
268270
importFrom(SingleCellExperiment,"altExp<-")
269271
importFrom(SingleCellExperiment,"int_colData<-")
270272
importFrom(SingleCellExperiment,"int_elementMetadata<-")
@@ -389,6 +391,7 @@ importFrom(spdep,graph2nb)
389391
importFrom(spdep,knearneigh)
390392
importFrom(spdep,knn2nb)
391393
importFrom(spdep,mat2listw)
394+
importFrom(spdep,n.comp.nb)
392395
importFrom(spdep,nb2listw)
393396
importFrom(spdep,nb2listwdist)
394397
importFrom(spdep,poly2nb)

R/spatialGraphs.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
#' Spatial neighborhood graphs as \code{spdep}'s \code{listw} objects are stored
1515
#' in the \code{int_metadata} of the SFE object. The \code{listw} class is used
1616
#' because \code{spdep} has many useful methods that rely on the neighborhood
17-
#' graph as \code{listw}.
17+
#' graph as \code{listw}. See the
18+
#' \href{https://r-spatial.github.io/spdep/reference/index.html}{\code{spdep}
19+
#' doumentation website} for functions to edit the spatial neighborhood graph,
20+
#' or the \code{nb} object within the \code{listw}.
1821
#'
1922
#' @param x A \code{SpatialFeatureExperiment} object.
2023
#' @param value A \code{listw} object (\code{*Graph}), or a named list of list
@@ -38,9 +41,9 @@
3841
#' argument in \code{\link[SpatialExperiment]{SpatialExperiment}}.
3942
#' @name spatialGraphs
4043
#' @concept Getters and setters
41-
#' @return Getters for multiple graphs return a named list. Getters for
42-
#' names return a character vector of the names. Getters for single graphs
43-
#' return a \code{listw} object. Setters return an SFE object.
44+
#' @return Getters for multiple graphs return a named list. Getters for names
45+
#' return a character vector of the names. Getters for single graphs return a
46+
#' \code{listw} object. Setters return an SFE object.
4447
#' @aliases rowGraphs rowGraphs<- spatialGraph spatialGraph<- spatialGraphNames
4548
#' colGraphs colGraphs<- spatialGraphNames<- spatialGraphs<- annotGraphs
4649
#' annotGraphs<-

R/split.R

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#' SFE object by \code{sample_id} so each sample will become a separate SFE
99
#' object. The \code{splitContiguity} function splits the SFE object by
1010
#' contiguity of an \code{annotGeometry}, which by default is "tissueBoundary".
11+
#' The \code{splitComponent} function splits the SFE object by graph component,
12+
#' so if there are disconnected components in the graph, then each component
13+
#' will become a new SFE object.
1114
#'
1215
#' @inheritParams crop
1316
#' @param x An SFE object
@@ -25,6 +28,13 @@
2528
#' @param min_area Minimum area in the same unit as the geometry coordinates
2629
#' (squared) for each piece to be considered a separate piece when splitting
2730
#' by contiguity. Only pieces that are large enough are considered.
31+
#' @param colGraphName Name of graph to use for \code{splitComponent}. We
32+
#' recommend distance based neighbors (`dnearneigh` in
33+
#' \code{\link{findSpatialNeighbors}}), and recommend NOT using k nearest
34+
#' neighbors (`knearneigh`) or triangulation (`tri2nb`).
35+
#' @param min_cells Minimum number of cells per graph component; components with
36+
#' fewer than this number of cells are considered debris and removed.
37+
#'
2838
#' @return A list of SFE objects.
2939
#' @concept Geometric operations
3040
#' @name splitByCol
@@ -105,4 +115,18 @@ splitContiguity <- function(x, colGeometryName = 1L,
105115
splitByCol(x, ag_union, colGeometryName = colGeometryName, cover = cover)
106116
}
107117

108-
# TODO: split by graph components
118+
#' @importFrom spdep n.comp.nb
119+
#' @importFrom S4Vectors split
120+
#' @rdname splitByCol
121+
#' @export
122+
splitComponent <- function(x, colGraphName = 1L, min_cells = 100) {
123+
g <- colGraph(x, colGraphName)
124+
comps <- n.comp.nb(g$neighbours)
125+
n_cells <- table(comps$comp.id)
126+
sfes <- split(x, comps$comp.id)
127+
if (any(n_cells < min_cells)) {
128+
inds <- n_cells >= min_cells
129+
sfes <- sfes[inds]
130+
}
131+
sfes
132+
}

man/spatialGraphs.Rd

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/splitByCol.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-split.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,12 @@ test_that("Split by contiguity of an annotGeometry", {
8686
# When it has pieces that are too small
8787
})
8888

89+
test_that("Split by graph component", {
90+
colGraph(sfe, "dnn") <- findSptialNeighbors(sfe, method = "dnearneigh", d2 = 25)
91+
sfes <- splitComponent(sfe, colGraphName = "dnn")
92+
classes <- vapply(sfes, class, FUN.VALUE = character(1))
93+
expect_true(all(classes == "SpatialFeatureExperiment"))
94+
expect_equal(length(sfes), 2L)
95+
})
96+
8997
unlink(fn, recursive = TRUE)

0 commit comments

Comments
 (0)