@vitessce/json 2.0.0-beta.0

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.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/dist/JsonSource.js +20 -0
  3. package/dist/index.js +4 -0
  4. package/dist/json-loaders/JsonLoader.js +51 -0
  5. package/dist/json-loaders/JsonLoader.test.js +25 -0
  6. package/dist/json-loaders/ObsSegmentationsJson.js +30 -0
  7. package/dist/json-loaders/ObsSetsJson.js +43 -0
  8. package/dist/json-loaders/ObsSetsJson.test-old.js +32 -0
  9. package/dist/json-loaders/ObsSetsJson.test.js +28 -0
  10. package/dist/json-loaders/index.js +3 -0
  11. package/dist/legacy-loaders/CellsJsonAsObsEmbedding.js +41 -0
  12. package/dist/legacy-loaders/CellsJsonAsObsLabels.js +28 -0
  13. package/dist/legacy-loaders/CellsJsonAsObsLocations.js +39 -0
  14. package/dist/legacy-loaders/CellsJsonAsObsSegmentations.js +55 -0
  15. package/dist/legacy-loaders/CellsJsonLoaders.test.js +66 -0
  16. package/dist/legacy-loaders/ClustersJsonAsObsFeatureMatrix.js +48 -0
  17. package/dist/legacy-loaders/ClustersJsonLoaders.test.js +31 -0
  18. package/dist/legacy-loaders/GenesJsonAsObsFeatureMatrix.js +34 -0
  19. package/dist/legacy-loaders/GenesJsonLoaders.test.js +31 -0
  20. package/dist/legacy-loaders/MoleculesJsonAsObsLabels.js +35 -0
  21. package/dist/legacy-loaders/MoleculesJsonAsObsLocations.js +46 -0
  22. package/dist/legacy-loaders/MoleculesJsonLoaders.test.js +44 -0
  23. package/dist/legacy-loaders/index.js +8 -0
  24. package/dist/legacy-loaders/schemas/cells.schema.json +52 -0
  25. package/dist/legacy-loaders/schemas/clusters.schema.json +30 -0
  26. package/dist/legacy-loaders/schemas/fixtures/cells.bad.json +8 -0
  27. package/dist/legacy-loaders/schemas/fixtures/cells.good.json +29 -0
  28. package/dist/legacy-loaders/schemas/fixtures/clusters.good.json +9 -0
  29. package/dist/legacy-loaders/schemas/fixtures/genes.good.json +4 -0
  30. package/dist/legacy-loaders/schemas/fixtures/molecules.good.json +7 -0
  31. package/dist/legacy-loaders/schemas/genes.schema.json +27 -0
  32. package/dist/legacy-loaders/schemas/molecules.schema.json +20 -0
  33. package/dist/legacy-loaders/schemas/neighborhoods.schema.json +28 -0
  34. package/dist/legacy-loaders/schemas/schema.test.js +48 -0
  35. package/dist/raster-json-loaders/RasterJsonAsImageLoader.js +36 -0
  36. package/dist/raster-json-loaders/RasterJsonAsObsSegmentationsLoader.js +37 -0
  37. package/dist/raster-json-loaders/RasterJsonLoader.js +102 -0
  38. package/dist/raster-json-loaders/index.js +2 -0
  39. package/package.json +34 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,20 @@
