@vitessce/zarr 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.
- package/dist/deflate.9bff78ef.mjs +12 -0
- package/dist/index.ae29d0d3.mjs +139533 -0
- package/dist/index.mjs +18 -0
- package/dist/jpeg.7d8fcd34.mjs +839 -0
- package/dist/lerc.242ab449.mjs +1942 -0
- package/dist/lzw.801ba405.mjs +127 -0
- package/dist/packbits.300e46d9.mjs +29 -0
- package/dist/pako.esm.4b234125.mjs +3940 -0
- package/dist/raw.de2b32d2.mjs +11 -0
- package/dist/webimage.97478b33.mjs +31 -0
- package/{dist → dist-tsc}/index.js +1 -0
- package/package.json +11 -11
- package/src/AnnDataSource.js +270 -0
- package/src/AnnDataSource.test.js +55 -0
- package/src/MuDataSource.js +78 -0
- package/src/MuDataSource.test.js +48 -0
- package/src/ZarrDataSource.js +36 -0
- package/src/anndata-loaders/FeatureLabelsAnndataLoader.js +63 -0
- package/src/anndata-loaders/ObsEmbeddingAnndataLoader.js +48 -0
- package/src/anndata-loaders/ObsEmbeddingAnndataLoader.test.js +79 -0
- package/src/anndata-loaders/ObsFeatureMatrixAnndataLoader.js +387 -0
- package/src/anndata-loaders/ObsFeatureMatrixAnndataLoader.test.js +124 -0
- package/src/anndata-loaders/ObsLabelsAnndataLoader.js +47 -0
- package/src/anndata-loaders/ObsLocationsAnndataLoader.js +45 -0
- package/src/anndata-loaders/ObsSegmentationsAnndataLoader.js +61 -0
- package/src/anndata-loaders/ObsSetsAnndataLoader.js +83 -0
- package/{dist → src}/anndata-loaders/index.js +0 -0
- package/src/genomic-loaders/GenomicProfilesZarrLoader.js +17 -0
- package/{dist → src}/genomic-loaders/index.js +0 -0
- package/src/index.js +7 -0
- package/src/matrix-loaders/MatrixZarrAsObsFeatureMatrix.js +36 -0
- package/{dist → src}/matrix-loaders/index.js +0 -0
- package/src/ome-loaders/OmeZarrLoader.js +81 -0
- package/{dist → src}/ome-loaders/index.js +0 -0
- package/src/utils.js +5 -0
- package/dist/AnnDataSource.js +0 -245
- package/dist/AnnDataSource.test.js +0 -49
- package/dist/MuDataSource.js +0 -70
- package/dist/MuDataSource.test.js +0 -42
- package/dist/ZarrDataSource.js +0 -35
- package/dist/anndata-loaders/FeatureLabelsAnndataLoader.js +0 -52
- package/dist/anndata-loaders/ObsEmbeddingAnndataLoader.js +0 -36
- package/dist/anndata-loaders/ObsEmbeddingAnndataLoader.test.js +0 -31
- package/dist/anndata-loaders/ObsFeatureMatrixAnndataLoader.js +0 -363
- package/dist/anndata-loaders/ObsFeatureMatrixAnndataLoader.test.js +0 -72
- package/dist/anndata-loaders/ObsLabelsAnndataLoader.js +0 -37
- package/dist/anndata-loaders/ObsLocationsAnndataLoader.js +0 -36
- package/dist/anndata-loaders/ObsSegmentationsAnndataLoader.js +0 -51
- package/dist/anndata-loaders/ObsSetsAnndataLoader.js +0 -50
- package/dist/genomic-loaders/GenomicProfilesZarrLoader.js +0 -15
- package/dist/matrix-loaders/MatrixZarrAsObsFeatureMatrix.js +0 -30
- package/dist/ome-loaders/OmeZarrLoader.js +0 -62
- package/dist/utils.js +0 -5
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LoaderResult, AbstractTwoStepLoader, AbstractLoaderError, obsLocationsAnndataSchema,
|
|
3
|
+
} from '@vitessce/vit-s';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Loader for embedding arrays located in anndata.zarr stores.
|
|
7
|
+
*/
|
|
8
|
+
export default class ObsLocationsAnndataLoader extends AbstractTwoStepLoader {
|
|
9
|
+
constructor(dataSource, params) {
|
|
10
|
+
super(dataSource, params);
|
|
11
|
+
this.optionsSchema = obsLocationsAnndataSchema;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Class method for loading embedding coordinates, such as those from UMAP or t-SNE.
|
|
16
|
+
* @returns {Promise} A promise for an array of columns.
|
|
17
|
+
*/
|
|
18
|
+
loadLocations() {
|
|
19
|
+
const { path, dims = [0, 1] } = this.options;
|
|
20
|
+
if (this.locations) {
|
|
21
|
+
return this.locations;
|
|
22
|
+
}
|
|
23
|
+
if (!this.locations) {
|
|
24
|
+
this.locations = this.dataSource.loadNumericForDims(path, dims);
|
|
25
|
+
return this.locations;
|
|
26
|
+
}
|
|
27
|
+
this.locations = Promise.resolve(null);
|
|
28
|
+
return this.locations;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async load() {
|
|
32
|
+
const { path } = this.options;
|
|
33
|
+
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
34
|
+
if (superResult instanceof AbstractLoaderError) {
|
|
35
|
+
return Promise.reject(superResult);
|
|
36
|
+
}
|
|
37
|
+
return Promise.all([
|
|
38
|
+
this.dataSource.loadObsIndex(path),
|
|
39
|
+
this.loadLocations(),
|
|
40
|
+
]).then(([obsIndex, obsLocations]) => Promise.resolve(new LoaderResult(
|
|
41
|
+
{ obsIndex, obsLocations },
|
|
42
|
+
null,
|
|
43
|
+
)));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LoaderResult, AbstractTwoStepLoader, AbstractLoaderError, obsSegmentationsAnndataSchema,
|
|
3
|
+
} from '@vitessce/vit-s';
|
|
4
|
+
import { DEFAULT_CELLS_LAYER } from '@vitessce/spatial-utils';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Loader for embedding arrays located in anndata.zarr stores.
|
|
9
|
+
*/
|
|
10
|
+
export default class ObsSegmentationsAnndataLoader extends AbstractTwoStepLoader {
|
|
11
|
+
constructor(dataSource, params) {
|
|
12
|
+
super(dataSource, params);
|
|
13
|
+
this.optionsSchema = obsSegmentationsAnndataSchema;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Class method for loading embedding coordinates, such as those from UMAP or t-SNE.
|
|
18
|
+
* @returns {Promise} A promise for an array of columns.
|
|
19
|
+
*/
|
|
20
|
+
loadSegmentations() {
|
|
21
|
+
const { path } = this.options;
|
|
22
|
+
if (this.segmentations) {
|
|
23
|
+
return this.segmentations;
|
|
24
|
+
}
|
|
25
|
+
if (!this.segmentations) {
|
|
26
|
+
this.segmentations = this.dataSource.loadNumeric(path).then(arr => ({
|
|
27
|
+
...arr,
|
|
28
|
+
// Bug introduced from DeckGL v8.6.x to v8.8.x:
|
|
29
|
+
// Polygon vertices cannot be passed via Uint32Arrays, which is how they load via Zarr.
|
|
30
|
+
// For now, a workaround is to cast each vertex to a plain Array.
|
|
31
|
+
data: arr.data
|
|
32
|
+
.map(poly => poly.map(vertex => Array.from(vertex))),
|
|
33
|
+
}));
|
|
34
|
+
return this.segmentations;
|
|
35
|
+
}
|
|
36
|
+
this.segmentations = Promise.resolve(null);
|
|
37
|
+
return this.segmentations;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async load() {
|
|
41
|
+
const { path } = this.options;
|
|
42
|
+
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
43
|
+
if (superResult instanceof AbstractLoaderError) {
|
|
44
|
+
return Promise.reject(superResult);
|
|
45
|
+
}
|
|
46
|
+
const coordinationValues = {
|
|
47
|
+
spatialSegmentationLayer: DEFAULT_CELLS_LAYER,
|
|
48
|
+
};
|
|
49
|
+
return Promise.all([
|
|
50
|
+
this.dataSource.loadObsIndex(path),
|
|
51
|
+
this.loadSegmentations(),
|
|
52
|
+
]).then(([obsIndex, obsSegmentations]) => Promise.resolve(new LoaderResult(
|
|
53
|
+
{
|
|
54
|
+
obsIndex,
|
|
55
|
+
obsSegmentations,
|
|
56
|
+
obsSegmentationsType: 'polygon',
|
|
57
|
+
},
|
|
58
|
+
coordinationValues,
|
|
59
|
+
)));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LoaderResult, AbstractTwoStepLoader, AbstractLoaderError, obsSetsAnndataSchema,
|
|
3
|
+
} from '@vitessce/vit-s';
|
|
4
|
+
import {
|
|
5
|
+
initializeCellSetColor,
|
|
6
|
+
treeToMembershipMap,
|
|
7
|
+
dataToCellSetsTree,
|
|
8
|
+
} from '@vitessce/sets-utils';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Loader for converting zarr into the cell sets json schema.
|
|
13
|
+
*/
|
|
14
|
+
export default class ObsSetsAnndataLoader extends AbstractTwoStepLoader {
|
|
15
|
+
constructor(dataSource, params) {
|
|
16
|
+
super(dataSource, params);
|
|
17
|
+
this.optionsSchema = obsSetsAnndataSchema;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
loadObsIndices() {
|
|
21
|
+
const { options } = this;
|
|
22
|
+
const obsIndexPromises = options
|
|
23
|
+
.map(({ path }) => path)
|
|
24
|
+
.map((pathOrPaths) => {
|
|
25
|
+
if (Array.isArray(pathOrPaths)) {
|
|
26
|
+
// The multi-level case, try using the first item to get the obsIndex.
|
|
27
|
+
if (pathOrPaths.length > 0) {
|
|
28
|
+
return this.dataSource.loadObsIndex(pathOrPaths[0]);
|
|
29
|
+
}
|
|
30
|
+
// pathOrPaths should not be of length 0, but if so, fall back to the default obsIndex.
|
|
31
|
+
return this.dataSource.loadObsIndex();
|
|
32
|
+
}
|
|
33
|
+
// The single-level case.
|
|
34
|
+
return this.dataSource.loadObsIndex(pathOrPaths);
|
|
35
|
+
});
|
|
36
|
+
return Promise.all(obsIndexPromises);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
loadCellSetIds() {
|
|
40
|
+
const { options } = this;
|
|
41
|
+
const cellSetZarrLocation = options.map(({ path }) => path);
|
|
42
|
+
return this.dataSource.loadObsColumns(cellSetZarrLocation);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
loadCellSetScores() {
|
|
46
|
+
const { options } = this;
|
|
47
|
+
const cellSetScoreZarrLocation = options.map(option => option.scorePath || undefined);
|
|
48
|
+
return this.dataSource.loadObsColumns(cellSetScoreZarrLocation);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async load() {
|
|
52
|
+
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
53
|
+
if (superResult instanceof AbstractLoaderError) {
|
|
54
|
+
return Promise.reject(superResult);
|
|
55
|
+
}
|
|
56
|
+
if (!this.cachedResult) {
|
|
57
|
+
const { options } = this;
|
|
58
|
+
this.cachedResult = Promise.all([
|
|
59
|
+
this.dataSource.loadObsIndex(),
|
|
60
|
+
this.loadObsIndices(),
|
|
61
|
+
this.loadCellSetIds(),
|
|
62
|
+
this.loadCellSetScores(),
|
|
63
|
+
]).then(data => [data[0], dataToCellSetsTree([data[1], data[2], data[3]], options)]);
|
|
64
|
+
}
|
|
65
|
+
const [obsIndex, obsSets] = await this.cachedResult;
|
|
66
|
+
const obsSetsMembership = treeToMembershipMap(obsSets);
|
|
67
|
+
const coordinationValues = {};
|
|
68
|
+
const { tree } = obsSets;
|
|
69
|
+
const newAutoSetSelectionParentName = tree[0].name;
|
|
70
|
+
// Create a list of set paths to initally select.
|
|
71
|
+
const newAutoSetSelections = tree[0].children.map(node => [
|
|
72
|
+
newAutoSetSelectionParentName,
|
|
73
|
+
node.name,
|
|
74
|
+
]);
|
|
75
|
+
// Create a list of cell set objects with color mappings.
|
|
76
|
+
const newAutoSetColors = initializeCellSetColor(obsSets, []);
|
|
77
|
+
coordinationValues.obsSetSelection = newAutoSetSelections;
|
|
78
|
+
coordinationValues.obsSetColor = newAutoSetColors;
|
|
79
|
+
return Promise.resolve(
|
|
80
|
+
new LoaderResult({ obsIndex, obsSets, obsSetsMembership }, null, coordinationValues),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AbstractTwoStepLoader, LoaderResult } from '@vitessce/vit-s';
|
|
2
|
+
|
|
3
|
+
export default class GenomicProfilesZarrLoader extends AbstractTwoStepLoader {
|
|
4
|
+
loadAttrs() {
|
|
5
|
+
if (this.attrs) {
|
|
6
|
+
return this.attrs;
|
|
7
|
+
}
|
|
8
|
+
this.attrs = this.dataSource.getJson('.zattrs');
|
|
9
|
+
return this.attrs;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
load() {
|
|
13
|
+
const { url } = this;
|
|
14
|
+
return this.loadAttrs()
|
|
15
|
+
.then(attrs => Promise.resolve(new LoaderResult(attrs, url)));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
File without changes
|
package/src/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as AnnDataSource } from './AnnDataSource';
|
|
2
|
+
export { default as MuDataSource } from './MuDataSource';
|
|
3
|
+
export { default as ZarrDataSource } from './ZarrDataSource';
|
|
4
|
+
export * from './matrix-loaders/index';
|
|
5
|
+
export * from './anndata-loaders/index';
|
|
6
|
+
export * from './ome-loaders/index';
|
|
7
|
+
export * from './genomic-loaders/index';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { openArray } from 'zarr';
|
|
2
|
+
import { AbstractTwoStepLoader, LoaderResult } from '@vitessce/vit-s';
|
|
3
|
+
|
|
4
|
+
export default class MatrixZarrAsObsFeatureMatrixLoader extends AbstractTwoStepLoader {
|
|
5
|
+
async loadAttrs() {
|
|
6
|
+
if (this.attrs) {
|
|
7
|
+
return this.attrs;
|
|
8
|
+
}
|
|
9
|
+
this.attrs = {
|
|
10
|
+
data: await this.dataSource.getJson('.zattrs'),
|
|
11
|
+
url: null,
|
|
12
|
+
};
|
|
13
|
+
return this.attrs;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
loadArr() {
|
|
17
|
+
const { store } = this.dataSource;
|
|
18
|
+
if (this.arr) {
|
|
19
|
+
return this.arr;
|
|
20
|
+
}
|
|
21
|
+
this.arr = openArray({ store, path: '/', mode: 'r' }).then(z => new Promise((resolve) => {
|
|
22
|
+
z.getRaw([null, null])
|
|
23
|
+
.then(resolve);
|
|
24
|
+
}));
|
|
25
|
+
return this.arr;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
load() {
|
|
29
|
+
return Promise
|
|
30
|
+
.all([this.loadAttrs(), this.loadArr()])
|
|
31
|
+
.then(([attrs, arr]) => Promise.resolve(new LoaderResult(
|
|
32
|
+
{ obsIndex: attrs.data.rows, featureIndex: attrs.data.cols, obsFeatureMatrix: arr },
|
|
33
|
+
null,
|
|
34
|
+
)));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { viv } from '@vitessce/gl';
|
|
2
|
+
import { initializeRasterLayersAndChannels } from '@vitessce/spatial-utils';
|
|
3
|
+
import { AbstractLoaderError, LoaderResult, AbstractTwoStepLoader } from '@vitessce/vit-s';
|
|
4
|
+
|
|
5
|
+
function hexToRgb(hex) {
|
|
6
|
+
const result = /^#?([A-F\d]{2})([A-F\d]{2})([A-F\d]{2})$/i.exec(hex);
|
|
7
|
+
return [
|
|
8
|
+
parseInt(result[1].toLowerCase(), 16),
|
|
9
|
+
parseInt(result[2].toLowerCase(), 16),
|
|
10
|
+
parseInt(result[3].toLowerCase(), 16),
|
|
11
|
+
];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default class OmeZarrLoader extends AbstractTwoStepLoader {
|
|
15
|
+
async load() {
|
|
16
|
+
const payload = await this.dataSource.getJson('.zattrs').catch(reason => Promise.resolve(reason));
|
|
17
|
+
if (payload instanceof AbstractLoaderError) {
|
|
18
|
+
return Promise.reject(payload);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const loader = await viv.loadOmeZarr(this.url, { fetchOptions: this.requestInit, type: 'multiscales' });
|
|
22
|
+
const { metadata, data } = loader;
|
|
23
|
+
|
|
24
|
+
const { omero } = metadata;
|
|
25
|
+
|
|
26
|
+
if (!omero) {
|
|
27
|
+
console.error('Path for image not valid');
|
|
28
|
+
return Promise.reject(payload);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const { rdefs, channels } = omero;
|
|
32
|
+
|
|
33
|
+
const t = rdefs.defaultT ?? 0;
|
|
34
|
+
const z = rdefs.defaultZ ?? 0;
|
|
35
|
+
|
|
36
|
+
const filterSelection = (sel) => {
|
|
37
|
+
// Remove selection keys for which there is no dimension.
|
|
38
|
+
if (data.length > 0) {
|
|
39
|
+
const nextSel = {};
|
|
40
|
+
// eslint-disable-next-line prefer-destructuring
|
|
41
|
+
const labels = data[0].labels;
|
|
42
|
+
Object.keys(sel).forEach((key) => {
|
|
43
|
+
if (labels.includes(key)) {
|
|
44
|
+
nextSel[key] = sel[key];
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return nextSel;
|
|
48
|
+
}
|
|
49
|
+
return sel;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const imagesWithLoaderCreators = [
|
|
53
|
+
{
|
|
54
|
+
name: omero.name || 'Image',
|
|
55
|
+
channels: channels.map((channel, i) => ({
|
|
56
|
+
selection: filterSelection({ z, t, c: i }),
|
|
57
|
+
slider: [channel.window.start, channel.window.end],
|
|
58
|
+
color: hexToRgb(channel.color),
|
|
59
|
+
})),
|
|
60
|
+
loaderCreator: async () => ({ ...loader, channels: channels.map(c => c.label) }),
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
// TODO: use options for initial selection of channels
|
|
65
|
+
// which omit domain/slider ranges.
|
|
66
|
+
const [
|
|
67
|
+
autoImageLayers, imageLayerLoaders, imageLayerMeta,
|
|
68
|
+
] = await initializeRasterLayersAndChannels(
|
|
69
|
+
imagesWithLoaderCreators, undefined,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const coordinationValues = {
|
|
73
|
+
spatialImageLayer: autoImageLayers,
|
|
74
|
+
};
|
|
75
|
+
return Promise.resolve(new LoaderResult(
|
|
76
|
+
{ image: { loaders: imageLayerLoaders, meta: imageLayerMeta } },
|
|
77
|
+
[],
|
|
78
|
+
coordinationValues,
|
|
79
|
+
));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
File without changes
|
package/src/utils.js
ADDED
package/dist/AnnDataSource.js
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
import { openArray } from 'zarr';
|
|
3
|
-
import range from 'lodash/range';
|
|
4
|
-
import ZarrDataSource from './ZarrDataSource';
|
|
5
|
-
const readFloat32FromUint8 = (bytes) => {
|
|
6
|
-
if (bytes.length !== 4) {
|
|
7
|
-
throw new Error('readFloat32 only takes in length 4 byte buffers');
|
|
8
|
-
}
|
|
9
|
-
return new Int32Array(bytes.buffer)[0];
|
|
10
|
-
};
|
|
11
|
-
const HEADER_LENGTH = 4;
|
|
12
|
-
function dirname(path) {
|
|
13
|
-
const arr = path.split('/');
|
|
14
|
-
arr.pop();
|
|
15
|
-
return arr.join('/');
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Method for decoding text arrays from zarr.
|
|
19
|
-
* Largerly a port of https://github.com/zarr-developers/numcodecs/blob/2c1aff98e965c3c4747d9881d8b8d4aad91adb3a/numcodecs/vlen.pyx#L135-L178
|
|
20
|
-
* @returns {string[]} An array of strings.
|
|
21
|
-
*/
|
|
22
|
-
function parseVlenUtf8(buffer) {
|
|
23
|
-
const decoder = new TextDecoder();
|
|
24
|
-
let data = 0;
|
|
25
|
-
const dataEnd = data + buffer.length;
|
|
26
|
-
const length = readFloat32FromUint8(buffer.slice(data, HEADER_LENGTH));
|
|
27
|
-
if (buffer.length < HEADER_LENGTH) {
|
|
28
|
-
throw new Error('corrupt buffer, missing or truncated header');
|
|
29
|
-
}
|
|
30
|
-
data += HEADER_LENGTH;
|
|
31
|
-
const output = new Array(length);
|
|
32
|
-
for (let i = 0; i < length; i += 1) {
|
|
33
|
-
if (data + 4 > dataEnd) {
|
|
34
|
-
throw new Error('corrupt buffer, data seem truncated');
|
|
35
|
-
}
|
|
36
|
-
const l = readFloat32FromUint8(buffer.slice(data, data + 4));
|
|
37
|
-
data += 4;
|
|
38
|
-
if (data + l > dataEnd) {
|
|
39
|
-
throw new Error('corrupt buffer, data seem truncated');
|
|
40
|
-
}
|
|
41
|
-
output[i] = decoder.decode(buffer.slice(data, data + l));
|
|
42
|
-
data += l;
|
|
43
|
-
}
|
|
44
|
-
return output;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* A base AnnData loader which has all shared methods for more comlpex laoders,
|
|
48
|
-
* like loading cell names and ids. It inherits from AbstractLoader.
|
|
49
|
-
*/
|
|
50
|
-
export default class AnnDataSource extends ZarrDataSource {
|
|
51
|
-
constructor(...args) {
|
|
52
|
-
super(...args);
|
|
53
|
-
/** @type {Map<string, Promise<string[]>} */
|
|
54
|
-
this.promises = new Map();
|
|
55
|
-
}
|
|
56
|
-
loadObsColumns(paths) {
|
|
57
|
-
return this._loadColumns(paths);
|
|
58
|
-
}
|
|
59
|
-
loadVarColumns(paths) {
|
|
60
|
-
return this._loadColumns(paths);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Class method for loading obs variables.
|
|
64
|
-
* Takes the location as an argument because this is shared across objects,
|
|
65
|
-
* which have different ways of specifying location.
|
|
66
|
-
* @param {string[]} paths An array of strings like "obs/leiden" or "obs/bulk_labels."
|
|
67
|
-
* @returns {Promise} A promise for an array of ids with one per cell.
|
|
68
|
-
*/
|
|
69
|
-
_loadColumns(paths) {
|
|
70
|
-
const promises = paths.map((path) => {
|
|
71
|
-
const getCol = (col) => {
|
|
72
|
-
if (!this.promises.has(col)) {
|
|
73
|
-
const obsPromise = this._loadColumn(col).catch((err) => {
|
|
74
|
-
// clear from cache if promise rejects
|
|
75
|
-
this.promises.delete(col);
|
|
76
|
-
// propagate error
|
|
77
|
-
throw err;
|
|
78
|
-
});
|
|
79
|
-
this.promises.set(col, obsPromise);
|
|
80
|
-
}
|
|
81
|
-
return this.promises.get(col);
|
|
82
|
-
};
|
|
83
|
-
if (!path) {
|
|
84
|
-
return Promise.resolve(undefined);
|
|
85
|
-
}
|
|
86
|
-
if (Array.isArray(path)) {
|
|
87
|
-
return Promise.resolve(Promise.all(path.map(getCol)));
|
|
88
|
-
}
|
|
89
|
-
return getCol(path);
|
|
90
|
-
});
|
|
91
|
-
return Promise.all(promises);
|
|
92
|
-
}
|
|
93
|
-
async _loadColumn(path) {
|
|
94
|
-
const { store } = this;
|
|
95
|
-
const prefix = dirname(path);
|
|
96
|
-
const { categories, 'encoding-type': encodingType } = await this.getJson(`${path}/.zattrs`);
|
|
97
|
-
let categoriesValues;
|
|
98
|
-
let codes;
|
|
99
|
-
if (categories) {
|
|
100
|
-
const { dtype } = await this.getJson(`/${prefix}/${categories}/.zarray`);
|
|
101
|
-
if (dtype === '|O') {
|
|
102
|
-
categoriesValues = await this.getFlatArrDecompressed(`/${prefix}/${categories}`);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else if (encodingType === 'categorical') {
|
|
106
|
-
const { dtype } = await this.getJson(`/${path}/categories/.zarray`);
|
|
107
|
-
if (dtype === '|O') {
|
|
108
|
-
categoriesValues = await this.getFlatArrDecompressed(`/${path}/categories`);
|
|
109
|
-
}
|
|
110
|
-
codes = `/${path}/codes`;
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
const { dtype } = await this.getJson(`/${path}/.zarray`);
|
|
114
|
-
if (dtype === '|O') {
|
|
115
|
-
return this.getFlatArrDecompressed(path);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
const arr = await openArray({ store, path: codes || path, mode: 'r' });
|
|
119
|
-
const values = await arr.get();
|
|
120
|
-
const { data } = values;
|
|
121
|
-
const mappedValues = Array.from(data).map(i => (!categoriesValues ? String(i) : categoriesValues[i]));
|
|
122
|
-
return mappedValues;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Class method for loading general numeric arrays.
|
|
126
|
-
* @param {string} path A string like obsm.X_pca.
|
|
127
|
-
* @returns {Promise} A promise for a zarr array containing the data.
|
|
128
|
-
*/
|
|
129
|
-
loadNumeric(path) {
|
|
130
|
-
const { store } = this;
|
|
131
|
-
return openArray({
|
|
132
|
-
store,
|
|
133
|
-
path,
|
|
134
|
-
mode: 'r',
|
|
135
|
-
}).then(arr => arr.get());
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Class method for loading specific columns of numeric arrays.
|
|
139
|
-
* @param {string} path A string like obsm.X_pca.
|
|
140
|
-
* @param {number[]} dims The column indices to load.
|
|
141
|
-
* @returns {Promise} A promise for a zarr array containing the data.
|
|
142
|
-
*/
|
|
143
|
-
loadNumericForDims(path, dims) {
|
|
144
|
-
const { store } = this;
|
|
145
|
-
const arr = openArray({
|
|
146
|
-
store,
|
|
147
|
-
path,
|
|
148
|
-
mode: 'r',
|
|
149
|
-
});
|
|
150
|
-
return Promise.all(dims.map(dim => arr.then(loadedArr => loadedArr.get([null, dim])))).then(cols => ({
|
|
151
|
-
data: cols.map(col => col.data),
|
|
152
|
-
shape: [dims.length, cols[0].shape[0]],
|
|
153
|
-
}));
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* A common method for loading flattened data
|
|
157
|
-
* i.e that which has shape [n] where n is a natural number.
|
|
158
|
-
* @param {string} path A path to a flat array location, like obs/_index
|
|
159
|
-
* @returns {Array} The data from the zarr array.
|
|
160
|
-
*/
|
|
161
|
-
getFlatArrDecompressed(path) {
|
|
162
|
-
const { store } = this;
|
|
163
|
-
return openArray({
|
|
164
|
-
store,
|
|
165
|
-
path,
|
|
166
|
-
mode: 'r',
|
|
167
|
-
}).then(async (z) => {
|
|
168
|
-
let data;
|
|
169
|
-
const parseAndMergeTextBytes = (dbytes) => {
|
|
170
|
-
const text = parseVlenUtf8(dbytes);
|
|
171
|
-
if (!data) {
|
|
172
|
-
data = text;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
data = data.concat(text);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
const mergeBytes = (dbytes) => {
|
|
179
|
-
if (!data) {
|
|
180
|
-
data = dbytes;
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
const tmp = new Uint8Array(dbytes.buffer.byteLength + data.buffer.byteLength);
|
|
184
|
-
tmp.set(new Uint8Array(data.buffer), 0);
|
|
185
|
-
tmp.set(dbytes, data.buffer.byteLength);
|
|
186
|
-
data = tmp;
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
const numRequests = Math.ceil(z.meta.shape[0] / z.meta.chunks[0]);
|
|
190
|
-
const requests = range(numRequests).map(async (item) => store
|
|
191
|
-
.getItem(`${z.keyPrefix}${String(item)}`)
|
|
192
|
-
.then(buf => z.compressor.then(compressor => compressor.decode(buf))));
|
|
193
|
-
const dbytesArr = await Promise.all(requests);
|
|
194
|
-
dbytesArr.forEach((dbytes) => {
|
|
195
|
-
// Use vlenutf-8 decoding if necessary and merge `data` as a normal array.
|
|
196
|
-
if (Array.isArray(z.meta.filters)
|
|
197
|
-
&& z.meta.filters[0].id === 'vlen-utf8') {
|
|
198
|
-
parseAndMergeTextBytes(dbytes);
|
|
199
|
-
// Otherwise just merge the bytes as a typed array.
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
mergeBytes(dbytes);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
const { meta: { shape: [length], }, } = z;
|
|
206
|
-
// truncate the filled in values
|
|
207
|
-
return data.slice(0, length);
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Class method for loading the obs index.
|
|
212
|
-
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
213
|
-
*/
|
|
214
|
-
loadObsIndex() {
|
|
215
|
-
if (this.obsIndex) {
|
|
216
|
-
return this.obsIndex;
|
|
217
|
-
}
|
|
218
|
-
this.obsIndex = this.getJson('obs/.zattrs').then(({ _index }) => this.getFlatArrDecompressed(`/obs/${_index}`));
|
|
219
|
-
return this.obsIndex;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Class method for loading the var index.
|
|
223
|
-
* @returns {Promise} An promise for a zarr array containing the indices.
|
|
224
|
-
*/
|
|
225
|
-
loadVarIndex() {
|
|
226
|
-
if (this.varIndex) {
|
|
227
|
-
return this.varIndex;
|
|
228
|
-
}
|
|
229
|
-
this.varIndex = this.getJson('var/.zattrs').then(({ _index }) => this.getFlatArrDecompressed(`/var/${_index}`));
|
|
230
|
-
return this.varIndex;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Class method for loading the var alias.
|
|
234
|
-
* @returns {Promise} An promise for a zarr array containing the aliased names.
|
|
235
|
-
*/
|
|
236
|
-
async loadVarAlias(varPath) {
|
|
237
|
-
if (this.varAlias) {
|
|
238
|
-
return this.varAlias;
|
|
239
|
-
}
|
|
240
|
-
[this.varAlias] = await this.loadVarColumns([varPath]);
|
|
241
|
-
const index = await this.loadVarIndex();
|
|
242
|
-
this.varAlias = this.varAlias.map((val, ind) => (val ? val.concat(` (${index[ind]})`) : index[ind]));
|
|
243
|
-
return this.varAlias;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import AnnDataSource from './AnnDataSource';
|
|
2
|
-
describe('sources/AnnDataSource', () => {
|
|
3
|
-
describe('AnnData v0.7', () => {
|
|
4
|
-
it('getJson returns json', async () => {
|
|
5
|
-
const dataSource = new AnnDataSource({
|
|
6
|
-
url: 'http://localhost:51204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr',
|
|
7
|
-
});
|
|
8
|
-
const zGroup = await dataSource.getJson('.zgroup');
|
|
9
|
-
expect(zGroup.zarr_format).toEqual(2);
|
|
10
|
-
});
|
|
11
|
-
it('loadObsColumns returns ids for location in store', async () => {
|
|
12
|
-
const dataSource = new AnnDataSource({
|
|
13
|
-
url: 'http://localhost:51204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr',
|
|
14
|
-
});
|
|
15
|
-
const ids = await dataSource.loadObsColumns(['obs/leiden']);
|
|
16
|
-
expect(ids).toEqual([['1', '1', '2']]);
|
|
17
|
-
});
|
|
18
|
-
it('loadObsIndex returns names', async () => {
|
|
19
|
-
const dataSource = new AnnDataSource({
|
|
20
|
-
url: 'http://localhost:51204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr',
|
|
21
|
-
});
|
|
22
|
-
const names = await dataSource.loadObsIndex();
|
|
23
|
-
expect(names).toEqual(['CTG', 'GCA', 'CTG']);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
describe('AnnData v0.8', () => {
|
|
27
|
-
it('getJson returns json', async () => {
|
|
28
|
-
const dataSource = new AnnDataSource({
|
|
29
|
-
url: 'http://localhost:51204/@fixtures/zarr/anndata-0.8/anndata-dense.zarr',
|
|
30
|
-
});
|
|
31
|
-
const zGroup = await dataSource.getJson('.zgroup');
|
|
32
|
-
expect(zGroup.zarr_format).toEqual(2);
|
|
33
|
-
});
|
|
34
|
-
it('loadObsColumns returns ids for location in store', async () => {
|
|
35
|
-
const dataSource = new AnnDataSource({
|
|
36
|
-
url: 'http://localhost:51204/@fixtures/zarr/anndata-0.8/anndata-dense.zarr',
|
|
37
|
-
});
|
|
38
|
-
const ids = await dataSource.loadObsColumns(['obs/leiden']);
|
|
39
|
-
expect(ids).toEqual([['1', '1', '2']]);
|
|
40
|
-
});
|
|
41
|
-
it('loadObsIndex returns names', async () => {
|
|
42
|
-
const dataSource = new AnnDataSource({
|
|
43
|
-
url: 'http://localhost:51204/@fixtures/zarr/anndata-0.8/anndata-dense.zarr',
|
|
44
|
-
});
|
|
45
|
-
const names = await dataSource.loadObsIndex();
|
|
46
|
-
expect(names).toEqual(['CTG', 'GCA', 'CTG']);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|