--- title: PGx Population Pharmacogenomics Platform emoji: 🧬 colorFrom: blue colorTo: indigo sdk: streamlit sdk_version: 1.41.1 app_file: app.py pinned: false --- # 🧬 Automated Population Pharmacogenomics Analysis Platform An industry-level, production-grade automated platform designed for genotype, allele frequency, Hardy–Weinberg Equilibrium (HWE), regional/gender demographic stratification, and CYP2C19 star allele / diplotype / CPIC phenotype classification. --- ## 🎯 1. Platform Aim & What the Tool Does Exactly Currently, population pharmacogenomic analysis involves studying the distribution of pharmacogenetic variants across diverse geographic and demographic cohorts. Manual spreadsheet calculations for genotype counts, allele frequencies, Hardy–Weinberg Equilibrium (HWE), demographic subgrouping, and diplotype assignment are time-consuming, prone to human error, and unscalable for growing patient cohorts. **This platform automates the complete end-to-end analytical workflow:** - **Automated Data Validation & QC**: Parses `.xlsx`, `.xls`, and `.csv` files, standardizing raw column headers, detecting duplicate Sample IDs, and quarantining invalid allele calls. - **Genetic & Statistical Engine**: Instantly computes genotype frequencies, allele frequencies ($p$ and $q$), observed vs. expected counts, Chi-Square goodness-of-fit statistics ($\chi^2$), P-values, and Haldane exact test fallbacks. - **Demographic & Regional Stratification**: Categorizes samples across 5 Indian geographic regions (**South India, North India, East India, West India, Central India**), 27+ individual state profiles, and Male vs. Female subgroups. - **CYP2C19 Star Allele & Phenotype Classifier**: Translates *2 (rs4244285), *3 (rs4986893), and *17 (rs12248560) variants into diplotypes (`*1/*1`, `*1/*2`, `*1/*17`, `*2/*17`, `*2/*2`, `*17/*17`) and CPIC Clinical Metabolizer Phenotypes (**Ultrarapid, Rapid, Normal, Intermediate, Poor Metabolizers**). - **Multi-Format Exporters**: Generates publication-ready multi-tab Excel (`.xlsx`), summary CSV (`.csv`), and formal PDF (`.pdf`) summary reports with one click. --- ## ⚑ 2. Automated End-to-End Pipeline Architecture The platform executes an 11-stage automated pipeline upon dataset upload: ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Excel / CSV File Uploadβ”‚ (.xlsx, .xls, .csv) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Data Validation & QC β”‚ (Header normalization, duplicate detection, invalid allele quarantine) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Genotype Normalization β”‚ (Genotype string cleaning: G/A -> GA, T/C -> CT) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Genotype Frequencies β”‚ (N_AA, N_Aa, N_aa counts, frequencies, percentages) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Allele Frequencies β”‚ (Ref/Var allele counts, p and q frequencies) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Hardy-Weinberg Engine β”‚ (Observed vs Expected, Chi-Square, P-value, Haldane Exact Test) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Population Grouping β”‚ (South, North, East, West, Central India & Gender Subgroups) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Star Allele Calling β”‚ (CYP2C19 *2, *3, *17 variant combination mapping) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Diplotype Assignment β”‚ (*1/*1, *1/*2, *1/*17, *2/*17, *2/*2, *17/*17) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Phenotype Calling β”‚ (CPIC classification: UM, RM, NM, IM, PM) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Interactive Dashboard β”‚ (9-Tab Streamlit UI, Plotly charts, HTML warning badges) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Multi-Format Exporters β”‚ (Excel .xlsx, CSV .csv, PDF .pdf reports) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ’Ύ 3. Database Architecture & Automated Batch Updates ### 3.1 Why SQLite Was Chosen The platform utilizes **SQLite** (`database/db_manager.py` $\rightarrow$ `pharmacogenomics.db`) as its core persistence backend. - **Zero Configuration Overhead**: Lightweight, serverless embedded database requiring zero database administration or external daemon processes. - **ACID Compliance & Reliability**: Ensures atomic transactions, preventing database corruption during batch sample writes. - **Local Zero-Latency Access**: Disk-backed persistence allows instant query execution and in-memory caching. - **High Performance Scaling**: Optimized to handle initial cohorts of **1,000 samples**, scaling seamlessly to **1,200**, **2,000**, and **10,000+ samples**. ### 3.2 How Data Updates & Recalculate Automatically 1. **Deduplication Engine**: Every sample is assigned a primary key (`Sample_ID`). When a new sample batch is ingested, the engine verifies `Sample_ID` against historical records. 2. **Incremental Batch Appending**: New unique patient records are appended to the `samples` database table. 3. **Automatic Recalculation**: Upon inserting new records, the analytical engine automatically re-triggers statistical pipelines across the combined dataset, updating all overall, regional, gender, and diplotype/phenotype distributions in real time. --- ## πŸ”¬ 4. Analytical Methodology & Formulas ### 4.1 Allele & Genotype Frequencies For a locus with Reference allele $A$ and Variant allele $a$, with valid sample size $N$: - **Genotype Frequencies**: $$f(AA) = \frac{N_{AA}}{N}, \quad f(Aa) = \frac{N_{Aa}}{N}, \quad f(aa) = \frac{N_{aa}}{N}$$ - **Allele Frequencies**: $$p = f(A) = \frac{2 N_{AA} + N_{Aa}}{2 N}, \quad q = f(a) = \frac{2 N_{aa} + N_{Aa}}{2 N} = 1 - p$$ ### 4.2 Hardy–Weinberg Equilibrium (HWE) & Chi-Square Test - **Expected Genotype Counts**: $$E_{AA} = N \cdot p^2, \quad E_{Aa} = N \cdot 2pq, \quad E_{aa} = N \cdot q^2$$ - **Chi-Square Goodness-of-Fit Statistic ($\chi^2$, $df = 1$)**: $$\chi^2 = \frac{(N_{AA} - E_{AA})^2}{E_{AA}} + \frac{(N_{Aa} - E_{Aa})^2}{E_{Aa}} + \frac{(N_{aa} - E_{aa})^2}{E_{aa}}$$ - **P-value Calculation**: $$\text{P-value} = 1 - \text{CDF}_{\chi^2_1}(\chi^2)$$ - **Critical Threshold**: $\chi^2 \ge 3.841$ ($\alpha = 0.05$). Values $\ge 3.841$ indicate deviation from HWE and are highlighted with a **bright red warning badge (`⚠️ ChiΒ² β‰₯ 3.841`)**. - **Haldane Exact Test**: Automatically triggered when any expected count $E < 5$ to prevent Chi-square small-sample bias. ### 4.3 3-Tier Demographic Gazetteer Engine Samples are assigned to 5 geographic regions using a 3-tier lookup priority: 1. **South India**: Tamil Nadu, Kerala, Karnataka, Andhra Pradesh, Telangana, Puducherry, Lakshadweep. 2. **North India**: Delhi, Punjab, Haryana, Himachal Pradesh, Jammu & Kashmir, Ladakh, Uttarakhand, Uttar Pradesh. 3. **East India**: West Bengal, Odisha, Bihar, Jharkhand, Assam, Sikkim, Arunachal Pradesh, Manipur, Meghalaya, Mizoram, Nagaland, Tripura, Andaman & Nicobar. 4. **West India**: Maharashtra, Gujarat, Goa, Rajasthan, Dadra & Nagar Haveli, Daman & Diu. 5. **Central India**: Madhya Pradesh, Chhattisgarh. ### 4.4 CPIC CYP2C19 Metabolizer Phenotype Translation Matrix | Diplotype Call | CPIC Metabolizer Phenotype | Activity Score | Functional Description | | :--- | :--- | :---: | :--- | | **`*17/*17`** | **Ultrarapid Metabolizer (UM)** | $> 2.0$ | Increased enzyme activity (Hom. Gain of Function) | | **`*1/*17`** | **Rapid Metabolizer (RM)** | $1.5$ | Increased enzyme activity (Het. Gain of Function) | | **`*1/*1`** | **Normal Metabolizer (NM)** | $2.0$ | Normal enzyme activity (Wildtype) | | **`*1/*2`, `*1/*3`, `*2/*17`, `*3/*17`** | **Intermediate Metabolizer (IM)** | $0.5 - 1.0$ | Reduced enzyme activity | | **`*2/*2`, `*2/*3`, `*3/*3`** | **Poor Metabolizer (PM)** | $0.0$ | No enzyme activity (Hom. Loss of Function) | --- ## πŸ“ 5. Project Directory Structure ``` analysis tools/ β”œβ”€β”€ app.py # 9-Tab Streamlit Web Dashboard UI & Workflow Renderer β”œβ”€β”€ config.py # Global SNP configurations, Gazetteer rules & CPIC matrix β”œβ”€β”€ core/ # Core Analytical & Statistical Engine Package β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ qc.py # Data Quality Control, Validation & Cleaning Engine β”‚ β”œβ”€β”€ stats.py # Genotype, Allele Frequency & HWE Engine β”‚ β”œβ”€β”€ stratification.py # Demographic & Geographic Stratification Engine β”‚ └── cyp2c19.py # CYP2C19 Star Allele, Diplotype & Phenotype Classifier β”œβ”€β”€ database/ # Data Persistence Layer β”‚ β”œβ”€β”€ __init__.py β”‚ └── db_manager.py # SQLite database manager for batch sample growth (1k -> 10k+) β”œβ”€β”€ reports/ # Exporter & Reporting Layer β”‚ β”œβ”€β”€ __init__.py β”‚ └── report_generator.py # Multi-Tab Excel (.xlsx), CSV, and PDF Report Exporter β”œβ”€β”€ tests/ # Automated Unit Test Suite (PyTest) β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ test_qc.py β”‚ β”œβ”€β”€ test_stats.py β”‚ β”œβ”€β”€ test_stratification.py β”‚ β”œβ”€β”€ test_cyp2c19.py β”‚ β”œβ”€β”€ test_db_manager.py β”‚ └── test_report_generator.py β”œβ”€β”€ .streamlit/ # Streamlit UI server and theme configuration β”‚ └── config.toml β”œβ”€β”€ requirements.txt # Python dependencies (Pandas, SciPy, Plotly, ReportLab, etc.) └── README.md # Full platform documentation & methodology guide ``` --- ## πŸš€ 6. How to Run & Test ### 6.1 Execute Web Application Dashboard ```bash python3 -m streamlit run app.py ``` *Access the web dashboard in your browser at `http://localhost:8501`.* ### 6.2 Execute Automated Unit Test Suite ```bash python3 -m pytest tests/ ``` *Executes all 8 automated tests for QC, statistics, HWE Chi-Square, diplotype calling, SQLite database, and report exporters (`100% pass rate`).* --- ## πŸ“œ 7. License & Compliance Designed for research and population pharmacogenomics analysis in compliance with CPIC guidelines.