def test_url_http(self):
     url = "http://raw.githubusercontent.com/chanzuckerberg/cellxgene/master/example-dataset/pbmc3k.h5ad"
     locator = DataLocator(url)
     config = AppConfig()
     config.update(**self.args)
     data = AnndataAdaptor(locator, config)
     self.stdAsserts(data)
Exemple #2
0
def data_with_tmp_annotations(ext: MatrixDataType, annotations_fixture=False):
    tmp_dir = tempfile.mkdtemp()
    annotations_file = path.join(tmp_dir, "test_annotations.csv")
    if annotations_fixture:
        shutil.copyfile(
            f"{PROJECT_ROOT}/server/test/test_datasets/pbmc3k-annotations.csv",
            annotations_file)
    args = {
        "embeddings__names": ["umap"],
        "presentation__max_categories": 100,
        "single_dataset__obs_names": None,
        "single_dataset__var_names": None,
        "diffexp__lfc_cutoff": 0.01,
    }
    fname = {
        MatrixDataType.H5AD: f"{PROJECT_ROOT}/example-dataset/pbmc3k.h5ad",
        MatrixDataType.CXG: "test/test_datasets/pbmc3k.cxg",
    }[ext]
    data_locator = DataLocator(fname)
    config = AppConfig()
    config.update(**args)
    config.update(single_dataset__datapath=data_locator.path)
    config.complete_config()
    data = MatrixDataLoader(data_locator.abspath()).open(config)
    annotations = AnnotationsLocalFile(None, annotations_file)
    return data, tmp_dir, annotations
 def test_posix_file(self):
     locator = DataLocator("../example-dataset/pbmc3k.h5ad")
     config = AppConfig()
     config.update(**self.args)
     config.update(single_dataset__datapath=locator.path)
     config.complete_config()
     data = AnndataAdaptor(locator, config)
     self.stdAsserts(data)
Exemple #4
0
    def test_update(self):
        c = AppConfig()
        c.update(server__verbose=True, multi_dataset__dataroot="datadir")

        v = c.changes_from_default()
        self.assertCountEqual(v,
                              [("server__verbose", True, False),
                               ("multi_dataset__dataroot", "datadir", None)]),
Exemple #5
0
def app_config(data_locator, backed=False):
    args = {
        "embeddings__names": ["umap", "tsne", "pca"],
        "presentation__max_categories": 100,
        "single_dataset__obs_names": None,
        "single_dataset__var_names": None,
        "diffexp__lfc_cutoff": 0.01,
        "adaptor__anndata_adaptor__backed": backed,
        "single_dataset__datapath": data_locator,
        "limits__diffexp_cellcount_max": None,
        "limits__column_request_max": None,
    }
    config = AppConfig()
    config.update(**args)
    config.complete_config()
    return config
