{ "cells": [ { "cell_type": "markdown", "id": "9904be0b-9bdb-4a28-850a-8aec7786fab4", "metadata": {}, "source": [ "# Process data for training CATLAS binary ATAC-seq model" ] }, { "cell_type": "markdown", "id": "83fd567a-5263-458a-97f9-6b9f2d3db6c1", "metadata": {}, "source": [ "## Set up W&B" ] }, { "cell_type": "code", "execution_count": 1, "id": "f6cc91ab-89b9-448b-a2ec-6d43dafe5e86", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import wandb\n", "import os\n", "import anndata\n", "import pandas as pd\n", "import numpy as np\n", "import bioframe as bf\n", "from grelu.sequence.utils import resize\n", "from grelu.data.preprocess import filter_blacklist, filter_chromosomes" ] }, { "cell_type": "code", "execution_count": 2, "id": "4f87b690-04e0-4099-8536-15ebec7c5102", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: Using wandb-core as the SDK backend. Please refer to https://wandb.me/wandb-core for more information.\n", "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mavantikalal\u001b[0m (\u001b[33mgrelu\u001b[0m) to \u001b[32mhttps://api.wandb.ai\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wandb.login(host=\"https://api.wandb.ai\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "d4cfb6b2-2f49-4c0f-8c0d-9f402852d150", "metadata": {}, "outputs": [], "source": [ "project_name='human-atac-catlas'" ] }, { "cell_type": "code", "execution_count": 4, "id": "52c22759-6d2c-4b41-bc1d-d2a3233d7c73", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Tracking run with wandb version 0.19.7" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "Run data is saved locally in /code/github/gReLU-applications/catlas/wandb/run-20250306_000043-vphymp69" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "Syncing run prep to Weights & Biases (docs)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View project at https://wandb.ai/grelu/human-atac-catlas" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View run at https://wandb.ai/grelu/human-atac-catlas/runs/vphymp69" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "run = wandb.init(\n", " entity='grelu', project=project_name, job_type='preprocessing', name='prep',\n", " settings=wandb.Settings(\n", " program_relpath='1_data.ipynb',\n", " program_abspath='/code/github/gReLU-applications/catlas/1_data.ipynb')\n", ")" ] }, { "cell_type": "markdown", "id": "9b3caead-af54-4e7a-88af-ac9e283f0750", "metadata": {}, "source": [ "## Load CATLAS snATAC-seq matrix" ] }, { "cell_type": "code", "execution_count": 5, "id": "0d70c98f-f45f-45a9-98ca-946c507c36c6", "metadata": {}, "outputs": [], "source": [ "#!wget http://catlas.org/catlas_downloads/humantissues/cCRE_by_cell_type/matrix.tsv.gz" ] }, { "cell_type": "code", "execution_count": 6, "id": "b909e398-8de7-41e2-83b7-3c32021cef5b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.11/site-packages/anndata/utils.py:429: FutureWarning: Importing read_mtx from `anndata` is deprecated. Import anndata.io.read_mtx instead.\n", " warnings.warn(msg, FutureWarning)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "(222, 1154611)\n" ] } ], "source": [ "ad = anndata.read_mtx('matrix.tsv.gz').T\n", "\n", "# Prepare ad.obs\n", "ad.obs = pd.read_table('http://catlas.org/catlas_downloads/humantissues/cCRE_by_cell_type/celltypes.txt.gz', header=None, names=['cell type'])\n", "ad.obs_names = ad.obs['cell type']\n", "\n", "# Prepare ad.var\n", "var = pd.read_table('http://catlas.org/catlas_downloads/humantissues/cCRE_hg38.tsv.gz')\n", "var.columns = ['chrom', 'start', 'end', 'cre_class', 'in_fetal', 'in_adult', 'cre_module']\n", "var[\"width\"] = (var.end - var.start).astype(int)\n", "var.index = var.index.astype(str)\n", "ad.var = var\n", "\n", "print(ad.shape)" ] }, { "cell_type": "code", "execution_count": 7, "id": "bd2463e1-8fa8-40ee-a1a2-b5b60516976d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " chrom start end cre_class in_fetal in_adult cre_module \\\n", "0 chr1 9955 10355 Promoter Proximal yes yes 146 \n", "1 chr1 29163 29563 Promoter yes yes 37 \n", "2 chr1 79215 79615 Distal no yes 75 \n", "3 chr1 102755 103155 Distal no yes 51 \n", "4 chr1 115530 115930 Distal yes no 36 \n", "\n", " width \n", "0 400 \n", "1 400 \n", "2 400 \n", "3 400 \n", "4 400 \n", " cell type\n", "cell type \n", "Follicular Follicular\n", "Fibro General Fibro General\n", "Acinar Acinar\n", "T Lymphocyte 1 (CD8+) T Lymphocyte 1 (CD8+)\n", "T lymphocyte 2 (CD4+) T lymphocyte 2 (CD4+)\n" ] } ], "source": [ "print(ad.var.head())\n", "print(ad.obs.head())" ] }, { "cell_type": "markdown", "id": "84f93545-eaf0-4d39-b16d-2e843367d626", "metadata": {}, "source": [ "## Filter peaks" ] }, { "cell_type": "code", "execution_count": 8, "id": "b5181175-02ce-4614-81a3-962d4705e4f6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keeping 1121319 intervals\n" ] } ], "source": [ "ad = filter_chromosomes(ad, 'autosomes')" ] }, { "cell_type": "code", "execution_count": 9, "id": "db96deff-fdec-4cc3-8c39-ad2541394a70", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keeping 1121319 intervals\n" ] } ], "source": [ "ad = filter_blacklist(ad, genome='hg38')" ] }, { "cell_type": "code", "execution_count": 10, "id": "517b43dc-8e4f-4f44-a6de-e4b459dca5af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(222, 1121319)\n", "(204, 1121319)\n" ] } ], "source": [ "# Drop peaks that are only accessible in few cell types\n", "print(ad.shape)\n", "ad = ad[ad.X.mean(axis=1) > .03, :]\n", "print(ad.shape)" ] }, { "cell_type": "markdown", "id": "7c53c564-17ed-42e8-bd62-2e4ee182a082", "metadata": {}, "source": [ "## Resize peaks" ] }, { "cell_type": "code", "execution_count": 11, "id": "493f5905-02bb-477b-ae5f-abde1572962d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
chromstartendcre_classin_fetalin_adultcre_modulewidth
0chr11005510255Promoter Proximalyesyes146400
1chr12926329463Promoteryesyes37400
2chr17931579515Distalnoyes75400
\n", "
" ], "text/plain": [ " chrom start end cre_class in_fetal in_adult cre_module width\n", "0 chr1 10055 10255 Promoter Proximal yes yes 146 400\n", "1 chr1 29263 29463 Promoter yes yes 37 400\n", "2 chr1 79315 79515 Distal no yes 75 400" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seq_len = 200\n", "ad.var = resize(ad.var, seq_len)\n", "ad.var.head(3)" ] }, { "cell_type": "markdown", "id": "26745890-cb7f-4f8c-9801-133c66a0ef96", "metadata": {}, "source": [ "## Load enformer splits" ] }, { "cell_type": "code", "execution_count": 12, "id": "85bc5e37-ba54-4e34-8826-96e266c7ec87", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: 1 of 1 files downloaded. \n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
chromstartendsplit
0chr188956181092226train
1chr4113598179113794787train
2chr111839495218591560train
\n", "
" ], "text/plain": [ " chrom start end split\n", "0 chr18 895618 1092226 train\n", "1 chr4 113598179 113794787 train\n", "2 chr11 18394952 18591560 train" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "artifact = run.use_artifact('enformer/human_intervals:latest')\n", "dir = artifact.download()\n", "enformer_intervals = pd.read_table(os.path.join(dir, \"data.tsv\"))\n", "enformer_intervals.head(3)" ] }, { "cell_type": "markdown", "id": "842a7aa6-e8ae-4969-8bba-72bb532575c9", "metadata": {}, "source": [ "## Split peaks based on their overlap with enformer" ] }, { "cell_type": "code", "execution_count": 13, "id": "d3c8c95a-9386-4c6e-9f21-d4370dac178c", "metadata": {}, "outputs": [], "source": [ "ad.var = ad.var.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 14, "id": "57727d0b-6187-4640-9947-d247d4225166", "metadata": {}, "outputs": [], "source": [ "ad.var['cre_idx'] = range(len(ad.var))" ] }, { "cell_type": "code", "execution_count": 15, "id": "04f3e704-cd4d-44ea-bcc1-d4c9dc1db1db", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "split_\n", "train 966953\n", "test 72507\n", "valid 71703\n", "None 9670\n", "testtrain 222\n", "trainvalid 169\n", "testvalid 95\n", "Name: count, dtype: int64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "overlaps = bf.overlap(ad.var, enformer_intervals, how='left')\n", "overlaps.split_ = overlaps.split_.fillna('None')\n", "\n", "overlaps = overlaps.groupby('cre_idx').split_.apply(lambda x: ''.join(list(np.unique(x))))\n", "overlaps.value_counts()" ] }, { "cell_type": "code", "execution_count": 16, "id": "72775c43-2996-456d-879a-e077b11ba736", "metadata": {}, "outputs": [], "source": [ "assert np.all(overlaps.index == ad.var.cre_idx)" ] }, { "cell_type": "code", "execution_count": 17, "id": "a317db18-c0e9-425d-86da-98cd6bdac3e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "train 977014\n", "test 72507\n", "valid 71798\n", "Name: count, dtype: int64" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_splits = np.array(['train'] * len(overlaps))\n", "new_splits[[(('valid' in x) and ('train' not in x)) for x in overlaps]] = 'valid'\n", "new_splits[[(('test' in x) and ('train' not in x) and ('valid' not in x)) for x in overlaps]] = 'test'\n", "pd.Series(new_splits).value_counts()" ] }, { "cell_type": "code", "execution_count": 18, "id": "3c0f944d-ee42-45a9-ad3b-41e4a53aba10", "metadata": {}, "outputs": [], "source": [ "ad.var['enformer_split'] = overlaps\n", "ad.var['split'] = new_splits" ] }, { "cell_type": "markdown", "id": "04ff9b90-956d-4150-bbf8-f5c29826f6fb", "metadata": {}, "source": [ "## Save" ] }, { "cell_type": "code", "execution_count": 19, "id": "e947777a-3a1f-4e07-995f-c0e85e9dedf6", "metadata": {}, "outputs": [], "source": [ "ad.write_h5ad('preprocessed.h5ad')" ] }, { "cell_type": "code", "execution_count": 20, "id": "e39fb051-0a80-4832-a962-ab62e6995e95", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "artifact = wandb.Artifact('dataset', type='dataset')\n", "artifact.add_file(local_path='preprocessed.h5ad', name='data.h5ad')\n", "run.log_artifact(artifact)" ] }, { "cell_type": "code", "execution_count": 21, "id": "573db082-362c-4caa-a7d7-53e1dff271e6", "metadata": {}, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View run prep at: https://wandb.ai/grelu/human-atac-catlas/runs/vphymp69
View project at: https://wandb.ai/grelu/human-atac-catlas
Synced 6 W&B file(s), 0 media file(s), 2 artifact file(s) and 0 other file(s)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "Find logs at: ./wandb/run-20250306_000043-vphymp69/logs" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "run.finish()" ] }, { "cell_type": "code", "execution_count": null, "id": "8e408e2f-babd-491b-8287-f131b6a82c7e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }