@vitessce/scatterplot-embedding 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.
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,18 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import React from 'react';
3
+ import TableCell from '@material-ui/core/TableCell';
4
+ import TableRow from '@material-ui/core/TableRow';
5
+ import { usePlotOptionsStyles, OptionSelect } from '@vitessce/vit-s';
6
+ export default function EmbeddingScatterplotOptions(props) {
7
+ const { mappingSelectEnabled, mappings, selectedMapping, setSelectedMapping, } = props;
8
+ const classes = usePlotOptionsStyles();
9
+ // Handlers for custom option field changes.
10
+ const handleSelectedMappingChange = (event) => {
11
+ setSelectedMapping(event.target.value);
12
+ };
13
+ return mappingSelectEnabled
14
+ ? (_jsxs(TableRow, { children: [_jsx(TableCell, { className: classes.labelCell, children: "Embedding Type" }), _jsx(TableCell, { className: classes.inputCell, children: _jsx(OptionSelect, { className: classes.select, value: selectedMapping, onChange: handleSelectedMappingChange, inputProps: {
15
+ id: 'scatterplot-mapping-select',
16
+ }, children: mappings.map(name => (_jsx("option", { value: name, children: name }, name))) }, "scatterplot-mapping-select") })] }, "mapping-option-row"))
17
+ : null;
18
+ }
@@ -0,0 +1,153 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import React, { useState, useEffect, useCallback, useMemo, } from 'react';
3
+ import { extent } from 'd3-array';
4
+ import isEqual from 'lodash/isEqual';
5
+ import plur from 'plur';
6
+ import { TitleInfo, registerPluginViewType, useReady, useUrls, useDeckCanvasSize, useExpressionValueGetter, useGetObsInfo, useObsEmbeddingData, useObsSetsData, useFeatureSelection, useObsFeatureMatrixIndices, useMultiObsLabels, useCoordination, useLoaders, useSetComponentHover, useSetComponentViewInfo, } from '@vitessce/vit-s';
7
+ import { setObsSelection, mergeObsSets, getCellSetPolygons } from '@vitessce/sets';
8
+ import { getCellColors, commaNumber } from '@vitessce/utils';
9
+ import { Scatterplot, ScatterplotTooltipSubscriber, ScatterplotOptions, getPointSizeDevicePixels, getPointOpacity, } from '@vitessce/scatterplot';
10
+ import { ViewType, COMPONENT_COORDINATION_TYPES } from '@vitessce/constants-internal';
11
+ /**
12
+ * A subscriber component for the scatterplot.
13
+ * @param {object} props
14
+ * @param {number} props.uuid The unique identifier for this component.
15
+ * @param {string} props.theme The current theme name.
16
+ * @param {object} props.coordinationScopes The mapping from coordination types to coordination
17
+ * scopes.
18
+ * @param {boolean} props.disableTooltip Should the tooltip be disabled?
19
+ * @param {function} props.removeGridComponent The callback function to pass to TitleInfo,
20
+ * to call when the component has been removed from the grid.
21
+ * @param {string} props.title An override value for the component title.
22
+ * @param {number} props.averageFillDensity Override the average fill density calculation
23
+ * when using dynamic opacity mode.
24
+ */
25
+ export function EmbeddingScatterplotSubscriber(props) {
26
+ const { uuid, coordinationScopes, removeGridComponent, theme, disableTooltip = false, observationsLabelOverride, title: titleOverride,
27
+ // Average fill density for dynamic opacity calculation.
28
+ averageFillDensity, } = props;
29
+ const loaders = useLoaders();
30
+ const setComponentHover = useSetComponentHover();
31
+ const setComponentViewInfo = useSetComponentViewInfo(uuid);
32
+ // Get "props" from the coordination space.
33
+ const [{ dataset, obsType, featureType, featureValueType, embeddingZoom: zoom, embeddingTargetX: targetX, embeddingTargetY: targetY, embeddingTargetZ: targetZ, embeddingType: mapping, obsFilter: cellFilter, obsHighlight: cellHighlight, featureSelection: geneSelection, obsSetSelection: cellSetSelection, obsSetColor: cellSetColor, obsColorEncoding: cellColorEncoding, additionalObsSets: additionalCellSets, embeddingObsSetPolygonsVisible: cellSetPolygonsVisible, embeddingObsSetLabelsVisible: cellSetLabelsVisible, embeddingObsSetLabelSize: cellSetLabelSize, embeddingObsRadius: cellRadiusFixed, embeddingObsRadiusMode: cellRadiusMode, embeddingObsOpacity: cellOpacityFixed, embeddingObsOpacityMode: cellOpacityMode, featureValueColormap: geneExpressionColormap, featureValueColormapRange: geneExpressionColormapRange, }, { setEmbeddingZoom: setZoom, setEmbeddingTargetX: setTargetX, setEmbeddingTargetY: setTargetY, setEmbeddingTargetZ: setTargetZ, setObsFilter: setCellFilter, setObsSetSelection: setCellSetSelection, setObsHighlight: setCellHighlight, setObsSetColor: setCellSetColor, setObsColorEncoding: setCellColorEncoding, setAdditionalObsSets: setAdditionalCellSets, setEmbeddingObsSetPolygonsVisible: setCellSetPolygonsVisible, setEmbeddingObsSetLabelsVisible: setCellSetLabelsVisible, setEmbeddingObsSetLabelSize: setCellSetLabelSize, setEmbeddingObsRadius: setCellRadiusFixed, setEmbeddingObsRadiusMode: setCellRadiusMode, setEmbeddingObsOpacity: setCellOpacityFixed, setEmbeddingObsOpacityMode: setCellOpacityMode, setFeatureValueColormap: setGeneExpressionColormap, setFeatureValueColormapRange: setGeneExpressionColormapRange, }] = useCoordination(COMPONENT_COORDINATION_TYPES[ViewType.SCATTERPLOT], coordinationScopes);
34
+ const observationsLabel = observationsLabelOverride || obsType;
35
+ const [urls, addUrl] = useUrls(loaders, dataset);
36
+ const [width, height, deckRef] = useDeckCanvasSize();
37
+ const title = titleOverride || `Scatterplot (${mapping})`;
38
+ const [obsLabelsTypes, obsLabelsData] = useMultiObsLabels(coordinationScopes, obsType, loaders, dataset, addUrl);
39
+ // Get data from loaders using the data hooks.
40
+ const [{ obsIndex: obsEmbeddingIndex, obsEmbedding }, obsEmbeddingStatus] = useObsEmbeddingData(loaders, dataset, addUrl, true, {}, {}, { obsType, embeddingType: mapping });
41
+ const cellsCount = obsEmbeddingIndex?.length || 0;
42
+ const [{ obsSets: cellSets, obsSetsMembership }, obsSetsStatus] = useObsSetsData(loaders, dataset, addUrl, false, { setObsSetSelection: setCellSetSelection, setObsSetColor: setCellSetColor }, { obsSetSelection: cellSetSelection, obsSetColor: cellSetColor }, { obsType });
43
+ // eslint-disable-next-line no-unused-vars
44
+ const [expressionData, loadedFeatureSelection, featureSelectionStatus] = useFeatureSelection(loaders, dataset, false, geneSelection, { obsType, featureType, featureValueType });
45
+ const [{ obsIndex: matrixObsIndex }, matrixIndicesStatus] = useObsFeatureMatrixIndices(loaders, dataset, addUrl, false, { obsType, featureType, featureValueType });
46
+ const isReady = useReady([
47
+ obsEmbeddingStatus,
48
+ obsSetsStatus,
49
+ featureSelectionStatus,
50
+ matrixIndicesStatus,
51
+ ]);
52
+ const [dynamicCellRadius, setDynamicCellRadius] = useState(cellRadiusFixed);
53
+ const [dynamicCellOpacity, setDynamicCellOpacity] = useState(cellOpacityFixed);
54
+ const mergedCellSets = useMemo(() => mergeObsSets(cellSets, additionalCellSets), [cellSets, additionalCellSets]);
55
+ const setCellSelectionProp = useCallback((v) => {
56
+ setObsSelection(v, additionalCellSets, cellSetColor, setCellSetSelection, setAdditionalCellSets, setCellSetColor, setCellColorEncoding);
57
+ }, [additionalCellSets, cellSetColor, setCellColorEncoding,
58
+ setAdditionalCellSets, setCellSetColor, setCellSetSelection]);
59
+ const cellColors = useMemo(() => getCellColors({
60
+ cellColorEncoding,
61
+ expressionData: expressionData && expressionData[0],
62
+ geneSelection,
63
+ cellSets: mergedCellSets,
64
+ cellSetSelection,
65
+ cellSetColor,
66
+ obsIndex: matrixObsIndex,
67
+ theme,
68
+ }), [cellColorEncoding, geneSelection, mergedCellSets, theme,
69
+ cellSetSelection, cellSetColor, expressionData, matrixObsIndex]);
70
+ // cellSetPolygonCache is an array of tuples like [(key0, val0), (key1, val1), ...],
71
+ // where the keys are cellSetSelection arrays.
72
+ const [cellSetPolygonCache, setCellSetPolygonCache] = useState([]);
73
+ const cacheHas = (cache, key) => cache.findIndex(el => isEqual(el[0], key)) !== -1;
74
+ const cacheGet = (cache, key) => cache.find(el => isEqual(el[0], key))?.[1];
75
+ const cellSetPolygons = useMemo(() => {
76
+ if ((cellSetLabelsVisible || cellSetPolygonsVisible)
77
+ && !cacheHas(cellSetPolygonCache, cellSetSelection)
78
+ && mergedCellSets?.tree?.length
79
+ && obsEmbedding
80
+ && obsEmbeddingIndex
81
+ && cellSetColor?.length) {
82
+ const newCellSetPolygons = getCellSetPolygons({
83
+ obsIndex: obsEmbeddingIndex,
84
+ obsEmbedding,
85
+ cellSets: mergedCellSets,
86
+ cellSetSelection,
87
+ cellSetColor,
88
+ theme,
89
+ });
90
+ setCellSetPolygonCache(cache => [...cache, [cellSetSelection, newCellSetPolygons]]);
91
+ return newCellSetPolygons;
92
+ }
93
+ return cacheGet(cellSetPolygonCache, cellSetSelection) || [];
94
+ }, [cellSetPolygonsVisible, cellSetPolygonCache, cellSetLabelsVisible, theme,
95
+ obsEmbeddingIndex, obsEmbedding, mergedCellSets, cellSetSelection, cellSetColor]);
96
+ const cellSelection = useMemo(() => Array.from(cellColors.keys()), [cellColors]);
97
+ const [xRange, yRange, xExtent, yExtent, numCells] = useMemo(() => {
98
+ if (obsEmbedding && obsEmbedding.data && obsEmbedding.shape) {
99
+ const cellCount = obsEmbedding.shape[1];
100
+ const xE = extent(obsEmbedding.data[0]);
101
+ const yE = extent(obsEmbedding.data[1]);
102
+ const xR = xE[1] - xE[0];
103
+ const yR = yE[1] - yE[0];
104
+ return [xR, yR, xE, yE, cellCount];
105
+ }
106
+ return [null, null, null, null, null];
107
+ }, [obsEmbedding]);
108
+ // After cells have loaded or changed,
109
+ // compute the cell radius scale based on the
110
+ // extents of the cell coordinates on the x/y axes.
111
+ useEffect(() => {
112
+ if (xRange && yRange) {
113
+ const pointSizeDevicePixels = getPointSizeDevicePixels(window.devicePixelRatio, zoom, xRange, yRange, width, height);
114
+ setDynamicCellRadius(pointSizeDevicePixels);
115
+ const nextCellOpacityScale = getPointOpacity(zoom, xRange, yRange, width, height, numCells, averageFillDensity);
116
+ setDynamicCellOpacity(nextCellOpacityScale);
117
+ if (typeof targetX !== 'number' || typeof targetY !== 'number') {
118
+ const newTargetX = xExtent[0] + xRange / 2;
119
+ const newTargetY = yExtent[0] + yRange / 2;
120
+ const newZoom = Math.log2(Math.min(width / xRange, height / yRange));
121
+ setTargetX(newTargetX);
122
+ // Graphics rendering has the y-axis going south so we need to multiply by negative one.
123
+ setTargetY(-newTargetY);
124
+ setZoom(newZoom);
125
+ }
126
+ }
127
+ // eslint-disable-next-line react-hooks/exhaustive-deps
128
+ }, [xRange, yRange, xExtent, yExtent, numCells,
129
+ width, height, zoom, averageFillDensity]);
130
+ const getObsInfo = useGetObsInfo(observationsLabel, obsLabelsTypes, obsLabelsData, obsSetsMembership);
131
+ const cellSelectionSet = useMemo(() => new Set(cellSelection), [cellSelection]);
132
+ const getCellIsSelected = useCallback((object, { index }) => ((cellSelectionSet || new Set([])).has(obsEmbeddingIndex[index]) ? 1.0 : 0.0), [cellSelectionSet, obsEmbeddingIndex]);
133
+ const cellRadius = (cellRadiusMode === 'manual' ? cellRadiusFixed : dynamicCellRadius);
134
+ const cellOpacity = (cellOpacityMode === 'manual' ? cellOpacityFixed : dynamicCellOpacity);
135
+ // Set up a getter function for gene expression values, to be used
136
+ // by the DeckGL layer to obtain values for instanced attributes.
137
+ const getExpressionValue = useExpressionValueGetter({
138
+ instanceObsIndex: obsEmbeddingIndex,
139
+ matrixObsIndex,
140
+ expressionData,
141
+ });
142
+ return (_jsxs(TitleInfo, { title: title, info: `${commaNumber(cellsCount)} ${plur(observationsLabel, cellsCount)}`, removeGridComponent: removeGridComponent, urls: urls, theme: theme, isReady: isReady, options: (_jsx(ScatterplotOptions, { observationsLabel: observationsLabel, cellRadius: cellRadiusFixed, setCellRadius: setCellRadiusFixed, cellRadiusMode: cellRadiusMode, setCellRadiusMode: setCellRadiusMode, cellOpacity: cellOpacityFixed, setCellOpacity: setCellOpacityFixed, cellOpacityMode: cellOpacityMode, setCellOpacityMode: setCellOpacityMode, cellSetLabelsVisible: cellSetLabelsVisible, setCellSetLabelsVisible: setCellSetLabelsVisible, cellSetLabelSize: cellSetLabelSize, setCellSetLabelSize: setCellSetLabelSize, cellSetPolygonsVisible: cellSetPolygonsVisible, setCellSetPolygonsVisible: setCellSetPolygonsVisible, cellColorEncoding: cellColorEncoding, setCellColorEncoding: setCellColorEncoding, geneExpressionColormap: geneExpressionColormap, setGeneExpressionColormap: setGeneExpressionColormap, geneExpressionColormapRange: geneExpressionColormapRange, setGeneExpressionColormapRange: setGeneExpressionColormapRange })), children: [_jsx(Scatterplot, { ref: deckRef, uuid: uuid, theme: theme, viewState: { zoom, target: [targetX, targetY, targetZ] }, setViewState: ({ zoom: newZoom, target }) => {
143
+ setZoom(newZoom);
144
+ setTargetX(target[0]);
145
+ setTargetY(target[1]);
146
+ setTargetZ(target[2] || 0);
147
+ }, obsEmbeddingIndex: obsEmbeddingIndex, obsEmbedding: obsEmbedding, cellFilter: cellFilter, cellSelection: cellSelection, cellHighlight: cellHighlight, cellColors: cellColors, cellSetPolygons: cellSetPolygons, cellSetLabelSize: cellSetLabelSize, cellSetLabelsVisible: cellSetLabelsVisible, cellSetPolygonsVisible: cellSetPolygonsVisible, setCellFilter: setCellFilter, setCellSelection: setCellSelectionProp, setCellHighlight: setCellHighlight, cellRadius: cellRadius, cellOpacity: cellOpacity, cellColorEncoding: cellColorEncoding, geneExpressionColormap: geneExpressionColormap, geneExpressionColormapRange: geneExpressionColormapRange, setComponentHover: () => {
148
+ setComponentHover(uuid);
149
+ }, updateViewInfo: setComponentViewInfo, getExpressionValue: getExpressionValue, getCellIsSelected: getCellIsSelected }), !disableTooltip && (_jsx(ScatterplotTooltipSubscriber, { parentUuid: uuid, obsHighlight: cellHighlight, width: width, height: height, getObsInfo: getObsInfo }))] }));
150
+ }
151
+ export function register() {
152
+ registerPluginViewType(ViewType.SCATTERPLOT, EmbeddingScatterplotSubscriber, COMPONENT_COORDINATION_TYPES[ViewType.SCATTERPLOT]);
153
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { EmbeddingScatterplotSubscriber, register } from './EmbeddingScatterplotSubscriber';
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@vitessce/scatterplot-embedding",
3
+ "version": "2.0.0-beta.0",
4
+ "author": "Gehlenborg Lab",
5
+ "homepage": "http://vitessce.io",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/vitessce/vitessce.git"
9
+ },
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "dependencies": {
16
+ "@material-ui/core": "~4.12.3",
17
+ "@vitessce/constants-internal": "2.0.0-beta.0",
18
+ "@vitessce/scatterplot": "2.0.0-beta.0",
19
+ "@vitessce/sets": "2.0.0-beta.0",
20
+ "@vitessce/utils": "2.0.0-beta.0",
21
+ "@vitessce/vit-s": "2.0.0-beta.0",
22
+ "d3-array": "^2.4.0",
23
+ "lodash": "^4.17.21",
24
+ "plur": "^5.1.0"
25
+ },
26
+ "devDependencies": {
27
+ "react": "^18.0.0",
28
+ "vite": "^3.0.0",
29
+ "vitest": "^0.23.4"
30
+ },
31
+ "peerDependencies": {
32
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0"
33
+ },
34
+ "scripts": {
35
+ "start": "tsc --watch",
36
+ "build": "tsc",
37
+ "test": "pnpm exec vitest --run -r ../../../ --dir ."
38
+ }
39
+ }