@vitessce/sets-utils 3.4.6 → 3.4.7
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.js +319 -52
- package/dist-tsc/cell-set-utils.d.ts +37 -0
- package/dist-tsc/cell-set-utils.d.ts.map +1 -1
- package/dist-tsc/cell-set-utils.js +93 -0
- package/dist-tsc/expr-utils.d.ts +63 -0
- package/dist-tsc/expr-utils.d.ts.map +1 -0
- package/dist-tsc/expr-utils.js +238 -0
- package/dist-tsc/expr-utils.test.d.ts +2 -0
- package/dist-tsc/expr-utils.test.d.ts.map +1 -0
- package/dist-tsc/expr-utils.test.js +115 -0
- package/dist-tsc/index.d.ts +2 -1
- package/dist-tsc/index.js +2 -1
- package/dist-tsc/interpolate-colors.d.ts.map +1 -1
- package/dist-tsc/set-path-utils.d.ts.map +1 -1
- package/dist-tsc/utils.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/cell-set-utils.js +96 -0
- package/src/expr-utils.js +292 -0
- package/src/expr-utils.test.js +141 -0
- package/src/index.js +8 -0
package/src/cell-set-utils.js
CHANGED
|
@@ -424,6 +424,41 @@ export function treeToCellColorsBySetNames(currTree, selectedNamePaths, cellSetC
|
|
|
424
424
|
return new Map(cellColorsArray);
|
|
425
425
|
}
|
|
426
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Given a tree with state, get the cellIds and cellColors,
|
|
429
|
+
* based on the nodes currently marked as "visible".
|
|
430
|
+
* @param {object} currTree A tree object.
|
|
431
|
+
* @param {array} selectedNamePaths Array of arrays of strings,
|
|
432
|
+
* representing set "paths".
|
|
433
|
+
* @param {object[]} cellSetColor Array of objects with the
|
|
434
|
+
* properties `path` and `color`.
|
|
435
|
+
* @param {string} theme "light" or "dark" for the vitessce theme
|
|
436
|
+
* @returns {array} Tuple of [cellIds, cellColors]
|
|
437
|
+
* where cellIds is an array of strings,
|
|
438
|
+
* and cellColors is an object mapping cellIds to color [r,g,b] arrays.
|
|
439
|
+
*/
|
|
440
|
+
export function treeToSelectedSetMap(currTree, selectedNamePaths) {
|
|
441
|
+
let result = [];
|
|
442
|
+
selectedNamePaths.forEach((setNamePath) => {
|
|
443
|
+
const node = treeFindNodeByNamePath(currTree, setNamePath);
|
|
444
|
+
if (node) {
|
|
445
|
+
const nodeSet = nodeToSet(node);
|
|
446
|
+
result = [
|
|
447
|
+
...result,
|
|
448
|
+
...nodeSet.map(([cellId]) => [
|
|
449
|
+
cellId,
|
|
450
|
+
// TODO: should this be the full path
|
|
451
|
+
// (rather than only the node name)?
|
|
452
|
+
// Or the index of the selected set
|
|
453
|
+
// with respect to the selectedNamePaths array?
|
|
454
|
+
setNamePath,
|
|
455
|
+
]),
|
|
456
|
+
];
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
return new Map(result);
|
|
460
|
+
}
|
|
461
|
+
|
|
427
462
|
/**
|
|
428
463
|
* Given a tree with state, get a mapping from cell ID to cell set color index,
|
|
429
464
|
* based on the nodes currently marked as "visible".
|
|
@@ -585,6 +620,67 @@ export function treeToSetSizesBySetNames(
|
|
|
585
620
|
return sizes;
|
|
586
621
|
}
|
|
587
622
|
|
|
623
|
+
/**
|
|
624
|
+
* Given a tree with state, get the indices of observations
|
|
625
|
+
* contained in each selected obs set.
|
|
626
|
+
* @param {object} currTree A tree object.
|
|
627
|
+
* @param {array} selectedNamePaths Array of arrays of strings,
|
|
628
|
+
* representing set "paths".
|
|
629
|
+
* @returns {object[]} Array of objects
|
|
630
|
+
* with the properties `name`, `size`, `key`,
|
|
631
|
+
* and `color`.
|
|
632
|
+
*/
|
|
633
|
+
export function treeToObsIdsBySetNames(currTree, selectedNamePaths) {
|
|
634
|
+
const indices = [];
|
|
635
|
+
selectedNamePaths.forEach((setNamePath) => {
|
|
636
|
+
const node = treeFindNodeByNamePath(currTree, setNamePath);
|
|
637
|
+
if (node) {
|
|
638
|
+
const nodeSet = nodeToSet(node);
|
|
639
|
+
indices.push({
|
|
640
|
+
key: generateKey(),
|
|
641
|
+
name: node.name,
|
|
642
|
+
path: setNamePath,
|
|
643
|
+
size: nodeSet.length,
|
|
644
|
+
// TODO: handle the case where the ID is in the set but missing
|
|
645
|
+
// from the obsIndexMap
|
|
646
|
+
ids: nodeSet.map(([obsId]) => obsId),
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
return indices;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Given a tree with state, get the indices of observations
|
|
655
|
+
* contained in each selected obs set.
|
|
656
|
+
* @param {object} currTree A tree object.
|
|
657
|
+
* @param {array} selectedNamePaths Array of arrays of strings,
|
|
658
|
+
* representing set "paths".
|
|
659
|
+
* @param {object[]} obsIndex The observation index.
|
|
660
|
+
* @returns {object[]} Array of objects
|
|
661
|
+
* with the properties `name`, `size`, `key`,
|
|
662
|
+
* and `color`.
|
|
663
|
+
*/
|
|
664
|
+
export function treeToObsIndicesBySetNames(currTree, selectedNamePaths, obsIndexMap) {
|
|
665
|
+
const indices = [];
|
|
666
|
+
selectedNamePaths.forEach((setNamePath) => {
|
|
667
|
+
const node = treeFindNodeByNamePath(currTree, setNamePath);
|
|
668
|
+
if (node) {
|
|
669
|
+
const nodeSet = nodeToSet(node);
|
|
670
|
+
indices.push({
|
|
671
|
+
key: generateKey(),
|
|
672
|
+
name: node.name,
|
|
673
|
+
path: setNamePath,
|
|
674
|
+
size: nodeSet.length,
|
|
675
|
+
// TODO: handle the case where the ID is in the set but missing
|
|
676
|
+
// from the obsIndexMap
|
|
677
|
+
indices: nodeSet.map(([obsId]) => obsIndexMap[obsId]),
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
return indices;
|
|
682
|
+
}
|
|
683
|
+
|
|
588
684
|
/**
|
|
589
685
|
* Find and remove a node from the descendants of the current node.
|
|
590
686
|
* @param {object} node A node to search on.
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import { InternMap } from 'internmap';
|
|
3
|
+
import { getValueTransformFunction } from '@vitessce/utils';
|
|
4
|
+
import { isEqual } from 'lodash-es';
|
|
5
|
+
import {
|
|
6
|
+
treeToSelectedSetMap,
|
|
7
|
+
treeToObsIdsBySetNames,
|
|
8
|
+
} from './cell-set-utils.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Input: selections and data.
|
|
12
|
+
* Output: expression data in a hierarchy like
|
|
13
|
+
* [
|
|
14
|
+
* {
|
|
15
|
+
* "selectedCellSet1": {
|
|
16
|
+
* "selectedSampleSet1": [],
|
|
17
|
+
* }
|
|
18
|
+
* },
|
|
19
|
+
* exprMax
|
|
20
|
+
* ]
|
|
21
|
+
* @param {*} sampleEdges
|
|
22
|
+
* @param {*} sampleSets
|
|
23
|
+
* @param {*} sampleSetSelection
|
|
24
|
+
* @param {*} expressionData
|
|
25
|
+
* @param {*} obsIndex
|
|
26
|
+
* @param {*} mergedCellSets
|
|
27
|
+
* @param {*} geneSelection
|
|
28
|
+
* @param {*} cellSetSelection
|
|
29
|
+
* @param {*} cellSetColor
|
|
30
|
+
* @param {*} featureValueTransform
|
|
31
|
+
* @param {*} featureValueTransformCoefficient
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
export function stratifyArrays(
|
|
35
|
+
sampleEdges, sampleIdToObsIdsMap,
|
|
36
|
+
sampleSets, sampleSetSelection,
|
|
37
|
+
obsIndex, mergedCellSets, cellSetSelection,
|
|
38
|
+
arraysToStratify, // Assumed to be sorted with respect to the obsIndex.
|
|
39
|
+
) {
|
|
40
|
+
const result = new InternMap([], JSON.stringify);
|
|
41
|
+
|
|
42
|
+
const hasSampleSetSelection = Array.isArray(sampleSetSelection) && sampleSetSelection.length > 0;
|
|
43
|
+
const hasCellSetSelection = Array.isArray(cellSetSelection) && cellSetSelection.length > 0;
|
|
44
|
+
|
|
45
|
+
const sampleSetKeys = hasSampleSetSelection ? sampleSetSelection : [null];
|
|
46
|
+
const cellSetKeys = hasCellSetSelection ? cellSetSelection : [null];
|
|
47
|
+
const arrKeys = Object.keys(arraysToStratify);
|
|
48
|
+
|
|
49
|
+
if (arrKeys.includes('obsIndex') || arrKeys.includes('i')) {
|
|
50
|
+
throw new Error('The keys "obsIndex" and "i" are reserved for internal use.');
|
|
51
|
+
}
|
|
52
|
+
if (Object.values(arraysToStratify).some(arr => arr.length !== obsIndex.length)) {
|
|
53
|
+
throw new Error('All arrays must have the same length as the obsIndex.');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const sampleSetInfo = sampleSets && sampleSetSelection
|
|
57
|
+
? treeToObsIdsBySetNames(sampleSets, sampleSetSelection)
|
|
58
|
+
: null;
|
|
59
|
+
const cellSetInfo = mergedCellSets && cellSetSelection
|
|
60
|
+
? treeToObsIdsBySetNames(mergedCellSets, cellSetSelection)
|
|
61
|
+
: null;
|
|
62
|
+
|
|
63
|
+
// First level: cell set
|
|
64
|
+
cellSetKeys.forEach((cellSetKey) => {
|
|
65
|
+
result.set(cellSetKey, new InternMap([], JSON.stringify));
|
|
66
|
+
// Second level: sample set
|
|
67
|
+
sampleSetKeys.forEach((sampleSetKey) => {
|
|
68
|
+
result.get(cellSetKey).set(sampleSetKey, new InternMap([], JSON.stringify));
|
|
69
|
+
// Third level: array
|
|
70
|
+
|
|
71
|
+
// Handle cellSetKey and sampleSetKey being null.
|
|
72
|
+
const sampleIdsInSampleSet = (sampleSetInfo && sampleSetKey
|
|
73
|
+
? sampleSetInfo?.find(n => isEqual(n.path, sampleSetKey))
|
|
74
|
+
: null
|
|
75
|
+
)?.ids;
|
|
76
|
+
// The sampleSets contain sampleIds. We need to get the obsIds
|
|
77
|
+
// corresponding to each sampleId.
|
|
78
|
+
const obsIdsInSampleSet = sampleIdsInSampleSet
|
|
79
|
+
? sampleIdsInSampleSet.flatMap(sampleId => sampleIdToObsIdsMap?.get(sampleId) || [])
|
|
80
|
+
: null;
|
|
81
|
+
const obsIdsInCellSet = (cellSetInfo && cellSetKey
|
|
82
|
+
? cellSetInfo?.find(n => isEqual(n.path, cellSetKey))
|
|
83
|
+
: null
|
|
84
|
+
)?.ids;
|
|
85
|
+
|
|
86
|
+
// Handle cases where obsIdsInSampleSet and/or obsIdsInCellSet is null.
|
|
87
|
+
let subObsIndex = obsIndex;
|
|
88
|
+
if (obsIdsInSampleSet && obsIdsInCellSet) {
|
|
89
|
+
subObsIndex = Array.from(
|
|
90
|
+
(new Set(obsIdsInSampleSet)).intersection(new Set(obsIdsInCellSet)),
|
|
91
|
+
);
|
|
92
|
+
} else if (obsIdsInSampleSet) {
|
|
93
|
+
subObsIndex = obsIdsInSampleSet;
|
|
94
|
+
} else if (obsIdsInCellSet) {
|
|
95
|
+
subObsIndex = obsIdsInCellSet;
|
|
96
|
+
}
|
|
97
|
+
const arrLen = subObsIndex.length;
|
|
98
|
+
|
|
99
|
+
arrKeys.forEach((arrKey) => {
|
|
100
|
+
// Instantiate a new array of the appropriate size.
|
|
101
|
+
const newArr = new arraysToStratify[arrKey].constructor(arrLen);
|
|
102
|
+
result.get(cellSetKey).get(sampleSetKey).set(arrKey, newArr);
|
|
103
|
+
});
|
|
104
|
+
result.get(cellSetKey).get(sampleSetKey).set('obsIndex', subObsIndex);
|
|
105
|
+
// We initialize an incrementer to keep track of
|
|
106
|
+
// the next index to use for insertion into the array.
|
|
107
|
+
result.get(cellSetKey).get(sampleSetKey).set('i', 0);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const sampleIdToSetMap = sampleSets && sampleSetSelection
|
|
112
|
+
? treeToSelectedSetMap(sampleSets, sampleSetSelection)
|
|
113
|
+
: null;
|
|
114
|
+
const cellIdToSetMap = mergedCellSets && cellSetSelection
|
|
115
|
+
? treeToSelectedSetMap(mergedCellSets, cellSetSelection)
|
|
116
|
+
: null;
|
|
117
|
+
|
|
118
|
+
// Iterate over every observation.
|
|
119
|
+
// Insert the ID and all corresponding values into
|
|
120
|
+
// the appropriate arrays within the InternMap.
|
|
121
|
+
for (let i = 0; i < obsIndex.length; i += 1) {
|
|
122
|
+
const obsId = obsIndex[i];
|
|
123
|
+
|
|
124
|
+
const cellSet = cellIdToSetMap?.get(obsId) || null;
|
|
125
|
+
const sampleId = sampleEdges?.get(obsId);
|
|
126
|
+
const sampleSet = sampleId ? (sampleIdToSetMap?.get(sampleId) || null) : null;
|
|
127
|
+
|
|
128
|
+
if ((hasSampleSetSelection && !sampleSet) || (hasCellSetSelection && !cellSet)) {
|
|
129
|
+
// Skip this sample if it is not in the selected sample set.
|
|
130
|
+
// eslint-disable-next-line no-continue
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const insertionIndex = result.get(cellSet).get(sampleSet).get('i');
|
|
135
|
+
|
|
136
|
+
// eslint-disable-next-line no-loop-func
|
|
137
|
+
arrKeys.forEach((arrKey) => {
|
|
138
|
+
const value = arraysToStratify[arrKey][i];
|
|
139
|
+
|
|
140
|
+
result
|
|
141
|
+
.get(cellSet)
|
|
142
|
+
.get(sampleSet)
|
|
143
|
+
.get(arrKey)[insertionIndex] = value;
|
|
144
|
+
});
|
|
145
|
+
result.get(cellSet).get(sampleSet).set('i', insertionIndex + 1);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
cellSetKeys.forEach((cellSetKey) => {
|
|
149
|
+
// Second level: sample set
|
|
150
|
+
sampleSetKeys.forEach((sampleSetKey) => {
|
|
151
|
+
const finalInsertionIndex = result.get(cellSetKey).get(sampleSetKey).get('i');
|
|
152
|
+
if (finalInsertionIndex !== result.get(cellSetKey).get(sampleSetKey).get('obsIndex').length) {
|
|
153
|
+
console.warn('The final insertion index is lower than expected.');
|
|
154
|
+
}
|
|
155
|
+
result.get(cellSetKey).get(sampleSetKey).delete('i');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Input: selections and data.
|
|
164
|
+
* Output: expression data in a hierarchy like
|
|
165
|
+
* [
|
|
166
|
+
* {
|
|
167
|
+
* "selectedCellSet1": {
|
|
168
|
+
* "selectedSampleSet1": [],
|
|
169
|
+
* }
|
|
170
|
+
* },
|
|
171
|
+
* exprMax
|
|
172
|
+
* ]
|
|
173
|
+
* @param {*} sampleEdges
|
|
174
|
+
* @param {*} sampleSets
|
|
175
|
+
* @param {*} sampleSetSelection
|
|
176
|
+
* @param {*} expressionData
|
|
177
|
+
* @param {*} obsIndex
|
|
178
|
+
* @param {*} mergedCellSets
|
|
179
|
+
* @param {*} geneSelection
|
|
180
|
+
* @param {*} cellSetSelection
|
|
181
|
+
* @param {*} cellSetColor
|
|
182
|
+
* @param {*} featureValueTransform
|
|
183
|
+
* @param {*} featureValueTransformCoefficient
|
|
184
|
+
* @returns
|
|
185
|
+
*/
|
|
186
|
+
export function stratifyExpressionData(
|
|
187
|
+
sampleEdges, sampleSets, sampleSetSelection,
|
|
188
|
+
expressionData, obsIndex, mergedCellSets,
|
|
189
|
+
geneSelection, cellSetSelection,
|
|
190
|
+
cellSetColor, // TODO: remove this parameter
|
|
191
|
+
featureValueTransform, featureValueTransformCoefficient,
|
|
192
|
+
) {
|
|
193
|
+
const result = new InternMap([], JSON.stringify);
|
|
194
|
+
|
|
195
|
+
const hasSampleSetSelection = Array.isArray(sampleSetSelection) && sampleSetSelection.length > 0;
|
|
196
|
+
const hasCellSetSelection = Array.isArray(cellSetSelection) && cellSetSelection.length > 0;
|
|
197
|
+
const hasGeneSelection = Array.isArray(geneSelection) && geneSelection.length > 0;
|
|
198
|
+
|
|
199
|
+
const sampleSetKeys = hasSampleSetSelection ? sampleSetSelection : [null];
|
|
200
|
+
const cellSetKeys = hasCellSetSelection ? cellSetSelection : [null];
|
|
201
|
+
const geneKeys = hasGeneSelection ? geneSelection : [null];
|
|
202
|
+
|
|
203
|
+
// TODO: return a flat array with { cellSet, sampleSet, gene } objects.
|
|
204
|
+
// Then, use d3.group to create nested InternMaps.
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
// First level: cell set
|
|
208
|
+
cellSetKeys.forEach((cellSetKey) => {
|
|
209
|
+
result.set(cellSetKey, new InternMap([], JSON.stringify));
|
|
210
|
+
// Second level: sample set
|
|
211
|
+
sampleSetKeys.forEach((sampleSetKey) => {
|
|
212
|
+
result.get(cellSetKey).set(sampleSetKey, new InternMap([], JSON.stringify));
|
|
213
|
+
// Third level: gene
|
|
214
|
+
geneKeys.forEach((geneKey) => {
|
|
215
|
+
result.get(cellSetKey).get(sampleSetKey).set(geneKey, []);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
if (mergedCellSets && cellSetSelection
|
|
222
|
+
&& geneSelection && geneSelection.length >= 1
|
|
223
|
+
&& expressionData
|
|
224
|
+
) {
|
|
225
|
+
const sampleIdToSetMap = sampleSets && sampleSetSelection
|
|
226
|
+
? treeToSelectedSetMap(sampleSets, sampleSetSelection)
|
|
227
|
+
: null;
|
|
228
|
+
const cellIdToSetMap = treeToSelectedSetMap(mergedCellSets, cellSetSelection);
|
|
229
|
+
|
|
230
|
+
let exprMax = -Infinity;
|
|
231
|
+
|
|
232
|
+
for (let i = 0; i < obsIndex.length; i += 1) {
|
|
233
|
+
const obsId = obsIndex[i];
|
|
234
|
+
|
|
235
|
+
const cellSet = cellIdToSetMap?.get(obsId);
|
|
236
|
+
const sampleId = sampleEdges?.get(obsId);
|
|
237
|
+
const sampleSet = sampleId ? sampleIdToSetMap?.get(sampleId) : null;
|
|
238
|
+
|
|
239
|
+
if (hasSampleSetSelection && !sampleSet) {
|
|
240
|
+
// Skip this sample if it is not in the selected sample set.
|
|
241
|
+
// eslint-disable-next-line no-continue
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// eslint-disable-next-line no-loop-func
|
|
246
|
+
geneKeys.forEach((geneKey, geneI) => {
|
|
247
|
+
const value = expressionData[geneI][i];
|
|
248
|
+
const transformFunction = getValueTransformFunction(
|
|
249
|
+
featureValueTransform, featureValueTransformCoefficient,
|
|
250
|
+
);
|
|
251
|
+
const transformedValue = transformFunction(value);
|
|
252
|
+
exprMax = Math.max(transformedValue, exprMax);
|
|
253
|
+
|
|
254
|
+
result
|
|
255
|
+
.get(cellSet)
|
|
256
|
+
?.get(sampleSet)
|
|
257
|
+
?.get(geneKey)
|
|
258
|
+
?.push(transformedValue);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
return [result, exprMax];
|
|
262
|
+
}
|
|
263
|
+
return [null, null];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Supports three-level stratified input
|
|
268
|
+
* (cell set, sample set, gene).
|
|
269
|
+
* Returns two-level stratified output (cell set, sample set).
|
|
270
|
+
* Aggregate stratified expression data so that there is
|
|
271
|
+
* a single value for each (cell set, sample set) tuple.
|
|
272
|
+
* I.e., aggregate along the gene axis.
|
|
273
|
+
* @param {*} stratifiedResult
|
|
274
|
+
* @param {*} geneSelection
|
|
275
|
+
* @returns
|
|
276
|
+
*/
|
|
277
|
+
export function aggregateStratifiedExpressionData(
|
|
278
|
+
stratifiedResult, geneSelection,
|
|
279
|
+
) {
|
|
280
|
+
const result = new InternMap([], JSON.stringify);
|
|
281
|
+
([...stratifiedResult.keys()]).forEach((cellSetKey) => {
|
|
282
|
+
result.set(cellSetKey, new InternMap([], JSON.stringify));
|
|
283
|
+
([...stratifiedResult.get(cellSetKey).keys()]).forEach((sampleSetKey) => {
|
|
284
|
+
// For now, we just take the first gene.
|
|
285
|
+
// TODO: support multiple genes via signature score method.
|
|
286
|
+
const values = stratifiedResult.get(cellSetKey).get(sampleSetKey).get(geneSelection[0]);
|
|
287
|
+
result.get(cellSetKey).set(sampleSetKey, values);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
stratifyExpressionData,
|
|
4
|
+
aggregateStratifiedExpressionData,
|
|
5
|
+
stratifyArrays,
|
|
6
|
+
} from './expr-utils.js';
|
|
7
|
+
|
|
8
|
+
describe('Utility functions for processing expression data', () => {
|
|
9
|
+
describe('stratifyExpressionData and stratifyArrays functions', () => {
|
|
10
|
+
const sampleEdges = new Map([
|
|
11
|
+
['cell1-1', 'donor1'],
|
|
12
|
+
['cell1-2', 'donor1'],
|
|
13
|
+
['cell1-3', 'donor1'],
|
|
14
|
+
['cell1-4', 'donor1'],
|
|
15
|
+
|
|
16
|
+
['cell2-1', 'donor2'],
|
|
17
|
+
['cell2-2', 'donor2'],
|
|
18
|
+
['cell2-3', 'donor2'],
|
|
19
|
+
['cell2-4', 'donor2'],
|
|
20
|
+
]);
|
|
21
|
+
const sampleSets = {
|
|
22
|
+
tree: [
|
|
23
|
+
{
|
|
24
|
+
name: 'Clinical groups',
|
|
25
|
+
children: [
|
|
26
|
+
{
|
|
27
|
+
name: 'AKI',
|
|
28
|
+
set: [['donor1', null]],
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'CKD',
|
|
32
|
+
set: [['donor2', null]],
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
const sampleSetSelection = [
|
|
39
|
+
['Clinical groups', 'AKI'],
|
|
40
|
+
['Clinical groups', 'CKD'],
|
|
41
|
+
];
|
|
42
|
+
const expressionData = [
|
|
43
|
+
// Gene 1
|
|
44
|
+
[10, 20, 30, 40, 11, 21, 31, 41],
|
|
45
|
+
];
|
|
46
|
+
const obsIndex = ['cell1-1', 'cell1-2', 'cell1-3', 'cell1-4', 'cell2-1', 'cell2-2', 'cell2-3', 'cell2-4'];
|
|
47
|
+
const mergedCellSets = {
|
|
48
|
+
tree: [
|
|
49
|
+
{
|
|
50
|
+
name: 'Cell type',
|
|
51
|
+
children: [
|
|
52
|
+
{
|
|
53
|
+
name: 'T cell',
|
|
54
|
+
set: [['cell1-1', null], ['cell1-3', null], ['cell2-1', null], ['cell2-3', null]],
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'B cell',
|
|
58
|
+
set: [['cell1-2', null], ['cell1-4', null], ['cell2-2', null], ['cell2-4', null]],
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
};
|
|
64
|
+
const geneSelection = [
|
|
65
|
+
'Gene 1',
|
|
66
|
+
];
|
|
67
|
+
const cellSetSelection = [
|
|
68
|
+
['Cell type', 'T cell'],
|
|
69
|
+
['Cell type', 'B cell'],
|
|
70
|
+
];
|
|
71
|
+
const cellSetColor = [
|
|
72
|
+
{ set: ['Cell type', 'T cell'], color: [255, 0, 0] },
|
|
73
|
+
{ set: ['Cell type', 'B cell'], color: [0, 255, 0] },
|
|
74
|
+
];
|
|
75
|
+
const featureValueTransform = null;
|
|
76
|
+
const featureValueTransformCoefficient = 1;
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
it('stratifyExpressionData: stratify by cell set, then sample set', () => {
|
|
80
|
+
const [result, exprMax] = stratifyExpressionData(
|
|
81
|
+
sampleEdges, sampleSets, sampleSetSelection,
|
|
82
|
+
expressionData, obsIndex, mergedCellSets,
|
|
83
|
+
geneSelection, cellSetSelection, cellSetColor,
|
|
84
|
+
featureValueTransform, featureValueTransformCoefficient,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect([...result.keys()]).toEqual([['Cell type', 'T cell'], ['Cell type', 'B cell']]);
|
|
88
|
+
expect([...result.get(['Cell type', 'T cell']).keys()]).toEqual([['Clinical groups', 'AKI'], ['Clinical groups', 'CKD']]);
|
|
89
|
+
expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('Gene 1').length).toBe(2);
|
|
90
|
+
expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('Gene 1')).toEqual([10, 30]);
|
|
91
|
+
expect(exprMax).toEqual(41);
|
|
92
|
+
|
|
93
|
+
const aggregateData = aggregateStratifiedExpressionData(
|
|
94
|
+
result, geneSelection,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect([...aggregateData.keys()]).toEqual([['Cell type', 'T cell'], ['Cell type', 'B cell']]);
|
|
98
|
+
expect([...aggregateData.get(['Cell type', 'T cell']).keys()]).toEqual([['Clinical groups', 'AKI'], ['Clinical groups', 'CKD']]);
|
|
99
|
+
expect(aggregateData.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).length).toBe(2);
|
|
100
|
+
|
|
101
|
+
// TODO: add expect statements that check the aggregateData result, once it supports
|
|
102
|
+
// aggregation of expression data from multiple genes into a single array.
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('stratifyArrays: stratifiy by cell set, then sample set', () => {
|
|
106
|
+
const sampleIdToObsIdsMap = new Map([
|
|
107
|
+
['donor1', ['cell1-1', 'cell1-2', 'cell1-3', 'cell1-4']],
|
|
108
|
+
['donor2', ['cell2-1', 'cell2-2', 'cell2-3', 'cell2-4']],
|
|
109
|
+
]);
|
|
110
|
+
const alignedEmbeddingIndex = ['cell1-1', 'cell1-2', 'cell1-3', 'cell1-4', 'cell2-1', 'cell2-2', 'cell2-3', 'cell2-4'];
|
|
111
|
+
const alignedEmbeddingData = {
|
|
112
|
+
data: [
|
|
113
|
+
new Float32Array([0, 1, 2, 3, 4, 5, 6, 7]),
|
|
114
|
+
new Float32Array([0, 1, 2, 3, 4, 5, 6, 7]),
|
|
115
|
+
],
|
|
116
|
+
};
|
|
117
|
+
const uint8ExpressionData = [
|
|
118
|
+
new Uint8Array([10, 20, 30, 40, 11, 21, 31, 41]),
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
const result = stratifyArrays(
|
|
122
|
+
sampleEdges, sampleIdToObsIdsMap,
|
|
123
|
+
sampleSets, sampleSetSelection,
|
|
124
|
+
alignedEmbeddingIndex, mergedCellSets, cellSetSelection, {
|
|
125
|
+
obsEmbeddingX: alignedEmbeddingData.data[0],
|
|
126
|
+
obsEmbeddingY: alignedEmbeddingData.data[1],
|
|
127
|
+
// TODO: aggregate and transform expression data if needed prior to passing here
|
|
128
|
+
...(uint8ExpressionData?.[0] ? { featureValue: uint8ExpressionData?.[0] } : {}),
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
expect([...result.keys()]).toEqual([['Cell type', 'T cell'], ['Cell type', 'B cell']]);
|
|
133
|
+
expect([...result.get(['Cell type', 'T cell']).keys()]).toEqual([['Clinical groups', 'AKI'], ['Clinical groups', 'CKD']]);
|
|
134
|
+
expect([...result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).keys()]).toEqual(['obsEmbeddingX', 'obsEmbeddingY', 'featureValue', 'obsIndex']);
|
|
135
|
+
expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('featureValue').length).toBe(2);
|
|
136
|
+
expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('featureValue')).toEqual(new Uint8Array([10, 30]));
|
|
137
|
+
expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('obsEmbeddingX').length).toBe(2);
|
|
138
|
+
expect(result.get(['Cell type', 'T cell']).get(['Clinical groups', 'AKI']).get('obsEmbeddingY').length).toBe(2);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
});
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export {
|
|
2
|
+
treeToSelectedSetMap,
|
|
2
3
|
treeToCellColorsBySetNames,
|
|
3
4
|
treeToCellSetColorIndicesBySetNames,
|
|
4
5
|
treeToSetSizesBySetNames,
|
|
5
6
|
treeToObjectsBySetNames,
|
|
7
|
+
treeToObsIdsBySetNames,
|
|
8
|
+
treeToObsIndicesBySetNames,
|
|
6
9
|
treeExportLevelZeroNode,
|
|
7
10
|
treeExportSet,
|
|
8
11
|
treeToExpectedCheckedLevel,
|
|
@@ -63,3 +66,8 @@ export {
|
|
|
63
66
|
export {
|
|
64
67
|
getCellColors,
|
|
65
68
|
} from './interpolate-colors.js';
|
|
69
|
+
export {
|
|
70
|
+
stratifyArrays,
|
|
71
|
+
stratifyExpressionData,
|
|
72
|
+
aggregateStratifiedExpressionData,
|
|
73
|
+
} from './expr-utils.js';
|