Compare commits

...
Author SHA1 Message Date
Timmy Huang afb4ba906b server update 2024-01-24 10:16:38 -08:00
Timmy Huang ede95bfe4b update requirements.txt 2024-01-23 10:56:56 -08:00
Timmy Huang 7d61f47a31 updated visium branch 2024-01-23 10:48:27 -08:00
Emanuele Bezzi 11690eb745 Add README-visium 2021-12-22 16:41:11 -05:00
Emanuele Bezzi eac84c5d74 Clean comments and logs 2021-12-21 11:33:38 -05:00
Emanuele Bezzi cbf3ba240a Put scaling back in the backend 2021-12-09 14:12:16 -05:00
Emanuele Bezzi 2fe9cc4aac Small fix 2021-12-08 17:20:06 -05:00
Emanuele Bezzi 8b07e57257 Connect button 2021-12-08 16:37:22 -05:00
Colin Megill f4c4ac5bda undable config and conditional graph render of image 2021-12-08 11:54:22 -08:00
Emanuele Bezzi febf582a0b Merge branch 'visium-beta' of github.com:chanzuckerberg/cellxgene into visium-beta 2021-12-07 19:42:53 -05:00
Emanuele Bezzi efe3bf7a72 Parametrization 2021-12-07 19:42:40 -05:00
Colin Megill b411fca5a3 auto switch spatial off 2021-12-07 16:32:55 -08:00
Colin Megill caa1526eb6 intent 2021-12-07 15:59:05 -08:00
Colin Megill 99c8f37a60 button, reducer state 2021-12-07 15:53:56 -08:00
Emanuele Bezzi b048bbfd0c Checkpoint 2021-12-06 14:57:10 -05:00
Emanuele Bezzi b1ff638879 Checkpoint 2021-12-05 12:58:21 -05:00
Emanuele Bezzi db0f50d011 Add frontend 2021-12-01 16:49:13 -05:00
Emanuele Bezzi 54d4de431c Add backend endpoint 2021-12-01 12:02:45 -05:00
27 changed files with 6165 additions and 28961 deletions
+20
View File
@@ -0,0 +1,20 @@
# Cellxgene Visium Beta
## How it works
1. Launch `cellxgene` as normal.
1. If the loaded dataset has spatial information available, the image data will be loaded on startup.
1. On the toolbar, next to the Zoom icon, a `Toggle image` button will now appear. Click on it and the image will be added as an underlay.
1. You can now use any `cellxgene` functionality and the image will still be present. If you pan and zoom, the image will also be panned and zoomed.
1. If you want to hide the image, you can click on `Toggle image` again
In order for the image to be displayed with the correct size and alignment, the H5AD needs to have a few requirements. See the following section to learn more.
## h5ad requirements
1. The spatial embedding layer should be contained in `obsm` and be named `X_spatial`. Other layers can exist, but only this one will have the spatial feature enabled.
2. A `spatial` dict needs to be defined in the `uns` dictionary.
3. Inside the `spatial` dict, an `images` dict must be defined.
4. The `images` dict must contain a `hires` key, which should reference an image encoded as an RGB matrix (i.e., a three-dimensional matrix of size `height x width x 3` where the final dimension has the RGB values for each pixel)
5. The `images` dict must contain a `scalefactors` dict. This should in turn contain a `tissue_hires_scalef` key, which should reference a floating point number.
Moreover, in order to have the image correctly aligned with the dots, the following must be true:
1. `tissue_hires_scalef` should represent the ratio between the embedding layer `X_spatial` and the image matrix. In particular, if you multiply `X_spatial` by `tissue_hires_scalef`, you should obtain an array of points that ovelap the tissue image if you plot them in a plane.
@@ -2,7 +2,6 @@ const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
const ScriptExtHtmlWebpackPlugin = require("script-ext-html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { merge } = require("webpack-merge");
@@ -73,9 +72,6 @@ const devConfig = {
CXG_SERVER_PORT: process.env.CXG_SERVER_PORT || "5005",
}),
}),
new ScriptExtHtmlWebpackPlugin({
async: "obsolete",
}),
],
infrastructureLogging: {
level: "warn",
@@ -3,8 +3,6 @@ const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const TerserJSPlugin = require("terser-webpack-plugin");
const CleanCss = require("clean-css");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
@@ -27,12 +25,7 @@ const prodConfig = {
},
optimization: {
minimize: true,
minimizer: [
new TerserJSPlugin({}),
new OptimizeCSSAssetsPlugin({
cssProcessor: CleanCss,
}),
],
minimizer: [new TerserJSPlugin({})],
},
devtool: "source-map",
module: {
@@ -1,22 +1,12 @@
const path = require("path");
const fs = require("fs");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ObsoleteWebpackPlugin = require("obsolete-webpack-plugin");
// eslint-disable-next-line @blueprintjs/classes-constants -- incorrect match
const ScriptExtHtmlWebpackPlugin = require("script-ext-html-webpack-plugin");
const src = path.resolve("src");
const nodeModules = path.resolve("node_modules");
const publicPath = "";
const rawObsoleteHTMLTemplate = fs.readFileSync(
`${__dirname}/obsoleteHTMLTemplate.html`,
"utf8"
);
const obsoleteHTMLTemplate = rawObsoleteHTMLTemplate.replace(/'/g, '"');
module.exports = {
entry: [
"core-js",
@@ -61,14 +51,4 @@ module.exports = {
},
],
},
plugins: [
new ObsoleteWebpackPlugin({
name: "obsolete",
template: obsoleteHTMLTemplate,
promptOnNonTargetBrowser: false,
}),
new ScriptExtHtmlWebpackPlugin({
async: "obsolete",
}),
],
};
+5541 -28871
View File
File diff suppressed because it is too large Load Diff
+2 -3
View File
@@ -84,6 +84,8 @@
"@babel/plugin-proposal-function-bind": "^7.10.5",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
"@babel/plugin-proposal-optional-chaining": "^7.10.4",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-react-constant-elements": "^7.13.13",
"@babel/plugin-transform-runtime": "^7.13.15",
"@babel/preset-env": "^7.13.15",
@@ -133,12 +135,9 @@
"lodash.map": "^4.6.0",
"lodash.zip": "^4.2.0",
"mini-css-extract-plugin": "^1.5.0",
"obsolete-webpack-plugin": "^0.5.6",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"prettier": "^2.0.5",
"puppeteer": "^8.0.0",
"rimraf": "^3.0.2",
"script-ext-html-webpack-plugin": "^2.1.4",
"serve-favicon": "^2.5.0",
"terser-webpack-plugin": "^5.1.1",
"webpack": "^5.34.0",
+2
View File
@@ -8,6 +8,7 @@ import {
import { loadUserColorConfig } from "../util/stateManager/colorHelpers";
import * as selnActions from "./selection";
import * as annoActions from "./annotation";
import * as spatialActions from "./spatial";
import * as viewActions from "./viewStack";
import * as embActions from "./embedding";
import * as genesetActions from "./geneset";
@@ -272,4 +273,5 @@ export default {
genesetDelete: genesetActions.genesetDelete,
genesetAddGenes: genesetActions.genesetAddGenes,
genesetDeleteGenes: genesetActions.genesetDeleteGenes,
requestSpatialMetadata: spatialActions.requestSpatialMetadata,
};
+35
View File
@@ -0,0 +1,35 @@
import * as globals from "../globals";
export const requestSpatialMetadata = () => async (dispatch) => {
dispatch({ type: "request spatial metadata started" });
try {
const res = await fetch(
`${globals.API.prefix}${globals.API.version}spatial/meta`,
{
method: "GET",
headers: new Headers({
Accept: "application/json",
"Content-Type": "application/json",
}),
credentials: "include",
}
);
if (!res.ok || res.headers.get("Content-Type") !== "application/json") {
return null; // TODO need a dispatch //dispatchDiffExpErrors(dispatch, res);
}
const response = await res.json();
/* then send the success case action through */
return dispatch({
type: "request spatial metadata success",
data: response,
});
} catch (error) {
return dispatch({
type: "request spatial metadata error",
error,
});
}
};
+2
View File
@@ -23,6 +23,8 @@ class App extends React.Component {
componentDidMount() {
const { dispatch } = this.props;
dispatch(actions.requestSpatialMetadata());
/* listen for url changes, fire one when we start the app up */
window.addEventListener("popstate", this._onURLChanged);
this._onURLChanged();
+16 -5
View File
@@ -16,10 +16,11 @@ import actions from "../../actions";
import { getDiscreteCellEmbeddingRowIndex } from "../../util/stateManager/viewStackHelpers";
@connect((state) => ({
layoutChoice: state.layoutChoice, // TODO: really should clean up naming, s/layout/embedding/g
schema: state.annoMatrix?.schema,
crossfilter: state.obsCrossfilter,
}))
imageUnderlay: state.imageUnderlay,
layoutChoice: state.layoutChoice, // TODO: really should clean up naming, s/layout/embedding/g
schema: state.annoMatrix?.schema,
crossfilter: state.obsCrossfilter,
}))
class Embedding extends React.PureComponent {
constructor(props) {
super(props);
@@ -27,8 +28,18 @@ class Embedding extends React.PureComponent {
}
handleLayoutChoiceChange = (e) => {
const { dispatch } = this.props;
const { dispatch, imageUnderlay } = this.props;
dispatch(actions.layoutChoiceAction(e.currentTarget.value));
// if we just switched off spatial, if the image is on, turn it off
if (
imageUnderlay.isActive &&
e.target.value !== globals.spatialEmbeddingKeyword
) {
dispatch({
type: "toggle image underlay",
});
}
};
render() {
@@ -0,0 +1,62 @@
export default function drawSpatialImageRegl(regl) {
return regl({
frag: `
precision mediump float;
// our texture
uniform sampler2D u_image;
// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;
void main() {
gl_FragColor = texture2D(u_image, v_texCoord);
}`,
vert: `
attribute vec2 a_position;
attribute vec2 a_texCoord;
uniform vec2 u_resolution;
uniform mat3 projView;
varying vec2 v_texCoord;
void main() {
// convert the rectangle from pixels to 0.0 to 1.0
vec3 pos = vec3(a_position, 1.);
vec2 zeroToOne = pos.xy / u_resolution;
// convert from 0->1 to 0->2
vec2 zeroToTwo = zeroToOne * 2.0;
// convert from 0->2 to -1->+1 (clipspace)
vec2 clipSpace = zeroToTwo - 1.0;
vec3 pos2 = projView * vec3(clipSpace, 1.);
gl_Position = vec4(pos2.xy , 0, 1);
// pass the texCoord to the fragment shader
// The GPU will interpolate this value between points.
v_texCoord = a_texCoord;
}`,
attributes: {
a_texCoord: [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0],
a_position: regl.prop("rectCoords"),
},
uniforms: {
projView: regl.prop("projView"),
u_image: regl.prop("spatialImageAsTexture"),
color: [1, 0, 0, 1],
u_resolution: [regl.prop("imageWidth"), regl.prop("imageHeight")],
image_width: regl.prop("imageWidth"),
// translate:
},
count: 6,
});
}
+79 -27
View File
@@ -14,6 +14,7 @@ import {
createColorTable,
createColorQuery,
} from "../../util/stateManager/colorHelpers";
import _drawSpatialImage from "./drawSpatialImageRegl";
import * as globals from "../../globals";
import GraphOverlayLayer from "./overlays/graphOverlayLayer";
@@ -77,6 +78,8 @@ function createModelTF() {
colors: state.colors,
pointDilation: state.pointDilation,
genesets: state.genesets.genesets,
spatial: state.spatial.metadata,
imageUnderlay: state.imageUnderlay,
}))
class Graph extends React.Component {
static createReglState(canvas) {
@@ -87,6 +90,7 @@ class Graph extends React.Component {
const camera = _camera(canvas);
const regl = _regl(canvas);
const drawPoints = _drawPoints(regl);
const drawSpatialImage = _drawSpatialImage(regl);
// preallocate webgl buffers
const pointBuffer = regl.buffer();
@@ -100,6 +104,7 @@ class Graph extends React.Component {
pointBuffer,
colorBuffer,
flagBuffer,
drawSpatialImage,
};
}
@@ -232,6 +237,8 @@ class Graph extends React.Component {
pointBuffer: null,
colorBuffer: null,
flagBuffer: null,
drawSpatialImage: null,
spatial: null,
// component rendering derived state - these must stay synchronized
// with the reducer state they were generated from.
@@ -317,7 +324,10 @@ class Graph extends React.Component {
if (e.type !== "wheel") e.preventDefault();
if (camera.handleEvent(e, projectionTF)) {
this.renderCanvas();
this.setState((state) => ({ ...state, updateOverlay: !state.updateOverlay }));
this.setState((state) => ({
...state,
updateOverlay: !state.updateOverlay,
}));
}
};
@@ -509,6 +519,14 @@ class Graph extends React.Component {
return { toolSVG: newToolSVG, tool, container };
};
loadTextureFromUrl = (src) =>
new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = src;
});
fetchAsyncProps = async (props) => {
const {
annoMatrix,
@@ -517,6 +535,8 @@ class Graph extends React.Component {
crossfilter,
pointDilation,
viewport,
spatial,
imageUnderlay,
} = props.watchProps;
const { modelTF } = this.state;
@@ -524,7 +544,8 @@ class Graph extends React.Component {
annoMatrix,
layoutChoice,
colorsProp,
pointDilation
pointDilation,
imageUnderlay
);
const { currentDimNames } = layoutChoice;
@@ -551,6 +572,10 @@ class Graph extends React.Component {
pointDilationLabel
);
this.spatialImage = await this.loadTextureFromUrl(
"/api/v0.2/spatial/image"
);
const { width, height } = viewport;
return {
positions,
@@ -558,6 +583,8 @@ class Graph extends React.Component {
flags,
width,
height,
spatial,
imageUnderlay,
};
};
@@ -721,6 +748,7 @@ class Graph extends React.Component {
flagBuffer,
camera,
projectionTF,
drawSpatialImage,
} = this.state;
this.renderPoints(
regl,
@@ -729,12 +757,14 @@ class Graph extends React.Component {
pointBuffer,
flagBuffer,
camera,
projectionTF
projectionTF,
drawSpatialImage
);
});
updateReglAndRender(asyncProps, prevAsyncProps) {
const { positions, colors, flags, height, width } = asyncProps;
const { positions, colors, flags, height, width, imageUnderlay } =
asyncProps;
this.cachedAsyncProps = asyncProps;
const { pointBuffer, colorBuffer, flagBuffer } = this.state;
let needToRenderCanvas = false;
@@ -754,6 +784,9 @@ class Graph extends React.Component {
flagBuffer({ data: flags, dimension: 1 });
needToRenderCanvas = true;
}
if (imageUnderlay !== prevAsyncProps?.imageUnderlay) {
needToRenderCanvas = true;
}
if (needToRenderCanvas) this.renderCanvas();
}
@@ -797,20 +830,25 @@ class Graph extends React.Component {
pointBuffer,
flagBuffer,
camera,
projectionTF
projectionTF,
drawSpatialImage
) {
const { annoMatrix } = this.props;
const { annoMatrix, spatial, imageUnderlay } = this.props;
if (!this.reglCanvas || !annoMatrix) return;
const { schema } = annoMatrix;
const cameraTF = camera.view();
const projView = mat3.multiply(mat3.create(), projectionTF, cameraTF);
const { width, height } = this.reglCanvas;
const imW = spatial.data.imageWidth;
const imH = spatial.data.imageHeight;
regl.poll();
regl.clear({
depth: 1,
color: [1, 1, 1, 1],
color: [0, 0, 0, 0],
});
drawPoints({
distance: camera.distance(),
color: colorBuffer,
@@ -821,6 +859,19 @@ class Graph extends React.Component {
nPoints: schema.dataframe.nObs,
minViewportDimension: Math.min(width, height),
});
if (imageUnderlay?.isActive) {
drawSpatialImage({
projView,
imageWidth: imW,
imageHeight: imH,
rectCoords: [0, 0, imW, 0, 0, imH, 0, imH, imW, 0, imW, imH],
spatialImageAsTexture: regl.texture({
data: this.spatialImage,
wrapS: "clamp",
wrapT: "clamp",
}),
});
}
regl._gl.flush();
}
@@ -832,6 +883,8 @@ class Graph extends React.Component {
layoutChoice,
pointDilation,
crossfilter,
spatial,
imageUnderlay,
} = this.props;
const { modelTF, projectionTF, camera, viewport, regl } = this.state;
const cameraTF = camera?.view()?.slice();
@@ -902,6 +955,8 @@ class Graph extends React.Component {
pointDilation,
crossfilter,
viewport,
spatial,
imageUnderlay,
}}
>
<Async.Pending initial>
@@ -951,32 +1006,29 @@ const ErrorLoading = ({ displayName, error, width, height }) => {
);
};
const StillLoading = ({ displayName, width, height }) =>
const StillLoading = ({ displayName, width, height }) => (
/*
Render a busy/loading indicator
*/
(
<div
style={{
position: "fixed",
fontWeight: 500,
top: height / 2,
width,
}}
>
<div
style={{
position: "fixed",
fontWeight: 500,
top: height / 2,
width,
display: "flex",
justifyContent: "center",
justifyItems: "center",
alignItems: "center",
}}
>
<div
style={{
display: "flex",
justifyContent: "center",
justifyItems: "center",
alignItems: "center",
}}
>
<Button minimal loading intent="primary" />
<span style={{ fontStyle: "italic" }}>Loading {displayName}</span>
</div>
<Button minimal loading intent="primary" />
<span style={{ fontStyle: "italic" }}>Loading {displayName}</span>
</div>
)
;
</div>
);
export default Graph;
+27
View File
@@ -28,6 +28,8 @@ import { getEmbSubsetView } from "../../util/stateManager/viewStackHelpers";
subsetPossible,
subsetResetPossible,
graphInteractionMode: state.controls.graphInteractionMode,
imageUnderlay: state.imageUnderlay,
layoutChoice: state.layoutChoice, // TODO: really should clean up naming, s/layout/embedding/g
clipPercentileMin: Math.round(100 * (annoMatrix?.clipRange?.[0] ?? 0)),
clipPercentileMax: Math.round(100 * (annoMatrix?.clipRange?.[1] ?? 1)),
userDefinedGenes: state.controls.userDefinedGenes,
@@ -206,6 +208,8 @@ class MenuBar extends React.PureComponent {
colorAccessor,
subsetPossible,
subsetResetPossible,
imageUnderlay,
layoutChoice,
} = this.props;
const { pendingClipPercentiles } = this.state;
@@ -268,6 +272,29 @@ class MenuBar extends React.PureComponent {
disabled={!isColoredByCategorical}
/>
</Tooltip>
{layoutChoice?.available?.includes(globals.spatialEmbeddingKeyword) && (
<ButtonGroup className={styles.menubarButton}>
<Tooltip
content="Toggle image"
position="bottom"
hoverOpenDelay={globals.tooltipHoverOpenDelay}
>
<AnchorButton
type="button"
data-testid="toggle-image-underlay"
icon="media"
intent={imageUnderlay.isActive ? "primary" : "none"}
active={imageUnderlay.isActive}
onClick={() => {
dispatch({
type: "toggle image underlay",
});
}}
/>
</Tooltip>
</ButtonGroup>
)}
<ButtonGroup className={styles.menubarButton}>
<Tooltip
content={selectionTooltip}
+3
View File
@@ -2,6 +2,9 @@ import { Colors } from "@blueprintjs/core";
import { dispatchNetworkErrorMessageToUser } from "./util/actionHelpers";
import ENV_DEFAULT from "../../environment.default.json";
// visium embedding word, spatial image underlay
export const spatialEmbeddingKeyword = "spatial";
/* overflow category values are created using this string */
export const overflowCategoryLabel = ": all other labels";
+14
View File
@@ -0,0 +1,14 @@
const imageUnderlay = (state = { isActive: false }, action) => {
switch (action.type) {
case "toggle image underlay":
return {
...state,
isActive: !state.isActive,
};
default:
return state;
}
};
export default imageUnderlay;
+5
View File
@@ -11,6 +11,7 @@ import continuousSelection from "./continuousSelection";
import graphSelection from "./graphSelection";
import colors from "./colors";
import differential from "./differential";
import spatial from "./spatial";
import layoutChoice from "./layoutChoice";
import controls from "./controls";
import annotations from "./annotations";
@@ -19,6 +20,7 @@ import genesetsUI from "./genesetsUI";
import autosave from "./autosave";
import centroidLabels from "./centroidLabels";
import pointDialation from "./pointDilation";
import imageUnderlay from "./imageUnderlay";
import { gcMiddleware as annoMatrixGC } from "../annoMatrix";
import undoableConfig from "./undoableConfig";
@@ -38,7 +40,9 @@ const Reducer = undoable(
["colors", colors],
["controls", controls],
["differential", differential],
["spatial", spatial],
["centroidLabels", centroidLabels],
["imageUnderlay", imageUnderlay],
["pointDilation", pointDialation],
["autosave", autosave],
]),
@@ -51,6 +55,7 @@ const Reducer = undoable(
"colors",
"controls",
"differential",
"spatial",
"layoutChoice",
"centroidLabels",
"genesets",
+34
View File
@@ -0,0 +1,34 @@
const Spatial = (
state = {
loading: null,
error: null,
metadata: null,
},
action
) => {
switch (action.type) {
case "request spatial metadata started":
return {
...state,
loading: true,
error: null,
};
case "request spatial metadata success":
return {
...state,
error: null,
loading: false,
metadata: action,
};
case "request spatial metadata error":
return {
...state,
loading: false,
error: action.data,
};
default:
return state;
}
};
export default Spatial;
+3
View File
@@ -52,6 +52,9 @@ const skipOnActions = new Set([
"geneset: disable add new genes mode",
"geneset: activate rename geneset mode",
"geneset: disable rename geneset mode",
/* spatial */
"toggle image underlay",
]);
/*
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2017-2021 Chan Zuckerberg Initiative
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,151 @@
Metadata-Version: 2.1
Name: cellxgene
Version: 1.0.0
Summary: Web application for exploration of large scale scRNA-seq datasets
Home-page: https://github.com/chanzuckerberg/cellxgene
Author: Chan Zuckerberg Initiative
Author-email: cellxgene@chanzuckerberg.com
License: MIT
Classifier: Framework :: Flask
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Programming Language :: JavaScript
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: anndata >=0.7.6
Requires-Dist: boto3 >=1.12.18
Requires-Dist: click >=7.1.2
Requires-Dist: Flask >=1.0.2
Requires-Dist: Flask-Compress >=1.4.0
Requires-Dist: Flask-Cors >=3.0.9
Requires-Dist: Flask-RESTful >=0.3.6
Requires-Dist: flask-server-timing >=0.1.2
Requires-Dist: flask-talisman >=0.7.0
Requires-Dist: flatbuffers <2.0.0,>=1.11.0
Requires-Dist: flatten-dict >=0.2.0
Requires-Dist: fsspec <0.8.0,>=0.4.4
Requires-Dist: gunicorn >=20.0.4
Requires-Dist: h5py >=3.0.0
Requires-Dist: matplotlib >=3.5.0
Requires-Dist: numba >=0.51.2
Requires-Dist: numpy >=1.17.5
Requires-Dist: packaging >=20.0
Requires-Dist: pandas !=1.1,>=1.0
Requires-Dist: PyYAML >=5.4
Requires-Dist: scipy >=1.4
Requires-Dist: requests >=2.22.0
Requires-Dist: s3fs ==0.4.2
Provides-Extra: prepare
Requires-Dist: python-igraph >=0.8 ; extra == 'prepare'
Requires-Dist: louvain >=0.6 ; extra == 'prepare'
Requires-Dist: scanpy ; extra == 'prepare'
Requires-Dist: umap-learn <0.5.0 ; extra == 'prepare'
<img src="./docs/cellxgene-logo.png" width="300">
_an interactive explorer for single-cell transcriptomics data_
[![DOI](https://zenodo.org/badge/105615409.svg)](https://zenodo.org/badge/latestdoi/105615409) [![PyPI](https://img.shields.io/pypi/v/cellxgene)](https://pypi.org/project/cellxgene/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/cellxgene)](https://pypistats.org/packages/cellxgene) [![GitHub last commit](https://img.shields.io/github/last-commit/chanzuckerberg/cellxgene)](https://github.com/chanzuckerberg/cellxgene/pulse)
[![Push Tests](https://github.com/chanzuckerberg/cellxgene/workflows/Push%20Tests/badge.svg)](https://github.com/chanzuckerberg/cellxgene/actions?query=workflow%3A%22Push+Tests%22)
[![Compatibility Tests](https://github.com/chanzuckerberg/cellxgene/workflows/Compatibility%20Tests/badge.svg)](https://github.com/chanzuckerberg/cellxgene/actions?query=workflow%3A%22Compatibility+Tests%22)
![Code Coverage](https://codecov.io/gh/chanzuckerberg/cellxgene/branch/main/graph/badge.svg)
cellxgene Desktop (pronounced "cell-by-gene") is an interactive data explorer for single-cell datasets, such as those coming from the [Human Cell Atlas](https://humancellatlas.org). Leveraging modern web development techniques to enable fast visualizations of at least 1 million cells, we hope to enable biologists and computational researchers to explore their data.
Whether you need to visualize one thousand cells or one million, cellxgene Desktop helps you gain insight into your single-cell data.
<img src="https://github.com/chanzuckerberg/cellxgene/raw/main/docs/images/crossfilter.gif" width="350" height="200" hspace="30"><img src="https://github.com/chanzuckerberg/cellxgene/raw/main/docs/images/category-breakdown.gif" width="350" height="200" hspace="30">
# Getting started
### The comprehensive guide to cellxgene Desktop
[The cellxgene documentation is your one-stop-shop for information about cellxgene Desktop](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/README.md)! You may be particularly interested in:
- Seeing [what cellxgene Desktop can do](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/explore-data/explorer-tutorials.md)
- Learning more about cellxgene [installation](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/desktop/install.md) and [usage](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/desktop/quick-start.md#quick-start-1)
- [Preparing your own data](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/desktop/data-reqs.md) for use in cellxgene Desktop
- Checking out [our roadmap](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/roadmap.md) for future development
- [Contributing](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/contribute.md) to cellxgene Desktop
### Quick start
To install cellxgene Desktop you need Python 3.6+. We recommend [installing cellxgene Desktop into a conda or virtual environment.](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/desktop/install.md)
Install the package.
```bash
pip install cellxgene
```
Launch cellxgene Desktop with an example [anndata](https://anndata.readthedocs.io/en/latest/) file
```bash
cellxgene launch https://cellxgene-example-data.czi.technology/pbmc3k.h5ad
```
To explore more datasets already formatted for cellxgene Desktop, check out the [Demo data](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/desktop/quick-start.md#example-datasets) or
see [Preparing your data](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/desktop/data-reqs.md) to learn more about formatting your own
data for cellxgene Desktop.
### Supported browsers
cellxgene Desktop currently supports the following browsers:
- Google Chrome 61+
- Edge 15+
- Firefox 60+
Please [file an issue](https://github.com/chanzuckerberg/cellxgene/issues/new/choose) if you would like us to add support for an unsupported browser.
### Finding help
We'd love to hear from you!
For questions, suggestions, or accolades, [join the `#cellxgene-users` channel on the CZI Science Slack](https://join-cellxgene-users.herokuapp.com/) and say "hi!".
For any errors, [report bugs on Github](https://github.com/chanzuckerberg/cellxgene/issues).
# Developing with cellxgene Desktop
### Contributing
We warmly welcome contributions from the community! Please see our [contributing guide](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/contribute.md) and don't hesitate to open an issue or send a pull request to improve cellxgene Desktop. Please see the [dev_docs](https://github.com/chanzuckerberg/cellxgene/tree/main/dev_docs) for pull request suggestions, unit test details, local documentation preview, and other development specifics.
This project adheres to the Contributor Covenant [code of conduct](https://github.com/chanzuckerberg/.github/blob/master/CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. Please report unacceptable behavior to opensource@chanzuckerberg.com.
### Reuse
This project was started with the sole goal of empowering the scientific community to explore and understand their data.
As such, we encourage other scientific tool builders in academia or industry to adopt the patterns, tools, and code from
this project. All code is freely available for reuse under the [MIT license](https://opensource.org/licenses/MIT).
Before extending cellxgene, we encourage you to reach out to us with ideas or questions. It might be possible that an
extension could be directly contributed, which would make it available for a wider audience, or that it's on our
[roadmap](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/roadmap.md) and under active development.
See the [cellxgene extensions](https://github.com/chanzuckerberg/cellxgene-documentation/blob/main/community-extensions.md) section of our documentation for examples of community use and cellxgene extensions.
### Security
If you believe you have found a security issue, we would appreciate notification. Please send email to <security@chanzuckerberg.com>.
# Inspiration
We've been heavily inspired by several other related single-cell visualization projects, including the [UCSC Cell Browser](http://cells.ucsc.edu/), [Cytoscape](http://www.cytoscape.org/), [Xena](https://xena.ucsc.edu/), [ASAP](https://asap.epfl.ch/), [GenePattern](http://genepattern-notebook.org/), and many others. We hope to explore collaborations where useful as this community works together on improving interactive visualization for single-cell data.
We were inspired by Mike Bostock and the [crossfilter](https://github.com/crossfilter) team for the design of our filtering implementation.
We have been working closely with the [scanpy](https://github.com/theislab/scanpy) team to integrate with their awesome analysis tools. Special thanks to Alex Wolf, Fabian Theis, and the rest of the team for their help during development and for providing an example dataset.
We are eager to explore integrations with other computational backends such as [Seurat](https://github.com/satijalab/seurat) or [Bioconductor](https://github.com/Bioconductor)
@@ -0,0 +1,2 @@
[console_scripts]
cellxgene = server.cli.cli:cli
@@ -0,0 +1,3 @@
build
server
test
+13
View File
@@ -190,6 +190,16 @@ class SummarizeVarAPI(Resource):
def post(self, data_adaptor):
return common_rest.summarize_var_post(request, data_adaptor)
class SpatialImageAPI(Resource):
@rest_get_data_adaptor
def get(self, data_adaptor):
return common_rest.spatial_image_get(request, data_adaptor)
class SpatialMetaAPI(Resource):
@rest_get_data_adaptor
def get(self, data_adaptor):
return data_adaptor.get_spatial_metadata()
def get_api_base_resources(bp_base):
"""Add resources that are accessed from the api url"""
@@ -222,6 +232,9 @@ def get_api_dataroot_resources(bp_dataroot):
# Computation routes
add_resource(DiffExpObsAPI, "/diffexp/obs")
add_resource(LayoutObsAPI, "/layout/obs")
# Spatial routes
add_resource(SpatialImageAPI, "/spatial/image")
add_resource(SpatialMetaAPI, "/spatial/meta")
return api
+46 -3
View File
@@ -4,8 +4,9 @@ import sys
from http import HTTPStatus
import zlib
import json
import numpy as np
from flask import make_response, jsonify, current_app, abort
from flask import make_response, jsonify, current_app, abort, send_file
from werkzeug.urls import url_unquote
from server.common.config.client_config import get_client_config
@@ -293,7 +294,9 @@ def layout_obs_get(request, data_adaptor):
try:
return make_response(
data_adaptor.layout_to_fbs_matrix(fields), HTTPStatus.OK, {"Content-Type": "application/octet-stream"}
data_adaptor.layout_to_fbs_matrix(fields, data_adaptor.get_spatial()),
HTTPStatus.OK,
{"Content-Type": "application/octet-stream"},
)
except (KeyError, DatasetAccessError) as e:
return abort_and_log(HTTPStatus.BAD_REQUEST, str(e), include_exc_info=True)
@@ -379,7 +382,7 @@ def summarize_var_helper(request, data_adaptor, key, raw_query):
HTTPStatus.OK,
{"Content-Type": "application/octet-stream"},
)
except (ValueError) as e:
except ValueError as e:
return abort(HTTPStatus.NOT_FOUND, description=str(e))
except (UnsupportedSummaryMethod, FilterError) as e:
return abort(HTTPStatus.BAD_REQUEST, description=str(e))
@@ -397,3 +400,43 @@ def summarize_var_post(request, data_adaptor):
key = request.args.get("key", default=None)
return summarize_var_helper(request, data_adaptor, key, request.get_data())
def spatial_image_get(request, data_adaptor):
import io
import matplotlib.pyplot
resolution = "hires"
spatial = data_adaptor.get_spatial()
if len(list(spatial)) == 0:
return abort_and_log(HTTPStatus.BAD_REQUEST, "uns does not have spatial information")
library_id = list(spatial)[0]
if len(spatial) > 1:
current_app.logger.warning(f"More than one library found under uns.spatial, using library '{library_id}'")
if "images" not in spatial[library_id]:
return abort_and_log(HTTPStatus.BAD_REQUEST, "spatial information does not contain images")
if resolution not in spatial[library_id]["images"]:
return abort_and_log(
HTTPStatus.BAD_REQUEST, f"spatial information does not contain requested resolution '{resolution}'"
)
response_image = io.BytesIO()
img = spatial[library_id]["images"][resolution]
matplotlib.pyplot.imsave(response_image, img)
response_image.seek(0)
try:
return send_file(response_image, download_name=f"{library_id}-{resolution}.png", mimetype="image/png")
except (KeyError, DatasetAccessError) as e:
return abort_and_log(HTTPStatus.BAD_REQUEST, str(e), include_exc_info=True)
except PrepareError:
return abort_and_log(
HTTPStatus.NOT_IMPLEMENTED,
f"No spatial image available {request.path}",
loglevel=logging.ERROR,
include_exc_info=True,
)
+37
View File
@@ -274,6 +274,43 @@ class AnndataAdaptor(DataAdaptor):
df = df[fields]
return encode_matrix_fbs(df, col_idx=df.columns)
def get_spatial(self):
return self.data.uns["spatial"]
def get_spatial_metadata(self):
spatial = self.get_spatial()
resolution = "hires"
if len(list(spatial)) == 0:
raise Exception("uns does not have spatial information")
library_id = list(spatial)[0]
if "images" not in spatial[library_id]:
raise Exception("spatial information does not contain images")
if resolution not in spatial[library_id]["images"]:
raise Exception(f"spatial information does not contain requested resolution '{resolution}'")
scaleref = spatial[library_id]["scalefactors"][f"tissue_{resolution}_scalef"]
(h, w, _) = spatial[library_id]["images"][resolution].shape
A = self.data.obsm["X_spatial"]
min = np.nanmin(A, axis=0)
max = np.nanmax(A, axis=0)
scale = np.amax(max - min)
translate = 0.5 - ((max - min) / scale / 2)
return {
"imageWidth": w,
"imageHeight": h,
"scaleref": scaleref,
"inverseScale": int(scale),
"inverseTranslate": translate.tolist(),
"inverseMin": min.tolist(),
}
def get_embedding_names(self):
"""
Return pre-computed embeddings.
+44 -18
View File
@@ -340,31 +340,57 @@ class DataAdaptor(metaclass=ABCMeta):
pass
@staticmethod
def normalize_embedding(embedding):
def normalize_embedding(embedding, spatial = None):
"""Normalize embedding layout to meet client assumptions.
Embedding is an ndarray, shape (n_obs, n)., where n is normally 2
Embedding is an ndarray, shape (n_obs, n)., where n is normally 2.
Note: if spatial data is available, the normalization will be done
according to the size of the underlying image
"""
# scale isotropically
try:
min = np.nanmin(embedding, axis=0)
max = np.nanmax(embedding, axis=0)
except RuntimeError:
# indicates entire array was NaN, which should propagate
min = np.NaN
max = np.NaN
if spatial is not None:
scale = np.amax(max - min)
normalized_layout = (embedding - min) / scale
# TODO: sync with the code in spatial_data_get
resolution = "hires"
# translate to center on both axis
translate = 0.5 - ((max - min) / scale / 2)
normalized_layout = normalized_layout + translate
if len(list(spatial)) == 0:
raise Exception("uns does not have spatial information")
library_id = list(spatial)[0]
if "images" not in spatial[library_id]:
raise Exception("spatial information does not contain images")
if resolution not in spatial[library_id]["images"]:
raise Exception(f"spatial information does not contain requested resolution '{resolution}'")
scaleref = spatial[library_id]["scalefactors"][f"tissue_{resolution}_scalef"]
(h, w, _) = spatial[library_id]["images"][resolution].shape
A = embedding * scaleref
A = np.column_stack([A[:, 0] / w, A[:, 1] / h])
normalized_layout = A.astype(dtype=np.float32)
else:
# scale isotropically
try:
min = np.nanmin(embedding, axis=0)
max = np.nanmax(embedding, axis=0)
except RuntimeError:
# indicates entire array was NaN, which should propagate
min = np.NaN
max = np.NaN
scale = np.amax(max - min)
normalized_layout = (embedding - min) / scale
# translate to center on both axis
translate = 0.5 - ((max - min) / scale / 2)
normalized_layout = normalized_layout + translate
normalized_layout = normalized_layout.astype(dtype=np.float32)
return normalized_layout
def layout_to_fbs_matrix(self, fields):
def layout_to_fbs_matrix(self, fields, spatial = None):
"""
return specified embeddings as a flatbuffer, using the cellxgene matrix fbs encoding.
@@ -380,7 +406,7 @@ class DataAdaptor(metaclass=ABCMeta):
with ServerTiming.time("layout.query"):
for ename in embeddings:
embedding = self.get_embedding_array(ename, 2)
normalized_layout = DataAdaptor.normalize_embedding(embedding)
normalized_layout = DataAdaptor.normalize_embedding(embedding, ename == "spatial" and spatial)
layout_data.append(pd.DataFrame(normalized_layout, columns=[f"{ename}_0", f"{ename}_1"]))
with ServerTiming.time("layout.encode"):
+3 -2
View File
@@ -3,7 +3,7 @@
anndata>=0.7.6 # we need to_memory(), added in 0.7.6
boto3>=1.12.18
click>=7.1.2
Flask>=1.0.2
Flask==2.2.3
Flask-Compress>=1.4.0
Flask-Cors>=3.0.9 # CVE-2020-25032
Flask-RESTful>=0.3.6
@@ -14,6 +14,7 @@ flatten-dict>=0.2.0
fsspec>=0.4.4,<0.8.0
gunicorn>=20.0.4
h5py>=3.0.0
matplotlib>=3.5.0
numba>=0.51.2
numpy>=1.17.5
packaging>=20.0
@@ -21,4 +22,4 @@ pandas>=1.0,!=1.1 # pandas 1.1 breaks tests, https://github.com/pandas-dev/pand
PyYAML>=5.4 # CVE-2020-14343
scipy>=1.4
requests>=2.22.0
s3fs==0.4.2
Werkzeug==2.2.3