@vitessce/sets-utils 4.0.4 → 4.0.5

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 (48) hide show
  1. package/dist/index.js +986 -5396
  2. package/dist-tsc/CellSetsZarrLoader.d.ts +23 -0
  3. package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -1
  4. package/dist-tsc/CellSetsZarrLoader.js +90 -6
  5. package/dist-tsc/CellSetsZarrLoader.js.map +1 -1
  6. package/dist-tsc/CellSetsZarrLoader.test.js +76 -1
  7. package/dist-tsc/CellSetsZarrLoader.test.js.map +1 -1
  8. package/dist-tsc/cell-set-utils.d.ts +78 -1
  9. package/dist-tsc/cell-set-utils.d.ts.map +1 -1
  10. package/dist-tsc/cell-set-utils.js +308 -66
  11. package/dist-tsc/cell-set-utils.js.map +1 -1
  12. package/dist-tsc/cell-set-utils.test.fixtures.d.ts +14 -0
  13. package/dist-tsc/cell-set-utils.test.fixtures.js +18 -0
  14. package/dist-tsc/cell-set-utils.test.fixtures.js.map +1 -1
  15. package/dist-tsc/cell-set-utils.test.js +319 -2
  16. package/dist-tsc/cell-set-utils.test.js.map +1 -1
  17. package/dist-tsc/expr-utils.d.ts +51 -46
  18. package/dist-tsc/expr-utils.d.ts.map +1 -1
  19. package/dist-tsc/expr-utils.js +297 -222
  20. package/dist-tsc/expr-utils.js.map +1 -1
  21. package/dist-tsc/expr-utils.test.js +77 -5
  22. package/dist-tsc/expr-utils.test.js.map +1 -1
  23. package/dist-tsc/index.d.ts +3 -2
  24. package/dist-tsc/index.js +3 -2
  25. package/dist-tsc/index.js.map +1 -1
  26. package/dist-tsc/io.d.ts.map +1 -1
  27. package/dist-tsc/io.js +1 -3
  28. package/dist-tsc/io.js.map +1 -1
  29. package/dist-tsc/membership.d.ts +43 -0
  30. package/dist-tsc/membership.d.ts.map +1 -0
  31. package/dist-tsc/membership.js +212 -0
  32. package/dist-tsc/membership.js.map +1 -0
  33. package/dist-tsc/membership.test.d.ts +2 -0
  34. package/dist-tsc/membership.test.d.ts.map +1 -0
  35. package/dist-tsc/membership.test.js +128 -0
  36. package/dist-tsc/membership.test.js.map +1 -0
  37. package/package.json +6 -5
  38. package/src/CellSetsZarrLoader.js +99 -6
  39. package/src/CellSetsZarrLoader.test.js +88 -1
  40. package/src/cell-set-utils.js +322 -57
  41. package/src/cell-set-utils.test.fixtures.js +19 -0
  42. package/src/cell-set-utils.test.js +392 -0
  43. package/src/expr-utils.js +315 -248
  44. package/src/expr-utils.test.js +121 -5
  45. package/src/index.js +11 -0
  46. package/src/io.js +1 -3
  47. package/src/membership.js +217 -0
  48. package/src/membership.test.js +136 -0
