Skip to content

Commit a48232a

Browse files
authored
Merge pull request #99 from HelenaLC/nodefix
+`plotCoordGraph`
2 parents 666a06a + 5ebf03c commit a48232a

File tree

7 files changed

+114
-14
lines changed

7 files changed

+114
-14
lines changed

DESCRIPTION

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: SpatialData
22
Title: Representation of Python's SpatialData in R
33
Depends: R (>= 4.4)
4-
Version: 0.99.18
4+
Version: 0.99.19
55
Description: Interface to Python's 'SpatialData', currently including:
66
reticulate-based use of 'spatialdata-io' for reading of manufracturer
77
data and writing to .zarr, on-disk representation of images/labels as
@@ -38,34 +38,35 @@ Authors@R: c(
3838
Imports:
3939
arrow,
4040
basilisk,
41+
BiocFileCache,
42+
BiocGenerics,
4143
dplyr,
4244
DelayedArray,
4345
geoarrow,
4446
graph,
4547
jsonlite,
48+
methods,
49+
Rarr,
4650
RBGL,
51+
reticulate,
52+
Rgraphviz,
4753
rlang,
4854
sf,
55+
S4Arrays,
4956
S4Vectors,
5057
SingleCellExperiment,
5158
SummarizedExperiment,
52-
zellkonverter,
53-
BiocGenerics,
54-
S4Arrays,
55-
Rarr,
56-
reticulate,
57-
BiocFileCache,
58-
methods
59+
zellkonverter
5960
Suggests:
6061
anndataR,
6162
BiocStyle,
6263
ggnewscale,
6364
knitr,
65+
magick,
66+
paws,
6467
pizzarr,
6568
Rgraphviz,
66-
testthat,
67-
magick,
68-
paws
69+
testthat
6970
Remotes:
7071
keller-mark/pizzarr,
7172
keller-mark/anndataR@spatialdata

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export(do_tx_to_ext)
2020
export(mask)
2121
export(merfish_demo_path)
2222
export(path_to_10x_xen_demo)
23+
export(plotCoordGraph)
2324
export(readImage)
2425
export(readLabel)
2526
export(readPoint)
@@ -95,6 +96,8 @@ importFrom(BiocGenerics,colnames)
9596
importFrom(BiocGenerics,rownames)
9697
importFrom(RBGL,sp.between)
9798
importFrom(Rarr,ZarrArray)
99+
importFrom(Rgraphviz,layoutGraph)
100+
importFrom(Rgraphviz,renderGraph)
98101
importFrom(S4Vectors,"metadata<-")
99102
importFrom(S4Vectors,coolcat)
100103
importFrom(S4Vectors,isSequence)
@@ -121,9 +124,11 @@ importFrom(graph,"edgeData<-")
121124
importFrom(graph,"edgeDataDefaults<-")
122125
importFrom(graph,"nodeData<-")
123126
importFrom(graph,"nodeDataDefaults<-")
127+
importFrom(graph,"nodes<-")
124128
importFrom(graph,addEdge)
125129
importFrom(graph,addNode)
126130
importFrom(graph,edgeData)
131+
importFrom(graph,graph.par)
127132
importFrom(graph,graphAM)
128133
importFrom(graph,nodeData)
129134
importFrom(graph,nodes)

R/coord_utils.R

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#'
2525
#' # object-wide
2626
#' g <- CTgraph(x)
27-
#' graphics.off(); graph::plot(g)
27+
#' plotCoordGraph(g)
2828
#'
2929
#' # retrieve transformation from element to target space
3030
#' CTpath(x, "blobs_labels", "sequence")
@@ -349,3 +349,48 @@ setMethod("addCT", "Zattrs", \(x, name, type="identity", data=NULL) {
349349
#' #' @importFrom EBImage resize
350350
#' setMethod("translation", "ImageArray", \(x, t, ...) {})
351351
#' setMethod("transform", "ImageArray", \(x, t) get(t$type)(x, unlist(t[[t$type]])))
352+
353+
# plotCoordGraph() ----
354+
355+
#' @name plotCoordGraph
356+
#' @title CT graph viz.
357+
#' @description
358+
#' given a \code{graphNEL} instance, nodes with \code{nchar>max} are
359+
#' split and hyphenated at character position \code{floor(nchar/fac)}.
360+
#'
361+
#' @examples
362+
#' x <- file.path("extdata", "blobs.zarr")
363+
#' x <- system.file(x, package="SpatialData")
364+
#' x <- readSpatialData(x, tables=FALSE)
365+
#'
366+
#' g <- CTgraph(x)
367+
#' plotCoordGraph(g, cex=0.6)
368+
#'
369+
#' @importFrom graph nodes nodes<- graph.par
370+
#' @importFrom Rgraphviz layoutGraph renderGraph
371+
#' @export
372+
plotCoordGraph <- \(g, cex=0.6) {
373+
g <- CTgraph(x)
374+
g2view <- g # leave 'g' alone
375+
nodes(g2view) <- .nodefix(nodes(g2view))
376+
graph.par(list(nodes=list(shape="plaintext", cex=cex)))
377+
g2view <- layoutGraph(g2view)
378+
renderGraph(g2view)
379+
}
380+
381+
.nodefix <- \(x, fac=2, max=10) {
382+
if (any((nc <- nchar(x)) > max))
383+
x[nc > max] <- .fixup(x[nc > max], fac)
384+
x
385+
}
386+
387+
.fixup <- \(x, fac) {
388+
nc <- nchar(x)
389+
hm <- floor(nc/fac)
390+
xs <- strsplit(x, "")
391+
xu <- lapply(seq_along(xs), \(i) c(
392+
xs[[i]][seq_len(hm[i])], "-\n",
393+
xs[[i]][-seq_len(hm[i])]))
394+
xu <- lapply(xu, \(z) paste(z, collapse=""))
395+
unlist(xu)
396+
}

inst/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
changes in version 0.99.19
2+
3+
- adding code to improve visualization of transformation network (PR #98)
4+
5+
changes in version 0.99.18
6+
7+
- split off 'SpatialData.plot'
8+
19
changes in version 0.99.17
210

311
- rewrite of points and labels plotting, including

man/coord-utils.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotCoordGraph.Rd

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

vignettes/SpatialData.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ We can represent these are a graph as follows:
136136

137137
```{r cs-graph}
138138
(g <- CTgraph(x))
139-
graph::plot(g)
139+
plotCoordGraph(g)
140140
```
141141

142142
The above representation greatly facilitates queries of the transformation(s)

0 commit comments

Comments
 (0)