{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "6c08a8f0", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2026-06-09 13:29:16.828 WARNING streamlit.runtime.caching.cache_data_api: No runtime found, using MemoryCacheStorageManager\n", "2026-06-09 13:29:16.829 WARNING streamlit.runtime.caching.cache_data_api: No runtime found, using MemoryCacheStorageManager\n", "2026-06-09 13:29:16.830 WARNING streamlit.runtime.caching.cache_data_api: No runtime found, using MemoryCacheStorageManager\n", "2026-06-09 13:29:16.830 WARNING streamlit.runtime.caching.cache_data_api: No runtime found, using MemoryCacheStorageManager\n", "2026-06-09 13:29:16.831 WARNING streamlit.runtime.caching.cache_data_api: No runtime found, using MemoryCacheStorageManager\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Base directory: /home/lippincm/gene-process-dependencies/9.webapp\n", "Repo root: /home/lippincm/gene-process-dependencies\n" ] } ], "source": [ "import os\n", "import pathlib\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import streamlit as st\n", "from app_utils import latent_load_data, load_model_data, single_load_data\n", "from sklearn.decomposition import PCA" ] }, { "cell_type": "code", "execution_count": 3, "id": "328325ad", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2026-06-09 13:29:16.839 WARNING streamlit.runtime.caching.cache_data_api: No runtime found, using MemoryCacheStorageManager\n", "2026-06-09 13:29:16.840 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Base directory: /home/lippincm/gene-process-dependencies/9.webapp\n", "Repo root: /home/lippincm/gene-process-dependencies\n" ] } ], "source": [ "_base_reactome, _base_corum, _base_drug = latent_load_data()\n", "ALL_DISEASES = sorted(_base_reactome[\"OncotreePrimaryDisease\"].unique().tolist())\n", "ALL_MODEL_IDS = sorted(_base_reactome[\"ModelID\"].unique().tolist())\n", "# Go up one level to repo root, then into data/\n", "BASE_DIR = pathlib.Path().resolve()\n", "REPO_ROOT = BASE_DIR.parent\n", "REPO_ROOT = BASE_DIR.parent # goes up from 9.webapp/ to repo root\n", "print(f\"Base directory: {BASE_DIR}\")\n", "print(f\"Repo root: {REPO_ROOT}\")" ] }, { "cell_type": "code", "execution_count": 46, "id": "37924565", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1150, 18444)\n" ] } ], "source": [ "combined_df = single_load_data()\n", "data_directory = BASE_DIR / \"data\"\n", "dependency_file = data_directory / \"CRISPRGeneEffect.parquet\"\n", "gene_dict_file = data_directory / \"CRISPR_gene_dictionary.parquet\"\n", "cancer_type_input_file = BASE_DIR / \"data\" / \"Model.parquet\"\n", "dependency_df, gene_dict_df = load_model_data(dependency_file, gene_dict_file)\n", "dependency_df = dependency_df.set_index(\"ModelID\")\n", "cancer_type_input_file = pathlib.Path(f\"{BASE_DIR}/data/Model.parquet\")\n", "cancer_type_df = pd.read_parquet(cancer_type_input_file)\n", "reactome_matrix, corum_matrix, drug_matrix = latent_load_data()" ] }, { "cell_type": "code", "execution_count": null, "id": "0ef7365a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 63, "id": "4f84dac0", "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ModelIDOncotreePrimaryDiseaselatent_scoregsea_es_scorepathway_scorefeature
0ACH-000001Ovarian Epithelial Tumor0.3928600.8243150.392860Inactivation, Recovery And Regulation Of Photo...
1ACH-000001Ovarian Epithelial Tumor0.7636300.8801160.763630Elastic Fibre Formation R-HSA-1566948
2ACH-000001Ovarian Epithelial Tumor0.4355310.8395060.435531Mismatch Repair (MMR) Directed By MSH2:MSH6 (M...
3ACH-000001Ovarian Epithelial Tumor0.531491-0.7333750.531491Processive Synthesis On Lagging Strand R-HSA-6...
4ACH-000001Ovarian Epithelial Tumor0.2677500.8642570.267750Mismatch Repair (MMR) Directed By MSH2:MSH3 (M...
.....................
169777ACH-003012Non-Small Cell Lung Cancer0.5052960.9382710.505296Kinase maturation complex 1 (human)
169778ACH-003012Non-Small Cell Lung Cancer0.2826340.8082170.282634immunoproteasome (mouse)
169779ACH-003012Non-Small Cell Lung Cancer0.0736970.7480240.073697ALL-1 supercomplex (human)
169780ACH-003012Non-Small Cell Lung Cancer0.0329450.7396780.032945Emerin complex 32 (human)
169781ACH-003012Non-Small Cell Lung Cancer0.2832630.8150850.283263Ikaros complex (mouse)
\n", "

169782 rows × 6 columns

\n", "
" ], "text/plain": [ " ModelID OncotreePrimaryDisease latent_score gsea_es_score \\\n", "0 ACH-000001 Ovarian Epithelial Tumor 0.392860 0.824315 \n", "1 ACH-000001 Ovarian Epithelial Tumor 0.763630 0.880116 \n", "2 ACH-000001 Ovarian Epithelial Tumor 0.435531 0.839506 \n", "3 ACH-000001 Ovarian Epithelial Tumor 0.531491 -0.733375 \n", "4 ACH-000001 Ovarian Epithelial Tumor 0.267750 0.864257 \n", "... ... ... ... ... \n", "169777 ACH-003012 Non-Small Cell Lung Cancer 0.505296 0.938271 \n", "169778 ACH-003012 Non-Small Cell Lung Cancer 0.282634 0.808217 \n", "169779 ACH-003012 Non-Small Cell Lung Cancer 0.073697 0.748024 \n", "169780 ACH-003012 Non-Small Cell Lung Cancer 0.032945 0.739678 \n", "169781 ACH-003012 Non-Small Cell Lung Cancer 0.283263 0.815085 \n", "\n", " pathway_score feature \n", "0 0.392860 Inactivation, Recovery And Regulation Of Photo... \n", "1 0.763630 Elastic Fibre Formation R-HSA-1566948 \n", "2 0.435531 Mismatch Repair (MMR) Directed By MSH2:MSH6 (M... \n", "3 0.531491 Processive Synthesis On Lagging Strand R-HSA-6... \n", "4 0.267750 Mismatch Repair (MMR) Directed By MSH2:MSH3 (M... \n", "... ... ... \n", "169777 0.505296 Kinase maturation complex 1 (human) \n", "169778 0.282634 immunoproteasome (mouse) \n", "169779 0.073697 ALL-1 supercomplex (human) \n", "169780 0.032945 Emerin complex 32 (human) \n", "169781 0.283263 Ikaros complex (mouse) \n", "\n", "[169782 rows x 6 columns]" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "corum_matrix_path = (\n", " REPO_ROOT / \"5.drug-dependency\" / \"results\" / \"all_corum_results.parquet\"\n", ")\n", "corum_matrix = pd.read_parquet(corum_matrix_path)\n", "corum_matrix[\"feature\"] = corum_matrix[\"reactome_pathway\"]\n", "corum_matrix = corum_matrix.drop(\n", " columns=[\"model\", \"latent_dim_total\", \"init\", \"seed\", \"z\", \"reactome_pathway\"]\n", ")\n", "meta_cols = [\"ModelID\", \"OncotreePrimaryDisease\"]\n", "\n", "corum_meta = corum_matrix[meta_cols].drop_duplicates()\n", "\n", "# corum_matrix = corum_matrix.pivot(index=\"ModelID\", columns=\"feature\", values=\"latent_score\")\n", "# corum_matrix = corum_matrix.merge(corum_meta, on=\"ModelID\", how=\"left\")\n", "# print(corum_matrix.isna().sum().sum())\n", "# corum_matrix.drop(columns=['gsea_es_score',\"pathway_score\",\"OncotreePrimaryDisease\"], inplace=True)\n", "# corum_matrix.pivot(index=\"ModelID\", columns=\"feature\", values=\"latent_score\")\n", "corum_matrix" ] }, { "cell_type": "code", "execution_count": 61, "id": "0f7fe22e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inactivation, Recovery And Regulation Of Phototransduction Cascade R-HSA-2514859\n", "Elastic Fibre Formation R-HSA-1566948\n", "Mismatch Repair (MMR) Directed By MSH2:MSH6 (MutSalpha) R-HSA-5358565\n", "Processive Synthesis On Lagging Strand R-HSA-69183\n", "Mismatch Repair (MMR) Directed By MSH2:MSH3 (MutSbeta) R-HSA-5358606\n", "Phototransduction Cascade R-HSA-2514856\n", "RNA polymerase II complex, chromatin structure modifying (human)\n", "Integrator-RNAPII complex (human)\n", "Parvulin-associated pre-rRNP complex (mouse)\n", "PCAF complex (human)\n", "DNA synthesome complex (17 subunits) (human)\n", "CTF18-cohesion-RFC-POLH complex (human)\n", "FCP1-associated protein complex (human)\n", "CF IIAm complex (Cleavage factor IIAm complex) (human)\n", "BRM-SIN3A-HDAC complex (human)\n", "TNF-alpha/NF-kappa B signaling complex 6 (human)\n", "TNF-alpha/NF-kappa B signaling complex (CHUK, BTRC, NFKB2, PPP6C, REL, CUL1, IKBKE, SAPS2, SAPS1, ANKRD28, RELA, SKP1) (human)\n", "E2F-6 complex (human)\n", "BRD4 complex (human)\n", "RC complex (Replication competent complex) (human)\n", "PBAF complex (Polybromo- and BAF containing complex) (human)\n", "Emerin complex 24 (human)\n", "DSS1 complex (human)\n", "TFIIH transcription factor complex (human)\n", "Prefoldin complex (human)\n", "Gata1-Fog1-MeCP1 complex (mouse)\n", "URI complex (Unconventional prefoldin RPB5 Interactor) (human)\n", "BRM-SIN3A complex (human)\n", "RSmad complex (human)\n", "MLL1-WDR5 complex (human)\n", "12S U11 snRNP (human)\n", "BRAFT complex (human)\n", "FA complex (Fanconi anemia complex) (human)\n", "SMN complex (human)\n", "p400-associated complex (human)\n", "GCN5-TRRAP histone acetyltransferase complex (human)\n", "Anaphase-promoting complex (human)\n", "BAF complex (human)\n", "UTX-MLL2/3 complex (human)\n", "Rnase/Mrp complex (human)\n", "NCOR2 complex (human)\n", "NUMAC complex (nucleosomal methylation activator complex) (human)\n", "PA700 complex (human)\n", "Cell cycle kinase complex CDC2 (human)\n", "CDC5L core complex (human)\n", "SRCAP-associated chromatin remodeling complex (human)\n", "5S-DNA-TFIIIA-TFIIIC2-TFIIIB subcomplex (human)\n", "Respiratory chain complex I, mitochondrial (mouse)\n", "TRAP complex (human)\n", "TNF-alpha/NF-kappa B signaling complex 5 (human)\n", "HCF-1 complex (human)\n", "SMN-PolII-RHA complex (human)\n", "DAB complex (human)\n", "mRNA decay complex (UPF1, UPF2, UPF3B, DCP2, XRN1, XRN2, EXOSC2, EXOSC4, EXOSC10, PARN) (human)\n", "TFTC-type histone acetyl transferase complex (human)\n", "BLM complex II (human)\n", "ARC-L complex (human)\n", "RNA polymerase II (RNAPII) (human)\n", "PA28gamma-20S proteasome (human)\n", "BRG1-SIN3A complex (human)\n", "5S-DNA-TFIIIA-TFIIIC2 subcomplex (human)\n", "LSm1-7 complex (human)\n", "NCOR1 complex (human)\n", "PA28-20S proteasome (human)\n", "TFTC complex (TATA-binding protein-free TAF-II-containing complex) (human)\n", "PC2 complex (human)\n", "SNF2h-cohesin-NuRD complex (human)\n", "Respiratory chain complex I (early intermediate NDUFAF1 assembly), mitochondrial (human)\n", "RC complex during G2/M-phase of cell cycle (human)\n", "Polycystin-1 multiprotein complex (ACTN1, CDH1, SRC, JUP, VCL, CTNNB1, PXN, BCAR1, PKD1, PTK2, TLN1) (human)\n", "Anti-HDAC2 complex (human)\n", "p300-CBP-p270-SWI/SNF complex (human)\n", "Ubiquitin E3 ligase (CSN1, CSN8, HRT1, SKP1, SKP2, CUL1, CUL2, CUL3) (human)\n", "TFIID complex (mouse)\n", "TFIID complex (human)\n", "TNF-alpha/NF-kappa B signaling complex (SEC16A, CHUK, IKBKB, NFKB2, REL, IKBKG, MAP3K14, RELA, FBXW7, USP2) (human)\n", "NAT complex (human)\n", "EBAFb complex (human)\n", "Nop56p-associated pre-rRNA complex (human)\n", "DA complex (human)\n", "HDAC1-associated core complex cII (human)\n", "BRCA1-RNA polymerase II complex (human)\n", "p97/VCP-VIMP-DERL1-DERL2-HRD1-SEL1L complex (human)\n", "INO80 chromatin remodeling complex (human)\n", "ARC complex (human)\n", "DRIP complex (human)\n", "WINAC complex (human)\n", "SIN3-ING1b complex II (human)\n", "ING2 complex (human)\n", "SIN3-SAP25 complex (human)\n", "COP9 signalosome complex (human)\n", "Integrator complex (human)\n", "CtBP complex (human)\n", "ESR1-CDK7-CCNH-MNAT1-MTA1-HDAC2 complex (human)\n", "26S proteasome (human)\n", "ESCRT-III complex (human)\n", "SIN3-ING1b complex I (human)\n", "HDAC2-asscociated core complex (human)\n", "CHUK-NFKB2-REL-IKBKG-SPAG9-NFKB1-NFKBIE-COPB2-TNIP1-NFKBIA-RELA-TNIP2 complex (human)\n", "SWI-SNF chromatin remodeling-related-BRCA1 complex (human)\n", "STAGA complex (SPT3-TAF9-GCN5 acetyltransferase complex) (human)\n", "Cytochrome c oxidase (EC 1.9.3.1), mitochondrial (mouse)\n", "NuA4/Tip60-HAT complex B (human)\n", "LARC complex (LCR-associated remodeling complex) (human)\n", "MeCP1 complex (human)\n", "HES1 promoter-Notch enhancer complex (human)\n", "NuA4/Tip60-HAT complex A (human)\n", "Exosome (human)\n", "18S U11/U12 snRNP (human)\n", "Conserved oligomeric Golgi (COG) complex (human)\n", "Emerin complex 25 (human)\n", "TNF-alpha/Nf-kappa B signaling complex (RPL6, RPL30, RPS13, CHUK, DDX3X, NFKB2, NFKBIB, REL, IKBKG, NFKB1, MAP3K8, RELB, GLG1, NFKBIA, RELA, TNIP2, GTF2I) (human)\n", "BRD4-RFC complex (human)\n", "Ubiquitin E3 ligase (AHR, ARNT, DDB1, TBL3, CUL4B, RBX1) (human)\n", "CRM1-RAN-PHAX-CBC complex (cap binding complex) (human)\n", "RNA polymerase II holoenzyme complex (human)\n", "BRCA1-core RNA polymerase II complex (human)\n", "RC complex during S-phase of cell cycle (human)\n", "TNF-alpha/NF-kappa B signaling complex (CHUK, KPNA3, NFKB2, NFKBIB, REL, IKBKG, NFKB1, NFKBIE, RELB, NFKBIA, RELA, TNIP2) (human)\n", "LSm2-8 complex (human)\n", "Toposome (human)\n", "ARC92-Mediator complex (human)\n", "CSA complex (human)\n", "CENP-A NAC-CAD complex (human)\n", "CCT complex (chaperonin containing TCP1 complex) (mouse)\n", "CSA-POLIIa complex (human)\n", "PTIP-DNA damage response complex (human)\n", "BRCA1-BARD1-BACH1-DNA damage complex II (human)\n", "cMYC-ATPase-helicase complex (human)\n", "Anaphase-promoting complex (mouse)\n", "Large Drosha complex (human)\n", "CRSP complex (human)\n", "SMCC complex (human)\n", "17S U2 snRNP (human)\n", "TRAP-SMCC mediator complex (human)\n", "MTA2 complex (human)\n", "Mediator complex (mouse)\n", "Anti-Sm protein complex (human)\n", "20S proteasome (human)\n", "DMAP1-associated complex (human)\n", "RNA polymerase II complex, incomplete (CDK8 complex), chromatin structure modifying (human)\n", "Cytochrome bc1-complex (EC 1.10.2.2), mitochondrial (mouse)\n", "RAD17-RFC-9-1-1 checkpoint supercomplex (human)\n", "TFIID complex, B-cell specific (human)\n", "CEN complex (human)\n", "DDB2 complex (human)\n", "BASC complex (BRCA1-associated genome surveillance complex) (human)\n", "CRSP-Mediator 2 complex (human)\n", "RNA polymerase II core complex (human)\n", "MDC1-MRN-ATM-FANCD2 complex (human)\n", "PCNA-CHL12-RFC2-5 complex (human)\n", "EBAFa complex (human)\n", "Condensin II (human)\n", "Kaiso-NCOR complex (human)\n", "SF3b complex (human)\n", "PTIP-HMT complex (human)\n", "Nup 107-160 subcomplex (human)\n", "p27-cyclinE-Cdk2 - Ubiquitin E3 ligase (SKP1A, SKP2, CUL1, CKS1B, RBX1) complex (human)\n", "TFIID-beta complex (human)\n", "PYR complex (mouse)\n", "Kinase maturation complex 1 (human)\n", "immunoproteasome (mouse)\n", "ALL-1 supercomplex (human)\n", "Emerin complex 32 (human)\n", "Ikaros complex (mouse)\n" ] } ], "source": [ "for x in corum_matrix[\"feature\"].unique():\n", " print(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "2766b131", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "97fd0a08", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "14ed27d8", "metadata": {}, "outputs": [], "source": [ "# get all modelids that have NA values in the corum matrix\n", "corum_matrix[corum_matrix.isna().any(axis=1)][\"ModelID\"].sort_values().unique()" ] }, { "cell_type": "code", "execution_count": null, "id": "484e6f14", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 18, "id": "6cdc8f80", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1150 rows before dropping NA\n", "1150 rows after dropping NA\n", "1150 rows before dropping NA\n", "958 rows after dropping NA\n", "1150 rows before dropping NA\n", "958 rows after dropping NA\n", "1150 rows before dropping NA\n", "958 rows after dropping NA\n" ] } ], "source": [ "# @st.cache_data\n", "\n", "\n", "def compute_pca(df, metadata_columns) -> pd.DataFrame:\n", " \"\"\"Cache PCA result per disease selection.\"\"\"\n", " # df.fillna(0, inplace=True) # Fill NA with 0 for PCA, or drop rows with NA\n", " print(df.shape[0], \"rows before dropping NA\")\n", " df.dropna(inplace=True) # PCA can't handle missing values, so drop rows with any NA\n", " print(df.shape[0], \"rows after dropping NA\")\n", " metadata_df = df[metadata_columns]\n", " feat_cols = df.columns.drop(metadata_columns)\n", " if \"source\" in feat_cols:\n", " feat_cols = feat_cols.drop(\"source\")\n", "\n", " pca_input = df[feat_cols].apply(pd.to_numeric, errors=\"coerce\")\n", " pca = PCA(n_components=2, random_state=0)\n", " pca_embedding = pca.fit_transform(pca_input)\n", " metadata_df[\"PCA1\"] = pca_embedding[:, 0]\n", " metadata_df[\"PCA2\"] = pca_embedding[:, 1]\n", " return metadata_df\n", "\n", "\n", "pca_single_df = compute_pca(combined_df, [\"ModelID\", \"OncotreePrimaryDisease\"])\n", "# save the PCA result to a parquet file for later use in the app\n", "pca_single_df.to_parquet(\n", " BASE_DIR / \"data\" / \"pca_embeddings_single_dependencies.parquet\"\n", ")\n", "\n", "pca_latent_reactome_df = compute_pca(\n", " reactome_matrix, [\"OncotreePrimaryDisease\", \"ModelID\"]\n", ")\n", "pca_latent_corum_df = compute_pca(corum_matrix, [\"OncotreePrimaryDisease\", \"ModelID\"])\n", "pca_latent_drug_df = compute_pca(drug_matrix, [\"OncotreePrimaryDisease\", \"ModelID\"])\n", "pca_latent_reactome_df.to_parquet(\n", " BASE_DIR / \"data\" / \"pca_embeddings_latent_reactome.parquet\"\n", ")\n", "pca_latent_corum_df.to_parquet(\n", " BASE_DIR / \"data\" / \"pca_embeddings_latent_corum.parquet\"\n", ")\n", "pca_latent_drug_df.to_parquet(BASE_DIR / \"data\" / \"pca_embeddings_latent_drug.parquet\")" ] }, { "cell_type": "code", "execution_count": null, "id": "dc8ccfe3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "26a895e4", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.13.1)", "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.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }