@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.
- package/dist/index.js +986 -5396
- package/dist-tsc/CellSetsZarrLoader.d.ts +23 -0
- package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -1
- package/dist-tsc/CellSetsZarrLoader.js +90 -6
- package/dist-tsc/CellSetsZarrLoader.js.map +1 -1
- package/dist-tsc/CellSetsZarrLoader.test.js +76 -1
- package/dist-tsc/CellSetsZarrLoader.test.js.map +1 -1
- package/dist-tsc/cell-set-utils.d.ts +78 -1
- package/dist-tsc/cell-set-utils.d.ts.map +1 -1
- package/dist-tsc/cell-set-utils.js +308 -66
- package/dist-tsc/cell-set-utils.js.map +1 -1
- package/dist-tsc/cell-set-utils.test.fixtures.d.ts +14 -0
- package/dist-tsc/cell-set-utils.test.fixtures.js +18 -0
- package/dist-tsc/cell-set-utils.test.fixtures.js.map +1 -1
- package/dist-tsc/cell-set-utils.test.js +319 -2
- package/dist-tsc/cell-set-utils.test.js.map +1 -1
- package/dist-tsc/expr-utils.d.ts +51 -46
- package/dist-tsc/expr-utils.d.ts.map +1 -1
- package/dist-tsc/expr-utils.js +297 -222
- package/dist-tsc/expr-utils.js.map +1 -1
- package/dist-tsc/expr-utils.test.js +77 -5
- package/dist-tsc/expr-utils.test.js.map +1 -1
- package/dist-tsc/index.d.ts +3 -2
- package/dist-tsc/index.js +3 -2
- package/dist-tsc/index.js.map +1 -1
- package/dist-tsc/io.d.ts.map +1 -1
- package/dist-tsc/io.js +1 -3
- package/dist-tsc/io.js.map +1 -1
- package/dist-tsc/membership.d.ts +43 -0
- package/dist-tsc/membership.d.ts.map +1 -0
- package/dist-tsc/membership.js +212 -0
- package/dist-tsc/membership.js.map +1 -0
- package/dist-tsc/membership.test.d.ts +2 -0
- package/dist-tsc/membership.test.d.ts.map +1 -0
- package/dist-tsc/membership.test.js +128 -0
- package/dist-tsc/membership.test.js.map +1 -0
- package/package.json +6 -5
- package/src/CellSetsZarrLoader.js +99 -6
- package/src/CellSetsZarrLoader.test.js +88 -1
- package/src/cell-set-utils.js +322 -57
- package/src/cell-set-utils.test.fixtures.js +19 -0
- package/src/cell-set-utils.test.js +392 -0
- package/src/expr-utils.js +315 -248
- package/src/expr-utils.test.js +121 -5
- package/src/index.js +11 -0
- package/src/io.js +1 -3
- package/src/membership.js +217 -0
- package/src/membership.test.js +136 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-underscore-dangle */
|
|
2
2
|
import { describe, it, expect } from 'vitest';
|
|
3
3
|
import { cloneDeep } from 'lodash-es';
|
|
4
|
+
import { MISSING_VALUE_PLACEHOLDER } from '@vitessce/utils';
|
|
4
5
|
|
|
5
6
|
import {
|
|
6
7
|
nodeToRenderProps,
|
|
@@ -15,12 +16,24 @@ import {
|
|
|
15
16
|
treeExport,
|
|
16
17
|
filterNode,
|
|
17
18
|
treeToSetSizesBySetNames,
|
|
19
|
+
treeToCellColorsBySetNames,
|
|
20
|
+
treeToSelectedSetMap,
|
|
21
|
+
treeToCellSetColorIndicesBySetNames,
|
|
22
|
+
treeToObjectsBySetNames,
|
|
23
|
+
treeToColorIndicesArray,
|
|
24
|
+
colorIndicesFromCodes,
|
|
25
|
+
treeToSetIndicesArray,
|
|
26
|
+
setIndicesFromCodes,
|
|
27
|
+
getObsIndexMap,
|
|
28
|
+
treeToMembershipMap,
|
|
18
29
|
} from './cell-set-utils.js';
|
|
30
|
+
import { codesToCellSetsTree } from './CellSetsZarrLoader.js';
|
|
19
31
|
|
|
20
32
|
import {
|
|
21
33
|
levelTwoNodeLeaf,
|
|
22
34
|
levelZeroNode,
|
|
23
35
|
tree,
|
|
36
|
+
treeWithScores,
|
|
24
37
|
} from './cell-set-utils.test.fixtures.js';
|
|
25
38
|
|
|
26
39
|
describe('Hierarchical sets cell-set-utils', () => {
|
|
@@ -275,4 +288,383 @@ describe('Hierarchical sets cell-set-utils', () => {
|
|
|
275
288
|
.toEqual(tree.tree[0].children[0].children[0].name);
|
|
276
289
|
});
|
|
277
290
|
});
|
|
291
|
+
|
|
292
|
+
describe('Map observations to colors by set', () => {
|
|
293
|
+
const PERICYTES = ['Cell Type Annotations', 'Vasculature', 'Pericytes'];
|
|
294
|
+
const ENDOTHELIAL = ['Cell Type Annotations', 'Vasculature', 'Endothelial'];
|
|
295
|
+
const MISSING = ['Cell Type Annotations', 'Vasculature', 'Does Not Exist'];
|
|
296
|
+
// Pericytes and Endothelial both contain cell_3, so the later selected path wins.
|
|
297
|
+
const setColor = [
|
|
298
|
+
{ path: PERICYTES, color: [255, 0, 0] },
|
|
299
|
+
{ path: ENDOTHELIAL, color: [0, 255, 0] },
|
|
300
|
+
];
|
|
301
|
+
|
|
302
|
+
it('treeToCellColorsBySetNames assigns each set its color', () => {
|
|
303
|
+
const cellColors = treeToCellColorsBySetNames(
|
|
304
|
+
tree, [PERICYTES, ENDOTHELIAL], setColor, 'light',
|
|
305
|
+
);
|
|
306
|
+
expect(cellColors.size).toEqual(5);
|
|
307
|
+
expect(cellColors.get('cell_1')).toEqual([255, 0, 0]);
|
|
308
|
+
expect(cellColors.get('cell_2')).toEqual([255, 0, 0]);
|
|
309
|
+
// cell_3 is in both sets; the later path in selectedNamePaths wins.
|
|
310
|
+
expect(cellColors.get('cell_3')).toEqual([0, 255, 0]);
|
|
311
|
+
expect(cellColors.get('cell_4')).toEqual([0, 255, 0]);
|
|
312
|
+
expect(cellColors.get('cell_5')).toEqual([0, 255, 0]);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('treeToCellColorsBySetNames falls back to the theme default color', () => {
|
|
316
|
+
const cellColors = treeToCellColorsBySetNames(tree, [PERICYTES], [], 'light');
|
|
317
|
+
expect(cellColors.get('cell_1')).toEqual([200, 200, 200]);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it('treeToCellColorsBySetNames mixes in per-observation uncertainty', () => {
|
|
321
|
+
const cellColors = treeToCellColorsBySetNames(
|
|
322
|
+
treeWithScores,
|
|
323
|
+
[['Cell Type Annotations', 'Pericytes']],
|
|
324
|
+
[{ path: ['Cell Type Annotations', 'Pericytes'], color: [255, 0, 0] }],
|
|
325
|
+
'light',
|
|
326
|
+
);
|
|
327
|
+
// A score of 1.0 keeps the set color, 0.0 collapses to the [128, 128, 128]
|
|
328
|
+
// mixing color, and 0.5 lands halfway between.
|
|
329
|
+
expect(cellColors.get('cell_1')).toEqual([255, 0, 0]);
|
|
330
|
+
expect(cellColors.get('cell_2')).toEqual([191.5, 64, 64]);
|
|
331
|
+
expect(cellColors.get('cell_3')).toEqual([128, 128, 128]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('treeToCellColorsBySetNames skips paths absent from the tree', () => {
|
|
335
|
+
const cellColors = treeToCellColorsBySetNames(
|
|
336
|
+
tree, [PERICYTES, MISSING], setColor, 'light',
|
|
337
|
+
);
|
|
338
|
+
expect(cellColors.size).toEqual(3);
|
|
339
|
+
expect(treeToCellColorsBySetNames(tree, [], setColor, 'light').size).toEqual(0);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('treeToSelectedSetMap maps each observation to its set path', () => {
|
|
343
|
+
const setMap = treeToSelectedSetMap(tree, [PERICYTES, ENDOTHELIAL]);
|
|
344
|
+
expect(setMap.size).toEqual(5);
|
|
345
|
+
expect(setMap.get('cell_1')).toEqual(PERICYTES);
|
|
346
|
+
expect(setMap.get('cell_3')).toEqual(ENDOTHELIAL);
|
|
347
|
+
expect(treeToSelectedSetMap(tree, [MISSING]).size).toEqual(0);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('treeToCellSetColorIndicesBySetNames maps observations to palette indices', () => {
|
|
351
|
+
const colorIndices = treeToCellSetColorIndicesBySetNames(
|
|
352
|
+
tree, [PERICYTES, ENDOTHELIAL], setColor,
|
|
353
|
+
);
|
|
354
|
+
expect(colorIndices.size).toEqual(5);
|
|
355
|
+
expect(colorIndices.get('cell_1')).toEqual(0);
|
|
356
|
+
expect(colorIndices.get('cell_3')).toEqual(1);
|
|
357
|
+
expect(colorIndices.get('cell_5')).toEqual(1);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('treeToCellSetColorIndicesBySetNames tolerates a null selection', () => {
|
|
361
|
+
expect(treeToCellSetColorIndicesBySetNames(tree, null, setColor).size).toEqual(0);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('treeToObjectsBySetNames returns one entry per set membership', () => {
|
|
365
|
+
const objects = treeToObjectsBySetNames(tree, [PERICYTES, ENDOTHELIAL], setColor, 'light');
|
|
366
|
+
// An array rather than a Map, so cell_3 appears once per set it belongs to.
|
|
367
|
+
expect(objects.length).toEqual(6);
|
|
368
|
+
expect(objects[0]).toEqual({ obsId: 'cell_1', name: 'Pericytes', color: [255, 0, 0] });
|
|
369
|
+
expect(objects[3]).toEqual({ obsId: 'cell_3', name: 'Endothelial', color: [0, 255, 0] });
|
|
370
|
+
expect(objects.filter(d => d.obsId === 'cell_3').length).toEqual(2);
|
|
371
|
+
expect(treeToObjectsBySetNames(tree, [], setColor, 'light')).toEqual([]);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe('Positionally-indexed set colors', () => {
|
|
376
|
+
const PERICYTES = ['Cell Type Annotations', 'Vasculature', 'Pericytes'];
|
|
377
|
+
const ENDOTHELIAL = ['Cell Type Annotations', 'Vasculature', 'Endothelial'];
|
|
378
|
+
const setColor = [
|
|
379
|
+
{ path: PERICYTES, color: [255, 0, 0] },
|
|
380
|
+
{ path: ENDOTHELIAL, color: [0, 255, 0] },
|
|
381
|
+
];
|
|
382
|
+
// cell_6 and cell_7 are in the observation index but in neither selected set.
|
|
383
|
+
const obsIndex = ['cell_1', 'cell_2', 'cell_3', 'cell_4', 'cell_5', 'cell_6', 'cell_7'];
|
|
384
|
+
|
|
385
|
+
it('getObsIndexMap maps IDs to positions and memoizes on the array', () => {
|
|
386
|
+
const map = getObsIndexMap(obsIndex);
|
|
387
|
+
expect(map.get('cell_1')).toEqual(0);
|
|
388
|
+
expect(map.get('cell_7')).toEqual(6);
|
|
389
|
+
// Same array reference returns the very same Map, so views can share it.
|
|
390
|
+
expect(getObsIndexMap(obsIndex)).toBe(map);
|
|
391
|
+
expect(getObsIndexMap([...obsIndex])).not.toBe(map);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('treeToColorIndicesArray aligns indices to obsIndex positions', () => {
|
|
395
|
+
const { colorIndices, colorProbs, colors } = treeToColorIndicesArray(
|
|
396
|
+
tree, [PERICYTES, ENDOTHELIAL], setColor, obsIndex, 'light',
|
|
397
|
+
);
|
|
398
|
+
expect(colors).toEqual([[255, 0, 0], [0, 255, 0]]);
|
|
399
|
+
// 0 means "in no selected set"; cell_3 is in both, so the later path wins.
|
|
400
|
+
expect(Array.from(colorIndices)).toEqual([1, 1, 2, 2, 2, 0, 0]);
|
|
401
|
+
expect(colorProbs).toEqual(null);
|
|
402
|
+
// Small selections fit in the narrowest typed array.
|
|
403
|
+
expect(colorIndices.BYTES_PER_ELEMENT).toEqual(1);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('treeToColorIndicesArray agrees with treeToCellColorsBySetNames', () => {
|
|
407
|
+
const paths = [PERICYTES, ENDOTHELIAL];
|
|
408
|
+
const cellColors = treeToCellColorsBySetNames(tree, paths, setColor, 'light');
|
|
409
|
+
const { colorIndices, colors } = treeToColorIndicesArray(
|
|
410
|
+
tree, paths, setColor, obsIndex, 'light',
|
|
411
|
+
);
|
|
412
|
+
obsIndex.forEach((obsId, i) => {
|
|
413
|
+
const expected = cellColors.get(obsId) || [200, 200, 200];
|
|
414
|
+
const actual = colorIndices[i] === 0 ? [200, 200, 200] : colors[colorIndices[i] - 1];
|
|
415
|
+
expect(actual).toEqual(expected);
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('treeToColorIndicesArray carries per-observation confidence scores', () => {
|
|
420
|
+
const path = ['Cell Type Annotations', 'Pericytes'];
|
|
421
|
+
const { colorIndices, colorProbs } = treeToColorIndicesArray(
|
|
422
|
+
treeWithScores, [path], [{ path, color: [255, 0, 0] }],
|
|
423
|
+
['cell_1', 'cell_2', 'cell_3', 'cell_4'], 'light',
|
|
424
|
+
);
|
|
425
|
+
expect(Array.from(colorIndices)).toEqual([1, 1, 1, 0]);
|
|
426
|
+
// Observations outside any scored set default to full confidence.
|
|
427
|
+
expect(Array.from(colorProbs)).toEqual([1, 0.5, 0, 1]);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it('treeToColorIndicesArray keeps colors aligned when a path is absent', () => {
|
|
431
|
+
const MISSING = ['Cell Type Annotations', 'Vasculature', 'Does Not Exist'];
|
|
432
|
+
const { colorIndices, colors } = treeToColorIndicesArray(
|
|
433
|
+
tree, [MISSING, PERICYTES], setColor, obsIndex, 'light',
|
|
434
|
+
);
|
|
435
|
+
// The missing path still consumes index 1, so Pericytes must land on index 2.
|
|
436
|
+
expect(colors.length).toEqual(2);
|
|
437
|
+
expect(Array.from(colorIndices)).toEqual([2, 2, 2, 0, 0, 0, 0]);
|
|
438
|
+
expect(colors[colorIndices[0] - 1]).toEqual([255, 0, 0]);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('treeToColorIndicesArray handles empty and absent inputs', () => {
|
|
442
|
+
const empty = treeToColorIndicesArray(tree, [], setColor, obsIndex, 'light');
|
|
443
|
+
expect(Array.from(empty.colorIndices)).toEqual([0, 0, 0, 0, 0, 0, 0]);
|
|
444
|
+
expect(treeToColorIndicesArray(tree, null, setColor, obsIndex, 'light').colors)
|
|
445
|
+
.toEqual([]);
|
|
446
|
+
const noObs = treeToColorIndicesArray(tree, [PERICYTES], setColor, [], 'light');
|
|
447
|
+
expect(noObs.colorIndices.length).toEqual(0);
|
|
448
|
+
// The palette is still built, so it stays usable regardless of obsIndex.
|
|
449
|
+
expect(noObs.colors).toEqual([[255, 0, 0]]);
|
|
450
|
+
// Observations in a selected set but absent from obsIndex are skipped.
|
|
451
|
+
const partial = treeToColorIndicesArray(
|
|
452
|
+
tree, [PERICYTES], setColor, ['cell_2'], 'light',
|
|
453
|
+
);
|
|
454
|
+
expect(Array.from(partial.colorIndices)).toEqual([1]);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
describe('Color indices from raw codes', () => {
|
|
459
|
+
const obsIndex = ['cell_1', 'cell_2', 'cell_3', 'cell_4', 'cell_5'];
|
|
460
|
+
const columns = [
|
|
461
|
+
{
|
|
462
|
+
name: 'Cell Type',
|
|
463
|
+
path: ['Cell Type'],
|
|
464
|
+
codes: Int8Array.from([0, 1, -1, 0, 1]),
|
|
465
|
+
categories: ['T cell', 'B cell'],
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
name: 'Leiden',
|
|
469
|
+
path: ['Leiden'],
|
|
470
|
+
codes: Int8Array.from([1, 1, 0, -1, 0]),
|
|
471
|
+
categories: ['0', '1'],
|
|
472
|
+
},
|
|
473
|
+
];
|
|
474
|
+
const options = [{ name: 'Cell Type' }, { name: 'Leiden' }];
|
|
475
|
+
const codesTree = codesToCellSetsTree({ obsIndex, columns }, options);
|
|
476
|
+
const setColor = [
|
|
477
|
+
{ path: ['Cell Type', 'T cell'], color: [255, 0, 0] },
|
|
478
|
+
{ path: ['Leiden', '0'], color: [0, 255, 0] },
|
|
479
|
+
];
|
|
480
|
+
|
|
481
|
+
// The two routes must be interchangeable, so compare against the tree route
|
|
482
|
+
// on the tree built from the same columns.
|
|
483
|
+
function expectSameAsTreeRoute(selection) {
|
|
484
|
+
const expected = treeToColorIndicesArray(
|
|
485
|
+
codesTree, selection, setColor, obsIndex, 'light',
|
|
486
|
+
);
|
|
487
|
+
const actual = colorIndicesFromCodes({
|
|
488
|
+
columns, obsIndex, selectedNamePaths: selection, cellSetColor: setColor, theme: 'light',
|
|
489
|
+
});
|
|
490
|
+
expect(actual).not.toEqual(null);
|
|
491
|
+
expect(Array.from(actual.colorIndices)).toEqual(Array.from(expected.colorIndices));
|
|
492
|
+
expect(actual.colors).toEqual(expected.colors);
|
|
493
|
+
expect(actual.colorProbs).toEqual(expected.colorProbs);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
it('matches the tree route for a single-hierarchy selection', () => {
|
|
497
|
+
expectSameAsTreeRoute([['Cell Type', 'T cell'], ['Cell Type', 'B cell']]);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it('matches the tree route across hierarchies (later path wins)', () => {
|
|
501
|
+
expectSameAsTreeRoute([['Cell Type', 'T cell'], ['Leiden', '1']]);
|
|
502
|
+
expectSameAsTreeRoute([['Leiden', '1'], ['Cell Type', 'T cell']]);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it('matches the tree route for missing colors and empty selections', () => {
|
|
506
|
+
// 'Leiden' > '1' has no entry in setColor, so it takes the theme default.
|
|
507
|
+
expectSameAsTreeRoute([['Leiden', '1']]);
|
|
508
|
+
expectSameAsTreeRoute([]);
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('returns null for selections it cannot resolve', () => {
|
|
512
|
+
// A user-defined selection from additionalObsSets.
|
|
513
|
+
expect(colorIndicesFromCodes({
|
|
514
|
+
columns,
|
|
515
|
+
obsIndex,
|
|
516
|
+
selectedNamePaths: [['My Selections', 'Selection 1']],
|
|
517
|
+
cellSetColor: setColor,
|
|
518
|
+
theme: 'light',
|
|
519
|
+
})).toEqual(null);
|
|
520
|
+
// A category that is not in the column.
|
|
521
|
+
expect(colorIndicesFromCodes({
|
|
522
|
+
columns,
|
|
523
|
+
obsIndex,
|
|
524
|
+
selectedNamePaths: [['Cell Type', 'NK cell']],
|
|
525
|
+
cellSetColor: setColor,
|
|
526
|
+
theme: 'light',
|
|
527
|
+
})).toEqual(null);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it('resolves the placeholder-named set to observations with missing codes', () => {
|
|
531
|
+
// cell_3 has a missing Cell Type code; cell_4 has a missing Leiden code.
|
|
532
|
+
const missing = colorIndicesFromCodes({
|
|
533
|
+
columns,
|
|
534
|
+
obsIndex,
|
|
535
|
+
selectedNamePaths: [['Cell Type', MISSING_VALUE_PLACEHOLDER]],
|
|
536
|
+
cellSetColor: setColor,
|
|
537
|
+
theme: 'light',
|
|
538
|
+
});
|
|
539
|
+
expect(Array.from(missing.colorIndices)).toEqual([0, 0, 1, 0, 0]);
|
|
540
|
+
expectSameAsTreeRoute([['Cell Type', MISSING_VALUE_PLACEHOLDER]]);
|
|
541
|
+
expectSameAsTreeRoute([['Leiden', MISSING_VALUE_PLACEHOLDER], ['Cell Type', 'T cell']]);
|
|
542
|
+
expectSameAsTreeRoute([
|
|
543
|
+
['Cell Type', MISSING_VALUE_PLACEHOLDER], ['Leiden', MISSING_VALUE_PLACEHOLDER],
|
|
544
|
+
]);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it('returns null for a wrong-depth path', () => {
|
|
548
|
+
// A path with the wrong depth.
|
|
549
|
+
expect(colorIndicesFromCodes({
|
|
550
|
+
columns,
|
|
551
|
+
obsIndex,
|
|
552
|
+
selectedNamePaths: [['Cell Type', 'T cell', 'extra']],
|
|
553
|
+
cellSetColor: setColor,
|
|
554
|
+
theme: 'light',
|
|
555
|
+
})).toEqual(null);
|
|
556
|
+
});
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
describe('Observation set membership', () => {
|
|
560
|
+
const PERICYTES = ['Cell Type Annotations', 'Vasculature', 'Pericytes'];
|
|
561
|
+
const ENDOTHELIAL = ['Cell Type Annotations', 'Vasculature', 'Endothelial'];
|
|
562
|
+
const SQUAMOUS = ['Cell Type Annotations', 'Vasculature', 'Epithelial', 'Squamous'];
|
|
563
|
+
|
|
564
|
+
it('treeToMembershipMap records each leaf set once per observation', () => {
|
|
565
|
+
const membership = treeToMembershipMap(tree);
|
|
566
|
+
// Pericytes and Endothelial are leaves at depth 3, Squamous at depth 4.
|
|
567
|
+
// Leaves shallower than the tree height must not be recorded more than once.
|
|
568
|
+
expect(membership.get('cell_1')).toEqual([PERICYTES]);
|
|
569
|
+
expect(membership.get('cell_3')).toEqual([PERICYTES, ENDOTHELIAL]);
|
|
570
|
+
expect(membership.get('cell_5')).toEqual([ENDOTHELIAL, SQUAMOUS]);
|
|
571
|
+
expect(membership.get('cell_7')).toEqual([SQUAMOUS]);
|
|
572
|
+
expect(membership.size).toEqual(7);
|
|
573
|
+
expect(treeToMembershipMap(null).size).toEqual(0);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
describe('Missing set names', () => {
|
|
579
|
+
// A negative categorical code is a missing value. codesToCellSetsTree (like
|
|
580
|
+
// dataToCellSetsTree) places those observations in a set named by the placeholder.
|
|
581
|
+
const obsIndex = ['c1', 'c2', 'c3', 'c4'];
|
|
582
|
+
const columns = [{ codes: Int8Array.from([0, -1, 1, -1]), categories: ['A', 'B'] }];
|
|
583
|
+
const missingTree = codesToCellSetsTree({ obsIndex, columns }, [{ name: 'Label' }]);
|
|
584
|
+
const missingPath = ['Label', MISSING_VALUE_PLACEHOLDER];
|
|
585
|
+
const missingNode = missingTree.tree[0].children.find(c => c.name === MISSING_VALUE_PLACEHOLDER);
|
|
586
|
+
|
|
587
|
+
it('gives the missing set a real name that survives serialization', () => {
|
|
588
|
+
expect(missingNode).toBeDefined();
|
|
589
|
+
// A selection path into the set round-trips through JSON (as a view config
|
|
590
|
+
// does) and still resolves to the same node.
|
|
591
|
+
const roundTripped = JSON.parse(JSON.stringify(missingPath));
|
|
592
|
+
expect(treeFindNodeByNamePath(missingTree, roundTripped)).toBe(missingNode);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
it('titles the missing set with the shared placeholder in the sets manager', () => {
|
|
596
|
+
const renderProps = nodeToRenderProps(missingNode, missingPath, []);
|
|
597
|
+
expect(renderProps.title).toEqual(MISSING_VALUE_PLACEHOLDER);
|
|
598
|
+
expect(renderProps.size).toEqual(2);
|
|
599
|
+
// Named sets are untouched.
|
|
600
|
+
const namedNode = missingTree.tree[0].children.find(c => c.name === 'A');
|
|
601
|
+
expect(nodeToRenderProps(namedNode, ['Label', 'A'], []).title).toEqual('A');
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
it('labels the missing set with the same placeholder in plot data', () => {
|
|
605
|
+
const sizes = treeToSetSizesBySetNames(
|
|
606
|
+
missingTree, [['Label', 'A'], missingPath], [missingPath], [], 'dark',
|
|
607
|
+
);
|
|
608
|
+
expect(sizes.map(s => s.name)).toEqual(['A', MISSING_VALUE_PLACEHOLDER]);
|
|
609
|
+
expect(sizes[1].size).toEqual(2);
|
|
610
|
+
// The identity of the set is still carried by its path, not the display name.
|
|
611
|
+
expect(sizes[1].setNamePath).toEqual(missingPath);
|
|
612
|
+
|
|
613
|
+
const objects = treeToObjectsBySetNames(missingTree, [missingPath], [], 'dark');
|
|
614
|
+
expect(objects.map(o => o.obsId)).toEqual(['c2', 'c4']);
|
|
615
|
+
expect(objects.every(o => o.name === MISSING_VALUE_PLACEHOLDER)).toEqual(true);
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
describe('Positional set indices', () => {
|
|
620
|
+
const obsIndex = ['c1', 'c2', 'c3', 'c4', 'c5'];
|
|
621
|
+
const columns = [{
|
|
622
|
+
name: 'Cell Type',
|
|
623
|
+
path: ['Cell Type'],
|
|
624
|
+
codes: Int8Array.from([0, 1, -1, 0, 1]),
|
|
625
|
+
categories: ['T cell', 'B cell'],
|
|
626
|
+
}];
|
|
627
|
+
const codesTree = codesToCellSetsTree({ obsIndex, columns }, [{ name: 'Cell Type' }]);
|
|
628
|
+
const selection = [['Cell Type', 'B cell'], ['Cell Type', 'T cell']];
|
|
629
|
+
|
|
630
|
+
it('treeToSetIndicesArray gives 1-based selection positions, later paths winning', () => {
|
|
631
|
+
const indices = treeToSetIndicesArray(codesTree, selection, obsIndex);
|
|
632
|
+
expect(indices).toBeInstanceOf(Uint8Array);
|
|
633
|
+
expect(Array.from(indices)).toEqual([2, 1, 0, 2, 1]);
|
|
634
|
+
// Identical to the color encoding's indices.
|
|
635
|
+
expect(Array.from(indices)).toEqual(
|
|
636
|
+
Array.from(treeToColorIndicesArray(codesTree, selection, [], obsIndex, 'light').colorIndices),
|
|
637
|
+
);
|
|
638
|
+
// A doubly-selected observation takes the later path.
|
|
639
|
+
const both = [['Cell Type', 'T cell'], ['Cell Type', 'B cell'], ['Cell Type', 'T cell']];
|
|
640
|
+
expect(Array.from(treeToSetIndicesArray(codesTree, both, obsIndex))).toEqual([3, 2, 0, 3, 2]);
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
it('treeToSetIndicesArray handles empty selections and absent trees', () => {
|
|
644
|
+
expect(Array.from(treeToSetIndicesArray(codesTree, [], obsIndex))).toEqual([0, 0, 0, 0, 0]);
|
|
645
|
+
expect(Array.from(treeToSetIndicesArray(codesTree, null, obsIndex))).toEqual([0, 0, 0, 0, 0]);
|
|
646
|
+
expect(Array.from(treeToSetIndicesArray(null, selection, obsIndex))).toEqual([0, 0, 0, 0, 0]);
|
|
647
|
+
expect(treeToSetIndicesArray(codesTree, selection, []).length).toEqual(0);
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
it('setIndicesFromCodes matches the tree and resolves the missing-value set', () => {
|
|
651
|
+
expect(Array.from(setIndicesFromCodes({ columns, obsIndex, selectedNamePaths: selection })))
|
|
652
|
+
.toEqual(Array.from(treeToSetIndicesArray(codesTree, selection, obsIndex)));
|
|
653
|
+
const withMissing = [...selection, ['Cell Type', MISSING_VALUE_PLACEHOLDER]];
|
|
654
|
+
expect(Array.from(setIndicesFromCodes({ columns, obsIndex, selectedNamePaths: withMissing })))
|
|
655
|
+
.toEqual(Array.from(treeToSetIndicesArray(codesTree, withMissing, obsIndex)));
|
|
656
|
+
expect(Array.from(setIndicesFromCodes({ columns, obsIndex, selectedNamePaths: withMissing })))
|
|
657
|
+
.toEqual([2, 1, 3, 2, 1]);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
it('setIndicesFromCodes returns null for selections it cannot resolve', () => {
|
|
661
|
+
const unresolvable = [
|
|
662
|
+
[['My Selections', 'Selection 1']],
|
|
663
|
+
[['Cell Type', 'NK cell']],
|
|
664
|
+
[['Cell Type', 'T cell', 'extra']],
|
|
665
|
+
];
|
|
666
|
+
unresolvable.forEach((selectedNamePaths) => {
|
|
667
|
+
expect(setIndicesFromCodes({ columns, obsIndex, selectedNamePaths })).toEqual(null);
|
|
668
|
+
});
|
|
669
|
+
});
|
|
278
670
|
});
|