Skip to main content

hla-analysis-python (hapy)

A Python framework for HLA association analysis and fine-mapping in immunogenetic studies.

Overview

hapy is a Python package for downstream analysis of imputed and directly typed HLA data. The framework supports the analysis of classical HLA alleles, amino acid polymorphisms and SNP markers across the HLA region through a unified workflow interface.

Supported input formats include:

  • SNP2HLA output
  • Michigan Imputation Server HLA output
  • PLINK-derived genotype formats (both PLINK1/2)
  • Beagle-derived genotype formats
  • HLA allele calls and amino acid dosage data

Features

Association analysis

  • Amino acid omnibus association testing
  • Individual amino acid residue association testing
  • Classical HLA allele association analysis
  • SNP-level association analysis
  • Covariate-adjusted regression models
  • Conditional association analysis
  • Survival analysis
  • Gene–gene (G×G) and gene–environment (G×E) interaction analysis

Supported statistical models include:

  • Linear regression
  • Logistic regression
  • Cox proportional hazards regression

Conditional fine-mapping

  • Stepwise conditional analysis at the amino acid position and within-gene motif (haplotype) levels
  • LD-informed stepwise model selection
  • Multiallelic linkage disequilibrium (LD) analysis using canonical correlation analysis (CCA) and Cramér's V
  • Model comparison using information criteria and likelihood ratio tests

Quality control and targeted analysis

  • HLA gene and variant selection for association testing
  • Minor allele frequency (MAF) filtering

HLA allele mapping

  • HLA allele-to-amino-acid conversion using locally built IPD-IMGT/HLA reference panels
  • Within-gene amino acid motif construction
  • Support for high-resolution HLA allele calls

Meta-analysis

  • Weighted Stouffer's method for combining association P-values
  • Inverse-variance weighted meta-analysis of effect estimates

Visualisation and computational performance

  • Amino acid position-level Manhattan plots
  • Three-dimensional interaction visualisation
  • Parallelised association testing with thread- and process-based execution

Installation

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

conda create -n hapy python=3.11 # creates the conda environment
conda activate hapy # accesses the environment
pip install hla-hapy # installs 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.2

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.2
File Size Uploaded Provenance Actions
hla_hapy-1.0.2.tar.gz 176.7 kB Without provenance Details for hla_hapy-1.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 376.3 kB

Release files / hla_hapy-1.0.2.tar.gz

Download URL hla_hapy-1.0.2.tar.gz
Size 176.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a569cfcb087215ce9bd35d23ebdac6b0c345fa0592ab44128d29cbbcd7ecf42f
BLAKE2b-256 checksum
How to use checksums
7194f1b238be9c0318184082e8c6ef3ffa1fa5672f34b32567c76f4c952a6501
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.2-py3-none-any.whl

Download URL hla_hapy-1.0.2-py3-none-any.whl
Size 199.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
698f4715975daed2ded5b9cf4571dd2878c9b096bcce651c22e9dd5ca0053fa5
BLAKE2b-256 checksum
How to use checksums
d8118239272f56b25aba935ad7c9399d1707b9fb941498dcbbc45504c3760880
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.2

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