@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
package/dist/MuDataSource.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import AnnDataSource from './AnnDataSource';
|
|
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
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import range from 'lodash/range';
|
|
2
|
-
import MuDataSource, { getObsPath, getVarPath } from './MuDataSource';
|
|
3
|
-
describe('sources/MuDataSource', () => {
|
|
4
|
-
it('loadObsColumns returns ids for location in store, with joint-modality path', async () => {
|
|
5
|
-
const dataSource = new MuDataSource({
|
|
6
|
-
url: 'http://localhost:51204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
7
|
-
});
|
|
8
|
-
const ids = await dataSource.loadObsColumns(['obs/leiden']);
|
|
9
|
-
expect(ids).toEqual([['3', '2', '3']]);
|
|
10
|
-
});
|
|
11
|
-
it('loadObsColumns returns ids for location in store, with modality-specific path', async () => {
|
|
12
|
-
const dataSource = new MuDataSource({
|
|
13
|
-
url: 'http://localhost:51204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
14
|
-
});
|
|
15
|
-
const ids = await dataSource.loadObsColumns(['mod/rna/obs/leiden']);
|
|
16
|
-
expect(ids).toEqual([['1', '1', '2']]);
|
|
17
|
-
});
|
|
18
|
-
it('loadVarIndex supports modality-specific paths', async () => {
|
|
19
|
-
const dataSource = new MuDataSource({
|
|
20
|
-
url: 'http://localhost:51204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
21
|
-
});
|
|
22
|
-
const names = await dataSource.loadVarIndex('mod/atac/X');
|
|
23
|
-
expect(names).toEqual(range(15).map(i => `peak_${i}`));
|
|
24
|
-
});
|
|
25
|
-
it('loadObsIndex returns names', async () => {
|
|
26
|
-
const dataSource = new MuDataSource({
|
|
27
|
-
url: 'http://localhost:51204/@fixtures/zarr/mudata-0.2/mudata-dense.zarr',
|
|
28
|
-
});
|
|
29
|
-
const names = await dataSource.loadObsIndex('mod/atac/X');
|
|
30
|
-
expect(names).toEqual(['CTG', 'GCA', 'CTT']);
|
|
31
|
-
});
|
|
32
|
-
it('getObsPath regex works', async () => {
|
|
33
|
-
expect(getObsPath('mod/rna/obsm/X_umap')).toEqual('mod/rna/obs');
|
|
34
|
-
expect(getObsPath('mod/rna/X')).toEqual('mod/rna/obs');
|
|
35
|
-
expect(getObsPath('obsm/X_umap')).toEqual('obs');
|
|
36
|
-
});
|
|
37
|
-
it('getVarPath regex works', async () => {
|
|
38
|
-
expect(getVarPath('mod/rna/obsm/X_umap')).toEqual('mod/rna/var');
|
|
39
|
-
expect(getVarPath('mod/rna/X')).toEqual('mod/rna/var');
|
|
40
|
-
expect(getVarPath('obsm/X_umap')).toEqual('var');
|
|
41
|
-
});
|
|
42
|
-
});
|
package/dist/ZarrDataSource.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { LoaderResult, AbstractTwoStepLoader, AbstractLoaderError } from '@vitessce/vit-s';
|
|
2
|
-
const optionsSchema = {
|
|
3
|
-
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
4
|
-
$id: 'https://github.com/vitessce/vitessce/#cells',
|
|
5
|
-
title: 'obsEmbedding.anndata.zarr options',
|
|
6
|
-
type: 'object',
|
|
7
|
-
additionalProperties: false,
|
|
8
|
-
required: ['path'],
|
|
9
|
-
properties: {
|
|
10
|
-
path: { type: 'string' },
|
|
11
|
-
},
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Loader for string arrays located in anndata.zarr stores.
|
|
15
|
-
*/
|
|
16
|
-
export default class FeatureLabelsAnndataLoader extends AbstractTwoStepLoader {
|
|
17
|
-
constructor(dataSource, params) {
|
|
18
|
-
super(dataSource, params);
|
|
19
|
-
this.optionsSchema = optionsSchema;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Class method for loading feature string labels.
|
|
23
|
-
* @returns {Promise} A promise for the array.
|
|
24
|
-
*/
|
|
25
|
-
loadLabels() {
|
|
26
|
-
const { path } = this.options;
|
|
27
|
-
if (this.labels) {
|
|
28
|
-
return this.labels;
|
|
29
|
-
}
|
|
30
|
-
if (!this.labels) {
|
|
31
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
32
|
-
this.labels = this.dataSource._loadColumn(path);
|
|
33
|
-
return this.labels;
|
|
34
|
-
}
|
|
35
|
-
this.labels = Promise.resolve(null);
|
|
36
|
-
return this.labels;
|
|
37
|
-
}
|
|
38
|
-
async load() {
|
|
39
|
-
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
40
|
-
if (superResult instanceof AbstractLoaderError) {
|
|
41
|
-
return Promise.reject(superResult);
|
|
42
|
-
}
|
|
43
|
-
return Promise.all([
|
|
44
|
-
this.dataSource.loadVarIndex(),
|
|
45
|
-
this.loadLabels(),
|
|
46
|
-
]).then(([featureIndex, featureLabels]) => Promise.resolve(new LoaderResult({
|
|
47
|
-
featureIndex,
|
|
48
|
-
featureLabels,
|
|
49
|
-
featureLabelsMap: new Map(featureIndex.map((key, i) => ([key, featureLabels[i]]))),
|
|
50
|
-
}, null)));
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { LoaderResult, AbstractTwoStepLoader, AbstractLoaderError, obsEmbeddingAnndataSchema, } from '@vitessce/vit-s';
|
|
2
|
-
/**
|
|
3
|
-
* Loader for embedding arrays located in anndata.zarr stores.
|
|
4
|
-
*/
|
|
5
|
-
export default class ObsEmbeddingAnndataLoader extends AbstractTwoStepLoader {
|
|
6
|
-
constructor(dataSource, params) {
|
|
7
|
-
super(dataSource, params);
|
|
8
|
-
this.optionsSchema = obsEmbeddingAnndataSchema;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Class method for loading embedding coordinates, such as those from UMAP or t-SNE.
|
|
12
|
-
* @returns {Promise} A promise for an array of columns.
|
|
13
|
-
*/
|
|
14
|
-
loadEmbedding() {
|
|
15
|
-
const { path, dims = [0, 1] } = this.options;
|
|
16
|
-
if (this.embedding) {
|
|
17
|
-
return this.embedding;
|
|
18
|
-
}
|
|
19
|
-
if (!this.embedding) {
|
|
20
|
-
this.embedding = this.dataSource.loadNumericForDims(path, dims);
|
|
21
|
-
return this.embedding;
|
|
22
|
-
}
|
|
23
|
-
this.embedding = Promise.resolve(null);
|
|
24
|
-
return this.embedding;
|
|
25
|
-
}
|
|
26
|
-
async load() {
|
|
27
|
-
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
28
|
-
if (superResult instanceof AbstractLoaderError) {
|
|
29
|
-
return Promise.reject(superResult);
|
|
30
|
-
}
|
|
31
|
-
return Promise.all([
|
|
32
|
-
this.dataSource.loadObsIndex(),
|
|
33
|
-
this.loadEmbedding(),
|
|
34
|
-
]).then(([obsIndex, obsEmbedding]) => Promise.resolve(new LoaderResult({ obsIndex, obsEmbedding }, null)));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/* eslint-disable func-names */
|
|
2
|
-
import { LoaderResult } from '@vitessce/vit-s';
|
|
3
|
-
import ObsEmbeddingAnndataLoader from './ObsEmbeddingAnndataLoader';
|
|
4
|
-
import AnnDataSource from '../AnnDataSource';
|
|
5
|
-
const createMatrixLoader = (url) => {
|
|
6
|
-
const config = {
|
|
7
|
-
url,
|
|
8
|
-
fileType: 'obsEmbedding.anndata.zarr',
|
|
9
|
-
options: {
|
|
10
|
-
path: 'obsm/X_umap',
|
|
11
|
-
},
|
|
12
|
-
coordinationValues: {
|
|
13
|
-
embeddingType: 'UMAP',
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
const source = new AnnDataSource(config);
|
|
17
|
-
return new ObsEmbeddingAnndataLoader(source, config);
|
|
18
|
-
};
|
|
19
|
-
describe('loaders/ObsEmbeddingAnndataLoader', () => {
|
|
20
|
-
it('load returns obsIndex and obsEmbedding', async () => {
|
|
21
|
-
const loader = createMatrixLoader('http://localhost:51204/@fixtures/zarr/anndata-0.7/anndata-dense.zarr');
|
|
22
|
-
const result = await loader.load();
|
|
23
|
-
expect(result).toBeInstanceOf(LoaderResult);
|
|
24
|
-
const payload = result.data;
|
|
25
|
-
expect(Object.keys(payload)).toEqual(['obsIndex', 'obsEmbedding']);
|
|
26
|
-
expect(payload.obsIndex).toEqual(['CTG', 'GCA', 'CTG']);
|
|
27
|
-
expect(payload.obsEmbedding.shape).toEqual([2, 3]);
|
|
28
|
-
expect(Array.from(payload.obsEmbedding.data[0])).toEqual([-1, 0, 1]);
|
|
29
|
-
expect(Array.from(payload.obsEmbedding.data[1])).toEqual([-1, 0, 1]);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
import { openArray, slice } from 'zarr';
|
|
3
|
-
import { extent } from 'd3-array';
|
|
4
|
-
import { LoaderResult, AbstractTwoStepLoader, AbstractLoaderError, obsFeatureMatrixAnndataSchema, } from '@vitessce/vit-s';
|
|
5
|
-
const normalize = (arr) => {
|
|
6
|
-
const [min, max] = extent(arr);
|
|
7
|
-
const ratio = 255 / (max - min);
|
|
8
|
-
const data = new Uint8Array(arr.map(i => Math.floor((i - min) * ratio)));
|
|
9
|
-
return { data };
|
|
10
|
-
};
|
|
11
|
-
const concatenateColumnVectors = (arr) => {
|
|
12
|
-
const numCols = arr.length;
|
|
13
|
-
const numRows = arr[0].length;
|
|
14
|
-
const { BYTES_PER_ELEMENT } = arr[0];
|
|
15
|
-
const view = new DataView(new ArrayBuffer(numCols * numRows * BYTES_PER_ELEMENT));
|
|
16
|
-
const TypedArray = arr[0].constructor;
|
|
17
|
-
const dtype = TypedArray.name.replace('Array', '');
|
|
18
|
-
for (let i = 0; i < numCols; i += 1) {
|
|
19
|
-
for (let j = 0; j < numRows; j += 1) {
|
|
20
|
-
view[`set${dtype}`](BYTES_PER_ELEMENT * (j * numCols + i), arr[i][j], true);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return new TypedArray(view.buffer);
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Loader for converting zarr into the a cell x gene matrix for use in Genes/Heatmap components.
|
|
27
|
-
*/
|
|
28
|
-
export default class ObsFeatureMatrixAnndataLoader extends AbstractTwoStepLoader {
|
|
29
|
-
constructor(dataSource, params) {
|
|
30
|
-
super(dataSource, params);
|
|
31
|
-
this.optionsSchema = obsFeatureMatrixAnndataSchema;
|
|
32
|
-
}
|
|
33
|
-
getOptions() {
|
|
34
|
-
return this.options;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Class method for loading the genes list from AnnData.var,
|
|
38
|
-
* filtered if a there is a `geneFilterZarr` present in the view config.
|
|
39
|
-
* @returns {Promise} A promise for the zarr array contianing the gene names.
|
|
40
|
-
*/
|
|
41
|
-
async loadFilteredGeneNames() {
|
|
42
|
-
if (this.filteredGeneNames) {
|
|
43
|
-
return this.filteredGeneNames;
|
|
44
|
-
}
|
|
45
|
-
const { featureFilterPath: geneFilterZarr, geneAlias } = this.getOptions();
|
|
46
|
-
const getFilterFn = async () => {
|
|
47
|
-
if (!geneFilterZarr)
|
|
48
|
-
return data => data;
|
|
49
|
-
const geneFilter = await this.dataSource.getFlatArrDecompressed(geneFilterZarr);
|
|
50
|
-
return data => data.filter((_, j) => geneFilter[j]);
|
|
51
|
-
};
|
|
52
|
-
const geneNamesPromise = geneAlias
|
|
53
|
-
? this.dataSource.loadVarAlias(geneAlias)
|
|
54
|
-
: this.dataSource.loadVarIndex();
|
|
55
|
-
this.filteredGeneNames = Promise.all([
|
|
56
|
-
geneNamesPromise,
|
|
57
|
-
getFilterFn(),
|
|
58
|
-
]).then(([data, filter]) => filter(data));
|
|
59
|
-
return this.filteredGeneNames;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Class method for loading a filtered subset of the genes list
|
|
63
|
-
* @param {String} filterZarr A location in the zarr store to fetch a boolean array from.
|
|
64
|
-
* @returns {Array} A list of filtered genes.
|
|
65
|
-
*/
|
|
66
|
-
async _getFilteredGenes(filterZarr) {
|
|
67
|
-
const filter = await this.dataSource.getFlatArrDecompressed(filterZarr);
|
|
68
|
-
const geneNames = await this.loadFilteredGeneNames();
|
|
69
|
-
const genes = geneNames.filter((_, i) => filter[i]);
|
|
70
|
-
return genes;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Class method for getting the integer indices of a selection of genes within a list.
|
|
74
|
-
* @param {Array} selection A list of gene names.
|
|
75
|
-
* @returns {Array} A list of integer indices.
|
|
76
|
-
*/
|
|
77
|
-
async _getGeneIndices(selection) {
|
|
78
|
-
const geneNames = await this.loadFilteredGeneNames();
|
|
79
|
-
return selection.map(gene => geneNames.indexOf(gene));
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Class method for getting the number of cells i.e entries in `obs`.
|
|
83
|
-
* @returns {Number} The number of cells.
|
|
84
|
-
*/
|
|
85
|
-
async _getNumCells() {
|
|
86
|
-
const cells = await this.dataSource.loadObsIndex();
|
|
87
|
-
return cells.length;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Class method for getting the number of genes i.e entries in `var`,
|
|
91
|
-
* potentially filtered by `genesFilter`.
|
|
92
|
-
* @returns {Number} The number of genes.
|
|
93
|
-
*/
|
|
94
|
-
async _getNumGenes() {
|
|
95
|
-
const genes = await this.loadFilteredGeneNames();
|
|
96
|
-
return genes.length;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Class method for opening the sparse matrix arrays in zarr.
|
|
100
|
-
* @returns {Array} A list of promises pointing to the indptr, indices, and data of the matrix.
|
|
101
|
-
*/
|
|
102
|
-
async _openSparseArrays() {
|
|
103
|
-
const { path: matrix } = this.getOptions();
|
|
104
|
-
const { store } = this.dataSource;
|
|
105
|
-
if (this.sparseArrays) {
|
|
106
|
-
return this.sparseArrays;
|
|
107
|
-
}
|
|
108
|
-
this.sparseArrays = Promise.all(['indptr', 'indices', 'data'].map(name => openArray({ store, path: `${matrix}/${name}`, mode: 'r' })));
|
|
109
|
-
return this.sparseArrays;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Class method for loading a gene selection from a CSC matrix.
|
|
113
|
-
* @param {Array} selection A list of gene names whose data should be fetched.
|
|
114
|
-
* @returns {Promise} A Promise.all array of promises containing Uint8Arrays, one per selection.
|
|
115
|
-
*/
|
|
116
|
-
async _loadCSCGeneSelection(selection) {
|
|
117
|
-
const indices = await this._getGeneIndices(selection);
|
|
118
|
-
const [indptrArr, indexArr, cellXGeneArr] = await this._openSparseArrays();
|
|
119
|
-
const numCells = await this._getNumCells();
|
|
120
|
-
const { data: cols } = await indptrArr.getRaw(null);
|
|
121
|
-
// If there is not change in the column indexer, then the data is all zeros
|
|
122
|
-
return Promise.all(indices.map(async (index) => {
|
|
123
|
-
const startRowIndex = cols[index];
|
|
124
|
-
const endRowIndex = cols[index + 1];
|
|
125
|
-
const isColumnAllZeros = startRowIndex === endRowIndex;
|
|
126
|
-
const geneData = new Float32Array(numCells).fill(0);
|
|
127
|
-
if (isColumnAllZeros) {
|
|
128
|
-
return geneData;
|
|
129
|
-
}
|
|
130
|
-
const { data: rowIndices } = await indexArr.get([
|
|
131
|
-
slice(startRowIndex, endRowIndex),
|
|
132
|
-
]);
|
|
133
|
-
const { data: cellXGeneData } = await cellXGeneArr.get([
|
|
134
|
-
slice(startRowIndex, endRowIndex),
|
|
135
|
-
]);
|
|
136
|
-
for (let rowIndex = 0; rowIndex < rowIndices.length; rowIndex += 1) {
|
|
137
|
-
geneData[rowIndices[rowIndex]] = cellXGeneData[rowIndex];
|
|
138
|
-
}
|
|
139
|
-
return geneData;
|
|
140
|
-
}));
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Class method for loading a gene selection from a CSR matrix.
|
|
144
|
-
* @param {Array} selection A list of gene names whose data should be fetched.
|
|
145
|
-
* @returns {Promise} A Promise.all array of promises containing Uint8Arrays, one per selection.
|
|
146
|
-
*/
|
|
147
|
-
async _loadCSRGeneSelection(selection) {
|
|
148
|
-
const indices = await this._getGeneIndices(selection);
|
|
149
|
-
const numGenes = await this._getNumGenes();
|
|
150
|
-
const numCells = await this._getNumCells();
|
|
151
|
-
const cellXGene = await this._loadCSRSparseCellXGene();
|
|
152
|
-
return indices.map((index) => {
|
|
153
|
-
const geneData = new Float32Array(numCells).fill(0);
|
|
154
|
-
for (let i = 0; i < numCells; i += 1) {
|
|
155
|
-
geneData[i] = cellXGene[i * numGenes + index];
|
|
156
|
-
}
|
|
157
|
-
return geneData;
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Class method for loading row oriented (CSR) sparse data from zarr.
|
|
162
|
-
*
|
|
163
|
-
* @returns {Object} A { data: Float32Array } contianing the CellXGene matrix.
|
|
164
|
-
*/
|
|
165
|
-
async _loadCSRSparseCellXGene() {
|
|
166
|
-
if (this._sparseMatrix) {
|
|
167
|
-
return this._sparseMatrix;
|
|
168
|
-
}
|
|
169
|
-
this._sparseMatrix = this._openSparseArrays().then(async (sparseArrays) => {
|
|
170
|
-
const { path: matrix } = this.getOptions();
|
|
171
|
-
const { shape } = await this.dataSource.getJson(`${matrix}/.zattrs`);
|
|
172
|
-
const [rows, cols, cellXGene] = await Promise.all(sparseArrays.map(async (arr) => {
|
|
173
|
-
const { data } = await arr.getRaw(null);
|
|
174
|
-
return data;
|
|
175
|
-
}));
|
|
176
|
-
const cellXGeneMatrix = new Float32Array(shape[0] * shape[1]).fill(0);
|
|
177
|
-
let row = 0;
|
|
178
|
-
rows.forEach((_, index) => {
|
|
179
|
-
const rowStart = rows[index];
|
|
180
|
-
const rowEnd = rows[index + 1];
|
|
181
|
-
for (let i = rowStart; i < rowEnd; i += 1) {
|
|
182
|
-
const val = cellXGene[i];
|
|
183
|
-
const col = cols[i];
|
|
184
|
-
cellXGeneMatrix[row * shape[1] + col] = val;
|
|
185
|
-
}
|
|
186
|
-
row += 1;
|
|
187
|
-
});
|
|
188
|
-
return cellXGeneMatrix;
|
|
189
|
-
});
|
|
190
|
-
return this._sparseMatrix;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Class method for loading column oriented (CSC) sparse data from zarr.
|
|
194
|
-
* @returns {Object} A { data: Float32Array } contianing the CellXGene matrix.
|
|
195
|
-
*/
|
|
196
|
-
async _loadCSCSparseCellXGene() {
|
|
197
|
-
if (this._sparseMatrix) {
|
|
198
|
-
return this._sparseMatrix;
|
|
199
|
-
}
|
|
200
|
-
this._sparseMatrix = this._openSparseArrays().then(async (sparseArrays) => {
|
|
201
|
-
const { path: matrix } = this.getOptions();
|
|
202
|
-
const { shape } = await this.dataSource.getJson(`${matrix}/.zattrs`);
|
|
203
|
-
const [cols, rows, cellXGene] = await Promise.all(sparseArrays.map(async (arr) => {
|
|
204
|
-
const { data } = await arr.getRaw(null);
|
|
205
|
-
return data;
|
|
206
|
-
}));
|
|
207
|
-
const cellXGeneMatrix = new Float32Array(shape[0] * shape[1]).fill(0);
|
|
208
|
-
let col = 0;
|
|
209
|
-
cols.forEach((_, index) => {
|
|
210
|
-
const colStart = cols[index];
|
|
211
|
-
const colEnd = cols[index + 1];
|
|
212
|
-
for (let i = colStart; i < colEnd; i += 1) {
|
|
213
|
-
const val = cellXGene[i];
|
|
214
|
-
const row = rows[i];
|
|
215
|
-
cellXGeneMatrix[row * shape[1] + col] = val;
|
|
216
|
-
}
|
|
217
|
-
col += 1;
|
|
218
|
-
});
|
|
219
|
-
return cellXGeneMatrix;
|
|
220
|
-
});
|
|
221
|
-
return this._sparseMatrix;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Class method for loading the cell x gene matrix.
|
|
225
|
-
* @returns {Promise} A promise for the zarr array contianing the cell x gene data.
|
|
226
|
-
*/
|
|
227
|
-
async loadCellXGene() {
|
|
228
|
-
const { store } = this.dataSource;
|
|
229
|
-
if (this.cellXGene) {
|
|
230
|
-
return this.cellXGene;
|
|
231
|
-
}
|
|
232
|
-
const { path: matrix, initialFeatureFilterPath: matrixGeneFilter, } = this.getOptions();
|
|
233
|
-
if (!this._matrixZattrs) {
|
|
234
|
-
this._matrixZattrs = await this.dataSource.getJson(`${matrix}/.zattrs`);
|
|
235
|
-
}
|
|
236
|
-
const encodingType = this._matrixZattrs['encoding-type'];
|
|
237
|
-
if (!matrixGeneFilter) {
|
|
238
|
-
if (encodingType === 'csr_matrix') {
|
|
239
|
-
this.cellXGene = this._loadCSRSparseCellXGene().then(data => normalize(data));
|
|
240
|
-
}
|
|
241
|
-
else if (encodingType === 'csc_matrix') {
|
|
242
|
-
this.cellXGene = this._loadCSCSparseCellXGene().then(data => normalize(data));
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
if (!this.arr) {
|
|
246
|
-
this.arr = openArray({ store, path: matrix, mode: 'r' });
|
|
247
|
-
}
|
|
248
|
-
this.cellXGene = this.arr.then(z => z.getRaw(null).then(({ data }) => normalize(data)));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
else if (encodingType === 'csr_matrix') {
|
|
252
|
-
this.cellXGene = this._loadCSRSparseCellXGene().then(async (cellXGene) => {
|
|
253
|
-
const filteredGenes = await this._getFilteredGenes(matrixGeneFilter);
|
|
254
|
-
const numGenesFiltered = filteredGenes.length;
|
|
255
|
-
const geneNames = await this.loadFilteredGeneNames();
|
|
256
|
-
const numGenes = geneNames.length;
|
|
257
|
-
const numCells = await this._getNumCells();
|
|
258
|
-
const cellXGeneMatrixFiltered = new Float32Array(numCells * numGenesFiltered).fill(0);
|
|
259
|
-
for (let i = 0; i < numGenesFiltered; i += 1) {
|
|
260
|
-
const index = geneNames.indexOf(filteredGenes[i]);
|
|
261
|
-
for (let j = 0; j < numCells; j += 1) {
|
|
262
|
-
cellXGeneMatrixFiltered[j * numGenesFiltered + i] = cellXGene[j * numGenes + index];
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return normalize(cellXGeneMatrixFiltered);
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
const genes = await this._getFilteredGenes(matrixGeneFilter);
|
|
270
|
-
this.cellXGene = this.loadGeneSelection({ selection: genes, shouldNormalize: false })
|
|
271
|
-
.then(({ data }) => (normalize(concatenateColumnVectors(data))));
|
|
272
|
-
}
|
|
273
|
-
return this.cellXGene;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Class method for loading a gene selection.
|
|
277
|
-
* @param {Object} args
|
|
278
|
-
* @param {Array} args.selection A list of gene names whose data should be fetched.
|
|
279
|
-
* @param {Boolean} args.shouldNormalize A list of gene names whose data should be fetched.
|
|
280
|
-
* @returns {Object} { data } containing an array of gene expression data.
|
|
281
|
-
*/
|
|
282
|
-
async loadGeneSelection({ selection, shouldNormalize = true }) {
|
|
283
|
-
const { path: matrix } = this.getOptions();
|
|
284
|
-
const { store } = this.dataSource;
|
|
285
|
-
if (!this._matrixZattrs) {
|
|
286
|
-
this._matrixZattrs = await this.dataSource.getJson(`${matrix}/.zattrs`);
|
|
287
|
-
}
|
|
288
|
-
const encodingType = this._matrixZattrs['encoding-type'];
|
|
289
|
-
let genes;
|
|
290
|
-
if (encodingType === 'csc_matrix') {
|
|
291
|
-
genes = await this._loadCSCGeneSelection(selection);
|
|
292
|
-
}
|
|
293
|
-
else if (encodingType === 'csr_matrix') {
|
|
294
|
-
genes = await this._loadCSRGeneSelection(selection);
|
|
295
|
-
}
|
|
296
|
-
else {
|
|
297
|
-
if (!this.arr) {
|
|
298
|
-
this.arr = openArray({ store, path: matrix, mode: 'r' });
|
|
299
|
-
}
|
|
300
|
-
const indices = await this._getGeneIndices(selection);
|
|
301
|
-
// We can index directly into a normal dense array zarr store via `get`.
|
|
302
|
-
genes = await Promise.all(indices.map(index => this.arr.then(z => z.get([null, index])).then(({ data }) => data)));
|
|
303
|
-
}
|
|
304
|
-
return { data: genes.map(i => (shouldNormalize ? normalize(i).data : i)), url: null };
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Class method for loading only attributes i.e rows and columns
|
|
308
|
-
* @param {Array} selection A list of gene names whose data should be fetched.
|
|
309
|
-
* @returns {Object} { data: { rows, cols }, url } containing row and col labels for the matrix.
|
|
310
|
-
*/
|
|
311
|
-
loadAttrs() {
|
|
312
|
-
return Promise.all([this.dataSource.loadObsIndex(), this.loadFilteredGeneNames()])
|
|
313
|
-
.then((d) => {
|
|
314
|
-
const [cellNames, geneNames] = d;
|
|
315
|
-
const attrs = { rows: cellNames, cols: geneNames };
|
|
316
|
-
return {
|
|
317
|
-
data: attrs,
|
|
318
|
-
url: null,
|
|
319
|
-
};
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
async loadInitialFilteredGeneNames() {
|
|
323
|
-
const filteredGeneNames = await this.loadFilteredGeneNames();
|
|
324
|
-
const { initialFeatureFilterPath: matrixGeneFilterZarr, } = this.getOptions();
|
|
325
|
-
// In order to return the correct gene list with the heatmap data,
|
|
326
|
-
// we need to filter the columns of attrs so it matches the cellXGene data.
|
|
327
|
-
if (matrixGeneFilterZarr) {
|
|
328
|
-
const matrixGeneFilter = await this.dataSource.getFlatArrDecompressed(matrixGeneFilterZarr);
|
|
329
|
-
return filteredGeneNames.filter((_, i) => matrixGeneFilter[i]);
|
|
330
|
-
}
|
|
331
|
-
return filteredGeneNames;
|
|
332
|
-
}
|
|
333
|
-
/* load() {
|
|
334
|
-
return Promise.all([this.loadAttrs(), this.loadCellXGene()]).then(
|
|
335
|
-
async (d) => {
|
|
336
|
-
const [{ data: attrs }, cellXGene] = d;
|
|
337
|
-
const {
|
|
338
|
-
initialFeatureFilterPath: matrixGeneFilterZarr,
|
|
339
|
-
} = this.getOptions();
|
|
340
|
-
// In order to return the correct gene list with the heatmap data,
|
|
341
|
-
// we need to filter the columns of attrs so it matches the cellXGene data.
|
|
342
|
-
if (matrixGeneFilterZarr) {
|
|
343
|
-
const matrixGeneFilter = await this.dataSource.getFlatArrDecompressed(
|
|
344
|
-
matrixGeneFilterZarr,
|
|
345
|
-
);
|
|
346
|
-
attrs.cols = attrs.cols.filter((_, i) => matrixGeneFilter[i]);
|
|
347
|
-
}
|
|
348
|
-
return Promise.resolve(new LoaderResult([attrs, cellXGene], null));
|
|
349
|
-
},
|
|
350
|
-
);
|
|
351
|
-
} */
|
|
352
|
-
async load() {
|
|
353
|
-
const superResult = await super.load().catch(reason => Promise.resolve(reason));
|
|
354
|
-
if (superResult instanceof AbstractLoaderError) {
|
|
355
|
-
return Promise.reject(superResult);
|
|
356
|
-
}
|
|
357
|
-
return Promise.all([
|
|
358
|
-
this.dataSource.loadObsIndex(),
|
|
359
|
-
this.loadInitialFilteredGeneNames(),
|
|
360
|
-
this.loadCellXGene(),
|
|
361
|
-
]).then(([obsIndex, featureIndex, obsFeatureMatrix]) => Promise.resolve(new LoaderResult({ obsIndex, featureIndex, obsFeatureMatrix }, null)));
|
|
362
|
-
}
|
|
363
|
-
}
|