@vitessce/sets-utils 2.0.3-beta.0 → 2.0.3

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.
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/sets-utils",
3
- "version": "2.0.3-beta.0",
3
+ "version": "2.0.3",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -8,9 +8,10 @@
8
8
  "url": "git+https://github.com/vitessce/vitessce.git"
9
9
  },
10
10
  "license": "MIT",
11
- "main": "dist/index.js",
11
+ "main": "dist/index.mjs",
12
12
  "files": [
13
- "dist"
13
+ "dist",
14
+ "src"
14
15
  ],
15
16
  "dependencies": {
16
17
  "@turf/centroid": "^6.5.0",
@@ -23,12 +24,11 @@
23
24
  "lodash": "^4.17.21",
24
25
  "tinycolor2": "^1.4.1",
25
26
  "uuid": "^3.3.2",
26
- "@vitessce/utils": "2.0.3-beta.0",
27
- "@vitessce/vit-s": "2.0.3-beta.0"
27
+ "@vitessce/utils": "2.0.3",
28
+ "@vitessce/vit-s": "2.0.3"
28
29
  },
29
30
  "scripts": {
30
- "start": "tsc --watch",
31
- "build": "tsc",
31
+ "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
32
32
  "test": "pnpm exec vitest --run -r ../../../ --dir ."
33
33
  }
34
34
  }
