@vitessce/heatmap 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.
- package/dist/deflate.1b70f605.mjs +13 -0
- package/dist/index.35c24bfa.mjs +132140 -0
- package/dist/index.mjs +9 -0
- package/dist/jpeg.1b7865ea.mjs +840 -0
- package/dist/lerc.ff3140f6.mjs +1943 -0
- package/dist/lzw.1036ab46.mjs +128 -0
- package/dist/packbits.088a4e84.mjs +30 -0
- package/dist/pako.esm.4b234125.mjs +3940 -0
- package/dist/raw.b3ce459e.mjs +12 -0
- package/dist/webimage.4cecc301.mjs +32 -0
- package/{dist → dist-tsc}/index.js +0 -0
- package/package.json +12 -12
- package/src/Heatmap.js +857 -0
- package/src/Heatmap.test.fixtures.js +21 -0
- package/src/Heatmap.test.jsx +31 -0
- package/src/HeatmapOptions.js +71 -0
- package/src/HeatmapSubscriber.js +253 -0
- package/src/HeatmapTooltipSubscriber.js +51 -0
- package/src/HeatmapWorkerPool.js +52 -0
- package/src/heatmap-indexing.pdf +0 -0
- package/src/heatmap-indexing.tex +65 -0
- package/src/index.js +2 -0
- package/src/utils.js +247 -0
- package/src/utils.test.js +75 -0
- package/dist/Heatmap.js +0 -689
- package/dist/Heatmap.test.fixtures.js +0 -20
- package/dist/Heatmap.test.js +0 -16
- package/dist/HeatmapOptions.js +0 -22
- package/dist/HeatmapSubscriber.js +0 -104
- package/dist/HeatmapTooltipSubscriber.js +0 -24
- package/dist/HeatmapWorkerPool.js +0 -49
- package/dist/utils.js +0 -181
- package/dist/utils.test.js +0 -71
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export const expressionMatrix = {
|
|
2
|
-
rows: ['cell-0', 'cell-1', 'cell-2', 'cell-3', 'cell-4'],
|
|
3
|
-
cols: ['gene-0', 'gene-1', 'gene-2', 'gene-3'],
|
|
4
|
-
// "An image with an 'F' on it has a clear direction so it's easy to tell
|
|
5
|
-
// if it's turned or flipped etc when we use it as a texture."
|
|
6
|
-
// - https://webglfundamentals.org/webgl/lessons/webgl-3d-textures.html
|
|
7
|
-
matrix: Uint8Array.from([
|
|
8
|
-
0, 255, 255, 0,
|
|
9
|
-
0, 255, 0, 0,
|
|
10
|
-
0, 255, 255, 0,
|
|
11
|
-
0, 255, 0, 0,
|
|
12
|
-
0, 255, 0, 0,
|
|
13
|
-
]),
|
|
14
|
-
};
|
|
15
|
-
export const cellColors = new Map([
|
|
16
|
-
['cell-1', [0, 0, 255]],
|
|
17
|
-
['cell-0', [0, 0, 255]],
|
|
18
|
-
['cell-3', [255, 0, 0]],
|
|
19
|
-
['cell-2', [255, 0, 0]],
|
|
20
|
-
]);
|
package/dist/Heatmap.test.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
/* eslint-disable func-names */
|
|
3
|
-
import '@testing-library/jest-dom';
|
|
4
|
-
import { cleanup, render } from '@testing-library/react';
|
|
5
|
-
import { afterEach } from 'vitest';
|
|
6
|
-
import Heatmap from './Heatmap';
|
|
7
|
-
import { expressionMatrix, cellColors } from './Heatmap.test.fixtures';
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
cleanup();
|
|
10
|
-
});
|
|
11
|
-
describe('<Heatmap/>', () => {
|
|
12
|
-
it('renders a DeckGL element', function () {
|
|
13
|
-
const { container } = render(_jsx(Heatmap, { uuid: "heatmap-0", theme: "dark", width: 100, height: 100, colormap: "plasma", colormapRange: [0.0, 1.0], expressionMatrix: expressionMatrix, cellColors: cellColors, transpose: true, viewState: { zoom: 0, target: [0, 0] } }));
|
|
14
|
-
expect(container.querySelectorAll('#deckgl-overlay-heatmap-0-wrapper').length).toEqual(1);
|
|
15
|
-
});
|
|
16
|
-
});
|
package/dist/HeatmapOptions.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import React, { useCallback } from 'react';
|
|
3
|
-
import debounce from 'lodash/debounce';
|
|
4
|
-
import Slider from '@material-ui/core/Slider';
|
|
5
|
-
import TableCell from '@material-ui/core/TableCell';
|
|
6
|
-
import TableRow from '@material-ui/core/TableRow';
|
|
7
|
-
import { usePlotOptionsStyles, OptionsContainer, OptionSelect } from '@vitessce/vit-s';
|
|
8
|
-
import { GLSL_COLORMAPS } from '@vitessce/gl';
|
|
9
|
-
export default function HeatmapOptions(props) {
|
|
10
|
-
const { geneExpressionColormap, setGeneExpressionColormap, geneExpressionColormapRange, setGeneExpressionColormapRange, } = props;
|
|
11
|
-
const classes = usePlotOptionsStyles();
|
|
12
|
-
function handleGeneExpressionColormapChange(event) {
|
|
13
|
-
setGeneExpressionColormap(event.target.value);
|
|
14
|
-
}
|
|
15
|
-
function handleColormapRangeChange(event, value) {
|
|
16
|
-
setGeneExpressionColormapRange(value);
|
|
17
|
-
}
|
|
18
|
-
const handleColormapRangeChangeDebounced = useCallback(debounce(handleColormapRangeChange, 5, { trailing: true }), [handleColormapRangeChange]);
|
|
19
|
-
return (_jsxs(OptionsContainer, { children: [_jsxs(TableRow, { children: [_jsx(TableCell, { className: classes.labelCell, htmlFor: "gene-expression-colormap-select", children: "Gene Expression Colormap" }), _jsx(TableCell, { className: classes.inputCell, children: _jsx(OptionSelect, { className: classes.select, value: geneExpressionColormap, onChange: handleGeneExpressionColormapChange, inputProps: {
|
|
20
|
-
id: 'gene-expression-colormap-select',
|
|
21
|
-
}, children: GLSL_COLORMAPS.map(cmap => (_jsx("option", { value: cmap, children: cmap }, cmap))) }) })] }), _jsxs(TableRow, { children: [_jsx(TableCell, { className: classes.labelCell, children: "Gene Expression Colormap Range" }), _jsx(TableCell, { className: classes.inputCell, children: _jsx(Slider, { classes: { root: classes.slider, valueLabel: classes.sliderValueLabel }, value: geneExpressionColormapRange, onChange: handleColormapRangeChangeDebounced, "aria-labelledby": "gene-expression-colormap-range-slider", valueLabelDisplay: "auto", step: 0.005, min: 0.0, max: 1.0 }) })] })] }));
|
|
22
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import React, { useState, useCallback, useMemo, } from 'react';
|
|
3
|
-
import plur from 'plur';
|
|
4
|
-
import { TitleInfo, useDeckCanvasSize, useGetObsInfo, useReady, useUrls, useObsSetsData, useObsFeatureMatrixData, useMultiObsLabels, useFeatureLabelsData, useCoordination, useLoaders, useSetComponentHover, useSetComponentViewInfo, registerPluginViewType, } from '@vitessce/vit-s';
|
|
5
|
-
import { capitalize, commaNumber, getCellColors } from '@vitessce/utils';
|
|
6
|
-
import { mergeObsSets } from '@vitessce/sets-utils';
|
|
7
|
-
import { COMPONENT_COORDINATION_TYPES, ViewType } from '@vitessce/constants-internal';
|
|
8
|
-
import Heatmap from './Heatmap';
|
|
9
|
-
import HeatmapTooltipSubscriber from './HeatmapTooltipSubscriber';
|
|
10
|
-
import HeatmapOptions from './HeatmapOptions';
|
|
11
|
-
/**
|
|
12
|
-
* @param {object} props
|
|
13
|
-
* @param {number} props.uuid The unique identifier for this component.
|
|
14
|
-
* @param {object} props.coordinationScopes The mapping from coordination types to coordination
|
|
15
|
-
* scopes.
|
|
16
|
-
* @param {function} props.removeGridComponent The callback function to pass to TitleInfo,
|
|
17
|
-
* to call when the component has been removed from the grid.
|
|
18
|
-
* @param {string} props.title The component title.
|
|
19
|
-
* @param {boolean} props.transpose Whether to
|
|
20
|
-
* render as cell-by-gene or gene-by-cell.
|
|
21
|
-
* @param {boolean} props.disableTooltip Whether to disable the
|
|
22
|
-
* tooltip on mouse hover.
|
|
23
|
-
*/
|
|
24
|
-
export function HeatmapSubscriber(props) {
|
|
25
|
-
const { uuid, coordinationScopes, removeGridComponent, theme, transpose, observationsLabelOverride, variablesLabelOverride, disableTooltip = false, title = 'Heatmap', } = props;
|
|
26
|
-
const loaders = useLoaders();
|
|
27
|
-
const setComponentHover = useSetComponentHover();
|
|
28
|
-
const setComponentViewInfo = useSetComponentViewInfo(uuid);
|
|
29
|
-
// Get "props" from the coordination space.
|
|
30
|
-
const [{ dataset, obsType, featureType, featureValueType, heatmapZoomX: zoomX, heatmapTargetX: targetX, heatmapTargetY: targetY, featureSelection: geneSelection, obsHighlight: cellHighlight, featureHighlight: geneHighlight, obsSetSelection: cellSetSelection, obsSetColor: cellSetColor, additionalObsSets: additionalCellSets, featureValueColormap: geneExpressionColormap, featureValueColormapRange: geneExpressionColormapRange, }, { setHeatmapZoomX: setZoomX, setHeatmapZoomY: setZoomY, setHeatmapTargetX: setTargetX, setHeatmapTargetY: setTargetY, setObsHighlight: setCellHighlight, setFeatureHighlight: setGeneHighlight, setObsSetSelection: setCellSetSelection, setObsSetColor: setCellSetColor, setFeatureValueColormapRange: setGeneExpressionColormapRange, setFeatureValueColormap: setGeneExpressionColormap, }] = useCoordination(COMPONENT_COORDINATION_TYPES[ViewType.HEATMAP], coordinationScopes);
|
|
31
|
-
const observationsLabel = observationsLabelOverride || obsType;
|
|
32
|
-
const observationsPluralLabel = plur(observationsLabel);
|
|
33
|
-
const variablesLabel = variablesLabelOverride || featureType;
|
|
34
|
-
const variablesPluralLabel = plur(variablesLabel);
|
|
35
|
-
const observationsTitle = capitalize(observationsPluralLabel);
|
|
36
|
-
const variablesTitle = capitalize(variablesPluralLabel);
|
|
37
|
-
const [isRendering, setIsRendering] = useState(false);
|
|
38
|
-
const [urls, addUrl] = useUrls(loaders, dataset);
|
|
39
|
-
const [width, height, deckRef] = useDeckCanvasSize();
|
|
40
|
-
// Get data from loaders using the data hooks.
|
|
41
|
-
const [obsLabelsTypes, obsLabelsData] = useMultiObsLabels(coordinationScopes, obsType, loaders, dataset, addUrl);
|
|
42
|
-
// TODO: support multiple feature labels using featureLabelsType coordination values.
|
|
43
|
-
const [{ featureLabelsMap }, featureLabelsStatus] = useFeatureLabelsData(loaders, dataset, addUrl, false, {}, {}, { featureType });
|
|
44
|
-
const [{ obsIndex, featureIndex, obsFeatureMatrix }, matrixStatus] = useObsFeatureMatrixData(loaders, dataset, addUrl, true, {}, {}, { obsType, featureType, featureValueType });
|
|
45
|
-
const [{ obsSets: cellSets, obsSetsMembership }, obsSetsStatus] = useObsSetsData(loaders, dataset, addUrl, false, { setObsSetSelection: setCellSetSelection, setObsSetColor: setCellSetColor }, { obsSetSelection: cellSetSelection, obsSetColor: cellSetColor }, { obsType });
|
|
46
|
-
const isReady = useReady([
|
|
47
|
-
featureLabelsStatus,
|
|
48
|
-
matrixStatus,
|
|
49
|
-
obsSetsStatus,
|
|
50
|
-
]);
|
|
51
|
-
const mergedCellSets = useMemo(() => mergeObsSets(cellSets, additionalCellSets), [cellSets, additionalCellSets]);
|
|
52
|
-
const cellColors = useMemo(() => getCellColors({
|
|
53
|
-
// Only show cell set selection on heatmap labels.
|
|
54
|
-
cellColorEncoding: 'cellSetSelection',
|
|
55
|
-
geneSelection,
|
|
56
|
-
cellSets: mergedCellSets,
|
|
57
|
-
cellSetSelection,
|
|
58
|
-
cellSetColor,
|
|
59
|
-
obsIndex,
|
|
60
|
-
theme,
|
|
61
|
-
}), [mergedCellSets, geneSelection, theme,
|
|
62
|
-
cellSetColor, cellSetSelection, obsIndex]);
|
|
63
|
-
const getObsInfo = useGetObsInfo(observationsLabel, obsLabelsTypes, obsLabelsData, obsSetsMembership);
|
|
64
|
-
const getFeatureInfo = useCallback((featureId) => {
|
|
65
|
-
if (featureId) {
|
|
66
|
-
return { [`${capitalize(variablesLabel)} ID`]: featureId };
|
|
67
|
-
}
|
|
68
|
-
return null;
|
|
69
|
-
}, [variablesLabel]);
|
|
70
|
-
const expressionMatrix = useMemo(() => {
|
|
71
|
-
if (obsIndex && featureIndex && obsFeatureMatrix) {
|
|
72
|
-
return {
|
|
73
|
-
rows: obsIndex,
|
|
74
|
-
cols: (featureLabelsMap
|
|
75
|
-
? featureIndex.map(key => featureLabelsMap.get(key) || key)
|
|
76
|
-
: featureIndex),
|
|
77
|
-
matrix: obsFeatureMatrix.data,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
return null;
|
|
81
|
-
}, [obsIndex, featureIndex, obsFeatureMatrix, featureLabelsMap]);
|
|
82
|
-
const cellsCount = obsIndex ? obsIndex.length : 0;
|
|
83
|
-
const genesCount = featureIndex ? featureIndex.length : 0;
|
|
84
|
-
const setTrackHighlight = useCallback(() => {
|
|
85
|
-
// No-op, since the default handler
|
|
86
|
-
// logs in the console on every hover event.
|
|
87
|
-
}, []);
|
|
88
|
-
const cellColorLabels = useMemo(() => ([
|
|
89
|
-
`${capitalize(observationsLabel)} Set`,
|
|
90
|
-
]), [observationsLabel]);
|
|
91
|
-
const selectedCount = cellColors.size;
|
|
92
|
-
return (_jsxs(TitleInfo, { title: title, info: `${commaNumber(cellsCount)} ${plur(observationsLabel, cellsCount)} × ${commaNumber(genesCount)} ${plur(variablesLabel, genesCount)},
|
|
93
|
-
with ${commaNumber(selectedCount)} ${plur(observationsLabel, selectedCount)} selected`, urls: urls, theme: theme, removeGridComponent: removeGridComponent, isReady: isReady && !isRendering, options: (_jsx(HeatmapOptions, { geneExpressionColormap: geneExpressionColormap, setGeneExpressionColormap: setGeneExpressionColormap, geneExpressionColormapRange: geneExpressionColormapRange, setGeneExpressionColormapRange: setGeneExpressionColormapRange })), children: [_jsx(Heatmap, { ref: deckRef, transpose: transpose, viewState: { zoom: zoomX, target: [targetX, targetY] }, setViewState: ({ zoom, target }) => {
|
|
94
|
-
setZoomX(zoom);
|
|
95
|
-
setZoomY(zoom);
|
|
96
|
-
setTargetX(target[0]);
|
|
97
|
-
setTargetY(target[1]);
|
|
98
|
-
}, colormapRange: geneExpressionColormapRange, setColormapRange: setGeneExpressionColormapRange, height: height, width: width, theme: theme, uuid: uuid, expressionMatrix: expressionMatrix, cellColors: cellColors, colormap: geneExpressionColormap, setIsRendering: setIsRendering, setCellHighlight: setCellHighlight, setGeneHighlight: setGeneHighlight, setTrackHighlight: setTrackHighlight, setComponentHover: () => {
|
|
99
|
-
setComponentHover(uuid);
|
|
100
|
-
}, updateViewInfo: setComponentViewInfo, observationsTitle: observationsTitle, variablesTitle: variablesTitle, variablesDashes: false, observationsDashes: false, cellColorLabels: cellColorLabels, useDevicePixels: true }), !disableTooltip && (_jsx(HeatmapTooltipSubscriber, { parentUuid: uuid, width: width, height: height, transpose: transpose, getObsInfo: getObsInfo, getFeatureInfo: getFeatureInfo, obsHighlight: cellHighlight, featureHighlight: geneHighlight }))] }));
|
|
101
|
-
}
|
|
102
|
-
export function register() {
|
|
103
|
-
registerPluginViewType(ViewType.HEATMAP, HeatmapSubscriber, COMPONENT_COORDINATION_TYPES[ViewType.HEATMAP]);
|
|
104
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { Tooltip2D, TooltipContent } from '@vitessce/tooltip';
|
|
4
|
-
import { useComponentHover, useComponentViewInfo } from '@vitessce/vit-s';
|
|
5
|
-
export default function HeatmapTooltipSubscriber(props) {
|
|
6
|
-
const { parentUuid, width, height, transpose, getObsInfo, getFeatureInfo, obsHighlight, featureHighlight, } = props;
|
|
7
|
-
const sourceUuid = useComponentHover();
|
|
8
|
-
const viewInfo = useComponentViewInfo(parentUuid);
|
|
9
|
-
const [cellInfo, cellCoord] = (obsHighlight && getObsInfo ? ([
|
|
10
|
-
getObsInfo(obsHighlight),
|
|
11
|
-
(viewInfo && viewInfo.project
|
|
12
|
-
? viewInfo.project(obsHighlight, null)[(transpose ? 0 : 1)]
|
|
13
|
-
: null),
|
|
14
|
-
]) : ([null, null]));
|
|
15
|
-
const [geneInfo, geneCoord] = (featureHighlight && getFeatureInfo ? ([
|
|
16
|
-
getFeatureInfo(featureHighlight),
|
|
17
|
-
(viewInfo && viewInfo.project
|
|
18
|
-
? viewInfo.project(null, featureHighlight)[(transpose ? 1 : 0)]
|
|
19
|
-
: null),
|
|
20
|
-
]) : ([null, null]));
|
|
21
|
-
const x = (transpose ? cellCoord : geneCoord);
|
|
22
|
-
const y = (transpose ? geneCoord : cellCoord);
|
|
23
|
-
return ((cellInfo || geneInfo ? (_jsx(Tooltip2D, { x: x, y: y, parentUuid: parentUuid, parentWidth: width, parentHeight: height, sourceUuid: sourceUuid, children: _jsx(TooltipContent, { info: { ...geneInfo, ...cellInfo } }) })) : null));
|
|
24
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { HeatmapWorker } from '@vitessce/workers';
|
|
2
|
-
import { Pool } from '@vitessce/utils';
|
|
3
|
-
// Reference: https://github.com/developit/jsdom-worker/issues/14#issuecomment-1268070123
|
|
4
|
-
function createWorker() {
|
|
5
|
-
return new HeatmapWorker();
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Pool for workers to decode chunks of the images.
|
|
9
|
-
* This is a line-for-line copy of GeoTIFFs old implementation: https://github.com/geotiffjs/geotiff.js/blob/v1.0.0-beta.6/src/pool.js
|
|
10
|
-
*/
|
|
11
|
-
export default class HeatmapPool extends Pool {
|
|
12
|
-
constructor() {
|
|
13
|
-
super(createWorker);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Process each heatmap tile
|
|
17
|
-
* @param {object} params The arguments passed to the heatmap worker.
|
|
18
|
-
* @param {string} params.curr The current task uuid.
|
|
19
|
-
* @param {number} params.xTiles How many tiles required in the x direction?
|
|
20
|
-
* @param {number} params.yTiles How many tiles required in the y direction?
|
|
21
|
-
* @param {number} params.tileSize How many entries along each tile axis?
|
|
22
|
-
* @param {string[]} params.cellOrdering The current ordering of cells.
|
|
23
|
-
* @param {string[]} params.rows The name of each row (cell ID).
|
|
24
|
-
* Does not take transpose into account (always cells).
|
|
25
|
-
* @param {string[]} params.cols The name of each column (gene ID).
|
|
26
|
-
* Does not take transpose into account (always genes).
|
|
27
|
-
* @param {ArrayBuffer} params.data The array buffer.
|
|
28
|
-
* Need to transfer back to main thread when done.
|
|
29
|
-
* @param {boolean} params.transpose Is the heatmap transposed?
|
|
30
|
-
* @returns {array} [message, transfers]
|
|
31
|
-
* @returns {Promise.<ArrayBuffer>} the decoded result as a `Promise`
|
|
32
|
-
*/
|
|
33
|
-
async process(args) {
|
|
34
|
-
const currentWorker = await this.waitForWorker();
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
currentWorker.onmessage = (event) => {
|
|
37
|
-
// this.workers.push(currentWorker);
|
|
38
|
-
this.finishTask(currentWorker);
|
|
39
|
-
resolve(event.data);
|
|
40
|
-
};
|
|
41
|
-
currentWorker.onerror = (error) => {
|
|
42
|
-
// this.workers.push(currentWorker);
|
|
43
|
-
this.finishTask(currentWorker);
|
|
44
|
-
reject(error);
|
|
45
|
-
};
|
|
46
|
-
currentWorker.postMessage(['getTile', args], [args.data]);
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
package/dist/utils.js
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import clamp from 'lodash/clamp';
|
|
2
|
-
import { AXIS_LABEL_TEXT_SIZE, AXIS_FONT_FAMILY, AXIS_PADDING, AXIS_MIN_SIZE, AXIS_MAX_SIZE, } from '@vitessce/gl';
|
|
3
|
-
/**
|
|
4
|
-
* Called before a layer is drawn to determine whether it should be rendered.
|
|
5
|
-
* Reference: https://deck.gl/docs/api-reference/core/deck#layerfilter
|
|
6
|
-
* @param {object} params A viewport, layer pair.
|
|
7
|
-
* @param {object} params.layer The layer to check.
|
|
8
|
-
* @param {object} params.viewport The viewport to check.
|
|
9
|
-
* @returns {boolean} Should this layer be rendered in this viewport?
|
|
10
|
-
*/
|
|
11
|
-
export function layerFilter({ layer, viewport }) {
|
|
12
|
-
if (viewport.id === 'axisLeft') {
|
|
13
|
-
return layer.id.startsWith('axisLeft');
|
|
14
|
-
}
|
|
15
|
-
if (viewport.id === 'axisTop') {
|
|
16
|
-
return layer.id.startsWith('axisTop');
|
|
17
|
-
}
|
|
18
|
-
if (viewport.id === 'cellColorLabel') {
|
|
19
|
-
return layer.id.startsWith('cellColorLabel');
|
|
20
|
-
}
|
|
21
|
-
if (viewport.id === 'heatmap') {
|
|
22
|
-
return layer.id.startsWith('heatmap');
|
|
23
|
-
}
|
|
24
|
-
if (viewport.id.startsWith('colorsLeft')) {
|
|
25
|
-
const matches = viewport.id.match(/-(\d)/);
|
|
26
|
-
if (matches)
|
|
27
|
-
return layer.id.startsWith(`colorsLeftLayer-${matches[1]}`);
|
|
28
|
-
}
|
|
29
|
-
if (viewport.id.startsWith('colorsTop')) {
|
|
30
|
-
const matches = viewport.id.match(/-(\d)/);
|
|
31
|
-
if (matches)
|
|
32
|
-
return layer.id.startsWith(`colorsTopLayer-${matches[1]}`);
|
|
33
|
-
}
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Uses canvas.measureText to compute and return the width of the given text
|
|
38
|
-
* of given font in pixels.
|
|
39
|
-
*
|
|
40
|
-
* @param {String} text The text to be rendered.
|
|
41
|
-
* @param {String} font The css font descriptor that text is to be rendered
|
|
42
|
-
* with (e.g. "bold 14px verdana").
|
|
43
|
-
*
|
|
44
|
-
* @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
|
|
45
|
-
*/
|
|
46
|
-
function getTextWidth(text, font) {
|
|
47
|
-
// re-use canvas object for better performance
|
|
48
|
-
const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement('canvas'));
|
|
49
|
-
const context = canvas.getContext('2d');
|
|
50
|
-
context.font = font;
|
|
51
|
-
const metrics = context.measureText(text);
|
|
52
|
-
return metrics.width;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Get the size of the left and top heatmap axes,
|
|
56
|
-
* taking into account the maximum label string lengths.
|
|
57
|
-
* @param {boolean} transpose Is the heatmap transposed?
|
|
58
|
-
* @param {String} longestGeneLabel longest gene label
|
|
59
|
-
* @param {String} longestCellLabel longest cell label
|
|
60
|
-
* @param {boolean} hideObservationLabels are cell labels hidden?
|
|
61
|
-
* @param {boolean} hideVariableLabels are gene labels hidden?
|
|
62
|
-
* Increases vertical space for heatmap
|
|
63
|
-
* @returns {number[]} [axisOffsetLeft, axisOffsetTop]
|
|
64
|
-
*/
|
|
65
|
-
export function getAxisSizes(transpose, longestGeneLabel, longestCellLabel, hideObservationLabels, hideVariableLabels) {
|
|
66
|
-
const font = `${AXIS_LABEL_TEXT_SIZE}pt ${AXIS_FONT_FAMILY}`;
|
|
67
|
-
const geneLabelMaxWidth = hideVariableLabels
|
|
68
|
-
? 0 : getTextWidth(longestGeneLabel, font) + AXIS_PADDING;
|
|
69
|
-
const cellLabelMaxWidth = hideObservationLabels
|
|
70
|
-
? 0 : getTextWidth(longestCellLabel, font) + AXIS_PADDING;
|
|
71
|
-
const axisOffsetLeft = clamp((transpose ? geneLabelMaxWidth : cellLabelMaxWidth), AXIS_MIN_SIZE, AXIS_MAX_SIZE);
|
|
72
|
-
const axisOffsetTop = clamp((transpose ? cellLabelMaxWidth : geneLabelMaxWidth), AXIS_MIN_SIZE, AXIS_MAX_SIZE);
|
|
73
|
-
return [axisOffsetLeft, axisOffsetTop];
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Convert a mouse coordinate (x, y) to a heatmap coordinate (col index, row index).
|
|
77
|
-
* @param {number} mouseX The mouse X of interest.
|
|
78
|
-
* @param {number} mouseY The mouse Y of interest.
|
|
79
|
-
* @param {object} param2 An object containing current sizes and scale factors.
|
|
80
|
-
* @returns {number[]} [colI, rowI]
|
|
81
|
-
*/
|
|
82
|
-
export function mouseToHeatmapPosition(mouseX, mouseY, { offsetLeft, offsetTop, targetX, targetY, scaleFactor, matrixWidth, matrixHeight, numRows, numCols, }) {
|
|
83
|
-
// TODO: use linear algebra
|
|
84
|
-
const viewMouseX = mouseX - offsetLeft;
|
|
85
|
-
const viewMouseY = mouseY - offsetTop;
|
|
86
|
-
if (viewMouseX < 0 || viewMouseY < 0) {
|
|
87
|
-
// The mouse is outside the heatmap.
|
|
88
|
-
return [null, null];
|
|
89
|
-
}
|
|
90
|
-
// Determine the rowI and colI values based on the current viewState.
|
|
91
|
-
const bboxTargetX = targetX * scaleFactor + matrixWidth * scaleFactor / 2;
|
|
92
|
-
const bboxTargetY = targetY * scaleFactor + matrixHeight * scaleFactor / 2;
|
|
93
|
-
const bboxLeft = bboxTargetX - matrixWidth / 2;
|
|
94
|
-
const bboxTop = bboxTargetY - matrixHeight / 2;
|
|
95
|
-
const zoomedOffsetLeft = bboxLeft / (matrixWidth * scaleFactor);
|
|
96
|
-
const zoomedOffsetTop = bboxTop / (matrixHeight * scaleFactor);
|
|
97
|
-
const zoomedViewMouseX = viewMouseX / (matrixWidth * scaleFactor);
|
|
98
|
-
const zoomedViewMouseY = viewMouseY / (matrixHeight * scaleFactor);
|
|
99
|
-
const zoomedMouseX = zoomedOffsetLeft + zoomedViewMouseX;
|
|
100
|
-
const zoomedMouseY = zoomedOffsetTop + zoomedViewMouseY;
|
|
101
|
-
const rowI = Math.floor(zoomedMouseY * numRows);
|
|
102
|
-
const colI = Math.floor(zoomedMouseX * numCols);
|
|
103
|
-
return [colI, rowI];
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Convert a heatmap coordinate (col index, row index) to a mouse coordinate (x, y).
|
|
107
|
-
* @param {number} colI The column index of interest.
|
|
108
|
-
* @param {number} rowI The row index of interest.
|
|
109
|
-
* @param {object} param2 An object containing current sizes and scale factors.
|
|
110
|
-
* @returns {number[]} [x, y]
|
|
111
|
-
*/
|
|
112
|
-
export function heatmapToMousePosition(colI, rowI, { offsetLeft, offsetTop, targetX, targetY, scaleFactor, matrixWidth, matrixHeight, numRows, numCols, }) {
|
|
113
|
-
// TODO: use linear algebra
|
|
114
|
-
let zoomedMouseY = null;
|
|
115
|
-
let zoomedMouseX = null;
|
|
116
|
-
if (rowI !== null) {
|
|
117
|
-
const minY = -matrixHeight * scaleFactor / 2;
|
|
118
|
-
const maxY = matrixHeight * scaleFactor / 2;
|
|
119
|
-
const totalHeight = maxY - minY;
|
|
120
|
-
const minInViewY = (targetY * scaleFactor) - (matrixHeight / 2);
|
|
121
|
-
const maxInViewY = (targetY * scaleFactor) + (matrixHeight / 2);
|
|
122
|
-
const inViewHeight = maxInViewY - minInViewY;
|
|
123
|
-
const normalizedRowY = (rowI + 0.5) / numRows;
|
|
124
|
-
const globalRowY = minY + (normalizedRowY * totalHeight);
|
|
125
|
-
if (minInViewY <= globalRowY && globalRowY <= maxInViewY) {
|
|
126
|
-
zoomedMouseY = offsetTop + ((globalRowY - minInViewY) / inViewHeight) * matrixHeight;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (colI !== null) {
|
|
130
|
-
const minX = -matrixWidth * scaleFactor / 2;
|
|
131
|
-
const maxX = matrixWidth * scaleFactor / 2;
|
|
132
|
-
const totalWidth = maxX - minX;
|
|
133
|
-
const minInViewX = (targetX * scaleFactor) - (matrixWidth / 2);
|
|
134
|
-
const maxInViewX = (targetX * scaleFactor) + (matrixWidth / 2);
|
|
135
|
-
const inViewWidth = maxInViewX - minInViewX;
|
|
136
|
-
const normalizedRowX = (colI + 0.5) / numCols;
|
|
137
|
-
const globalRowX = minX + (normalizedRowX * totalWidth);
|
|
138
|
-
if (minInViewX <= globalRowX && globalRowX <= maxInViewX) {
|
|
139
|
-
zoomedMouseX = offsetLeft + ((globalRowX - minInViewX) / inViewWidth) * matrixWidth;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return [zoomedMouseX, zoomedMouseY];
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Convert a mouse coordinate (x, y) to a heatmap color bar coordinate (cell index, track index).
|
|
146
|
-
* @param {number} mouseX The mouse X of interest.
|
|
147
|
-
* @param {number} mouseY The mouse Y of interest.
|
|
148
|
-
* @param {object} param2 An object containing current sizes and scale factors.
|
|
149
|
-
* @returns {number[]} [cellI, trackI]
|
|
150
|
-
*/
|
|
151
|
-
export function mouseToCellColorPosition(mouseX, mouseY, { axisOffsetTop, axisOffsetLeft, offsetTop, offsetLeft, colorBarSize, numCellColorTracks, transpose, targetX, targetY, scaleFactor, matrixWidth, matrixHeight, numRows, numCols, }) {
|
|
152
|
-
const cellPosition = transpose ? mouseX - offsetLeft : mouseY - offsetTop;
|
|
153
|
-
const trackPosition = transpose ? mouseY - axisOffsetTop : mouseX - axisOffsetLeft;
|
|
154
|
-
const tracksWidth = numCellColorTracks * colorBarSize;
|
|
155
|
-
// outside of cell color tracks
|
|
156
|
-
if (cellPosition < 0 || trackPosition < 0 || trackPosition >= tracksWidth) {
|
|
157
|
-
return [null, null];
|
|
158
|
-
}
|
|
159
|
-
// Determine the trackI and cellI values based on the current viewState.
|
|
160
|
-
const trackI = Math.floor(trackPosition / colorBarSize);
|
|
161
|
-
let cellI;
|
|
162
|
-
if (transpose) {
|
|
163
|
-
const viewMouseX = mouseX - offsetLeft;
|
|
164
|
-
const bboxTargetX = targetX * scaleFactor + matrixWidth * scaleFactor / 2;
|
|
165
|
-
const bboxLeft = bboxTargetX - matrixWidth / 2;
|
|
166
|
-
const zoomedOffsetLeft = bboxLeft / (matrixWidth * scaleFactor);
|
|
167
|
-
const zoomedViewMouseX = viewMouseX / (matrixWidth * scaleFactor);
|
|
168
|
-
const zoomedMouseX = zoomedOffsetLeft + zoomedViewMouseX;
|
|
169
|
-
cellI = Math.floor(zoomedMouseX * numCols);
|
|
170
|
-
return [cellI, trackI];
|
|
171
|
-
}
|
|
172
|
-
// Not transposed
|
|
173
|
-
const viewMouseY = mouseY - axisOffsetTop;
|
|
174
|
-
const bboxTargetY = targetY * scaleFactor + matrixHeight * scaleFactor / 2;
|
|
175
|
-
const bboxTop = bboxTargetY - matrixHeight / 2;
|
|
176
|
-
const zoomedOffsetTop = bboxTop / (matrixHeight * scaleFactor);
|
|
177
|
-
const zoomedViewMouseY = viewMouseY / (matrixHeight * scaleFactor);
|
|
178
|
-
const zoomedMouseY = zoomedOffsetTop + zoomedViewMouseY;
|
|
179
|
-
cellI = Math.floor(zoomedMouseY * numRows);
|
|
180
|
-
return [cellI, trackI];
|
|
181
|
-
}
|
package/dist/utils.test.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { mouseToHeatmapPosition, heatmapToMousePosition } from './utils';
|
|
2
|
-
describe('heatmap tooltip utils', () => {
|
|
3
|
-
it('transforms mouse coordinates to row and column indices when zoomed out', () => {
|
|
4
|
-
const mouseX = 35;
|
|
5
|
-
const mouseY = 78;
|
|
6
|
-
const [colI, rowI] = mouseToHeatmapPosition(mouseX, mouseY, {
|
|
7
|
-
offsetLeft: 10,
|
|
8
|
-
offsetTop: 10,
|
|
9
|
-
targetX: 0,
|
|
10
|
-
targetY: 0,
|
|
11
|
-
scaleFactor: 1,
|
|
12
|
-
matrixWidth: 100,
|
|
13
|
-
matrixHeight: 100,
|
|
14
|
-
numRows: 5,
|
|
15
|
-
numCols: 4,
|
|
16
|
-
});
|
|
17
|
-
expect(colI).toEqual(1);
|
|
18
|
-
expect(rowI).toEqual(3);
|
|
19
|
-
});
|
|
20
|
-
it('transforms mouse coordinates to row and column indices when zoomed in', () => {
|
|
21
|
-
const mouseX = 35;
|
|
22
|
-
const mouseY = 78;
|
|
23
|
-
const [colI, rowI] = mouseToHeatmapPosition(mouseX, mouseY, {
|
|
24
|
-
offsetLeft: 10,
|
|
25
|
-
offsetTop: 10,
|
|
26
|
-
targetX: 21,
|
|
27
|
-
targetY: -11,
|
|
28
|
-
scaleFactor: 4,
|
|
29
|
-
matrixWidth: 100,
|
|
30
|
-
matrixHeight: 100,
|
|
31
|
-
numRows: 5,
|
|
32
|
-
numCols: 4,
|
|
33
|
-
});
|
|
34
|
-
expect(colI).toEqual(2);
|
|
35
|
-
expect(rowI).toEqual(2);
|
|
36
|
-
});
|
|
37
|
-
it('transforms row and column indices when zoomed out', () => {
|
|
38
|
-
const colI = 1;
|
|
39
|
-
const rowI = 3;
|
|
40
|
-
const [mouseX, mouseY] = heatmapToMousePosition(colI, rowI, {
|
|
41
|
-
offsetLeft: 10,
|
|
42
|
-
offsetTop: 10,
|
|
43
|
-
targetX: 0,
|
|
44
|
-
targetY: 0,
|
|
45
|
-
scaleFactor: 1,
|
|
46
|
-
matrixWidth: 100,
|
|
47
|
-
matrixHeight: 100,
|
|
48
|
-
numRows: 5,
|
|
49
|
-
numCols: 4,
|
|
50
|
-
});
|
|
51
|
-
expect(mouseX).toEqual(47.5);
|
|
52
|
-
expect(mouseY).toEqual(80);
|
|
53
|
-
});
|
|
54
|
-
it('transforms row and column indices when zoomed in', () => {
|
|
55
|
-
const colI = 2;
|
|
56
|
-
const rowI = 2;
|
|
57
|
-
const [mouseX, mouseY] = heatmapToMousePosition(colI, rowI, {
|
|
58
|
-
offsetLeft: 10,
|
|
59
|
-
offsetTop: 10,
|
|
60
|
-
targetX: 21,
|
|
61
|
-
targetY: -11,
|
|
62
|
-
scaleFactor: 4,
|
|
63
|
-
matrixWidth: 100,
|
|
64
|
-
matrixHeight: 100,
|
|
65
|
-
numRows: 5,
|
|
66
|
-
numCols: 4,
|
|
67
|
-
});
|
|
68
|
-
expect(mouseX).toEqual(26);
|
|
69
|
-
expect(mouseY).toEqual(104);
|
|
70
|
-
});
|
|
71
|
-
});
|