@vitessce/json 2.0.2 → 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 (85) 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/legacy-loaders/CellsJsonAsObsEmbedding.js +0 -41
  64. package/dist/legacy-loaders/CellsJsonAsObsLabels.js +0 -28
  65. package/dist/legacy-loaders/CellsJsonAsObsLocations.js +0 -39
  66. package/dist/legacy-loaders/CellsJsonAsObsSegmentations.js +0 -55
  67. package/dist/legacy-loaders/CellsJsonLoaders.test.js +0 -66
  68. package/dist/legacy-loaders/ClustersJsonAsObsFeatureMatrix.js +0 -48
  69. package/dist/legacy-loaders/ClustersJsonLoaders.test.js +0 -31
  70. package/dist/legacy-loaders/GenesJsonAsObsFeatureMatrix.js +0 -34
  71. package/dist/legacy-loaders/GenesJsonLoaders.test.js +0 -31
  72. package/dist/legacy-loaders/MoleculesJsonAsObsLabels.js +0 -35
  73. package/dist/legacy-loaders/MoleculesJsonAsObsLocations.js +0 -46
  74. package/dist/legacy-loaders/MoleculesJsonLoaders.test.js +0 -44
  75. package/dist/legacy-loaders/schemas/cells.schema.json +0 -52
  76. package/dist/legacy-loaders/schemas/clusters.schema.json +0 -30
  77. package/dist/legacy-loaders/schemas/fixtures/cells.bad.json +0 -8
  78. package/dist/legacy-loaders/schemas/fixtures/cells.good.json +0 -29
  79. package/dist/legacy-loaders/schemas/genes.schema.json +0 -27
  80. package/dist/legacy-loaders/schemas/molecules.schema.json +0 -20
  81. package/dist/legacy-loaders/schemas/neighborhoods.schema.json +0 -28
  82. package/dist/legacy-loaders/schemas/schema.test.js +0 -48
  83. package/dist/raster-json-loaders/RasterJsonAsImageLoader.js +0 -36
  84. package/dist/raster-json-loaders/RasterJsonAsObsSegmentationsLoader.js +0 -37
  85. package/dist/raster-json-loaders/RasterJsonLoader.js +0 -103
