@vitessce/zarr 2.0.3 → 3.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/dist/{deflate.9bff78ef.mjs → deflate-4cf9f098.js} +2 -2
- package/dist/{index.ae29d0d3.mjs → index-2a8972b1.js} +39282 -30489
- package/dist/{index.mjs → index.js} +2 -2
- package/dist/{jpeg.7d8fcd34.mjs → jpeg-e220efb2.js} +1 -1
- package/dist/{lerc.242ab449.mjs → lerc-12b63f0c.js} +76 -5
- package/dist/{lzw.801ba405.mjs → lzw-68593c7a.js} +1 -1
- package/dist/{packbits.300e46d9.mjs → packbits-c500b823.js} +1 -1
- package/dist/{pako.esm.4b234125.mjs → pako.esm-68f84e2a.js} +97 -15
- package/dist/{raw.de2b32d2.mjs → raw-d95d6509.js} +1 -1
- package/dist/{webimage.97478b33.mjs → webimage-7938b145.js} +1 -1
- package/dist-tsc/AnnDataSource.d.ts +60 -0
- package/dist-tsc/AnnDataSource.d.ts.map +1 -0
- package/dist-tsc/AnnDataSource.js +241 -0
- package/dist-tsc/AnnDataSource.test.d.ts +2 -0
- package/dist-tsc/AnnDataSource.test.d.ts.map +1 -0
- package/dist-tsc/AnnDataSource.test.js +50 -0
- package/dist-tsc/MuDataSource.d.ts +21 -0
- package/dist-tsc/MuDataSource.d.ts.map +1 -0
- package/dist-tsc/MuDataSource.js +70 -0
- package/dist-tsc/MuDataSource.test.d.ts +2 -0
- package/dist-tsc/MuDataSource.test.d.ts.map +1 -0
- package/dist-tsc/MuDataSource.test.js +43 -0
- package/dist-tsc/ZarrDataSource.d.ts +21 -0
- package/dist-tsc/ZarrDataSource.d.ts.map +1 -0
- package/dist-tsc/ZarrDataSource.js +35 -0
- package/dist-tsc/anndata-loaders/FeatureLabelsAnndataLoader.d.ts +13 -0
- package/dist-tsc/anndata-loaders/FeatureLabelsAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/FeatureLabelsAnndataLoader.js +41 -0
- package/dist-tsc/anndata-loaders/ObsEmbeddingAnndataLoader.d.ts +13 -0
- package/dist-tsc/anndata-loaders/ObsEmbeddingAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsEmbeddingAnndataLoader.js +36 -0
- package/dist-tsc/anndata-loaders/ObsEmbeddingAnndataLoader.test.d.ts +2 -0
- package/dist-tsc/anndata-loaders/ObsEmbeddingAnndataLoader.test.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsEmbeddingAnndataLoader.test.js +69 -0
- package/dist-tsc/anndata-loaders/ObsFeatureMatrixAnndataLoader.d.ts +94 -0
- package/dist-tsc/anndata-loaders/ObsFeatureMatrixAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsFeatureMatrixAnndataLoader.js +340 -0
- package/dist-tsc/anndata-loaders/ObsFeatureMatrixAnndataLoader.test.d.ts +2 -0
- package/dist-tsc/anndata-loaders/ObsFeatureMatrixAnndataLoader.test.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsFeatureMatrixAnndataLoader.test.js +113 -0
- package/dist-tsc/anndata-loaders/ObsLabelsAnndataLoader.d.ts +13 -0
- package/dist-tsc/anndata-loaders/ObsLabelsAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsLabelsAnndataLoader.js +34 -0
- package/dist-tsc/anndata-loaders/ObsLocationsAnndataLoader.d.ts +13 -0
- package/dist-tsc/anndata-loaders/ObsLocationsAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsLocationsAnndataLoader.js +33 -0
- package/dist-tsc/anndata-loaders/ObsSegmentationsAnndataLoader.d.ts +13 -0
- package/dist-tsc/anndata-loaders/ObsSegmentationsAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsSegmentationsAnndataLoader.js +48 -0
- package/dist-tsc/anndata-loaders/ObsSetsAnndataLoader.d.ts +11 -0
- package/dist-tsc/anndata-loaders/ObsSetsAnndataLoader.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/ObsSetsAnndataLoader.js +65 -0
- package/dist-tsc/anndata-loaders/index.d.ts +8 -0
- package/dist-tsc/anndata-loaders/index.d.ts.map +1 -0
- package/dist-tsc/anndata-loaders/index.js +7 -0
- package/dist-tsc/genomic-loaders/GenomicProfilesZarrLoader.d.ts +6 -0
- package/dist-tsc/genomic-loaders/GenomicProfilesZarrLoader.d.ts.map +1 -0
- package/dist-tsc/genomic-loaders/GenomicProfilesZarrLoader.js +15 -0
- package/dist-tsc/genomic-loaders/index.d.ts +2 -0
- package/dist-tsc/genomic-loaders/index.d.ts.map +1 -0
- package/dist-tsc/genomic-loaders/index.js +1 -0
- package/dist-tsc/index.d.ts +8 -0
- package/dist-tsc/index.d.ts.map +1 -0
- package/dist-tsc/index.js +8 -7
- package/dist-tsc/matrix-loaders/MatrixZarrAsObsFeatureMatrix.d.ts +14 -0
- package/dist-tsc/matrix-loaders/MatrixZarrAsObsFeatureMatrix.d.ts.map +1 -0
- package/dist-tsc/matrix-loaders/MatrixZarrAsObsFeatureMatrix.js +30 -0
- package/dist-tsc/matrix-loaders/index.d.ts +2 -0
- package/dist-tsc/matrix-loaders/index.d.ts.map +1 -0
- package/dist-tsc/matrix-loaders/index.js +1 -0
- package/dist-tsc/ome-loaders/OmeZarrLoader.d.ts +4 -0
- package/dist-tsc/ome-loaders/OmeZarrLoader.d.ts.map +1 -0
- package/dist-tsc/ome-loaders/OmeZarrLoader.js +79 -0
- package/dist-tsc/ome-loaders/index.d.ts +2 -0
- package/dist-tsc/ome-loaders/index.d.ts.map +1 -0
- package/dist-tsc/ome-loaders/index.js +1 -0
- package/dist-tsc/utils.d.ts +2 -0
- package/dist-tsc/utils.d.ts.map +1 -0
- package/dist-tsc/utils.js +5 -0
- package/package.json +20 -11
- package/src/AnnDataSource.js +3 -3
- package/src/AnnDataSource.test.js +8 -7
- package/src/MuDataSource.js +1 -1
- package/src/MuDataSource.test.js +7 -6
- package/src/anndata-loaders/FeatureLabelsAnndataLoader.js +0 -17
- package/src/anndata-loaders/ObsEmbeddingAnndataLoader.js +1 -6
- package/src/anndata-loaders/ObsEmbeddingAnndataLoader.test.js +7 -6
- package/src/anndata-loaders/ObsFeatureMatrixAnndataLoader.js +11 -21
- package/src/anndata-loaders/ObsFeatureMatrixAnndataLoader.test.js +25 -24
- package/src/anndata-loaders/ObsLabelsAnndataLoader.js +1 -6
- package/src/anndata-loaders/ObsLocationsAnndataLoader.js +1 -6
- package/src/anndata-loaders/ObsSegmentationsAnndataLoader.js +1 -6
- package/src/anndata-loaders/ObsSetsAnndataLoader.js +1 -6
- package/src/anndata-loaders/index.js +7 -7
- package/src/genomic-loaders/index.js +1 -1
- package/src/index.js +8 -7
- package/src/matrix-loaders/index.js +1 -1
- package/src/ome-loaders/OmeZarrLoader.js +39 -5
- package/src/ome-loaders/index.js +1 -1
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/* eslint-disable no-underscore-dangle */
|
|
2
|
+
import { openArray } from 'zarr';
|
|
3
|
+
import { range } from 'lodash-es';
|
|
4
|
+
import { dirname } from './utils.js';
|
|
5
|
+
import ZarrDataSource from './ZarrDataSource.js';
|
|
6
|
+
const readFloat32FromUint8 = (bytes) => {
|
|
7
|
+
if (bytes.length !== 4) {
|
|
8
|
+
throw new Error('readFloat32 only takes in length 4 byte buffers');
|
|
9
|
+
}
|
|
10
|
+
return new Int32Array(bytes.buffer)[0];
|
|
11
|
+
};
|
|
12
|
+
const HEADER_LENGTH = 4;
|
|
13
|
+
/**
|
|
14
|
+
* Method for decoding text arrays from zarr.
|
|
15
|
+
* Largerly a port of https://github.com/zarr-developers/numcodecs/blob/2c1aff98e965c3c4747d9881d8b8d4aad91adb3a/numcodecs/vlen.pyx#L135-L178
|
|
16
|
+
* @returns {string[]} An array of strings.
|
|
17
|
+
*/
|
|
18
|
+
function parseVlenUtf8(buffer) {
|
|
19
|
+
const decoder = new TextDecoder();
|
|
20
|
+
let data = 0;
|
|
21
|
+
const dataEnd = data + buffer.length;
|
|
22
|
+
const length = readFloat32FromUint8(buffer.slice(data, HEADER_LENGTH));
|
|
23
|
+
if (buffer.length < HEADER_LENGTH) {
|
|
24
|
+
throw new Error('corrupt buffer, missing or truncated header');
|
|
25
|
+
}
|
|
26
|
+
data += HEADER_LENGTH;
|
|
27
|
+
const output = new Array(length);
|
|
28
|
+
for (let i = 0; i < length; i += 1) {
|
|
29
|
+
if (data + 4 > dataEnd) {
|
|
30
|
+
throw new Error('corrupt buffer, data seem truncated');
|
|
31
|
+
}
|
|
32
|
+
const l = readFloat32FromUint8(buffer.slice(data, data + 4));
|
|
33
|
+
data += 4;
|
|
34
|
+
if (data + l > dataEnd) {
|
|
35
|
+
throw new Error('corrupt buffer, data seem truncated');
|
|
36
|
+
}
|
|
37
|
+
output[i] = decoder.decode(buffer.slice(data, data + l));
|
|
38
|
+
data += l;
|
|
39
|
+
}
|
|
40
|
+
return output;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A base AnnData loader which has all shared methods for more comlpex laoders,
|
|
44
|
+
* like loading cell names and ids. It inherits from AbstractLoader.
|
|
45
|
+
*/
|
|
46
|
+
export default class AnnDataSource extends ZarrDataSource {
|
|
47
|
+
constructor(...args) {
|
|
48
|
+
super(...args);
|
|
49
|
+
/** @type {Map<string, Promise<string[]>} */
|
|
50
|
+
this.promises = new Map();
|
|
51
|
+
}
|
|
52
|
+
loadObsColumns(paths) {
|
|
53
|
+
return this._loadColumns(paths);
|
|
54
|
+
}
|
|
55
|
+
loadVarColumns(paths) {
|
|
56
|
+
return this._loadColumns(paths);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Class method for loading obs variables.
|
|
60
|
+
* Takes the location as an argument because this is shared across objects,
|
|
61
|
+
* which have different ways of specifying location.
|
|
62
|
+
* @param {string[]} paths An array of strings like "obs/leiden" or "obs/bulk_labels."
|
|
63
|
+
* @returns {Promise} A promise for an array of ids with one per cell.
|
|
64
|
+
*/
|
|
65
|
+
_loadColumns(paths) {
|
|
66
|
+
const promises = paths.map((path) => {
|
|
67
|
+
const getCol = (col) => {
|
|
68
|
+
if (!this.promises.has(col)) {
|
|
69
|
+
const obsPromise = this._loadColumn(col).catch((err) => {
|
|
70
|
+
// clear from cache if promise rejects
|
|
71
|
+
this.promises.delete(col);
|
|
72
|
+
// propagate error
|
|
73
|
+
throw err;
|
|
74
|
+
});
|
|
75
|
+
this.promises.set(col, obsPromise);
|
|
76
|
+
}
|
|
77
|
+
return this.promises.get(col);
|
|
78
|
+
};
|
|
79
|
+
if (!path) {
|
|
80
|
+
return Promise.resolve(undefined);
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(path)) {
|
|
83
|
+
return Promise.resolve(Promise.all(path.map(getCol)));
|
|
84
|
+
}
|
|
85
|
+
return getCol(path);
|
|
86
|
+
});
|
|
87
|
+
return Promise.all(promises);
|
|
88
|
+
}
|
|
89
|
+
async _loadColumn(path) {
|
|
90
|
+
const { store } = this;
|
|
91
|
+
const prefix = dirname(path);
|
|
92
|
+
const { categories, 'encoding-type': encodingType } = await this.getJson(`${path}/.zattrs`);
|
|
93
|
+
let categoriesValues;
|
|
94
|
+
let codes;
|
|
95
|
+
if (categories) {
|
|
96
|
+
const { dtype } = await this.getJson(`/${prefix}/${categories}/.zarray`);
|
|
97
|
+
if (dtype === '|O') {
|
|
98
|
+
categoriesValues = await this.getFlatArrDecompressed(`/${prefix}/${categories}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (encodingType === 'categorical') {
|
|
102
|
+
const { dtype } = await this.getJson(`/${path}/categories/.zarray`);
|
|
103
|
+
if (dtype === '|O') {
|
|
104
|
+
categoriesValues = await this.getFlatArrDecompressed(`/${path}/categories`);
|
|
105
|
+
}
|
|
106
|
+
codes = `/${path}/codes`;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const { dtype } = await this.getJson(`/${path}/.zarray`);
|
|
110
|
+
if (dtype === '|O') {
|
|
111
|
+
return this.getFlatArrDecompressed(path);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const arr = await openArray({ store, path: codes || path, mode: 'r' });
|
|
115
|
+
const values = await arr.get();
|
|
116
|
+
const { data } = values;
|
|
117
|
+
const mappedValues = Array.from(data).map(i => (!categoriesValues ? String(i) : categoriesValues[i]));
|
|
118
|
+
return mappedValues;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Class method for loading general numeric arrays.
|
|
122
|
+
* @param {string} path A string like obsm.X_pca.
|
|
123
|
+
* @returns {Promise} A promise for a zarr array containing the data.
|
|
124
|
+
*/
|
|
125
|
+
loadNumeric(path) {
|
|
126
|
+
const { store } = this;
|
|
127
|
+
return openArray({
|
|
128
|
+
store,
|
|
129
|
+
path,
|
|
130
|
+
mode: 'r',
|
|
131
|
+
}).then(arr => arr.get());
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Class method for loading specific columns of numeric arrays.
|
|
135
|
+
* @param {string} path A string like obsm.X_pca.
|
|
136
|
+
* @param {number[]} dims The column indices to load.
|
|
137
|
+
* @returns {Promise} A promise for a zarr array containing the data.
|
|
138
|
+
*/
|
|
139
|
+
loadNumericForDims(path, dims) {
|
|
140
|
+
const { store } = this;
|
|
141
|
+
const arr = openArray({
|
|
142
|
+
store,
|
|
143
|
+
path,
|
|
144
|
+
mode: 'r',
|
|
145
|
+
});
|
|
146
|
+
return Promise.all(dims.map(dim => arr.then(loadedArr => loadedArr.get([null, dim])))).then(cols => ({
|
|
147
|
+
data: cols.map(col => col.data),
|
|
148
|
+
shape: [dims.length, cols[0].shape[0]],
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* A common method for loading flattened data
|
|
153
|
+
* i.e that which has shape [n] where n is a natural number.
|
|
154
|
+
* @param {string} path A path to a flat array location, like obs/_index
|
|
155
|
+
* @returns {Array} The data from the zarr array.
|
|
156
|
+
*/
|
|
157
|
+
getFlatArrDecompressed(path) {
|
|
158
|
+
const { store } = this;
|
|
159
|
+
return openArray({
|
|
160
|
+
store,
|
|
161
|
+
path,
|
|
162
|
+
mode: 'r',
|
|
163
|
+
}).then(async (z) => {
|
|
164
|
+
let data;
|
|
165
|
+
const parseAndMergeTextBytes = (dbytes) => {
|
|
166
|
+
const text = parseVlenUtf8(dbytes);
|
|
167
|
+
if (!data) {
|
|
168
|
+
data = text;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
data = data.concat(text);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const mergeBytes = (dbytes) => {
|
|
175
|
+
if (!data) {
|
|
176
|
+
data = dbytes;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const tmp = new Uint8Array(dbytes.buffer.byteLength + data.buffer.byteLength);
|
|
180
|
+
tmp.set(new Uint8Array(data.buffer), 0);
|
|
181
|
+
tmp.set(dbytes, data.buffer.byteLength);
|
|
182
|
+
data = tmp;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const numRequests = Math.ceil(z.meta.shape[0] / z.meta.chunks[0]);
|
|
186
|
+
const requests = range(numRequests).map(async (item) => store
|
|
187
|
+
.getItem(`${z.keyPrefix}${String(item)}`)
|
|
188
|
+
.then(buf => z.compressor.then(compressor => compressor.decode(buf))));
|
|
189
|
+
const dbytesArr = await Promise.all(requests);
|
|
190
|
+
dbytesArr.forEach((dbytes) => {
|
|
191
|
+
// Use vlenutf-8 decoding if necessary and merge `data` as a normal array.
|
|
192
|
+
if (Array.isArray(z.meta.filters)
|
|
193
|
+
&& z.meta.filters[0].id === 'vlen-utf8') {
|
|
194
|
+
parseAndMergeTextBytes(dbytes);
|
|
195
|
+
// Otherwise just merge the bytes as a typed array.
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
mergeBytes(dbytes);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
const { meta: { shape: [length], }, } = z;
|
|
202
|
+
// truncate the filled in values
|
|
203
|
+
return data.slice(0, length);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Class method for loading the obs index.
|
|
208
|
+
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
209
|
+
*/
|
|
210
|
+
loadObsIndex() {
|
|
211
|
+
if (this.obsIndex) {
|
|
212
|
+
return this.obsIndex;
|
|
213
|
+
}
|
|
214
|
+
this.obsIndex = this.getJson('obs/.zattrs').then(({ _index }) => this.getFlatArrDecompressed(`/obs/${_index}`));
|
|
215
|
+
return this.obsIndex;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Class method for loading the var index.
|
|
219
|
+
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
220
|
+
*/
|
|
221
|
+
loadVarIndex() {
|
|
222
|
+
if (this.varIndex) {
|
|
223
|
+
return this.varIndex;
|
|
224
|
+
}
|
|
225
|
+
this.varIndex = this.getJson('var/.zattrs').then(({ _index }) => this.getFlatArrDecompressed(`/var/${_index}`));
|
|
226
|
+
return this.varIndex;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Class method for loading the var alias.
|
|
230
|
+
* @returns {Promise} An promise for a zarr array containing the aliased names.
|
|
231
|
+
*/
|
|
232
|
+
async loadVarAlias(varPath) {
|
|
233
|
+
if (this.varAlias) {
|
|
234
|
+
return this.varAlias;
|
|
235
|
+
}
|
|
236
|
+
[this.varAlias] = await this.loadVarColumns([varPath]);
|
|
237
|
+
const index = await this.loadVarIndex();
|
|
238
|
+
this.varAlias = this.varAlias.map((val, ind) => (val ? val.concat(` (${index[ind]})`) : index[ind]));
|
|
239
|
+
return this.varAlias;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnnDataSource.test.d.ts","sourceRoot":"","sources":["../src/AnnDataSource.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import AnnDataSource from './AnnDataSource.js';
|
|
3
|
+
describe('sources/AnnDataSource', () => {
|
|
4
|
+
describe('AnnData v0.7', () => {
|
|
5
|
+
it('getJson returns json', async () => {
|
|
6
|
+
const dataSource = new AnnDataSource({
|
|
7
|
+
url: 'http://localhost:4204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr',
|
|
8
|
+
});
|
|
9
|
+
const zGroup = await dataSource.getJson('.zgroup');
|
|
10
|
+
expect(zGroup.zarr_format).toEqual(2);
|
|
11
|
+
});
|
|
12
|
+
it('loadObsColumns returns ids for location in store', async () => {
|
|
13
|
+
const dataSource = new AnnDataSource({
|
|
14
|
+
url: 'http://localhost:4204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr',
|
|
15
|
+
});
|
|
16
|
+
const ids = await dataSource.loadObsColumns(['obs/leiden']);
|
|
17
|
+
expect(ids).toEqual([['1', '1', '2']]);
|
|
18
|
+
});
|
|
19
|
+
it('loadObsIndex returns names', async () => {
|
|
20
|
+
const dataSource = new AnnDataSource({
|
|
21
|
+
url: 'http://localhost:4204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr',
|
|
22
|
+
});
|
|
23
|
+
const names = await dataSource.loadObsIndex();
|
|
24
|
+
expect(names).toEqual(['CTG', 'GCA', 'CTG']);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe('AnnData v0.8', () => {
|
|
28
|
+
it('getJson returns json', async () => {
|
|
29
|
+
const dataSource = new AnnDataSource({
|
|
30
|
+
url: 'http://localhost:4204/@fixtures/zarr/anndata-0.8/anndata-dense.zarr',
|
|
31
|
+
});
|
|
32
|
+
const zGroup = await dataSource.getJson('.zgroup');
|
|
33
|
+
expect(zGroup.zarr_format).toEqual(2);
|
|
34
|
+
});
|
|
35
|
+
it('loadObsColumns returns ids for location in store', async () => {
|
|
36
|
+
const dataSource = new AnnDataSource({
|
|
37
|
+
url: 'http://localhost:4204/@fixtures/zarr/anndata-0.8/anndata-dense.zarr',
|
|
38
|
+
});
|
|
39
|
+
const ids = await dataSource.loadObsColumns(['obs/leiden']);
|
|
40
|
+
expect(ids).toEqual([['1', '1', '2']]);
|
|
41
|
+
});
|
|
42
|
+
it('loadObsIndex returns names', async () => {
|
|
43
|
+
const dataSource = new AnnDataSource({
|
|
44
|
+
url: 'http://localhost:4204/@fixtures/zarr/anndata-0.8/anndata-dense.zarr',
|
|
45
|
+
});
|
|
46
|
+
const names = await dataSource.loadObsIndex();
|
|
47
|
+
expect(names).toEqual(['CTG', 'GCA', 'CTG']);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function getObsPath(arrPath: any): string;
|
|
2
|
+
export function getVarPath(arrPath: any): string;
|
|
3
|
+
export default class MuDataSource extends AnnDataSource {
|
|
4
|
+
/**
|
|
5
|
+
* Class method for loading the obs index.
|
|
6
|
+
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
7
|
+
*/
|
|
8
|
+
loadObsIndex(path?: null): Promise<any>;
|
|
9
|
+
/**
|
|
10
|
+
* Class method for loading the var index.
|
|
11
|
+
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
12
|
+
*/
|
|
13
|
+
loadVarIndex(path?: null): Promise<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Class method for loading the var alias.
|
|
16
|
+
* @returns {Promise} An promise for a zarr array containing the aliased names.
|
|
17
|
+
*/
|
|
18
|
+
loadVarAlias(varPath: any, matrixPath: any): Promise<any>;
|
|
19
|
+
}
|
|
20
|
+
import AnnDataSource from "./AnnDataSource.js";
|
|
21
|
+
//# sourceMappingURL=MuDataSource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MuDataSource.d.ts","sourceRoot":"","sources":["../src/MuDataSource.js"],"names":[],"mappings":"AAeA,iDAEC;AAED,iDAEC;AAED;IACE;;;OAGG;IACH,wCAWC;IAED;;;OAGG;IACH,wCAWC;IAED;;;OAGG;IACH,0DAcC;CACF"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import AnnDataSource from './AnnDataSource.js';
|
|
2
|
+
// If the array path starts with mod/something/rest
|
|
3
|
+
// capture mod/something.
|
|
4
|
+
const regex = /^mod\/([^/]*)\/(.*)$/;
|
|
5
|
+
function getModPrefix(arrPath) {
|
|
6
|
+
if (arrPath) {
|
|
7
|
+
const matches = arrPath.match(regex);
|
|
8
|
+
if (matches && matches.length === 3) {
|
|
9
|
+
return `mod/${matches[1]}/`;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return '';
|
|
13
|
+
}
|
|
14
|
+
export function getObsPath(arrPath) {
|
|
15
|
+
return `${getModPrefix(arrPath)}obs`;
|
|
16
|
+
}
|
|
17
|
+
export function getVarPath(arrPath) {
|
|
18
|
+
return `${getModPrefix(arrPath)}var`;
|
|
19
|
+
}
|
|
20
|
+
export default class MuDataSource extends AnnDataSource {
|
|
21
|
+
/**
|
|
22
|
+
* Class method for loading the obs index.
|
|
23
|
+
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
24
|
+
*/
|
|
25
|
+
loadObsIndex(path = null) {
|
|
26
|
+
if (!this.obsIndex) {
|
|
27
|
+
this.obsIndex = {};
|
|
28
|
+
}
|
|
29
|
+
const obsPath = getObsPath(path);
|
|
30
|
+
if (this.obsIndex[obsPath]) {
|
|
31
|
+
return this.obsIndex[obsPath];
|
|
32
|
+
}
|
|
33
|
+
this.obsIndex[obsPath] = this.getJson(`${obsPath}/.zattrs`)
|
|
34
|
+
.then(({ _index }) => this.getFlatArrDecompressed(`${obsPath}/${_index}`));
|
|
35
|
+
return this.obsIndex[obsPath];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Class method for loading the var index.
|
|
39
|
+
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
40
|
+
*/
|
|
41
|
+
loadVarIndex(path = null) {
|
|
42
|
+
if (!this.varIndex) {
|
|
43
|
+
this.varIndex = {};
|
|
44
|
+
}
|
|
45
|
+
const varPath = getVarPath(path);
|
|
46
|
+
if (this.varIndex[varPath]) {
|
|
47
|
+
return this.varIndex[varPath];
|
|
48
|
+
}
|
|
49
|
+
this.varIndex[varPath] = this.getJson(`${varPath}/.zattrs`)
|
|
50
|
+
.then(({ _index }) => this.getFlatArrDecompressed(`${varPath}/${_index}`));
|
|
51
|
+
return this.varIndex[varPath];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Class method for loading the var alias.
|
|
55
|
+
* @returns {Promise} An promise for a zarr array containing the aliased names.
|
|
56
|
+
*/
|
|
57
|
+
async loadVarAlias(varPath, matrixPath) {
|
|
58
|
+
if (!this.varAlias) {
|
|
59
|
+
this.varAlias = {};
|
|
60
|
+
}
|
|
61
|
+
if (this.varAlias[varPath]) {
|
|
62
|
+
return this.varAlias[varPath];
|
|
63
|
+
}
|
|
64
|
+
const [varAliasData] = await this.loadVarColumns([varPath]);
|
|
65
|
+
this.varAlias[varPath] = varAliasData;
|
|
66
|
+
const index = await this.loadVarIndex(matrixPath);
|
|
67
|
+
this.varAlias[varPath] = this.varAlias[varPath].map((val, ind) => (val ? val.concat(` (${index[ind]})`) : index[ind]));
|
|
68
|
+
return this.varAlias[varPath];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MuDataSource.test.d.ts","sourceRoot":"","sources":["../src/MuDataSource.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { range } from 'lodash-es';
|
|
3
|
+
import MuDataSource, { getObsPath, getVarPath } from './MuDataSource.js';
|
|
4
|
+
describe('sources/MuDataSource', () => {
|
|
5
|
+
it('loadObsColumns returns ids for location in store, with joint-modality path', async () => {
|
|
6
|
+
const dataSource = new MuDataSource({
|
|
7
|
+
url: 'http://localhost:4204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
8
|
+
});
|
|
9
|
+
const ids = await dataSource.loadObsColumns(['obs/leiden']);
|
|
10
|
+
expect(ids).toEqual([['3', '2', '3', '1', '1']]);
|
|
11
|
+
});
|
|
12
|
+
it('loadObsColumns returns ids for location in store, with modality-specific path', async () => {
|
|
13
|
+
const dataSource = new MuDataSource({
|
|
14
|
+
url: 'http://localhost:4204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
15
|
+
});
|
|
16
|
+
const ids = await dataSource.loadObsColumns(['mod/rna/obs/leiden']);
|
|
17
|
+
expect(ids).toEqual([['1', '1', '2', '2']]);
|
|
18
|
+
});
|
|
19
|
+
it('loadVarIndex supports modality-specific paths', async () => {
|
|
20
|
+
const dataSource = new MuDataSource({
|
|
21
|
+
url: 'http://localhost:4204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
22
|
+
});
|
|
23
|
+
const names = await dataSource.loadVarIndex('mod/atac/X');
|
|
24
|
+
expect(names).toEqual(range(15).map(i => `peak_${i}`));
|
|
25
|
+
});
|
|
26
|
+
it('loadObsIndex returns names', async () => {
|
|
27
|
+
const dataSource = new MuDataSource({
|
|
28
|
+
url: 'http://localhost:4204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
29
|
+
});
|
|
30
|
+
const names = await dataSource.loadObsIndex('mod/atac/X');
|
|
31
|
+
expect(names).toEqual(['CTG', 'GCA', 'CTT', 'GGG']);
|
|
32
|
+
});
|
|
33
|
+
it('getObsPath regex works', async () => {
|
|
34
|
+
expect(getObsPath('mod/rna/obsm/X_umap')).toEqual('mod/rna/obs');
|
|
35
|
+
expect(getObsPath('mod/rna/X')).toEqual('mod/rna/obs');
|
|
36
|
+
expect(getObsPath('obsm/X_umap')).toEqual('obs');
|
|
37
|
+
});
|
|
38
|
+
it('getVarPath regex works', async () => {
|
|
39
|
+
expect(getVarPath('mod/rna/obsm/X_umap')).toEqual('mod/rna/var');
|
|
40
|
+
expect(getVarPath('mod/rna/X')).toEqual('mod/rna/var');
|
|
41
|
+
expect(getVarPath('obsm/X_umap')).toEqual('var');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A loader ancestor class containing a default constructor
|
|
3
|
+
* and a stub for the required load() method.
|
|
4
|
+
*/
|
|
5
|
+
export default class ZarrDataSource {
|
|
6
|
+
constructor({ url, requestInit }: {
|
|
7
|
+
url: any;
|
|
8
|
+
requestInit: any;
|
|
9
|
+
});
|
|
10
|
+
store: HTTPStore;
|
|
11
|
+
/**
|
|
12
|
+
* Class method for decoding json from the store.
|
|
13
|
+
* @param {string} key A path to the item.
|
|
14
|
+
* @returns {Promise} This async function returns a promise
|
|
15
|
+
* that resolves to the parsed JSON if successful.
|
|
16
|
+
* @throws This may throw an error.
|
|
17
|
+
*/
|
|
18
|
+
getJson(key: string): Promise<any>;
|
|
19
|
+
}
|
|
20
|
+
import { HTTPStore } from "zarr/types/storage/httpStore";
|
|
21
|
+
//# sourceMappingURL=ZarrDataSource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ZarrDataSource.d.ts","sourceRoot":"","sources":["../src/ZarrDataSource.js"],"names":[],"mappings":"AAEA;;;GAGG;AACH;IACE;;;OAOC;IAHC,iBAEE;IAGJ;;;;;;OAMG;IACH,aALW,MAAM,gBAgBhB;CACF"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { HTTPStore, KeyError } from 'zarr';
|
|
2
|
+
/**
|
|
3
|
+
* A loader ancestor class containing a default constructor
|
|
4
|
+
* and a stub for the required load() method.
|
|
5
|
+
*/
|
|
6
|
+
export default class ZarrDataSource {
|
|
7
|
+
constructor({ url, requestInit }) {
|
|
8
|
+
// TODO: We should probably add a way of allowing HEAD requests as well:
|
|
9
|
+
// https://github.com/gzuidhof/zarr.js/blob/375ce0c299469a970da6bb5653513564e25806bb/docs/getting-started/remote-data.md#stores
|
|
10
|
+
const supportedMethods = ['GET'];
|
|
11
|
+
this.store = new HTTPStore(url, {
|
|
12
|
+
supportedMethods, fetchOptions: requestInit,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Class method for decoding json from the store.
|
|
17
|
+
* @param {string} key A path to the item.
|
|
18
|
+
* @returns {Promise} This async function returns a promise
|
|
19
|
+
* that resolves to the parsed JSON if successful.
|
|
20
|
+
* @throws This may throw an error.
|
|
21
|
+
*/
|
|
22
|
+
async getJson(key) {
|
|
23
|
+
try {
|
|
24
|
+
const buf = await this.store.getItem(key);
|
|
25
|
+
const text = new TextDecoder('utf-8').decode(buf);
|
|
26
|
+
return JSON.parse(text);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
if (err instanceof KeyError) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loader for string arrays located in anndata.zarr stores.
|
|
3
|
+
*/
|
|
4
|
+
export default class FeatureLabelsAnndataLoader {
|
|
5
|
+
/**
|
|
6
|
+
* Class method for loading feature string labels.
|
|
7
|
+
* @returns {Promise} A promise for the array.
|
|
8
|
+
*/
|
|
9
|
+
loadLabels(): Promise<any>;
|
|
10
|
+
labels: any;
|
|
11
|
+
load(): Promise<any>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=FeatureLabelsAnndataLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FeatureLabelsAnndataLoader.d.ts","sourceRoot":"","sources":["../../src/anndata-loaders/FeatureLabelsAnndataLoader.js"],"names":[],"mappings":"AAEA;;GAEG;AACH;IACE;;;OAGG;IACH,2BAYC;IALG,YAA+C;IAOnD,qBAoBC;CACF"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LoaderResult, AbstractTwoStepLoader, AbstractLoaderError } from '@vitessce/vit-s';
|
|
2
|
+
/**
|
|
3
|
+
* Loader for string arrays located in anndata.zarr stores.
|
|
4
|
+
*/
|
|
5
|
+
export default class FeatureLabelsAnndataLoader extends AbstractTwoStepLoader {
|
|
6
|
+
/**
|
|
7
|
+
* Class method for loading feature string labels.
|
|
8
|
+
* @returns {Promise} A promise for the array.
|
|
9
|
+
*/
|
|
10
|
+
loadLabels() {
|
|
11
|
+
const { path } = this.options;
|
|
12
|
+
if (this.labels) {
|
|
13
|
+
return this.labels;
|
|
14
|
+
}
|
|
15
|
+
if (!this.labels) {
|
|
16
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
17
|
+
this.labels = this.dataSource._loadColumn(path);
|
|
18
|
+
return this.labels;
|
|
19
|
+
}
|
|
20
|
+
this.labels = Promise.resolve(null);
|
|
21
|
+
return this.labels;
|
|
22
|
+
}
|
|
23
|
+
async load() {
|
|
24
|
+
const { path } = this.options;
|
|
25
|
+
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
26
|
+
if (superResult instanceof AbstractLoaderError) {
|
|
27
|
+
return Promise.reject(superResult);
|
|
28
|
+
}
|
|
29
|
+
return Promise.all([
|
|
30
|
+
// Pass in the obsEmbedding path,
|
|
31
|
+
// to handle the MuData case where the obsIndex is located at
|
|
32
|
+
// `mod/rna/index` rather than `index`.
|
|
33
|
+
this.dataSource.loadVarIndex(path),
|
|
34
|
+
this.loadLabels(),
|
|
35
|
+
]).then(([featureIndex, featureLabels]) => Promise.resolve(new LoaderResult({
|
|
36
|
+
featureIndex,
|
|
37
|
+
featureLabels,
|
|
38
|
+
featureLabelsMap: new Map(featureIndex.map((key, i) => ([key, featureLabels[i]]))),
|
|
39
|
+
}, null)));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loader for embedding arrays located in anndata.zarr stores.
|
|
3
|
+
*/
|
|
4
|
+
export default class ObsEmbeddingAnndataLoader {
|
|
5
|
+
/**
|
|
6
|
+
* Class method for loading embedding coordinates, such as those from UMAP or t-SNE.
|
|
7
|
+
* @returns {Promise} A promise for an array of columns.
|
|
8
|
+
*/
|
|
9
|
+
loadEmbedding(): Promise<any>;
|
|
10
|
+
embedding: any;
|
|
11
|
+
load(): Promise<any>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=ObsEmbeddingAnndataLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObsEmbeddingAnndataLoader.d.ts","sourceRoot":"","sources":["../../src/anndata-loaders/ObsEmbeddingAnndataLoader.js"],"names":[],"mappings":"AAIA;;GAEG;AACH;IACE;;;OAGG;IACH,8BAWC;IALG,eAA+D;IAOnE,qBAgBC;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { LoaderResult, AbstractTwoStepLoader, AbstractLoaderError, } from '@vitessce/vit-s';
|
|
2
|
+
/**
|
|
3
|
+
* Loader for embedding arrays located in anndata.zarr stores.
|
|
4
|
+
*/
|
|
5
|
+
export default class ObsEmbeddingAnndataLoader extends AbstractTwoStepLoader {
|
|
6
|
+
/**
|
|
7
|
+
* Class method for loading embedding coordinates, such as those from UMAP or t-SNE.
|
|
8
|
+
* @returns {Promise} A promise for an array of columns.
|
|
9
|
+
*/
|
|
10
|
+
loadEmbedding() {
|
|
11
|
+
const { path, dims = [0, 1] } = this.options;
|
|
12
|
+
if (this.embedding) {
|
|
13
|
+
return this.embedding;
|
|
14
|
+
}
|
|
15
|
+
if (!this.embedding) {
|
|
16
|
+
this.embedding = this.dataSource.loadNumericForDims(path, dims);
|
|
17
|
+
return this.embedding;
|
|
18
|
+
}
|
|
19
|
+
this.embedding = Promise.resolve(null);
|
|
20
|
+
return this.embedding;
|
|
21
|
+
}
|
|
22
|
+
async load() {
|
|
23
|
+
const { path } = this.options;
|
|
24
|
+
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
25
|
+
if (superResult instanceof AbstractLoaderError) {
|
|
26
|
+
return Promise.reject(superResult);
|
|
27
|
+
}
|
|
28
|
+
return Promise.all([
|
|
29
|
+
// Pass in the obsEmbedding path,
|
|
30
|
+
// to handle the MuData case where the obsIndex is located at
|
|
31
|
+
// `mod/rna/index` rather than `index`.
|
|
32
|
+
this.dataSource.loadObsIndex(path),
|
|
33
|
+
this.loadEmbedding(),
|
|
34
|
+
]).then(([obsIndex, obsEmbedding]) => Promise.resolve(new LoaderResult({ obsIndex, obsEmbedding }, null)));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObsEmbeddingAnndataLoader.test.d.ts","sourceRoot":"","sources":["../../src/anndata-loaders/ObsEmbeddingAnndataLoader.test.js"],"names":[],"mappings":""}
|