# hakyimlab/metaxcan architecture

> Architecture diagram of the GitHub repository hakyimlab/metaxcan, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hakyimlab/metaxcan
- Repository: https://github.com/hakyimlab/metaxcan

## Overview

1. **What this repository is**
   MetaXcan is a Python 3 command-line toolkit for transcriptome/TWAS-style association analysis. The repo is centered on running association methods over three kinds of input data: prediction model databases, genotype or GWAS data, and precomputed covariance/summary products.

2. **Top-level architectural split**
   The `software/` directory is the executable and library layer. The root `README.md`, `papers/`, and `DevNotes.Rmd` are documentation/support. `software/tests/` is a fairly extensive fixture-driven test suite with synthetic and GTEx-like data. `software/setup.py`, `requirements.txt`, and `conda_env.yaml` define packaging and runtime dependencies.

3. **Primary entrypoints**
   The main user-facing tools are `SPrediXcan.py`, `PrediXcan.py`, `MulTiXcan.py`, and `SMulTiXcan.py`. These are the core pipeline entrypoints: S-PrediXcan consumes GWAS summary statistics; PrediXcan consumes individual-level genotypes and phenotypes; MultiXcan/SMultiXcan aggregate across tissues or models. `MetaXcanUI.py` and the `metax/` GUI-related modules are ancillary.

4. **Core library boundary**
   Most computation lives under `software/metax/`. This package is the internal domain layer: it contains reusable code for data parsing, matrices, model handling, GWAS handling, genotype handling, and association algorithms. The scripts at the top of `software/` are thin orchestration wrappers around these library modules.

5. **Domain model and shared abstractions**
   The `metax/` package defines the main objects and helpers used across the pipeline: `DataSet`, `DataSetSNP`, `Person`, `Gene`, `PredictionModel`, `MatrixManager`, `KeyedDataSet`, and `Constants/Formats/NamingConventions`. These represent the repository’s internal contract for “who/what is being analyzed” and standardize how SNPs, genes, samples, and matrices are keyed and aligned.

6. **Input adapters by data type**
   Input handling is split by modality:
   - `metax/genotype/` reads individual-level genotype from BGEN, VCF, dosage, and GTEx-like formats.
   - `metax/gwas/` reads summary-statistic GWAS inputs and special-case formats.
   - `metax/expression/` reads expression matrices from plain text or HDF5.
   These adapters isolate file-format complexity from the association logic.

7. **Association computation layer**
   The central analysis code is under `metax/metaxcan/` and `metax/predixcan/`. `metaxcan/AssociationCalculation.py` performs the S-PrediXcan-style gene-level statistic from GWAS summary data plus model weights and SNP covariance. `predixcan/PrediXcanAssociation.py` and `predixcan/MultiPrediXcanAssociation.py` implement the individual-level PrediXcan and multi-tissue logic. This is the main “engine” boundary in the repo.

8. **Multi-tissue and cross-model analysis**
   `MulTiXcan.py` / `SMulTiXcan.py` depend on tissue-level predicted expression correlation structure and singular-value decomposition-style reduction. The supporting code in `metax/cross_model/JointAnalysis.py` and `metax/predixcan/MultiPrediXcanAssociation.py` combines correlated tissue predictions into a smaller number of independent components before association testing. HDF5 support in `metax/expression/HDF5Expression.py` exists specifically to make this cross-tissue access efficient.

9. **Preprocessing and model-building pipeline**
   Several scripts prepare the derived inputs needed by the association engines: `BuildExpressionProduct.py`, `CovarianceBuilder.py`, `ToHDF5.py`, `Predict.py`, and the `M00_`–`M04_` scripts. These generate or transform model databases, covariance matrices, betas, and z-scores. Conceptually, this is an offline preparation layer that feeds the runtime association tools.