@@ -0,0 +1,128 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { MISSING_VALUE_PLACEHOLDER } from '@vitessce/utils';
3
+ import { cloneDeep } from 'lodash-es';
4
+ import { buildMembershipCsr } from '@vitessce/workers';
5
+ import { treeToLeafSets, treeToMembershipMap } from './cell-set-utils.js';
6
+ import { lazyTreeToMembershipMap, membershipFromCodes } from './membership.js';
7
+ import { codesToCellSetsTree } from './CellSetsZarrLoader.js';
8
+ import { tree } from './cell-set-utils.test.fixtures.js';
9
+ const PERICYTES = ['Cell Type Annotations', 'Vasculature', 'Pericytes'];
10
+ // Decode a CSR encoding the way the membership store's lookup does.
11
+ function decode(csr, leafSets, obsIndex, obsId) {
12
+ const obsI = obsIndex.indexOf(obsId);
13
+ if (obsI === -1)
14
+ return undefined;
15
+ const start = csr.offsets[obsI];
16
+ const end = csr.offsets[obsI + 1];
17
+ if (start === end)
18
+ return undefined;
19
+ return Array.from(csr.setIds.slice(start, end)).map(id => leafSets[id].path);
20
+ }
21
+ describe('obs set membership store', () => {
22
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3', 'cell_4', 'cell_5', 'cell_6', 'cell_7'];
23
+ it('the worker encoding decodes to the same answer as the main-thread map', () => {
24
+ // This is the composition the worker path performs: pack the leaf sets, build
25
+ // the CSR off-thread, then resolve set IDs back to paths on lookup.
26
+ const leafSets = treeToLeafSets(tree);
27
+ const setSizes = new Uint32Array(leafSets.length);
28
+ const setObsIds = [];
29
+ leafSets.forEach(({ set }, i) => {
30
+ setSizes[i] = set.length;
31
+ set.forEach(([obsId]) => setObsIds.push(obsId));
32
+ });
33
+ const csr = buildMembershipCsr(obsIndex, setObsIds, setSizes);
34
+ const expected = treeToMembershipMap(tree);
35
+ obsIndex.forEach((obsId) => {
36
+ expect(decode(csr, leafSets, obsIndex, obsId)).toEqual(expected.get(obsId));
37
+ });
38
+ // Every membership was representable, which is what lets the store accept it.
39
+ expect(csr.setIds.length).toEqual(setObsIds.length);
40
+ });
41
+ it('answers correctly before any worker result arrives', () => {
42
+ // Worker construction is unavailable under jsdom, so this exercises the
43
+ // synchronous fallback that also covers browsers without worker support.
44
+ const membership = lazyTreeToMembershipMap(tree, obsIndex);
45
+ const expected = treeToMembershipMap(tree);
46
+ obsIndex.forEach((obsId) => {
47
+ expect(membership.get(obsId)).toEqual(expected.get(obsId));
48
+ });
49
+ expect(membership.size).toEqual(expected.size);
50
+ expect(membership.has('cell_1')).toEqual(true);
51
+ expect(membership.has('not_a_cell')).toEqual(false);
52
+ expect(membership.get('not_a_cell')).toEqual(undefined);
53
+ });
54
+ it('does no work until first lookup', () => {
55
+ // Traversal has to read `tree`, so an untouched getter proves the map was never
56
+ // built. The value is stable because the traversal reads it more than once.
57
+ const treeValue = cloneDeep(tree).tree;
58
+ let reads = 0;
59
+ const spyTree = {
60
+ version: '0.1.3',
61
+ datatype: 'cell',
62
+ get tree() {
63
+ reads += 1;
64
+ return treeValue;
65
+ },
66
+ };
67
+ const membership = lazyTreeToMembershipMap(spyTree);
68
+ expect(reads).toEqual(0);
69
+ expect(membership.get('cell_1')).toEqual([PERICYTES]);
70
+ expect(reads).toBeGreaterThan(0);
71
+ });
72
+ it('builds once and shares across instances', () => {
73
+ const treeValue = cloneDeep(tree).tree;
74
+ let reads = 0;
75
+ const spyTree = {
76
+ version: '0.1.3',
77
+ datatype: 'cell',
78
+ get tree() {
79
+ reads += 1;
80
+ return treeValue;
81
+ },
82
+ };
83
+ const first = lazyTreeToMembershipMap(spyTree);
84
+ const second = lazyTreeToMembershipMap(spyTree);
85
+ first.get('cell_1');
86
+ const readsAfterFirstBuild = reads;
87
+ first.get('cell_2');
88
+ // Memoized on the tree, so a second store over the same tree — a repeated
89
+ // loader invocation, or another view — reuses the built map.
90
+ second.get('cell_3');
91
+ expect(second.size).toEqual(first.size);
92
+ expect(reads).toEqual(readsAfterFirstBuild);
93
+ });
94
+ it('tolerates an absent tree and an absent obsIndex', () => {
95
+ expect(lazyTreeToMembershipMap(null, obsIndex).get('cell_1')).toEqual(undefined);
96
+ expect(lazyTreeToMembershipMap(null, obsIndex).size).toEqual(0);
97
+ expect(lazyTreeToMembershipMap(null, obsIndex).has('cell_1')).toEqual(false);
98
+ // Without an obsIndex there is nothing to align to, so it stays on the
99
+ // main-thread path.
100
+ expect(lazyTreeToMembershipMap(tree).get('cell_1')).toEqual([PERICYTES]);
101
+ });
102
+ });
103
+ describe('membershipFromCodes', () => {
104
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3', 'cell_4'];
105
+ const columns = [
106
+ { name: 'Cell Type', codes: Int8Array.from([0, 1, -1, 0]), categories: ['T cell', 'B cell'] },
107
+ { name: 'Leiden', codes: Int8Array.from([1, 1, 0, -1]), categories: ['0', '1'] },
108
+ ];
109
+ it('matches the tree-based membership map, including missing codes', () => {
110
+ const options = [{ name: 'Cell Type' }, { name: 'Leiden' }];
111
+ const codesTree = codesToCellSetsTree({ obsIndex, columns }, options);
112
+ const expected = treeToMembershipMap(codesTree);
113
+ const membership = membershipFromCodes(obsIndex, columns);
114
+ obsIndex.forEach((obsId) => {
115
+ expect(membership.get(obsId)).toEqual(expected.get(obsId));
116
+ expect(membership.has(obsId)).toEqual(true);
117
+ });
118
+ expect(membership.size).toEqual(expected.size);
119
+ // A missing code reports the placeholder-named set, as the tree does.
120
+ expect(membership.get('cell_3')).toEqual([['Cell Type', MISSING_VALUE_PLACEHOLDER], ['Leiden', '0']]);
121
+ expect(membership.get('not_a_cell')).toEqual(undefined);
122
+ expect(membership.has('not_a_cell')).toEqual(false);
123
+ });
124
+ it('reports zero size with no columns', () => {
125
+ expect(membershipFromCodes(obsIndex, []).size).toEqual(0);
126
+ });
127
+ });
128
+ //# sourceMappingURL=membership.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"membership.test.js","sourceRoot":"","sources":["../src/membership.test.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AAEzD,MAAM,SAAS,GAAG,CAAC,uBAAuB,EAAE,aAAa,EAAE,WAAW,CAAC,CAAC;AAExE,oEAAoE;AACpE,SAAS,MAAM,CAAC,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK;IAC5C,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACrC,IAAI,IAAI,KAAK,CAAC,CAAC;QAAE,OAAO,SAAS,CAAC;IAClC,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,CA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AAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/sets-utils",
3
- "version": "4.0.4",
3
+ "version": "4.0.5",
4
4
  "author": "HIDIVE Lab at HMS",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -21,13 +21,14 @@
