#!/usr/bin/env python

import click
from scanpy_scripts.click_utils import CommaSeparatedText


@click.command()
@click.argument("anndata_file", type=click.Path(exists=True))
@click.argument("anndata_config", type=click.Path(exists=True))
@click.argument("bundle_dir", type=click.Path(dir_okay=True))
@click.option(
    "--max-rank-for-stats",
    type=click.INT,
    default=5,
    help=(
        "For how many top marker genes should stats (mean, median expression)"
        " be output?"
    ),
)
@click.option(
    "--matrix-for-markers",
    required=True,
    default="X",
    help=(
        "Where cell groups in the configuration file have been flagged with"
        " markers, which matrix should be used? Can be X, or an entry in"
        " .layers(). The matrix must be appropriate for Scanpy's"
        " tl.rank_genes_groups() method, usually meaning filtered, normalised"
        " and log transformed, but without additional scaling."
    ),
)
@click.option(
    "--no-write-matrices",
    "write_matrices",
    default=True,
    is_flag=True,
    help=(
        "Use to disable writing of matrices to bundle, for example in early"
        " metadata evaluation"
    ),
)
@click.option(
    "--write-premagetab",
    default=False,
    is_flag=True,
    help=(
        "Should we write pre-magetab files for curation by SCXA team? If not,"
        " we assume curation is done, and we will look for curated metadata"
        " via the the exp_name parameter"
    ),
)
@click.option(
    "--conda-prefix",
    type=click.Path(dir_okay=True),
    default=None,
    help=(
        "Specify a Conda directory to be used for environments when running"
        " Snakemake workflows."
    ),
)
@click.option(
    "--scxa-metadata-branch",
    required=True,
    default="master",
    help=(
        "When searching the SCXA metadata repository for curation for this"
        " experiment, which branch should we use?"
    ),
)
@click.option(
    "--sanitize-columns",
    default=True,
    is_flag=True,
    help=(
        "When adding data from curation into the anndata object, should we"
        " remove the Comment, Characteristic etc?"
    ),
)
@click.option(
    "--exp-name",
    required=True,
    default="NONAME",
    help=(
        "Specify an Expression Atlas identifier that will be used for this"
        " experiment. If not set, a placeholder value E-EXP-1234 will be used"
        " and can be edited in the bundle later."
    ),
)
@click.option(
    "--scxa-db-scale",
    type=click.INT,
    default=1000000,
    help=(
        "To what overall scale should cell counts be multiplied for the SCXA"
        " DB? A multiplier will be calculated from this value and the median"
        " cell-wise sum in the given matrix."
    ),
)
def make_bundle(*args, **kwargs):

    """Build a bundle directory compatible with Single Cell Expression Atlas
    (SCXA) build proceseses

    \b
    anndata_file   - A file of the annData hdf5 specification, with all
                     necessaryinformation for SCXA.
    anndata_config - A config file generated with
                     `make_starting_config_from_anndata` and manually edited to
                     supply necessary information.
    bundle_dir     - A directory in which to create the bundle.
    """

    from atlas_anndata import make_bundle_from_anndata

    make_bundle_from_anndata(*args, **kwargs)


if __name__ == "__main__":
    make_bundle()