10. **Data flow from inputs to results**
   The canonical flow is: prediction model database + SNP covariance + GWAS summary stats or genotype/phenotype data → library loaders/adapters → aligned SNP/gene matrices → association engine → tabular CSV/TSV results. For MultiXcan, an additional intermediate product is per-tissue predicted expression, often stored as HDF5 or text, which is then aggregated across tissues/models.

11. **External data and ecosystem dependencies**
   The repository assumes externally produced model and reference data from PredictDB / predictdb.org and often GTEx / 1000 Genomes-derived assets. Runtime dependencies include NumPy, SciPy, Pandas, HDF5, and for some genotype readers BGEN and VCF libraries (`bgen_reader`, `cyvcf2`). R is optional for some statistics/plots, but not the core computation path.

12. **Testing and fixture strategy**
   `software/tests/_td/` contains representative compressed fixture data for GWAS, covariance, db files, dosage, VCF, and GTEx-like expression. Tests cover both low-level abstractions and end-to-end behavior, which indicates the repo treats format parsing and mathematical correctness as first-class architectural concerns.

13. **Packaging and deployment model**
   The project is packaged as an installable Python distribution, with `setup.py`, `setup.cfg`, `MANIFEST.in`, and `__init__.py` files exposing the library and command-line scripts. The architecture is therefore primarily a local batch-computation toolkit, not a service application: users install it into a Python environment and run the scripts against local files.

