@vitessce/sets-utils 3.4.6 → 3.4.8

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.
@@ -391,6 +391,40 @@ export function treeToCellColorsBySetNames(currTree, selectedNamePaths, cellSetC
391
391
  });
392
392
  return new Map(cellColorsArray);
393
393
  }
394
+ /**
395
+ * Given a tree with state, get the cellIds and cellColors,
396
+ * based on the nodes currently marked as "visible".
397
+ * @param {object} currTree A tree object.
398
+ * @param {array} selectedNamePaths Array of arrays of strings,
399
+ * representing set "paths".
400
+ * @param {object[]} cellSetColor Array of objects with the
401
+ * properties `path` and `color`.
402
+ * @param {string} theme "light" or "dark" for the vitessce theme
403
+ * @returns {array} Tuple of [cellIds, cellColors]
404
+ * where cellIds is an array of strings,
405
+ * and cellColors is an object mapping cellIds to color [r,g,b] arrays.
406
+ */
407
+ export function treeToSelectedSetMap(currTree, selectedNamePaths) {
408
+ let result = [];
409
+ selectedNamePaths.forEach((setNamePath) => {
410
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
411
+ if (node) {
412
+ const nodeSet = nodeToSet(node);
413
+ result = [
414
+ ...result,
415
+ ...nodeSet.map(([cellId]) => [
416
+ cellId,
417
+ // TODO: should this be the full path
418
+ // (rather than only the node name)?
419
+ // Or the index of the selected set
420
+ // with respect to the selectedNamePaths array?
421
+ setNamePath,
422
+ ]),
423
+ ];
424
+ }
425
+ });
426
+ return new Map(result);
427
+ }
394
428
  /**
395
429
  * Given a tree with state, get a mapping from cell ID to cell set color index,
396
430
  * based on the nodes currently marked as "visible".
@@ -535,6 +569,65 @@ export function treeToSetSizesBySetNames(currTree, allNamePaths, selectedNamePat
535
569
  });
536
570
  return sizes;
537
571
  }
572
+ /**
573
+ * Given a tree with state, get the indices of observations
574
+ * contained in each selected obs set.
575
+ * @param {object} currTree A tree object.
576
+ * @param {array} selectedNamePaths Array of arrays of strings,
577
+ * representing set "paths".
578
+ * @returns {object[]} Array of objects
579
+ * with the properties `name`, `size`, `key`,
580
+ * and `color`.
581
+ */
582
+ export function treeToObsIdsBySetNames(currTree, selectedNamePaths) {
583
+ const indices = [];
584
+ selectedNamePaths.forEach((setNamePath) => {
585
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
586
+ if (node) {
587
+ const nodeSet = nodeToSet(node);
588
+ indices.push({
589
+ key: generateKey(),
590
+ name: node.name,
591
+ path: setNamePath,
592
+ size: nodeSet.length,
593
+ // TODO: handle the case where the ID is in the set but missing
594
+ // from the obsIndexMap
595
+ ids: nodeSet.map(([obsId]) => obsId),
596
+ });
597
+ }
598
+ });
599
+ return indices;
600
+ }
601
+ /**
602
+ * Given a tree with state, get the indices of observations
603
+ * contained in each selected obs set.
604
+ * @param {object} currTree A tree object.
605
+ * @param {array} selectedNamePaths Array of arrays of strings,
606
+ * representing set "paths".
607
+ * @param {object[]} obsIndex The observation index.
608
+ * @returns {object[]} Array of objects
609
+ * with the properties `name`, `size`, `key`,
610
+ * and `color`.
611
+ */
612
+ export function treeToObsIndicesBySetNames(currTree, selectedNamePaths, obsIndexMap) {
613
+ const indices = [];
614
+ selectedNamePaths.forEach((setNamePath) => {
615
+ const node = treeFindNodeByNamePath(currTree, setNamePath);
616
+ if (node) {
617
+ const nodeSet = nodeToSet(node);
618
+ indices.push({
619
+ key: generateKey(),
620
+ name: node.name,
621
+ path: setNamePath,
622
+ size: nodeSet.length,
623
+ // TODO: handle the case where the ID is in the set but missing
624
+ // from the obsIndexMap
625
+ indices: nodeSet.map(([obsId]) => obsIndexMap[obsId]),
626
+ });
627
+ }
628
+ });
629
+ return indices;
630
+ }
538
631
  /**
539
632
  * Find and remove a node from the descendants of the current node.
540
633
  * @param {object} node A node to search on.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Input: selections and data.
3
+ * Output: expression data in a hierarchy like
4
+ * [
5
+ * {
6
+ * "selectedCellSet1": {
7
+ * "selectedSampleSet1": [],
8
+ * }
9
+ * },
10
+ * exprMax
11
+ * ]
12
+ * @param {*} sampleEdges
13
+ * @param {*} sampleSets
14
+ * @param {*} sampleSetSelection
15
+ * @param {*} expressionData
16
+ * @param {*} obsIndex
17
+ * @param {*} mergedCellSets
18
+ * @param {*} geneSelection
19
+ * @param {*} cellSetSelection
20
+ * @param {*} cellSetColor
21
+ * @param {*} featureValueTransform
22
+ * @param {*} featureValueTransformCoefficient
23
+ * @returns
24
+ */
25
+ export function stratifyArrays(sampleEdges: any, sampleIdToObsIdsMap: any, sampleSets: any, sampleSetSelection: any, obsIndex: any, mergedCellSets: any, cellSetSelection: any, arraysToStratify: any): any;
26
+ /**
27
+ * Input: selections and data.
28
+ * Output: expression data in a hierarchy like
29
+ * [
30
+ * {
31
+ * "selectedCellSet1": {
32
+ * "selectedSampleSet1": [],
33
+ * }
34
+ * },
35
+ * exprMax
36
+ * ]
37
+ * @param {*} sampleEdges
38
+ * @param {*} sampleSets
39
+ * @param {*} sampleSetSelection
40
+ * @param {*} expressionData
41
+ * @param {*} obsIndex
42
+ * @param {*} mergedCellSets
43
+ * @param {*} geneSelection
44
+ * @param {*} cellSetSelection
45
+ * @param {*} cellSetColor
46
+ * @param {*} featureValueTransform
47
+ * @param {*} featureValueTransformCoefficient
48
+ * @returns
49
+ */
50
+ export function stratifyExpressionData(sampleEdges: any, sampleSets: any, sampleSetSelection: any, expressionData: any, obsIndex: any, mergedCellSets: any, geneSelection: any, cellSetSelection: any, cellSetColor: any, featureValueTransform: any, featureValueTransformCoefficient: any): any[];
51
+ /**
52
+ * Supports three-level stratified input
53
+ * (cell set, sample set, gene).
54
+ * Returns two-level stratified output (cell set, sample set).
55
+ * Aggregate stratified expression data so that there is
56
+ * a single value for each (cell set, sample set) tuple.
57
+ * I.e., aggregate along the gene axis.
58
+ * @param {*} stratifiedResult
59
+ * @param {*} geneSelection
60
+ * @returns
61
+ */
62
+ export function aggregateStratifiedExpressionData(stratifiedResult: any, geneSelection: any): any;
63
+ //# sourceMappingURL=expr-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expr-utils.d.ts","sourceRoot":"","sources":["../src/expr-utils.js"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,4CAbW,GAAC,wCACD,GAAC,sBACD,GAAC,YAED,GAAC,kBACD,GAAC,oBAED,GAAC,8BAoIX;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,oDAbW,GAAC,cACD,GAAC,sBACD,GAAC,kBACD,GAAC,YACD,GAAC,kBACD,GAAC,iBACD,GAAC,oBACD,GAAC,gBACD,GAAC,yBACD,GAAC,oCACD,GAAC,SAiFX;AAED;;;;;;;;;;GAUG;AACH,oEAJW,GAAC,iBACD,GAAC,OAmBX"}
@@ -0,0 +1,240 @@
1
+ /* eslint-disable camelcase */
2
+ import { InternMap } from 'internmap';
3
+ import { getValueTransformFunction } from '@vitessce/utils';
4
+ import { isEqual } from 'lodash-es';
5
+ import { treeToSelectedSetMap, treeToObsIdsBySetNames, } from './cell-set-utils.js';
6
+ /**
7
+ * Input: selections and data.
8
+ * Output: expression data in a hierarchy like
9
+ * [
10
+ * {
11
+ * "selectedCellSet1": {
12
+ * "selectedSampleSet1": [],
13
+ * }
14
+ * },
15
+ * exprMax
16
+ * ]
17
+ * @param {*} sampleEdges
18
+ * @param {*} sampleSets
19
+ * @param {*} sampleSetSelection
20
+ * @param {*} expressionData
21
+ * @param {*} obsIndex
22
+ * @param {*} mergedCellSets
23
+ * @param {*} geneSelection
24
+ * @param {*} cellSetSelection
25
+ * @param {*} cellSetColor
26
+ * @param {*} featureValueTransform
27
+ * @param {*} featureValueTransformCoefficient
28
+ * @returns
29
+ */
30
+ export function stratifyArrays(sampleEdges, sampleIdToObsIdsMap, sampleSets, sampleSetSelection, obsIndex, mergedCellSets, cellSetSelection, arraysToStratify) {
31
+ const result = new InternMap([], JSON.stringify);
32
+ const hasSampleSetSelection = Array.isArray(sampleSetSelection) && sampleSetSelection.length > 0;
33
+ const hasCellSetSelection = Array.isArray(cellSetSelection) && cellSetSelection.length > 0;
34
+ const sampleSetKeys = hasSampleSetSelection ? sampleSetSelection : [null];
35
+ const cellSetKeys = hasCellSetSelection ? cellSetSelection : [null];
36
+ const arrKeys = Object.keys(arraysToStratify);
37
+ if (arrKeys.includes('obsIndex') || arrKeys.includes('i')) {
38
+ throw new Error('The keys "obsIndex" and "i" are reserved for internal use.');
39
+ }
40
+ if (Object.values(arraysToStratify).some(arr => arr.length !== obsIndex.length)) {
41
+ throw new Error('All arrays must have the same length as the obsIndex.');
42
+ }
43
+ const sampleSetInfo = sampleSets && sampleSetSelection
44
+ ? treeToObsIdsBySetNames(sampleSets, sampleSetSelection)
45
+ : null;
46
+ const cellSetInfo = mergedCellSets && cellSetSelection
47
+ ? treeToObsIdsBySetNames(mergedCellSets, cellSetSelection)
48
+ : null;
49
+ // First level: cell set
50
+ cellSetKeys.forEach((cellSetKey) => {
51
+ result.set(cellSetKey, new InternMap([], JSON.stringify));
52
+ // Second level: sample set
53
+ sampleSetKeys.forEach((sampleSetKey) => {
54
+ result.get(cellSetKey).set(sampleSetKey, new InternMap([], JSON.stringify));
55
+ // Third level: array
56
+ // Handle cellSetKey and sampleSetKey being null.
57
+ const sampleIdsInSampleSet = (sampleSetInfo && sampleSetKey
58
+ ? sampleSetInfo?.find(n => isEqual(n.path, sampleSetKey))
59
+ : null)?.ids;
60
+ // The sampleSets contain sampleIds. We need to get the obsIds
61
+ // corresponding to each sampleId.
62
+ const obsIdsInSampleSet = sampleIdsInSampleSet
63
+ ? sampleIdsInSampleSet.flatMap(sampleId => sampleIdToObsIdsMap?.get(sampleId) || [])
64
+ : null;
65
+ const obsIdsInCellSet = (cellSetInfo && cellSetKey
66
+ ? cellSetInfo?.find(n => isEqual(n.path, cellSetKey))
67
+ : null)?.ids;
68
+ // Handle cases where obsIdsInSampleSet and/or obsIdsInCellSet is null.
69
+ let subObsIndex = obsIndex;
70
+ if (obsIdsInSampleSet && obsIdsInCellSet) {
71
+ subObsIndex = Array.from((new Set(obsIdsInSampleSet)).intersection(new Set(obsIdsInCellSet)));
72
+ }
73
+ else if (obsIdsInSampleSet) {
74
+ subObsIndex = obsIdsInSampleSet;
75
+ }
76
+ else if (obsIdsInCellSet) {
77
+ subObsIndex = obsIdsInCellSet;
78
+ }
79
+ const arrLen = subObsIndex.length;
80
+ arrKeys.forEach((arrKey) => {
81
+ // Instantiate a new array of the appropriate size.
82
+ const newArr = new arraysToStratify[arrKey].constructor(arrLen);
83
+ result.get(cellSetKey).get(sampleSetKey).set(arrKey, newArr);
84
+ });
85
+ result.get(cellSetKey).get(sampleSetKey).set('obsIndex', subObsIndex);
86
+ // We initialize an incrementer to keep track of
87
+ // the next index to use for insertion into the array.
88
+ result.get(cellSetKey).get(sampleSetKey).set('i', 0);
89
+ });
90
+ });
91
+ const sampleIdToSetMap = sampleSets && sampleSetSelection
92
+ ? treeToSelectedSetMap(sampleSets, sampleSetSelection)
93
+ : null;
94
+ const cellIdToSetMap = mergedCellSets && cellSetSelection
95
+ ? treeToSelectedSetMap(mergedCellSets, cellSetSelection)
96
+ : null;
97
+ // Iterate over every observation.
98
+ // Insert the ID and all corresponding values into
99
+ // the appropriate arrays within the InternMap.
100
+ for (let i = 0; i < obsIndex.length; i += 1) {
101
+ const obsId = obsIndex[i];
102
+ const cellSet = cellIdToSetMap?.get(obsId) || null;
103
+ const sampleId = sampleEdges?.get(obsId);
104
+ const sampleSet = sampleId ? (sampleIdToSetMap?.get(sampleId) || null) : null;
105
+ if ((hasSampleSetSelection && !sampleSet) || (hasCellSetSelection && !cellSet)) {
106
+ // Skip this sample if it is not in the selected sample set.
107
+ // eslint-disable-next-line no-continue
108
+ continue;
109
+ }
110
+ const insertionIndex = result.get(cellSet).get(sampleSet).get('i');
111
+ // eslint-disable-next-line no-loop-func
112
+ arrKeys.forEach((arrKey) => {
113
+ const value = arraysToStratify[arrKey][i];
114
+ result
115
+ .get(cellSet)
116
+ .get(sampleSet)
117
+ .get(arrKey)[insertionIndex] = value;
118
+ });
119
+ result.get(cellSet).get(sampleSet).set('i', insertionIndex + 1);
120
+ }
121
+ cellSetKeys.forEach((cellSetKey) => {
122
+ // Second level: sample set
123
+ sampleSetKeys.forEach((sampleSetKey) => {
124
+ const finalInsertionIndex = result.get(cellSetKey).get(sampleSetKey).get('i');
125
+ if (finalInsertionIndex !== result.get(cellSetKey).get(sampleSetKey).get('obsIndex').length) {
126
+ console.warn('The final insertion index is lower than expected.');
127
+ }
128
+ result.get(cellSetKey).get(sampleSetKey).delete('i');
129
+ });
130
+ });
131
+ return result;
132
+ }
133
+ /**
134
+ * Input: selections and data.
135
+ * Output: expression data in a hierarchy like
136
+ * [
137
+ * {
138
+ * "selectedCellSet1": {
139
+ * "selectedSampleSet1": [],
140
+ * }
141
+ * },
142
+ * exprMax
143
+ * ]
144
+ * @param {*} sampleEdges
145
+ * @param {*} sampleSets
146
+ * @param {*} sampleSetSelection
147
+ * @param {*} expressionData
148
+ * @param {*} obsIndex
149
+ * @param {*} mergedCellSets
150
+ * @param {*} geneSelection
151
+ * @param {*} cellSetSelection
152
+ * @param {*} cellSetColor
153
+ * @param {*} featureValueTransform
154
+ * @param {*} featureValueTransformCoefficient
155
+ * @returns
156
+ */
157
+ export function stratifyExpressionData(sampleEdges, sampleSets, sampleSetSelection, expressionData, obsIndex, mergedCellSets, geneSelection, cellSetSelection, cellSetColor, // TODO: remove this parameter
158
+ featureValueTransform, featureValueTransformCoefficient) {
159
+ const result = new InternMap([], JSON.stringify);
160
+ const hasSampleSetSelection = Array.isArray(sampleSetSelection) && sampleSetSelection.length > 0;
161
+ const hasCellSetSelection = Array.isArray(cellSetSelection) && cellSetSelection.length > 0;
162
+ const hasGeneSelection = Array.isArray(geneSelection) && geneSelection.length > 0;
163
+ const sampleSetKeys = hasSampleSetSelection ? sampleSetSelection : [null];
164
+ const cellSetKeys = hasCellSetSelection ? cellSetSelection : [null];
165
+ const geneKeys = hasGeneSelection ? geneSelection : [null];
166
+ // TODO: return a flat array with { cellSet, sampleSet, gene } objects.
167
+ // Then, use d3.group to create nested InternMaps.
168
+ // First level: cell set
169
+ cellSetKeys.forEach((cellSetKey) => {
170
+ result.set(cellSetKey, new InternMap([], JSON.stringify));
171
+ // Second level: sample set
172
+ sampleSetKeys.forEach((sampleSetKey) => {
173
+ result.get(cellSetKey).set(sampleSetKey, new InternMap([], JSON.stringify));
174
+ // Third level: gene
175
+ geneKeys.forEach((geneKey) => {
176
+ result.get(cellSetKey).get(sampleSetKey).set(geneKey, []);
177
+ });
178
+ });
179
+ });
180
+ if (mergedCellSets && cellSetSelection
181
+ && geneSelection && geneSelection.length >= 1
182
+ && expressionData) {
183
+ const sampleIdToSetMap = sampleSets && sampleSetSelection
184
+ ? treeToSelectedSetMap(sampleSets, sampleSetSelection)
185
+ : null;
186
+ const cellIdToSetMap = treeToSelectedSetMap(mergedCellSets, cellSetSelection);
187
+ let exprMax = -Infinity;
188
+ for (let i = 0; i < obsIndex.length; i += 1) {
189
+ const obsId = obsIndex[i];
190
+ const cellSet = cellIdToSetMap?.get(obsId);
191
+ const sampleId = sampleEdges?.get(obsId);
192
+ const sampleSet = sampleId ? sampleIdToSetMap?.get(sampleId) : null;
193
+ if (hasSampleSetSelection && !sampleSet) {
194
+ // Skip this sample if it is not in the selected sample set.
195
+ // eslint-disable-next-line no-continue
196
+ continue;
197
+ }
198
+ // eslint-disable-next-line no-loop-func
199
+ geneKeys.forEach((geneKey, geneI) => {
200
+ const value = expressionData[geneI][i];
201
+ const transformFunction = getValueTransformFunction(featureValueTransform, featureValueTransformCoefficient);
202
+ const transformedValue = transformFunction(value);
203
+ exprMax = Math.max(transformedValue, exprMax);
204
+ result
205
+ .get(cellSet)
206
+ ?.get(sampleSet)
207
+ ?.get(geneKey)
208
+ ?.push(transformedValue);
209
+ });
210
+ }
211
+ return [result, exprMax];
212
+ }
213
+ return [null, null];
214
+ }
215
+ /**
216
+ * Supports three-level stratified input
217
+ * (cell set, sample set, gene).
218
+ * Returns two-level stratified output (cell set, sample set).
219
+ * Aggregate stratified expression data so that there is
220
+ * a single value for each (cell set, sample set) tuple.
221
+ * I.e., aggregate along the gene axis.
222
+ * @param {*} stratifiedResult
223
+ * @param {*} geneSelection
224
+ * @returns
225
+ */
226
+ export function aggregateStratifiedExpressionData(stratifiedResult, geneSelection) {
227
+ const result = new InternMap([], JSON.stringify);
228
+ if (stratifiedResult) {
229
+ Array.from(stratifiedResult.keys()).forEach((cellSetKey) => {
230
+ result.set(cellSetKey, new InternMap([], JSON.stringify));
231
+ Array.from(stratifiedResult.get(cellSetKey).keys()).forEach((sampleSetKey) => {
232
+ // For now, we just take the first gene.
233
+ // TODO: support multiple genes via signature score method.
234
+ const values = stratifiedResult.get(cellSetKey).get(sampleSetKey).get(geneSelection[0]);
235
+ result.get(cellSetKey).set(sampleSetKey, values);
236
+ });
237
+ });
238
+ }
239
+ return result;
240
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=expr-utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expr-utils.test.d.ts","sourceRoot":"","sources":["../src/expr-utils.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,115 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { stratifyExpressionData, aggregateStratifiedExpressionData, stratifyArrays, } from './expr-utils.js';
3
+ describe('Utility functions for processing expression data', () => {
4
+ describe('stratifyExpressionData and stratifyArrays functions', () => {
5
+ const sampleEdges = new Map([
6
+ ['cell1-1', 'donor1'],
7
+ ['cell1-2', 'donor1'],
8
+ ['cell1-3', 'donor1'],
9
+ ['cell1-4', 'donor1'],
10
+ ['cell2-1', 'donor2'],
11
+ ['cell2-2', 'donor2'],
12
+ ['cell2-3', 'donor2'],
13
+ ['cell2-4', 'donor2'],
14
+ ]);
15
+ const sampleSets = {
16
+ tree: [
17
+ {
18
+ name: 'Clinical groups',
19
+ children: [
20
+ {
21
+ name: 'AKI',
22
+ set: [['donor1', null]],
23
+ },
24
+ {
25
+ name: 'CKD',
26
+ set: [['donor2', null]],
27
+ },
28
+ ],
29
+ },
30
+ ],
31
+ };
32
+ const sampleSetSelection = [
33
+ ['Clinical groups', 'AKI'],
34
+ ['Clinical groups', 'CKD'],
35
+ ];
36
+ const expressionData = [
37
+ // Gene 1
38
+ [10, 20, 30, 40, 11, 21, 31, 41],
39
+ ];
40
+ const obsIndex = ['cell1-1', 'cell1-2', 'cell1-3', 'cell1-4', 'cell2-1', 'cell2-2', 'cell2-3', 'cell2-4'];
41
+ const mergedCellSets = {
42
+ tree: [
43
+ {
44
+ name: 'Cell type',
45
+ children: [
46
+ {
47
+ name: 'T cell',
48
+ set: [['cell1-1', null], ['cell1-3', null], ['cell2-1', null], ['cell2-3', null]],
49
+ },
50
+ {
51
+ name: 'B cell',
52
+ set: [['cell1-2', null], ['cell1-4', null], ['cell2-2', null], ['cell2-4', null]],
53
+ },
54
+ ],
55
+ },
56
+ ],
57
+ };
58
+ const geneSelection = [
59
+ 'Gene 1',
60
+ ];
61
+ const cellSetSelection = [
62
+ ['Cell type', 'T cell'],
63
+ ['Cell type', 'B cell'],
64
+ ];
65
+ const cellSetColor = [
66
+ { set: ['Cell type', 'T cell'], color: [255, 0, 0] },
67
+ { set: ['Cell type', 'B cell'], color: [0, 255, 0] },
68
+ ];
69
+ const featureValueTransform = null;
70
+ const featureValueTransformCoefficient = 1;
71
+ it('stratifyExpressionData: stratify by cell set, then sample set', () => {
72
+ const [result, exprMax] = stratifyExpressionData(sampleEdges, sampleSets, sampleSetSelection, expressionData, obsIndex, mergedCellSets, geneSelection, cellSetSelection, cellSetColor, featureValueTransform, featureValueTransformCoefficient);
73
+ expect(Array.from(result.keys())).toEqual([['Cell type', 'T cell'], ['Cell type', 'B cell']]);
74
+ expect(Array.from(result.get(['Cell type', 'T cell']).keys())).toEqual([['Clinical groups', 'AKI'], ['Clinical groups', 'CKD']]);
75
+ expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('Gene 1').length).toBe(2);
76
+ expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('Gene 1')).toEqual([10, 30]);
77
+ expect(exprMax).toEqual(41);
78
+ const aggregateData = aggregateStratifiedExpressionData(result, geneSelection);
79
+ expect(Array.from(aggregateData.keys())).toEqual([['Cell type', 'T cell'], ['Cell type', 'B cell']]);
80
+ expect(Array.from(aggregateData.get(['Cell type', 'T cell']).keys())).toEqual([['Clinical groups', 'AKI'], ['Clinical groups', 'CKD']]);
81
+ expect(aggregateData.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).length).toBe(2);
82
+ // TODO: add expect statements that check the aggregateData result, once it supports
83
+ // aggregation of expression data from multiple genes into a single array.
84
+ });
85
+ it('stratifyArrays: stratifiy by cell set, then sample set', () => {
86
+ const sampleIdToObsIdsMap = new Map([
87
+ ['donor1', ['cell1-1', 'cell1-2', 'cell1-3', 'cell1-4']],
88
+ ['donor2', ['cell2-1', 'cell2-2', 'cell2-3', 'cell2-4']],
89
+ ]);
90
+ const alignedEmbeddingIndex = ['cell1-1', 'cell1-2', 'cell1-3', 'cell1-4', 'cell2-1', 'cell2-2', 'cell2-3', 'cell2-4'];
91
+ const alignedEmbeddingData = {
92
+ data: [
93
+ new Float32Array([0, 1, 2, 3, 4, 5, 6, 7]),
94
+ new Float32Array([0, 1, 2, 3, 4, 5, 6, 7]),
95
+ ],
96
+ };
97
+ const uint8ExpressionData = [
98
+ new Uint8Array([10, 20, 30, 40, 11, 21, 31, 41]),
99
+ ];
100
+ const result = stratifyArrays(sampleEdges, sampleIdToObsIdsMap, sampleSets, sampleSetSelection, alignedEmbeddingIndex, mergedCellSets, cellSetSelection, {
101
+ obsEmbeddingX: alignedEmbeddingData.data[0],
102
+ obsEmbeddingY: alignedEmbeddingData.data[1],
103
+ // TODO: aggregate and transform expression data if needed prior to passing here
104
+ ...(uint8ExpressionData?.[0] ? { featureValue: uint8ExpressionData?.[0] } : {}),
105
+ });
106
+ expect(Array.from(result.keys())).toEqual([['Cell type', 'T cell'], ['Cell type', 'B cell']]);
107
+ expect(Array.from(result.get(['Cell type', 'T cell']).keys())).toEqual([['Clinical groups', 'AKI'], ['Clinical groups', 'CKD']]);
108
+ expect(Array.from(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).keys())).toEqual(['obsEmbeddingX', 'obsEmbeddingY', 'featureValue', 'obsIndex']);
109
+ expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('featureValue').length).toBe(2);
110
+ expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('featureValue')).toEqual(new Uint8Array([10, 30]));
111
+ expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('obsEmbeddingX').length).toBe(2);
112
+ expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('obsEmbeddingY').length).toBe(2);
113
+ });
114
+ });
115
+ });
@@ -1,8 +1,9 @@
1
1
  export { dataToCellSetsTree } from "./CellSetsZarrLoader.js";
