@vitessce/json 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.
Files changed (89) hide show
  1. package/dist/deflate.40c5a64d.mjs +12 -0
  2. package/dist/index.cd98204c.mjs +146933 -0
  3. package/dist/index.mjs +19 -0
  4. package/dist/jpeg.71d7c24e.mjs +839 -0
  5. package/dist/lerc.1bd33f8e.mjs +1934 -0
  6. package/dist/lzw.55eb0ed4.mjs +127 -0
  7. package/dist/packbits.244c3ff7.mjs +29 -0
  8. package/dist/pako.esm.4b234125.mjs +3940 -0
  9. package/dist/raw.e260117e.mjs +11 -0
  10. package/dist/webimage.9c635930.mjs +31 -0
  11. package/{dist → dist-tsc}/index.js +0 -0
  12. package/package.json +11 -11
  13. package/src/JsonSource.js +21 -0
  14. package/src/index.js +4 -0
  15. package/src/json-loaders/JsonLoader.js +66 -0
  16. package/src/json-loaders/JsonLoader.test.js +28 -0
  17. package/src/json-loaders/ObsSegmentationsJson.js +37 -0
  18. package/src/json-loaders/ObsSetsJson.js +53 -0
  19. package/{dist → src}/json-loaders/ObsSetsJson.test-old.js +0 -1
  20. package/{dist → src}/json-loaders/index.js +0 -0
  21. package/src/legacy-loaders/CellsJsonAsObsEmbedding.js +53 -0
  22. package/src/legacy-loaders/CellsJsonAsObsLabels.js +32 -0
  23. package/src/legacy-loaders/CellsJsonAsObsLocations.js +45 -0
  24. package/src/legacy-loaders/CellsJsonAsObsSegmentations.js +57 -0
  25. package/src/legacy-loaders/CellsJsonLoaders.test.js +68 -0
  26. package/src/legacy-loaders/ClustersJsonAsObsFeatureMatrix.js +55 -0
  27. package/src/legacy-loaders/ClustersJsonLoaders.test.js +33 -0
  28. package/src/legacy-loaders/GenesJsonAsObsFeatureMatrix.js +44 -0
  29. package/src/legacy-loaders/GenesJsonLoaders.test.js +33 -0
  30. package/src/legacy-loaders/MoleculesJsonAsObsLabels.js +39 -0
  31. package/src/legacy-loaders/MoleculesJsonAsObsLocations.js +50 -0
  32. package/src/legacy-loaders/MoleculesJsonLoaders.test.js +46 -0
  33. package/{dist → src}/legacy-loaders/index.js +0 -0
  34. package/src/legacy-loaders/schemas/cells.schema.json +52 -0
  35. package/src/legacy-loaders/schemas/clusters.schema.json +30 -0
  36. package/src/legacy-loaders/schemas/fixtures/cells.bad.json +8 -0
  37. package/src/legacy-loaders/schemas/fixtures/cells.bad.message.json +11 -0
  38. package/src/legacy-loaders/schemas/fixtures/cells.good.json +29 -0
  39. package/src/legacy-loaders/schemas/fixtures/clusters.bad.json +9 -0
  40. package/src/legacy-loaders/schemas/fixtures/clusters.bad.message.json +13 -0
  41. package/src/legacy-loaders/schemas/fixtures/clusters.good.json +9 -0
  42. package/src/legacy-loaders/schemas/fixtures/genes.bad.json +6 -0
  43. package/src/legacy-loaders/schemas/fixtures/genes.bad.message.json +13 -0
  44. package/src/legacy-loaders/schemas/fixtures/genes.good.json +4 -0
  45. package/src/legacy-loaders/schemas/fixtures/molecules.bad.json +7 -0
  46. package/src/legacy-loaders/schemas/fixtures/molecules.bad.message.json +11 -0
  47. package/src/legacy-loaders/schemas/fixtures/molecules.good.json +7 -0
  48. package/src/legacy-loaders/schemas/fixtures/neighborhoods.bad.json +4 -0
  49. package/src/legacy-loaders/schemas/fixtures/neighborhoods.bad.message.json +11 -0
  50. package/src/legacy-loaders/schemas/fixtures/neighborhoods.good.json +4 -0
  51. package/src/legacy-loaders/schemas/genes.schema.json +27 -0
  52. package/src/legacy-loaders/schemas/molecules.schema.json +20 -0
  53. package/src/legacy-loaders/schemas/neighborhoods.schema.json +28 -0
  54. package/src/raster-json-loaders/RasterJsonAsImageLoader.js +45 -0
  55. package/src/raster-json-loaders/RasterJsonAsObsSegmentationsLoader.js +46 -0
  56. package/src/raster-json-loaders/RasterJsonLoader.js +127 -0
  57. package/{dist → src}/raster-json-loaders/index.js +0 -0
  58. package/dist/JsonSource.js +0 -20
  59. package/dist/json-loaders/JsonLoader.js +0 -51
  60. package/dist/json-loaders/JsonLoader.test.js +0 -25
  61. package/dist/json-loaders/ObsSegmentationsJson.js +0 -30
  62. package/dist/json-loaders/ObsSetsJson.js +0 -43
  63. package/dist/json-loaders/ObsSetsJson.test.js +0 -28
  64. package/dist/legacy-loaders/CellsJsonAsObsEmbedding.js +0 -41
  65. package/dist/legacy-loaders/CellsJsonAsObsLabels.js +0 -28
  66. package/dist/legacy-loaders/CellsJsonAsObsLocations.js +0 -39
  67. package/dist/legacy-loaders/CellsJsonAsObsSegmentations.js +0 -55
  68. package/dist/legacy-loaders/CellsJsonLoaders.test.js +0 -66
  69. package/dist/legacy-loaders/ClustersJsonAsObsFeatureMatrix.js +0 -48
  70. package/dist/legacy-loaders/ClustersJsonLoaders.test.js +0 -31
  71. package/dist/legacy-loaders/GenesJsonAsObsFeatureMatrix.js +0 -34
  72. package/dist/legacy-loaders/GenesJsonLoaders.test.js +0 -31
  73. package/dist/legacy-loaders/MoleculesJsonAsObsLabels.js +0 -35
  74. package/dist/legacy-loaders/MoleculesJsonAsObsLocations.js +0 -46
  75. package/dist/legacy-loaders/MoleculesJsonLoaders.test.js +0 -44
  76. package/dist/legacy-loaders/schemas/cells.schema.json +0 -52
  77. package/dist/legacy-loaders/schemas/clusters.schema.json +0 -30
  78. package/dist/legacy-loaders/schemas/fixtures/cells.bad.json +0 -8
  79. package/dist/legacy-loaders/schemas/fixtures/cells.good.json +0 -29
  80. package/dist/legacy-loaders/schemas/fixtures/clusters.good.json +0 -9
  81. package/dist/legacy-loaders/schemas/fixtures/genes.good.json +0 -4
  82. package/dist/legacy-loaders/schemas/fixtures/molecules.good.json +0 -7
  83. package/dist/legacy-loaders/schemas/genes.schema.json +0 -27
  84. package/dist/legacy-loaders/schemas/molecules.schema.json +0 -20
  85. package/dist/legacy-loaders/schemas/neighborhoods.schema.json +0 -28
  86. package/dist/legacy-loaders/schemas/schema.test.js +0 -48
  87. package/dist/raster-json-loaders/RasterJsonAsImageLoader.js +0 -36
  88. package/dist/raster-json-loaders/RasterJsonAsObsSegmentationsLoader.js +0 -37
  89. package/dist/raster-json-loaders/RasterJsonLoader.js +0 -103
