@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
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Check if an imported tree has an old schema version that we know how to
3
+ * "upgrade" to the latest schema version. Validate against the schema.
4
+ * @param {object} currTree A hierarchical tree object with a .version property,
5
+ * which has already passed schema validation, but may not have the latest schema version.
6
+ * @param {string} datatype The data type of the items in the schema.
7
+ * @returns {[object, boolean]} The upgraded tree object and a boolean indicating
8
+ * whether the tree was upgraded.
9
+ */
10
+ export function tryUpgradeTreeToLatestSchema(currTree: object, datatype: string): [object, boolean];
11
+ /**
12
+ * Handler for JSON imports. Validates and upgrades against the hierarchical sets schema.
13
+ * @param {string} result The data passed from the FileReader as a string.
14
+ * @param {string} datatype The data type to validate against.
15
+ * @param {string} theme "light" or "dark" for the vitessce theme
16
+ * @returns {object} The imported tree object.
17
+ * @throws {Error} Throws error if validation fails or if the datatype does not match.
18
+ */
19
+ export function handleImportJSON(result: string, datatype: string, theme: string): object;
20
+ /**
21
+ * Handler for tabular imports. Validates against the tabular sets schema.
22
+ * @param {string} result The data passed from the FileReader as a string.
23
+ * @param {string} datatype The data type to validate against.
24
+ * @param {string} theme "light" or "dark" for the vitessce theme
25
+ * @returns {object} The imported tree object.
26
+ * @throws {Error} Throws error if validation fails or if the datatype does not match.
27
+ */
28
+ export function handleImportTabular(result: string, datatype: string, theme: string): object;
29
+ /**
30
+ * Convert a tree object to a JSON representation.
31
+ * @param {object} result The object to export.
32
+ * @returns {string} The data in a string representation.
33
+ */
34
+ export function handleExportJSON(result: object): string;
35
+ /**
36
+ * Convert a tree object with one level (height === 1) to a tabular representation.
37
+ * @param {object} result The object to export.
38
+ * @returns {string} The data in a string representation.
39
+ */
40
+ export function handleExportTabular(result: object): string;
41
+ /**
42
+ * Download a file. Appends and removes an anchor node in the DOM.
43
+ * @param {string} dataString The function that converts the data to a string.
44
+ * @param {string} fileName The name of the file to be downloaded.
45
+ */
46
+ export function downloadForUser(dataString: string, fileName: string): void;
47
+ //# sourceMappingURL=io.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"io.d.ts","sourceRoot":"","sources":["../src/io.js"],"names":[],"mappings":"AAaA;;;;;;;;GAQG;AAEH,uDAPW,MAAM,YAEN,MAAM,GACJ,CAAC,MAAM,EAAE,OAAO,CAAC,CAiB7B;AAED;;;;;;;GAOG;AAEH,yCAPW,MAAM,YACN,MAAM,SACN,MAAM,GACJ,MAAM,CASlB;AAED;;;;;;;GAOG;AACH,4CANW,MAAM,YACN,MAAM,SACN,MAAM,GACJ,MAAM,CAuDlB;AAED;;;;GAIG;AACH,yCAHW,MAAM,GACJ,MAAM,CAMlB;AAED;;;;GAIG;AACH,4CAHW,MAAM,GACJ,MAAM,CA2BlB;AAED;;;;GAIG;AACH,4CAHW,MAAM,YACN,MAAM,QAShB"}
@@ -1,40 +1,34 @@
1
- import Ajv from 'ajv';
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';
10
- import { HIERARCHICAL_SCHEMAS, TABULAR_SCHEMAS, MIME_TYPE_JSON, MIME_TYPE_TABULAR, SEPARATOR_TABULAR, NA_VALUE_TABULAR, } from './constants';
7
+ import { colorArrayToString, colorStringToArray } from './utils.js';
8
+ import { HIERARCHICAL_SCHEMAS, TABULAR_SCHEMAS, MIME_TYPE_JSON, MIME_TYPE_TABULAR, SEPARATOR_TABULAR, NA_VALUE_TABULAR, } from './constants.js';
11
9
  /**
12
10
  * Check if an imported tree has an old schema version that we know how to
13
11
  * "upgrade" to the latest schema version. Validate against the schema.
14
12
  * @param {object} currTree A hierarchical tree object with a .version property,
15
13
  * which has already passed schema validation, but may not have the latest schema version.
16
14
  * @param {string} datatype The data type of the items in the schema.
15
+ * @returns {[object, boolean]} The upgraded tree object and a boolean indicating
16
+ * whether the tree was upgraded.
17
17
  */
