{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to use `refgenconf ` to manage Refgenie assets in a pipeline\n", "\n", "Below we present an example use of `refgenconf` package. It is installed automatically with `refgenie` (or separately installable with `pip install refgenconf`). All the asset fetching functionality is impelmented in `refgenconf` package, so pipelines that just use Python API do not need to depend on `refgenie`.\n", "\n", "## Goal\n", "The goal of the code below is to **get a path to the refgenie-managed fasta file for a user-specified genome**. \n", "\n", "Genome FASTA is a part of `fasta` asset, accessible as a `fasta` seek key. To retrieve the path this file on the command line one would say: `refgenie seek /fasta`. For example:\n", "```\n", "refgenie seek hg38/fasta\n", "```\n", "\n", "## Steps\n", "\n", "First, let's set the `$REFGENIE` environmet variable. It should be set by a pipeline user or the config file path should be provided explictly, e.g. as an input to the pipeline (here shown as `user_provided_cfg_path = None` -- not provided) " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ[\"REFGENIE\"] = \"./refgenie.yaml\"\n", "user_provided_cfg_path = None\n", "user_provided_genome = \"rCRSd\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, let's import components of `refgenconf` that we'll use" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "from refgenconf import RefGenConf, select_genome_config, RefgenconfError, CFG_ENV_VARS, CFG_FOLDER_KEY\n", "from yacman import UndefinedAliasError" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can use the `select_genome_config` function to determine the preferred path to the config file. If `user_provided_cfg_path` is `None` (not specified) the `$REFGENIE` environment variable is used. " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "refgenie_cfg_path = select_genome_config(filename=user_provided_cfg_path, check_exist=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function returns `None` if none of the above point to a valid path. That's why we raise an aproppriate error below. Obviously, the name of `--rfg-config` argument depends on pipeline design. " ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "if not refgenie_cfg_path:\n", " raise OSError(f\"Could not determine path to a refgenie genome configuration file.\"\n", " f\"Use --rfg-config argument or set '{CFG_ENV_VARS}' environment variable to provide it\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Otherwise it returns a determined path (`str`). So, we check if it exists and read the object if it does. If it does not, we can initialize the config file" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File '/Users/mstolarczyk/code/refgenie/docs_jupyter/refgenie.yaml' does not exist. Initializing refgenie genome configuration file.\n" ] } ], "source": [ "if isinstance(refgenie_cfg_path, str) and os.path.exists(refgenie_cfg_path):\n", " print(f\"Reading refgenie genome configuration file from file: {refgenie_cfg_path}\")\n", " rgc = RefGenConf(filepath=refgenie_cfg_path)\n", "else:\n", " print(f\"File '{refgenie_cfg_path}' does not exist. Initializing refgenie genome configuration file.\")\n", " rgc = RefGenConf(entries={CFG_FOLDER_KEY: os.path.dirname(refgenie_cfg_path)})\n", " rgc.initialize_config_file(filepath=refgenie_cfg_path)\n", " rgc.subscribe(urls=\"http://rg.databio.org:82\", reset=True) # subscribe to the desired server, if needed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we try to retrieve the path to out asset of interest and pull from `refgenieserver` if the retrieval fails." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Could not determine path to chrom.sizes asset, pulling\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a258a7dec75f4bbcb3f2973e0a3b9cc6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Determined path to fasta asset: /Users/mstolarczyk/code/refgenie/docs_jupyter/alias/rCRSd/fasta/default/rCRSd.fa\n" ] } ], "source": [ "try:\n", " fasta = rgc.seek(genome_name=user_provided_genome, asset_name=\"fasta\", tag_name=\"default\",\n", " seek_key=\"fasta\")\n", "except (RefgenconfError, UndefinedAliasError):\n", " print(\"Could not determine path to chrom.sizes asset, pulling\")\n", " rgc.pull(genome=user_provided_genome, asset=\"fasta\", tag=\"default\")\n", " fasta = rgc.seek(genome_name=user_provided_genome, asset_name=\"fasta\", tag_name=\"default\",\n", " seek_key=\"fasta\")\n", "print(f\"Determined path to fasta asset: {fasta}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }