Skip to content

Commit

Permalink
fix: clean up the cli (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bento007 authored Dec 3, 2024
1 parent 5293502 commit bc905f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
37 changes: 18 additions & 19 deletions cellxgene_schema_cli/cellxgene_schema/cli.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import logging
import sys

import click

logger = logging.getLogger("cellxgene_schema")


@click.group(
name="schema",
subcommand_metavar="COMMAND <args>",
short_help="Apply and validate the cellxgene data integration schema to an h5ad file.",
context_settings=dict(max_content_width=85, help_option_names=["-h", "--help"]),
)
def schema_cli():
pass
@click.option("-v", "--verbose", help="When present will set logging level to debug", is_flag=True)
def schema_cli(verbose):
logging.basicConfig(level=logging.ERROR)
logger.setLevel(logging.DEBUG if verbose else logging.INFO)


@click.command(
@schema_cli.command(
name="validate",
short_help="Check that an h5ad follows the cellxgene data integration schema.",
help="Check that an h5ad follows the cellxgene data integration schema. If validation fails this command will "
Expand All @@ -31,27 +36,25 @@ def schema_cli():
type=click.Path(exists=False, dir_okay=False, writable=True),
)
@click.option("-i", "--ignore-labels", help="Ignore ontology labels when validating", is_flag=True)
@click.option("-v", "--verbose", help="When present will set logging level to debug", is_flag=True)
def schema_validate(h5ad_file, add_labels_file, ignore_labels, verbose):
def schema_validate(h5ad_file, add_labels_file, ignore_labels):
# Imports are very slow so we defer loading until Click arg validation has passed

print("Loading dependencies")
logger.info("Loading dependencies")
try:
import anndata # noqa: F401
except ImportError:
raise click.ClickException("[cellxgene] cellxgene-schema requires anndata") from None

print("Loading validator modules")
logger.info("Loading validator modules")
from .validate import validate

is_valid, _, _ = validate(h5ad_file, add_labels_file, ignore_labels=ignore_labels, verbose=verbose)
is_valid, _, _ = validate(h5ad_file, add_labels_file, ignore_labels=ignore_labels)
if is_valid:
sys.exit(0)
else:
sys.exit(1)


@click.command(
@schema_cli.command(
name="remove-labels",
short_help="Create a copy of an h5ad without portal-added labels",
help="Create a copy of an h5ad without portal-added labels.",
Expand All @@ -61,24 +64,24 @@ def schema_validate(h5ad_file, add_labels_file, ignore_labels, verbose):
def remove_labels(input_file, output_file):
from .remove_labels import AnnDataLabelRemover

print("Loading dependencies")
logger.info("Loading dependencies")
try:
import anndata # noqa: F401
except ImportError:
raise click.ClickException("[cellxgene] cellxgene-schema requires anndata") from None

print(f"Loading h5ad from {input_file}")
logger.info(f"Loading h5ad from {input_file}")
adata = anndata.read_h5ad(input_file)
anndata_label_remover = AnnDataLabelRemover(adata)
if not anndata_label_remover.schema_def:
return
print("Removing labels")
logger.info("Removing labels")
anndata_label_remover.remove_labels()
print(f"Labels have been removed. Writing to {output_file}")
logger.info(f"Labels have been removed. Writing to {output_file}")
anndata_label_remover.adata.write(output_file, compression="gzip")


@click.command(
@schema_cli.command(
name="migrate",
short_help="Convert an h5ad to the latest schema version.",
help="Convert an h5ad from the previous to latest minor schema version. No validation will be "
Expand All @@ -94,9 +97,5 @@ def migrate(input_file, output_file, collection_id, dataset_id):
migrate(input_file, output_file, collection_id, dataset_id)


schema_cli.add_command(schema_validate)
schema_cli.add_command(migrate)
schema_cli.add_command(remove_labels)

if __name__ == "__main__":
schema_cli()
4 changes: 0 additions & 4 deletions cellxgene_schema_cli/cellxgene_schema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2130,10 +2130,6 @@ def validate(

# Perform validation
start = datetime.now()
if verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO, format="%(message)s")
validator = Validator(
ignore_labels=ignore_labels,
)
Expand Down

0 comments on commit bc905f4

Please sign in to comment.