1
+ /* eslint-disable no-underscore-dangle */
2
+ import { DataSourceFetchError } from '@vitessce/vit-s';
3
+ export default class JsonSource {
4
+ constructor({ url, requestInit }) {
5
+ this.url = url;
6
+ this.requestInit = requestInit;
7
+ }
8
+ get data() {
9
+ if (this._data)
10
+ return this._data;
11
+ this._data = fetch(this.url, this.requestInit).then((response) => {
12
+ if (!response.ok) {
13
+ return Promise.reject(new DataSourceFetchError('JsonSource', this.url, response.headers));
14
+ }
15
+ return response.json();
16
+ // eslint-disable-next-line no-console
17
+ }).catch(() => Promise.reject(new DataSourceFetchError('JsonSource', this.url, {})));
18
+ return this._data;
19
+ }
20
+ }
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { default as JsonSource } from './JsonSource';
2
+ export * from './json-loaders/index';
3
+ export * from './legacy-loaders/index';
4
+ export * from './raster-json-loaders/index';
@@ -0,0 +1,51 @@
1
+ import Ajv from 'ajv';
2
+ import { FileType } from '@vitessce/constants-internal';
3
+ import { AbstractTwoStepLoader, LoaderValidationError, AbstractLoaderError, LoaderResult, obsSetsSchema, rasterSchema, } from '@vitessce/vit-s';
4
+ import cellsSchema from '../legacy-loaders/schemas/cells.schema.json';
5
+ import moleculesSchema from '../legacy-loaders/schemas/molecules.schema.json';
6
+ import neighborhoodsSchema from '../legacy-loaders/schemas/neighborhoods.schema.json';
7
+ const fileTypeToSchema = {
8
+ [FileType.CELLS_JSON]: cellsSchema,
9
+ [FileType.MOLECULES_JSON]: moleculesSchema,
10
+ [FileType.NEIGHBORHOODS_JSON]: neighborhoodsSchema,
11
+ [FileType.RASTER_JSON]: rasterSchema,
12
+ [FileType.CELL_SETS_JSON]: obsSetsSchema,
13
+ };
14
+ export default class JsonLoader extends AbstractTwoStepLoader {
15
+ constructor(dataSource, params) {
16
+ super(dataSource, params);
17
+ const { fileType } = params;
18
+ this.schema = fileTypeToSchema[fileType];
19
+ }
20
+ load() {
21
+ const { url, type, fileType, } = this;
22
+ if (this.data) {
23
+ return this.data;
24
+ }
25
+ this.data = this.dataSource.data
26
+ .then((data) => {
27
+ if (data instanceof AbstractLoaderError) {
28
+ return Promise.reject(data);
29
+ }
30
+ const [valid, reason] = this.validate(data);
31
+ if (valid) {
32
+ return Promise.resolve(new LoaderResult(data, url));
33
+ }
34
+ return Promise.reject(new LoaderValidationError(type, fileType, url, reason));
35
+ });
36
+ return this.data;
37
+ }
38
+ validate(data) {
39
+ const { schema } = this;
40
+ if (!schema) {
41
+ return [true, null];
42
+ }
43
+ const validate = new Ajv().compile(schema);
44
+ const valid = validate(data);
45
+ let failureReason;
46
+ if (!valid) {
47
+ failureReason = validate.errors;
48
+ }
49
+ return [valid, failureReason];
50
+ }
51
+ }
@@ -0,0 +1,25 @@
1
+ import JsonLoader from './JsonLoader';
2
+ import cellsGoodFixture from '../legacy-loaders/schemas/fixtures/cells.good.json';
3
+ import cellsBadFixture from '../legacy-loaders/schemas/fixtures/cells.bad.json';
4
+ describe('loaders/JsonLoader', () => {
5
+ describe('validation against JSON schema', () => {
6
+ it('can validate against a schema when data looks good', () => {
7
+ const loader = new JsonLoader(null, {
8
+ fileType: 'cells.json',
9
+ });
10
+ expect(loader.schema).toBeDefined();
11
+ const [valid, reason] = loader.validate(cellsGoodFixture);
12
+ expect(valid).toBeTruthy();
13
+ expect(reason).toBeUndefined();
14
+ });
15
+ it('can validate against a schema when data looks bad', () => {
16
+ const loader = new JsonLoader(null, {
17
+ fileType: 'cells.json',
18
+ });
19
+ expect(loader.schema).toBeDefined();
20
+ const [valid, reason] = loader.validate(cellsBadFixture);
21
+ expect(valid).toBeFalsy();
22
+ expect(reason[0].message).toEqual('should NOT have additional properties');
23
+ });
24
+ });
25
+ });
@@ -0,0 +1,30 @@
1
+ import { AbstractLoaderError, LoaderResult, emptySchema } from '@vitessce/vit-s';
2
+ import JsonLoader from './JsonLoader';
3
+ export default class ObsSegmentationsJsonLoader extends JsonLoader {
4
+ constructor(dataSource, params) {
5
+ super(dataSource, params);
6
+ this.optionsSchema = emptySchema;
7
+ }
8
+ loadFromCache(data) {
9
+ if (this.cachedResult) {
10
+ return this.cachedResult;
11
+ }
12
+ const obsIndex = Object.keys(data);
13
+ const obsPolygons = Object.values(data);
14
+ const obsSegmentations = {
15
+ data: obsPolygons,
16
+ shape: [obsPolygons.length, obsPolygons[0].length],
17
+ };
18
+ this.cachedResult = { obsIndex, obsSegmentations, obsSegmentationsType: 'polygon' };
19
+ return this.cachedResult;
20
+ }
21
+ async load() {
22
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
23
+ if (payload instanceof AbstractLoaderError) {
24
+ return Promise.reject(payload);
25
+ }
26
+ const { data, url } = payload;
27
+ const result = this.loadFromCache(data);
28
+ return Promise.resolve(new LoaderResult(result, url));
29
+ }
30
+ }
@@ -0,0 +1,43 @@
1
+ import { tryUpgradeTreeToLatestSchema, initializeCellSetColor, nodeToSet, treeToMembershipMap, } from '@vitessce/sets';
2
+ import { obsSetsSchema, AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
3
+ import JsonLoader from './JsonLoader';
4
+ export default class ObsSetsJsonLoader extends JsonLoader {
5
+ constructor(dataSource, params) {
6
+ super(dataSource, params);
7
+ this.schema = obsSetsSchema;
8
+ }
9
+ async load() {
10
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
11
+ if (payload instanceof AbstractLoaderError) {
12
+ return Promise.reject(payload);
13
+ }
14
+ const { data: rawData, url } = payload;
15
+ const datatype = this.fileType.endsWith('cell-sets.json') ? 'cell' : 'obs';
16
+ const upgradedData = tryUpgradeTreeToLatestSchema(rawData, datatype);
17
+ let obsIndex = [];
18
+ const coordinationValues = {
19
+ obsSetSelection: [],
20
+ obsSetColor: [],
21
+ };
22
+ let obsSetsMembership = new Map();
23
+ // Set up the initial coordination values.
24
+ if (upgradedData && upgradedData.tree.length >= 1) {
25
+ const { tree } = upgradedData;
26
+ obsSetsMembership = treeToMembershipMap(upgradedData);
27
+ const newAutoSetSelectionParentName = tree[0].name;
28
+ // Create a list of set paths to initally select.
29
+ const newAutoSetSelections = tree[0].children
30
+ .map(node => ([newAutoSetSelectionParentName, node.name]));
31
+ // Create a list of cell set objects with color mappings.
32
+ const newAutoSetColors = initializeCellSetColor(upgradedData, []);
33
+ coordinationValues.obsSetSelection = newAutoSetSelections;
34
+ coordinationValues.obsSetColor = newAutoSetColors;
35
+ obsIndex = nodeToSet(tree[0]).map(d => d[0]);
36
+ }
37
+ return Promise.resolve(new LoaderResult({
38
+ obsIndex,
39
+ obsSets: upgradedData,
40
+ obsSetsMembership,
41
+ }, url, coordinationValues));
42
+ }
43
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ // TODO(monorepo)
3
+ /*
4
+ import { LoaderResult } from '@vitessce/vit-s';
5
+ import ObsSetsJsonLoader from './ObsSetsJson';
6
+ import JsonSource from '../JsonSource';
7
+
8
+ const createLoader = (ClassDef, config, url) => {
9
+ const configWithUrl = {
10
+ ...config,
11
+ url,
12
+ };
13
+ const source = new JsonSource(configWithUrl);
14
+ return new ClassDef(source, configWithUrl);
15
+ };
16
+
17
+ describe('loaders/json-loaders/ObsSetsJson', () => {
18
+ describe('ObsSetsJsonLoader', () => {
19
+ it('can load obsSets', async () => {
20
+ const loader = createLoader(ObsSetsJsonLoader, {
21
+ fileType: 'obsSets.cells.json',
22
+ }, 'http://localhost:51204/@fixtures/vit-s/obsSets.good.json');
23
+ const result = await loader.load();
24
+ expect(result).toBeInstanceOf(LoaderResult);
25
+ const payload = result.data;
26
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsSets', 'obsSetsMembership']);
27
+ expect(payload.obsSets.datatype).toEqual('obs');
28
+ expect(payload.obsSets.tree[0].name).toEqual('Clustering Algorithm');
29
+ });
30
+ });
31
+ });
32
+ */
@@ -0,0 +1,28 @@
1
+ export {};
2
+ // TODO(monorepo)
3
+ /*
4
+ const createLoader = (ClassDef, config, url) => {
5
+ const configWithUrl = {
6
+ ...config,
7
+ url,
8
+ };
9
+ const source = new JsonSource(configWithUrl);
10
+ return new ClassDef(source, configWithUrl);
11
+ };
12
+
13
+ describe('loaders/json-loaders/ObsSetsJson', () => {
14
+ describe('ObsSetsJsonLoader', () => {
15
+ it('can load obsSets', async () => {
16
+ const loader = createLoader(ObsSetsJsonLoader, {
17
+ fileType: 'obsSets.cells.json',
18
+ }, 'http://localhost:51204/@fixtures/vit-s/obsSets.good.json');
19
+ const result = await loader.load();
20
+ expect(result).toBeInstanceOf(LoaderResult);
21
+ const payload = result.data;
22
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsSets', 'obsSetsMembership']);
23
+ expect(payload.obsSets.datatype).toEqual('obs');
24
+ expect(payload.obsSets.tree[0].name).toEqual('Clustering Algorithm');
25
+ });
26
+ });
27
+ });
28
+ */
@@ -0,0 +1,3 @@
1
+ export { default as JsonLoader } from './JsonLoader';
2
+ export { default as ObsSegmentationsJsonLoader } from './ObsSegmentationsJson';
3
+ export { default as ObsSetsJsonLoader } from './ObsSetsJson';
@@ -0,0 +1,41 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import cellsSchema from './schemas/cells.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+ export default class CellsJsonAsObsEmbeddingLoader extends JsonLoader {
5
+ constructor(dataSource, params) {
6
+ super(dataSource, params);
7
+ this.schema = cellsSchema;
8
+ }
9
+ loadFromCache(data) {
10
+ if (this.cachedResult) {
11
+ return this.cachedResult;
12
+ }
13
+ const { embeddingType } = this.coordinationValues;
14
+ const cellObjs = Object.values(data);
15
+ if (cellObjs.length > 0 && (!cellObjs[0].mappings
16
+ || !Array.isArray(cellObjs[0].mappings[embeddingType]))) {
17
+ // The cells file does not contain this embedding.
18
+ this.cachedResult = null;
19
+ }
20
+ else {
21
+ const obsIndex = Object.keys(data);
22
+ const obsEmbeddingX = Float32Array.from(cellObjs.map(cellObj => cellObj.mappings[embeddingType][0]));
23
+ const obsEmbeddingY = Float32Array.from(cellObjs.map(cellObj => cellObj.mappings[embeddingType][1]));
24
+ const obsEmbedding = {
25
+ data: [obsEmbeddingX, obsEmbeddingY],
26
+ shape: [2, obsEmbeddingX.length],
27
+ };
28
+ this.cachedResult = { obsIndex, obsEmbedding };
29
+ }
30
+ return this.cachedResult;
31
+ }
32
+ async load() {
33
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
34
+ if (payload instanceof AbstractLoaderError) {
35
+ return Promise.reject(payload);
36
+ }
37
+ const { data, url } = payload;
38
+ const result = this.loadFromCache(data);
39
+ return Promise.resolve(new LoaderResult(result, url));
40
+ }
41
+ }
@@ -0,0 +1,28 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import cellsSchema from './schemas/cells.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+ export default class CellsJsonAsObsLabelsLoader extends JsonLoader {
5
+ constructor(dataSource, params) {
6
+ super(dataSource, params);
7
+ this.schema = cellsSchema;
8
+ }
9
+ loadFromCache(data) {
10
+ if (this.cachedResult) {
11
+ return this.cachedResult;
12
+ }
13
+ const { obsLabelsType } = this.coordinationValues;
14
+ const obsIndex = Object.keys(data);
15
+ const obsLabels = Object.values(data).map(cellObj => cellObj.factors[obsLabelsType]);
16
+ this.cachedResult = { obsIndex, obsLabels };
17
+ return this.cachedResult;
18
+ }
19
+ async load() {
20
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
21
+ if (payload instanceof AbstractLoaderError) {
22
+ return Promise.reject(payload);
23
+ }
24
+ const { data, url } = payload;
25
+ const result = this.loadFromCache(data);
26
+ return Promise.resolve(new LoaderResult(result, url));
27
+ }
28
+ }
@@ -0,0 +1,39 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import cellsSchema from './schemas/cells.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+ export default class CellsJsonAsObsLocationsLoader extends JsonLoader {
5
+ constructor(dataSource, params) {
6
+ super(dataSource, params);
7
+ this.schema = cellsSchema;
8
+ }
9
+ loadFromCache(data) {
10
+ if (this.cachedResult) {
11
+ return this.cachedResult;
12
+ }
13
+ const cellObjs = Object.values(data);
14
+ if (cellObjs.length > 0 && !Array.isArray(cellObjs[0].xy)) {
15
+ // This cells file does not contain xy coordinates.
16
+ this.cachedResult = null;
17
+ }
18
+ else {
19
+ const obsIndex = Object.keys(data);
20
+ const obsLocationsX = Float32Array.from(cellObjs.map(cellObj => cellObj.xy[0]));
21
+ const obsLocationsY = Float32Array.from(cellObjs.map(cellObj => cellObj.xy[1]));
22
+ const obsLocations = {
23
+ data: [obsLocationsX, obsLocationsY],
24
+ shape: [2, obsLocationsX.length],
25
+ };
26
+ this.cachedResult = { obsIndex, obsLocations };
27
+ }
28
+ return this.cachedResult;
29
+ }
30
+ async load() {
31
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
32
+ if (payload instanceof AbstractLoaderError) {
33
+ return Promise.reject(payload);
34
+ }
35
+ const { data, url } = payload;
36
+ const result = this.loadFromCache(data);
37
+ return Promise.resolve(new LoaderResult(result, url));
38
+ }
39
+ }
@@ -0,0 +1,55 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import { DEFAULT_CELLS_LAYER, square } from '@vitessce/gl';
3
+ import cellsSchema from './schemas/cells.schema.json';
4
+ import JsonLoader from '../json-loaders/JsonLoader';
5
+ export default class CellsJsonAsObsSegmentationsLoader extends JsonLoader {
6
+ constructor(dataSource, params) {
7
+ super(dataSource, params);
8
+ this.schema = cellsSchema;
9
+ }
10
+ loadFromCache(data) {
11
+ if (this.cachedResult) {
12
+ return this.cachedResult;
13
+ }
14
+ const obsIndex = Object.keys(data);
15
+ const cellObjs = Object.values(data);
16
+ if (cellObjs.length > 0 && !Array.isArray(cellObjs[0].poly) && !Array.isArray(cellObjs[0].xy)) {
17
+ // This cells.json file does not have segmentations.
18
+ this.cachedResult = null;
19
+ }
20
+ else {
21
+ let cellPolygons;
22
+ if (!Array.isArray(cellObjs[0].poly) || !cellObjs[0].poly?.length) {
23
+ // This cells.json file has xy positions but not segmentation polygons.
24
+ const radius = 50;
25
+ // If you want to use a different radius value,
26
+ // then create the polygons upstream and pass them in via .poly
27
+ cellPolygons = cellObjs.map(cellObj => square(cellObj.xy[0], cellObj.xy[1], radius));
28
+ }
29
+ else {
30
+ cellPolygons = cellObjs.map(cellObj => cellObj.poly);
31
+ }
32
+ const obsSegmentations = {
33
+ data: cellPolygons,
34
+ shape: [cellPolygons.length, cellPolygons[0].length],
35
+ };
36
+ this.cachedResult = { obsIndex, obsSegmentations };
37
+ }
38
+ return this.cachedResult;
39
+ }
40
+ async load() {
41
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
42
+ if (payload instanceof AbstractLoaderError) {
43
+ return Promise.reject(payload);
44
+ }
45
+ const { data, url } = payload;
46
+ const result = this.loadFromCache(data);
47
+ const coordinationValues = {
48
+ spatialSegmentationLayer: DEFAULT_CELLS_LAYER,
49
+ };
50
+ return Promise.resolve(new LoaderResult({
51
+ ...result,
52
+ obsSegmentationsType: 'polygon',
53
+ }, url, coordinationValues));
54
+ }
55
+ }
@@ -0,0 +1,66 @@
1
+ import { LoaderResult } from '@vitessce/vit-s';
2
+ import CellsJsonAsObsEmbeddingLoader from './CellsJsonAsObsEmbedding';
3
+ import CellsJsonAsObsLabelsLoader from './CellsJsonAsObsLabels';
4
+ import CellsJsonAsObsSegmentationsLoader from './CellsJsonAsObsSegmentations';
5
+ import JsonSource from '../JsonSource';
6
+ const createLoader = (ClassDef, config, url) => {
7
+ const configWithUrl = {
8
+ ...config,
9
+ url,
10
+ };
11
+ const source = new JsonSource(configWithUrl);
12
+ return new ClassDef(source, configWithUrl);
13
+ };
14
+ describe('loaders/cells-json-loaders', () => {
15
+ describe('CellsJsonAsObsEmbeddingLoader', () => {
16
+ it('can load obsEmbedding', async () => {
17
+ const loader = createLoader(CellsJsonAsObsEmbeddingLoader, {
18
+ fileType: 'obsEmbedding.cells.json',
19
+ coordinationValues: {
20
+ embeddingType: 'PCA',
21
+ },
22
+ }, 'http://localhost:51204/@fixtures/json-legacy/cells.good.json');
23
+ const result = await loader.load();
24
+ expect(result).toBeInstanceOf(LoaderResult);
25
+ const payload = result.data;
26
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsEmbedding']);
27
+ expect(payload.obsIndex).toEqual(['778']);
28
+ expect(Array.from(payload.obsEmbedding.data[0])).toEqual([1]);
29
+ expect(Array.from(payload.obsEmbedding.data[1])).toEqual([2]);
30
+ });
31
+ });
32
+ describe('CellsJsonAsObsLabelsLoader', () => {
33
+ it('can load obsLabels', async () => {
34
+ const loader = createLoader(CellsJsonAsObsLabelsLoader, {
35
+ fileType: 'obsLabels.cells.json',
36
+ coordinationValues: {
37
+ obsLabelsType: 'cluster',
38
+ },
39
+ }, 'http://localhost:51204/@fixtures/json-legacy/cells.good.json');
40
+ const result = await loader.load();
41
+ expect(result).toBeInstanceOf(LoaderResult);
42
+ const payload = result.data;
43
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsLabels']);
44
+ expect(payload.obsIndex).toEqual(['778']);
45
+ expect(payload.obsLabels).toEqual(['Inhibitory CP']);
46
+ });
47
+ });
48
+ describe('CellsJsonAsObsSegmentationsLoader', () => {
49
+ it('can load obsSegmentations', async () => {
50
+ const loader = createLoader(CellsJsonAsObsSegmentationsLoader, {
51
+ fileType: 'obsSegmentations.cells.json',
52
+ }, 'http://localhost:51204/@fixtures/json-legacy/cells.good.json');
53
+ const result = await loader.load();
54
+ expect(result).toBeInstanceOf(LoaderResult);
55
+ const payload = result.data;
56
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsSegmentations', 'obsSegmentationsType']);
57
+ expect(payload.obsIndex).toEqual(['778']);
58
+ expect(payload.obsSegmentationsType).toEqual('polygon');
59
+ expect(payload.obsSegmentations.data[0]).toEqual([
60
+ [1, 2],
61
+ [3, 4],
62
+ [5, 6],
63
+ ]);
64
+ });
65
+ });
66
+ });
@@ -0,0 +1,48 @@
1
+ import { extent } from 'd3-array';
2
+ import range from 'lodash/range';
3
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
4
+ import clustersSchema from './schemas/clusters.schema.json';
5
+ import JsonLoader from '../json-loaders/JsonLoader';
6
+ export default class ClustersJsonAsObsFeatureMatrixLoader extends JsonLoader {
7
+ constructor(dataSource, params) {
8
+ super(dataSource, params);
9
+ this.schema = clustersSchema;
10
+ }
11
+ loadFromCache(data) {
12
+ if (this.cachedResult) {
13
+ return this.cachedResult;
14
+ }
15
+ const { rows: featureIndex, cols: obsIndex, matrix } = data;
16
+ const attrs = {
17
+ rows: obsIndex,
18
+ cols: featureIndex,
19
+ };
20
+ const shape = [attrs.rows.length, attrs.cols.length];
21
+ // Normalize values by converting to one-byte integers.
22
+ // Normalize for each gene (column) independently.
23
+ const normalizedMatrix = matrix.map((col) => {
24
+ const [min, max] = extent(col);
25
+ const normalize = d => Math.floor(((d - min) / (max - min)) * 255);
26
+ return col.map(normalize);
27
+ });
28
+ // Transpose the normalized matrix.
29
+ const tNormalizedMatrix = range(shape[0])
30
+ .map(i => range(shape[1]).map(j => normalizedMatrix[j][i]));
31
+ // Flatten the transposed matrix.
32
+ const normalizedFlatMatrix = tNormalizedMatrix.flat();
33
+ // Need to wrap the NestedArray to mock the HTTPStore-based array
34
+ // which returns promises.
35
+ const obsFeatureMatrix = { data: Uint8Array.from(normalizedFlatMatrix) };
36
+ this.cachedResult = { obsIndex, featureIndex, obsFeatureMatrix };
37
+ return this.cachedResult;
38
+ }
39
+ async load() {
40
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
41
+ if (payload instanceof AbstractLoaderError) {
42
+ return Promise.reject(payload);
43
+ }
44
+ const { data, url } = payload;
45
+ const result = this.loadFromCache(data);
46
+ return Promise.resolve(new LoaderResult(result, url));
47
+ }
48
+ }
@@ -0,0 +1,31 @@
1
+ import { LoaderResult } from '@vitessce/vit-s';
2
+ import ClustersJsonAsObsFeatureMatrixLoader from './ClustersJsonAsObsFeatureMatrix';
3
+ import JsonSource from '../JsonSource';
4
+ const createLoader = (ClassDef, config, url) => {
5
+ const configWithUrl = {
6
+ ...config,
7
+ url,
8
+ };
9
+ const source = new JsonSource(configWithUrl);
10
+ return new ClassDef(source, configWithUrl);
11
+ };
12
+ describe('loaders/clusters-json-loaders', () => {
13
+ describe('ClustersJsonAsObsFeatureMatrixLoader', () => {
14
+ it('can load obsFeatureMatrix', async () => {
15
+ const loader = createLoader(ClustersJsonAsObsFeatureMatrixLoader, {
16
+ fileType: 'obsFeatureMatrix.clusters.json',
17
+ }, 'http://localhost:51204/@fixtures/json-legacy/clusters.good.json');
18
+ const result = await loader.load();
19
+ expect(result).toBeInstanceOf(LoaderResult);
20
+ const payload = result.data;
21
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'featureIndex', 'obsFeatureMatrix']);
22
+ expect(payload.obsIndex).toEqual(['3572', '1904', '1670']);
23
+ expect(payload.featureIndex).toEqual(['Lamp5', 'Sox10', 'Crhbp']);
24
+ expect(Array.from(payload.obsFeatureMatrix.data)).toEqual([
25
+ 0, 0, 0,
26
+ 127, 127, 127,
27
+ 255, 255, 255,
28
+ ]);
29
+ });
30
+ });
31
+ });
@@ -0,0 +1,34 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import genesSchema from './schemas/genes.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+ export default class GenesJsonAsObsFeatureMatrixLoader extends JsonLoader {
5
+ constructor(dataSource, params) {
6
+ super(dataSource, params);
7
+ this.schema = genesSchema;
8
+ }
9
+ loadFromCache(data) {
10
+ if (this.cachedResult) {
11
+ return this.cachedResult;
12
+ }
13
+ const cols = Object.keys(data);
14
+ const rows = (cols.length > 0 ? Object.keys(data[cols[0]].cells) : []);
15
+ const normalizedFlatMatrix = rows
16
+ .flatMap(cellId => cols.map(geneId => (data[geneId].cells[cellId] / data[geneId].max) * 255));
17
+ // Need to wrap the NestedArray to mock the HTTPStore-based array
18
+ // which returns promises.
19
+ const featureIndex = cols;
20
+ const obsIndex = rows;
21
+ const obsFeatureMatrix = { data: Uint8Array.from(normalizedFlatMatrix) };
22
+ this.cachedResult = { obsIndex, featureIndex, obsFeatureMatrix };
23
+ return this.cachedResult;
24
+ }
25
+ async load() {
26
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
27
+ if (payload instanceof AbstractLoaderError) {
28
+ return Promise.reject(payload);
29
+ }
30
+ const { data, url } = payload;
31
+ const result = this.loadFromCache(data);
32
+ return Promise.resolve(new LoaderResult(result, url));
33
+ }
34
+ }
@@ -0,0 +1,31 @@
1
+ import { LoaderResult } from '@vitessce/vit-s';
2
+ import GenesJsonAsObsFeatureMatrixLoader from './GenesJsonAsObsFeatureMatrix';
3
+ import JsonSource from '../JsonSource';
4
+ const createLoader = (ClassDef, config, url) => {
5
+ const configWithUrl = {
6
+ ...config,
7
+ url,
8
+ };
9
+ const source = new JsonSource(configWithUrl);
10
+ return new ClassDef(source, configWithUrl);
11
+ };
12
+ describe('loaders/genes-json-loaders', () => {
13
+ describe('GenesJsonAsObsFeatureMatrixLoader', () => {
14
+ it('can load obsFeatureMatrix', async () => {
15
+ const loader = createLoader(GenesJsonAsObsFeatureMatrixLoader, {
16
+ fileType: 'obsFeatureMatrix.genes.json',
17
+ }, 'http://localhost:51204/@fixtures/json-legacy/genes.good.json');
18
+ const result = await loader.load();
19
+ expect(result).toBeInstanceOf(LoaderResult);
20
+ const payload = result.data;
21
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'featureIndex', 'obsFeatureMatrix']);
22
+ expect(payload.obsIndex).toEqual(['778', '1409', '3642']);
23
+ expect(payload.featureIndex).toEqual(['Gad2', 'Slc32a1']);
24
+ expect(Array.from(payload.obsFeatureMatrix.data)).toEqual([
25
+ 93, 213,
26
+ 42, 135,
27
+ 255, 255,
28
+ ]);
29
+ });
30
+ });
31
+ });