Main entry points: [`software/metax`](https://github.com/hakyimlab/metaxcan/tree/master/software/metax) (metax), [`software`](https://github.com/hakyimlab/metaxcan/tree/master/software) (Software), [`software/metax/metaxcan/AssociationCalculation.py`](https://github.com/hakyimlab/metaxcan/blob/master/software/metax/metaxcan/AssociationCalculation.py) (S-PrediXcan engine), [`software/tests`](https://github.com/hakyimlab/metaxcan/tree/master/software/tests) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_docs["Docs & support"]
  node_readme["README<br/>docs<br/>[README.md]"]
  node_papers["Papers<br/>docs"]
end

subgraph group_runtime["Runtime toolkit"]
  node_software_pkg["Software<br/>distribution"]
  node_predixcan_cli["PrediXcan<br/>cli<br/>[PrediXcan.py]"]
  node_spredixcan_cli["S-PrediXcan<br/>cli<br/>[SPrediXcan.py]"]
  node_multixcan_cli["MulTiXcan<br/>cli<br/>[MulTiXcan.py]"]
  node_smultixcan_cli["SMulTiXcan<br/>cli<br/>[SMulTiXcan.py]"]
  node_predict_cli["Predict<br/>prep cli<br/>[Predict.py]"]
  node_cov_builder["Covariance<br/>prep cli"]
  node_expr_builder["Expr Product<br/>prep cli"]
  node_to_hdf5["To HDF5<br/>prep cli<br/>[ToHDF5.py]"]
  node_core["metax"]
  node_domain["Domain model<br/>abstractions<br/>[DataSet.py]"]
  node_genotype["Genotype I/O<br/>adapter"]
  node_gwas["GWAS I/O<br/>adapter"]
  node_expression["Expression I/O<br/>adapter"]
  node_metaxcan_engine["S-PrediXcan engine<br/>association engine"]
  node_predixcan_engine["PrediXcan engine<br/>association engine"]
  node_cross_model["Cross-model analysis<br/>reduction<br/>[JointAnalysis.py]"]
  node_packaging["Packaging<br/>build<br/>[setup.py]"]
end

subgraph group_tests["Tests & fixtures"]
  node_tests_root["Tests"]
  node_fixtures[("Fixtures<br/>test data")]
end

node_readme -->|"describes"| node_software_pkg
node_papers -->|"supports"| node_software_pkg
node_software_pkg -->|"exposes"| node_predixcan_cli
node_software_pkg -->|"exposes"| node_spredixcan_cli
node_software_pkg -->|"exposes"| node_multixcan_cli
node_software_pkg -->|"exposes"| node_smultixcan_cli
node_software_pkg -->|"exposes"| node_predict_cli
node_software_pkg -->|"exposes"| node_cov_builder
node_software_pkg -->|"exposes"| node_expr_builder
node_software_pkg -->|"exposes"| node_to_hdf5
node_predixcan_cli -->|"uses"| node_core
node_spredixcan_cli -->|"uses"| node_core
node_multixcan_cli -->|"uses"| node_core
node_smultixcan_cli -->|"uses"| node_core
node_core -->|"defines"| node_domain
node_core -->|"routes"| node_genotype
node_core -->|"routes"| node_gwas
node_core -->|"routes"| node_expression
node_core -->|"contains"| node_metaxcan_engine
node_core -->|"contains"| node_predixcan_engine
node_core -->|"contains"| node_cross_model
node_multixcan_cli -->|"depends on"| node_cross_model
node_smultixcan_cli -->|"depends on"| node_metaxcan_engine
node_spredixcan_cli -->|"depends on"| node_metaxcan_engine
node_predixcan_cli -->|"depends on"| node_predixcan_engine
node_predict_cli -->|"prepares"| node_expression
node_cov_builder -->|"feeds"| node_metaxcan_engine
node_expr_builder -->|"feeds"| node_expression
node_to_hdf5 -->|"feeds"| node_expression
node_tests_root -->|"uses"| node_fixtures
node_tests_root -->|"verifies"| node_core
node_tests_root -->|"verifies"| node_metaxcan_engine
node_tests_root -->|"verifies"| node_predixcan_engine

click node_readme "https://github.com/hakyimlab/metaxcan/blob/master/README.md"
click node_papers "https://github.com/hakyimlab/metaxcan/tree/master/papers"
click node_software_pkg "https://github.com/hakyimlab/metaxcan/tree/master/software"
click node_predixcan_cli "https://github.com/hakyimlab/metaxcan/blob/master/software/PrediXcan.py"
click node_spredixcan_cli "https://github.com/hakyimlab/metaxcan/blob/master/software/SPrediXcan.py"
click node_multixcan_cli "https://github.com/hakyimlab/metaxcan/blob/master/software/MulTiXcan.py"
click node_smultixcan_cli "https://github.com/hakyimlab/metaxcan/blob/master/software/SMulTiXcan.py"
click node_predict_cli "https://github.com/hakyimlab/metaxcan/blob/master/software/Predict.py"
click node_cov_builder "https://github.com/hakyimlab/metaxcan/blob/master/software/CovarianceBuilder.py"
click node_expr_builder "https://github.com/hakyimlab/metaxcan/blob/master/software/BuildExpressionProduct.py"
click node_to_hdf5 "https://github.com/hakyimlab/metaxcan/blob/master/software/ToHDF5.py"
click node_core "https://github.com/hakyimlab/metaxcan/tree/master/software/metax"
click node_domain "https://github.com/hakyimlab/metaxcan/blob/master/software/metax/DataSet.py"
click node_genotype "https://github.com/hakyimlab/metaxcan/tree/master/software/metax/genotype"
click node_gwas "https://github.com/hakyimlab/metaxcan/tree/master/software/metax/gwas"
click node_expression "https://github.com/hakyimlab/metaxcan/tree/master/software/metax/expression"
click node_metaxcan_engine "https://github.com/hakyimlab/metaxcan/blob/master/software/metax/metaxcan/AssociationCalculation.py"
click node_predixcan_engine "https://github.com/hakyimlab/metaxcan/blob/master/software/metax/predixcan/PrediXcanAssociation.py"
click node_cross_model "https://github.com/hakyimlab/metaxcan/blob/master/software/metax/cross_model/JointAnalysis.py"
click node_packaging "https://github.com/hakyimlab/metaxcan/blob/master/software/setup.py"
click node_tests_root "https://github.com/hakyimlab/metaxcan/tree/master/software/tests"
click node_fixtures "https://github.com/hakyimlab/metaxcan/tree/master/software/tests/_td"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_readme,node_papers toneBlue
class node_software_pkg,node_predixcan_cli,node_spredixcan_cli,node_multixcan_cli,node_smultixcan_cli,node_predict_cli,node_cov_builder,node_expr_builder,node_to_hdf5,node_core,node_domain,node_genotype,node_gwas,node_expression,node_metaxcan_engine,node_predixcan_engine,node_cross_model,node_packaging toneAmber
class node_tests_root,node_fixtures toneMint
```

## Components

### Docs & support

Repository documentation and packaging support.

- [README](https://github.com/hakyimlab/metaxcan/blob/master/README.md): docs · Project overview and usage notes. · `README.md`
- [Papers](https://github.com/hakyimlab/metaxcan/tree/master/papers): docs · Method papers and references for the toolkit. · `papers`

### Runtime toolkit

Executable scripts and the shared Python library used to run analyses.

- [Software](https://github.com/hakyimlab/metaxcan/tree/master/software): distribution · Installable Python package containing CLI tools and library code. · `software`
- [PrediXcan](https://github.com/hakyimlab/metaxcan/blob/master/software/PrediXcan.py): cli · Individual-level genotype/phenotype association entrypoint. · `software/PrediXcan.py`
- [S-PrediXcan](https://github.com/hakyimlab/metaxcan/blob/master/software/SPrediXcan.py): cli · Summary-statistic association entrypoint. · `software/SPrediXcan.py`
- [MulTiXcan](https://github.com/hakyimlab/metaxcan/blob/master/software/MulTiXcan.py): cli · Multi-tissue aggregation entrypoint. · `software/MulTiXcan.py`
- [SMulTiXcan](https://github.com/hakyimlab/metaxcan/blob/master/software/SMulTiXcan.py): cli · Summary-statistic multi-tissue entrypoint. · `software/SMulTiXcan.py`
- [Predict](https://github.com/hakyimlab/metaxcan/blob/master/software/Predict.py): prep cli · Offline prediction and model-transform utility. · `software/Predict.py`
- [Covariance](https://github.com/hakyimlab/metaxcan/blob/master/software/CovarianceBuilder.py): prep cli · Builds SNP covariance and related derived products. · `software/CovarianceBuilder.py`
- [Expr Product](https://github.com/hakyimlab/metaxcan/blob/master/software/BuildExpressionProduct.py): prep cli · Builds expression products for downstream analysis. · `software/BuildExpressionProduct.py`
- [To HDF5](https://github.com/hakyimlab/metaxcan/blob/master/software/ToHDF5.py): prep cli · Converts expression and related data into HDF5. · `software/ToHDF5.py`
- [metax](https://github.com/hakyimlab/metaxcan/tree/master/software/metax): library · Shared domain objects, constants, utilities, and orchestration support. · `software/metax`
- [Domain model](https://github.com/hakyimlab/metaxcan/blob/master/software/metax/DataSet.py): abstractions · Datasets, samples, genes, models, and keyed alignment primitives. · `software/metax/DataSet.py`
- [Genotype I/O](https://github.com/hakyimlab/metaxcan/tree/master/software/metax/genotype): adapter · Readers and helpers for BGEN, VCF, dosage, and GTEx-like genotype data. · `software/metax/genotype`
- [GWAS I/O](https://github.com/hakyimlab/metaxcan/tree/master/software/metax/gwas): adapter · Summary-statistic parsers and special-case GWAS handling. · `software/metax/gwas`
- [Expression I/O](https://github.com/hakyimlab/metaxcan/tree/master/software/metax/expression): adapter · Plain-text and HDF5 expression readers. · `software/metax/expression`
- [S-PrediXcan engine](https://github.com/hakyimlab/metaxcan/blob/master/software/metax/metaxcan/AssociationCalculation.py): association engine · Gene-level statistics from GWAS summary data plus model weights and covariance. · `software/metax/metaxcan/AssociationCalculation.py`
- [PrediXcan engine](https://github.com/hakyimlab/metaxcan/blob/master/software/metax/predixcan/PrediXcanAssociation.py): association engine · Individual-level association and multi-prediction testing. · `software/metax/predixcan/PrediXcanAssociation.py`
- [Cross-model analysis](https://github.com/hakyimlab/metaxcan/blob/master/software/metax/cross_model/JointAnalysis.py): reduction · Combines correlated tissue/model predictions into independent components. · `software/metax/cross_model/JointAnalysis.py`
- [Packaging](https://github.com/hakyimlab/metaxcan/blob/master/software/setup.py): build · setup.py, requirements, and environment definitions for installation. · `software/setup.py`

### Tests & fixtures

Fixture-driven tests and representative input data.

- [Tests](https://github.com/hakyimlab/metaxcan/tree/master/software/tests): tests · Unit and integration tests for loaders, data structures, and association math. · `software/tests`
- [Fixtures](https://github.com/hakyimlab/metaxcan/tree/master/software/tests/_td): test data · Representative compressed inputs for GWAS, covariance, genotype, and expression. · `software/tests/_td`

## Connections

- README → Software: describes · Documentation explains how to install and run the toolkit.
- Papers → Software: supports · Method papers document the analysis workflows.
- Software → PrediXcan: exposes · Package installs the main CLI entrypoints.
- Software → S-PrediXcan: exposes · Package installs the main CLI entrypoints.
- Software → MulTiXcan: exposes · Package installs the main CLI entrypoints.
- Software → SMulTiXcan: exposes · Package installs the main CLI entrypoints.
- Software → Predict: exposes · Package installs preprocessing utilities.
- Software → Covariance: exposes · Package installs preprocessing utilities.
- Software → Expr Product: exposes · Package installs preprocessing utilities.
- Software → To HDF5: exposes · Package installs preprocessing utilities.
- PrediXcan → metax: uses · CLI orchestration delegates to the shared library.
- S-PrediXcan → metax: uses · CLI orchestration delegates to the shared library.
- MulTiXcan → metax: uses · CLI orchestration delegates to the shared library.
- SMulTiXcan → metax: uses · CLI orchestration delegates to the shared library.
- metax → Domain model: defines · Shared objects and alignment abstractions.
- metax → Genotype I/O: routes · Core library coordinates genotype loading.
- metax → GWAS I/O: routes · Core library coordinates GWAS loading.
- metax → Expression I/O: routes · Core library coordinates expression loading.
- metax → S-PrediXcan engine: contains · Association calculation lives in the metaxcan engine.
- metax → PrediXcan engine: contains · Individual-level association lives in the predixcan engine.
- metax → Cross-model analysis: contains · Cross-tissue/model reduction and aggregation logic.
- MulTiXcan → Cross-model analysis: depends on · Multi-tissue workflows require component reduction.
- SMulTiXcan → S-PrediXcan engine: depends on · Summary-statistic multi-tissue workflow builds on gene-level association.
- S-PrediXcan → S-PrediXcan engine: depends on · Summary-statistic workflow calls the S-PrediXcan engine.
- PrediXcan → PrediXcan engine: depends on · Individual-level workflow calls the PrediXcan engine.
- Predict → Expression I/O: prepares · Transforms outputs into expression products.
- Covariance → S-PrediXcan engine: feeds · Produces covariance inputs needed by association calculation.
- Expr Product → Expression I/O: feeds · Creates intermediate expression products for downstream tools.
- To HDF5 → Expression I/O: feeds · Builds HDF5-backed expression access for efficient reuse.
- Tests → Fixtures: uses · Tests are driven by representative compressed fixtures.
- Tests → metax: verifies · Tests cover the shared data model and utilities.
- Tests → S-PrediXcan engine: verifies · Tests cover association math and end-to-end behavior.
- Tests → PrediXcan engine: verifies · Tests cover individual-level association behavior.