18
+ // eslint-disable-next-line no-unused-vars
18
19
  export function tryUpgradeTreeToLatestSchema(currTree, datatype) {
19
- const validate = new Ajv().compile(HIERARCHICAL_SCHEMAS[datatype].schema);
20
- const valid = validate(currTree);
20
+ const zodSchema = HIERARCHICAL_SCHEMAS.schema;
21
+ const latestSchemaVersion = HIERARCHICAL_SCHEMAS.latestVersion;
22
+ const parseResult = zodSchema.safeParse(currTree);
23
+ const valid = parseResult.success;
21
24
  if (!valid) {
22
- const failureReason = JSON.stringify(validate.errors, null, 2);
25
+ const failureReason = JSON.stringify(parseResult.error.message, null, 2);
23
26
  throw new Error(`Tree validation failed: ${failureReason}`);
24
27
  }
25
- else if (currTree.datatype !== datatype) {
26
- throw new Error(`The data type does not match the expected data type of '${datatype}'.`);
27
- }
28
- if (currTree.version === '0.1.2') {
29
- // To upgrade from cell-sets schema 0.1.2 to 0.1.3,
30
- // add a confidence value of null for each cell ID.
31
- return {
32
- ...currTree,
33
- version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
34
- tree: currTree.tree.map(levelZeroNode => nodeTransform(levelZeroNode, n => !n.children && Array.isArray(n.set), n => ({ ...n, set: n.set.map(itemId => ([itemId, null])) }), [])),
35
- };
36
- }
37
- return currTree;
28
+ // Zod will not only validate, but also will upgrade from v0.1.2 to v0.1.3 via transform.
29
+ // We also want to return a boolean indicating whether the tree was upgraded.
30
+ const didUpgrade = (currTree.version !== latestSchemaVersion);
31
+ return [parseResult.data, didUpgrade];
38
32
  }
