Skip to main content

hla-analysis-python (hapy)

A Python framework for HLA allele, amino-acid, SNP, interaction, survival, and meta-analysis workflows in immunogenetic association studies.

Overview

hapy is a Python package for downstream analysis of HLA imputation outputs from genetic association studies. The framework supports analysis of classical HLA alleles, amino-acid polymorphisms, and SNP markers across the HLA region using a unified workflow interface.

Currently supported input formats include:

  • SNP2HLA output
  • Michigan Imputation Server HLA output
  • PLINK-derived genotype formats
  • Beagle-derived genotype format

Intended use

hapy is designed for:

  • HLA fine-mapping studies
  • amino-acid association analysis
  • autoimmune disease genetics
  • immunogenetics workflows
  • multi-cohort HLA association studies

Features

  • HLA allele-to-amino-acid conversion with locally built IPD references
  • Amino-acid omnibus association testing
  • Classical HLA allele association analysis
  • SNP-level association analysis
  • Covariate-adjusted regression models
  • HLA variant conditional analysis
  • Gene/Variant filtering
  • MAF filtering
  • Interaction analysis
  • Survival analysis
  • Cohort-level meta-analysis using Stouffer’s method and inverse-variance weighted method
  • Parallelised association testing workflows

Supported models include:

  • Linear regression
  • Logistic regression
  • Cox proportional hazards models

Installation

Clone the repository and install locally:

It is recommended to install hapy within a clean conda environment.

conda create -n hapy python=3.11
conda activate hapy
pip install .

Alternatively install via PyPI

pip install hla-hapy

Quick Start

For imputed data, PGEN is preferred when fractional dosages are stored in the file. The BED/BIM/FAM (bfile) format stores hard genotype calls rather than fractional dosages. RAW input preserves the dosage values exported into it, but with reduced numerical precision due to fewer decimal places.

Imputation-quality scores must be retained separately, for example in a PVAR column or INFO field, or an external R² file. A PVAR file does not necessarily contain quality scores, and supplying one alone does not enable quality filtering. Request filtering with pvar_R2_column or filter_R2, together with R2_minimum; see the association guide for examples. RAW can also be read without PVAR, but imputation-quality filtering requires a suitable quality source.

To read PLINK files, use read_plink and choose a PLINK input format:

import hapy as hp

# .raw (exported by PLINK with `--export A`) 
hladat = hp.read_plink("cohort.raw", format="raw", pvarfileloc="cohort.pvar")

# Binary PLINK 2 fileset: cohort.pgen, cohort.pvar, cohort.psam.
hladat = hp.read_plink("cohort", format="pgen")

# Binary PLINK 1 fileset: cohort.bed, cohort.bim, cohort.fam.
hladat = hp.read_plink("cohort", format="bfile")

The corresponding companion files for pgen and bilfes must share that prefix (e.g. pgen, pvar and psam, or bed, bim, fam) just like PLINK.

SNP markers starting with SNP_ or SNPS_ are stored in hladat.SNP.extradata, with their metadata in hladat.SNP.extrainfo. Other SNPs are stored in hladat.SNP.data and hladat.SNP.info. The two genotype tables do not overlap.

Beagle input

import hapy as hp

# Load SNP2HLA or Michigan HLA imputation output in Beagle format
hladat = hp.read_bgl(
    fileloc="example.bgl.phased",
    load_types=("HLA","AA"), # loads on HLA allele and Amino Acid data
    filter_R2="example.bgl.r2", R2_minimum=0.9) # input imputation confidence and filter

Accesing data

# reading amino acid dosage/count data
hladat.AA.data # returns Amino Acid variant by sample matrix in pandas DataFrame format

hladat.HLA.data # returns HLA allele variant by sample matrix in pandas DataFrame format

hladat.SNP.data # returns SNP variant by sample matrix in pandas DataFrame format

hladat.SNP.extradata # returns SNP (that has SNP_ or SNPS_ prefix.) variant by sample matrix in pandas DataFrame format

