@vitessce/sets-utils 2.0.3 → 3.0.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.
- package/dist/{index.mjs → index.js} +9301 -7998
- package/dist-tsc/CellSetsZarrLoader.d.ts +2 -0
- package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -0
- package/dist-tsc/CellSetsZarrLoader.js +91 -0
- package/dist-tsc/CellSetsZarrLoader.test.d.ts +2 -0
- package/dist-tsc/CellSetsZarrLoader.test.d.ts.map +1 -0
- package/dist-tsc/CellSetsZarrLoader.test.js +154 -0
- package/dist-tsc/cell-set-utils.d.ts +204 -0
- package/dist-tsc/cell-set-utils.d.ts.map +1 -0
- package/dist-tsc/cell-set-utils.js +630 -0
- package/dist-tsc/cell-set-utils.test.d.ts +2 -0
- package/dist-tsc/cell-set-utils.test.d.ts.map +1 -0
- package/dist-tsc/cell-set-utils.test.fixtures.d.ts +47 -0
- package/dist-tsc/cell-set-utils.test.fixtures.d.ts.map +1 -0
- package/dist-tsc/cell-set-utils.test.fixtures.js +68 -0
- package/dist-tsc/cell-set-utils.test.js +185 -0
- package/dist-tsc/constants.d.ts +17 -0
- package/dist-tsc/constants.d.ts.map +1 -0
- package/dist-tsc/constants.js +17 -0
- package/dist-tsc/index.d.ts +7 -0
- package/dist-tsc/index.d.ts.map +1 -0
- package/dist-tsc/index.js +6 -5
- package/dist-tsc/io.d.ts +47 -0
- package/dist-tsc/io.d.ts.map +1 -0
- package/dist-tsc/io.js +159 -0
- package/dist-tsc/io.test.d.ts +2 -0
- package/dist-tsc/io.test.d.ts.map +1 -0
- package/dist-tsc/io.test.js +124 -0
- package/dist-tsc/set-path-utils.d.ts +28 -0
- package/dist-tsc/set-path-utils.d.ts.map +1 -0
- package/dist-tsc/set-path-utils.js +133 -0
- package/dist-tsc/set-path-utils.test.d.ts +2 -0
- package/dist-tsc/set-path-utils.test.d.ts.map +1 -0
- package/dist-tsc/set-path-utils.test.js +210 -0
- package/dist-tsc/utils.d.ts +45 -0
- package/dist-tsc/utils.d.ts.map +1 -0
- package/dist-tsc/utils.js +139 -0
- package/dist-tsc/utils.test.d.ts +2 -0
- package/dist-tsc/utils.test.d.ts.map +1 -0
- package/dist-tsc/utils.test.js +39 -0
- package/package.json +16 -8
- package/src/CellSetsZarrLoader.js +2 -2
- package/src/CellSetsZarrLoader.test.js +1 -1
- package/src/cell-set-utils.js +33 -16
- package/src/cell-set-utils.test.js +105 -4
- package/src/constants.js +4 -15
- package/src/index.js +11 -5
- package/src/io.js +25 -35
- package/src/io.test.js +1 -4
- package/src/set-path-utils.js +155 -0
- package/src/set-path-utils.test.js +257 -0
- package/src/utils.js +20 -9
- package/src/utils.test.js +48 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import tinycolor from 'tinycolor2';
|
|
2
|
+
import { isEqual } from 'lodash-es';
|
|
3
|
+
import { PALETTE } from '@vitessce/utils';
|
|
4
|
+
import { SETS_DATATYPE_OBS, HIERARCHICAL_SCHEMAS, } from './constants.js';
|
|
5
|
+
/**
|
|
6
|
+
* Execute a callback function based on a keypress event.
|
|
7
|
+
* @param {object} event The event from onKeyPress
|
|
8
|
+
* @param {string} key The key identifier to match.
|
|
9
|
+
* @param {Function} callback The function to execute.
|
|
10
|
+
*/
|
|
11
|
+
export function callbackOnKeyPress(event, key, callback) {
|
|
12
|
+
if (event.key === key) {
|
|
13
|
+
event.preventDefault();
|
|
14
|
+
callback();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Convert an array of [r, g, b] numbers to a hex color.
|
|
19
|
+
* @param {number[]} rgbArray The color [r, g, b] array.
|
|
20
|
+
* @returns {string} The hex color as a string.
|
|
21
|
+
*/
|
|
22
|
+
export function colorArrayToString(rgbArray) {
|
|
23
|
+
return tinycolor({ r: rgbArray[0], g: rgbArray[1], b: rgbArray[2] }).toHexString();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Convert a string color representation to an array of [r,g,b].
|
|
27
|
+
* @param {string} colorString The color as a string.
|
|
28
|
+
* @returns {number[]} The color as an array.
|
|
29
|
+
*/
|
|
30
|
+
export function colorStringToArray(colorString) {
|
|
31
|
+
const colorObj = tinycolor(colorString).toRgb();
|
|
32
|
+
return [colorObj.r, colorObj.g, colorObj.b];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get a string of help text for coloring a particular hierarchy level.
|
|
36
|
+
* @param {integer} i The level. 1 for cluster, 2 for subcluster, etc.
|
|
37
|
+
* @returns {string} The tooltip text for coloring the level.
|
|
38
|
+
*/
|
|
39
|
+
export function getLevelTooltipText(i) {
|
|
40
|
+
if (i === 0)
|
|
41
|
+
return 'Color by hierarchy';
|
|
42
|
+
if (i <= 2) {
|
|
43
|
+
const subs = j => ('sub'.repeat(j));
|
|
44
|
+
return `Color by ${subs(i - 1)}cluster`;
|
|
45
|
+
}
|
|
46
|
+
return `Color by cluster level ${i}`;
|
|
47
|
+
}
|
|
48
|
+
export function isEqualOrPrefix(targetPath, testPath) {
|
|
49
|
+
if (targetPath.length <= testPath.length) {
|
|
50
|
+
return isEqual(targetPath, testPath.slice(0, targetPath.length));
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
export function tryRenamePath(targetPath, testPath, nextTargetPath) {
|
|
55
|
+
if (isEqualOrPrefix(targetPath, testPath)) {
|
|
56
|
+
return [...nextTargetPath, ...testPath.slice(nextTargetPath.length)];
|
|
57
|
+
}
|
|
58
|
+
return testPath;
|
|
59
|
+
}
|
|
60
|
+
export const PATH_SEP = '___';
|
|
61
|
+
export function pathToKey(path) {
|
|
62
|
+
return path.join(PATH_SEP);
|
|
63
|
+
}
|
|
64
|
+
// Moved from src/components/utils.js
|
|
65
|
+
export function getNextNumberedNodeName(nodes, prefix, suffix) {
|
|
66
|
+
let i = 1;
|
|
67
|
+
if (nodes) {
|
|
68
|
+
// eslint-disable-next-line no-loop-func
|
|
69
|
+
while (nodes.find(n => n.name.includes(`${prefix}${i}`))) {
|
|
70
|
+
// eslint-disable-next-line no-plusplus
|
|
71
|
+
i++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return `${prefix}${i}${suffix}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Create a new selected cell set based on a cell selection.
|
|
78
|
+
* @param {string[]} cellSelection An array of cell IDs.
|
|
79
|
+
* @param {object[]} additionalCellSets The previous array of user-defined cell sets.
|
|
80
|
+
* @param {function} setCellSetSelection The setter function for cell set selections.
|
|
81
|
+
* @param {function} setAdditionalCellSets The setter function for user-defined cell sets.
|
|
82
|
+
*/
|
|
83
|
+
export function setObsSelection(cellSelection, additionalCellSets, cellSetColor, setCellSetSelection, setAdditionalCellSets, setCellSetColor, setCellColorEncoding, prefix = 'Selection ', suffix = '') {
|
|
84
|
+
const CELL_SELECTIONS_LEVEL_ZERO_NAME = 'My Selections';
|
|
85
|
+
const selectionsLevelZeroNode = additionalCellSets?.tree.find(n => n.name === CELL_SELECTIONS_LEVEL_ZERO_NAME);
|
|
86
|
+
const nextAdditionalCellSets = {
|
|
87
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
88
|
+
datatype: SETS_DATATYPE_OBS,
|
|
89
|
+
tree: [...(additionalCellSets ? additionalCellSets.tree : [])],
|
|
90
|
+
};
|
|
91
|
+
const nextName = getNextNumberedNodeName(selectionsLevelZeroNode?.children, prefix, suffix);
|
|
92
|
+
let colorIndex = 0;
|
|
93
|
+
if (selectionsLevelZeroNode) {
|
|
94
|
+
colorIndex = selectionsLevelZeroNode.children.length;
|
|
95
|
+
selectionsLevelZeroNode.children.push({
|
|
96
|
+
name: nextName,
|
|
97
|
+
set: cellSelection.map(d => [d, null]),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
nextAdditionalCellSets.tree.push({
|
|
102
|
+
name: CELL_SELECTIONS_LEVEL_ZERO_NAME,
|
|
103
|
+
children: [
|
|
104
|
+
{
|
|
105
|
+
name: nextName,
|
|
106
|
+
set: cellSelection.map(d => [d, null]),
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
setAdditionalCellSets(nextAdditionalCellSets);
|
|
112
|
+
const nextPath = ['My Selections', nextName];
|
|
113
|
+
setCellSetColor([
|
|
114
|
+
...(cellSetColor || []),
|
|
115
|
+
{
|
|
116
|
+
path: nextPath,
|
|
117
|
+
color: PALETTE[colorIndex % PALETTE.length],
|
|
118
|
+
},
|
|
119
|
+
]);
|
|
120
|
+
setCellSetSelection([nextPath]);
|
|
121
|
+
setCellColorEncoding('cellSetSelection');
|
|
122
|
+
}
|
|
123
|
+
export function mergeObsSets(cellSets, additionalCellSets) {
|
|
124
|
+
return {
|
|
125
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
126
|
+
datatype: SETS_DATATYPE_OBS,
|
|
127
|
+
tree: [
|
|
128
|
+
...(cellSets ? cellSets.tree : []),
|
|
129
|
+
...(additionalCellSets ? additionalCellSets.tree : []),
|
|
130
|
+
],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
export function getObsInfoFromDataWithinRange(range, data) {
|
|
134
|
+
const [lowerBound, upperBound] = range;
|
|
135
|
+
const cellIds = data
|
|
136
|
+
.filter(item => item.value >= lowerBound && item.value <= upperBound)
|
|
137
|
+
.map(item => item.cellId);
|
|
138
|
+
return cellIds;
|
|
139
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.test.d.ts","sourceRoot":"","sources":["../src/utils.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { getObsInfoFromDataWithinRange } from './utils.js';
|
|
2
|
+
describe('getObsInfoFromDataWithinRange', () => {
|
|
3
|
+
it('should return the correct obs info with no gene defined', () => {
|
|
4
|
+
const data = [
|
|
5
|
+
{ value: 107.84313725490196, gene: null, cellId: '778' },
|
|
6
|
+
{ value: 75.29411764705883, gene: null, cellId: '1409' },
|
|
7
|
+
{ value: 111.76470588235294, gene: null, cellId: '3642' },
|
|
8
|
+
{ value: 34.11764705882353, gene: null, cellId: '1302' },
|
|
9
|
+
{ value: 75.68627450980392, gene: null, cellId: '1285' },
|
|
10
|
+
];
|
|
11
|
+
const range = [0, 100];
|
|
12
|
+
const expectedObsInfo = ['1409', '1302', '1285'];
|
|
13
|
+
const actualObsInfo = getObsInfoFromDataWithinRange(range, data);
|
|
14
|
+
actualObsInfo.forEach((item) => {
|
|
15
|
+
expect(expectedObsInfo).toContain(item);
|
|
16
|
+
});
|
|
17
|
+
expectedObsInfo.forEach((item) => {
|
|
18
|
+
expect(actualObsInfo).toContain(item);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
it('should return the correct obs info when gene is defined', () => {
|
|
22
|
+
const data = [
|
|
23
|
+
{ value: 107.84313725490196, gene: 'ABC', cellId: '778' },
|
|
24
|
+
{ value: 75.29411764705883, gene: 'ABC', cellId: '1409' },
|
|
25
|
+
{ value: 111.76470588235294, gene: 'ABC', cellId: '3642' },
|
|
26
|
+
{ value: 34.11764705882353, gene: 'ABC', cellId: '1302' },
|
|
27
|
+
{ value: 75.68627450980392, gene: 'ABC', cellId: '1285' },
|
|
28
|
+
];
|
|
29
|
+
const range = [0, 100];
|
|
30
|
+
const expectedObsInfo = ['1409', '1302', '1285'];
|
|
31
|
+
const actualObsInfo = getObsInfoFromDataWithinRange(range, data);
|
|
32
|
+
actualObsInfo.forEach((item) => {
|
|
33
|
+
expect(expectedObsInfo).toContain(item);
|
|
34
|
+
});
|
|
35
|
+
expectedObsInfo.forEach((item) => {
|
|
36
|
+
expect(actualObsInfo).toContain(item);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitessce/sets-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"author": "Gehlenborg Lab",
|
|
5
5
|
"homepage": "http://vitessce.io",
|
|
6
6
|
"repository": {
|
|
@@ -8,27 +8,35 @@
|
|
|
8
8
|
"url": "git+https://github.com/vitessce/vitessce.git"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/index.js",
|
|
12
13
|
"files": [
|
|
14
|
+
"src",
|
|
13
15
|
"dist",
|
|
14
|
-
"
|
|
16
|
+
"dist-tsc"
|
|
15
17
|
],
|
|
16
18
|
"dependencies": {
|
|
17
19
|
"@turf/centroid": "^6.5.0",
|
|
18
20
|
"@turf/helpers": "^6.5.0",
|
|
19
|
-
"ajv": "^6.10.0",
|
|
20
21
|
"concaveman": "^1.2.1",
|
|
21
22
|
"d3-dsv": "^1.1.1",
|
|
22
23
|
"internmap": "^2.0.3",
|
|
23
24
|
"json2csv": "^5.0.0",
|
|
24
|
-
"lodash": "^4.17.21",
|
|
25
|
+
"lodash-es": "^4.17.21",
|
|
25
26
|
"tinycolor2": "^1.4.1",
|
|
26
|
-
"uuid": "^
|
|
27
|
-
"@vitessce/
|
|
28
|
-
"@vitessce/
|
|
27
|
+
"uuid": "^9.0.0",
|
|
28
|
+
"@vitessce/schemas": "3.0.0",
|
|
29
|
+
"@vitessce/utils": "3.0.0"
|
|
29
30
|
},
|
|
30
31
|
"scripts": {
|
|
31
32
|
"bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
|
|
32
33
|
"test": "pnpm exec vitest --run -r ../../../ --dir ."
|
|
34
|
+
},
|
|
35
|
+
"module": "dist/index.js",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./dist-tsc/index.d.ts",
|
|
39
|
+
"import": "./dist/index.js"
|
|
40
|
+
}
|
|
33
41
|
}
|
|
34
42
|
}
|
|
@@ -2,10 +2,10 @@ import { InternMap } from 'internmap';
|
|
|
2
2
|
import {
|
|
3
3
|
treeInitialize,
|
|
4
4
|
nodeAppendChild,
|
|
5
|
-
} from './cell-set-utils';
|
|
5
|
+
} from './cell-set-utils.js';
|
|
6
6
|
import {
|
|
7
7
|
SETS_DATATYPE_OBS,
|
|
8
|
-
} from './constants';
|
|
8
|
+
} from './constants.js';
|
|
9
9
|
|
|
10
10
|
export function dataToCellSetsTree(data, options) {
|
|
11
11
|
// obsIndex is an array of all cell IDs, for the purposes of set complement operations only.
|
package/src/cell-set-utils.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
import uuidv4 from 'uuid
|
|
3
|
-
import isNil from 'lodash
|
|
4
|
-
import isEqual from 'lodash/isEqual';
|
|
5
|
-
import range from 'lodash/range';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
import { isNil, isEqual, range } from 'lodash-es';
|
|
6
4
|
import { featureCollection as turfFeatureCollection, point as turfPoint } from '@turf/helpers';
|
|
7
5
|
import centroid from '@turf/centroid';
|
|
8
6
|
import concaveman from 'concaveman';
|
|
9
7
|
import { getDefaultColor, PALETTE } from '@vitessce/utils';
|
|
10
8
|
import {
|
|
11
9
|
HIERARCHICAL_SCHEMAS,
|
|
12
|
-
} from './constants';
|
|
13
|
-
import { pathToKey } from './utils';
|
|
10
|
+
} from './constants.js';
|
|
11
|
+
import { pathToKey } from './utils.js';
|
|
14
12
|
|
|
15
13
|
/**
|
|
16
14
|
* Alias for the uuidv4 function to make code more readable.
|
|
@@ -291,7 +289,7 @@ export function nodeToLevelDescendantNamePaths(node, level, prevPath, stopEarly
|
|
|
291
289
|
*/
|
|
292
290
|
export function treeExport(currTree, datatype) {
|
|
293
291
|
return {
|
|
294
|
-
version: HIERARCHICAL_SCHEMAS
|
|
292
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
295
293
|
datatype,
|
|
296
294
|
tree: currTree.tree,
|
|
297
295
|
};
|
|
@@ -346,7 +344,7 @@ export function treeExportSet(currTree, nodePath) {
|
|
|
346
344
|
*/
|
|
347
345
|
export function treeInitialize(datatype) {
|
|
348
346
|
return {
|
|
349
|
-
version: HIERARCHICAL_SCHEMAS
|
|
347
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
350
348
|
datatype,
|
|
351
349
|
tree: [],
|
|
352
350
|
};
|
|
@@ -509,8 +507,9 @@ export function treeToCellPolygonsBySetNames(
|
|
|
509
507
|
* Given a tree with state, get the sizes of the
|
|
510
508
|
* sets currently marked as "visible".
|
|
511
509
|
* @param {object} currTree A tree object.
|
|
510
|
+
* @param {array} allNamePaths Array of all paths.
|
|
512
511
|
* @param {array} selectedNamePaths Array of arrays of strings,
|
|
513
|
-
* representing
|
|
512
|
+
* representing selected paths.
|
|
514
513
|
* @param {object[]} setColor Array of objects with the
|
|
515
514
|
* properties `path` and `color`.
|
|
516
515
|
* @param {string} theme "light" or "dark" for the vitessce theme
|
|
@@ -518,20 +517,38 @@ export function treeToCellPolygonsBySetNames(
|
|
|
518
517
|
* with the properties `name`, `size`, `key`,
|
|
519
518
|
* and `color`.
|
|
520
519
|
*/
|
|
521
|
-
export function treeToSetSizesBySetNames(
|
|
520
|
+
export function treeToSetSizesBySetNames(
|
|
521
|
+
currTree, allNamePaths, selectedNamePaths, setColor, theme,
|
|
522
|
+
) {
|
|
522
523
|
const sizes = [];
|
|
523
|
-
|
|
524
|
-
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Checks if a path is contained in an array of paths.
|
|
527
|
+
* @param {array} path Array of strings, which compose the path.
|
|
528
|
+
* @param {array} paths Array of arrays of strings, which compose paths.
|
|
529
|
+
* */
|
|
530
|
+
const contains = (path, paths) => paths.some(p => isEqual(p, path));
|
|
531
|
+
|
|
532
|
+
allNamePaths.forEach((clusterPath) => {
|
|
533
|
+
const node = treeFindNodeByNamePath(currTree, clusterPath);
|
|
525
534
|
if (node) {
|
|
526
535
|
const nodeSet = nodeToSet(node);
|
|
527
|
-
const nodeColor = setColor?.find(d => isEqual(d.path,
|
|
528
|
-
|
|
529
|
-
|
|
536
|
+
const nodeColor = setColor?.find(d => isEqual(d.path, clusterPath))?.color
|
|
537
|
+
|| getDefaultColor(theme);
|
|
538
|
+
const nodeProps = {
|
|
530
539
|
key: generateKey(),
|
|
531
540
|
name: node.name,
|
|
532
541
|
size: nodeSet.length,
|
|
533
542
|
color: nodeColor,
|
|
534
|
-
|
|
543
|
+
setNamePath: clusterPath,
|
|
544
|
+
// used by the CellSetSizesPlot to determine if the bar should be grayed out
|
|
545
|
+
isGrayedOut: true,
|
|
546
|
+
};
|
|
547
|
+
// if the current path is selected, we need to show it
|
|
548
|
+
if (contains(clusterPath, selectedNamePaths)) {
|
|
549
|
+
nodeProps.isGrayedOut = false;
|
|
550
|
+
}
|
|
551
|
+
sizes.push(nodeProps);
|
|
535
552
|
}
|
|
536
553
|
});
|
|
537
554
|
return sizes;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
import { cloneDeep } from 'lodash';
|
|
2
|
+
import { cloneDeep } from 'lodash-es';
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
nodeToRenderProps,
|
|
@@ -13,14 +13,14 @@ import {
|
|
|
13
13
|
nodeToLevelDescendantNamePaths,
|
|
14
14
|
treeExport,
|
|
15
15
|
filterNode,
|
|
16
|
-
|
|
16
|
+
treeToSetSizesBySetNames,
|
|
17
|
+
} from './cell-set-utils.js';
|
|
17
18
|
|
|
18
19
|
import {
|
|
19
20
|
levelTwoNodeLeaf,
|
|
20
21
|
levelZeroNode,
|
|
21
22
|
tree,
|
|
22
|
-
} from './cell-set-utils.test.fixtures';
|
|
23
|
-
|
|
23
|
+
} from './cell-set-utils.test.fixtures.js';
|
|
24
24
|
|
|
25
25
|
describe('Hierarchical sets cell-set-utils', () => {
|
|
26
26
|
describe('Node rendering', () => {
|
|
@@ -107,6 +107,7 @@ describe('Hierarchical sets cell-set-utils', () => {
|
|
|
107
107
|
[],
|
|
108
108
|
['Cell Type Annotations'],
|
|
109
109
|
);
|
|
110
|
+
|
|
110
111
|
// No nodes transformed for fals-y predicate.
|
|
111
112
|
expect(nodeTransformedWithoutPredicate.name).toEqual('Cell Type Annotations');
|
|
112
113
|
expect(
|
|
@@ -115,6 +116,106 @@ describe('Hierarchical sets cell-set-utils', () => {
|
|
|
115
116
|
});
|
|
116
117
|
});
|
|
117
118
|
|
|
119
|
+
describe('Generate properties from paths', () => {
|
|
120
|
+
it('treeToSetSizesBySetNames where one bar is selected', () => {
|
|
121
|
+
const setColor = [
|
|
122
|
+
{
|
|
123
|
+
path: ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
|
|
124
|
+
color: [255, 0, 0],
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
path: ['Cell Type Annotations', 'Vasculature'],
|
|
128
|
+
color: [68, 119, 170],
|
|
129
|
+
},
|
|
130
|
+
];
|
|
131
|
+
const setSizes = treeToSetSizesBySetNames(
|
|
132
|
+
tree,
|
|
133
|
+
[['Cell Type Annotations', 'Vasculature', 'Endothelial'], ['Cell Type Annotations', 'Vasculature']],
|
|
134
|
+
[['Cell Type Annotations', 'Vasculature', 'Endothelial']],
|
|
135
|
+
setColor,
|
|
136
|
+
'dark',
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
expect(setSizes.length).toEqual(2);
|
|
140
|
+
expect(setSizes[0].name).toEqual('Endothelial');
|
|
141
|
+
expect(setSizes[0].size).toEqual(3);
|
|
142
|
+
expect(setSizes[0].color).toEqual([255, 0, 0]);
|
|
143
|
+
expect(setSizes[0].isGrayedOut).toEqual(false);
|
|
144
|
+
expect(setSizes[0].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature', 'Endothelial']);
|
|
145
|
+
|
|
146
|
+
expect(setSizes[1].name).toEqual('Vasculature');
|
|
147
|
+
expect(setSizes[1].size).toEqual(9);
|
|
148
|
+
expect(setSizes[1].color).toEqual([68, 119, 170]);
|
|
149
|
+
expect(setSizes[1].isGrayedOut).toEqual(true);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('treeToSetSizesBySetNames where nothing is selected', () => {
|
|
153
|
+
const setColor = [
|
|
154
|
+
{
|
|
155
|
+
path: ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
|
|
156
|
+
color: [255, 0, 0],
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
path: ['Cell Type Annotations', 'Vasculature'],
|
|
160
|
+
color: [68, 119, 170],
|
|
161
|
+
},
|
|
162
|
+
];
|
|
163
|
+
const setSizes = treeToSetSizesBySetNames(
|
|
164
|
+
tree,
|
|
165
|
+
[['Cell Type Annotations', 'Vasculature', 'Endothelial'], ['Cell Type Annotations', 'Vasculature']],
|
|
166
|
+
[],
|
|
167
|
+
setColor,
|
|
168
|
+
'dark',
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
expect(setSizes.length).toEqual(2);
|
|
172
|
+
expect(setSizes[0].name).toEqual('Endothelial');
|
|
173
|
+
expect(setSizes[0].size).toEqual(3);
|
|
174
|
+
expect(setSizes[0].color).toEqual([255, 0, 0]);
|
|
175
|
+
expect(setSizes[0].isGrayedOut).toEqual(true);
|
|
176
|
+
expect(setSizes[0].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature', 'Endothelial']);
|
|
177
|
+
|
|
178
|
+
expect(setSizes[1].name).toEqual('Vasculature');
|
|
179
|
+
expect(setSizes[1].size).toEqual(9);
|
|
180
|
+
expect(setSizes[1].color).toEqual([68, 119, 170]);
|
|
181
|
+
expect(setSizes[1].isGrayedOut).toEqual(true);
|
|
182
|
+
expect(setSizes[1].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature']);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('treeToSetSizesBySetNames where selected paths are not part of all paths', () => {
|
|
186
|
+
const setColor = [
|
|
187
|
+
{
|
|
188
|
+
path: ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
|
|
189
|
+
color: [255, 0, 0],
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
path: ['Cell Type Annotations', 'Vasculature'],
|
|
193
|
+
color: [68, 119, 170],
|
|
194
|
+
},
|
|
195
|
+
];
|
|
196
|
+
const setSizes = treeToSetSizesBySetNames(
|
|
197
|
+
tree,
|
|
198
|
+
[['Cell Type Annotations', 'Vasculature', 'Endothelial'], ['Cell Type Annotations', 'Vasculature']],
|
|
199
|
+
[['Louvain Clusters', '0'], ['Louvain Clusters', '1']],
|
|
200
|
+
setColor,
|
|
201
|
+
'dark',
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
expect(setSizes.length).toEqual(2);
|
|
205
|
+
expect(setSizes[0].name).toEqual('Endothelial');
|
|
206
|
+
expect(setSizes[0].size).toEqual(3);
|
|
207
|
+
expect(setSizes[0].color).toEqual([255, 0, 0]);
|
|
208
|
+
expect(setSizes[0].isGrayedOut).toEqual(true);
|
|
209
|
+
expect(setSizes[0].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature', 'Endothelial']);
|
|
210
|
+
|
|
211
|
+
expect(setSizes[1].name).toEqual('Vasculature');
|
|
212
|
+
expect(setSizes[1].size).toEqual(9);
|
|
213
|
+
expect(setSizes[1].color).toEqual([68, 119, 170]);
|
|
214
|
+
expect(setSizes[1].isGrayedOut).toEqual(true);
|
|
215
|
+
expect(setSizes[1].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature']);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
118
219
|
describe('Get from tree', () => {
|
|
119
220
|
it('Tree Union', () => {
|
|
120
221
|
const union = treeToUnion(
|
package/src/constants.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { obsSetsSchema, obsSetsTabularSchema } from '@vitessce/
|
|
1
|
+
import { obsSetsSchema, obsSetsTabularSchema } from '@vitessce/schemas';
|
|
2
2
|
|
|
3
3
|
export const FILE_EXTENSION_JSON = 'json';
|
|
4
4
|
export const MIME_TYPE_JSON = 'application/json';
|
|
@@ -12,21 +12,10 @@ export const NA_VALUE_TABULAR = 'NA';
|
|
|
12
12
|
|
|
13
13
|
export const SETS_DATATYPE_OBS = 'obs';
|
|
14
14
|
export const HIERARCHICAL_SCHEMAS = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
schema: obsSetsSchema,
|
|
18
|
-
},
|
|
19
|
-
obs: {
|
|
20
|
-
latestVersion: '0.1.3',
|
|
21
|
-
schema: obsSetsSchema,
|
|
22
|
-
},
|
|
15
|
+
latestVersion: '0.1.3',
|
|
16
|
+
schema: obsSetsSchema,
|
|
23
17
|
};
|
|
24
18
|
|
|
25
19
|
export const TABULAR_SCHEMAS = {
|
|
26
|
-
|
|
27
|
-
schema: obsSetsTabularSchema,
|
|
28
|
-
},
|
|
29
|
-
obs: {
|
|
30
|
-
schema: obsSetsTabularSchema,
|
|
31
|
-
},
|
|
20
|
+
schema: obsSetsTabularSchema,
|
|
32
21
|
};
|
package/src/index.js
CHANGED
|
@@ -22,7 +22,12 @@ export {
|
|
|
22
22
|
getCellSetPolygons,
|
|
23
23
|
treeToMembershipMap,
|
|
24
24
|
nodeToSet,
|
|
25
|
-
} from './cell-set-utils';
|
|
25
|
+
} from './cell-set-utils.js';
|
|
26
|
+
export {
|
|
27
|
+
findLongestCommonPath,
|
|
28
|
+
filterPathsByExpansionAndSelection,
|
|
29
|
+
findChangedHierarchy,
|
|
30
|
+
} from './set-path-utils.js';
|
|
26
31
|
export {
|
|
27
32
|
isEqualOrPrefix,
|
|
28
33
|
tryRenamePath,
|
|
@@ -34,7 +39,8 @@ export {
|
|
|
34
39
|
callbackOnKeyPress,
|
|
35
40
|
getLevelTooltipText,
|
|
36
41
|
pathToKey,
|
|
37
|
-
|
|
42
|
+
getObsInfoFromDataWithinRange,
|
|
43
|
+
} from './utils.js';
|
|
38
44
|
export {
|
|
39
45
|
downloadForUser,
|
|
40
46
|
handleExportJSON,
|
|
@@ -42,14 +48,14 @@ export {
|
|
|
42
48
|
tryUpgradeTreeToLatestSchema,
|
|
43
49
|
handleImportJSON,
|
|
44
50
|
handleImportTabular,
|
|
45
|
-
} from './io';
|
|
51
|
+
} from './io.js';
|
|
46
52
|
export {
|
|
47
53
|
FILE_EXTENSION_JSON,
|
|
48
54
|
FILE_EXTENSION_TABULAR,
|
|
49
55
|
SETS_DATATYPE_OBS,
|
|
50
56
|
MIME_TYPE_JSON,
|
|
51
57
|
MIME_TYPE_TABULAR,
|
|
52
|
-
} from './constants';
|
|
58
|
+
} from './constants.js';
|
|
53
59
|
export {
|
|
54
60
|
dataToCellSetsTree,
|
|
55
|
-
} from './CellSetsZarrLoader';
|
|
61
|
+
} from './CellSetsZarrLoader.js';
|
package/src/io.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import isNil from 'lodash/isNil';
|
|
1
|
+
import { isNil } from 'lodash-es';
|
|
3
2
|
import { dsvFormat } from 'd3-dsv';
|
|
4
3
|
// TODO(monorepo): try to find a different package for this.
|
|
5
4
|
// Reference: https://github.com/zemirco/json2csv/issues/539
|
|
6
|
-
import { Parser } from 'json2csv/dist/json2csv.umd';
|
|
5
|
+
import { Parser } from 'json2csv/dist/json2csv.umd.js';
|
|
7
6
|
import { getDefaultColor } from '@vitessce/utils';
|
|
8
|
-
import { colorArrayToString, colorStringToArray } from './utils';
|
|
9
|
-
import { nodeTransform } from './cell-set-utils';
|
|
7
|
+
import { colorArrayToString, colorStringToArray } from './utils.js';
|
|
10
8
|
import {
|
|
11
9
|
HIERARCHICAL_SCHEMAS, TABULAR_SCHEMAS,
|
|
12
10
|
MIME_TYPE_JSON, MIME_TYPE_TABULAR,
|
|
13
11
|
SEPARATOR_TABULAR, NA_VALUE_TABULAR,
|
|
14
|
-
} from './constants';
|
|
12
|
+
} from './constants.js';
|
|
15
13
|
|
|
16
14
|
/**
|
|
17
15
|
* Check if an imported tree has an old schema version that we know how to
|
|
@@ -19,32 +17,23 @@ import {
|
|
|
19
17
|
* @param {object} currTree A hierarchical tree object with a .version property,
|
|
20
18
|
* which has already passed schema validation, but may not have the latest schema version.
|
|
21
19
|
* @param {string} datatype The data type of the items in the schema.
|
|
20
|
+
* @returns {[object, boolean]} The upgraded tree object and a boolean indicating
|
|
21
|
+
* whether the tree was upgraded.
|
|
22
22
|
*/
|
|
23
|
+
// eslint-disable-next-line no-unused-vars
|
|
23
24
|
export function tryUpgradeTreeToLatestSchema(currTree, datatype) {
|
|
24
|
-
const
|
|
25
|
-
const
|
|
25
|
+
const zodSchema = HIERARCHICAL_SCHEMAS.schema;
|
|
26
|
+
const latestSchemaVersion = HIERARCHICAL_SCHEMAS.latestVersion;
|
|
27
|
+
const parseResult = zodSchema.safeParse(currTree);
|
|
28
|
+
const valid = parseResult.success;
|
|
26
29
|
if (!valid) {
|
|
27
|
-
const failureReason = JSON.stringify(
|
|
30
|
+
const failureReason = JSON.stringify(parseResult.error.message, null, 2);
|
|
28
31
|
throw new Error(`Tree validation failed: ${failureReason}`);
|
|
29
|
-
} else if (currTree.datatype !== datatype) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
`The data type does not match the expected data type of '${datatype}'.`,
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
if (currTree.version === '0.1.2') {
|
|
35
|
-
// To upgrade from cell-sets schema 0.1.2 to 0.1.3,
|
|
36
|
-
// add a confidence value of null for each cell ID.
|
|
37
|
-
return {
|
|
38
|
-
...currTree,
|
|
39
|
-
version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
|
|
40
|
-
tree: currTree.tree.map(levelZeroNode => nodeTransform(
|
|
41
|
-
levelZeroNode,
|
|
42
|
-
n => !n.children && Array.isArray(n.set),
|
|
43
|
-
n => ({ ...n, set: n.set.map(itemId => ([itemId, null])) }), [],
|
|
44
|
-
)),
|
|
45
|
-
};
|
|
46
32
|
}
|
|
47
|
-
|
|
33
|
+
// Zod will not only validate, but also will upgrade from v0.1.2 to v0.1.3 via transform.
|
|
34
|
+
// We also want to return a boolean indicating whether the tree was upgraded.
|
|
35
|
+
const didUpgrade = (currTree.version !== latestSchemaVersion);
|
|
36
|
+
return [parseResult.data, didUpgrade];
|
|
48
37
|
}
|
|
49
38
|
|
|
50
39
|
/**
|
|
@@ -59,7 +48,7 @@ export function tryUpgradeTreeToLatestSchema(currTree, datatype) {
|
|
|
59
48
|
export function handleImportJSON(result, datatype, theme) {
|
|
60
49
|
let importData = JSON.parse(result);
|
|
61
50
|
// Validate the imported file.
|
|
62
|
-
importData = tryUpgradeTreeToLatestSchema(importData, datatype);
|
|
51
|
+
[importData] = tryUpgradeTreeToLatestSchema(importData, datatype);
|
|
63
52
|
return importData;
|
|
64
53
|
}
|
|
65
54
|
|
|
@@ -88,25 +77,26 @@ export function handleImportTabular(result, datatype, theme) {
|
|
|
88
77
|
),
|
|
89
78
|
}));
|
|
90
79
|
// Validate the imported file.
|
|
91
|
-
const
|
|
92
|
-
const
|
|
80
|
+
const zodSchema = TABULAR_SCHEMAS.schema;
|
|
81
|
+
const parseResult = zodSchema.safeParse(importData);
|
|
82
|
+
const valid = parseResult.success;
|
|
93
83
|
if (!valid) {
|
|
94
|
-
const failureReason = JSON.stringify(
|
|
84
|
+
const failureReason = JSON.stringify(parseResult.error.message, null, 2);
|
|
95
85
|
throw new Error(`Import validation failed: ${failureReason}`);
|
|
96
86
|
} else {
|
|
87
|
+
const parsedData = parseResult.data;
|
|
97
88
|
// Convert the validated array to a tree representation.
|
|
98
89
|
const treeToImport = {
|
|
99
|
-
version: HIERARCHICAL_SCHEMAS
|
|
100
|
-
datatype,
|
|
90
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
101
91
|
tree: [],
|
|
102
92
|
};
|
|
103
|
-
const uniqueGroupNames = Array.from(new Set(
|
|
93
|
+
const uniqueGroupNames = Array.from(new Set(parsedData.map(d => d.groupName)));
|
|
104
94
|
uniqueGroupNames.forEach((groupName) => {
|
|
105
95
|
const levelZeroNode = {
|
|
106
96
|
name: groupName,
|
|
107
97
|
children: [],
|
|
108
98
|
};
|
|
109
|
-
const groupRows =
|
|
99
|
+
const groupRows = parsedData.filter(d => d.groupName === groupName);
|
|
110
100
|
const uniqueSetNames = Array.from(new Set(groupRows.map(d => d.setName)));
|
|
111
101
|
uniqueSetNames.forEach((setName) => {
|
|
112
102
|
const setRows = groupRows.filter(d => d.setName === setName);
|