spatialGE 2.0.0
Behavior changes
-
transform_data()now populatesvst.variance.standardizedin@gene_meta. Previously the column was added only as a side effect of whichever analysis ran first, so whetherget_gene_meta()showed it depended on call order, andgene_interpolation()/STplot_interpolation()dropped any existing column and recomputed it.transform_data()now computes it for bothmethod='log'andmethod='sct', through the samecalculate_vst()helperSTclust()uses, so every caller sees one definition. Code that inspectedcolnames(get_gene_meta(x))will see one additional column, and code that counted onSTclust()being what created it no longer needs to.Note this does not change which genes any function selects:
STclust(),STgradient(),gene_interpolation()andSTdiff()already computed VST themselves before ranking, so theirtopgeneschoices were consistent before this change too. (An earlier draft of this entry claimed otherwise, and also cited a top-20 overlap betweengene_stdevsand VST rankings of 6; measured across the eight Visium samples of thetnbc_bassiouniset it is 7-13.) What changed is where the column comes from, not what downstream functions do with it.It does change column order, which matters if you index by position. Because
transform_data()now adds the column beforeSThet()appends its statistics,get_gene_meta()returns1.2.2: sample, gene, gene_mean, gene_stdevs, moran_i, geary_c, vst.variance.standardized 2.0.0: sample, gene, gene_mean, gene_stdevs, vst.variance.standardized, moran_i, geary_cThe same seven columns with the same contents, in a different order: VST used to arrive last, appended by whichever analysis ran first, and now sits ahead of
moran_i/geary_c. Code selecting by name is unaffected; code using[[5]],[, 5:7]ortail(colnames(...), 1)will pick a different column. Select by name. transform_data()no longer fails on a sample with very few variable genes. Because of the change above,transform_data()began fitting the variance-mean loess thatvst.variance.standardizedrequires. That fit needs at least 4 genes with non-zero variance, and below itstats::loessstops withspan is too small– so normalizing a very small or low-complexity STlist aborted, where in 1.2.2 it had succeeded. The count that matters is of non-constant genes, so a matrix with many genes of which only a few vary hit it as well. Such a sample now gets a warning naming it, and is left without the column rather than failing;calculate_vst()only computes when the column is absent, so a later analysis retries instead of inheriting a wrong value. Functions that rank genes by variability still raise an error on such a sample, since there is no ranking to return.dim()on anSTlistnow obeys the base R contract. It returnsc(n_genes, n_spots)as an integer vector when the object holds exactly one sample, andNULLfor a multi-sample object – because there is no single gene x spot shape to report. It previously returned a named list of per-sample dimensions, which madenrow()andncol()meaningless on an STlist and was reported byR CMD checkas “dim() returns class ‘list’”. Usesummarize_STlist()for a per-sample table of dimensions. Nothing inside the package calleddim()on an STlist.STlistobjects are now validated at construction.setValidity()rejects a malformed object up front: a sample-keyed slot referring to a sample that is not in@counts, a count matrix without gene/spot names, or an@spatial_metaentry whose row count disagrees with the number of spots. These previously surfaced much later, far from their cause.filter_data()reports what it did. Every count threshold defaults to0(and the*_max*thresholds toNULL), so a call supplying none of them filtered nothing – while looking like a successful filter. With the newverbose=TRUEdefault it now says explicitly that nothing was filtered, or how many samples, spots/cells and genes were removed.filter_data()remains single-threaded and deliberately has nocoresargument, unlike the analysis functions.Requesting a gene, annotation or reference domain that does not exist is now an error rather than a silent no-op.
SThet(genes=),STgradient(annot=),STgradient(ref=)andgene_interpolation(genes=)previously accepted a name matching nothing and returned without complaint, so a typo looked like a run that legitimately found nothing. The rule is now: error when nothing matches, and message naming what was dropped when only some does. Code that relied on the no-op will now stop; that reliance was almost certainly accidental.STenrich()now requiresannotanddomaintogether. Domain-restricted mode was guarded by!is.null(annot) & !is.null(domain), so supplying one without the other skipped the entire block: the unrestricted analysis ran and returned results a caller would reasonably read as domain-restricted, with nothing downstream able to detect the discrepancy. Supplying one alone is now an error. If you have results from 1.2.x produced by passingannotwithoutdomain, they are not domain-restricted and are worth regenerating.SThet()’s default (k=NULL) statistics moved by ~2e-15. The distance-weighted complete graph is no longer materialized as alistw; Moran’s I and Geary’s C are computed directly from the coordinates in C++.Szerois bit-identical, and the residual difference is summation order inside the weighted dot product – four orders of magnitude below the ~2e-2 spot-order sensitivity already documented forSThet(k=). The reason for the change is capacity: the oldlistwneeded 1.2 GB for a single 4,096-spot sample and about 181 GB extrapolated to 50,000 cells, i.e. it could not run on Xenium or CosMx data at all. It now completes in ~39 s using ~100 KB.@misc$sthet$listwsis no longer populated on this path;SThet(k=)still builds a realspdepobject.gene_stdevsin@gene_metamoved by ~1e-16. Per-gene standard deviations are computed without densifying the matrix, which changes the summation order. Nothing downstream consumesgene_stdevs–topgenesselection usesvst.variance.standardized– and cluster assignments, allvst.*columns and@tr_countswere verified bit-identical.A Seurat object’s platform is no longer assumed to be Visium.
STlist()hardcoded@misc$platform = 'visium'for any Seurat input; it is now inferred from the image class. This matters becauseplatformselectsSTgradient()’s defaultnb_dist_thrand branches inplot_image()andSTplot_interpolation(), so importing an imaging-based (Xenium/CosMx) Seurat object previously produced results computed with Visium-appropriate distance thresholds.pseudobulk_heatmap()displays a different set of genes, because it was selecting the wrong ones. The heatmap keeps thehm_display_genesmost variable genes, ranked by coefficient of variation, but the CV was computed upside down asmean/sdinstead ofsd/mean. Sorting that decreasing put the highestmean/sdfirst – that is, the lowest true CV – so the plot showed the most uniformly-expressed genes rather than the most variable ones, the exact opposite of the intent. Any heatmap produced by 1.2.x is showing the wrong gene set and is worth regenerating.cores=is now honored bySTlist()andtransform_data(). Both accepted the argument and then ignored it, so they always ran at the auto-detected core count no matter what was passed. Passingcores=now actually changes the number of forked workers. Two consequences: wall-clock times for these two functions will differ from 1.2.x for any explicitcores=, and because forked children re-seed their own RNG,transform_data(method='sct')is only reproducible atcores=1once a sample exceedssctransform::vst’s internal sampling threshold. Passcores=1where exact SCT reproducibility matters.-
Spot/cell ordering within an
STlistis now deterministic.process_lists()contained a coordinate sort that silently had no effect, so through 1.2.2 spots simply retained whatever order they appeared in in the input files – meaning two files describing the same tissue with rows in a different order produced differently-orderedSTlists. The sort now applies, so ordering depends on the coordinates themselves rather than on input file layout.Note what it sorts by: the third column of the coordinate data, whichever that is, not
xposspecifically. With the usuallibname,ypos,xposcolumn order that meansxpos, and because the sort is stable the result reads asxposthenypos. Supply the coordinate columns in a different order and the sort key changes with them.No function signatures, arguments, or return types changed. Counts, transformed counts, and the
vst.*gene-level statistics are bit-identical once matched by name. What changes for them is order: the column order of@counts/@tr_counts, the row order of@spatial_meta, and therefore the row order of results built from them. Code that addresses spots bylibnameis unaffected; code that indexes them positionally, or that depends on how ties break in anadj_p_value-sorted result, may need updating.Four results are not merely reordered, because they depend on spot order by construction rather than by layout. All four behaved this way in 1.2.x too – there, the order came from your input file, so the same tissue supplied with rows in a different order gave different numbers. Ordering is now determined by the coordinates, which removes that file-layout dependence, but the underlying sensitivity remains and is worth knowing about:
-
STenrich()p-values. The permutation null is drawn over spot positions, so a fixedseedselects a different random subset when the spots are in a different order. The observed statistic is unaffected; only the null, and thereforep_value/adj_p_value, move. -
SThet(k = )Moran’s I. Withkgiven, neighbours come from a k-nearest neighbour graph, and on a regular grid the k-th distance is tied between several spots; the tie is broken by position. On our test data Moran’s I moves by up to 0.02 – large enough to matter.SThet()withoutkuses a distance-based weight list and is unaffected (agreement to ~1e-15). -
STgradient(distsumm = 'avg')Spearman correlations. Averaging each spot’s distances to the reference domain produces exact ties on a regular grid, and a change in summation order of ~1e-16 is enough to flip one, which flips a rank. Correlations move by up to 0.004.distsumm = 'min'does not create the same ties. -
plot_counts()density curves, which plot a seeded randomdistrib_subsetof spots selected by position. Cosmetic, but the curve is not identical. The point jitter indistribution_plots(plot_type = 'violin')behaves the same way.
If you need a value that does not depend on spot order at all, prefer
SThet()withoutkandSTgradient(distsumm = 'min'), and raiserepsinSTenrich()so the permutation estimate is less sensitive to which subset is drawn. -
New features
STniche()andplot_stniche()for spatial niche detection.STniche()identifies tissue niches from gene-set scores, local Moran’s I and model-based clustering, on spot-based (Visium) and single-cell (CosMx) data; niche labels are shared across samples analysed together.plot_stniche()draws the results. The method, theplot_stniche()plot types and worked Visium and CosMx examples are in the vignette:vignette("vignette_stniche", package = "spatialGE").-
An
STlistcan now be subset by sample, and has a length.x[i]returns a new, validSTlistholding only the selected spatial arrays, andlength(x)reports how many it holds. Previously neither worked: subsetting failed with “object of type ‘S4’ is not subsettable”, andlength(x)returned the number of slots (7) rather than samples, soseq_along(x)andfor(i in seq_len(length(x)))silently did the wrong thing. To work with one sample out of a multi-sample object you had to rebuild anSTlistfrom the raw files.Select by name (
x[tissue_names(x)[1]]), position, negative position or logical vector. Every sample-keyed slot is subset alongside@counts–@spatial_meta,@gene_meta,@tr_counts, and the per-sample level inside each@gene_krigeelement – so the result passes validity, and@sample_metarows are matched to their sample rather than sliced positionally. Subsetting is deliberately one-dimensional: anSTlistholds several arrays with independent gene and spot sets, so there is no shared second dimension.x[i, j]andx[[i]]raise an error saying so. An unknown sample name is also an error, listing the available names, rather than a silent omission. Parallelism on Windows.
resolve_cores()returned1Lunconditionally on Windows andsge_lapply()forwarded only toparallel::mclapply, which has nofork()there – so the package was entirely serial. A non-forking (PSOCK) backend is now used on Windows, and can be forced anywhere withoptions(spatialGE.psock=TRUE).mclapplyremains the default wherefork()exists. ThespatialGE.cores/SPATIALGE_CORESoverride is also now consulted before any platform decision; it was previously ignored on Windows.Seurat v5 support, and conversion in both directions. Seurat input was v4 only: v5 stores counts in an
Assay5’s layers, so the old access returnedNULL, and v5’sVisiumV2/FOVimages carry no@coordinatesframe. Both are handled now, the assay defaults toDefaultAssay()rather than a hardcoded'Spatial', and the import is sparse-to-sparse instead of densifying every gene by every cell across all slices up front.as_Seurat()is new and provides the reverse conversion.Optional on-disk storage.
STlist_to_disk(x, root)moves count matrices into per-sample parquet files so data larger than memory can be held and read in gene-subset slices;STlist_to_memory()reverses it, exactly. Opt-in and off by default – an STlist built the normal way is unchanged.is_backed()andvalidate_store()support it. Note this buys capacity, not speed: aggregates still compute in memory, so a backed STlist is currently slower on those paths.Large-data performance. Several operations no longer densify sparse matrices. Measured on a 200-gene x 50,000-cell synthetic sample:
filter_data()27x faster, per-gene standard deviations 42x, and log transformation 33x (bit-identical output).STgradient()’s distance step no longer materializes the n x n distance matrix.stgradient_dists_one_sample()builtas.matrix(stats::dist(coords))– 934 MB allocated at 4,096 spots and about 20 GB at 50,000 cells, so the function could not run on Xenium or CosMx data at all. The distances are now computed in C++ in O(n) memory. Output is bit-identical for bothdistsumm='min'anddistsumm='avg'(the latter reproduces R’s two-pass correctedmean()), so no results change; measured 1.6x faster and about 3,100x less allocation at 4,096 spots, and 50,000 cells now completes instead of failing to allocate.STdiff()’st_testandwilcoxonpaths are 17-27x faster.stdiff_mean_test()re-filtered and re-selected the whole expression frame once per gene x cluster combination; the two groups depend only on the annotation, so the split is now computed with base indexing and the gene column is pulled directly. Output is unchanged – the samet.test/wilcox.testcalls on the same vectors. The defaultmmpath is not affected.Set the environment variable
SPATIALGE_CORES, or the optionspatialGE.cores, to force every parallelized call site to run serially and in-process without editing any code. Intended for profiling (profviscannot see insideparallel::mclapply’s forks) and for debugging a worker that fails only under forking. The option takes precedence over the environment variable. This is a development aid and is deliberately not part of the documented API.
Documentation
- New STniche vignette (
vignette_stniche), with Visium (GSE175540) and CosMx (ccRCC) case studies and a table of everyplot_stniche()plot type. - The basic functionality tutorial (
basic_functions_vignette) now has a section on loading data from other platforms: Xenium (as a Seurat object), CosMx-SMI, and the generic counts-plus-coordinates format. The other tutorials link to it and to the STniche vignette.
Bug fixes
STlistvalidity no longer rejects an object that has kriging results.setValidity()treated@gene_krigeas sample-keyed and compared its names to the sample names, but that slot is keyed by gene first and sample second (x@gene_krige[[gene]][[sample]]), so its top-level names are gene symbols. Every object returned bygene_interpolation()was therefore reported invalid – but only when validity actually ran, and assignment through@<-does not trigger it, so the function appeared to succeed and a latervalidObject()(or any construction from that object) failed. The sample level is now checked, one gene at a time.load_images()no longer drops samples whose image is not the first path match. Two defects combined: sample-to-file matching used a vectorized&where it needed a scalar&&, and the match was positional, so with more than one image directory or a sample whose name did not sort first the wrong file – or no file – was attached. Matching is now by normalized sample name, across every supplied directory and both extensions (.png/.jpg/.jpeg), with a decoder fallback when the file extension disagrees with its actual encoding. A sample whose image genuinely cannot be read is skipped with a warning rather than silently, and if no image matches at all the error names what was searched.STplot_interpolation()works on its documented default. With the defaultgenes=NULLit selected genes by VST but then intersected that selection with a kriging-derived gene list; because it also required the column to be present, the call failed on anySTlistthat had not already been throughgene_interpolation(). It now errors with a clear message when the object has no kriged genes, restricts to those that do exist (warning only when the user named genes explicitly), usescalculate_vst()rather than its own inline copy, and errors clearly when a color scale has no finite values.STdiff()explains an unusableannot=instead of failing inside dplyr. A numeric metadata column – a perfectly ordinary one, such astotal_counts– reached an internal join and producedCan't join `x$orig_annot` with `y$orig_annot` due to incompatible types. i `x$orig_annot` is a <double>, which names neither the function nor the argument to change.annotmust hold group labels, so a numeric column now raises an error namingSTdiff, the requirement and the fix (as.character()), as does a column with fewer than two distinct groups – which previously produced no usable comparison.transform_data(method='sct')reports an actionable error when SCTransform fails. On a small or low-complexity matrixsctransform::vst()failed withmissing value where TRUE/FALSE needed, giving no indication of which sample, how large it was, or what to change. The error now names the sample and its dimensions (n_genesx spots/cells) and suggests loweringsct_n_regr_genesor usingmethod='log', while still reporting the underlying message.A bad
cores=value is caught on every parallelized path. The gene-metadata step oftransform_data()never validated it, socores='abc'coerced toNAand was handed directly to the parallel lapply instead of raising an error. Validation is now centralized (see Minor), which fixes that path and removes the five duplicated checks elsewhere.pseudobulk_samples()no longer fails on a gene whose pseudobulk total is constant across samples. Such a gene produced an all-NaNcolumn after scaling andprcomp()rejected it outright, so any dataset containing an all-zero gene – routine after a permissivefilter_data()– could not be analysed. Those genes are now dropped with a warning.SThet()no longer fails when@spatial_metahas been row-subset with base[. Both spatial-weight builders fed the frame totibble::column_to_rownames(), which rejects non-contiguous row names, so the call died with an error naming neitherSThetnor the cause. This is the same defect that madeSTenrich()’sdomain=path unreachable through 1.2.2, in two call sites that fix did not cover.STenrich()no longer crashes on a gene set with exactly one gene present. Subsetting dropped the matrix to a dimensionless vector, whichDelayedMatrixStats::colMeans2()rejects – even thoughmin_genes=1explicitly permits single-gene sets.Seurat_FindVariableFeatures()no longer fails on any input that is not already adgCMatrix. It calledas.sparse(), which isSeuratObject::as.sparse()and was never imported, so the call had been unresolvable since the code was vendored – masked because every caller happened to pass adgCMatrix, and because the name was listed inutils::globalVariables(), suppressing the check NOTE that would have caught it.STlist()accepts a Seurat object withsamples=NULL. The sample-name parsing tested an argument derived fromsamplesbefore reaching the Seurat branch, so the normal way to call it raised “argument is of length zero”.filter_data()can now produce a sample with exactly one surviving spot. The dense intermediate collapsed to a vector at one column, which then broke row-name handling.transform_data(),SThet(),STenrich()andgene_interpolation()report invalid input as spatialGE errors naming the function and argument, instead of surfacing internal failures such asobject 'tr_results' not foundorattempt to select less than one element in get1index.STenrich(annot=, domain=)now works. Domain-restricted gene set testing was announced in 1.2.1 but never functioned: restricting to a domain subsets the spatial metadata rows, which leaves stray row names thattibble::column_to_rownames()rejects, so the call always failed with`.data` must be a data frame without row names.Every domain-restricted call in 1.2.1 and 1.2.2 errored out. The unrestricted andscore_type='gsva'paths were unaffected.All 14 documented function examples now run. A doubled roxygen comment marker (
#' #' zip_tmp = ...) commented out the assignment that the followingunzip()call depended on, so every example failed withobject 'zip_tmp' not found. Each example’stryCatchreported this as “Could not run example. Are you connected to the internet?”, misattributing a code defect to the network, whileR CMD checkstill reportedchecking examples ... OK.STgradient()no longer leaks state between genes.pval_warnwas only assigned inside a branch that requires more than one spot in the fitted range, so a gene that fell below that threshold left the previous gene’s value in place. Affects thepval_commentcolumn only..github/is no longer shipped inside the package tarball (added to.Rbuildignore).
Minor
User-facing validation errors are now raised through the shared error catalog, so they all carry the
spatialGE exception:signature. About thirty sites used a barestop(), which meant an argument-validation failure looked no different from an internal crash; and three sites wrote thespatialGE exception:prefix by hand instead of using the catalog, so their wording could drift from it. Their text is unchanged in substance but now lives ininst/err.csv, andraise_err()gained optionalsprintf-style interpolation so messages that embed a gene name, sample size or threshold can be catalogued too. Code matching on the exact wording of these messages may need updating; matching onspatialGE exception:(the documented pattern) is unaffected.An unusable
cores=value now fails the same way everywhere. Validation of a core count used to be each call site’s job, and was done three different ways: some sites raisederror0032, otherserror0036, five used an uncatalogued barestop(), andtransform_data()’s gene-metadata path did not check at all – socores='abc'there becameNAand was passed straight into the parallel lapply. Resolution now happens once inresolve_cores(), which rejects a value that is not one finite positive number with an error naming the fix. The caller’s coercion is preserved:coerce='ceiling'andcoerce='none'still return a non-integral value unchanged.The error raised when an argument that should be an
STlistisNULLor of the wrong type now readscannot get a slot (...) from an object of type ...rather thanno applicable method for `@` applied to an object of class ..., because the shared argument-resolution helper reads slots viamethods::slot(). Both messages are raw R errors and neither is informative; nothing is caught or handled differently.STdiff()no longer emits dplyr’s “Detected an unexpected many-to-many relationship” warning under dplyr 1.1 and later. The gene x annotation table it builds is deliberately many-to-many – every gene of a sample combines with every annotation of that sample – but it was declared with the deprecatedmultiple='all', which newer dplyr checks separately fromrelationship=and therefore ignores. The join now declaresrelationship='many-to-many'. The joined rows, and every result built from them, are unchanged.
spatialGE 1.2.1
CRAN release: 2025-05-26
- Added C++ code to speed up
STenrichcomputations (Thank you, Dr. Soupir!). - The
STenrichfunction can now calculate gene set enrichment scores (viaGSVA) in addition to gene set average expression. - The
STenrichfunction can now test for gene set “hot-spots” within an specific tissue domain (annotanddomainarguments). - Spatial distances can now be log-transformed for
STgradient(logargument). - Example data sets are now in the spatialGE_Data GitHub repository.
- New functions
spatial_metadataandtissue_namesto quickly access the names of spot/cell annotations and sample names. - Re-assessed package dependencies.
- Use of
DelayedArrayfor some calculations. - Several bug fixes.