@@ -0,0 +1,127 @@
1
+ import { viv } from '@vitessce/gl';
2
+ import { initializeRasterLayersAndChannels } from '@vitessce/spatial-utils';
3
+ import { openArray } from 'zarr';
4
+ import { rasterSchema, AbstractLoaderError, LoaderResult } from '@vitessce/vit-s';
5
+ import JsonLoader from '../json-loaders/JsonLoader';
6
+
7
+ async function initLoader(imageData) {
8
+ const {
9
+ type, url, metadata, requestInit,
10
+ } = imageData;
11
+ switch (type) {
12
+ case ('zarr'): {
13
+ const {
14
+ dimensions, isPyramid, transform,
15
+ } = metadata || {};
16
+ const labels = dimensions.map(d => d.field);
17
+ let source;
18
+ if (isPyramid) {
19
+ const metadataUrl = `${url}${
20
+ url.slice(-1) === '/' ? '' : '/'
21
+ }.zmetadata`;
22
+ const response = await fetch(metadataUrl);
23
+ const { metadata: zarrMetadata } = await response.json();
24
+ const paths = Object.keys(zarrMetadata)
25
+ .filter(metaKey => metaKey.includes('.zarray'))
26
+ .map(arrMetaKeys => arrMetaKeys.slice(0, -7));
27
+ const data = await Promise.all(
28
+ paths.map(path => openArray({ store: url, path })),
29
+ );
30
+ const [yChunk, xChunk] = data[0].chunks.slice(-2);
31
+ const size = Math.min(yChunk, xChunk);
32
+ // deck.gl requirement for power-of-two tile size.
33
+ const tileSize = 2 ** Math.floor(Math.log2(size));
34
+ source = data.map(d => new viv.ZarrPixelSource(d, labels, tileSize));
35
+ } else {
36
+ const data = await openArray({ store: url });
37
+ source = new viv.ZarrPixelSource(data, labels);
38
+ }
39
+ return { data: source, metadata: { dimensions, transform }, channels: (dimensions.find(d => d.field === 'channel') || dimensions[0]).values };
40
+ }
41
+ case ('ome-tiff'): {
42
+ let loader;
43
+ // Fetch offsets for ome-tiff if needed.
44
+ if (metadata && 'omeTiffOffsetsUrl' in metadata) {
45
+ const { omeTiffOffsetsUrl } = metadata;
46
+ const res = await fetch(omeTiffOffsetsUrl, (requestInit || {}));
47
+ if (res.ok) {
48
+ const offsets = await res.json();
49
+ loader = await viv.loadOmeTiff(
50
+ url,
51
+ {
52
+ offsets,
53
+ headers: requestInit?.headers,
54
+ },
55
+ );
56
+ } else {
57
+ throw new Error(`Offsets not found but provided: ${res.status} from ${res.url}`);
58
+ }
59
+ } else {
60
+ loader = await viv.loadOmeTiff(url, { headers: requestInit?.headers });
61
+ }
62
+ const { Pixels: { Channels } } = loader.metadata;
63
+ const channels = Array.isArray(Channels)
64
+ ? Channels.map((channel, i) => channel.Name || `Channel ${i}`)
65
+ : [Channels.Name || `Channel ${0}`];
66
+ return { ...loader, channels };
67
+ }
68
+ default: {
69
+ throw Error(`Image type (${type}) is not supported`);
70
+ }
71
+ }
72
+ }
73
+
74
+ export default class RasterLoader extends JsonLoader {
75
+ constructor(dataSource, params) {
76
+ const { url, options } = params;
77
+ if (!url && options) {
78
+ // eslint-disable-next-line no-param-reassign
79
+ dataSource.url = URL.createObjectURL(new Blob([JSON.stringify(options)]));
80
+ }
81
+ super(dataSource, params);
82
+ this.schema = rasterSchema;
83
+ }
84
+
85
+ async load() {
86
+ const payload = await super.load().catch(reason => Promise.resolve(reason));
87
+ if (payload instanceof AbstractLoaderError) {
88
+ return Promise.reject(payload);
89
+ }
90
+ const { data: raster } = payload;
91
+ const { images, renderLayers, usePhysicalSizeScaling = false } = raster;
92
+
93
+ // Get image name and URL tuples.
94
+ const urls = images
95
+ .filter(image => !image.url.includes('zarr'))
96
+ .map(image => ([image.url, image.name]));
97
+
98
+ // Add a loaderCreator function for each image layer.
99
+ const imagesWithLoaderCreators = images.map(image => ({
100
+ ...image,
101
+ loaderCreator: async () => initLoader(image),
102
+ }));
103
+
104
+ // TODO: use options for initial selection of channels
105
+ // which omit domain/slider ranges.
106
+ if (!this.autoImageCache) {
107
+ this.autoImageCache = initializeRasterLayersAndChannels(
108
+ imagesWithLoaderCreators,
109
+ renderLayers,
110
+ usePhysicalSizeScaling,
111
+ );
112
+ }
113
+
114
+ return this.autoImageCache.then((autoImages) => {
115
+ const [autoImageLayers, imageLayerLoaders, imageLayerMeta] = autoImages;
116
+
117
+ const coordinationValues = {
118
+ spatialImageLayer: autoImageLayers,
119
+ };
120
+ return new LoaderResult(
121
+ { loaders: imageLayerLoaders, meta: imageLayerMeta },
122
+ urls,
123
+ coordinationValues,
124
+ );
125
+ });
126
+ }
127
+ }
File without changes
@@ -1,20 +0,0 @@
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
- }
@@ -1,51 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
- });
@@ -1,30 +0,0 @@
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
- }
@@ -1,43 +0,0 @@
1
- import { tryUpgradeTreeToLatestSchema, initializeCellSetColor, nodeToSet, treeToMembershipMap, } from '@vitessce/sets-utils';
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
- }
@@ -1,41 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
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
- }
@@ -1,39 +0,0 @@
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
- }
@@ -1,55 +0,0 @@
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
- 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
- }
@@ -1,66 +0,0 @@
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
- });
@@ -1,48 +0,0 @@
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
- }
@@ -1,31 +0,0 @@
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
- });
@@ -1,34 +0,0 @@
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
- }