@@ -0,0 +1,11 @@
1
+ import { B as BaseDecoder } from "./index.cd98204c.mjs";
2
+ import "@vitessce/vit-s";
3
+ import "react";
4
+ class RawDecoder extends BaseDecoder {
5
+ decodeBlock(buffer) {
6
+ return buffer;
7
+ }
8
+ }
9
+ export {
10
+ RawDecoder as default
11
+ };
@@ -0,0 +1,31 @@
1
+ import { B as BaseDecoder } from "./index.cd98204c.mjs";
2
+ import "@vitessce/vit-s";
3
+ import "react";
4
+ class WebImageDecoder extends BaseDecoder {
5
+ constructor() {
6
+ super();
7
+ if (typeof createImageBitmap === "undefined") {
8
+ throw new Error("Cannot decode WebImage as `createImageBitmap` is not available");
9
+ } else if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
10
+ throw new Error("Cannot decode WebImage as neither `document` nor `OffscreenCanvas` is not available");
11
+ }
12
+ }
13
+ async decode(fileDirectory, buffer) {
14
+ const blob = new Blob([buffer]);
15
+ const imageBitmap = await createImageBitmap(blob);
16
+ let canvas;
17
+ if (typeof document !== "undefined") {
18
+ canvas = document.createElement("canvas");
19
+ canvas.width = imageBitmap.width;
20
+ canvas.height = imageBitmap.height;
21
+ } else {
22
+ canvas = new OffscreenCanvas(imageBitmap.width, imageBitmap.height);
23
+ }
24
+ const ctx = canvas.getContext("2d");
25
+ ctx.drawImage(imageBitmap, 0, 0);
26
+ return ctx.getImageData(0, 0, imageBitmap.width, imageBitmap.height).data.buffer;
27
+ }
28
+ }
29
+ export {
30
+ WebImageDecoder as default
31
+ };
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/json",
3
- "version": "2.0.3-beta.0",
3
+ "version": "2.0.3",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -8,28 +8,28 @@
8
8
  "url": "git+https://github.com/vitessce/vitessce.git"