@@ -0,0 +1,111 @@
1
+ import { InternMap } from 'internmap';
2
+ import {
3
+ treeInitialize,
4
+ nodeAppendChild,
5
+ } from './cell-set-utils';
6
+ import {
7
+ SETS_DATATYPE_OBS,
8
+ } from './constants';
9
+
10
+ export function dataToCellSetsTree(data, options) {
11
+ // obsIndex is an array of all cell IDs, for the purposes of set complement operations only.
12
+ // cellNames is per-cellSets arrays of cell IDs.
13
+ const [cellNames, cellSets, cellSetScores] = data;
14
+ const cellSetsTree = treeInitialize(SETS_DATATYPE_OBS);
15
+ cellSets.forEach((cellSetIds, j) => {
16
+ const { name } = options[j];
17
+ let levelZeroNode = {
18
+ name,
19
+ children: [],
20
+ };
21
+ if (cellSetIds.length > 0 && Array.isArray(cellSetIds[0])) {
22
+ // Multi-level case.
23
+ // TODO: throw a warning if the levels are not in order coarser->finer.
24
+ const levelSets = new InternMap([], JSON.stringify);
25
+
26
+ cellNames[j].forEach((id, i) => {
27
+ const classes = cellSetIds.map(col => col[i]);
28
+ if (levelSets.has(classes)) {
29
+ levelSets.get(classes).push([id, null]);
30
+ } else {
31
+ levelSets.set(classes, [[id, null]]);
32
+ }
33
+ });
34
+
35
+ const levels = Array.from(levelSets.keys());
36
+
37
+ const getNextLevelNames = (levelSuffixes) => {
38
+ const nextLevelNames = Array.from(new Set(levelSuffixes.map(l => l[0])));
39
+ return nextLevelNames.sort((a, b) => a.localeCompare(b));
40
+ };
41
+
42
+ // Recursive function to create nodes.
43
+ const getNode = (parentLevelPrefixes, currLevelName, childLevelSuffixes) => {
44
+ const isLeaf = childLevelSuffixes.length === 0;
45
+ const resultNode = {
46
+ name: currLevelName,
47
+ };
48
+ if (isLeaf) {
49
+ // Base case: this is a leaf node.
50
+ resultNode.set = levelSets.get([...parentLevelPrefixes, currLevelName]);
51
+ } else {
52
+ // Are the remaining suffices redundant?
53
+ // Consider ["Parent", "Child", "Child"]
54
+ // where parentLevelPrefixes is ["Parent"] and currLevelName is "Child".
55
+ const shouldBeLeaf = (
56
+ childLevelSuffixes.length === 1
57
+ && currLevelName === childLevelSuffixes[0][childLevelSuffixes[0].length - 1]
58
+ );
59
+ if (shouldBeLeaf) {
60
+ resultNode.set = levelSets.get(
61
+ [...parentLevelPrefixes, currLevelName, ...childLevelSuffixes[0]],
62
+ );
63
+ } else {
64
+ // Recursion, run getNode() on each of the unique names at the next level.
65
+ const nextLevelNames = getNextLevelNames(childLevelSuffixes);
66
+
67
+ resultNode.children = nextLevelNames
68
+ .map(nextLevelName => getNode(
69
+ [...parentLevelPrefixes, currLevelName],
70
+ nextLevelName,
71
+ childLevelSuffixes
72
+ .filter(l => l[0] === nextLevelName)
73
+ .map(l => l.slice(1))
74
+ .filter(v => v.length > 0),
75
+ ));
76
+ }
77
+ }
78
+ return resultNode;
79
+ };
80
+ // Start the recursion.
81
+ const levelOneNodes = getNextLevelNames(levels)
82
+ .map(levelOneName => getNode(
83
+ [],
84
+ levelOneName,
85
+ levels.filter(l => l[0] === levelOneName).map(l => l.slice(1)),
86
+ ));
87
+
88
+ levelZeroNode.children = levelOneNodes;
89
+ } else {
90
+ // Single-level case.
91
+ // Check for the optional corresponding confidence score column name.
92
+ const uniqueCellSetIds = Array.from(new Set(cellSetIds)).sort();
93
+ const clusters = {};
94
+ // eslint-disable-next-line no-return-assign
95
+ uniqueCellSetIds.forEach(id => (clusters[id] = { name: id, set: [] }));
96
+ if (cellSetScores[j]) {
97
+ cellSetIds
98
+ .forEach((id, i) => clusters[id].set.push([cellNames[j][i], cellSetScores[j][i]]));
99
+ } else {
100
+ cellSetIds
101
+ .forEach((id, i) => clusters[id].set.push([cellNames[j][i], null]));
102
+ }
103
+ Object.values(clusters).forEach(
104
+ // eslint-disable-next-line no-return-assign
105
+ cluster => (levelZeroNode = nodeAppendChild(levelZeroNode, cluster)),
106
+ );
107
+ }
108
+ cellSetsTree.tree.push(levelZeroNode);
109
+ });
110
+ return cellSetsTree;
111
+ }
@@ -0,0 +1,163 @@
1
+ import { dataToCellSetsTree } from './CellSetsZarrLoader';
2
+
3
+ describe('loaders/CellSetsZarrLoader', () => {
4
+ it('dataToCellSetsTree constructs a hierarchy from an array of columns', async () => {
5
+ const data = [
6
+ [
7
+ ['cell_1', 'cell_2', 'cell_3', 'cell_4'],
8
+ ],
9
+ [
10
+ [
11
+ ['Immune', 'Immune', 'Immune', 'Neuron'],
12
+ ['B cell', 'B cell', 'B cell', 'Excitatory neuron'],
13
+ ['CD19+', 'CD19-', 'CD19-', 'Retinal bipolar neuron'],
14
+ ],
15
+ ],
16
+ [undefined],
17
+ ];
18
+ const options = [
19
+ { name: 'Subclass Levels', path: ['obs/L1', 'obs/L2', 'obs/L3'] },
20
+ ];
21
+
22
+ const tree = dataToCellSetsTree(data, options);
23
+
24
+ expect(tree).toEqual({
25
+ version: '0.1.3',
26
+ datatype: 'obs',
27
+ tree: [
28
+ {
29
+ name: 'Subclass Levels',
30
+ children: [
31
+ {
32
+ name: 'Immune',
33
+ children: [
34
+ {
35
+ name: 'B cell',
36
+ children: [
37
+ {
38
+ name: 'CD19-',
39
+ set: [
40
+ ['cell_2', null],
41
+ ['cell_3', null],
42
+ ],
43
+ },
44
+ {
45
+ name: 'CD19+',
46
+ set: [
47
+ ['cell_1', null],
48
+ ],
49
+ },
50
+ ],
51
+ },
52
+ ],
53
+ },
54
+ {
55
+ name: 'Neuron',
56
+ children: [
57
+ {
58
+ name: 'Excitatory neuron',
59
+ children: [
60
+ {
61
+ name: 'Retinal bipolar neuron',
62
+ set: [
63
+ ['cell_4', null],
64
+ ],
65
+ },
66
+ ],
67
+ },
68
+ ],
69
+ },
70
+ ],
71
+ },
72
+ ],
73
+ });
74
+ });
75
+
76
+ it('dataToCellSetsTree creates a flat tree from a single column', async () => {
77
+ const data = [
78
+ [
79
+ ['cell_1', 'cell_2', 'cell_3', 'cell_4'],
80
+ ],
81
+ [
82
+ ['Immune', 'Immune', 'Immune', 'Neuron'],
83
+ ],
84
+ [undefined],
85
+ ];
86
+ const options = [
87
+ { name: 'Subclass Level 1', path: 'obs/L1' },
88
+ ];
89
+
90
+ const tree = dataToCellSetsTree(data, options);
91
+
92
+ expect(tree).toEqual({
93
+ version: '0.1.3',
94
+ datatype: 'obs',
95
+ tree: [
96
+ {
97
+ name: 'Subclass Level 1',
98
+ children: [
99
+ {
100
+ name: 'Immune',
101
+ set: [
102
+ ['cell_1', null],
103
+ ['cell_2', null],
104
+ ['cell_3', null],
105
+ ],
106
+ },
107
+ {
108
+ name: 'Neuron',
109
+ set: [
110
+ ['cell_4', null],
111
+ ],
112
+ },
113
+ ],
114
+ },
115
+ ],
116
+ });
117
+ });
118
+
119
+ it('dataToCellSetsTree creates a flat tree from a single column with scores', async () => {
120
+ const data = [
121
+ [
122
+ ['cell_1', 'cell_2', 'cell_3', 'cell_4'],
123
+ ],
124
+ [
125
+ ['Immune', 'Immune', 'Immune', 'Neuron'],
126
+ ],
127
+ [
128
+ [0.25, 0.5, 0.6, 0.1],
129
+ ],
130
+ ];
131
+ const options = [
132
+ { name: 'Subclass Level 1', path: 'obs/L1' },
133
+ ];
134
+
135
+ const tree = dataToCellSetsTree(data, options);
136
+
137
+ expect(tree).toEqual({
138
+ version: '0.1.3',
139
+ datatype: 'obs',
140
+ tree: [
141
+ {
142
+ name: 'Subclass Level 1',
143
+ children: [
144
+ {
145
+ name: 'Immune',
146
+ set: [
147
+ ['cell_1', 0.25],
148
+ ['cell_2', 0.5],
149
+ ['cell_3', 0.6],
150
+ ],
151
+ },
152
+ {
153
+ name: 'Neuron',
154
+ set: [
155
+ ['cell_4', 0.1],
156
+ ],
157
+ },
158
+ ],
159
+ },
160
+ ],
161
+ });
162
+ });
163
+ });