39
33
  /**
40
34
  * Handler for JSON imports. Validates and upgrades against the hierarchical sets schema.
@@ -48,7 +42,7 @@ export function tryUpgradeTreeToLatestSchema(currTree, datatype) {
48
42
  export function handleImportJSON(result, datatype, theme) {
49
43
  let importData = JSON.parse(result);
50
44
  // Validate the imported file.
51
- importData = tryUpgradeTreeToLatestSchema(importData, datatype);
45
+ [importData] = tryUpgradeTreeToLatestSchema(importData, datatype);
52
46
  return importData;
53
47
  }
54
48
  /**
@@ -72,26 +66,27 @@ export function handleImportTabular(result, datatype, theme) {
72
66
  : +row.predictionScore),
73
67
  }));
74
68
  // Validate the imported file.
75
- const validate = new Ajv().compile(TABULAR_SCHEMAS[datatype].schema);
76
- const valid = validate(importData);
69
+ const zodSchema = TABULAR_SCHEMAS.schema;
70
+ const parseResult = zodSchema.safeParse(importData);
71
+ const valid = parseResult.success;
77
72
  if (!valid) {
78
- const failureReason = JSON.stringify(validate.errors, null, 2);
73
+ const failureReason = JSON.stringify(parseResult.error.message, null, 2);
79
74
  throw new Error(`Import validation failed: ${failureReason}`);
80
75
  }
81
76
  else {
77
+ const parsedData = parseResult.data;
82
78
  // Convert the validated array to a tree representation.
83
79
  const treeToImport = {
84
- version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
85
- datatype,
80
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
86
81
  tree: [],
87
82
  };
88
- const uniqueGroupNames = Array.from(new Set(importData.map(d => d.groupName)));
83
+ const uniqueGroupNames = Array.from(new Set(parsedData.map(d => d.groupName)));
89
84
  uniqueGroupNames.forEach((groupName) => {
90
85
  const levelZeroNode = {
91
86
  name: groupName,
92
87
  children: [],
93
88
  };
94
- const groupRows = importData.filter(d => d.groupName === groupName);
89
+ const groupRows = parsedData.filter(d => d.groupName === groupName);
95
90
  const uniqueSetNames = Array.from(new Set(groupRows.map(d => d.setName)));
96
91
  uniqueSetNames.forEach((setName) => {
97
92
  const setRows = groupRows.filter(d => d.setName === setName);
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=io.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"io.test.d.ts","sourceRoot":"","sources":["../src/io.test.js"],"names":[],"mappings":""}
@@ -1,7 +1,6 @@
1
- import { handleImportJSON, handleExportJSON, handleImportTabular, handleExportTabular, } from './io';
1
+ import { handleImportJSON, handleExportJSON, handleImportTabular, handleExportTabular, } from './io.js';
2
2
  const treeV012 = {
3
3
  version: '0.1.2',
4
- datatype: 'cell',
5
4
  tree: [
6
5
  {
7
6
  name: 'Clustering Algorithm',
@@ -17,7 +16,6 @@ const treeV012 = {
17
16
  };
18
17
  const treeV013 = {
19
18
  version: '0.1.3',
20
- datatype: 'cell',
21
19
  tree: [
22
20
  {
23
21
  name: 'Clustering Algorithm',
@@ -38,7 +36,6 @@ const treeV013 = {
38
36
  };
39
37
  const treeV013WithPredictionScores = {
40
38
  version: '0.1.3',
41
- datatype: 'cell',
42
39
  tree: [
43
40
  {
44
41
  name: 'Clustering Algorithm',
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Returns a path that belongs to cellSetSelection and is the longest path
3
+ * found in at least one of the paths in arrOfPaths.
4
+ * @param {array of paths} arrOfPaths contains all paths we are interested in
5
+ * @param {array of paths} cellSetSelection contains the paths of the selected cell sets
6
+ * @returns
7
+ */
8
+ export function findLongestCommonPath(arrOfPaths: any, cellSetSelection: any): any[];
9
+ /**
10
+ * Generates paths by doing DFS of tree and filters out the ones that are irrelevant.
11
+ * Filtering is based on the hierarchy, currently expanded and currently selected cell sets.
12
+ * @param {object} tree The current tree.
13
+ * @param {string[]} hierarchy Array of level-zero set names, like
14
+ * `['Louvain Clustering']`.
15
+ * @param {array} cellSetExpansion An array of cell set paths that are expanded.
16
+ * @param {array} cellSetSelection An array of cell set paths that are selected.
17
+ * @returns An array of paths that should be displayed.
18
+ */
19
+ export function filterPathsByExpansionAndSelection(mergedCellSets: any, hierarchy: string[], cellSetExpansion: array, cellSetSelection: array): any;
20
+ /**
21
+ * Finds the hierarchy, where cell sets were most recently selected/deselected.
22
+ * @param {array} lastSelection All selected cell sets prior to the modification.
23
+ * @param {array} currentSelection All cell sets that are currently selected.
24
+ * @returns The name of the hierarchy, within which the most recent change occured.
25
+ * If nothing changed, returns 0.
26
+ */
27
+ export function findChangedHierarchy(prevSelectedPaths: any, currSelectedPaths: any): any;
28
+ //# sourceMappingURL=set-path-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-path-utils.d.ts","sourceRoot":"","sources":["../src/set-path-utils.js"],"names":[],"mappings":"AAoDA;;;;;;GAMG;AACH,qFAeC;AAED;;;;;;;;;GASG;AACH,mFANW,MAAM,EAAE,yDA2ClB;AAED;;;;;;GAMG;AACH,0FAsBC"}
@@ -0,0 +1,133 @@
1
+ import { isEqual } from 'lodash-es';
2
+ /**
3
+ * Depth-first search of cell set tree, starting from node.
4
+ * @param {*} node The node to start from.
5
+ * @param {*} currentPath The current path.
6
+ * @param {*} paths Accumulated paths so far.
7
+ * @returns all paths from node to leaves.
8
+ */
9
+ function getPaths(node, currentPath = [], paths = []) {
10
+ if (node.children) {
11
+ node.children.forEach((child) => {
12
+ const newPath = [...currentPath, child.name];
13
+ paths.push(newPath);
14
+ getPaths(child, newPath, paths);
15
+ });
16
+ }
17
+ return paths;
18
+ }
19
+ /**
20
+ * Finds either the longest subset or the longest superset of path in
21
+ * arrOfPaths.
22
+ * @param {array} arrOfPaths An array of paths.
23
+ * @param {array} path An array of strings, representing a path.
24
+ * @param {boolean} isSubset A boolean flag that indicates whether we are
25
+ * looking for the longest subset (true) or the longest superset (false).
26
+ * @returns {array} The longest subset or superset of path in arrOfPaths.
27
+ */
28
+ function findLongestPath(arrOfPaths, path, isSubset) {
29
+ if (Array.isArray(arrOfPaths)) {
30
+ let longest = null;
31
+ let longestLength = 0;
32
+ arrOfPaths.forEach((subArray) => {
33
+ const matchCount = subArray.filter((v, i) => v === path[i]).length;
34
+ if (matchCount === (isSubset ? subArray.length : path.length)
35
+ && subArray.length > longestLength) {
36
+ longest = subArray;
37
+ longestLength = subArray.length;
38
+ }
39
+ });
40
+ if (longestLength > 0) {
41
+ return longest;
42
+ }
43
+ if (isSubset) {
44
+ return [];
45
+ }
46
+ }
47
+ return [];
48
+ }
49
+ /**
50
+ * Returns a path that belongs to cellSetSelection and is the longest path
51
+ * found in at least one of the paths in arrOfPaths.
52
+ * @param {array of paths} arrOfPaths contains all paths we are interested in
53
+ * @param {array of paths} cellSetSelection contains the paths of the selected cell sets
54
+ * @returns
55
+ */
56
+ export function findLongestCommonPath(arrOfPaths, cellSetSelection) {
57
+ let maxCount = 0;
58
+ let commonPath = [];
59
+ cellSetSelection.forEach((currentCellSet) => {
60
+ // Find the longest common path
61
+ const longestPath = findLongestPath(arrOfPaths, currentCellSet, false);
62
+ if (longestPath.length > maxCount) {
63
+ maxCount = longestPath.length;
64
+ commonPath = currentCellSet;
65
+ }
66
+ });
67
+ return commonPath;
68
+ }
69
+ /**
70
+ * Generates paths by doing DFS of tree and filters out the ones that are irrelevant.
71
+ * Filtering is based on the hierarchy, currently expanded and currently selected cell sets.
72
+ * @param {object} tree The current tree.
73
+ * @param {string[]} hierarchy Array of level-zero set names, like
74
+ * `['Louvain Clustering']`.
75
+ * @param {array} cellSetExpansion An array of cell set paths that are expanded.
76
+ * @param {array} cellSetSelection An array of cell set paths that are selected.
77
+ * @returns An array of paths that should be displayed.
78
+ */
79
+ export function filterPathsByExpansionAndSelection(mergedCellSets, hierarchy, cellSetExpansion, cellSetSelection) {
80
+ const paths = getPaths({ children: mergedCellSets.tree });
81
+ // returns true if path is contained in allPaths, false otherwise
82
+ const contains = (allPaths, path) => allPaths?.some(p => isEqual(p, path));
83
+ return paths.filter((clusterPath) => {
84
+ // clusterPath is a parent of some selected cell set and is expanded. We should discard it.
85
+ if (contains(cellSetExpansion, clusterPath))
86
+ return false;
87
+ // clusterPath is not selected. Now we need to determine if we should keep it.
88
+ if (!contains(cellSetSelection, clusterPath)) {
89
+ /* This line checks if the longest super set of clusterPath in cellSetSelection is longer
90
+ than clusterPath itself. If true, it means clusterPath is a parent of some selected cell
91
+ set but not expanded and we should discard it. */
92
+ if (findLongestPath(cellSetSelection, clusterPath, false).length > clusterPath.length) {
93
+ return false;
94
+ }
95
+ if (cellSetExpansion) {
96
+ // the clusterPath is too deep in the tree. We should discard it.
97
+ if (cellSetExpansion.length === 0 && clusterPath.length > 2)
98
+ return false;
99
+ const longestSubset = findLongestPath(cellSetExpansion, clusterPath, true);
100
+ // another case of the clusterPath being deep in the tree. We should discard it.
101
+ if (cellSetExpansion.length > 0 && longestSubset.length + 1 < clusterPath.length) {
102
+ return false;
103
+ }
104
+ }
105
+ }
106
+ return clusterPath[0] === hierarchy[0];
107
+ });
108
+ }
109
+ /**
110
+ * Finds the hierarchy, where cell sets were most recently selected/deselected.
111
+ * @param {array} lastSelection All selected cell sets prior to the modification.
112
+ * @param {array} currentSelection All cell sets that are currently selected.
113
+ * @returns The name of the hierarchy, within which the most recent change occured.
114
+ * If nothing changed, returns 0.
115
+ */
116
+ export function findChangedHierarchy(prevSelectedPaths, currSelectedPaths) {
117
+ const unselectedPaths = prevSelectedPaths.filter(prevPath => !currSelectedPaths.find(currPath => isEqual(currPath, prevPath)));
118
+ const newlySelectedPaths = currSelectedPaths.filter(currPath => !prevSelectedPaths.find(prevPath => isEqual(currPath, prevPath)));
119
+ // nothing was selected or unselected, therefore hierarchy stays the same
120
+ if (unselectedPaths.length === 0 && newlySelectedPaths.length === 0) {
121
+ return null;
122
+ }
123
+ /*
124
+ * Picks the first path from newlySelectedPathsStr, if there is one,
125
+ * otherwise from unselectedPathsStr.
126
+ * This is going to be used as the new hierarchy.
127
+ */
128
+ const changedPath = newlySelectedPaths.length > 0
129
+ ? newlySelectedPaths[0] : unselectedPaths[0];
130
+ // we assume that the last element of a path is the leaf node.
131
+ // As leaf nodes do not hold hierarchy information, we can remove it.
132
+ return changedPath.slice(0, -1);
133
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=set-path-utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-path-utils.test.d.ts","sourceRoot":"","sources":["../src/set-path-utils.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,210 @@
1
+ import { filterPathsByExpansionAndSelection, findChangedHierarchy } from './set-path-utils.js';
2
+ describe('Tests for findChangedHierarchy', () => {
3
+ it('Computes correct new hierarchy after new selection', () => {
4
+ const prevSelectedPaths = [
5
+ ['Louvain Clustering', 'Cluster 1'],
6
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
7
+ ];
8
+ const currSelectedPaths = [
9
+ ['Louvain Clustering', 'Cluster 1'],
10
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
11
+ ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
12
+ ];
13
+ const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
14
+ expect(changedHierarchy).toEqual(['Cell Type Annotations', 'Vasculature']);
15
+ });
16
+ it('Computes correct hierarchy after unselecting', () => {
17
+ const prevSelectedPaths = [
18
+ ['Louvain Clustering', 'Cluster 1'],
19
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
20
+ ];
21
+ const currSelectedPaths = [
22
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
23
+ ];
24
+ const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
25
+ expect(changedHierarchy).toEqual(['Louvain Clustering']);
26
+ });
27
+ it('Computes correct hierarchy after select only', () => {
28
+ const prevSelectedPaths = [
29
+ ['Louvain Clustering', 'Cluster 1'],
30
+ ['Louvain Clustering', 'Cluster 2'],
31
+ ['Louvain Clustering', 'Cluster 3'],
32
+ ['Louvain Clustering', 'Cluster 4'],
33
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
34
+ ];
35
+ const currSelectedPaths = [
36
+ ['Cell Type Annotations', 'Vasculature'],
37
+ ];
38
+ const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
39
+ expect(changedHierarchy).toEqual(['Cell Type Annotations']);
40
+ });
41
+ it('Returns null when hierarchy was not changed', () => {
42
+ const prevSelectedPaths = [
43
+ ['Louvain Clustering', 'Cluster 1'],
44
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
45
+ ];
46
+ const currSelectedPaths = [
47
+ ['Louvain Clustering', 'Cluster 1'],
48
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
49
+ ];
50
+ const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
51
+ expect(changedHierarchy).toEqual(null);
52
+ });
53
+ it('Works when nothing was previously selected', () => {
54
+ const prevSelectedPaths = [];
55
+ const currSelectedPaths = [
56
+ ['Louvain Clustering', 'Cluster 1'],
57
+ ];
58
+ const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
59
+ expect(changedHierarchy).toEqual(['Louvain Clustering']);
60
+ });
61
+ it('Works when nothing is selected', () => {
62
+ const prevSelectedPaths = [
63
+ ['Louvain Clustering', 'Cluster 1'],
64
+ ];
65
+ const currSelectedPaths = [];
66
+ const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
67
+ expect(changedHierarchy).toEqual(['Louvain Clustering']);
68
+ });
69
+ });
70
+ describe('Tests for filterPathsByExpansionAndSelection', () => {
71
+ const tree = {
72
+ version: '0.1.3',
73
+ datatype: 'cell',
74
+ tree: [
75
+ {
76
+ name: 'Louvain Clustering',
77
+ children: [
78
+ {
79
+ name: 'Cluster 1',
80
+ set: [['cell_1', null], ['cell_2', null], ['cell_3', null]],
81
+ },
82
+ {
83
+ name: 'Cluster 2',
84
+ set: [['cell_4', null], ['cell_5', null]],
85
+ },
86
+ {
87
+ name: 'Cluster 3',
88
+ set: [['cell_6', null], ['cell_7', null]],
89
+ },
90
+ {
91
+ name: 'Cluster 4',
92
+ set: [['cell_8', null], ['cell_9', null], ['cell_10', null]],
93
+ },
94
+ ],
95
+ },
96
+ {
97
+ name: 'Cell Type Annotations',
98
+ children: [
99
+ {
100
+ name: 'Vasculature',
101
+ children: [
102
+ {
103
+ name: 'Pericytes',
104
+ set: [['cell_1', null], ['cell_2', null], ['cell_3', null]],
105
+ },
106
+ {
107
+ name: 'Endothelial',
108
+ set: [['cell_3', null], ['cell_4', null], ['cell_5', null]],
109
+ },
110
+ {
111
+ name: 'Epithelial',
112
+ children: [
113
+ {
114
+ name: 'Squamous',
115
+ set: [['cell_5', null], ['cell_6', null], ['cell_7', null]],
116
+ },
117
+ ],
118
+ },
119
+ ],
120
+ },
121
+ {
122
+ name: 'Immune',
123
+ set: [['cell_8', null], ['cell_9', null], ['cell_10', null]],
124
+ },
125
+ ],
126
+ },
127
+ ],
128
+ };
129
+ it('Generates correct cell set paths when expanded', () => {
130
+ const hierarchy = ['Louvain Clustering'];
131
+ const cellSetExpansion = [
132
+ ['Louvain Clustering'],
133
+ ];
134
+ const cellSetSelection = [
135
+ ['Louvain Clustering', 'Cluster 1'],
136
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
137
+ ];
138
+ const cellSetPaths = filterPathsByExpansionAndSelection(tree, hierarchy, cellSetExpansion, cellSetSelection);
139
+ expect(cellSetPaths).toEqual([
140
+ ['Louvain Clustering', 'Cluster 1'],
141
+ ['Louvain Clustering', 'Cluster 2'],
142
+ ['Louvain Clustering', 'Cluster 3'],
143
+ ['Louvain Clustering', 'Cluster 4'],
144
+ ]);
145
+ });
146
+ it('Generates correct cell set paths when not expanded and no nested hierarchy', () => {
147
+ const hierarchy = ['Louvain Clustering'];
148
+ const cellSetExpansion = [];
149
+ const cellSetSelection = [
150
+ ['Louvain Clustering', 'Cluster 1'],
151
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
152
+ ];
153
+ const cellSetPaths = filterPathsByExpansionAndSelection(tree, hierarchy, cellSetExpansion, cellSetSelection);
154
+ expect(cellSetPaths).toEqual([
155
+ ['Louvain Clustering', 'Cluster 1'],
156
+ ['Louvain Clustering', 'Cluster 2'],
157
+ ['Louvain Clustering', 'Cluster 3'],
158
+ ['Louvain Clustering', 'Cluster 4'],
159
+ ]);
160
+ });
161
+ it('Generates correct cell set paths when expanded and nested hierarchy', () => {
162
+ const hierarchy = ['Cell Type Annotations'];
163
+ const cellSetExpansion = [
164
+ ['Cell Type Annotations', 'Vasculature'],
165
+ ];
166
+ const cellSetSelection = [
167
+ ['Louvain Clustering', 'Cluster 1'],
168
+ ['Louvain Clustering', 'Cluster 2'],
169
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
170
+ ];
171
+ const cellSetPaths = filterPathsByExpansionAndSelection(tree, hierarchy, cellSetExpansion, cellSetSelection);
172
+ expect(cellSetPaths).toEqual([
173
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
174
+ ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
175
+ ['Cell Type Annotations', 'Vasculature', 'Epithelial'],
176
+ ]);
177
+ });
178
+ it('Generates correct cell set paths when not expanded and nested hierarchy', () => {
179
+ const hierarchy = ['Cell Type Annotations'];
180
+ const cellSetExpansion = [
181
+ ['Cell Type Annotations'],
182
+ ];
183
+ const cellSetSelection = [
184
+ ['Louvain Clustering', 'Cluster 1'],
185
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
186
+ ];
187
+ const cellSetPaths = filterPathsByExpansionAndSelection(tree, hierarchy, cellSetExpansion, cellSetSelection);
188
+ expect(cellSetPaths).toEqual([
189
+ ['Cell Type Annotations', 'Vasculature', 'Pericytes'],
190
+ ['Cell Type Annotations', 'Immune'],
191
+ ]);
192
+ });
193
+ it('Uses paths in cellSetSelection when cellSetExpansion is null', () => {
194
+ const hierarchy = ['Louvain Clustering'];
195
+ const cellSetExpansion = null;
196
+ const cellSetSelection = [
197
+ ['Louvain Clustering', 'Cluster 1'],
198
+ ['Louvain Clustering', 'Cluster 2'],
199
+ ['Louvain Clustering', 'Cluster 3'],
200
+ ['Louvain Clustering', 'Cluster 4'],
201
+ ];
202
+ const cellSetPaths = filterPathsByExpansionAndSelection(tree, hierarchy, cellSetExpansion, cellSetSelection);
203
+ expect(cellSetPaths).toEqual([
204
+ ['Louvain Clustering', 'Cluster 1'],
205
+ ['Louvain Clustering', 'Cluster 2'],
206
+ ['Louvain Clustering', 'Cluster 3'],
207
+ ['Louvain Clustering', 'Cluster 4'],
208
+ ]);
209
+ });
210
+ });
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Execute a callback function based on a keypress event.
3
+ * @param {object} event The event from onKeyPress
4
+ * @param {string} key The key identifier to match.
5
+ * @param {Function} callback The function to execute.
6
+ */
7
+ export function callbackOnKeyPress(event: object, key: string, callback: Function): void;
8
+ /**
9
+ * Convert an array of [r, g, b] numbers to a hex color.
10
+ * @param {number[]} rgbArray The color [r, g, b] array.
11
+ * @returns {string} The hex color as a string.
12
+ */
13
+ export function colorArrayToString(rgbArray: number[]): string;
14
+ /**
15
+ * Convert a string color representation to an array of [r,g,b].
16
+ * @param {string} colorString The color as a string.
17
+ * @returns {number[]} The color as an array.
18
+ */
19
+ export function colorStringToArray(colorString: string): number[];
20
+ /**
21
+ * Get a string of help text for coloring a particular hierarchy level.
22
+ * @param {integer} i The level. 1 for cluster, 2 for subcluster, etc.
23
+ * @returns {string} The tooltip text for coloring the level.
24
+ */
25
+ export function getLevelTooltipText(i: integer): string;
26
+ export function isEqualOrPrefix(targetPath: any, testPath: any): any;
27
+ export function tryRenamePath(targetPath: any, testPath: any, nextTargetPath: any): any;
28
+ export function pathToKey(path: any): any;
29
+ export function getNextNumberedNodeName(nodes: any, prefix: any, suffix: any): string;
30
+ /**
31
+ * Create a new selected cell set based on a cell selection.
32
+ * @param {string[]} cellSelection An array of cell IDs.
33
+ * @param {object[]} additionalCellSets The previous array of user-defined cell sets.
34
+ * @param {function} setCellSetSelection The setter function for cell set selections.
35
+ * @param {function} setAdditionalCellSets The setter function for user-defined cell sets.
36
+ */
37
+ export function setObsSelection(cellSelection: string[], additionalCellSets: object[], cellSetColor: any, setCellSetSelection: Function, setAdditionalCellSets: Function, setCellSetColor: any, setCellColorEncoding: any, prefix?: string, suffix?: string): void;
38
+ export function mergeObsSets(cellSets: any, additionalCellSets: any): {
39
+ version: string;
40
+ datatype: string;
41
+ tree: any[];
42
+ };
43
+ export function getObsInfoFromDataWithinRange(range: any, data: any): any;
44
+ export const PATH_SEP: "___";
45
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,0CAJW,MAAM,OACN,MAAM,4BAQhB;AAED;;;;GAIG;AACH,6CAHW,MAAM,EAAE,GACN,MAAM,CAIlB;AAED;;;;GAIG;AACH,gDAHW,MAAM,GACJ,MAAM,EAAE,CAKpB;AAED;;;;GAIG;AACH,iDAFa,MAAM,CASlB;AAED,qEAKC;AAED,wFAKC;AAID,0CAEC;AAID,sFAUC;AAED;;;;;;GAMG;AACH,+CALW,MAAM,EAAE,sBACR,MAAM,EAAE,8KA+ClB;AAED;;;;EASC;AAED,0EAQC;AA3FD,6BAA8B"}