@vitessce/comparative 4.1.2 → 4.2.0

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.
Files changed (55) hide show
  1. package/dist/{GeometryAndMeshXR-CMf0dkQy.js → GeometryAndMeshXR-CYO2tqUV.js} +1 -1
  2. package/dist/ReactNeuroglancer-pk3hhe45.js +68657 -0
  3. package/dist/{XREnterButton-DpxW774d.js → XREnterButton-BdEVWrV7.js} +1 -1
  4. package/dist/{XRSceneComponents-C-VbCXlG.js → XRSceneComponents-DYeS3CwE.js} +1 -1
  5. package/dist/{XRWrapper-CoSbWhS_.js → XRWrapper-DbheKuSQ.js} +2 -2
  6. package/dist/{deflate-SFB4ekzB.js → deflate-D76idIbp.js} +1 -1
  7. package/dist/{higlass-D5qxYkfx.js → higlass-HE5ijNPv.js} +1 -1
  8. package/dist/{index-Dhz9cE6Y.js → index-CSfNzXQa.js} +126748 -124747
  9. package/dist/index-DOITJzp5.js +30089 -0
  10. package/dist/{index-iUzEaM0X.js → index-DeGz6Kci.js} +1 -1
  11. package/dist/{index-CPs7SJQa.js → index-YJLqH5aL.js} +4 -4
  12. package/dist/index.min.js +20 -20
  13. package/dist/{jpeg-X9rWVd_G.js → jpeg-CtV9axAi.js} +1 -1
  14. package/dist/{lerc-C1sD5A4o.js → lerc-C2iyBV3P.js} +1 -1
  15. package/dist/{lzw-8I9_xt6F.js → lzw-BDK3wuTu.js} +1 -1
  16. package/dist/{packbits-C2cIjHBj.js → packbits-BmhmFJpE.js} +1 -1
  17. package/dist/{raw-D7AJ-PIv.js → raw-AV6N1HZT.js} +1 -1
  18. package/dist/{webimage-DmbCXIUf.js → webimage-D8fd9G2X.js} +1 -1
  19. package/dist/{xrStore-CHRZYO3f.js → xrStore-B_SjnbWm.js} +1 -1
  20. package/dist-tsc/BiomarkerSelect.d.ts +19 -1
  21. package/dist-tsc/BiomarkerSelect.d.ts.map +1 -1
  22. package/dist-tsc/BiomarkerSelect.js +46 -39
  23. package/dist-tsc/BiomarkerSelect.js.map +1 -1
  24. package/dist-tsc/ComparativeConfig.d.ts +112 -3
  25. package/dist-tsc/ComparativeConfig.d.ts.map +1 -1
  26. package/dist-tsc/ComparativeConfig.js +255 -13
  27. package/dist-tsc/ComparativeConfig.js.map +1 -1
  28. package/dist-tsc/ComparativeConfig.test.d.ts +2 -0
  29. package/dist-tsc/ComparativeConfig.test.d.ts.map +1 -0
  30. package/dist-tsc/ComparativeConfig.test.js +119 -0
  31. package/dist-tsc/ComparativeConfig.test.js.map +1 -0
  32. package/dist-tsc/ControlledComparative.d.ts +13 -3
  33. package/dist-tsc/ControlledComparative.d.ts.map +1 -1
  34. package/dist-tsc/ControlledComparative.js +32 -14
  35. package/dist-tsc/ControlledComparative.js.map +1 -1
  36. package/dist-tsc/UncontrolledComparative.d.ts.map +1 -1
  37. package/dist-tsc/UncontrolledComparative.js +39 -12
  38. package/dist-tsc/UncontrolledComparative.js.map +1 -1
  39. package/dist-tsc/url-params.d.ts +24 -0
  40. package/dist-tsc/url-params.d.ts.map +1 -0
  41. package/dist-tsc/url-params.js +32 -0
  42. package/dist-tsc/url-params.js.map +1 -0
  43. package/dist-tsc/url-params.test.d.ts +2 -0
  44. package/dist-tsc/url-params.test.d.ts.map +1 -0
  45. package/dist-tsc/url-params.test.js +18 -0
  46. package/dist-tsc/url-params.test.js.map +1 -0
  47. package/package.json +15 -13
  48. package/src/BiomarkerSelect.jsx +70 -45
  49. package/src/ComparativeConfig.jsx +295 -13
  50. package/src/ComparativeConfig.test.jsx +151 -0
  51. package/src/ControlledComparative.jsx +47 -13
  52. package/src/UncontrolledComparative.jsx +62 -16
  53. package/src/url-params.js +34 -0
  54. package/src/url-params.test.js +20 -0
  55. package/dist/ReactNeuroglancer-DzBGJFIe.js +0 -43644
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable max-len */
2
2
  /* eslint-disable camelcase */
3
3
  /* eslint-disable no-unused-vars */