Exemple #6
0
                app_config.update_from_config_file(lh)
        else:
            logging.critical(f"Configuration file not found {config_file}")
            sys.exit(1)
    else:
        # no config file specified, try "config.yaml" in the current working directory
        config_file = "config.yaml"
        config_location = DataLocator(config_file)
        if config_location.exists():
            with config_location.local_handle() as lh:
                logging.info(f"Configuration from {config_file}")
                app_config.update_from_config_file(lh)

    if dataroot:
        logging.info(f"Configuration from CXG_DATAROOT")
        app_config.update(multi_dataset__dataroot=dataroot)

    if secret_name:
        if secret_region_name is None:
            secret_region_name = discover_s3_region_name(
                app_config.multi_dataset__dataroot)
            if not secret_region_name:
                logging.error(
                    f"Expected to discover the s3 region name from {app_config.multi_dataset__dataroot}"
                )
        flask_secret_key = get_flask_secret_key(secret_region_name,
                                                secret_name)
        app_config.update(server__flask_secret_key=flask_secret_key)

    # features are unsupported in the current hosted server
    app_config.update(
 def setUp(self):
     self.data_file = DataLocator(f"{PROJECT_ROOT}/example-dataset/pbmc3k.h5ad")
     config = AppConfig()
     config.update(single_dataset__datapath=self.data_file.path)
     config.complete_config()
     self.data = AnndataAdaptor(self.data_file, config)
Exemple #8
0
def launch(
    datapath,
    dataroot,
    verbose,
    debug,
    open_browser,
    port,
    host,
    embedding,
    obs_names,
    var_names,
    max_category_items,
    disable_custom_colors,
    diffexp_lfc_cutoff,
    title,
    scripts,
    about,
    disable_annotations,
    annotations_file,
    annotations_dir,
    backed,
    disable_diffexp,
    experimental_annotations_ontology,
    experimental_annotations_ontology_obo,
    experimental_enable_reembedding,
    config_file,
    dump_default_config,
):
    """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 "getting started" guide to learn more:
    https://chanzuckerberg.github.io/cellxgene/getting-started.html

    Examples:

    > cellxgene launch example-dataset/pbmc3k.h5ad --title pbmc3k

    > cellxgene launch <your data file> --title <your title>

    > cellxgene launch <url>"""

    # TODO Examples to provide when "--dataroot" is unhidden
    # > cellxgene launch --dataroot example-dataset/
    #
    # > cellxgene launch --dataroot <url>

    if dump_default_config:
        print(default_config)
        sys.exit(0)

    # Startup message
    click.echo("[cellxgene] Starting the CLI...")

    # app config
    app_config = AppConfig()

    try:
        if config_file:
            app_config.update_from_config_file(config_file)

        # Determine which config options were give on the command line.
        # Those will override the ones provided in the config file (if provided).
        cli_config = AppConfig()
        cli_config.update(
            server__verbose=verbose,
            server__debug=debug,
            server__host=host,
            server__port=port,
            server__scripts=scripts,
            server__open_browser=open_browser,
            single_dataset__datapath=datapath,
            single_dataset__title=title,
            single_dataset__about=about,
            single_dataset__obs_names=obs_names,
            single_dataset__var_names=var_names,
            multi_dataset__dataroot=dataroot,
            user_annotations__enable=not disable_annotations,
            user_annotations__local_file_csv__file=annotations_file,
            user_annotations__local_file_csv__directory=annotations_dir,
            user_annotations__ontology__enable=experimental_annotations_ontology,
            user_annotations__ontology__obo_location=experimental_annotations_ontology_obo,
            presentation__max_categories=max_category_items,
            presentation__custom_colors=not disable_custom_colors,
            embeddings__names=embedding,
            embeddings__enable_reembedding=experimental_enable_reembedding,
            diffexp__enable=not disable_diffexp,
            diffexp__lfc_cutoff=diffexp_lfc_cutoff,
            adaptor__anndata_adaptor__backed=backed,
        )
        diff = cli_config.changes_from_default()
        changes = {}
        for key, val, defval in diff:
            changes[key] = val
        app_config.update(**changes)

        # process the configuration
        #  any errors will be thrown as an exception.
        #  any info messages will be passed to the messagefn function.

        def messagefn(message):
            click.echo("[cellxgene] " + message)

        # Use a default secret if one is not provided
        if not app_config.server__flask_secret_key:
            app_config.update(server__flask_secret_key="SparkleAndShine")

        app_config.complete_config(messagefn)

    except (ConfigurationError, DatasetAccessError) as e:
        raise click.ClickException(e)

    handle_scripts(scripts)

    # create the server
    server = CliLaunchServer(app_config)

    if not app_config.server__verbose:
        log = logging.getLogger("werkzeug")
        log.setLevel(logging.ERROR)

    cellxgene_url = f"http://{app_config.server__host}:{app_config.server__port}"
    if app_config.server__open_browser:
        click.echo(f"[cellxgene] Launching! Opening your browser to {cellxgene_url} now.")
        webbrowser.open(cellxgene_url)
    else:
        click.echo(f"[cellxgene] Launching! Please go to {cellxgene_url} in your browser.")

    click.echo("[cellxgene] Type CTRL-C at any time to exit.")

    if not app_config.server__verbose:
        f = open(devnull, "w")
        sys.stdout = f

    try:
        server.app.run(
            host=app_config.server__host,
            debug=app_config.server__debug,
            port=app_config.server__port,
            threaded=not app_config.server__debug,
            use_debugger=False,
            use_reloader=False,
        )
    except OSError as e:
        if e.errno == errno.EADDRINUSE:
            raise click.ClickException("Port is in use, please specify an open port using the --port flag.") from e
        raise