2
2
  export { getCellColors } from "./interpolate-colors.js";
3
- export { treeToCellColorsBySetNames, treeToCellSetColorIndicesBySetNames, treeToSetSizesBySetNames, treeToObjectsBySetNames, treeExportLevelZeroNode, treeExportSet, treeToExpectedCheckedLevel, nodeToLevelDescendantNamePaths, treeToIntersection, treeToUnion, treeToComplement, treeFindNodeByNamePath, treesConflict, nodeTransform, nodeAppendChild, nodePrependChild, nodeInsertChild, filterNode, treeInitialize, initializeCellSetColor, nodeToRenderProps, getCellSetPolygons, treeToMembershipMap, nodeToSet } from "./cell-set-utils.js";
3
+ export { treeToSelectedSetMap, treeToCellColorsBySetNames, treeToCellSetColorIndicesBySetNames, treeToSetSizesBySetNames, treeToObjectsBySetNames, treeToObsIdsBySetNames, treeToObsIndicesBySetNames, treeExportLevelZeroNode, treeExportSet, treeToExpectedCheckedLevel, nodeToLevelDescendantNamePaths, treeToIntersection, treeToUnion, treeToComplement, treeFindNodeByNamePath, treesConflict, nodeTransform, nodeAppendChild, nodePrependChild, nodeInsertChild, filterNode, treeInitialize, initializeCellSetColor, nodeToRenderProps, getCellSetPolygons, treeToMembershipMap, nodeToSet } from "./cell-set-utils.js";
4
4
  export { findLongestCommonPath, filterPathsByExpansionAndSelection, findChangedHierarchy } from "./set-path-utils.js";
