{ "cells": [ { "cell_type": "markdown", "id": "b68cf1d8", "metadata": {}, "source": [ "# Tutorial: Creating an AnnData Object from Tahoe-100M Dataset\n", "This notebook is intented for users who are familiar with the anndata format for single-cell data. We'll walk through how to parse records in the huggingface dataset format and convert between the two." ] }, { "cell_type": "markdown", "id": "fc9a7282", "metadata": {}, "source": [ "## Install Required Libraries" ] }, { "cell_type": "code", "execution_count": null, "id": "21bab5b0", "metadata": {}, "outputs": [], "source": [ "!pip install datasets anndata scipy pandas pubchempy" ] }, { "cell_type": "markdown", "id": "7e9bf44e", "metadata": {}, "source": [ "## Import Libraries" ] }, { "cell_type": "code", "execution_count": null, "id": "a7589c73", "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset\n", "from scipy.sparse import csr_matrix\n", "import anndata\n", "import pandas as pd\n", "import pubchempy as pcp" ] }, { "cell_type": "markdown", "id": "f31cd11c", "metadata": {}, "source": [ "## Mapping records to anndata\n", "\n", "This function takes in a generator that emits records from the Tahoe-100M huggingface dataset and returns an anndata object. Use the `sample_size` argument to specify the number of records you need. You can also create a new generator using the `dataset.filter` function to only emit records that match a certain filter (eg: for a specific drug/plate/sample).\n", "\n", "If you'd like to create a DataLoader for an ML training application, it's likely best to use the data in it's native format without interfacing with anndata." ] }, { "cell_type": "code", "execution_count": null, "id": "f4391697", "metadata": {}, "outputs": [], "source": [ "\n", "def create_anndata_from_generator(generator, gene_vocab, sample_size=None):\n", " sorted_vocab_items = sorted(gene_vocab.items())\n", " token_ids, gene_names = zip(*sorted_vocab_items)\n", " token_id_to_col_idx = {token_id: idx for idx, token_id in enumerate(token_ids)}\n", "\n", " data, indices, indptr = [], [], [0]\n", " obs_data = []\n", "\n", " for i, cell in enumerate(generator):\n", " if sample_size is not None and i >= sample_size:\n", " break\n", " genes = cell['genes']\n", " expressions = cell['expressions']\n", " if expressions[0] < 0: \n", " genes = genes[1:]\n", " expressions = expressions[1:]\n", "\n", " col_indices = [token_id_to_col_idx[gene] for gene in genes if gene in token_id_to_col_idx]\n", " valid_expressions = [expr for gene, expr in zip(genes, expressions) if gene in token_id_to_col_idx]\n", "\n", " data.extend(valid_expressions)\n", " indices.extend(col_indices)\n", " indptr.append(len(data))\n", "\n", " obs_entry = {k: v for k, v in cell.items() if k not in ['genes', 'expressions']}\n", " obs_data.append(obs_entry)\n", "\n", " expr_matrix = csr_matrix((data, indices, indptr), shape=(len(indptr) - 1, len(gene_names)))\n", " obs_df = pd.DataFrame(obs_data)\n", "\n", " adata = anndata.AnnData(X=expr_matrix, obs=obs_df)\n", " adata.var.index = pd.Index(gene_names, name='ensembl_id')\n", "\n", " return adata\n" ] }, { "cell_type": "markdown", "id": "0cf683cd", "metadata": {}, "source": [ "## Load Tahoe-100M Dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "80eb5104", "metadata": {}, "outputs": [], "source": [ "tahoe_100m_ds = load_dataset('vevotx/Tahoe-100M', streaming=True, split='train')" ] }, { "cell_type": "markdown", "id": "337f02f2", "metadata": {}, "source": [ "## Load Gene Metadata\n", "\n", "The gene metadata contains the mapping between the integer token IDs used in the dataset and standard identifiers for genes (ensembl IDs and HGNC gene symbols)" ] }, { "cell_type": "code", "execution_count": null, "id": "a0eeaa83", "metadata": {}, "outputs": [], "source": [ "gene_metadata = load_dataset(\"vevotx/Tahoe-100M\", name=\"gene_metadata\", split=\"train\")\n", "gene_vocab = {entry[\"token_id\"]: entry[\"ensembl_id\"] for entry in gene_metadata}" ] }, { "cell_type": "markdown", "id": "ded9c086", "metadata": {}, "source": [ "## Create AnnData Object" ] }, { "cell_type": "code", "execution_count": null, "id": "6fb1d70d", "metadata": {}, "outputs": [], "source": [ "adata = create_anndata_from_generator(tahoe_100m_ds, gene_vocab, sample_size=1000)\n", "adata" ] }, { "cell_type": "markdown", "id": "c7c07f9e", "metadata": {}, "source": [ "## Inspect Metadata (`adata.obs`)" ] }, { "cell_type": "code", "execution_count": null, "id": "15214a5c", "metadata": {}, "outputs": [], "source": [ "adata.obs.head()" ] }, { "cell_type": "markdown", "id": "ec391116", "metadata": {}, "source": [ "## Enrich with Sample Metadata\n", "\n", "Although the main data contains several metadata fields, there are some additional columns (such as drug concentration) which are omitted to reduce the size of the data. If they are needed, they may be fetched using the sample_metadata." ] }, { "cell_type": "code", "execution_count": null, "id": "657524c8", "metadata": {}, "outputs": [], "source": [ "sample_metadata = load_dataset(\"vevotx/Tahoe-100M\",\"sample_metadata\", split=\"train\").to_pandas()\n", "adata.obs = pd.merge(adata.obs, sample_metadata.drop(columns=[\"drug\",\"plate\"]), on=\"sample\")\n", "adata.obs.head()" ] }, { "cell_type": "markdown", "id": "a1504ad7", "metadata": {}, "source": [ "## Add Drug Metadata\n", "\n", "The drug metadata contains additional information for the compounds used in Tahoe-100M. See the dataset card and our [paper](https://www.biorxiv.org/content/10.1101/2025.02.20.639398v1) for more information about how this information was generated." ] }, { "cell_type": "code", "execution_count": null, "id": "741c8bcc", "metadata": {}, "outputs": [], "source": [ "drug_metadata = load_dataset(\"vevotx/Tahoe-100M\",\"drug_metadata\", split=\"train\").to_pandas()\n", "adata.obs = pd.merge(adata.obs, drug_metadata.drop(columns=[\"canonical_smiles\",\"pubchem_cid\",\"moa-fine\"]), on=\"drug\")\n", "adata.obs.head()" ] }, { "cell_type": "markdown", "id": "d7eb71ff", "metadata": {}, "source": [ "## Drug Info from PubChem\n", "\n", "We also provide the pubchem IDs for the compounds in Tahoe, this can be used to querry additional information as needed." ] }, { "cell_type": "code", "execution_count": null, "id": "05d74c80", "metadata": {}, "outputs": [], "source": [ "drug_name = adata.obs[\"drug\"].values[0]\n", "cid = int(float(adata.obs[\"pubchem_cid\"].values[0]))\n", "compound = pcp.Compound.from_cid(cid)\n", "\n", "print(f\"Name: {drug_name}\")\n", "print(f\"Synonyms: {compound.synonyms[:10]}\")\n", "print(f\"Formula: {compound.molecular_formula}\")\n", "print(f\"SMILES: {compound.isomeric_smiles}\")\n", "print(f\"Mass: {compound.exact_mass}\")" ] }, { "cell_type": "markdown", "id": "2dc7179c", "metadata": {}, "source": [ "## Load Cell Line Metadata\n", "The cell-line metadata contains additional identifiers for the \n", "cell-lines used in Tahoe (eg: Depmap-IDs) as well as a curated list of driver mutations for each cell line. This information can be used for instance to train genotype aware models on the Tahoe data." ] }, { "cell_type": "code", "execution_count": null, "id": "6519967a", "metadata": {}, "outputs": [], "source": [ "cell_line_metadata = load_dataset(\"vevotx/Tahoe-100M\",\"cell_line_metadata\", split=\"train\").to_pandas()\n", "cell_line_metadata.head()" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }