@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.
Files changed (53) hide show
  1. package/dist/{index.mjs → index.js} +9301 -7998
  2. package/dist-tsc/CellSetsZarrLoader.d.ts +2 -0
  3. package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -0
  4. package/dist-tsc/CellSetsZarrLoader.js +91 -0
  5. package/dist-tsc/CellSetsZarrLoader.test.d.ts +2 -0
  6. package/dist-tsc/CellSetsZarrLoader.test.d.ts.map +1 -0
  7. package/dist-tsc/CellSetsZarrLoader.test.js +154 -0
  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-tsc/cell-set-utils.js +630 -0
  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-tsc/cell-set-utils.test.fixtures.js +68 -0
  16. package/dist-tsc/cell-set-utils.test.js +185 -0
  17. package/dist-tsc/constants.d.ts +17 -0
  18. package/dist-tsc/constants.d.ts.map +1 -0
  19. package/dist-tsc/constants.js +17 -0
  20. package/dist-tsc/index.d.ts +7 -0
  21. package/dist-tsc/index.d.ts.map +1 -0
  22. package/dist-tsc/index.js +6 -5
  23. package/dist-tsc/io.d.ts +47 -0
  24. package/dist-tsc/io.d.ts.map +1 -0
  25. package/dist-tsc/io.js +159 -0
  26. package/dist-tsc/io.test.d.ts +2 -0
  27. package/dist-tsc/io.test.d.ts.map +1 -0
  28. package/dist-tsc/io.test.js +124 -0
  29. package/dist-tsc/set-path-utils.d.ts +28 -0
  30. package/dist-tsc/set-path-utils.d.ts.map +1 -0
  31. package/dist-tsc/set-path-utils.js +133 -0
  32. package/dist-tsc/set-path-utils.test.d.ts +2 -0
  33. package/dist-tsc/set-path-utils.test.d.ts.map +1 -0
  34. package/dist-tsc/set-path-utils.test.js +210 -0
  35. package/dist-tsc/utils.d.ts +45 -0
  36. package/dist-tsc/utils.d.ts.map +1 -0
  37. package/dist-tsc/utils.js +139 -0
  38. package/dist-tsc/utils.test.d.ts +2 -0
  39. package/dist-tsc/utils.test.d.ts.map +1 -0
  40. package/dist-tsc/utils.test.js +39 -0
  41. package/package.json +16 -8
  42. package/src/CellSetsZarrLoader.js +2 -2
  43. package/src/CellSetsZarrLoader.test.js +1 -1
  44. package/src/cell-set-utils.js +33 -16
  45. package/src/cell-set-utils.test.js +105 -4
  46. package/src/constants.js +4 -15
  47. package/src/index.js +11 -5
  48. package/src/io.js +25 -35
  49. package/src/io.test.js +1 -4
  50. package/src/set-path-utils.js +155 -0
  51. package/src/set-path-utils.test.js +257 -0
  52. package/src/utils.js +20 -9
  53. package/src/utils.test.js +48 -0