9
9
  },
10
10
  "license": "MIT",
11
- "main": "dist/index.js",
11
+ "main": "dist/index.mjs",
12
12
  "files": [
13
- "dist"
13
+ "dist",
14
+ "src"
14
15
  ],
15
16
  "dependencies": {
16
17
  "ajv": "^6.10.0",
17
18
  "d3-array": "^2.4.0",
18
19
  "lodash": "^4.17.21",
19
20
  "zarr": "0.5.1",
20
- "@vitessce/constants-internal": "2.0.3-beta.0",
21
- "@vitessce/sets-utils": "2.0.3-beta.0",
22
- "@vitessce/gl": "2.0.3-beta.0",
23
- "@vitessce/utils": "2.0.3-beta.0",
24
- "@vitessce/spatial-utils": "2.0.3-beta.0",
25
- "@vitessce/vit-s": "2.0.3-beta.0"
21
+ "@vitessce/constants-internal": "2.0.3",
22
+ "@vitessce/gl": "2.0.3",
23
+ "@vitessce/sets-utils": "2.0.3",
24
+ "@vitessce/spatial-utils": "2.0.3",
25
+ "@vitessce/utils": "2.0.3",
26
+ "@vitessce/vit-s": "2.0.3"
26
27
  },
27
28
  "devDependencies": {
28
29
  "ajv": "^6.10.0"
29
30
  },
30
31
  "scripts": {
31
- "start": "tsc --watch",
32
- "build": "tsc",
32
+ "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
33
33
  "test": "pnpm exec vitest --run -r ../../../ --dir ."
34
34
  }
35
35
  }