21
21
  "concaveman": "^1.2.1",
22
22
  "d3-dsv": "^1.1.1",
23
23
  "internmap": "^2.0.3",
24
- "json2csv": "^5.0.0",
24
+ "@json2csv/plainjs": "^7.0.8",
25
25
  "lodash-es": "^4.17.21",
26
26
  "tinycolor2": "^1.4.1",
27
27
  "uuid": "^9.0.0",
28
- "@vitessce/schemas": "4.0.4",
29
- "@vitessce/utils": "4.0.4",
30
- "@vitessce/globals": "4.0.4"
28
+ "@vitessce/schemas": "4.0.5",
29
+ "@vitessce/workers": "4.0.5",
30
+ "@vitessce/utils": "4.0.5",
31
+ "@vitessce/globals": "4.0.5"
31
32
  },
32
33
  "scripts": {
33
34
  "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.mjs",
@@ -1,4 +1,5 @@
1
1
  import { InternMap } from 'internmap';
2
+ import { MISSING_VALUE_PLACEHOLDER } from '@vitessce/utils';
2
3
  import {
3
4
  treeInitialize,
4
5
  nodeAppendChild,
@@ -7,6 +8,88 @@ import {
7
8
  SETS_DATATYPE_OBS,
8
9
  } from './constants.js';
9
10
 
11
+ /**
12
+ * Whether a decoded set value is missing. A negative categorical code decodes to
13
+ * categories[-1] === undefined on the string route; other sources may yield null.
14
+ * @param {*} value A decoded set value.
15
+ * @returns {boolean} True when the value is missing.
16
+ */
17
+ function isMissing(value) {
18
+ return value === undefined || value === null;
19
+ }
20
+
21
+ /**
22
+ * Name a decoded set value. Missing values share one set named by the shared
23
+ * placeholder, so the name is a real string that survives JSON serialization
24
+ * (an undefined name would be dropped) and that every view renders identically.
25
+ * @param {*} value A decoded set value.
26
+ * @returns {string} The set name.
27
+ */
28
+ function toSetName(value) {
29
+ return isMissing(value) ? MISSING_VALUE_PLACEHOLDER : value;
30
+ }
31
+
32
+ /**
33
+ * Build a cell sets tree from raw categorical codes, without materializing one
34
+ * string per observation. Output is identical to what dataToCellSetsTree produces
35
+ * for the equivalent single-level string columns: observed categories only, the
36
+ * same child ordering (via the same plain-object construction), and a set named
37
+ * MISSING_VALUE_PLACEHOLDER, ordered last, for observations with a negative
38
+ * (missing) code.
39
+ * @param {object} params
40
+ * @param {string[]} params.obsIndex The observation index shared by all columns.
41
+ * @param {{ codes: ArrayLike<number>, categories: string[] }[]} params.columns
42
+ * Raw codes and category names, one entry per obsSets option.
43
+ * @param {{ name: string }[]} options The obsSets options, providing hierarchy names.
44
+ * @returns {object} A tree object.
45
+ */
46
+ export function codesToCellSetsTree({ obsIndex, columns }, options) {
47
+ const cellSetsTree = treeInitialize(SETS_DATATYPE_OBS);
48
+ columns.forEach(({ codes, categories }, j) => {
49
+ const { name } = options[j];
50
+ let levelZeroNode = {
51
+ name,
52
+ children: [],
53
+ };
54
+ // Determine which categories are observed, since dataToCellSetsTree only
55
+ // creates sets for values that occur in the data.
56
+ const seen = new Uint8Array(categories.length);
57
+ let hasMissing = false;
58
+ for (let i = 0; i < codes.length; i += 1) {
59
+ const code = codes[i];
60
+ if (code >= 0) {
61
+ seen[code] = 1;
62
+ } else {
63
+ hasMissing = true;
64
+ }
65
+ }
66
+ const uniqueCellSetIds = categories.filter((_, k) => seen[k]).sort();
67
+ if (hasMissing) {
68
+ // Observations with a negative (missing) code share one set, named by the
69
+ // placeholder and ordered after every real category, as in dataToCellSetsTree.
70
+ uniqueCellSetIds.push(MISSING_VALUE_PLACEHOLDER);
71
+ }
72
+ const clusters = {};
73
+ // eslint-disable-next-line no-return-assign
74
+ uniqueCellSetIds.forEach(id => (clusters[id] = { name: id, set: [] }));
75
+ // Resolve each observed code to its cluster once, so the per-observation
76
+ // loop is a typed-array read plus a push.
77
+ const clusterByCode = categories.map((cat, k) => (seen[k] ? clusters[cat] : null));
78
+ const missingCluster = hasMissing ? clusters[MISSING_VALUE_PLACEHOLDER] : null;
79
+ for (let i = 0; i < codes.length; i += 1) {
80
+ const code = codes[i];
81
+ const cluster = code >= 0 ? clusterByCode[code] : missingCluster;
82
+ cluster.set.push([obsIndex[i], null]);
83
+ }
84
+ Object.values(clusters).forEach(
85
+ // eslint-disable-next-line no-return-assign
86
+ cluster => (levelZeroNode = nodeAppendChild(levelZeroNode, cluster)),
87
+ );
88
+ cellSetsTree.tree.push(levelZeroNode);
89
+ });
90
+ return cellSetsTree;
91
+ }
92
+
10
93
  export function dataToCellSetsTree(data, options) {
11
94
  // obsIndex is an array of all cell IDs, for the purposes of set complement operations only.
12
95
  // cellNames is per-cellSets arrays of cell IDs.
@@ -24,7 +107,7 @@ export function dataToCellSetsTree(data, options) {
24
107
  const levelSets = new InternMap([], JSON.stringify);
25
108
 
26
109
  cellNames[j].forEach((id, i) => {
27
- const classes = cellSetIds.map(col => col[i]);
110
+ const classes = cellSetIds.map(col => toSetName(col[i]));
28
111
  if (levelSets.has(classes)) {
29
112
  levelSets.get(classes).push([id, null]);
30
113
  } else {
@@ -89,16 +172,26 @@ export function dataToCellSetsTree(data, options) {
89
172
  } else {
90
173
  // Single-level case.
91
174
  // Check for the optional corresponding confidence score column name.
92
- const uniqueCellSetIds = Array.from(new Set(cellSetIds)).sort();
175
+ // Missing values share one set, named by the placeholder and ordered last.
176
+ const setNames = cellSetIds.map(toSetName);
177
+ const hasMissing = cellSetIds.some(isMissing);
178
+ const uniqueCellSetIds = Array.from(
179
+ new Set(hasMissing ? cellSetIds.filter(id => !isMissing(id)) : cellSetIds),
180
+ ).sort();
181
+ if (hasMissing) {
182
+ uniqueCellSetIds.push(MISSING_VALUE_PLACEHOLDER);
183
+ }
93
184
  const clusters = {};
94
185
  // eslint-disable-next-line no-return-assign
95
186
  uniqueCellSetIds.forEach(id => (clusters[id] = { name: id, set: [] }));
96
187
  if (cellSetScores[j]) {
97
- cellSetIds
98
- .forEach((id, i) => clusters[id].set.push([cellNames[j][i], cellSetScores[j][i]]));
188
+ setNames.forEach((setName, i) => (
189
+ clusters[setName].set.push([cellNames[j][i], cellSetScores[j][i]])
190
+ ));
99
191
  } else {
100
- cellSetIds
101
- .forEach((id, i) => clusters[id].set.push([cellNames[j][i], null]));
192
+ setNames.forEach((setName, i) => (
193
+ clusters[setName].set.push([cellNames[j][i], null])
194
+ ));
102
195
  }
103
196
  Object.values(clusters).forEach(
104
197
  // eslint-disable-next-line no-return-assign
@@ -1,5 +1,6 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import { dataToCellSetsTree } from './CellSetsZarrLoader.js';
2
+ import { MISSING_VALUE_PLACEHOLDER } from '@vitessce/utils';
3
+ import { dataToCellSetsTree, codesToCellSetsTree } from './CellSetsZarrLoader.js';
3
4
 
4
5
  describe('loaders/CellSetsZarrLoader', () => {
5
6
  it('dataToCellSetsTree constructs a hierarchy from an array of columns', async () => {
@@ -162,3 +163,89 @@ describe('loaders/CellSetsZarrLoader', () => {
162
163
  });
163
164
  });
164
165
  });
166
+
167
+ describe('loaders/codesToCellSetsTree', () => {
168
+ // Decode codes to strings the way AnnDataSource._loadColumn does, so that the
169
+ // string route (dataToCellSetsTree) can serve as the reference output.
170
+ const decode = (codes, categories) => Array.from(codes).map(c => categories[c]);
171
+
172
+ it('matches dataToCellSetsTree for unsorted categories with an unused one', () => {
173
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3', 'cell_4', 'cell_5'];
174
+ // 'gamma' is unused: dataToCellSetsTree only creates sets for observed values.
175
+ const categories = ['beta', 'alpha', 'gamma'];
176
+ const codes = Int8Array.from([1, 0, 0, 1, 1]);
177
+ const options = [{ name: 'Cell Type' }];
178
+ const expected = dataToCellSetsTree(
179
+ [[obsIndex], [decode(codes, categories)], [undefined]], options,
180
+ );
181
+ const actual = codesToCellSetsTree({ obsIndex, columns: [{ codes, categories }] }, options);
182
+ expect(actual).toEqual(expected);
183
+ });
184
+
185
+ it('matches dataToCellSetsTree for negative (missing) codes', () => {
186
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3'];
187
+ const categories = ['alpha', 'beta'];
188
+ // categories[-1] is undefined in the string route; both routes place the
189
+ // observation in a set named by the shared placeholder, ordered last.
190
+ const codes = Int8Array.from([0, -1, 1]);
191
+ const options = [{ name: 'Cell Type' }];
192
+ const expected = dataToCellSetsTree(
193
+ [[obsIndex], [decode(codes, categories)], [undefined]], options,
194
+ );
195
+ const actual = codesToCellSetsTree({ obsIndex, columns: [{ codes, categories }] }, options);
196
+ expect(actual).toEqual(expected);
197
+ expect(actual.tree[0].children.map(c => c.name))
198
+ .toEqual(['alpha', 'beta', MISSING_VALUE_PLACEHOLDER]);
199
+ expect(actual.tree[0].children[2].set).toEqual([['cell_2', null]]);
200
+ // The name is a real string, so the tree (and any selection path into it)
201
+ // survives serialization; an undefined name would be dropped by JSON.
202
+ expect(JSON.parse(JSON.stringify(actual))).toEqual(actual);
203
+ });
204
+
205
+ it('dataToCellSetsTree names missing values consistently across levels', () => {
206
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3'];
207
+ const options = [{ name: 'Subclass Levels' }];
208
+ // A missing value at either level of a multi-level hierarchy.
209
+ const tree = dataToCellSetsTree([
210
+ [obsIndex],
211
+ [[['Immune', undefined, 'Immune'], ['B cell', 'T cell', null]]],
212
+ [undefined],
213
+ ], options);
214
+ const levelOne = tree.tree[0].children;
215
+ expect(levelOne.map(n => n.name)).toEqual([MISSING_VALUE_PLACEHOLDER, 'Immune']);
216
+ expect(levelOne[0].children.map(n => n.name)).toEqual(['T cell']);
217
+ expect(levelOne[1].children.map(n => n.name)).toEqual([MISSING_VALUE_PLACEHOLDER, 'B cell']);
218
+ expect(levelOne[1].children[0].set).toEqual([['cell_3', null]]);
219
+ expect(JSON.parse(JSON.stringify(tree))).toEqual(tree);
220
+ });
221
+
222
+ it('matches dataToCellSetsTree for numeric-string categories (key-order quirk)', () => {
223
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3', 'cell_4'];
224
+ // Integer-like names: plain-object key ordering places them in ascending
225
+ // numeric order regardless of the lexicographic sort. Both routes use the
226
+ // same construction, so both exhibit the same order.
227
+ const categories = ['10', '2', '1'];
228
+ const codes = Int8Array.from([0, 1, 2, 0]);
229
+ const options = [{ name: 'Leiden' }];
230
+ const expected = dataToCellSetsTree(
231
+ [[obsIndex], [decode(codes, categories)], [undefined]], options,
232
+ );
233
+ const actual = codesToCellSetsTree({ obsIndex, columns: [{ codes, categories }] }, options);
234
+ expect(actual).toEqual(expected);
235
+ expect(actual.tree[0].children.map(c => c.name)).toEqual(['1', '2', '10']);
236
+ });
237
+
238
+ it('matches dataToCellSetsTree across multiple hierarchies', () => {
239
+ const obsIndex = ['cell_1', 'cell_2', 'cell_3'];
240
+ const colA = { codes: Int8Array.from([0, 1, 0]), categories: ['T cell', 'B cell'] };
241
+ const colB = { codes: Int8Array.from([1, 1, 0]), categories: ['0', '1'] };
242
+ const options = [{ name: 'Cell Type' }, { name: 'Leiden' }];
243
+ const expected = dataToCellSetsTree([
244
+ [obsIndex, obsIndex],
245
+ [decode(colA.codes, colA.categories), decode(colB.codes, colB.categories)],
246
+ [undefined, undefined],
247
+ ], options);
248
+ const actual = codesToCellSetsTree({ obsIndex, columns: [colA, colB] }, options);
249
+ expect(actual).toEqual(expected);
250
+ });
251
+ });