HLA allele calls

Convert HLA allele calls into amino acid dosages or HLAdata objects, while preserving within-gene phasing for amino acid motif analysis. See allele mapping and reference building for reference preparation, genomic coordinate mapping and usage instructions.

Running association analyses

See the association guide for more details and bundled example to have an idea what the data formats should look like.

# load pheno and covariate file with pandas (be sure the index have same ID, these are sample IDs )
pheno = pd.read_csv("example_pheno.txt",index_col=1, ) # Binary PLINK-style {1,2} is auto-recoded to {0,1}.
covar = pd.read_csv("example_covar.txt", sep="\s+", index_col=1, )


# Run amino-acid omnibus association analysis for binary phenotype, with 8 threads, 
config = hp.stats.StandardConfig(model_type="logit", n_jobs=8, backend="thread",)
results = hp.stats.analyse(
    hladat=hladat,
    y=pheno[2], # if using y, it is a pandas Series format
    config=config,
    kind="AA", 
    covar=covar,
    verbose=True,
    condition_on="AA_DRB1_11_32552129", # condition on a VARIANT of choice (optional)
    use_progress_bar=False,
    print_every=10,
)
print(results.head())

Meta-analysis

inv, stouf = hp.meta.meta_analyse("example_studies.txt", data_type="AA")
# `example_studies.txt` refers to a txt file listing path to all the results that will be meta-analysed, this return two separate results where 

example_studies.txtrefers to a text file listing the paths to all association results to be meta-analysed. The function returns inv for inverse-variance results and stouf for weighted Stouffer results. For AA analysis, any position labelled MULTI in a cohort sends all available SINGLE/MULTI P-values for that position to Stouffer; all-SINGLE positions use IVW. OVR rows are excluded. Input column names are configurable; see meta-analysis.

HLA amino acid position plots

Use hp.vis.plot_aa_association() for AA position association plots and hp.vis.plot_aa_interaction() with kind="scatter3d" for position-level genetic (G×G) interactions. Both functions default to numeric amino acid sequence positions within one HLA gene; select it with, for example, genes="DRB1". Use x_axis="genomic" for physical genomic spacing or x_axis="equally_spaced" for evenly spaced positions across genes. See visualisation documentation and synthetic examples.

Status

hapy is ready to use for HLA amino acid fine mapping. If there are features you wish to see on the package please reach out.

Contact

For questions, feedback, or collaboration enquiries:

Chuan Fu Yap

University of Manchester

chuanfu.yap@manchester.ac.uk

Citation

If you use HLA Analysis Py in your research, please cite this repository. If a corresponding publication becomes available, please cite the publication instead.

Metadata

Release files for hla-hapy 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hla-hapy 1.0.0
File Size Uploaded Provenance Actions
hla_hapy-1.0.0.tar.gz 169.6 kB Without provenance Details for hla_hapy-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hla-hapy 1.0.0
File Interpreter ABI Platform Actions
hla_hapy-1.0.0-py3-none-any.whl Python 3 none any Details for hla_hapy-1.0.0-py3-none-any.whl

Total release size: 362.1 kB

Release files / hla_hapy-1.0.0.tar.gz

Download URL hla_hapy-1.0.0.tar.gz
Size 169.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ed0fa4ef068b9c4024427b3c11f8381ad172675e4c00626c176911202c3071fd
BLAKE2b-256 checksum
How to use checksums
0f69f90ddd1e32e9b5b9f1c4108eaeee58422bc81447704dda9a6208aebc9ab6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.13

Release files / hla_hapy-1.0.0-py3-none-any.whl

Download URL hla_hapy-1.0.0-py3-none-any.whl
Size 192.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35eea57d796ed5c87c3f9de3f2804b2fa4b74df89c4cca268c3722ddb0669b35
BLAKE2b-256 checksum
How to use checksums
189f63ec4db92da0cf1a08d153fb6fa434f265e7b92153fa0febfb2053bb4524
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.13

Security for hla-hapy 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page