{ "cells": [ { "cell_type": "markdown", "id": "0cb8e51a", "metadata": {}, "source": [ "# Building Genome Reference Files\n", "*Note: \n", "Building custom reference files and token dictionaries is completely optional.\n", "If you are working with standard human or mouse datasets,\n", " you can skip this tutorial and directly use the pre-compiled reference files provided at* `data\\gene_dict\\`.\n", "\n", "This tutorial demonstrates how to parse GFF annotation files, annotate transcription factors (TFs), and generate the required JSON and AnnData (`.h5ad`) reference files for scDynOmics.\n", "\n", "We will walk through setting up the inputs, initializing the reference maker, and exporting the indices." ] }, { "cell_type": "code", "execution_count": 7, "id": "f4387c26", "metadata": {}, "outputs": [], "source": [ "import anndata as ad, hdf5plugin\n", "from scdynomics.utils.ref import Maker" ] }, { "cell_type": "markdown", "id": "75fc56da", "metadata": {}, "source": [ "## 1. Initializing the Reference Maker\n", "\n", "To ensure this tutorial is easy to follow and fully reproducible, let's use the default `GFF3` file already provided in the `data` directory. The included `GFF3` annotations are sourced directly from [Ensembl](https://www.ensembl.org/Mus_musculus/Info/Index).\n", "\n", "For the transcription factor (TF) list, we are using reference files retrieved from the [SCENIC+](https://github.com/aertslab/scenicplus/tree/main/resources) project.\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": 2, "id": "790e2650", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded 22452 genes from the mm10 GFF.\n" ] } ], "source": [ "# Define paths to the GFF and TF list files\n", "mm10_gff3_path = \"../../data/gff/Mus_musculus.GRCm38.102.gff3.gz\"\n", "mm10_tf_list_path = \"../../data/tf_list/allTFs_mm.txt\"\n", "\n", "# Instantiate the Maker class\n", "reference_maker = Maker(\n", " gff_path=mm10_gff3_path,\n", " tf_list_path=mm10_tf_list_path,\n", " remove_non_coding=True,\n", " promoter_tss_upstream=1500,\n", " promoter_tss_downstream=500\n", ")\n", "\n", "print(f\"Loaded {len(reference_maker.gene_dict)} genes from the mm10 GFF.\")" ] }, { "cell_type": "markdown", "id": "73f1a5d2", "metadata": {}, "source": [ "## 2. Generating and Saving References\n", "\n", "With the data parsed, we can now output our reference formats. \n", "\n", "1. **JSON Dictionary:** Contains the mapped genes, transcripts, proteins, and promoters.\n", "2. **Gene Expression AnnData Index:** Creates a structural index for expression matrices, sorting transcription factors to the top.\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": 3, "id": "c31eba11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saved JSON reference.\n", "Saved AnnData reference.\n" ] } ], "source": [ "output_json_path = \"ref.json\"\n", "output_h5ad_path = \"ref.h5ad\"\n", "\n", "# 1. Output the dictionary\n", "ref_dict = reference_maker.output_dict(save_path=output_json_path)\n", "print(\"Saved JSON reference.\")\n", "\n", "# 2. Output the Gene Expression AnnData index\n", "gex_adata = reference_maker.output_gex_index(\n", " skip_mt=False, # Keeping mitochondrial genes in the reference\n", " save_path=output_h5ad_path\n", ")\n", "print(\"Saved AnnData reference.\")" ] }, { "cell_type": "markdown", "id": "91d3e25c", "metadata": {}, "source": [ "## 3. Inspecting the Result\n", "\n", "Let's look at the generated AnnData object to verify our gene indexing and TF tagging worked correctly.\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": 4, "id": "e7cc5502", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AnnData object with n_obs × n_vars = 0 × 22452\n", " var: 'name', 'type', 'chr', 'start', 'end', 'strand', 'promoters', 'mt'\n", " uns: 'n_tf'\n" ] }, { "data": { "text/html": [ "
| \n", " | name | \n", "type | \n", "chr | \n", "start | \n", "end | \n", "strand | \n", "promoters | \n", "mt | \n", "
|---|---|---|---|---|---|---|---|---|
| id | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
| ENSMUSG00000025902 | \n", "Sox17 | \n", "tf | \n", "1 | \n", "4490931 | \n", "4497354 | \n", "- | \n", "chr1:4495913-4497913;chr1:4496257-4498257;chr1... | \n", "False | \n", "
| ENSMUSG00000025912 | \n", "Mybl1 | \n", "tf | \n", "1 | \n", "9667415 | \n", "9700209 | \n", "- | \n", "chr1:9699709-9701709;chr1:9699524-9701524;chr1... | \n", "False | \n", "
| ENSMUSG00000099032 | \n", "Tcf24 | \n", "tf | \n", "1 | \n", "9960163 | \n", "9967932 | \n", "- | \n", "chr1:9967432-9969432;chr1:9962309-9964309 | \n", "False | \n", "
| ENSMUSG00000042414 | \n", "Prdm14 | \n", "tf | \n", "1 | \n", "13113457 | \n", "13127163 | \n", "- | \n", "chr1:13126663-13128663 | \n", "False | \n", "
| ENSMUSG00000005886 | \n", "Ncoa2 | \n", "tf | \n", "1 | \n", "13139105 | \n", "13374083 | \n", "- | \n", "chr1:13371939-13373939;chr1:13373583-13375583;... | \n", "False | \n", "