@@ -0,0 +1,630 @@
1
+ /* eslint-disable no-underscore-dangle */
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ import { isNil, isEqual, range } from 'lodash-es';
4
+ import { featureCollection as turfFeatureCollection, point as turfPoint } from '@turf/helpers';
5
+ import centroid from '@turf/centroid';
6
+ import concaveman from 'concaveman';
7
+ import { getDefaultColor, PALETTE } from '@vitessce/utils';
8
+ import { HIERARCHICAL_SCHEMAS, } from './constants.js';
9
+ import { pathToKey } from './utils.js';
10
+ /**
11
+ * Alias for the uuidv4 function to make code more readable.
12
+ * @returns {string} UUID.
13
+ */
14
+ function generateKey() {
15
+ return uuidv4();
16
+ }
17
+ /**
18
+ * Get the set associated with a particular node.
19
+ * Recursive.
20
+ * @param {object} currNode A node object.
21
+ * @returns {array} The array representing the set associated with the node.
22
+ */
23
+ export function nodeToSet(currNode) {
24
+ if (!currNode) {
25
+ return [];
26
+ }
27
+ if (!currNode.children) {
28
+ return (currNode.set || []);
29
+ }
30
+ return currNode.children.flatMap(c => nodeToSet(c));
31
+ }
32
+ /**
33
+ * Get the height of a node (the number of levels to reach a leaf).
34
+ * @param {object} currNode A node object.
35
+ * @param {number} level The level that the height will be computed relative to. By default, 0.
36
+ * @returns {number} The height. If the node has a .children property,
37
+ * then the minimum value returned is 1.
38
+ */
39
+ export function nodeToHeight(currNode, level = 0) {
40
+ if (!currNode.children) {
41
+ return level;
42
+ }
43
+ const newLevel = level + 1;
44
+ const childrenHeights = currNode.children.map(c => nodeToHeight(c, newLevel));
45
+ return Math.max(...childrenHeights, newLevel);
46
+ }
47
+ /**
48
+ * Get the size associated with a particular node.
49
+ * Recursive.
50
+ * @param {object} currNode A node object.
51
+ * @returns {number} The length of all the node's children
52
+ */
53
+ export function getNodeLength(currNode) {
54
+ if (!currNode) {
55
+ return 0;
56
+ }
57
+ if (!currNode.children) {
58
+ return (currNode.set?.length || 0);
59
+ }
60
+ return currNode.children.reduce((acc, curr) => acc + getNodeLength(curr), 0);
61
+ }
62
+ /**
63
+ * Find a node with a matching name path, relative to a particular node.
64
+ * @param {object} node A node object.
65
+ * @param {string[]} path The name path for the node of interest.
66
+ * @param {number} currLevelIndex The index of the current hierarchy level.
67
+ * @returns {object|null} A matching node object, or null if none is found.
68
+ */
69
+ function nodeFindNodeByNamePath(node, path, currLevelIndex) {
70
+ const currNodeName = path[currLevelIndex];
71
+ if (node.name === currNodeName) {
72
+ if (currLevelIndex === path.length - 1) {
73
+ return node;
74
+ }
75
+ if (node.children) {
76
+ const foundNodes = node.children
77
+ .map(child => nodeFindNodeByNamePath(child, path, currLevelIndex + 1))
78
+ .filter(Boolean);
79
+ if (foundNodes.length === 1) {
80
+ return foundNodes[0];
81
+ }
82
+ }
83
+ }
84
+ return null;
85
+ }
86
+ /**
87
+ * Find a node with a matching name path, relative to the whole tree.
88
+ * @param {object} currTree A tree object.
89
+ * @param {string[]} targetNamePath The name path for the node of interest.
90
+ * @returns {object|null} A matching node object, or null if none is found.
91
+ */
92
+ export function treeFindNodeByNamePath(currTree, targetNamePath) {
93
+ const foundNodes = currTree.tree
94
+ .map(levelZeroNode => nodeFindNodeByNamePath(levelZeroNode, targetNamePath, 0))
95
+ .filter(Boolean);
96
+ if (foundNodes.length === 1) {
97
+ return foundNodes[0];
98
+ }
99
+ return null;
100
+ }
101
+ /**
102
+ * Transform a node object using a transform function.
103
+ * @param {object} node A node object.
104
+ * @param {function} predicate Returns true if a node matches a condition of interest.
105
+ * @param {function} transform Takes the node matching the predicate as input, returns
106
+ * a transformed version of the node.
107
+ * @param {array} transformedPaths This array parameter is mutated. The path of
108
+ * each transformed node is appended to this array.
109
+ * @param {string[]} The current path of the node being updated, used internally
110
+ * during recursion.
111
+ * @returns {object} The updated node.
112
+ */
113
+ export function nodeTransform(node, predicate, transform, transformedPaths, currPath) {
114
+ let newPath;
115
+ if (!currPath) {
116
+ newPath = [node.name];
117
+ }
118
+ else {
119
+ newPath = [...currPath];
120
+ }
121
+ if (predicate(node, newPath)) {
122
+ transformedPaths.push(newPath);
123
+ return transform(node, newPath);
124
+ }
125
+ if (node.children) {
126
+ return {
127
+ ...node,
128
+ children: node.children.map(child => nodeTransform(child, predicate, transform, transformedPaths, newPath.concat([child.name]))),
129
+ };
130
+ }
131
+ return node;
132
+ }
133
+ /**
134
+ * Transform many node objects using a transform function.
135
+ * @param {object} node A node object.
136
+ * @param {function} predicate Returns true if a node matches a condition of interest.
137
+ * @param {function} transform Takes the node matching the predicate as input, returns
138
+ * a transformed version of the node.
139
+ * @param {array} transformedPaths This array parameter is mutated. The path of
140
+ * each transformed node is appended to this array.
141
+ * @param {string[]} The current path of the node being updated, used internally
142
+ * during recursion.
143
+ * @returns {object} The updated node.
144
+ */
145
+ export function nodeTransformAll(node, predicate, transform, transformedPaths, currPath) {
146
+ let newPath;
147
+ if (!currPath) {
148
+ newPath = [node.name];
149
+ }
150
+ else {
151
+ newPath = [...currPath];
152
+ }
153
+ let newNode = node;
154
+ if (predicate(node, newPath)) {
155
+ transformedPaths.push(newPath);
156
+ newNode = transform(node, newPath);
157
+ }
158
+ if (node.children) {
159
+ return {
160
+ ...newNode,
161
+ children: newNode.children.map(child => nodeTransformAll(child, predicate, transform, transformedPaths, newPath.concat([child.name]))),
162
+ };
163
+ }
164
+ return newNode;
165
+ }
166
+ /**
167
+ * Append a child to a parent node.
168
+ * @param {object} currNode A node object.
169
+ * @param {object} newChild The child node object.
170
+ * @returns {object} The updated node.
171
+ */
172
+ export function nodeAppendChild(currNode, newChild) {
173
+ return {
174
+ ...currNode,
175
+ children: [...currNode.children, newChild],
176
+ };
177
+ }
178
+ /**
179
+ * Prepend a child to a parent node.
180
+ * @param {object} currNode A node object.
181
+ * @param {object} newChild The child node object.
182
+ * @returns {object} The updated node.
183
+ */
184
+ export function nodePrependChild(currNode, newChild) {
185
+ return {
186
+ ...currNode,
187
+ children: [newChild, ...currNode.children],
188
+ };
189
+ }
190
+ /**
191
+ * Insert a child to a parent node.
192
+ * @param {object} currNode A node object.
193
+ * @param {*} newChild The child node object.
194
+ * @param {*} insertIndex The index at which to insert the child.
195
+ * @returns {object} The updated node.
196
+ */
197
+ export function nodeInsertChild(currNode, newChild, insertIndex) {
198
+ const newChildren = Array.from(currNode.children);
199
+ newChildren.splice(insertIndex, 0, newChild);
200
+ return {
201
+ ...currNode,
202
+ children: newChildren,
203
+ };
204
+ }
205
+ /**
206
+ * Get an array representing the union of the sets of checked nodes.
207
+ * @param {object} currTree A tree object.
208
+ * @returns {array} An array representing the union of the sets of checked nodes.
209
+ */
210
+ export function treeToUnion(currTree, checkedPaths) {
211
+ const nodes = checkedPaths.map(path => treeFindNodeByNamePath(currTree, path));
212
+ const nodeSets = nodes.map(node => nodeToSet(node).map(([cellId]) => cellId));
213
+ return nodeSets
214
+ .reduce((a, h) => a.concat(h.filter(hEl => !a.includes(hEl))), nodeSets[0] || []);
215
+ }
216
+ /**
217
+ * Get an array representing the intersection of the sets of checked nodes.
218
+ * @param {object} currTree A tree object.
219
+ * @returns {array} An array representing the intersection of the sets of checked nodes.
220
+ */
221
+ export function treeToIntersection(currTree, checkedPaths) {
222
+ const nodes = checkedPaths.map(path => treeFindNodeByNamePath(currTree, path));
223
+ const nodeSets = nodes.map(node => nodeToSet(node).map(([cellId]) => cellId));
224
+ return nodeSets
225
+ .reduce((a, h) => h.filter(hEl => a.includes(hEl)), nodeSets[0] || []);
226
+ }
227
+ /**
228
+ * Get an array representing the complement of the union of the sets of checked nodes.
229
+ * @param {object} currTree
230
+ * @returns {array} An array representing the complement of the
231
+ * union of the sets of checked nodes.
232
+ */
233
+ export function treeToComplement(currTree, checkedPaths, items) {
234
+ const primaryUnion = treeToUnion(currTree, checkedPaths);
235
+ return items.filter(el => !primaryUnion.includes(el));
236
+ }
237
+ /**
238
+ * Get an flattened array of descendants at a particular relative
239
+ * level of interest.
240
+ * @param {object} node A node object.
241
+ * @param {number} level The relative level of interest.
242
+ * 0 for this node's children, 1 for grandchildren, etc.
243
+ * @param {boolean} stopEarly Should a node be returned early if no children exist?
244
+ * @returns {object[]} An array of descendants at the specified level,
245
+ * where the level is relative to the node.
246
+ */
247
+ export function nodeToLevelDescendantNamePaths(node, level, prevPath, stopEarly = false) {
248
+ if (!node.children) {
249
+ if (!stopEarly) {
250
+ return null;
251
+ }
252
+ return [[...prevPath, node.name]];
253
+ }
254
+ if (level === 0) {
255
+ return [[...prevPath, node.name]];
256
+ }
257
+ return node.children
258
+ .flatMap(c => nodeToLevelDescendantNamePaths(c, level - 1, [...prevPath, node.name], stopEarly))
259
+ .filter(Boolean);
260
+ }
261
+ /**
262
+ * Export the tree by clearing tree state and all node states.
263
+ * @param {object} currTree A tree object.
264
+ * @returns {object} Tree object with tree and node state removed.
265
+ */
266
+ export function treeExport(currTree, datatype) {
267
+ return {
268
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
269
+ datatype,
270
+ tree: currTree.tree,
271
+ };
272
+ }
273
+ /**
274
+ * Export the tree by clearing tree state and all node states,
275
+ * and filter so that only the level zero node of interest is included.
276
+ * @param {object} currTree A tree object.
277
+ * @param {string} nodePath The path of the node of interest.
278
+ * @param {string} dataType Datatype (i.e cell sets)
279
+ * @param {Array} cellSetColors Array of objects of cell set colors and paths
280
+ * @param {string} theme "light" or "dark" for the vitessce theme
281
+ * @returns {object} { treeToExport, nodeName }
282
+ * Tree with one level zero node, and with state removed.
283
+ */
284
+ export function treeExportLevelZeroNode(currTree, nodePath, datatype, cellSetColors, theme) {
285
+ const node = treeFindNodeByNamePath(currTree, nodePath);
286
+ const nodeWithColors = nodeTransformAll(node, () => true, (n, nPath) => {
287
+ const nodeColor = cellSetColors?.find(c => isEqual(c.path, nPath))?.color
288
+ ?? getDefaultColor(theme);
289
+ return {
290
+ ...n,
291
+ color: nodeColor.slice(0, 3),
292
+ };
293
+ }, []);
294
+ const treeWithOneLevelZeroNode = {
295
+ ...currTree,
296
+ tree: [nodeWithColors],
297
+ };
298
+ return {
299
+ treeToExport: treeExport(treeWithOneLevelZeroNode, datatype),
300
+ nodeName: node.name,
301
+ };
302
+ }
303
+ /**
304
+ * Prepare the set of a node of interest for export.
305
+ * @param {object} currTree A tree object.
306
+ * @param {string} nodeKey The key of the node of interest.
307
+ * @returns {object} { setToExport, nodeName } The set as an array.
308
+ */
309
+ export function treeExportSet(currTree, nodePath) {
310
+ const node = treeFindNodeByNamePath(currTree, nodePath);
311
+ return { setToExport: nodeToSet(node), nodeName: node.name };
312
+ }
313
+ /**
314
+ * Get an empty tree, with a default tree state.
315
+ * @param {string} datatype The type of sets that this tree contains.
316
+ * @returns {object} Empty tree.
317
+ */
318
+ export function treeInitialize(datatype) {
319
+ return {
320
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
321
+ datatype,
322
+ tree: [],
323
+ };
324
+ }
325
+ /**
326
+ * For convenience, get an object with information required
327
+ * to render a node as a component.
328
+ * @param {object} node A node to be rendered.
329
+ * @returns {object} An object containing properties required
330
+ * by the TreeNode render functions.
331
+ */
332
+ export function nodeToRenderProps(node, path, cellSetColor) {
333
+ const level = path.length - 1;
334
+ return {
335
+ title: node.name,
336
+ nodeKey: pathToKey(path),
337
+ path,
338
+ size: getNodeLength(node),
339
+ color: cellSetColor?.find(d => isEqual(d.path, path))?.color,
340
+ level,
341
+ isLeaf: (!node.children || node.children.length === 0) && Boolean(node.set),
342
+ height: nodeToHeight(node),
343
+ };
344
+ }
345
+ /**
346
+ * Using a color and a probability, mix the color with an "uncertainty" color,
347
+ * for example, gray.
348
+ * Reference: https://github.com/bgrins/TinyColor/blob/80f7225029c428c0de0757f7d98ac15f497bee57/tinycolor.js#L701
349
+ * @param {number[]} originalColor The color assignment for the class.
350
+ * @param {number} p The mixing amount, or level certainty in the originalColor classification,
351
+ * between 0 and 1.
352
+ * @param {number[]} mixingColor The color with which to mix. By default, [128, 128, 128] gray.
353
+ * @returns {number[]} Returns the color after mixing.
354
+ */
355
+ function colorMixWithUncertainty(originalColor, p, mixingColor = [128, 128, 128]) {
356
+ return [
357
+ ((originalColor[0] - mixingColor[0]) * p) + mixingColor[0],
358
+ ((originalColor[1] - mixingColor[1]) * p) + mixingColor[1],
359
+ ((originalColor[2] - mixingColor[2]) * p) + mixingColor[2],
360
+ ];
361
+ }
362
+ /**
363
+ * Given a tree with state, get the cellIds and cellColors,
364
+ * based on the nodes currently marked as "visible".
365
+ * @param {object} currTree A tree object.
366
+ * @param {array} selectedNamePaths Array of arrays of strings,
367
+ * representing set "paths".
368
+ * @param {object[]} cellSetColor Array of objects with the
369
+ * properties `path` and `color`.
370
+ * @param {string} theme "light" or "dark" for the vitessce theme
371
+ * @returns {array} Tuple of [cellIds, cellColors]
372
+ * where cellIds is an array of strings,
373
+ * and cellColors is an object mapping cellIds to color [r,g,b] arrays.
374
+ */
375
+ export function treeToCellColorsBySetNames(currTree, selectedNamePaths, cellSetColor, theme) {
376
+ let cellColorsArray = [];
377
+ selectedNamePaths.forEach((setNamePath) => {
378
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
379
+ if (node) {
380
+ const nodeSet = nodeToSet(node);
381
+ const nodeColor = (cellSetColor?.find(d => isEqual(d.path, setNamePath))?.color
382
+ || getDefaultColor(theme));
383
+ cellColorsArray = [
384
+ ...cellColorsArray,
385
+ ...nodeSet.map(([cellId, prob]) => [
386
+ cellId,
387
+ (isNil(prob) ? nodeColor : colorMixWithUncertainty(nodeColor, prob)),
388
+ ]),
389
+ ];
390
+ }
391
+ });
392
+ return new Map(cellColorsArray);
393
+ }
394
+ /**
395
+ * Given a tree with state, get an array of
396
+ * objects with cellIds and cellColors,
397
+ * based on the nodes currently marked as "visible".
398
+ * @param {object} currTree A tree object.
399
+ * @param {array} selectedNamePaths Array of arrays of strings,
400
+ * representing set "paths".
401
+ * @param {object[]} setColor Array of objects with the
402
+ * properties `path` and `color`
403
+ * @param {string} theme "light" or "dark" for the vitessce theme.
404
+ * @returns {object[]} Array of objects with properties
405
+ * `obsId`, `name`, and `color`.
406
+ */
407
+ export function treeToObjectsBySetNames(currTree, selectedNamePaths, setColor, theme) {
408
+ let cellsArray = [];
409
+ for (let i = 0; i < selectedNamePaths.length; i += 1) {
410
+ const setNamePath = selectedNamePaths[i];
411
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
412
+ if (node) {
413
+ const nodeSet = nodeToSet(node);
414
+ const nodeColor = (setColor?.find(d => isEqual(d.path, setNamePath))?.color
415
+ || getDefaultColor(theme));
416
+ cellsArray = cellsArray.concat(nodeSet.map(([cellId]) => ({
417
+ obsId: cellId,
418
+ name: node.name,
419
+ color: nodeColor,
420
+ })));
421
+ }
422
+ }
423
+ return cellsArray;
424
+ }
425
+ export function treeToCellPolygonsBySetNames(currTree, obsIndex, obsEmbedding, selectedNamePaths, cellSetColor, theme) {
426
+ const obsIndexMap = new Map(obsIndex.map((key, i) => ([key, i])));
427
+ const cellSetPolygons = [];
428
+ selectedNamePaths.forEach((setNamePath) => {
429
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
430
+ if (node) {
431
+ const nodeSet = nodeToSet(node);
432
+ const nodeColor = (cellSetColor?.find(d => isEqual(d.path, setNamePath))?.color
433
+ || getDefaultColor(theme));
434
+ const cellPositions = nodeSet
435
+ .map(([cellId]) => {
436
+ const cellIdx = obsIndexMap.get(cellId);
437
+ return [
438
+ obsEmbedding.data[0][cellIdx],
439
+ -obsEmbedding.data[1][cellIdx],
440
+ ];
441
+ })
442
+ .filter(cell => cell.every(i => typeof i === 'number'));
443
+ if (cellPositions.length > 2) {
444
+ const points = turfFeatureCollection(cellPositions.map(turfPoint));
445
+ const concavity = Infinity;
446
+ const hullCoords = concaveman(cellPositions, concavity);
447
+ if (hullCoords) {
448
+ const centroidCoords = centroid(points).geometry.coordinates;
449
+ cellSetPolygons.push({
450
+ path: setNamePath,
451
+ name: setNamePath[setNamePath.length - 1],
452
+ hull: hullCoords,
453
+ color: nodeColor,
454
+ centroid: centroidCoords,
455
+ });
456
+ }
457
+ }
458
+ }
459
+ });
460
+ return cellSetPolygons;
461
+ }
462
+ /**
463
+ * Given a tree with state, get the sizes of the
464
+ * sets currently marked as "visible".
465
+ * @param {object} currTree A tree object.
466
+ * @param {array} allNamePaths Array of all paths.
467
+ * @param {array} selectedNamePaths Array of arrays of strings,
468
+ * representing selected paths.
469
+ * @param {object[]} setColor Array of objects with the
470
+ * properties `path` and `color`.
471
+ * @param {string} theme "light" or "dark" for the vitessce theme
472
+ * @returns {object[]} Array of objects
473
+ * with the properties `name`, `size`, `key`,
474
+ * and `color`.
475
+ */
476
+ export function treeToSetSizesBySetNames(currTree, allNamePaths, selectedNamePaths, setColor, theme) {
477
+ const sizes = [];
478
+ /**
479
+ * Checks if a path is contained in an array of paths.
480
+ * @param {array} path Array of strings, which compose the path.
481
+ * @param {array} paths Array of arrays of strings, which compose paths.
482
+ * */
483
+ const contains = (path, paths) => paths.some(p => isEqual(p, path));
484
+ allNamePaths.forEach((clusterPath) => {
485
+ const node = treeFindNodeByNamePath(currTree, clusterPath);
486
+ if (node) {
487
+ const nodeSet = nodeToSet(node);
488
+ const nodeColor = setColor?.find(d => isEqual(d.path, clusterPath))?.color
489
+ || getDefaultColor(theme);
490
+ const nodeProps = {
491
+ key: generateKey(),
492
+ name: node.name,
493
+ size: nodeSet.length,
494
+ color: nodeColor,
495
+ setNamePath: clusterPath,
496
+ // used by the CellSetSizesPlot to determine if the bar should be grayed out
497
+ isGrayedOut: true,
498
+ };
499
+ // if the current path is selected, we need to show it
500
+ if (contains(clusterPath, selectedNamePaths)) {
501
+ nodeProps.isGrayedOut = false;
502
+ }
503
+ sizes.push(nodeProps);
504
+ }
505
+ });
506
+ return sizes;
507
+ }
508
+ /**
509
+ * Find and remove a node from the descendants of the current node.
510
+ * @param {object} node A node to search on.
511
+ * @param {array} prevPath Path of the current node to be searched.
512
+ * @param {array} filterPath The path sought.
513
+ * @returns {object} A new node without a node at filterPath.
514
+ */
515
+ export function filterNode(node, prevPath, filterPath) {
516
+ if (isEqual([...prevPath, node.name], filterPath)) {
517
+ return null;
518
+ }
519
+ if (!node.children) {
520
+ return node;
521
+ }
522
+ return {
523
+ ...node,
524
+ children: node.children.map(c => filterNode(c, [...prevPath, node.name], filterPath)).filter(Boolean),
525
+ };
526
+ }
527
+ export function treeToExpectedCheckedLevel(currTree, checkedPaths) {
528
+ let result = null;
529
+ if (currTree) {
530
+ currTree.tree.forEach((lzn) => {
531
+ const levelZeroPath = [lzn.name];
532
+ const height = nodeToHeight(lzn);
533
+ range(height).forEach((i) => {
534
+ const levelIndex = i + 1;
535
+ const levelNodePaths = nodeToLevelDescendantNamePaths(lzn, levelIndex, [], true);
536
+ if (isEqual(levelNodePaths, checkedPaths)) {
537
+ result = { levelZeroPath, levelIndex };
538
+ }
539
+ });
540
+ });
541
+ }
542
+ return result;
543
+ }
544
+ export function treesConflict(cellSets, testCellSets) {
545
+ const paths = [];
546
+ const testPaths = [];
547
+ let hasConflict = false;
548
+ function getPaths(node, prevPath) {
549
+ paths.push([...prevPath, node.name]);
550
+ if (node.children) {
551
+ node.children.forEach(c => getPaths(c, [...prevPath, node.name]));
552
+ }
553
+ }
554
+ cellSets.tree.forEach(lzn => getPaths(lzn, []));
555
+ function getTestPaths(node, prevPath) {
556
+ testPaths.push([...prevPath, node.name]);
557
+ if (node.children) {
558
+ node.children.forEach(c => getPaths(c, [...prevPath, node.name]));
559
+ }
560
+ }
561
+ testCellSets.tree.forEach(lzn => getTestPaths(lzn, []));
562
+ testPaths.forEach((testPath) => {
563
+ if (paths.find(p => isEqual(p, testPath))) {
564
+ hasConflict = true;
565
+ }
566
+ });
567
+ return hasConflict;
568
+ }
569
+ export function initializeCellSetColor(cellSets, cellSetColor) {
570
+ const nextCellSetColor = [...(cellSetColor || [])];
571
+ const nodeCountPerTreePerLevel = cellSets.tree.map(tree => Array
572
+ .from({
573
+ length: nodeToHeight(tree) + 1, // Need to add one because its an array.
574
+ }).fill(0));
575
+ function processNode(node, prevPath, hierarchyLevel, treeIndex) {
576
+ const index = nodeCountPerTreePerLevel[treeIndex][hierarchyLevel];
577
+ const nodePath = [...prevPath, node.name];
578
+ const nodeColor = nextCellSetColor.find(d => isEqual(d.path, nodePath));
579
+ if (!nodeColor) {
580
+ // If there is a color for the node specified via the cell set tree,
581
+ // then use it. Otherwise, use a color from the default color palette.
582
+ const nodeColorArray = (node.color ? node.color : PALETTE[index % PALETTE.length]);
583
+ nextCellSetColor.push({
584
+ path: nodePath,
585
+ color: nodeColorArray,
586
+ });
587
+ }
588
+ nodeCountPerTreePerLevel[treeIndex][hierarchyLevel] += 1;
589
+ if (node.children) {
590
+ node.children.forEach(c => processNode(c, nodePath, hierarchyLevel + 1, treeIndex));
591
+ }
592
+ }
593
+ cellSets.tree.forEach((lzn, treeIndex) => processNode(lzn, [], 0, treeIndex));
594
+ return nextCellSetColor;
595
+ }
596
+ export function getCellSetPolygons(params) {
597
+ const { obsIndex, obsEmbedding, cellSets, cellSetSelection, cellSetColor, theme, } = params;
598
+ if (cellSetSelection && cellSetSelection.length > 0 && cellSets && obsIndex && obsEmbedding) {
599
+ return treeToCellPolygonsBySetNames(cellSets, obsIndex, obsEmbedding, cellSetSelection, cellSetColor, theme);
600
+ }
601
+ return [];
602
+ }
603
+ export function treeToMembershipMap(currTree) {
604
+ const result = new Map();
605
+ if (currTree) {
606
+ currTree.tree.forEach((lzn) => {
607
+ const height = nodeToHeight(lzn);
608
+ range(height).forEach((i) => {
609
+ const levelIndex = i + 1;
610
+ const levelNodePaths = nodeToLevelDescendantNamePaths(lzn, levelIndex, [], true);
611
+ levelNodePaths.forEach((setNamePath) => {
612
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
613
+ // If this is a child node, then we are interested.
614
+ if (node && !node.children) {
615
+ const nodeSet = nodeToSet(node);
616
+ nodeSet.forEach(([obsId]) => {
617
+ if (result.has(obsId)) {
618
+ result.get(obsId).push(setNamePath);
619
+ }
620
+ else {
621
+ result.set(obsId, [setNamePath]);
622
+ }
623
+ });
624
+ }
625
+ });
626
+ });
627
+ });
628
+ }
629
+ return result;
630
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cell-set-utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cell-set-utils.test.d.ts","sourceRoot":"","sources":["../src/cell-set-utils.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,47 @@
1
+ export namespace levelTwoNodeLeaf {
2
+ const name: string;
3
+ const set: (string | null)[][];
4
+ }
5
+ export namespace levelOneNode {
6
+ const name_1: string;
7
+ export { name_1 as name };
8
+ export const children: {
9
+ name: string;
10
+ set: (string | null)[][];
11
+ }[];
12
+ }
13
+ export namespace levelZeroNode {
14
+ const name_2: string;
15
+ export { name_2 as name };
16
+ const children_1: {
17
+ name: string;
18
+ children: {
19
+ name: string;
20
+ set: (string | null)[][];
21
+ }[];
22
+ }[];
23
+ export { children_1 as children };
24
+ }
25
+ export namespace tree {
26
+ const version: string;
27
+ const datatype: string;
28
+ const tree: {
29
+ name: string;
30
+ children: {
31
+ name: string;
32
+ children: ({
33
+ name: string;
34
+ set: (string | null)[][];
35
+ children?: undefined;
36
+ } | {
37
+ name: string;
38
+ children: {
39
+ name: string;
40
+ set: (string | null)[][];
41
+ }[];
42
+ set?: undefined;
43
+ })[];
44
+ }[];
45
+ }[];
46
+ }
47
+ //# sourceMappingURL=cell-set-utils.test.fixtures.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cell-set-utils.test.fixtures.d.ts","sourceRoot":"","sources":["../src/cell-set-utils.test.fixtures.js"],"names":[],"mappings":""}