@vitessce/sets-utils 2.0.3-beta.0 → 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.
Files changed (55) hide show
  1. package/dist/index.js +16700 -5
  2. package/dist-tsc/CellSetsZarrLoader.d.ts +2 -0
  3. package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -0
  4. package/{dist → dist-tsc}/CellSetsZarrLoader.js +9 -5
  5. package/dist-tsc/CellSetsZarrLoader.test.d.ts +2 -0
  6. package/dist-tsc/CellSetsZarrLoader.test.d.ts.map +1 -0
  7. package/{dist → dist-tsc}/CellSetsZarrLoader.test.js +10 -4
  8. package/dist-tsc/cell-set-utils.d.ts +204 -0
  9. package/dist-tsc/cell-set-utils.d.ts.map +1 -0
  10. package/{dist → dist-tsc}/cell-set-utils.js +28 -15
  11. package/dist-tsc/cell-set-utils.test.d.ts +2 -0
  12. package/dist-tsc/cell-set-utils.test.d.ts.map +1 -0
  13. package/dist-tsc/cell-set-utils.test.fixtures.d.ts +47 -0
  14. package/dist-tsc/cell-set-utils.test.fixtures.d.ts.map +1 -0
  15. package/{dist → dist-tsc}/cell-set-utils.test.js +76 -3
  16. package/dist-tsc/constants.d.ts +17 -0
  17. package/dist-tsc/constants.d.ts.map +1 -0
  18. package/{dist → dist-tsc}/constants.js +4 -15
  19. package/dist-tsc/index.d.ts +7 -0
  20. package/dist-tsc/index.d.ts.map +1 -0
  21. package/dist-tsc/index.js +6 -0
  22. package/dist-tsc/io.d.ts +47 -0
  23. package/dist-tsc/io.d.ts.map +1 -0
  24. package/{dist → dist-tsc}/io.js +25 -30
  25. package/dist-tsc/io.test.d.ts +2 -0
  26. package/dist-tsc/io.test.d.ts.map +1 -0
  27. package/{dist → dist-tsc}/io.test.js +1 -4
  28. package/dist-tsc/set-path-utils.d.ts +28 -0
  29. package/dist-tsc/set-path-utils.d.ts.map +1 -0
  30. package/dist-tsc/set-path-utils.js +133 -0
  31. package/dist-tsc/set-path-utils.test.d.ts +2 -0
  32. package/dist-tsc/set-path-utils.test.d.ts.map +1 -0
  33. package/dist-tsc/set-path-utils.test.js +210 -0
  34. package/dist-tsc/utils.d.ts +45 -0
  35. package/dist-tsc/utils.d.ts.map +1 -0
  36. package/{dist → dist-tsc}/utils.js +16 -9
  37. package/dist-tsc/utils.test.d.ts +2 -0
  38. package/dist-tsc/utils.test.d.ts.map +1 -0
  39. package/dist-tsc/utils.test.js +39 -0
  40. package/package.json +17 -9
  41. package/src/CellSetsZarrLoader.js +111 -0
  42. package/src/CellSetsZarrLoader.test.js +163 -0
  43. package/src/cell-set-utils.js +700 -0
  44. package/src/cell-set-utils.test.fixtures.js +71 -0
  45. package/src/cell-set-utils.test.js +277 -0
  46. package/src/constants.js +21 -0
  47. package/src/index.js +61 -0
  48. package/src/io.js +172 -0
  49. package/src/io.test.js +142 -0
  50. package/src/set-path-utils.js +155 -0
  51. package/src/set-path-utils.test.js +257 -0
  52. package/src/utils.js +160 -0
  53. package/src/utils.test.js +48 -0
  54. package/dist/hooks.js +0 -64
  55. /package/{dist → dist-tsc}/cell-set-utils.test.fixtures.js +0 -0
