mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-02 12:28:11 +08:00
Black -- formatter for python (#508)
* Add black * use black to format code * Black version
This commit is contained in:
+70
-26
@@ -13,30 +13,76 @@ from server.app.util.utils import custom_format_warning
|
||||
|
||||
@click.command()
|
||||
@click.argument("data", metavar="<data file>", type=click.Path(exists=True, file_okay=True, dir_okay=False))
|
||||
@click.option("--layout", "-l", type=click.Choice(["umap", "tsne"]), default="umap", show_default=True,
|
||||
help="Method for layout.")
|
||||
@click.option("--diffexp", "-d", type=click.Choice(["ttest"]), default="ttest", show_default=True,
|
||||
help="Method for differential expression.")
|
||||
@click.option(
|
||||
"--layout", "-l", type=click.Choice(["umap", "tsne"]), default="umap", show_default=True, help="Method for layout."
|
||||
)
|
||||
@click.option(
|
||||
"--diffexp",
|
||||
"-d",
|
||||
type=click.Choice(["ttest"]),
|
||||
default="ttest",
|
||||
show_default=True,
|
||||
help="Method for differential expression.",
|
||||
)
|
||||
@click.option("--title", "-t", help="Title to display (if omitted will use file name).", metavar="")
|
||||
@click.option("--verbose", "-v", is_flag=True, default=False, show_default=True,
|
||||
help="Provide verbose output, including warnings and all server requests.")
|
||||
@click.option("--debug", "-d", is_flag=True, default=False, show_default=True,
|
||||
help="Run in debug mode.")
|
||||
@click.option("--open", "-o", "open_browser", is_flag=True, default=False, show_default=True,
|
||||
help="Open the web browser after launch.")
|
||||
@click.option(
|
||||
"--verbose",
|
||||
"-v",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
show_default=True,
|
||||
help="Provide verbose output, including warnings and all server requests.",
|
||||
)
|
||||
@click.option("--debug", "-d", is_flag=True, default=False, show_default=True, help="Run in debug mode.")
|
||||
@click.option(
|
||||
"--open",
|
||||
"-o",
|
||||
"open_browser",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
show_default=True,
|
||||
help="Open the web browser after launch.",
|
||||
)
|
||||
@click.option("--port", "-p", help="Port to run server on.", metavar="", default=5005, show_default=True)
|
||||
@click.option("--obs-names", default=None, metavar="", help="Name of annotation field to use for observations.")
|
||||
@click.option("--var-names", default=None, metavar="", help="Name of annotation to use for variables.")
|
||||
@click.option("--host", default="127.0.0.1", help="Host IP address")
|
||||
@click.option("--max-category-items", default=100, metavar="", show_default=True,
|
||||
help="Limits the number of categorical annotation items displayed.")
|
||||
@click.option("--diffexp-lfc-cutoff", default=0.01, show_default=True,
|
||||
help="Relative expression cutoff used when selecting top N differentially expressed genes")
|
||||
@click.option("--nan-to-num", is_flag=True, default=False, show_default=True,
|
||||
help="Replace all floating point NaN with zero, and infinities with finite numbers")
|
||||
def launch(data, layout, diffexp, title, verbose, debug, obs_names, var_names,
|
||||
open_browser, port, host, max_category_items, diffexp_lfc_cutoff,
|
||||
nan_to_num):
|
||||
@click.option(
|
||||
"--max-category-items",
|
||||
default=100,
|
||||
metavar="",
|
||||
show_default=True,
|
||||
help="Limits the number of categorical annotation items displayed.",
|
||||
)
|
||||
@click.option(
|
||||
"--diffexp-lfc-cutoff",
|
||||
default=0.01,
|
||||
show_default=True,
|
||||
help="Relative expression cutoff used when selecting top N differentially expressed genes",
|
||||
)
|
||||
@click.option(
|
||||
"--nan-to-num",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
show_default=True,
|
||||
help="Replace all floating point NaN with zero, and infinities with finite numbers",
|
||||
)
|
||||
def launch(
|
||||
data,
|
||||
layout,
|
||||
diffexp,
|
||||
title,
|
||||
verbose,
|
||||
debug,
|
||||
obs_names,
|
||||
var_names,
|
||||
open_browser,
|
||||
port,
|
||||
host,
|
||||
max_category_items,
|
||||
diffexp_lfc_cutoff,
|
||||
nan_to_num,
|
||||
):
|
||||
"""Launch the cellxgene data viewer.
|
||||
This web app lets you explore single-cell expression data.
|
||||
Data must be in a format that cellxgene expects, read the
|
||||
@@ -76,10 +122,7 @@ def launch(data, layout, diffexp, title, verbose, debug, obs_names, var_names,
|
||||
# Import Flask app
|
||||
from server.app.app import app
|
||||
|
||||
app.config.update(
|
||||
DATASET_TITLE=title,
|
||||
CXG_API_BASE=api_base
|
||||
)
|
||||
app.config.update(DATASET_TITLE=title, CXG_API_BASE=api_base)
|
||||
|
||||
if not verbose:
|
||||
log = logging.getLogger("werkzeug")
|
||||
@@ -90,7 +133,8 @@ def launch(data, layout, diffexp, title, verbose, debug, obs_names, var_names,
|
||||
# Fix for anaconda python. matplotlib typically expects python to be installed as a framework TKAgg is usually
|
||||
# available and fixes this issue. See https://matplotlib.org/faq/virtualenv_faq.html
|
||||
import matplotlib as mpl
|
||||
mpl.use('TkAgg')
|
||||
|
||||
mpl.use("TkAgg")
|
||||
from server.app.scanpy_engine.scanpy_engine import ScanpyEngine
|
||||
|
||||
args = {
|
||||
@@ -100,7 +144,7 @@ def launch(data, layout, diffexp, title, verbose, debug, obs_names, var_names,
|
||||
"diffexp_lfc_cutoff": diffexp_lfc_cutoff,
|
||||
"obs_names": obs_names,
|
||||
"var_names": var_names,
|
||||
"nan_to_num": nan_to_num
|
||||
"nan_to_num": nan_to_num,
|
||||
}
|
||||
|
||||
try:
|
||||
@@ -117,7 +161,7 @@ def launch(data, layout, diffexp, title, verbose, debug, obs_names, var_names,
|
||||
click.echo("[cellxgene] Type CTRL-C at any time to exit.")
|
||||
|
||||
if not verbose:
|
||||
f = open(devnull, 'w')
|
||||
f = open(devnull, "w")
|
||||
sys.stdout = f
|
||||
|
||||
app.run(host=host, debug=debug, port=port, threaded=True)
|
||||
|
||||
+48
-17
@@ -7,22 +7,48 @@ from scipy.sparse.csc import csc_matrix
|
||||
|
||||
@click.command()
|
||||
@click.argument("data", nargs=1, metavar="<dataset: file or path to data>", required=True)
|
||||
@click.option("--layout", "-l", default=["umap", "tsne"], multiple=True, type=click.Choice(["umap", "tsne"]),
|
||||
help="Layout algorithm", show_default=True)
|
||||
@click.option("--recipe", "-r", default="none", type=click.Choice(["none", "seurat", "zheng17"]),
|
||||
help="Preprocessing to run.", show_default=True)
|
||||
@click.option(
|
||||
"--layout",
|
||||
"-l",
|
||||
default=["umap", "tsne"],
|
||||
multiple=True,
|
||||
type=click.Choice(["umap", "tsne"]),
|
||||
help="Layout algorithm",
|
||||
show_default=True,
|
||||
)
|
||||
@click.option(
|
||||
"--recipe",
|
||||
"-r",
|
||||
default="none",
|
||||
type=click.Choice(["none", "seurat", "zheng17"]),
|
||||
help="Preprocessing to run.",
|
||||
show_default=True,
|
||||
)
|
||||
@click.option("--output", "-o", default="", help="Save a new file to filename.", metavar="<filename>")
|
||||
@click.option("--plotting", "-p", default=False, is_flag=True, help="Whether to generate plots.", show_default=True)
|
||||
@click.option("--sparse", default=False, is_flag=True, help="Whether to force sparsity.", show_default=True)
|
||||
@click.option("--overwrite", default=False, is_flag=True, help="Allow file overwriting.", show_default=True)
|
||||
@click.option("--set-obs-names", default="", help="Named field to set as index for obs.", metavar="<name>")
|
||||
@click.option("--set-var-names", default="", help="Named field to set as index for var.", metavar="<name>")
|
||||
@click.option("--make-obs-names-unique", default=True, is_flag=True,
|
||||
help="Ensure obs index is unique.", show_default=True)
|
||||
@click.option("--make-var-names-unique", default=True, is_flag=True,
|
||||
help="Ensure var index is unique.", show_default=True)
|
||||
def prepare(data, layout, recipe, output, plotting, sparse, overwrite,
|
||||
set_obs_names, set_var_names, make_obs_names_unique, make_var_names_unique):
|
||||
@click.option(
|
||||
"--make-obs-names-unique", default=True, is_flag=True, help="Ensure obs index is unique.", show_default=True
|
||||
)
|
||||
@click.option(
|
||||
"--make-var-names-unique", default=True, is_flag=True, help="Ensure var index is unique.", show_default=True
|
||||
)
|
||||
def prepare(
|
||||
data,
|
||||
layout,
|
||||
recipe,
|
||||
output,
|
||||
plotting,
|
||||
sparse,
|
||||
overwrite,
|
||||
set_obs_names,
|
||||
set_var_names,
|
||||
make_obs_names_unique,
|
||||
make_var_names_unique,
|
||||
):
|
||||
"""Preprocesses data for use with cellxgene.
|
||||
|
||||
This tool runs a series of scanpy routines for preparing a dataset
|
||||
@@ -35,6 +61,7 @@ def prepare(data, layout, recipe, output, plotting, sparse, overwrite,
|
||||
# collect slow imports here to make CLI startup more responsive
|
||||
click.echo("[cellxgene] Starting CLI...")
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use("Agg")
|
||||
import scanpy.api as sc
|
||||
|
||||
@@ -49,8 +76,10 @@ def prepare(data, layout, recipe, output, plotting, sparse, overwrite,
|
||||
output = expanduser(output)
|
||||
|
||||
if not output:
|
||||
click.echo("Warning: No file will be saved, to save the results of cellxgene prepare include "
|
||||
"--output <filename> to save output to a new file")
|
||||
click.echo(
|
||||
"Warning: No file will be saved, to save the results of cellxgene prepare include "
|
||||
"--output <filename> to save output to a new file"
|
||||
)
|
||||
if isfile(output) and not overwrite:
|
||||
raise click.UsageError(f"Cannot overwrite existing file {output}, try using the flag --overwrite")
|
||||
|
||||
@@ -119,9 +148,11 @@ def prepare(data, layout, recipe, output, plotting, sparse, overwrite,
|
||||
try:
|
||||
sc.tl.louvain(adata)
|
||||
except ModuleNotFoundError:
|
||||
click.echo("\nWarning: louvain module is not installed, no clusters will be calculated. "
|
||||
"To fix this please install cellxgene with the optional feature louvain enabled: "
|
||||
"`pip install cellxgene[louvain]`")
|
||||
click.echo(
|
||||
"\nWarning: louvain module is not installed, no clusters will be calculated. "
|
||||
"To fix this please install cellxgene with the optional feature louvain enabled: "
|
||||
"`pip install cellxgene[louvain]`"
|
||||
)
|
||||
|
||||
def run_layout(adata):
|
||||
if len(unique(adata.obs["louvain"].values)) < 10:
|
||||
@@ -142,11 +173,11 @@ def prepare(data, layout, recipe, output, plotting, sparse, overwrite,
|
||||
def show_step(item):
|
||||
names = {
|
||||
"make_sparse": "Ensuring sparsity",
|
||||
"run_recipe": f"Running preprocessing recipe \"{recipe}\"",
|
||||
"run_recipe": f'Running preprocessing recipe "{recipe}"',
|
||||
"run_pca": "Running PCA",
|
||||
"run_neighbors": "Calculating neighbors",
|
||||
"run_louvain": "Calculating clusters",
|
||||
"run_layout": "Computing layout"
|
||||
"run_layout": "Computing layout",
|
||||
}
|
||||
if item is not None:
|
||||
return names[item.__name__]
|
||||
|
||||
Reference in New Issue
Block a user