Пример #1
0
 def setUp(self):
     args = {
         "layout": ["umap"],
         "max_category_items": 100,
         "obs_names": None,
         "var_names": None,
         "diffexp_lfc_cutoff": 0.01,
     }
     self.data = ScanpyEngine("example-dataset/pbmc3k.h5ad", args)
Пример #2
0
 def setUp(self):
     args = {
         "layout": ["umap"],
         "max_category_items": 100,
         "obs_names": None,
         "var_names": None,
         "diffexp_lfc_cutoff": 0.01,
         "layout_file": None
     }
     self.data = ScanpyEngine(DataLocator(self.data_locator), args)
Пример #3
0
 def setUp(self):
     self.args = {
         "layout": ["umap"],
         "max_category_items": 100,
         "obs_names": None,
         "var_names": None,
         "diffexp_lfc_cutoff": 0.01,
     }
     with warnings.catch_warnings():
         warnings.simplefilter("ignore", category=UserWarning)
         self.data = ScanpyEngine(DataLocator("server/test/test_datasets/nan.h5ad"), self.args)
         self.data._create_schema()
Пример #4
0
 def setUp(self):
     self.tmpDir = tempfile.mkdtemp()
     self.label_file = path.join(self.tmpDir, "labels.csv")
     args = {
         "layout": ["umap"],
         "max_category_items": 100,
         "obs_names": None,
         "var_names": None,
         "diffexp_lfc_cutoff": 0.01,
         "label_file": self.label_file
     }
     self.data = ScanpyEngine(DataLocator("example-dataset/pbmc3k.h5ad"),
                              args)
Пример #5
0
    def setUp(self):
        args = {
            "layout": "umap",
            "diffexp": "ttest",
            "max_category_items": 100,
            "obs_names": None,
            "var_names": None,
            "diffexp_lfc_cutoff": 0.01,
            "nan_to_num": True,
        }

        self.data = ScanpyEngine("example-dataset/pbmc3k.h5ad", args)
        self.data._create_schema()
Пример #6
0
 def run(self):
     super(Worker, self).run()
     if not self.data_file:
         self.emit("finished")
         return
     from server.app.app import Server
     from server.app.scanpy_engine.scanpy_engine import ScanpyEngine
     # create server
     try:
         server = Server()
         server.create_app()
     except Exception as e:
         self.emit("server_error", str(e))
         self.emit("finished")
         return
     # load data
     try:
         args = {
             "max_category_items": 100,
             "diffexp_lfc_cutoff": 0.01,
             "obs_names": None,
             "var_names": None,
         }
         args.update(self.engine_options)
         data = ScanpyEngine(self.data_file, args)
         server.attach_data(data, self.title)
         self.emit("ready")
     except Exception as e:
         self.emit("engine_error", str(e))
         self.emit("finished")
         return
     # launch server
     try:
         server.app.run(host=self.host,
                        debug=False,
                        port=self.port,
                        threaded=True)
     except Exception as e:
         self.emit("server_error", str(e))
     finally:
         self.emit("finished")
Пример #7
0
 def setUp(self):
     self.data = ScanpyEngine("example-dataset/", schema="data_schema.json")
