1717# ' imageNames(y)
1818# ' region(table(y, 1))
1919# ' region(table(y, 2))
20+ # '
21+ # ' y <- combine(list(Alpha=x, x, Omega=x))
22+ # ' shapeNames(y)
23+ # '
24+ # ' @importFrom BiocGenerics combine
2025NULL
2126
22- # ' @export
23- # ' @rdname combine
24- # ' @importFrom BiocGenerics combine
25- setMethod ("combine ", c("SpatialData", "SpatialData"), \(x, y, ...) {
26- # ensure element names are unique across objects
27- old <- list (unlist(colnames(x )), unlist(colnames(y )))
28- idx <- rep.int(c(1 , 2 ), lengths(old ))
29- new <- split(make.unique(unlist(old )), idx )
30- for (i in c(1 , 2 )) {
31- # get input element names
32- z <- get(c(" x" , " y" )[i ])
33- old_nms <- unlist(colnames(z )[.ls ])
34-
35- # find new names for these elements
27+ .combine <- \(xs , old , new ) {
28+ for (i in seq_along(xs )) {
29+ x <- xs [[i ]]
30+ # elements that might be referred to by tables (labels, shapes)
31+ old_nms <- unlist(colnames(x )[.ls ])
3632 j <- match(old_nms , old [[i ]])
3733 new_nms <- new [[i ]][j ]
38-
3934 # rename elements
4035 for (l in .ls ) {
41- j <- match(names(z [[l ]]), old [[i ]])
42- names(z [[l ]]) <- new [[i ]][j ]
36+ j <- match(names(x [[l ]]), old [[i ]])
37+ names(x [[l ]]) <- new [[i ]][j ]
4338 }
4439 # sync tables
45- z <- .sync_tables_sdattrs(z , old_nms , new_nms )
46-
40+ x <- .sync_tables_sdattrs(x , old_nms , new_nms )
4741 # rename tables themselves
48- j <- match(tableNames(z ), old [[i ]])
49- tableNames(z ) <- new [[i ]][j ]
50-
51- assign(c(" x" , " y" )[i ], z )
42+ j <- match(tableNames(x ), old [[i ]])
43+ tableNames(x ) <- new [[i ]][j ]
44+ xs [[i ]] <- x
5245 }
53- SpatialData(
54- images = c(x $ images , y $ images ),
55- labels = c(x $ labels , y $ labels ),
56- points = c(x $ points , y $ points ),
57- shapes = c(x $ shapes , y $ shapes ),
58- tables = c(x $ tables , y $ tables ))
46+ names(ls ) <- ls <- .LAYERS
47+ args <- lapply(ls , \(l ) do.call(c , lapply(unname(xs ), \(x ) x [[l ]])))
48+ do.call(SpatialData , args )
49+ }
50+
51+ # ' @export
52+ # ' @rdname combine
53+ setMethod ("combine ", c("list", "missing"), \(x, y, ...) {
54+ # validate input
55+ ok <- all(vapply(x , \(. ) is(. , " SpatialData" ), logical (1 )))
56+ if (! ok ) stop(" 'x' should be a list of 'SpatialData' objects" )
57+ # get current element names
58+ old <- lapply(x , \(z ) unlist(colnames(z )))
59+ # get list names; if missing, use empty strings
60+ if (is.null(nms <- names(x )))
61+ nms <- character (length(x ))
62+ # prepend list names to element names where available
63+ new <- lapply(seq_along(x ), \(i ) {
64+ if (nms [i ] == " " ) return (old [[i ]])
65+ paste(nms [i ], old [[i ]], sep = " ." )
66+ })
67+ # ensure global uniqueness
68+ new <- split(
69+ make.unique(unlist(new )),
70+ rep(seq_along(new ), lengths(new )))
71+ .combine(x , old , new )
5972})
73+
74+ # ' @export
75+ # ' @rdname combine
76+ setMethod ("combine ",
77+ c(" SpatialData" , " SpatialData" ),
78+ \(x , y , ... ) combine(list (x , y )))
79+
0 commit comments