5
5
  export { isEqualOrPrefix, tryRenamePath, PATH_SEP, setObsSelection, mergeObsSets, getNextNumberedNodeName, colorArrayToString, callbackOnKeyPress, getLevelTooltipText, pathToKey, getObsInfoFromDataWithinRange } from "./utils.js";
6
6
  export { downloadForUser, handleExportJSON, handleExportTabular, tryUpgradeTreeToLatestSchema, handleImportJSON, handleImportTabular } from "./io.js";
7
7
  export { FILE_EXTENSION_JSON, FILE_EXTENSION_TABULAR, SETS_DATATYPE_OBS, MIME_TYPE_JSON, MIME_TYPE_TABULAR } from "./constants.js";
8
+ export { stratifyArrays, stratifyExpressionData, aggregateStratifiedExpressionData } from "./expr-utils.js";
8
9
  //# sourceMappingURL=index.d.ts.map
package/dist-tsc/index.js CHANGED
@@ -1,7 +1,8 @@
1
- export { treeToCellColorsBySetNames, treeToCellSetColorIndicesBySetNames, treeToSetSizesBySetNames, treeToObjectsBySetNames, treeExportLevelZeroNode, treeExportSet, treeToExpectedCheckedLevel, nodeToLevelDescendantNamePaths, treeToIntersection, treeToUnion, treeToComplement, treeFindNodeByNamePath, treesConflict, nodeTransform, nodeAppendChild, nodePrependChild, nodeInsertChild, filterNode, treeInitialize, initializeCellSetColor, nodeToRenderProps, getCellSetPolygons, treeToMembershipMap, nodeToSet, } from './cell-set-utils.js';
1
+ export { treeToSelectedSetMap, treeToCellColorsBySetNames, treeToCellSetColorIndicesBySetNames, treeToSetSizesBySetNames, treeToObjectsBySetNames, treeToObsIdsBySetNames, treeToObsIndicesBySetNames, treeExportLevelZeroNode, treeExportSet, treeToExpectedCheckedLevel, nodeToLevelDescendantNamePaths, treeToIntersection, treeToUnion, treeToComplement, treeFindNodeByNamePath, treesConflict, nodeTransform, nodeAppendChild, nodePrependChild, nodeInsertChild, filterNode, treeInitialize, initializeCellSetColor, nodeToRenderProps, getCellSetPolygons, treeToMembershipMap, nodeToSet, } from './cell-set-utils.js';
2
2
  export { findLongestCommonPath, filterPathsByExpansionAndSelection, findChangedHierarchy, } from './set-path-utils.js';