Пример #8
0
def launch(data, verbose, debug, open_browser, port, host, embedding,
           obs_names, var_names, max_category_items, diffexp_lfc_cutoff, title,
           scripts, about, experimental_label_file, backed, disable_diffexp):
    """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.

    Examples:

    > cellxgene launch example_dataset/pbmc3k.h5ad --title pbmc3k

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

    > cellxgene launch <url>"""

    e_args = parse_engine_args(embedding, obs_names, var_names,
                               max_category_items, diffexp_lfc_cutoff,
                               experimental_label_file, backed,
                               disable_diffexp)
    try:
        data_locator = DataLocator(data)
    except RuntimeError as re:
        raise click.ClickException(
            f"Unable to access data at {data}.  {str(re)}")

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

    # Argument checking
    if data_locator.islocal():
        # if data locator is local, apply file system conventions and other "cheap"
        # validation checks.  If a URI, defer until we actually fetch the data and
        # try to read it.  Many of these tests don't make sense for URIs (eg, extension-
        # based typing).
        if not data_locator.exists():
            raise click.FileError(data, hint="file does not exist")
        if not data_locator.isfile():
            raise click.FileError(data, hint="data is not a file")
        name, extension = splitext(data)
        if extension != ".h5ad":
            raise click.FileError(basename(data),
                                  hint="file type must be .h5ad")

    if debug:
        verbose = True
        open_browser = False
    else:
        warnings.formatwarning = custom_format_warning

    if not verbose:
        sys.tracebacklimit = 0

    if scripts:
        click.echo(r"""
    / / /\ \ \__ _ _ __ _ __ (_)_ __   __ _
    \ \/  \/ / _` | '__| '_ \| | '_ \ / _` |
     \  /\  / (_| | |  | | | | | | | | (_| |
      \/  \/ \__,_|_|  |_| |_|_|_| |_|\__, |
                                      |___/
    The --scripts flag is intended for developers to include google analytics etc. You could be opening yourself to a
    security risk by including the --scripts flag. Make sure you trust the scripts that you are including.
            """)
        scripts_pretty = ", ".join(scripts)
        click.confirm(
            f"Are you sure you want to inject these scripts: {scripts_pretty}?",
            abort=True)

    if not title:
        file_parts = splitext(basename(data))
        title = file_parts[0]

    if port:
        if debug:
            raise click.ClickException(
                "--port and --debug may not be used together (try --verbose for error logging)."
            )
        if not is_port_available(host, int(port)):
            raise click.ClickException(
                f"The port selected {port} is in use, please specify an open port using the --port flag."
            )
    else:
        port = find_available_port(host)

    if experimental_label_file:
        lf_name, lf_ext = splitext(experimental_label_file)
        if lf_ext and lf_ext != ".csv":
            raise click.FileError(basename(experimental_label_file),
                                  hint="label file type must be .csv")

    if about:

        def url_check(url):
            try:
                result = urlparse(url)
                if all([result.scheme, result.netloc]):
                    return True
                else:
                    return False
            except ValueError:
                return False

        if not url_check(about):
            raise click.ClickException(
                "Must provide an absolute URL for --about. (Example format: http://example.com)"
            )

    # Setup app
    cellxgene_url = f"http://{host}:{port}"

    # Import Flask app
    server = Server()

    server.create_app()
    server.app.config.update(SCRIPTS=scripts)

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

    file_size = data_locator.size() if data_locator.islocal() else 0

    # if a big file, let the user know it may take a while to load.
    if file_size > BIG_FILE_SIZE_THRESHOLD:
        click.echo(
            f"[cellxgene] Loading data from {basename(data)}, this may take a while..."
        )
    else:
        click.echo(f"[cellxgene] Loading data from {basename(data)}.")

    from server.app.scanpy_engine.scanpy_engine import ScanpyEngine

    try:
        server.attach_data(ScanpyEngine(data_locator, e_args),
                           title=title,
                           about=about)
    except ScanpyFileError as e:
        raise click.ClickException(f"{e}")

    if not disable_diffexp and server.app.data.config['diffexp_may_be_slow']:
        click.echo(f"[cellxgene] CAUTION: due to the size of your dataset, "
                   f"running differential expression may take longer or fail.")

    if 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 verbose:
        f = open(devnull, "w")
        sys.stdout = f

    try:
        server.app.run(host=host,
                       debug=debug,
                       port=port,
                       threaded=False if debug else True,
                       use_debugger=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
 def test_url_http(self):
     url = "http://raw.githubusercontent.com/chanzuckerberg/cellxgene/master/example-dataset/pbmc3k.h5ad"
     locator = DataLocator(url)
     data = ScanpyEngine(locator, self.args)
     self.stdAsserts(data)
 def test_posix_file(self):
     locator = DataLocator("example-dataset/pbmc3k.h5ad")
     data = ScanpyEngine(locator, self.args)
     self.stdAsserts(data)
 def setUp(self):
     self.data_file = DataLocator("example-dataset/pbmc3k.h5ad")
     self.data = ScanpyEngine()
Пример #12
0
 def test_load(self):
     with self.assertWarns(UserWarning):
         ScanpyEngine("server/test/test_datasets/nan.h5ad", self.args)
Пример #13
0
def launch(data, verbose, debug, open_browser, port, host, layout, obs_names,
           var_names, max_category_items, diffexp_lfc_cutoff, title, scripts):
    """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.

    Examples:

    > cellxgene launch example_dataset/pbmc3k.h5ad --title pbmc3k

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

    e_args = parse_engine_args(layout, obs_names, var_names,
                               max_category_items, diffexp_lfc_cutoff)
    # Startup message
    click.echo("[cellxgene] Starting the CLI...")

    # Argument checking
    name, extension = splitext(data)
    if extension != ".h5ad":
        raise click.FileError(basename(data), hint="file type must be .h5ad")

    if debug:
        verbose = True
        open_browser = False
    else:
        warnings.formatwarning = custom_format_warning

    if not verbose:
        sys.tracebacklimit = 0

    if scripts:
        click.echo(r"""
    / / /\ \ \__ _ _ __ _ __ (_)_ __   __ _
    \ \/  \/ / _` | '__| '_ \| | '_ \ / _` |
     \  /\  / (_| | |  | | | | | | | | (_| |
      \/  \/ \__,_|_|  |_| |_|_|_| |_|\__, |
                                      |___/
    The --scripts flag is intended for developers to include google analytics etc. You could be opening yourself to a
    security risk by including the --scripts flag. Make sure you trust the scripts that you are including.
            """)
        scripts_pretty = ", ".join(scripts)
        click.confirm(
            f"Are you sure you want to inject these scripts: {scripts_pretty}?",
            abort=True)

    if not title:
        file_parts = splitext(basename(data))
        title = file_parts[0]

    if port:
        if debug:
            raise click.ClickException(
                "--port and --debug may not be used together (try --verbose for error logging)."
            )
        if not is_port_available(host, int(port)):
            raise click.ClickException(
                f"The port selected {port} is in use, please specify an open port using the --port flag."
            )
    else:
        port = find_available_port(host)

    # Setup app
    cellxgene_url = f"http://{host}:{port}"

    # Import Flask app
    server = Server()

    server.create_app()
    server.app.config.update(SCRIPTS=scripts)

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

    file_size = getsize(data)

    # if a big file, let the user know it may take a while to load.
    if file_size > BIG_FILE_SIZE_THRESHOLD:
        click.echo(
            f"[cellxgene] Loading data from {basename(data)}, this may take awhile..."
        )
    else:
        click.echo(f"[cellxgene] Loading data from {basename(data)}.")

    # 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")
    from server.app.scanpy_engine.scanpy_engine import ScanpyEngine

    try:
        server.attach_data(ScanpyEngine(data, e_args), title=title)
    except ScanpyFileError as e:
        raise click.ClickException(f"{e}")

    if 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 verbose:
        f = open(devnull, "w")
        sys.stdout = f

    try:
        server.app.run(host=host,
                       debug=debug,
                       port=port,
                       threaded=True,
                       use_debugger=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
Пример #14
0
def launch(
    data,
    layout,
    diffexp,
    title,
    verbose,
    debug,
    obs_names,
    var_names,
    open_browser,
    port,
    host,
    max_category_items,
    diffexp_lfc_cutoff,
):
    """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.

    Examples:

    > cellxgene launch example_dataset/pbmc3k.h5ad --title pbmc3k

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

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

    # Argument checking
    name, extension = splitext(data)
    if extension != ".h5ad":
        raise click.FileError(basename(data), hint="file type must be .h5ad")

    if debug:
        verbose = True
        open_browser = False
    else:
        warnings.formatwarning = custom_format_warning

    if not verbose:
        sys.tracebacklimit = 0

    if not title:
        file_parts = splitext(basename(data))
        title = file_parts[0]

    # Setup app
    cellxgene_url = f"http://{host}:{port}"

    # Import Flask app
    from server.app.app import app

    app.config.update(DATASET_TITLE=title)

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

    click.echo(
        f"[cellxgene] Loading data from {basename(data)}, this may take awhile..."
    )

    # 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")
    from server.app.scanpy_engine.scanpy_engine import ScanpyEngine

    args = {
        "layout": layout,
        "diffexp": diffexp,
        "max_category_items": max_category_items,
        "diffexp_lfc_cutoff": diffexp_lfc_cutoff,
        "obs_names": obs_names,
        "var_names": var_names,
    }

    try:
        app.data = ScanpyEngine(data, args)
    except ScanpyFileError as e:
        raise click.ClickException(f"{e}")

    if 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 verbose:
        f = open(devnull, "w")
        sys.stdout = f

    app.run(host=host, debug=debug, port=port, threaded=True)
Пример #15
0
 def setUp(self):
     self.data_file = "example-dataset/pbmc3k.h5ad"
     self.data = ScanpyEngine()