@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
package/src/io.test.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
handleImportJSON, handleExportJSON, handleImportTabular, handleExportTabular,
|
|
3
|
-
} from './io';
|
|
3
|
+
} from './io.js';
|
|
4
4
|
|
|
5
5
|
const treeV012 = {
|
|
6
6
|
version: '0.1.2',
|
|
7
|
-
datatype: 'cell',
|
|
8
7
|
tree: [
|
|
9
8
|
{
|
|
10
9
|
name: 'Clustering Algorithm',
|
|
@@ -20,7 +19,6 @@ const treeV012 = {
|
|
|
20
19
|
};
|
|
21
20
|
const treeV013 = {
|
|
22
21
|
version: '0.1.3',
|
|
23
|
-
datatype: 'cell',
|
|
24
22
|
tree: [
|
|
25
23
|
{
|
|
26
24
|
name: 'Clustering Algorithm',
|
|
@@ -42,7 +40,6 @@ const treeV013 = {
|
|
|
42
40
|
|
|
43
41
|
const treeV013WithPredictionScores = {
|
|
44
42
|
version: '0.1.3',
|
|
45
|
-
datatype: 'cell',
|
|
46
43
|
tree: [
|
|
47
44
|
{
|
|
48
45
|
name: 'Clustering Algorithm',
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { isEqual } from 'lodash-es';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Depth-first search of cell set tree, starting from node.
|
|
5
|
+
* @param {*} node The node to start from.
|
|
6
|
+
* @param {*} currentPath The current path.
|
|
7
|
+
* @param {*} paths Accumulated paths so far.
|
|
8
|
+
* @returns all paths from node to leaves.
|
|
9
|
+
*/
|
|
10
|
+
function getPaths(node, currentPath = [], paths = []) {
|
|
11
|
+
if (node.children) {
|
|
12
|
+
node.children.forEach((child) => {
|
|
13
|
+
const newPath = [...currentPath, child.name];
|
|
14
|
+
paths.push(newPath);
|
|
15
|
+
getPaths(child, newPath, paths);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return paths;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Finds either the longest subset or the longest superset of path in
|
|
23
|
+
* arrOfPaths.
|
|
24
|
+
* @param {array} arrOfPaths An array of paths.
|
|
25
|
+
* @param {array} path An array of strings, representing a path.
|
|
26
|
+
* @param {boolean} isSubset A boolean flag that indicates whether we are
|
|
27
|
+
* looking for the longest subset (true) or the longest superset (false).
|
|
28
|
+
* @returns {array} The longest subset or superset of path in arrOfPaths.
|
|
29
|
+
*/
|
|
30
|
+
function findLongestPath(arrOfPaths, path, isSubset) {
|
|
31
|
+
if (Array.isArray(arrOfPaths)) {
|
|
32
|
+
let longest = null;
|
|
33
|
+
let longestLength = 0;
|
|
34
|
+
arrOfPaths.forEach((subArray) => {
|
|
35
|
+
const matchCount = subArray.filter((v, i) => v === path[i]).length;
|
|
36
|
+
if (
|
|
37
|
+
matchCount === (isSubset ? subArray.length : path.length)
|
|
38
|
+
&& subArray.length > longestLength
|
|
39
|
+
) {
|
|
40
|
+
longest = subArray;
|
|
41
|
+
longestLength = subArray.length;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
if (longestLength > 0) {
|
|
45
|
+
return longest;
|
|
46
|
+
} if (isSubset) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns a path that belongs to cellSetSelection and is the longest path
|
|
55
|
+
* found in at least one of the paths in arrOfPaths.
|
|
56
|
+
* @param {array of paths} arrOfPaths contains all paths we are interested in
|
|
57
|
+
* @param {array of paths} cellSetSelection contains the paths of the selected cell sets
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
export function findLongestCommonPath(arrOfPaths, cellSetSelection) {
|
|
61
|
+
let maxCount = 0;
|
|
62
|
+
let commonPath = [];
|
|
63
|
+
|
|
64
|
+
cellSetSelection.forEach((currentCellSet) => {
|
|
65
|
+
// Find the longest common path
|
|
66
|
+
const longestPath = findLongestPath(arrOfPaths, currentCellSet, false);
|
|
67
|
+
|
|
68
|
+
if (longestPath.length > maxCount) {
|
|
69
|
+
maxCount = longestPath.length;
|
|
70
|
+
commonPath = currentCellSet;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return commonPath;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Generates paths by doing DFS of tree and filters out the ones that are irrelevant.
|
|
79
|
+
* Filtering is based on the hierarchy, currently expanded and currently selected cell sets.
|
|
80
|
+
* @param {object} tree The current tree.
|
|
81
|
+
* @param {string[]} hierarchy Array of level-zero set names, like
|
|
82
|
+
* `['Louvain Clustering']`.
|
|
83
|
+
* @param {array} cellSetExpansion An array of cell set paths that are expanded.
|
|
84
|
+
* @param {array} cellSetSelection An array of cell set paths that are selected.
|
|
85
|
+
* @returns An array of paths that should be displayed.
|
|
86
|
+
*/
|
|
87
|
+
export function filterPathsByExpansionAndSelection(
|
|
88
|
+
mergedCellSets,
|
|
89
|
+
hierarchy,
|
|
90
|
+
cellSetExpansion,
|
|
91
|
+
cellSetSelection,
|
|
92
|
+
) {
|
|
93
|
+
const paths = getPaths({ children: mergedCellSets.tree });
|
|
94
|
+
|
|
95
|
+
// returns true if path is contained in allPaths, false otherwise
|
|
96
|
+
const contains = (allPaths, path) => allPaths?.some(p => isEqual(p, path));
|
|
97
|
+
|
|
98
|
+
return paths.filter((clusterPath) => {
|
|
99
|
+
// clusterPath is a parent of some selected cell set and is expanded. We should discard it.
|
|
100
|
+
if (contains(cellSetExpansion, clusterPath)) return false;
|
|
101
|
+
|
|
102
|
+
// clusterPath is not selected. Now we need to determine if we should keep it.
|
|
103
|
+
if (!contains(cellSetSelection, clusterPath)) {
|
|
104
|
+
/* This line checks if the longest super set of clusterPath in cellSetSelection is longer
|
|
105
|
+
than clusterPath itself. If true, it means clusterPath is a parent of some selected cell
|
|
106
|
+
set but not expanded and we should discard it. */
|
|
107
|
+
if (findLongestPath(cellSetSelection, clusterPath, false).length > clusterPath.length) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (cellSetExpansion) {
|
|
112
|
+
// the clusterPath is too deep in the tree. We should discard it.
|
|
113
|
+
if (cellSetExpansion.length === 0 && clusterPath.length > 2) return false;
|
|
114
|
+
|
|
115
|
+
const longestSubset = findLongestPath(cellSetExpansion, clusterPath, true);
|
|
116
|
+
// another case of the clusterPath being deep in the tree. We should discard it.
|
|
117
|
+
if (cellSetExpansion.length > 0 && longestSubset.length + 1 < clusterPath.length) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return clusterPath[0] === hierarchy[0];
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Finds the hierarchy, where cell sets were most recently selected/deselected.
|
|
128
|
+
* @param {array} lastSelection All selected cell sets prior to the modification.
|
|
129
|
+
* @param {array} currentSelection All cell sets that are currently selected.
|
|
130
|
+
* @returns The name of the hierarchy, within which the most recent change occured.
|
|
131
|
+
* If nothing changed, returns 0.
|
|
132
|
+
*/
|
|
133
|
+
export function findChangedHierarchy(prevSelectedPaths, currSelectedPaths) {
|
|
134
|
+
const unselectedPaths = prevSelectedPaths.filter(
|
|
135
|
+
prevPath => !currSelectedPaths.find(currPath => isEqual(currPath, prevPath)),
|
|
136
|
+
);
|
|
137
|
+
const newlySelectedPaths = currSelectedPaths.filter(
|
|
138
|
+
currPath => !prevSelectedPaths.find(prevPath => isEqual(currPath, prevPath)),
|
|
139
|
+
);
|
|
140
|
+
// nothing was selected or unselected, therefore hierarchy stays the same
|
|
141
|
+
if (unselectedPaths.length === 0 && newlySelectedPaths.length === 0) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
/*
|
|
145
|
+
* Picks the first path from newlySelectedPathsStr, if there is one,
|
|
146
|
+
* otherwise from unselectedPathsStr.
|
|
147
|
+
* This is going to be used as the new hierarchy.
|
|
148
|
+
*/
|
|
149
|
+
const changedPath = newlySelectedPaths.length > 0
|
|
150
|
+
? newlySelectedPaths[0] : unselectedPaths[0];
|
|
151
|
+
|
|
152
|
+
// we assume that the last element of a path is the leaf node.
|
|
153
|
+
// As leaf nodes do not hold hierarchy information, we can remove it.
|
|
154
|
+
return changedPath.slice(0, -1);
|
|
155
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { filterPathsByExpansionAndSelection, findChangedHierarchy } from './set-path-utils.js';
|
|
2
|
+
|
|
3
|
+
describe('Tests for findChangedHierarchy', () => {
|
|
4
|
+
it('Computes correct new hierarchy after new selection', () => {
|
|
5
|
+
const prevSelectedPaths = [
|
|
6
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
7
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
8
|
+
];
|
|
9
|
+
const currSelectedPaths = [
|
|
10
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
11
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
12
|
+
['Cell Type Annotations', 'Vasculature', 'Endothelial'],
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
|
|
16
|
+
expect(changedHierarchy).toEqual(['Cell Type Annotations', 'Vasculature']);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('Computes correct hierarchy after unselecting', () => {
|
|
20
|
+
const prevSelectedPaths = [
|
|
21
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
22
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
23
|
+
];
|
|
24
|
+
const currSelectedPaths = [
|
|
25
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
|
|
29
|
+
expect(changedHierarchy).toEqual(['Louvain Clustering']);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('Computes correct hierarchy after select only', () => {
|
|
33
|
+
const prevSelectedPaths = [
|
|
34
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
35
|
+
['Louvain Clustering', 'Cluster 2'],
|
|
36
|
+
['Louvain Clustering', 'Cluster 3'],
|
|
37
|
+
['Louvain Clustering', 'Cluster 4'],
|
|
38
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
39
|
+
];
|
|
40
|
+
const currSelectedPaths = [
|
|
41
|
+
['Cell Type Annotations', 'Vasculature'],
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
|
|
45
|
+
expect(changedHierarchy).toEqual(['Cell Type Annotations']);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('Returns null when hierarchy was not changed', () => {
|
|
49
|
+
const prevSelectedPaths = [
|
|
50
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
51
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
52
|
+
];
|
|
53
|
+
const currSelectedPaths = [
|
|
54
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
55
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
|
|
59
|
+
expect(changedHierarchy).toEqual(null);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('Works when nothing was previously selected', () => {
|
|
63
|
+
const prevSelectedPaths = [];
|
|
64
|
+
const currSelectedPaths = [
|
|
65
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
|
|
69
|
+
expect(changedHierarchy).toEqual(['Louvain Clustering']);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('Works when nothing is selected', () => {
|
|
73
|
+
const prevSelectedPaths = [
|
|
74
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
75
|
+
];
|
|
76
|
+
const currSelectedPaths = [];
|
|
77
|
+
|
|
78
|
+
const changedHierarchy = findChangedHierarchy(prevSelectedPaths, currSelectedPaths);
|
|
79
|
+
expect(changedHierarchy).toEqual(['Louvain Clustering']);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('Tests for filterPathsByExpansionAndSelection', () => {
|
|
84
|
+
const tree = {
|
|
85
|
+
version: '0.1.3',
|
|
86
|
+
datatype: 'cell',
|
|
87
|
+
tree: [
|
|
88
|
+
{
|
|
89
|
+
name: 'Louvain Clustering',
|
|
90
|
+
children: [
|
|
91
|
+
{
|
|
92
|
+
name: 'Cluster 1',
|
|
93
|
+
set: [['cell_1', null], ['cell_2', null], ['cell_3', null]],
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: 'Cluster 2',
|
|
97
|
+
set: [['cell_4', null], ['cell_5', null]],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'Cluster 3',
|
|
101
|
+
set: [['cell_6', null], ['cell_7', null]],
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'Cluster 4',
|
|
105
|
+
set: [['cell_8', null], ['cell_9', null], ['cell_10', null]],
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'Cell Type Annotations',
|
|
111
|
+
children: [
|
|
112
|
+
{
|
|
113
|
+
name: 'Vasculature',
|
|
114
|
+
children: [
|
|
115
|
+
{
|
|
116
|
+
name: 'Pericytes',
|
|
117
|
+
set: [['cell_1', null], ['cell_2', null], ['cell_3', null]],
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: 'Endothelial',
|
|
121
|
+
set: [['cell_3', null], ['cell_4', null], ['cell_5', null]],
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'Epithelial',
|
|
125
|
+
children: [
|
|
126
|
+
{
|
|
127
|
+
name: 'Squamous',
|
|
128
|
+
set: [['cell_5', null], ['cell_6', null], ['cell_7', null]],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: 'Immune',
|
|
136
|
+
set: [['cell_8', null], ['cell_9', null], ['cell_10', null]],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
it('Generates correct cell set paths when expanded', () => {
|
|
144
|
+
const hierarchy = ['Louvain Clustering'];
|
|
145
|
+
const cellSetExpansion = [
|
|
146
|
+
['Louvain Clustering'],
|
|
147
|
+
];
|
|
148
|
+
const cellSetSelection = [
|
|
149
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
150
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
const cellSetPaths = filterPathsByExpansionAndSelection(
|
|
154
|
+
tree,
|
|
155
|
+
hierarchy,
|
|
156
|
+
cellSetExpansion,
|
|
157
|
+
cellSetSelection,
|
|
158
|
+
);
|
|
159
|
+
expect(cellSetPaths).toEqual([
|
|
160
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
161
|
+
['Louvain Clustering', 'Cluster 2'],
|
|
162
|
+
['Louvain Clustering', 'Cluster 3'],
|
|
163
|
+
['Louvain Clustering', 'Cluster 4'],
|
|
164
|
+
]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('Generates correct cell set paths when not expanded and no nested hierarchy', () => {
|
|
168
|
+
const hierarchy = ['Louvain Clustering'];
|
|
169
|
+
const cellSetExpansion = [
|
|
170
|
+
];
|
|
171
|
+
const cellSetSelection = [
|
|
172
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
173
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
const cellSetPaths = filterPathsByExpansionAndSelection(
|
|
177
|
+
tree,
|
|
178
|
+
hierarchy,
|
|
179
|
+
cellSetExpansion,
|
|
180
|
+
cellSetSelection,
|
|
181
|
+
);
|
|
182
|
+
expect(cellSetPaths).toEqual([
|
|
183
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
184
|
+
['Louvain Clustering', 'Cluster 2'],
|
|
185
|
+
['Louvain Clustering', 'Cluster 3'],
|
|
186
|
+
['Louvain Clustering', 'Cluster 4'],
|
|
187
|
+
]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('Generates correct cell set paths when expanded and nested hierarchy', () => {
|
|
191
|
+
const hierarchy = ['Cell Type Annotations'];
|
|
192
|
+
const cellSetExpansion = [
|
|
193
|
+
['Cell Type Annotations', 'Vasculature'],
|
|
194
|
+
];
|
|
195
|
+
const cellSetSelection = [
|
|
196
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
197
|
+
['Louvain Clustering', 'Cluster 2'],
|
|
198
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
const cellSetPaths = filterPathsByExpansionAndSelection(tree,
|
|
202
|
+
hierarchy,
|
|
203
|
+
cellSetExpansion,
|
|
204
|
+
cellSetSelection);
|
|
205
|
+
expect(cellSetPaths).toEqual([
|
|
206
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
207
|
+
['Cell Type Annotations', 'Vasculature', 'Endothelial'],
|
|
208
|
+
['Cell Type Annotations', 'Vasculature', 'Epithelial'],
|
|
209
|
+
]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('Generates correct cell set paths when not expanded and nested hierarchy', () => {
|
|
213
|
+
const hierarchy = ['Cell Type Annotations'];
|
|
214
|
+
const cellSetExpansion = [
|
|
215
|
+
['Cell Type Annotations'],
|
|
216
|
+
];
|
|
217
|
+
const cellSetSelection = [
|
|
218
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
219
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
220
|
+
];
|
|
221
|
+
|
|
222
|
+
const cellSetPaths = filterPathsByExpansionAndSelection(
|
|
223
|
+
tree,
|
|
224
|
+
hierarchy,
|
|
225
|
+
cellSetExpansion,
|
|
226
|
+
cellSetSelection,
|
|
227
|
+
);
|
|
228
|
+
expect(cellSetPaths).toEqual([
|
|
229
|
+
['Cell Type Annotations', 'Vasculature', 'Pericytes'],
|
|
230
|
+
['Cell Type Annotations', 'Immune'],
|
|
231
|
+
]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('Uses paths in cellSetSelection when cellSetExpansion is null', () => {
|
|
235
|
+
const hierarchy = ['Louvain Clustering'];
|
|
236
|
+
const cellSetExpansion = null;
|
|
237
|
+
const cellSetSelection = [
|
|
238
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
239
|
+
['Louvain Clustering', 'Cluster 2'],
|
|
240
|
+
['Louvain Clustering', 'Cluster 3'],
|
|
241
|
+
['Louvain Clustering', 'Cluster 4'],
|
|
242
|
+
];
|
|
243
|
+
|
|
244
|
+
const cellSetPaths = filterPathsByExpansionAndSelection(
|
|
245
|
+
tree,
|
|
246
|
+
hierarchy,
|
|
247
|
+
cellSetExpansion,
|
|
248
|
+
cellSetSelection,
|
|
249
|
+
);
|
|
250
|
+
expect(cellSetPaths).toEqual([
|
|
251
|
+
['Louvain Clustering', 'Cluster 1'],
|
|
252
|
+
['Louvain Clustering', 'Cluster 2'],
|
|
253
|
+
['Louvain Clustering', 'Cluster 3'],
|
|
254
|
+
['Louvain Clustering', 'Cluster 4'],
|
|
255
|
+
]);
|
|
256
|
+
});
|
|
257
|
+
});
|
package/src/utils.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import tinycolor from 'tinycolor2';
|
|
2
|
-
import isEqual from 'lodash
|
|
2
|
+
import { isEqual } from 'lodash-es';
|
|
3
3
|
import { PALETTE } from '@vitessce/utils';
|
|
4
4
|
import {
|
|
5
5
|
SETS_DATATYPE_OBS,
|
|
6
6
|
HIERARCHICAL_SCHEMAS,
|
|
7
|
-
} from './constants';
|
|
7
|
+
} from './constants.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Execute a callback function based on a keypress event.
|
|
@@ -74,16 +74,16 @@ export function pathToKey(path) {
|
|
|
74
74
|
|
|
75
75
|
// Moved from src/components/utils.js
|
|
76
76
|
|
|
77
|
-
export function getNextNumberedNodeName(nodes, prefix) {
|
|
77
|
+
export function getNextNumberedNodeName(nodes, prefix, suffix) {
|
|
78
78
|
let i = 1;
|
|
79
79
|
if (nodes) {
|
|
80
80
|
// eslint-disable-next-line no-loop-func
|
|
81
|
-
while (nodes.find(n => n.name
|
|
81
|
+
while (nodes.find(n => n.name.includes(`${prefix}${i}`))) {
|
|
82
82
|
// eslint-disable-next-line no-plusplus
|
|
83
83
|
i++;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
return `${prefix}${i}`;
|
|
86
|
+
return `${prefix}${i}${suffix}`;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
@@ -93,19 +93,20 @@ export function getNextNumberedNodeName(nodes, prefix) {
|
|
|
93
93
|
* @param {function} setCellSetSelection The setter function for cell set selections.
|
|
94
94
|
* @param {function} setAdditionalCellSets The setter function for user-defined cell sets.
|
|
95
95
|
*/
|
|
96
|
-
export function setObsSelection(cellSelection, additionalCellSets, cellSetColor, setCellSetSelection, setAdditionalCellSets, setCellSetColor, setCellColorEncoding, prefix = 'Selection ') {
|
|
96
|
+
export function setObsSelection(cellSelection, additionalCellSets, cellSetColor, setCellSetSelection, setAdditionalCellSets, setCellSetColor, setCellColorEncoding, prefix = 'Selection ', suffix = '') {
|
|
97
97
|
const CELL_SELECTIONS_LEVEL_ZERO_NAME = 'My Selections';
|
|
98
98
|
|
|
99
99
|
const selectionsLevelZeroNode = additionalCellSets?.tree.find(
|
|
100
100
|
n => n.name === CELL_SELECTIONS_LEVEL_ZERO_NAME,
|
|
101
101
|
);
|
|
102
102
|
const nextAdditionalCellSets = {
|
|
103
|
-
version: HIERARCHICAL_SCHEMAS
|
|
103
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
104
104
|
datatype: SETS_DATATYPE_OBS,
|
|
105
105
|
tree: [...(additionalCellSets ? additionalCellSets.tree : [])],
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
const nextName = getNextNumberedNodeName(selectionsLevelZeroNode?.children, prefix);
|
|
108
|
+
const nextName = getNextNumberedNodeName(selectionsLevelZeroNode?.children, prefix, suffix);
|
|
109
|
+
|
|
109
110
|
let colorIndex = 0;
|
|
110
111
|
if (selectionsLevelZeroNode) {
|
|
111
112
|
colorIndex = selectionsLevelZeroNode.children.length;
|
|
@@ -139,7 +140,7 @@ export function setObsSelection(cellSelection, additionalCellSets, cellSetColor,
|
|
|
139
140
|
|
|
140
141
|
export function mergeObsSets(cellSets, additionalCellSets) {
|
|
141
142
|
return {
|
|
142
|
-
version: HIERARCHICAL_SCHEMAS
|
|
143
|
+
version: HIERARCHICAL_SCHEMAS.latestVersion,
|
|
143
144
|
datatype: SETS_DATATYPE_OBS,
|
|
144
145
|
tree: [
|
|
145
146
|
...(cellSets ? cellSets.tree : []),
|
|
@@ -147,3 +148,13 @@ export function mergeObsSets(cellSets, additionalCellSets) {
|
|
|
147
148
|
],
|
|
148
149
|
};
|
|
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
|
+
});
|