package/src/utils.js ADDED
@@ -0,0 +1,160 @@
1
+ import tinycolor from 'tinycolor2';
2
+ import { isEqual } from 'lodash-es';
3
+ import { PALETTE } from '@vitessce/utils';
4
+ import {
5
+ SETS_DATATYPE_OBS,
6
+ HIERARCHICAL_SCHEMAS,
7
+ } from './constants.js';
8
+
9
+ /**
10
+ * Execute a callback function based on a keypress event.
11
+ * @param {object} event The event from onKeyPress
12
+ * @param {string} key The key identifier to match.
13
+ * @param {Function} callback The function to execute.
14
+ */
15
+ export function callbackOnKeyPress(event, key, callback) {
16
+ if (event.key === key) {
17
+ event.preventDefault();
18
+ callback();
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Convert an array of [r, g, b] numbers to a hex color.
24
+ * @param {number[]} rgbArray The color [r, g, b] array.
25
+ * @returns {string} The hex color as a string.
26
+ */
27
+ export function colorArrayToString(rgbArray) {
28
+ return tinycolor({ r: rgbArray[0], g: rgbArray[1], b: rgbArray[2] }).toHexString();
29
+ }
30
+
31
+ /**
32
+ * Convert a string color representation to an array of [r,g,b].
33
+ * @param {string} colorString The color as a string.
34
+ * @returns {number[]} The color as an array.
35
+ */
36
+ export function colorStringToArray(colorString) {
37
+ const colorObj = tinycolor(colorString).toRgb();
38
+ return [colorObj.r, colorObj.g, colorObj.b];
39
+ }
40
+
41
+ /**
42
+ * Get a string of help text for coloring a particular hierarchy level.
43
+ * @param {integer} i The level. 1 for cluster, 2 for subcluster, etc.
44
+ * @returns {string} The tooltip text for coloring the level.
45
+ */
46
+ export function getLevelTooltipText(i) {
47
+ if (i === 0) return 'Color by hierarchy';
48
+ if (i <= 2) {
49
+ const subs = j => ('sub'.repeat(j));
50
+ return `Color by ${subs(i - 1)}cluster`;
51
+ }
52
+ return `Color by cluster level ${i}`;
53
+ }
54
+
55
+ export function isEqualOrPrefix(targetPath, testPath) {
56
+ if (targetPath.length <= testPath.length) {
57
+ return isEqual(targetPath, testPath.slice(0, targetPath.length));
58
+ }
59
+ return false;
60
+ }
61
+
62
+ export function tryRenamePath(targetPath, testPath, nextTargetPath) {
63
+ if (isEqualOrPrefix(targetPath, testPath)) {
64
+ return [...nextTargetPath, ...testPath.slice(nextTargetPath.length)];
65
+ }
66
+ return testPath;
67
+ }
68
+
69
+ export const PATH_SEP = '___';
70
+
71
+ export function pathToKey(path) {
72
+ return path.join(PATH_SEP);
73
+ }
74
+
75
+ // Moved from src/components/utils.js
76
+
77
+ export function getNextNumberedNodeName(nodes, prefix, suffix) {
78
+ let i = 1;
79
+ if (nodes) {
80
+ // eslint-disable-next-line no-loop-func
81
+ while (nodes.find(n => n.name.includes(`${prefix}${i}`))) {
82
+ // eslint-disable-next-line no-plusplus
83
+ i++;
84
+ }
85
+ }
86
+ return `${prefix}${i}${suffix}`;
87
+ }
88
+
89
+ /**
90
+ * Create a new selected cell set based on a cell selection.
91
+ * @param {string[]} cellSelection An array of cell IDs.
92
+ * @param {object[]} additionalCellSets The previous array of user-defined cell sets.
93
+ * @param {function} setCellSetSelection The setter function for cell set selections.
94
+ * @param {function} setAdditionalCellSets The setter function for user-defined cell sets.
95
+ */
96
+ export function setObsSelection(cellSelection, additionalCellSets, cellSetColor, setCellSetSelection, setAdditionalCellSets, setCellSetColor, setCellColorEncoding, prefix = 'Selection ', suffix = '') {
97
+ const CELL_SELECTIONS_LEVEL_ZERO_NAME = 'My Selections';
98
+
99
+ const selectionsLevelZeroNode = additionalCellSets?.tree.find(
100
+ n => n.name === CELL_SELECTIONS_LEVEL_ZERO_NAME,
101
+ );
102
+ const nextAdditionalCellSets = {
103
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
104
+ datatype: SETS_DATATYPE_OBS,
105
+ tree: [...(additionalCellSets ? additionalCellSets.tree : [])],
106
+ };
107
+
108
+ const nextName = getNextNumberedNodeName(selectionsLevelZeroNode?.children, prefix, suffix);
109
+
110
+ let colorIndex = 0;
111
+ if (selectionsLevelZeroNode) {
112
+ colorIndex = selectionsLevelZeroNode.children.length;
113
+ selectionsLevelZeroNode.children.push({
114
+ name: nextName,
115
+ set: cellSelection.map(d => [d, null]),
116
+ });
117
+ } else {
118
+ nextAdditionalCellSets.tree.push({
119
+ name: CELL_SELECTIONS_LEVEL_ZERO_NAME,
120
+ children: [
121
+ {
122
+ name: nextName,
123
+ set: cellSelection.map(d => [d, null]),
124
+ },
125
+ ],
126
+ });
127
+ }
128
+ setAdditionalCellSets(nextAdditionalCellSets);
129
+ const nextPath = ['My Selections', nextName];
130
+ setCellSetColor([
131
+ ...(cellSetColor || []),
132
+ {
133
+ path: nextPath,
134
+ color: PALETTE[colorIndex % PALETTE.length],
135
+ },
136
+ ]);
137
+ setCellSetSelection([nextPath]);
138
+ setCellColorEncoding('cellSetSelection');
139
+ }
140
+
141
+ export function mergeObsSets(cellSets, additionalCellSets) {
142
+ return {
143
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
144
+ datatype: SETS_DATATYPE_OBS,
145
+ tree: [
146
+ ...(cellSets ? cellSets.tree : []),
147
+ ...(additionalCellSets ? additionalCellSets.tree : []),
148
+ ],
149
+ };
150
+ }
151
+
152
+ export function getObsInfoFromDataWithinRange(range, data) {
153
+ const [lowerBound, upperBound] = range;
154
+
155
+ const cellIds = data
156
+ .filter(item => item.value >= lowerBound && item.value <= upperBound)
157
+ .map(item => item.cellId);
158
+
159
+ return cellIds;
160
+ }
@@ -0,0 +1,48 @@
1
+ import { getObsInfoFromDataWithinRange } from './utils.js';
2
+
3
+
4
+ describe('getObsInfoFromDataWithinRange', () => {
5
+ it('should return the correct obs info with no gene defined', () => {
6
+ const data = [
7
+ { value: 107.84313725490196, gene: null, cellId: '778' },
8
+ { value: 75.29411764705883, gene: null, cellId: '1409' },
9
+ { value: 111.76470588235294, gene: null, cellId: '3642' },
10
+ { value: 34.11764705882353, gene: null, cellId: '1302' },
11
+ { value: 75.68627450980392, gene: null, cellId: '1285' },
12
+ ];
13
+ const range = [0, 100];
14
+ const expectedObsInfo = ['1409', '1302', '1285'];
15
+
16
+ const actualObsInfo = getObsInfoFromDataWithinRange(range, data);
17
+
18
+ actualObsInfo.forEach((item) => {
19
+ expect(expectedObsInfo).toContain(item);
20
+ });
21
+
22
+ expectedObsInfo.forEach((item) => {
23
+ expect(actualObsInfo).toContain(item);
24
+ });
25
+ });
26
+
27
+ it('should return the correct obs info when gene is defined', () => {
28
+ const data = [
29
+ { value: 107.84313725490196, gene: 'ABC', cellId: '778' },
30
+ { value: 75.29411764705883, gene: 'ABC', cellId: '1409' },
31
+ { value: 111.76470588235294, gene: 'ABC', cellId: '3642' },
32
+ { value: 34.11764705882353, gene: 'ABC', cellId: '1302' },
33
+ { value: 75.68627450980392, gene: 'ABC', cellId: '1285' },
34
+ ];
35
+ const range = [0, 100];
36
+ const expectedObsInfo = ['1409', '1302', '1285'];
37
+
38
+ const actualObsInfo = getObsInfoFromDataWithinRange(range, data);
39
+
40
+ actualObsInfo.forEach((item) => {
41
+ expect(expectedObsInfo).toContain(item);
42
+ });
43
+
44
+ expectedObsInfo.forEach((item) => {
45
+ expect(actualObsInfo).toContain(item);
46
+ });
47
+ });
48
+ });
package/dist/hooks.js DELETED
@@ -1,64 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { getValueTransformFunction } from '@vitessce/utils';
3
- import { treeToObjectsBySetNames, treeToSetSizesBySetNames } from './cell-set-utils';
4
- import { mergeObsSets } from './utils';
5
- /**
6
- * Get expression data for the cells
7
- * in the selected cell sets.
8
- * @param {object} expressionMatrix
9
- * @param {string[]} expressionMatrix.rows Cell IDs.
10
- * @param {string[]} expressionMatrix.cols Gene names.
11
- * @param {Uint8Array} expressionMatrix.matrix The
12
- * flattened expression matrix as a typed array.
13
- * @param {object} cellSets The cell sets from the dataset.
14
- * @param {object} additionalCellSets The user-defined cell sets
15
- * from the coordination space.
16
- * @param {array} geneSelection Array of selected genes.
17
- * @param {array} cellSetSelection Array of selected cell set paths.
18
- * @param {object[]} cellSetColor Array of objects with properties
19
- * @param {string|null} featureValueTransform The name of the
20
- * feature value transform function.
21
- * @param {number} featureValueTransformCoefficient A coefficient
22
- * to be used in the transform function.
23
- * @param {string} theme "light" or "dark" for the vitessce theme
24
- * `path` and `color`.
25
- */
26
- export function useExpressionByCellSet(expressionData, obsIndex, cellSets, additionalCellSets, geneSelection, cellSetSelection, cellSetColor, featureValueTransform, featureValueTransformCoefficient, theme) {
27
- const mergedCellSets = useMemo(() => mergeObsSets(cellSets, additionalCellSets), [cellSets, additionalCellSets]);
28
- // From the expression matrix and the list of selected genes / cell sets,
29
- // generate the array of data points for the plot.
30
- const [expressionArr, expressionMax] = useMemo(() => {
31
- if (mergedCellSets && cellSetSelection
32
- && geneSelection && geneSelection.length >= 1
33
- && expressionData) {
34
- const cellObjects = treeToObjectsBySetNames(mergedCellSets, cellSetSelection, cellSetColor, theme);
35
- const firstGeneSelected = geneSelection[0];
36
- // Create new cellColors map based on the selected gene.
37
- let exprMax = -Infinity;
38
- const cellIndices = {};
39
- for (let i = 0; i < obsIndex.length; i += 1) {
40
- cellIndices[obsIndex[i]] = i;
41
- }
42
- const exprValues = cellObjects.map((cell) => {
43
- const cellIndex = cellIndices[cell.obsId];
44
- const value = expressionData[0][cellIndex];
45
- const normValue = value * 100 / 255;
46
- const transformFunction = getValueTransformFunction(featureValueTransform, featureValueTransformCoefficient);
47
- const transformedValue = transformFunction(normValue);
48
- exprMax = Math.max(transformedValue, exprMax);
49
- return { value: transformedValue, gene: firstGeneSelected, set: cell.name };
50
- });
51
- return [exprValues, exprMax];
52
- }
53
- return [null, null];
54
- }, [expressionData, obsIndex, geneSelection, theme,
55
- mergedCellSets, cellSetSelection, cellSetColor,
56
- featureValueTransform, featureValueTransformCoefficient,
57
- ]);
58
- // From the cell sets hierarchy and the list of selected cell sets,
59
- // generate the array of set sizes data points for the bar plot.
60
- const setArr = useMemo(() => (mergedCellSets && cellSetSelection && cellSetColor
61
- ? treeToSetSizesBySetNames(mergedCellSets, cellSetSelection, cellSetColor, theme)
62
- : []), [mergedCellSets, cellSetSelection, cellSetColor, theme]);
63
- return [expressionArr, setArr, expressionMax];
64
- }