@@ -0,0 +1,21 @@
1
+ /* eslint-disable no-underscore-dangle */
2
+ import { DataSourceFetchError } from '@vitessce/vit-s';
3
+
4
+ export default class JsonSource {
5
+ constructor({ url, requestInit }) {
6
+ this.url = url;
7
+ this.requestInit = requestInit;
8
+ }
9
+
10
+ get data() {
11
+ if (this._data) return this._data;
12
+ this._data = fetch(this.url, this.requestInit).then((response) => {
13
+ if (!response.ok) {
14
+ return Promise.reject(new DataSourceFetchError('JsonSource', this.url, response.headers));
15
+ }
16
+ return response.json();
17
+ // eslint-disable-next-line no-console
18
+ }).catch(() => Promise.reject(new DataSourceFetchError('JsonSource', this.url, {})));
19
+ return this._data;
20
+ }
21
+ }
package/src/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,66 @@
1
+ import Ajv from 'ajv';
2
+ import { FileType } from '@vitessce/constants-internal';
3
+ import {
4
+ AbstractTwoStepLoader,
5
+ LoaderValidationError,
6
+ AbstractLoaderError,
7
+ LoaderResult,
8
+ obsSetsSchema,
9
+ rasterSchema,
10
+ } from '@vitessce/vit-s';
11
+
12
+ import cellsSchema from '../legacy-loaders/schemas/cells.schema.json';
13
+ import moleculesSchema from '../legacy-loaders/schemas/molecules.schema.json';
14
+ import neighborhoodsSchema from '../legacy-loaders/schemas/neighborhoods.schema.json';
15
+
16
+ const fileTypeToSchema = {
17
+ [FileType.CELLS_JSON]: cellsSchema,
18
+ [FileType.MOLECULES_JSON]: moleculesSchema,
19
+ [FileType.NEIGHBORHOODS_JSON]: neighborhoodsSchema,
20
+ [FileType.RASTER_JSON]: rasterSchema,
21
+ [FileType.CELL_SETS_JSON]: obsSetsSchema,
22
+ };
23
+
24
+ export default class JsonLoader extends AbstractTwoStepLoader {
25
+ constructor(dataSource, params) {
26
+ super(dataSource, params);
27
+
28
+ const { fileType } = params;
29
+ this.schema = fileTypeToSchema[fileType];
30
+ }
31
+
32
+ load() {
33
+ const {
34
+ url, type, fileType,
35
+ } = this;
36
+ if (this.data) {
37
+ return this.data;
38
+ }
39
+ this.data = this.dataSource.data
40
+ .then((data) => {
41
+ if (data instanceof AbstractLoaderError) {
42
+ return Promise.reject(data);
43
+ }
44
+ const [valid, reason] = this.validate(data);
45
+ if (valid) {
46
+ return Promise.resolve(new LoaderResult(data, url));
47
+ }
48
+ return Promise.reject(new LoaderValidationError(type, fileType, url, reason));
49
+ });
50
+ return this.data;
51
+ }
52
+
53
+ validate(data) {
54
+ const { schema } = this;
55
+ if (!schema) {
56
+ return [true, null];
57
+ }
58
+ const validate = new Ajv().compile(schema);
59
+ const valid = validate(data);
60
+ let failureReason;
61
+ if (!valid) {
62
+ failureReason = validate.errors;
63
+ }
64
+ return [valid, failureReason];
65
+ }
66
+ }
@@ -0,0 +1,28 @@
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
+
5
+
6
+ describe('loaders/JsonLoader', () => {
7
+ describe('validation against JSON schema', () => {
8
+ it('can validate against a schema when data looks good', () => {
9
+ const loader = new JsonLoader(null, {
10
+ fileType: 'cells.json',
11
+ });
12
+ expect(loader.schema).toBeDefined();
13
+ const [valid, reason] = loader.validate(cellsGoodFixture);
14
+ expect(valid).toBeTruthy();
15
+ expect(reason).toBeUndefined();
16
+ });
17
+
18
+ it('can validate against a schema when data looks bad', () => {
19
+ const loader = new JsonLoader(null, {
20
+ fileType: 'cells.json',
21
+ });
22
+ expect(loader.schema).toBeDefined();
23
+ const [valid, reason] = loader.validate(cellsBadFixture);
24
+ expect(valid).toBeFalsy();
25
+ expect(reason[0].message).toEqual('should NOT have additional properties');
26
+ });
27
+ });
28
+ });
@@ -0,0 +1,37 @@
1
+ import { AbstractLoaderError, LoaderResult, emptySchema } from '@vitessce/vit-s';
2
+ import JsonLoader from './JsonLoader';
3
+
4
+ export default class ObsSegmentationsJsonLoader extends JsonLoader {
5
+ constructor(dataSource, params) {
6
+ super(dataSource, params);
7
+
8
+ this.optionsSchema = emptySchema;
9
+ }
10
+
11
+ loadFromCache(data) {
12
+ if (this.cachedResult) {
13
+ return this.cachedResult;
14
+ }
15
+ const obsIndex = Object.keys(data);
16
+ const obsPolygons = Object.values(data);
17
+ const obsSegmentations = {
18
+ data: obsPolygons,
19
+ shape: [obsPolygons.length, obsPolygons[0].length],
20
+ };
21
+ this.cachedResult = { obsIndex, obsSegmentations, obsSegmentationsType: 'polygon' };
22
+ return this.cachedResult;
23
+ }
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(
33
+ result,
34
+ url,
35
+ ));
36
+ }
37
+ }
@@ -0,0 +1,53 @@
1
+ import {
2
+ tryUpgradeTreeToLatestSchema,
3
+ initializeCellSetColor,
4
+ nodeToSet,
5
+ treeToMembershipMap,
6
+ } from '@vitessce/sets-utils';
7
+ import { obsSetsSchema, AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
8
+ import JsonLoader from './JsonLoader';
9
+
10
+ export default class ObsSetsJsonLoader extends JsonLoader {
11
+ constructor(dataSource, params) {
12
+ super(dataSource, params);
13
+
14
+ this.schema = obsSetsSchema;
15
+ }
16
+
17
+ async load() {
18
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
19
+ if (payload instanceof AbstractLoaderError) {
20
+ return Promise.reject(payload);
21
+ }
22
+ const { data: rawData, url } = payload;
23
+ const datatype = this.fileType.endsWith('cell-sets.json') ? 'cell' : 'obs';
24
+ const upgradedData = tryUpgradeTreeToLatestSchema(rawData, datatype);
25
+ let obsIndex = [];
26
+ const coordinationValues = {
27
+ obsSetSelection: [],
28
+ obsSetColor: [],
29
+ };
30
+ let obsSetsMembership = new Map();
31
+
32
+ // Set up the initial coordination values.
33
+ if (upgradedData && upgradedData.tree.length >= 1) {
34
+ const { tree } = upgradedData;
35
+ obsSetsMembership = treeToMembershipMap(upgradedData);
36
+ const newAutoSetSelectionParentName = tree[0].name;
37
+ // Create a list of set paths to initally select.
38
+ const newAutoSetSelections = tree[0].children
39
+ .map(node => ([newAutoSetSelectionParentName, node.name]));
40
+ // Create a list of cell set objects with color mappings.
41
+ const newAutoSetColors = initializeCellSetColor(upgradedData, []);
42
+ coordinationValues.obsSetSelection = newAutoSetSelections;
43
+ coordinationValues.obsSetColor = newAutoSetColors;
44
+
45
+ obsIndex = nodeToSet(tree[0]).map(d => d[0]);
46
+ }
47
+ return Promise.resolve(new LoaderResult({
48
+ obsIndex,
49
+ obsSets: upgradedData,
50
+ obsSetsMembership,
51
+ }, url, coordinationValues));
52
+ }
53
+ }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  // TODO(monorepo)
3
2
  /*
4
3
  import { LoaderResult } from '@vitessce/vit-s';
File without changes
@@ -0,0 +1,53 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import cellsSchema from './schemas/cells.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+
5
+ export default class CellsJsonAsObsEmbeddingLoader extends JsonLoader {
6
+ constructor(dataSource, params) {
7
+ super(dataSource, params);
8
+
9
+ this.schema = cellsSchema;
10
+ }
11
+
12
+ loadFromCache(data) {
13
+ if (this.cachedResult) {
14
+ return this.cachedResult;
15
+ }
16
+ const { embeddingType } = this.coordinationValues;
17
+ const cellObjs = Object.values(data);
18
+ if (cellObjs.length > 0 && (
19
+ !cellObjs[0].mappings
20
+ || !Array.isArray(cellObjs[0].mappings[embeddingType])
21
+ )) {
22
+ // The cells file does not contain this embedding.
23
+ this.cachedResult = null;
24
+ } else {
25
+ const obsIndex = Object.keys(data);
26
+ const obsEmbeddingX = Float32Array.from(
27
+ cellObjs.map(cellObj => cellObj.mappings[embeddingType][0]),
28
+ );
29
+ const obsEmbeddingY = Float32Array.from(
30
+ cellObjs.map(cellObj => cellObj.mappings[embeddingType][1]),
31
+ );
32
+ const obsEmbedding = {
33
+ data: [obsEmbeddingX, obsEmbeddingY],
34
+ shape: [2, obsEmbeddingX.length],
35
+ };
36
+ this.cachedResult = { obsIndex, obsEmbedding };
37
+ }
38
+ return this.cachedResult;
39
+ }
40
+
41
+ async load() {
42
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
43
+ if (payload instanceof AbstractLoaderError) {
44
+ return Promise.reject(payload);
45
+ }
46
+ const { data, url } = payload;
47
+ const result = this.loadFromCache(data);
48
+ return Promise.resolve(new LoaderResult(
49
+ result,
50
+ url,
51
+ ));
52
+ }
53
+ }
@@ -0,0 +1,32 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import cellsSchema from './schemas/cells.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+
5
+ export default class CellsJsonAsObsLabelsLoader extends JsonLoader {
6
+ constructor(dataSource, params) {
7
+ super(dataSource, params);
8
+
9
+ this.schema = cellsSchema;
10
+ }
11
+
12
+ loadFromCache(data) {
13
+ if (this.cachedResult) {
14
+ return this.cachedResult;
15
+ }
16
+ const { obsLabelsType } = this.coordinationValues;
17
+ const obsIndex = Object.keys(data);
18
+ const obsLabels = Object.values(data).map(cellObj => cellObj.factors[obsLabelsType]);
19
+ this.cachedResult = { obsIndex, obsLabels };
20
+ return this.cachedResult;
21
+ }
22
+
23
+ async load() {
24
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
25
+ if (payload instanceof AbstractLoaderError) {
26
+ return Promise.reject(payload);
27
+ }
28
+ const { data, url } = payload;
29
+ const result = this.loadFromCache(data);
30
+ return Promise.resolve(new LoaderResult(result, url));
31
+ }
32
+ }
@@ -0,0 +1,45 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import cellsSchema from './schemas/cells.schema.json';
3
+ import JsonLoader from '../json-loaders/JsonLoader';
4
+
5
+ export default class CellsJsonAsObsLocationsLoader extends JsonLoader {
6
+ constructor(dataSource, params) {
7
+ super(dataSource, params);
8
+
9
+ this.schema = cellsSchema;
10
+ }
11
+
12
+ loadFromCache(data) {
13
+ if (this.cachedResult) {
14
+ return this.cachedResult;
15
+ }
16
+ const cellObjs = Object.values(data);
17
+ if (cellObjs.length > 0 && !Array.isArray(cellObjs[0].xy)) {
18
+ // This cells file does not contain xy coordinates.
19
+ this.cachedResult = null;
20
+ } else {
21
+ const obsIndex = Object.keys(data);
22
+ const obsLocationsX = Float32Array.from(cellObjs.map(cellObj => cellObj.xy[0]));
23
+ const obsLocationsY = Float32Array.from(cellObjs.map(cellObj => cellObj.xy[1]));
24
+ const obsLocations = {
25
+ data: [obsLocationsX, obsLocationsY],
26
+ shape: [2, obsLocationsX.length],
27
+ };
28
+ this.cachedResult = { obsIndex, obsLocations };
29
+ }
30
+ return this.cachedResult;
31
+ }
32
+
33
+ async load() {
34
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
35
+ if (payload instanceof AbstractLoaderError) {
36
+ return Promise.reject(payload);
37
+ }
38
+ const { data, url } = payload;
39
+ const result = this.loadFromCache(data);
40
+ return Promise.resolve(new LoaderResult(
41
+ result,
42
+ url,
43
+ ));
44
+ }
45
+ }
@@ -0,0 +1,57 @@
1
+ import { AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
2
+ import { DEFAULT_CELLS_LAYER, square } from '@vitessce/spatial-utils';
3
+ import cellsSchema from './schemas/cells.schema.json';
4
+ import JsonLoader from '../json-loaders/JsonLoader';
5
+
6
+ export default class CellsJsonAsObsSegmentationsLoader extends JsonLoader {
7
+ constructor(dataSource, params) {
8
+ super(dataSource, params);
9
+
10
+ this.schema = cellsSchema;
11
+ }
12
+
13
+ loadFromCache(data) {
14
+ if (this.cachedResult) {
15
+ return this.cachedResult;
16
+ }
17
+ const obsIndex = Object.keys(data);
18
+ const cellObjs = Object.values(data);
19
+ if (cellObjs.length > 0 && !Array.isArray(cellObjs[0].poly) && !Array.isArray(cellObjs[0].xy)) {
20
+ // This cells.json file does not have segmentations.
21
+ this.cachedResult = null;
22
+ } else {
23
+ let cellPolygons;
24
+ if (!Array.isArray(cellObjs[0].poly) || !cellObjs[0].poly?.length) {
25
+ // This cells.json file has xy positions but not segmentation polygons.
26
+ const radius = 50;
27
+ // If you want to use a different radius value,
28
+ // then create the polygons upstream and pass them in via .poly
29
+ cellPolygons = cellObjs.map(cellObj => square(cellObj.xy[0], cellObj.xy[1], radius));
30
+ } else {
31
+ cellPolygons = cellObjs.map(cellObj => cellObj.poly);
32
+ }
33
+ const obsSegmentations = {
34
+ data: cellPolygons,
35
+ shape: [cellPolygons.length, cellPolygons[0].length],
36
+ };
37
+ this.cachedResult = { obsIndex, obsSegmentations };
38
+ }
39
+ return this.cachedResult;
40
+ }
41
+
42
+ async load() {
43
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
44
+ if (payload instanceof AbstractLoaderError) {
45
+ return Promise.reject(payload);
46
+ }
47
+ const { data, url } = payload;
48
+ const result = this.loadFromCache(data);
49
+ const coordinationValues = {
50
+ spatialSegmentationLayer: DEFAULT_CELLS_LAYER,
51
+ };
52
+ return Promise.resolve(new LoaderResult({
53
+ ...result,
54
+ obsSegmentationsType: 'polygon',
55
+ }, url, coordinationValues));
56
+ }
57
+ }
@@ -0,0 +1,68 @@
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
+
7
+ const createLoader = (ClassDef, config, url) => {
8
+ const configWithUrl = {
9
+ ...config,
10
+ url,
11
+ };
12
+ const source = new JsonSource(configWithUrl);
13
+ return new ClassDef(source, configWithUrl);
14
+ };
15
+
16
+ describe('loaders/cells-json-loaders', () => {
17
+ describe('CellsJsonAsObsEmbeddingLoader', () => {
18
+ it('can load obsEmbedding', async () => {
19
+ const loader = createLoader(CellsJsonAsObsEmbeddingLoader, {
20
+ fileType: 'obsEmbedding.cells.json',
21
+ coordinationValues: {
22
+ embeddingType: 'PCA',
23
+ },
24
+ }, 'http://localhost:51204/@fixtures/json-legacy/cells.good.json');
25
+ const result = await loader.load();
26
+ expect(result).toBeInstanceOf(LoaderResult);
27
+ const payload = result.data;
28
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsEmbedding']);
29
+ expect(payload.obsIndex).toEqual(['778']);
30
+ expect(Array.from(payload.obsEmbedding.data[0])).toEqual([1]);
31
+ expect(Array.from(payload.obsEmbedding.data[1])).toEqual([2]);
32
+ });
33
+ });
34
+ describe('CellsJsonAsObsLabelsLoader', () => {
35
+ it('can load obsLabels', async () => {
36
+ const loader = createLoader(CellsJsonAsObsLabelsLoader, {
37
+ fileType: 'obsLabels.cells.json',
38
+ coordinationValues: {
39
+ obsLabelsType: 'cluster',
40
+ },
41
+ }, 'http://localhost:51204/@fixtures/json-legacy/cells.good.json');
42
+ const result = await loader.load();
43
+ expect(result).toBeInstanceOf(LoaderResult);
44
+ const payload = result.data;
45
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsLabels']);
46
+ expect(payload.obsIndex).toEqual(['778']);
47
+ expect(payload.obsLabels).toEqual(['Inhibitory CP']);
48
+ });
49
+ });
50
+ describe('CellsJsonAsObsSegmentationsLoader', () => {
51
+ it('can load obsSegmentations', async () => {
52
+ const loader = createLoader(CellsJsonAsObsSegmentationsLoader, {
53
+ fileType: 'obsSegmentations.cells.json',
54
+ }, 'http://localhost:51204/@fixtures/json-legacy/cells.good.json');
55
+ const result = await loader.load();
56
+ expect(result).toBeInstanceOf(LoaderResult);
57
+ const payload = result.data;
58
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'obsSegmentations', 'obsSegmentationsType']);
59
+ expect(payload.obsIndex).toEqual(['778']);
60
+ expect(payload.obsSegmentationsType).toEqual('polygon');
61
+ expect(payload.obsSegmentations.data[0]).toEqual([
62
+ [1, 2],
63
+ [3, 4],
64
+ [5, 6],
65
+ ]);
66
+ });
67
+ });
68
+ });
@@ -0,0 +1,55 @@
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
+
7
+ export default class ClustersJsonAsObsFeatureMatrixLoader extends JsonLoader {
8
+ constructor(dataSource, params) {
9
+ super(dataSource, params);
10
+
11
+ this.schema = clustersSchema;
12
+ }
13
+
14
+ loadFromCache(data) {
15
+ if (this.cachedResult) {
16
+ return this.cachedResult;
17
+ }
18
+ const { rows: featureIndex, cols: obsIndex, matrix } = data;
19
+ const attrs = {
20
+ rows: obsIndex,
21
+ cols: featureIndex,
22
+ };
23
+ const shape = [attrs.rows.length, attrs.cols.length];
24
+ // Normalize values by converting to one-byte integers.
25
+ // Normalize for each gene (column) independently.
26
+ const normalizedMatrix = matrix.map((col) => {
27
+ const [min, max] = extent(col);
28
+ const normalize = d => Math.floor(((d - min) / (max - min)) * 255);
29
+ return col.map(normalize);
30
+ });
31
+ // Transpose the normalized matrix.
32
+ const tNormalizedMatrix = range(shape[0])
33
+ .map(i => range(shape[1]).map(j => normalizedMatrix[j][i]));
34
+ // Flatten the transposed matrix.
35
+ const normalizedFlatMatrix = tNormalizedMatrix.flat();
36
+ // Need to wrap the NestedArray to mock the HTTPStore-based array
37
+ // which returns promises.
38
+ const obsFeatureMatrix = { data: Uint8Array.from(normalizedFlatMatrix) };
39
+ this.cachedResult = { obsIndex, featureIndex, obsFeatureMatrix };
40
+ return this.cachedResult;
41
+ }
42
+
43
+ async load() {
44
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
45
+ if (payload instanceof AbstractLoaderError) {
46
+ return Promise.reject(payload);
47
+ }
48
+ const { data, url } = payload;
49
+ const result = this.loadFromCache(data);
50
+ return Promise.resolve(new LoaderResult(
51
+ result,
52
+ url,
53
+ ));
54
+ }
55
+ }
@@ -0,0 +1,33 @@
1
+ import { LoaderResult } from '@vitessce/vit-s';
2
+ import ClustersJsonAsObsFeatureMatrixLoader from './ClustersJsonAsObsFeatureMatrix';
3
+ import JsonSource from '../JsonSource';
4
+
5
+ const createLoader = (ClassDef, config, url) => {
6
+ const configWithUrl = {
7
+ ...config,
8
+ url,
9
+ };
10
+ const source = new JsonSource(configWithUrl);
11
+ return new ClassDef(source, configWithUrl);
12
+ };
13
+
14
+ describe('loaders/clusters-json-loaders', () => {
15
+ describe('ClustersJsonAsObsFeatureMatrixLoader', () => {
16
+ it('can load obsFeatureMatrix', async () => {
17
+ const loader = createLoader(ClustersJsonAsObsFeatureMatrixLoader, {
18
+ fileType: 'obsFeatureMatrix.clusters.json',
19
+ }, 'http://localhost:51204/@fixtures/json-legacy/clusters.good.json');
20
+ const result = await loader.load();
21
+ expect(result).toBeInstanceOf(LoaderResult);
22
+ const payload = result.data;
23
+ expect(Object.keys(payload)).toEqual(['obsIndex', 'featureIndex', 'obsFeatureMatrix']);
24
+ expect(payload.obsIndex).toEqual(['3572', '1904', '1670']);
25
+ expect(payload.featureIndex).toEqual(['Lamp5', 'Sox10', 'Crhbp']);
26
+ expect(Array.from(payload.obsFeatureMatrix.data)).toEqual([
27
+ 0, 0, 0,
28
+ 127, 127, 127,
29
+ 255, 255, 255,
30
+ ]);
31
+ });
32
+ });
33
+ });