@vitessce/sets-utils 2.0.1
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/LICENSE +21 -0
- package/dist/CellSetsZarrLoader.js +87 -0
- package/dist/CellSetsZarrLoader.test.js +148 -0
- package/dist/cell-set-utils.js +617 -0
- package/dist/cell-set-utils.test.fixtures.js +68 -0
- package/dist/cell-set-utils.test.js +112 -0
- package/dist/constants.js +28 -0
- package/dist/index.js +5 -0
- package/dist/io.js +164 -0
- package/dist/io.test.js +127 -0
- package/dist/utils.js +132 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { InternMap } from 'internmap';
|
|
2
|
+
import { treeInitialize, nodeAppendChild, } from './cell-set-utils';
|
|
3
|
+
import { SETS_DATATYPE_OBS, } from './constants';
|
|
4
|
+
export function dataToCellSetsTree(data, options) {
|
|
5
|
+
const [cellNames, cellSets, cellSetScores] = data;
|
|
6
|
+
const cellSetsTree = treeInitialize(SETS_DATATYPE_OBS);
|
|
7
|
+
cellSets.forEach((cellSetIds, j) => {
|
|
8
|
+
const { name } = options[j];
|
|
9
|
+
let levelZeroNode = {
|
|
10
|
+
name,
|
|
11
|
+
children: [],
|
|
12
|
+
};
|
|
13
|
+
if (cellSetIds.length > 0 && Array.isArray(cellSetIds[0])) {
|
|
14
|
+
// Multi-level case.
|
|
15
|
+
// TODO: throw a warning if the levels are not in order coarser->finer.
|
|
16
|
+
const levelSets = new InternMap([], JSON.stringify);
|
|
17
|
+
cellNames.forEach((id, i) => {
|
|
18
|
+
const classes = cellSetIds.map(col => col[i]);
|
|
19
|
+
if (levelSets.has(classes)) {
|
|
20
|
+
levelSets.get(classes).push([id, null]);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
levelSets.set(classes, [[id, null]]);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const levels = Array.from(levelSets.keys());
|
|
27
|
+
const getNextLevelNames = (levelSuffixes) => {
|
|
28
|
+
const nextLevelNames = Array.from(new Set(levelSuffixes.map(l => l[0])));
|
|
29
|
+
return nextLevelNames.sort((a, b) => a.localeCompare(b));
|
|
30
|
+
};
|
|
31
|
+
// Recursive function to create nodes.
|
|
32
|
+
const getNode = (parentLevelPrefixes, currLevelName, childLevelSuffixes) => {
|
|
33
|
+
const isLeaf = childLevelSuffixes.length === 0;
|
|
34
|
+
const resultNode = {
|
|
35
|
+
name: currLevelName,
|
|
36
|
+
};
|
|
37
|
+
if (isLeaf) {
|
|
38
|
+
// Base case: this is a leaf node.
|
|
39
|
+
resultNode.set = levelSets.get([...parentLevelPrefixes, currLevelName]);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// Are the remaining suffices redundant?
|
|
43
|
+
// Consider ["Parent", "Child", "Child"]
|
|
44
|
+
// where parentLevelPrefixes is ["Parent"] and currLevelName is "Child".
|
|
45
|
+
const shouldBeLeaf = (childLevelSuffixes.length === 1
|
|
46
|
+
&& currLevelName === childLevelSuffixes[0][childLevelSuffixes[0].length - 1]);
|
|
47
|
+
if (shouldBeLeaf) {
|
|
48
|
+
resultNode.set = levelSets.get([...parentLevelPrefixes, currLevelName, ...childLevelSuffixes[0]]);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// Recursion, run getNode() on each of the unique names at the next level.
|
|
52
|
+
const nextLevelNames = getNextLevelNames(childLevelSuffixes);
|
|
53
|
+
resultNode.children = nextLevelNames
|
|
54
|
+
.map(nextLevelName => getNode([...parentLevelPrefixes, currLevelName], nextLevelName, childLevelSuffixes
|
|
55
|
+
.filter(l => l[0] === nextLevelName)
|
|
56
|
+
.map(l => l.slice(1))
|
|
57
|
+
.filter(v => v.length > 0)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return resultNode;
|
|
61
|
+
};
|
|
62
|
+
// Start the recursion.
|
|
63
|
+
const levelOneNodes = getNextLevelNames(levels)
|
|
64
|
+
.map(levelOneName => getNode([], levelOneName, levels.filter(l => l[0] === levelOneName).map(l => l.slice(1))));
|
|
65
|
+
levelZeroNode.children = levelOneNodes;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// Single-level case.
|
|
69
|
+
// Check for the optional corresponding confidence score column name.
|
|
70
|
+
const uniqueCellSetIds = Array.from(new Set(cellSetIds)).sort();
|
|
71
|
+
const clusters = {};
|
|
72
|
+
// eslint-disable-next-line no-return-assign
|
|
73
|
+
uniqueCellSetIds.forEach(id => (clusters[id] = { name: id, set: [] }));
|
|
74
|
+
if (cellSetScores[j]) {
|
|
75
|
+
cellSetIds.forEach((id, i) => clusters[id].set.push([cellNames[i], cellSetScores[j][i]]));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
cellSetIds.forEach((id, i) => clusters[id].set.push([cellNames[i], null]));
|
|
79
|
+
}
|
|
80
|
+
Object.values(clusters).forEach(
|
|
81
|
+
// eslint-disable-next-line no-return-assign
|
|
82
|
+
cluster => (levelZeroNode = nodeAppendChild(levelZeroNode, cluster)));
|
|
83
|
+
}
|
|
84
|
+
cellSetsTree.tree.push(levelZeroNode);
|
|
85
|
+
});
|
|
86
|
+
return cellSetsTree;
|
|
87
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { dataToCellSetsTree } from './CellSetsZarrLoader';
|
|
2
|
+
describe('loaders/CellSetsZarrLoader', () => {
|
|
3
|
+
it('dataToCellSetsTree constructs a hierarchy from an array of columns', async () => {
|
|
4
|
+
const data = [
|
|
5
|
+
['cell_1', 'cell_2', 'cell_3', 'cell_4'],
|
|
6
|
+
[
|
|
7
|
+
[
|
|
8
|
+
['Immune', 'Immune', 'Immune', 'Neuron'],
|
|
9
|
+
['B cell', 'B cell', 'B cell', 'Excitatory neuron'],
|
|
10
|
+
['CD19+', 'CD19-', 'CD19-', 'Retinal bipolar neuron'],
|
|
11
|
+
],
|
|
12
|
+
],
|
|
13
|
+
[undefined],
|
|
14
|
+
];
|
|
15
|
+
const options = [
|
|
16
|
+
{ name: 'Subclass Levels', path: ['obs/L1', 'obs/L2', 'obs/L3'] },
|
|
17
|
+
];
|
|
18
|
+
const tree = dataToCellSetsTree(data, options);
|
|
19
|
+
expect(tree).toEqual({
|
|
20
|
+
version: '0.1.3',
|
|
21
|
+
datatype: 'obs',
|
|
22
|
+
tree: [
|
|
23
|
+
{
|
|
24
|
+
name: 'Subclass Levels',
|
|
25
|
+
children: [
|
|
26
|
+
{
|
|
27
|
+
name: 'Immune',
|
|
28
|
+
children: [
|
|
29
|
+
{
|
|
30
|
+
name: 'B cell',
|
|
31
|
+
children: [
|
|
32
|
+
{
|
|
33
|
+
name: 'CD19-',
|
|
34
|
+
set: [
|
|
35
|
+
['cell_2', null],
|
|
36
|
+
['cell_3', null],
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'CD19+',
|
|
41
|
+
set: [
|
|
42
|
+
['cell_1', null],
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'Neuron',
|
|
51
|
+
children: [
|
|
52
|
+
{
|
|
53
|
+
name: 'Excitatory neuron',
|
|
54
|
+
children: [
|
|
55
|
+
{
|
|
56
|
+
name: 'Retinal bipolar neuron',
|
|
57
|
+
set: [
|
|
58
|
+
['cell_4', null],
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
it('dataToCellSetsTree creates a flat tree from a single column', async () => {
|
|
71
|
+
const data = [
|
|
72
|
+
['cell_1', 'cell_2', 'cell_3', 'cell_4'],
|
|
73
|
+
[
|
|
74
|
+
['Immune', 'Immune', 'Immune', 'Neuron'],
|
|
75
|
+
],
|
|
76
|
+
[undefined],
|
|
77
|
+
];
|
|
78
|
+
const options = [
|
|
79
|
+
{ name: 'Subclass Level 1', path: 'obs/L1' },
|
|
80
|
+
];
|
|
81
|
+
const tree = dataToCellSetsTree(data, options);
|
|
82
|
+
expect(tree).toEqual({
|
|
83
|
+
version: '0.1.3',
|
|
84
|
+
datatype: 'obs',
|
|
85
|
+
tree: [
|
|
86
|
+
{
|
|
87
|
+
name: 'Subclass Level 1',
|
|
88
|
+
children: [
|
|
89
|
+
{
|
|
90
|
+
name: 'Immune',
|
|
91
|
+
set: [
|
|
92
|
+
['cell_1', null],
|
|
93
|
+
['cell_2', null],
|
|
94
|
+
['cell_3', null],
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'Neuron',
|
|
99
|
+
set: [
|
|
100
|
+
['cell_4', null],
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
it('dataToCellSetsTree creates a flat tree from a single column with scores', async () => {
|
|
109
|
+
const data = [
|
|
110
|
+
['cell_1', 'cell_2', 'cell_3', 'cell_4'],
|
|
111
|
+
[
|
|
112
|
+
['Immune', 'Immune', 'Immune', 'Neuron'],
|
|
113
|
+
],
|
|
114
|
+
[
|
|
115
|
+
[0.25, 0.5, 0.6, 0.1],
|
|
116
|
+
],
|
|
117
|
+
];
|
|
118
|
+
const options = [
|
|
119
|
+
{ name: 'Subclass Level 1', path: 'obs/L1' },
|
|
120
|
+
];
|
|
121
|
+
const tree = dataToCellSetsTree(data, options);
|
|
122
|
+
expect(tree).toEqual({
|
|
123
|
+
version: '0.1.3',
|
|
124
|
+
datatype: 'obs',
|
|
125
|
+
tree: [
|
|
126
|
+
{
|
|
127
|
+
name: 'Subclass Level 1',
|
|
128
|
+
children: [
|
|
129
|
+
{
|
|
130
|
+
name: 'Immune',
|
|
131
|
+
set: [
|
|
132
|
+
['cell_1', 0.25],
|
|
133
|
+
['cell_2', 0.5],
|
|
134
|
+
['cell_3', 0.6],
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: 'Neuron',
|
|
139
|
+
set: [
|
|
140
|
+
['cell_4', 0.1],
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|