@vitessce/scatterplot-embedding 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.
@@ -1,153 +0,0 @@
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-utils';
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
- }