From 34e5a91dc66e1d249fd3e7a9e081a331abcf0667 Mon Sep 17 00:00:00 2001 From: Sidney Bell Date: Tue, 9 Apr 2019 08:56:07 -0700 Subject: [PATCH] Add `calculate_qc_metrics` to `prepare (#697) * Calculate QC metrics * Add QC metrics to prepare section of readme * Add pointer to scanpy qc metrics function * Don't explicitly pass qc flag as arg * Add explicit toggle for run-qc/skip-qc * Move qc metrics calculation to separate step/function --- README.md | 2 +- server/cli/prepare.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cf0f6ec1..9685e97f 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ To prepare from an existing `.h5ad` file use cellxgene prepare dataset.h5ad --output=dataset-processed.h5ad ``` -This will load the input data, perform PCA and nearest neighbor calculations, compute `umap` and `tsne` layouts and `louvain` cluster assignments, and save the results in a new file called `dataset-processed.h5ad` that can be loaded using `cellxgene launch`. Data can be loaded from several formats, including `.h5ad` `.loom` and a `10-Genomics-formatted` `mtx` directory. Several options are available, including running one of the preprocessing `recipes` included with `scanpy`, which include steps like cell filtering and gene selection. To learn more about the `recipes` please see the `scanpy` [documentation](https://scanpy.readthedocs.io/en/latest/api/index.html#recipes). +This will load the input data, calculate QC metrics, perform PCA and nearest neighbor calculations, compute `umap` and `tsne` layouts and `louvain` cluster assignments, and save the results in a new file called `dataset-processed.h5ad` that can be loaded using `cellxgene launch`. Data can be loaded from several formats, including `.h5ad` `.loom` and a `10-Genomics-formatted` `mtx` directory. Several options are available, including running one of the preprocessing `recipes` included with `scanpy`, which include steps like cell filtering and gene selection. To learn more about the `recipes` please see the `scanpy` [documentation](https://scanpy.readthedocs.io/en/latest/api/index.html#recipes). Depending on the options chosen, `prepare` can take a long time to run (a few minutes for datasets with 10-100k cells, up to an hour or more for datasets with >100k cells). If you want `prepare` to run faster we recommend using the `sparse` option and only computing the layout for `umap`, using a call like this diff --git a/server/cli/prepare.py b/server/cli/prepare.py index ed031dc7..bad6b18c 100644 --- a/server/cli/prepare.py +++ b/server/cli/prepare.py @@ -30,6 +30,10 @@ from scipy.sparse.csc import csc_matrix @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="") @click.option("--set-var-names", default="", help="Named field to set as index for var.", metavar="") +@click.option( + "--run-qc/--skip-qc", default=True, is_flag=True, + help="Whether to calculate QC metrics (saved to adata.obs and adata.var). \ + See scanpy.pp.calculate_qc_metrics for details.", show_default=True) @click.option( "--make-obs-names-unique", default=True, is_flag=True, help="Ensure obs index is unique.", show_default=True ) @@ -46,6 +50,7 @@ def prepare( overwrite, set_obs_names, set_var_names, + run_qc, make_obs_names_unique, make_var_names_unique, ): @@ -115,7 +120,11 @@ def prepare( click.echo("Warning: obs index is not unique") if not adata._var.index.is_unique: click.echo("Warning: var index is not unique") + return adata + def calculate_qc_metrics(adata): + if run_qc: + sc.pp.calculate_qc_metrics(adata, inplace=True) return adata def make_sparse(adata): @@ -171,7 +180,12 @@ def prepare( sc.pl.tsne(adata, color="louvain", palette=palette, save="_louvain") def show_step(item): + if run_qc: + qc_name = "Calculating QC metrics" + else: + qc_name = "Skipping QC" names = { + "calculate_qc_metrics": qc_name, "make_sparse": "Ensuring sparsity", "run_recipe": f'Running preprocessing recipe "{recipe}"', "run_pca": "Running PCA", @@ -182,7 +196,7 @@ def prepare( if item is not None: return names[item.__name__] - steps = [make_sparse, run_recipe, run_pca, run_neighbors, run_louvain, run_layout] + steps = [calculate_qc_metrics, make_sparse, run_recipe, run_pca, run_neighbors, run_louvain, run_layout] click.echo(f"[cellxgene] Loading data from {data}, please wait...") adata = load_data(data)