spatialquery.js 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/dist/assert.d.ts +15 -0
- package/dist/assert.d.ts.map +1 -0
- package/dist/assert.js +23 -0
- package/dist/assert.js.map +1 -0
- package/dist/bundled/index.js +14245 -0
- package/dist/bundled/index.js.map +1 -0
- package/dist/fpgrowth.d.ts +10 -0
- package/dist/fpgrowth.d.ts.map +1 -0
- package/dist/fpgrowth.js +35 -0
- package/dist/fpgrowth.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/multi-fov.d.ts +115 -0
- package/dist/multi-fov.d.ts.map +1 -0
- package/dist/multi-fov.js +325 -0
- package/dist/multi-fov.js.map +1 -0
- package/dist/nested.d.ts +17 -0
- package/dist/nested.d.ts.map +1 -0
- package/dist/nested.js +106 -0
- package/dist/nested.js.map +1 -0
- package/dist/single-fov.d.ts +149 -0
- package/dist/single-fov.d.ts.map +1 -0
- package/dist/single-fov.js +488 -0
- package/dist/single-fov.js.map +1 -0
- package/dist/stats.d.ts +25 -0
- package/dist/stats.d.ts.map +1 -0
- package/dist/stats.js +141 -0
- package/dist/stats.js.map +1 -0
- package/package.json +41 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Keller
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,170 @@
|
|
|
1
|
-
#
|
|
1
|
+
# spatialquery.js
|
|
2
|
+
JavaScript implementation of [Spatial-Query](https://github.com/ShaokunAn/Spatial-Query).
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
|
|
5
|
+
## Citation
|
|
6
|
+
|
|
7
|
+
If you use SpatialQuery in your research, please cite the [preprint](https://doi.org/10.64898/2026.04.22.720136):
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
> Shaokun An, Mark Keller, Nils Gehlenborg, Martin Hemberg. **SpatialQuery: scalable discovery and molecular characterization of multicellular motifs from spatial omics data.** bioRxiv. doi: 10.64898/2026.04.22.720136
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install spatialquery.js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
A build with all dependencies bundled in is available at `spatialquery.js/bundled` (e.g., for use from a CDN without import maps):
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { buildContext } from 'spatialquery.js/bundled';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Single field of view
|
|
28
|
+
|
|
29
|
+
This example loads an AnnData object from a Zarr store using [zarrita](https://github.com/manzt/zarrita.js) and [anndata.js](https://github.com/ilan-gold/anndata.js) (`npm install zarrita anndata.js`).
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import * as zarr from 'zarrita';
|
|
33
|
+
import { readZarr, get } from 'anndata.js';
|
|
34
|
+
import {
|
|
35
|
+
buildContext,
|
|
36
|
+
findPatternsGrid,
|
|
37
|
+
motifEnrichmentDist,
|
|
38
|
+
motifCompositionDist,
|
|
39
|
+
} from 'spatialquery.js';
|
|
40
|
+
|
|
41
|
+
// HuBMAP kidney dataset (AnnData-Zarr).
|
|
42
|
+
const url = 'https://assets.hubmapconsortium.org/0a21f3fa27109790483f2a0729be53de/hubmap_ui/anndata-zarr/secondary_analysis.zarr';
|
|
43
|
+
const store = await zarr.withMaybeConsolidatedMetadata(new zarr.FetchStore(url));
|
|
44
|
+
const adata = await readZarr(store);
|
|
45
|
+
|
|
46
|
+
// Cell IDs.
|
|
47
|
+
const obsIndex = Array.from((await get(await adata.obsNames(), [null])).data);
|
|
48
|
+
|
|
49
|
+
// Cell type label of each cell (a categorical obs column).
|
|
50
|
+
const predictedLabel = await adata.obs.get('predicted_label');
|
|
51
|
+
const codes = (await get(predictedLabel.codes, [null])).data;
|
|
52
|
+
const categories = (await get(predictedLabel.categories, [null])).data;
|
|
53
|
+
const labels = Array.from(codes, code => categories[code]);
|
|
54
|
+
|
|
55
|
+
// Spatial coordinates (an n_obs x 2 obsm array).
|
|
56
|
+
const spatial = await adata.obsm.get('X_spatial');
|
|
57
|
+
const xCoords = Float64Array.from((await get(spatial, [null, 0])).data);
|
|
58
|
+
const yCoords = Float64Array.from((await get(spatial, [null, 1])).data);
|
|
59
|
+
|
|
60
|
+
const obsIdToLabel = new Map(obsIndex.map((obsId, i) => [obsId, labels[i]]));
|
|
61
|
+
|
|
62
|
+
const context = buildContext({
|
|
63
|
+
obsIndex, // string[] of cell IDs
|
|
64
|
+
xCoords, // Float32Array | Float64Array
|
|
65
|
+
yCoords, // Float32Array | Float64Array
|
|
66
|
+
obsIdToObsSetName: obsId => obsIdToLabel.get(obsId),
|
|
67
|
+
// By default, coordinates are normalized so the mean nearest-neighbor distance is 1.
|
|
68
|
+
normalizeSpatialCoord: true,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Frequent cell type patterns around grid points.
|
|
72
|
+
const patterns = await findPatternsGrid(context, { maxDist: 20, minSupport: 0.5 });
|
|
73
|
+
// => [{ itemsets: ['epithelial cell of proximal tubule segment 1', ...], support: 0.54 }, ...]
|
|
74
|
+
|
|
75
|
+
// Motifs enriched around a center cell type (k-NN version: motifEnrichmentKnn).
|
|
76
|
+
const ct = 'kidney loop of Henle cortical thick ascending limb epithelial cell';
|
|
77
|
+
const enrichment = await motifEnrichmentDist(context, {
|
|
78
|
+
ct,
|
|
79
|
+
maxDist: 20,
|
|
80
|
+
returnCellIds: true, // adds centerIds / neighborIds (obs IDs) to each result
|
|
81
|
+
});
|
|
82
|
+
// => [{ center, motifs, nCenterMotif, nCenter, nMotif, expectation, pValue, adjPValue, ifSignificant, ... }]
|
|
83
|
+
|
|
84
|
+
// Composition of a motif in the neighborhoods of the center cells.
|
|
85
|
+
const composition = motifCompositionDist(context, {
|
|
86
|
+
ct,
|
|
87
|
+
motifs: enrichment[0].motifs,
|
|
88
|
+
maxDist: 20,
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Other functions: `findPatternsRand`, `findFpKnn`, `findFpDist`, `motifEnrichmentKnn`, `motifCompositionKnn`.
|
|
93
|
+
The enrichment functions also accept `motifs` (to test specific motifs) and `mode: 'nested'` (to also test sub-patterns of non-significant motifs).
|
|
94
|
+
|
|
95
|
+
### Multiple fields of view
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
import {
|
|
99
|
+
buildMultiContext,
|
|
100
|
+
multiMotifEnrichmentKnn,
|
|
101
|
+
differentialAnalysisKnn,
|
|
102
|
+
} from 'spatialquery.js';
|
|
103
|
+
|
|
104
|
+
// One entry per FOV, with a unique dataset ID and a class (e.g., condition) shared by related FOVs.
|
|
105
|
+
const multi = buildMultiContext([
|
|
106
|
+
{ datasetId: 'sample-1', datasetClass: 'control', obsIndex: obsIndex1, xCoords: x1, yCoords: y1, obsIdToObsSetName: toLabel1 },
|
|
107
|
+
{ datasetId: 'sample-2', datasetClass: 'control', obsIndex: obsIndex2, xCoords: x2, yCoords: y2, obsIdToObsSetName: toLabel2 },
|
|
108
|
+
{ datasetId: 'sample-3', datasetClass: 'disease', obsIndex: obsIndex3, xCoords: x3, yCoords: y3, obsIdToObsSetName: toLabel3 },
|
|
109
|
+
// ...
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
// Enrichment pooled across the FOVs of the selected classes (all FOVs by default).
|
|
113
|
+
// With returnCellIds: true, cell IDs are keyed by dataset ID.
|
|
114
|
+
const enrichment = await multiMotifEnrichmentKnn(multi, { ct: 'T cell', k: 30, maxDist: 20, datasetClass: 'disease' });
|
|
115
|
+
|
|
116
|
+
// Motifs whose support differs significantly between the FOVs of two classes.
|
|
117
|
+
const differential = await differentialAnalysisKnn(multi, {
|
|
118
|
+
ct: 'T cell',
|
|
119
|
+
datasetClasses: ['control', 'disease'],
|
|
120
|
+
});
|
|
121
|
+
// => { control: [{ itemsets, supportMean, adjPValue }, ...], disease: [...] }
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Other functions: `multiFindFpKnn`, `multiFindFpDist`, `findFpKnnFov` / `findFpDistFov` (one FOV, by `datasetId`), `multiMotifEnrichmentDist`, `multiMotifCompositionKnn`, `multiMotifCompositionDist`, `differentialAnalysisDist`, and `differentialTestsKnn` / `differentialTestsDist` (all tested motifs, not only the significant ones).
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
### Setup
|
|
130
|
+
|
|
131
|
+
```sh
|
|
132
|
+
pnpm install
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Download the test fixture files and run the Python fixture conversion/generation scripts:
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
cd fixtures
|
|
139
|
+
# The HuBMAP assets server rejects curl's default User-Agent.
|
|
140
|
+
curl -fL -A "Mozilla/5.0" -o secondary_analysis.h5ad https://assets.hubmapconsortium.org/0a21f3fa27109790483f2a0729be53de/secondary_analysis.h5ad
|
|
141
|
+
CC=/usr/bin/clang CXX=/usr/bin/clang++ uv run test_find_patterns_grid.py
|
|
142
|
+
CC=/usr/bin/clang CXX=/usr/bin/clang++ uv run generate_motif_fixtures.py
|
|
143
|
+
CC=/usr/bin/clang CXX=/usr/bin/clang++ uv run generate_multi_fov_fixtures.py
|
|
144
|
+
cd ..
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Testing
|
|
148
|
+
|
|
149
|
+
The tests compare the JS outputs to Python reference outputs. See [fixtures/README.md](fixtures/README.md) to download the test data and generate the reference outputs, then:
|
|
150
|
+
|
|
151
|
+
```sh
|
|
152
|
+
pnpm test
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Publish to NPM
|
|
156
|
+
|
|
157
|
+
```sh
|
|
158
|
+
npm login
|
|
159
|
+
pnpm publish --no-git-checks --access public
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Related work
|
|
163
|
+
|
|
164
|
+
Related to:
|
|
165
|
+
- Initial implementation work in [vitessce#2116](https://github.com/vitessce/vitessce/pull/2116/).
|
|
166
|
+
- Widget plugin in [vitessce-python](https://github.com/vitessce/vitessce-python/blob/main/src/vitessce/widget_plugins/spatial_query.py) ([example notebook](https://github.com/vitessce/vitessce-python/blob/main/docs/notebooks/widget_plugin_spatial-query.ipynb))
|
|
167
|
+
|
|
168
|
+
## AI usage disclosure
|
|
169
|
+
|
|
170
|
+
The initial package structure and initial porting efforts were performed by a human. Then, Claude Code was used to port additional functions from Python to TypeScript, and to assist in generation of unit tests to compare JavaScript outputs to the ground-truth Python outputs.
|
package/dist/assert.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an assertion fails.
|
|
3
|
+
*/
|
|
4
|
+
export declare class AssertionError extends Error {
|
|
5
|
+
/** @param message The error message. */
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Make an assertion. An error is thrown if `expr` does not have truthy value.
|
|
10
|
+
*
|
|
11
|
+
* @param expr The expression to test.
|
|
12
|
+
* @param msg The message to display if the assertion fails.
|
|
13
|
+
*/
|
|
14
|
+
export declare function assert(expr: unknown, msg?: string): asserts expr;
|
|
15
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,wCAAwC;gBAC5B,OAAO,EAAE,MAAM;CAI5B;AAGD;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,SAAK,GAAG,OAAO,CAAC,IAAI,CAI5D"}
|
package/dist/assert.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Reference: https://github.com/hms-dbmi/vizarr/blob/82756a8604b1612aa35bb8a292c1451ee9647b70/src/utils.ts#L434
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when an assertion fails.
|
|
4
|
+
*/
|
|
5
|
+
export class AssertionError extends Error {
|
|
6
|
+
/** @param message The error message. */
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "AssertionError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Make an assertion. An error is thrown if `expr` does not have truthy value.
|
|
14
|
+
*
|
|
15
|
+
* @param expr The expression to test.
|
|
16
|
+
* @param msg The message to display if the assertion fails.
|
|
17
|
+
*/
|
|
18
|
+
export function assert(expr, msg = "") {
|
|
19
|
+
if (!expr) {
|
|
20
|
+
throw new AssertionError(msg);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IACvC,wCAAwC;IACxC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF;AAGD;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,IAAa,EAAE,GAAG,GAAG,EAAE;IAC5C,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,cAAc,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;AACH,CAAC"}
|