4
+ /* eslint-disable no-underscore-dangle */
4
5
  /* eslint-disable react-refresh/only-export-components */
5
6
  /* eslint-disable jsx-a11y/anchor-is-valid */ // Remove once the "Show analysis details" dropdowns are implemented.
6
7
  import React, { useState } from 'react';
@@ -13,19 +14,244 @@ import {
13
14
  import { usePageModeView } from '@vitessce/vit-s';
14
15
  import Sticky from 'react-sticky-el';
15
16
  import clsx from 'clsx';
17
+ import { isEqual } from 'lodash-es';
16
18
  import { Tabs, Tab, Link } from '@vitessce/styles';
17
19
 
20
+ // The coordination scope name used for the `dataset` coordination type,
21
+ // which is shared by all views, so that the currently-viewed
22
+ // dataset (i.e., data modality) can be switched.
23
+ export const COMPARATIVE_DATASET_SCOPE = '__comparison__';
24
+
25
+ // The uid of the biomarkerSelectAlt view.
26
+ export const BIOMARKER_SELECT_VIEW_UID = 'biomarker-select';
27
+
28
+ // Supported data modalities. Each modality corresponds to one dataset
29
+ // in the generated view config (with the modality `uid` as the dataset uid).
30
+ export const COMPARATIVE_MODALITIES = [
31
+ {
32
+ uid: 'sc-rna-seq',
33
+ name: 'Single-cell RNA sequencing',
34
+ urlKey: 'singleCellDatasetUrl',
35
+ // TODO: this is a hack. adjust the Densmap extent in the pipeline itself
36
+ circleScaleFactor: 0.8,
37
+ },
38
+ {
39
+ uid: 'sn-rna-seq',
40
+ name: 'Single-nucleus RNA sequencing',
41
+ urlKey: 'singleNucleusDatasetUrl',
42
+ circleScaleFactor: 0.5,
43
+ },
44
+ ];
45
+
18
46
  /**
19
- * Generate a Vitessce config for comparative visualization.
20
- * If isBiomarkerSelectOnly is true, only the biomarkerSelect view is included.
21
- * Otherwise, a config without the biomarkerSelect view is generated.
22
- * @param {string} baseUrl
23
- * @param {boolean} isBiomarkerSelectOnly
24
- * @returns
47
+ * Get the list of modalities for which a dataset URL was provided.
48
+ * @param {object} datasetUrls
49
+ * @param {string|undefined} datasetUrls.singleCellDatasetUrl
50
+ * @param {string|undefined} datasetUrls.singleNucleusDatasetUrl
51
+ * @returns {{ uid: string, name: string, url: string, circleScaleFactor: number }[]}
25
52
  */
26
- export function generateComparativeConfig(baseUrl, isBiomarkerSelectOnly) {
27
- const vc = new VitessceConfig({ schemaVersion: '1.0.16', name: 'Lake et al.' });
28
- const dataset = vc.addDataset('lake_et_al').addFile({
53
+ export function getAvailableModalities(datasetUrls) {
54
+ return COMPARATIVE_MODALITIES
55
+ .filter(m => typeof datasetUrls?.[m.urlKey] === 'string' && datasetUrls[m.urlKey].length > 0)
56
+ .map(m => ({ ...m, url: datasetUrls[m.urlKey] }));
57
+ }
58
+
59
+ /**
60
+ * Resolve the modality to view, falling back to the first available modality
61
+ * if the requested one is not available.
62
+ * @param {string[]} availableModalityUids
63
+ * @param {string|null|undefined} requestedModality
64
+ * @returns {string|undefined}
65
+ */
66
+ export function resolveSelectedModality(availableModalityUids, requestedModality) {
67
+ if (availableModalityUids.includes(requestedModality)) {
68
+ return requestedModality;
69
+ }
70
+ return availableModalityUids[0];
71
+ }
72
+
73
+ /**
74
+ * Get the currently-selected modality (dataset uid) from a view config.
75
+ * @param {object|null} config
76
+ * @returns {string|undefined}
77
+ */
78
+ export function getSelectedModality(config) {
79
+ return config?.coordinationSpace?.dataset?.[COMPARATIVE_DATASET_SCOPE];
80
+ }
81
+
82
+ /**
83
+ * Return a new view config in which the given modality is selected.
84
+ * Upon a change of modality, all obsSetSelection values are reset to null,
85
+ * so that the selected cell types become re-initialized
86
+ * (since the new modality may contain different cell type categories).
87
+ * @param {object} config A view config generated by generateComparativeConfig.
88
+ * @param {string} modality The modality (dataset uid) to select.
89
+ * @returns {object} The updated view config (or the original
90
+ * config if the modality is not present in the config
91
+ * or is already selected).
92
+ */
93
+ export function withSelectedModality(config, modality) {
94
+ const hasDataset = config?.datasets?.some(d => d.uid === modality);
95
+ if (!hasDataset || getSelectedModality(config) === modality) {
96
+ return config;
97
+ }
98
+ const modalityInfo = COMPARATIVE_MODALITIES.find(m => m.uid === modality);
99
+ const prevObsSetSelection = config.coordinationSpace?.obsSetSelection;
100
+ return {
101
+ ...config,
102
+ coordinationSpace: {
103
+ ...config.coordinationSpace,
104
+ ...(prevObsSetSelection ? {
105
+ obsSetSelection: Object.fromEntries(
106
+ Object.keys(prevObsSetSelection).map(scope => [scope, null]),
107
+ ),
108
+ } : {}),
109
+ dataset: {
110
+ ...config.coordinationSpace?.dataset,
111
+ [COMPARATIVE_DATASET_SCOPE]: modality,
112
+ },
113
+ },
114
+ layout: config.layout.map(view => (
115
+ (view.uid === 'scatterplot' && modalityInfo)
116
+ ? { ...view, props: { ...view.props, circleScaleFactor: modalityInfo.circleScaleFactor } }
117
+ : view
118
+ )),
119
+ };
120
+ }
121
+
122
+ /**
123
+ * Get the biomarkerSelection value from the props of the biomarkerSelectAlt view.
124
+ * @param {object|null} config
125
+ * @returns {{ label: string, nodeType: string } | null | undefined}
126
+ */
127
+ export function getBiomarkerSelection(config) {
128
+ return config?.layout?.find(v => v.uid === BIOMARKER_SELECT_VIEW_UID)?.props?.biomarkerSelection;
129
+ }
130
+
131
+ /**
132
+ * Return a new view config in which the biomarkerSelectAlt view
133
+ * has the given biomarkerSelection prop value.
134
+ * @param {object} config A view config generated by generateComparativeConfig.
135
+ * @param {{ label: string, nodeType: string } | null} biomarkerSelection
136
+ * @returns {object} The updated view config (or the original config if unchanged).
137
+ */
138
+ export function withBiomarkerSelection(config, biomarkerSelection) {
139
+ if (isEqual(getBiomarkerSelection(config), biomarkerSelection)) {
140
+ return config;
141
+ }
142
+ return {
143
+ ...config,
144
+ layout: config.layout.map(view => (
145
+ view.uid === BIOMARKER_SELECT_VIEW_UID
146
+ ? { ...view, props: { ...view.props, biomarkerSelection } }
147
+ : view
148
+ )),
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Check whether a value is a valid gene selection (or null, representing a cleared selection).
154
+ * @param {any} value
155
+ * @returns {boolean}
156
+ */
157
+ function isValidGeneSelection(value) {
158
+ return value === null || (Array.isArray(value) && value.every(g => typeof g === 'string'));
159
+ }
160
+
161
+ /**
162
+ * Check whether a value is a valid sample set selection (or null, representing a cleared selection).
163
+ * @param {any} value
164
+ * @returns {boolean}
165
+ */
166
+ function isValidSampleSetSelection(value) {
167
+ return value === null || (
168
+ Array.isArray(value) && value.every(s => Array.isArray(s) && s.every(t => typeof t === 'string'))
169
+ );
170
+ }
171
+
172
+ /**
173
+ * Check whether a value is a valid biomarkerSelection (or null, representing a cleared selection).
174
+ * @param {any} value
175
+ * @returns {boolean}
176
+ */
177
+ function isValidBiomarkerSelection(value) {
178
+ return value === null || (
179
+ typeof value?.label === 'string' && typeof value?.nodeType === 'string'
180
+ );
181
+ }
182
+
183
+ /**
184
+ * Merge the selections from outside Vitessce (e.g., from URL params)
185
+ * into the view config for the biomarkerSelectAlt view.
186
+ * @param {object} params
187
+ * @param {object|null} params.currentConfig The most recent config emitted via onConfigChange
188
+ * (or null prior to the first onConfigChange).
189
+ * @param {object} params.initialConfig The generated initial config.
190
+ * @param {string[]|null} params.geneSelection
191
+ * @param {string[][]|null} params.sampleSetSelection
192
+ * @param {{ label: string, nodeType: string } | null} params.biomarkerSelection
193
+ * @param {string} params.selectedModality
194
+ * @param {() => string} params.createUid Function that returns a new unique config uid.
195
+ * @returns {object} The merged config. Returns currentConfig itself
196
+ * if it already reflects all of the outside selections.
197
+ */
198
+ export function mergeBiomarkerSelectConfig({
199
+ currentConfig,
200
+ initialConfig,
201
+ geneSelection,
202
+ sampleSetSelection,
203
+ biomarkerSelection,
204
+ selectedModality,
205
+ createUid,
206
+ }) {
207
+ // Prior to the first onConfigChange, use the initial config as the base,
208
+ // so that values from outside Vitessce (e.g., URL params on initial page load)
209
+ // are present from the very first render.
210
+ const baseConfig = currentConfig ?? initialConfig;
211
+ const prevGeneSelection = baseConfig.coordinationSpace?.featureSelection?.__comparison__;
212
+ const prevSampleSetSelection = baseConfig.coordinationSpace?.sampleSetSelection?.__comparison__;
213
+ const prevBiomarkerSelection = getBiomarkerSelection(baseConfig);
214
+
215
+ const nextGeneSelection = isValidGeneSelection(geneSelection) ? geneSelection : prevGeneSelection;
216
+ const nextSampleSetSelection = isValidSampleSetSelection(sampleSetSelection) ? sampleSetSelection : prevSampleSetSelection;
217
+ const nextBiomarkerSelection = isValidBiomarkerSelection(biomarkerSelection) ? biomarkerSelection : prevBiomarkerSelection;
218
+
219
+ if (currentConfig
220
+ && isEqual(prevGeneSelection, nextGeneSelection)
221
+ && isEqual(prevSampleSetSelection, nextSampleSetSelection)
222
+ && isEqual(prevBiomarkerSelection, nextBiomarkerSelection)
223
+ && getSelectedModality(currentConfig) === selectedModality
224
+ ) {
225
+ return currentConfig;
226
+ }
227
+
228
+ return withSelectedModality(withBiomarkerSelection({
229
+ ...baseConfig,
230
+ // After initialization, the config contains a uid, which Vitessce uses to detect
231
+ // changes to the config prop. Therefore, a new uid is required for Vitessce to
232
+ // apply changes that originated outside (e.g., a gene selected in ControlledComparative).
233
+ ...(currentConfig ? { uid: createUid() } : {}),
234
+ coordinationSpace: {
235
+ ...baseConfig.coordinationSpace,
236
+ featureSelection: {
237
+ ...baseConfig.coordinationSpace?.featureSelection,
238
+ __comparison__: nextGeneSelection,
239
+ },
240
+ sampleSetSelection: {
241
+ ...baseConfig.coordinationSpace?.sampleSetSelection,
242
+ __comparison__: nextSampleSetSelection,
243
+ },
244
+ },
245
+ }, nextBiomarkerSelection), selectedModality);
246
+ }
247
+
248
+ /**
249
+ * Add the files for a single comparative AnnData-Zarr store to a dataset.
250
+ * @param {object} dataset A VitessceConfigDataset instance.
251
+ * @param {string} baseUrl URL to the root of the AnnData-Zarr store.
252
+ */
253
+ function addComparativeFiles(dataset, baseUrl) {
254
+ dataset.addFile({
29
255
  fileType: 'comparisonMetadata.anndata.zarr',
30
256
  url: baseUrl,
31
257
  options: {
@@ -204,6 +430,51 @@ export function generateComparativeConfig(baseUrl, isBiomarkerSelectOnly) {
204
430
  sampleType: 'sample',
205
431
  },
206
432
  });
433
+ }
434
+
435
+ /**
436
+ * Generate a Vitessce config for comparative visualization.
437
+ * One dataset is defined per data modality (single-cell and/or single-nucleus),
438
+ * and all views are linked to a shared `dataset` coordination scope
439
+ * so that the currently-viewed modality can be switched.
440
+ * If isBiomarkerSelectOnly is true, only the biomarkerSelect view is included.
441
+ * Otherwise, a config without the biomarkerSelect view is generated.
442
+ * @param {object} datasetUrls
443
+ * @param {string|undefined} datasetUrls.singleCellDatasetUrl URL to the
444
+ * single-cell AnnData-Zarr store.
445
+ * @param {string|undefined} datasetUrls.singleNucleusDatasetUrl URL to the
446
+ * single-nucleus AnnData-Zarr store.
447
+ * @param {boolean} isBiomarkerSelectOnly
448
+ * @param {string|undefined} selectedModality The modality to view initially.
449
+ * By default, the first available modality.
450
+ * @returns {object} The view config JSON.
451
+ */
452
+ export function generateComparativeConfig(datasetUrls, isBiomarkerSelectOnly, selectedModality = undefined) {
453
+ const modalities = getAvailableModalities(datasetUrls);
454
+ if (modalities.length === 0) {
455
+ throw new Error('At least one of singleCellDatasetUrl or singleNucleusDatasetUrl must be provided.');
456
+ }
457
+ const initialModality = resolveSelectedModality(modalities.map(m => m.uid), selectedModality);
458
+ const initialModalityInfo = modalities.find(m => m.uid === initialModality);
459
+
460
+ const vc = new VitessceConfig({ schemaVersion: '1.0.16', name: 'Lake et al.' });
461
+ const datasets = modalities.map((modality) => {
462
+ const dataset = vc.addDataset(modality.name, undefined, { uid: modality.uid });
463
+ addComparativeFiles(dataset, modality.url);
464
+ return dataset;
465
+ });
466
+ // Views are initially added for the first dataset, but all views are subsequently
467
+ // linked to the shared dataset scope (whose value determines the dataset that is viewed).
468
+ const [dataset] = datasets;
469
+ // Note: the value of this scope is set only after all views have been added,
470
+ // since vc.addView requires a single dataset scope matching the dataset uid.
471
+ const [datasetScope] = vc.addCoordination(
472
+ {
473
+ cType: 'dataset',
474
+ cScope: COMPARATIVE_DATASET_SCOPE,
475
+ cValue: null,
476
+ },
477
+ );
207
478
 
208
479
  const [sampleSetScope_caseControl] = vc.addCoordination(
209
480
  {
@@ -238,10 +509,15 @@ export function generateComparativeConfig(baseUrl, isBiomarkerSelectOnly) {
238
509
  );
239
510
 
240
511
  if (isBiomarkerSelectOnly) {
241
- const biomarkerSelect = vc.addView(dataset, 'biomarkerSelectAlt', { uid: 'biomarker-select' });
512
+ const biomarkerSelect = vc.addView(dataset, 'biomarkerSelectAlt', { uid: BIOMARKER_SELECT_VIEW_UID }).setProps({
513
+ // The selected modality-agnostic item (e.g., gene or pathway), like { label, nodeType }.
514
+ // Note: this value is updated via withBiomarkerSelection.
515
+ biomarkerSelection: null,
516
+ });
242
517
 
243
518
  vc.linkViews([biomarkerSelect], ['sampleType'], ['sample']);
244
519
  vc.linkViewsByObject([biomarkerSelect], {
520
+ dataset: datasetScope,
245
521
  sampleSetSelection: sampleSetScope_caseControl,
246
522
  featureSelection: featureSelectionScope,
247
523
  }, { meta: false });
@@ -250,8 +526,8 @@ export function generateComparativeConfig(baseUrl, isBiomarkerSelectOnly) {
250
526
  } else {
251
527
  const comparativeHeading = vc.addView(dataset, 'comparativeHeading', { uid: 'comparative-heading' });
252
528
  const dualScatterplot = vc.addView(dataset, 'dualScatterplot', { uid: 'scatterplot' }).setProps({
253
- // TODO: this is a hack. adjust the Densmap extent in the pipeline itself
254
- circleScaleFactor: baseUrl.includes('sn-rna-seq') ? 0.5 : 0.8,
529
+ // Note: this value is updated upon modality changes via withSelectedModality.
530
+ circleScaleFactor: initialModalityInfo.circleScaleFactor,
255
531
  });
256
532
  const obsSets = vc.addView(dataset, 'obsSets', { uid: 'cell-sets' }).setProps({ title: 'Cell Types' });
257
533
  const obsSetSelector = vc.addView(dataset, 'singleObsSetSelector', { uid: 'single-cell-set' });
@@ -266,6 +542,11 @@ export function generateComparativeConfig(baseUrl, isBiomarkerSelectOnly) {
266
542
  const obsSetCompositionBarPlot = vc.addView(dataset, 'obsSetCompositionBarPlot', { uid: 'sccoda-plot' });
267
543
  const featureSetEnrichmentBarPlot = vc.addView(dataset, 'featureSetEnrichmentBarPlot', { uid: 'pathways-plot' });
268
544
 
545
+ // Link all views to the shared dataset scope.
546
+ vc.linkViewsByObject([dualScatterplot, obsSets, obsSetSelector, featureList, violinPlots, dotPlot, treemap, volcanoPlot, volcanoPlotTable, comparativeHeading, obsSetCompositionBarPlot, featureSetEnrichmentBarPlot, sampleSets], {
547
+ dataset: datasetScope,
548
+ }, { meta: false });
549
+
269
550
  vc.linkViewsByObject([dualScatterplot], {
270
551
  embeddingType: 'densMAP',
271
552
  embeddingContoursVisible: true,
@@ -316,12 +597,13 @@ export function generateComparativeConfig(baseUrl, isBiomarkerSelectOnly) {
316
597
  vconcat(volcanoPlotTable, featureList, featureSetEnrichmentBarPlot, violinPlots),
317
598
  ));
318
599
  }
600
+ datasetScope.setValue(initialModality);
319
601
  const configJSON = vc.toJSON();
320
602
  return configJSON;
321
603
  }
322
604
 
323
605
  export function BiomarkerSelectPageComponent() {
324
- const BiomarkerSelect = usePageModeView('biomarker-select');
606
+ const BiomarkerSelect = usePageModeView(BIOMARKER_SELECT_VIEW_UID);
325
607
 
326
608
  return (
327
609
  <>
@@ -0,0 +1,151 @@
1
+ /* eslint-disable no-underscore-dangle */
2
+ import { describe, it, expect } from 'vitest';
3
+ import {
4
+ generateComparativeConfig,
5
+ getSelectedModality,
6
+ withSelectedModality,
7
+ getBiomarkerSelection,
8
+ withBiomarkerSelection,
9
+ mergeBiomarkerSelectConfig,
10
+ } from './ComparativeConfig.jsx';
11
+
12
+ const singleCellDatasetUrl = 'https://example.com/sc.adata.zarr';
13
+ const singleNucleusDatasetUrl = 'https://example.com/sn.adata.zarr';
14
+
15
+ function getScatterplot(config) {
16
+ return config.layout.find(v => v.uid === 'scatterplot');
17
+ }
18
+
19
+ describe('generateComparativeConfig', () => {
20
+ it('defines one dataset per provided modality', () => {
21
+ const both = generateComparativeConfig(
22
+ { singleCellDatasetUrl, singleNucleusDatasetUrl },
23
+ false,
24
+ );
25
+ expect(both.datasets.map(d => d.uid)).toEqual(['sc-rna-seq', 'sn-rna-seq']);
26
+ expect(both.datasets[1].files.every(f => f.url.startsWith(singleNucleusDatasetUrl))).toBe(true);
27
+ expect(getSelectedModality(both)).toEqual('sc-rna-seq');
28
+
29
+ const snOnly = generateComparativeConfig({ singleNucleusDatasetUrl }, true);
30
+ expect(snOnly.datasets.map(d => d.uid)).toEqual(['sn-rna-seq']);
31
+ expect(getSelectedModality(snOnly)).toEqual('sn-rna-seq');
32
+ });
33
+
34
+ it('throws when no dataset URL is provided', () => {
35
+ expect(() => generateComparativeConfig({}, false)).toThrow();
36
+ });
37
+
38
+ it('links every view to the shared dataset scope', () => {
39
+ const config = generateComparativeConfig({ singleCellDatasetUrl, singleNucleusDatasetUrl }, false, 'sn-rna-seq');
40
+ expect(getSelectedModality(config)).toEqual('sn-rna-seq');
41
+ expect(config.layout.every(v => v.coordinationScopes.dataset === '__comparison__')).toBe(true);
42
+ expect(getScatterplot(config).props.circleScaleFactor).toEqual(0.5);
43
+ });
44
+ });
45
+
46
+ describe('withSelectedModality', () => {
47
+ const config = generateComparativeConfig(
48
+ { singleCellDatasetUrl, singleNucleusDatasetUrl },
49
+ false,
50
+ );
51
+
52
+ it('switches the selected dataset and modality-specific view props', () => {
53
+ const newConfig = withSelectedModality(config, 'sn-rna-seq');
54
+ expect(getSelectedModality(newConfig)).toEqual('sn-rna-seq');
55
+ expect(getScatterplot(newConfig).props.circleScaleFactor).toEqual(0.5);
56
+ expect(getScatterplot(config).props.circleScaleFactor).toEqual(0.8);
57
+ });
58
+
59
+ it('resets obsSetSelection to null upon a change of modality', () => {
60
+ const withSelection = {
61
+ ...config,
62
+ coordinationSpace: {
63
+ ...config.coordinationSpace,
64
+ obsSetSelection: {
65
+ __multi__: [['Subclass L1', 'POD'], ['Subclass L1', 'PT']],
66
+ __single__: [['Subclass L1', 'POD']],
67
+ },
68
+ },
69
+ };
70
+ const newConfig = withSelectedModality(withSelection, 'sn-rna-seq');
71
+ expect(newConfig.coordinationSpace.obsSetSelection).toEqual({
72
+ __multi__: null,
73
+ __single__: null,
74
+ });
75
+ // Selecting the already-selected modality does not reset obsSetSelection.
76
+ expect(withSelectedModality(withSelection, 'sc-rna-seq')).toBe(withSelection);
77
+ });
78
+
79
+ it('ignores modalities that are not in the config', () => {
80
+ const scOnly = generateComparativeConfig({ singleCellDatasetUrl }, false);
81
+ expect(withSelectedModality(scOnly, 'sn-rna-seq')).toBe(scOnly);
82
+ expect(withSelectedModality(config, 'sc-rna-seq')).toBe(config);
83
+ });
84
+ });
85
+
86
+ describe('withBiomarkerSelection', () => {
87
+ const config = generateComparativeConfig({ singleCellDatasetUrl }, true);
88
+
89
+ it('sets the biomarkerSelection prop of the biomarkerSelectAlt view', () => {
90
+ expect(getBiomarkerSelection(config)).toBeNull();
91
+ const pathway = { label: 'Cell Cycle R-HSA-1640170', nodeType: 'pathway' };
92
+ const newConfig = withBiomarkerSelection(config, pathway);
93
+ expect(getBiomarkerSelection(newConfig)).toEqual(pathway);
94
+ expect(withBiomarkerSelection(newConfig, { ...pathway })).toBe(newConfig);
95
+ expect(getBiomarkerSelection(withBiomarkerSelection(newConfig, null))).toBeNull();
96
+ });
97
+ });
98
+
99
+ describe('mergeBiomarkerSelectConfig', () => {
100
+ const initialConfig = generateComparativeConfig(
101
+ { singleCellDatasetUrl, singleNucleusDatasetUrl },
102
+ true,
103
+ );
104
+ // After initialization by Vitessce, the config contains a uid.
105
+ const currentConfig = withBiomarkerSelection({
106
+ ...initialConfig,
107
+ uid: 'A',
108
+ coordinationSpace: {
109
+ ...initialConfig.coordinationSpace,
110
+ featureSelection: { __comparison__: ['GPRIN3'] },
111
+ sampleSetSelection: { __comparison__: null },
112
+ },
113
+ }, { label: 'GPRIN3', nodeType: 'gene' });
114
+ const params = {
115
+ currentConfig,
116
+ initialConfig,
117
+ geneSelection: ['GPRIN3'],
118
+ sampleSetSelection: null,
119
+ biomarkerSelection: { label: 'GPRIN3', nodeType: 'gene' },
120
+ selectedModality: 'sc-rna-seq',
121
+ createUid: () => 'B',
122
+ };
123
+
124
+ it('returns the current config when it already reflects the outside selections', () => {
125
+ expect(mergeBiomarkerSelectConfig(params)).toBe(currentConfig);
126
+ });
127
+
128
+ it('assigns a new uid when a selection changes outside (e.g., from ControlledComparative)', () => {
129
+ const merged = mergeBiomarkerSelectConfig({
130
+ ...params,
131
+ geneSelection: ['AKR1B1'],
132
+ biomarkerSelection: { label: 'AKR1B1', nodeType: 'gene' },
133
+ });
134
+ expect(merged.uid).toEqual('B');
135
+ expect(getBiomarkerSelection(merged)).toEqual({ label: 'AKR1B1', nodeType: 'gene' });
136
+ expect(merged.coordinationSpace.featureSelection.__comparison__).toEqual(['AKR1B1']);
137
+ });
138
+
139
+ it('assigns a new uid upon a change of modality', () => {
140
+ const merged = mergeBiomarkerSelectConfig({ ...params, selectedModality: 'sn-rna-seq' });
141
+ expect(merged.uid).toEqual('B');
142
+ expect(getSelectedModality(merged)).toEqual('sn-rna-seq');
143
+ });
144
+
145
+ it('merges into the initial config (without a uid) prior to the first onConfigChange', () => {
146
+ const merged = mergeBiomarkerSelectConfig({ ...params, currentConfig: null });
147
+ expect(merged.uid).toBeUndefined();
148
+ expect(getBiomarkerSelection(merged)).toEqual({ label: 'GPRIN3', nodeType: 'gene' });
149
+ expect(merged.coordinationSpace.featureSelection.__comparison__).toEqual(['GPRIN3']);
150
+ });
151
+ });
@@ -3,14 +3,27 @@
3
3
  import React, { useMemo, useState, useCallback } from 'react';
4
4
  import { Vitessce } from '@vitessce/all';
5
5
  import { isEqual } from 'lodash-es';
6
- import { ComparativePageComponent, generateComparativeConfig } from './ComparativeConfig.jsx';
6
+ import {
7
+ ComparativePageComponent,
8
+ generateComparativeConfig,
9
+ getSelectedModality,
10
+ resolveSelectedModality,
11
+ withSelectedModality,
12
+ } from './ComparativeConfig.jsx';
7
13
 
8
14
  /**
9
15
  * A controlled component for comparative visualization that synchronizes
10
16
  * gene selection and sample set selection state with its parent.
11
17
  * @param {object} props
12
- * @param {string} props.datasetUrl - URL to the comparative AnnData-Zarr
13
- * store.
18
+ * At least one of singleCellDatasetUrl or singleNucleusDatasetUrl must be provided.
19
+ * @param {string} [props.singleCellDatasetUrl] - URL to the single-cell
20
+ * comparative AnnData-Zarr store.
21
+ * @param {string} [props.singleNucleusDatasetUrl] - URL to the single-nucleus
22
+ * comparative AnnData-Zarr store.
23
+ * @param {string} [props.selectedModality] - The modality currently being viewed
24
+ * (one of 'sc-rna-seq' or 'sn-rna-seq'). By default, the first modality for which a URL was provided.
25
+ * @param {Function} [props.setSelectedModality] - Setter function
26
+ * called when the selected modality has been updated from inside.
14
27
  * @param {string[]} props.geneSelection - Array of selected gene names.
15
28
  * @param {Function} props.setGeneSelection - Setter function
16
29
  * called when the gene selection has been updated from inside.
@@ -26,7 +39,10 @@ import { ComparativePageComponent, generateComparativeConfig } from './Comparati
26
39
  */
27
40
  export function ControlledComparative(props) {
28
41
  const {
29
- datasetUrl = 'https://data-2.vitessce.io/kpmp-atlas-v2/sc-rna-seq/processed/kpmp-aug-2026.adata.zarr',
42
+ singleCellDatasetUrl,
43
+ singleNucleusDatasetUrl,
44
+ selectedModality: selectedModalityProp,
45
+ setSelectedModality,
30
46
  geneSelection,
31
47
  setGeneSelection,
32
48
  sampleSetSelection,
@@ -36,28 +52,40 @@ export function ControlledComparative(props) {
36
52
  logLevel = undefined,
37
53
  } = props;
38
54
 
39
- const initialConfig = useMemo(() => generateComparativeConfig(datasetUrl, false), [datasetUrl]);
55
+ const initialConfig = useMemo(() => generateComparativeConfig(
56
+ { singleCellDatasetUrl, singleNucleusDatasetUrl },
57
+ false,
58
+ ), [singleCellDatasetUrl, singleNucleusDatasetUrl]);
59
+ // Fall back to the first available modality if the selected one is not available.
60
+ const selectedModality = useMemo(() => resolveSelectedModality(
61
+ initialConfig.datasets.map(d => d.uid),
62
+ selectedModalityProp,
63
+ ), [initialConfig, selectedModalityProp]);
40
64
  const [currentConfig, setCurrentConfig] = useState(null);
41
65
  // Merge currentConfig with geneSelection and sampleSetSelection,
42
66
  // which may have been updated from outside Vitessce (e.g., from URL params).
43
67
  const mergedConfig = useMemo(() => {
44
68
  if (!currentConfig) {
45
- return initialConfig;
69
+ return withSelectedModality(initialConfig, selectedModality);
46
70
  }
47
71
  // Merge currentConfig with geneSelection and sampleSetSelection.
48
- const isValidGeneSelection = Array.isArray(geneSelection) && geneSelection.every(g => typeof g === 'string');
49
- const isValidSampleSetSelection = Array.isArray(sampleSetSelection) && sampleSetSelection.every(s => Array.isArray(s) && s.every(t => typeof t === 'string'));
72
+ // Note: null is a valid value, which represents a cleared selection
73
+ // (e.g., after selecting "All" in the "In group" dropdown of the BiomarkerSelect UI).
74
+ const isValidGeneSelection = geneSelection === null || (Array.isArray(geneSelection) && geneSelection.every(g => typeof g === 'string'));
75
+ const isValidSampleSetSelection = sampleSetSelection === null || (Array.isArray(sampleSetSelection) && sampleSetSelection.every(s => Array.isArray(s) && s.every(t => typeof t === 'string')));
50
76
 
51
77
  // Check if geneSelection or sampleSetSelection have changed before specifying a different config.uid value.
52
78
  const prevGeneSelection = currentConfig.coordinationSpace?.featureSelection?.__comparison__;
53
79
  const prevSampleSetSelection = currentConfig.coordinationSpace?.sampleSetSelection?.__comparison__;
80
+ const prevModality = getSelectedModality(currentConfig);
54
81
  if (isEqual(prevGeneSelection, geneSelection)
55
82
  && isEqual(prevSampleSetSelection, sampleSetSelection)
83
+ && prevModality === selectedModality
56
84
  ) {
57
85
  return currentConfig;
58
86
  }
59
87
 
60
- const newConfig = {
88
+ const newConfig = withSelectedModality({
61
89
  ...currentConfig,
62
90
  // Note: this forces Vitessce to re-initialize the coordination space for featureSelection and sampleSetSelection,
63
91
  // but it also causes the data to be re-loaded.
@@ -80,11 +108,10 @@ export function ControlledComparative(props) {
80
108
  ),
81
109
  },
82
110
  },
83
- };
111
+ }, selectedModality);
84
112
 
85
- // console.log(geneSelection, isValidGeneSelection, newConfig);
86
113
  return newConfig;
87
- }, [initialConfig, currentConfig, geneSelection, sampleSetSelection]);
114
+ }, [initialConfig, currentConfig, geneSelection, sampleSetSelection, selectedModality]);
88
115
 
89
116
 
90
117
  const onConfigChange = useCallback((newConfig) => {
@@ -115,7 +142,14 @@ export function ControlledComparative(props) {
115
142
  if (hasNewSampleSetSelection) {
116
143
  setSampleSetSelection(newSampleSetSelection);
117
144
  }
118
- }, [geneSelection, sampleSetSelection]);
145
+
146
+ // Was a different modality (i.e., dataset) selected from inside Vitessce?
147
+ const newModality = getSelectedModality(newConfig);
148
+ if (typeof newModality === 'string' && newModality !== selectedModality) {
149
+ setSelectedModality?.(newModality);
150
+ }
151
+ }, [geneSelection, sampleSetSelection, selectedModality,
152
+ setGeneSelection, setSampleSetSelection, setSelectedModality]);
119
153
 
120
154
  const didFirstValidation = currentConfig !== null;
121
155