{ "cells": [ { "cell_type": "markdown", "id": "6b984a20-40eb-4c08-8494-ebb607e91b94", "metadata": {}, "source": [ "# Process data for chromHMM multiclass classification model" ] }, { "cell_type": "markdown", "id": "df5cf6ce-dd2c-445f-a5b3-a13e9fd07d17", "metadata": {}, "source": [ "## Set up wandb" ] }, { "cell_type": "code", "execution_count": 1, "id": "35c13d53-f8f7-45c9-a7f0-c0e92d7654c9", "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" ] } ], "source": [ "import wandb\n", "import anndata\n", "import pandas as pd\n", "import numpy as np\n", "import os\n", "\n", "wandb.login(host=\"https://api.wandb.ai\")\n", "project_name = 'human-chromhmm-fullstack'" ] }, { "cell_type": "code", "execution_count": 2, "id": "37ecd399-3fd9-4687-9d7d-95638f529dc1", "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/chromhmm/wandb/run-20250306_045811-8fux0bft" ], "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-chromhmm-fullstack" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View run at https://wandb.ai/grelu/human-chromhmm-fullstack/runs/8fux0bft" ], "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/chromhmm/1_data.ipynb')\n", ")" ] }, { "cell_type": "markdown", "id": "b4b88d95-a65a-4e75-a309-032faf846c03", "metadata": {}, "source": [ "## Load data" ] }, { "cell_type": "code", "execution_count": 3, "id": "1527622f-94c0-4377-b125-407a0eef4bec", "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", "
chromstartendstate
0chr110000104002_GapArtf2
1chr1104001060027_Acet1
2chr1106001080038_EnhWk4
3chr110800128001_GapArtf1
4chr1128001300038_EnhWk4
\n", "
" ], "text/plain": [ " chrom start end state\n", "0 chr1 10000 10400 2_GapArtf2\n", "1 chr1 10400 10600 27_Acet1\n", "2 chr1 10600 10800 38_EnhWk4\n", "3 chr1 10800 12800 1_GapArtf1\n", "4 chr1 12800 13000 38_EnhWk4" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chromhmm = pd.read_table('https://public.hoffman2.idre.ucla.edu/ernst/2K9RS//full_stack/full_stack_annotation_public_release/hg38/hg38_genome_100_segments.bed.gz', header=None)\n", "chromhmm.columns = ['chrom', 'start', 'end', 'state']\n", "chromhmm.head()" ] }, { "cell_type": "markdown", "id": "91bec5b4-201c-4c66-992f-6b54ba5bc71e", "metadata": {}, "source": [ "## Process data" ] }, { "cell_type": "code", "execution_count": 4, "id": "394d7f20-121f-4da7-886e-7b4184d2128d", "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" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Keeping 5845850 intervals\n", "Keeping 5809104 intervals\n" ] } ], "source": [ "from grelu.data.preprocess import filter_chromosomes, filter_blacklist\n", "from grelu.sequence.utils import resize\n", "\n", "chromhmm = filter_chromosomes(chromhmm, include='autosomes')\n", "chromhmm = resize(chromhmm, 1024)\n", "chromhmm = filter_blacklist(chromhmm, 'hg38')" ] }, { "cell_type": "markdown", "id": "8a93565f-d819-44d4-911c-d8d1f5d7a052", "metadata": {}, "source": [ "## Get coarse-grained state labels" ] }, { "cell_type": "code", "execution_count": 5, "id": "44474b8b-7f33-4608-95cb-b77e87fb4840", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "state\n", "Quies 1485576\n", "Acet 639669\n", "EnhA 613794\n", "ReprPC 610147\n", "Tx 561526\n", "EnhWk 543113\n", "HET 521161\n", "TxWk 254518\n", "TxEnh 190465\n", "TxEx 121833\n", "PromF 88429\n", "GapArtf 51474\n", "BivProm 48242\n", "znf 34146\n", "TSS 24402\n", "DNase 20609\n", "Name: count, dtype: int64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chromhmm['state'] = [\n", " x.split('_')[1][:-1] for x in chromhmm.state\n", "]\n", "chromhmm.loc[chromhmm.state.isin(['EnhA1', 'EnhA2']), 'state'] = 'EnhA'\n", "\n", "chromhmm['state'] = chromhmm['state'].astype('category')\n", "chromhmm.state.value_counts() " ] }, { "cell_type": "markdown", "id": "3eb00f57-972a-4b07-badd-d8edf20e9d9e", "metadata": {}, "source": [ "## Load Enformer splits" ] }, { "cell_type": "code", "execution_count": 6, "id": "89717895-524f-4ea5-bacc-4914e8893096", "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": 6, "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": "e19fd34a-5797-49f3-a838-c3e2d0342be0", "metadata": {}, "source": [ "## Split regions based on their overlap with Enformer split" ] }, { "cell_type": "code", "execution_count": 7, "id": "86d33170-ccb8-4245-9e7e-c12f1ca9fb2f", "metadata": {}, "outputs": [], "source": [ "chromhmm = chromhmm.reset_index(drop=True)\n", "chromhmm['interval_idx'] = range(len(chromhmm))" ] }, { "cell_type": "code", "execution_count": 8, "id": "6d7628c5-3e70-4835-8c04-b6d193490419", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "split_\n", "train 4963283\n", "test 402392\n", "valid 363619\n", "None 76215\n", "testtrain 1606\n", "trainvalid 1221\n", "testvalid 768\n", "Name: count, dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import bioframe as bf\n", "overlaps = bf.overlap(chromhmm, enformer_intervals, how='left')\n", "overlaps.split_ = overlaps.split_.fillna('None')\n", "\n", "overlaps = overlaps.groupby('interval_idx').split_.apply(lambda x: ''.join(list(np.unique(x))))\n", "overlaps.value_counts()" ] }, { "cell_type": "code", "execution_count": 9, "id": "562e9b6e-aaf3-4c11-82fb-41f8b69d94cf", "metadata": {}, "outputs": [], "source": [ "assert np.all(overlaps.index == chromhmm.interval_idx)" ] }, { "cell_type": "code", "execution_count": 10, "id": "a43a1227-154f-47f7-90fd-a7fd5f59ce3b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "train 5042325\n", "test 402392\n", "valid 364387\n", "Name: count, dtype: int64" ] }, "execution_count": 10, "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": 11, "id": "04c0bede-0f36-4d07-bd81-7988ca856c09", "metadata": {}, "outputs": [], "source": [ "chromhmm['enformer_split'] = overlaps\n", "chromhmm['split'] = new_splits" ] }, { "cell_type": "markdown", "id": "7dbf9d8a-aa8f-45b0-a572-e7e3a6abb607", "metadata": {}, "source": [ "## Save dataset" ] }, { "cell_type": "code", "execution_count": 12, "id": "801efe31-dfbc-4052-81bf-9b1de399c8f3", "metadata": {}, "outputs": [], "source": [ "chromhmm.to_csv('chromhmm.csv.gz', index=False) " ] }, { "cell_type": "code", "execution_count": 13, "id": "be914f0c-da00-4b1a-820b-8d61d6725b70", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "artifact = wandb.Artifact('dataset', type='dataset')\n", "artifact.add_file(local_path='chromhmm.csv.gz', name='data.csv.gz')\n", "run.log_artifact(artifact)" ] }, { "cell_type": "code", "execution_count": 14, "id": "7afe340f-d1cd-43d1-a6ce-0db10c2e2e63", "metadata": {}, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View run prep at: https://wandb.ai/grelu/human-chromhmm-fullstack/runs/8fux0bft
View project at: https://wandb.ai/grelu/human-chromhmm-fullstack
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_045811-8fux0bft/logs" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "run.finish()" ] }, { "cell_type": "code", "execution_count": null, "id": "46094f9a-324c-45ef-acf1-051c7d7ebe3d", "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 }