3
3
  export { isEqualOrPrefix, tryRenamePath, PATH_SEP, setObsSelection, mergeObsSets, getNextNumberedNodeName, colorArrayToString, callbackOnKeyPress, getLevelTooltipText, pathToKey, getObsInfoFromDataWithinRange, } from './utils.js';
4
4
  export { downloadForUser, handleExportJSON, handleExportTabular, tryUpgradeTreeToLatestSchema, handleImportJSON, handleImportTabular, } from './io.js';
5
5
  export { FILE_EXTENSION_JSON, FILE_EXTENSION_TABULAR, SETS_DATATYPE_OBS, MIME_TYPE_JSON, MIME_TYPE_TABULAR, } from './constants.js';
6
6
  export { dataToCellSetsTree, } from './CellSetsZarrLoader.js';
7
7
  export { getCellColors, } from './interpolate-colors.js';
8
+ export { stratifyArrays, stratifyExpressionData, aggregateStratifiedExpressionData, } from './expr-utils.js';
@@ -1 +1 @@
1
- {"version":3,"file":"interpolate-colors.d.ts","sourceRoot":"","sources":["../src/interpolate-colors.js"],"names":[],"mappings":"AAoFA;;;;;;;;;;;;GAYG;AACH;IAT2B,gBAAgB,EAAhC,OAAO;IACU,YAAY,EAA7B,MAAM,EAAE;IAES,QAAQ,EAAzB,MAAM,EAAE;IAEO,KAAK,EAApB,MAAM;kBAsBhB;AA/EU,+CAAmD;AAkCrD,+CAA2E"}
1
+ {"version":3,"file":"interpolate-colors.d.ts","sourceRoot":"","sources":["../src/interpolate-colors.js"],"names":[],"mappings":"AAoFA;;;;;;;;;;;;GAYG;AACH,sCATG;IAAwB,gBAAgB,EAAhC,KAAK,EAAE;IACU,YAAY,EAA7B,MAAM,EAAE;IAES,QAAQ,EAAzB,MAAM,EAAE;IAEO,KAAK,EAApB,MAAM;CAEd,iBAoBF;AA/EU,+CAAmD;AAkCrD,+CAA2E"}
@@ -1 +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"}
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,oBAER,KAAK,oBACL,KAAK,OAwCf;AAED;;;;;;GAMG;AACH,0FAsBC"}
@@ -1 +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"}
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,uCAHW,OAAO,GACL,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/sets-utils",
3
- "version": "3.4.6",
3
+ "version": "3.4.8",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -25,8 +25,8 @@
25
25
  "lodash-es": "^4.17.21",
26
26
  "tinycolor2": "^1.4.1",
27
27
  "uuid": "^9.0.0",
28
- "@vitessce/utils": "3.4.6",
29
- "@vitessce/schemas": "3.4.6"
28
+ "@vitessce/schemas": "3.4.8",
29
+ "@vitessce/utils": "3.4.8"
30
30
  },
31
31
  "scripts": {
32
32
  "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",