Skip to main content

glstring

A standards-conformant Python library for parsing, manipulating, validating, and serializing Genotype List (GL) Strings, implementing the GL String 1.1 grammar.

glstring is a shared, reusable implementation of the GL String grammar for HLA, KIR, and other immune-polymorphism applications. It is a project of the Society for Immune Polymorphism (SIP) and is intended to be used by DiplotypeForge, TissueTypeForge, HLA imputation-assessment tools, and any other application that needs a correct GL String parser. It does not depend on either Forge project.

Highlights

  • Correct operator precedence. A real recursive-descent parser builds an immutable AST strictly by the published precedence of the six GL String operators — never by the order operators happen to appear.
  • All six GL String 1.1 operators: / ~ + | ^ ? (including the ? gene-location ambiguity delimiter introduced in GL String 1.1).
  • Immutable, typed AST — one node type per operator, hashable and safe to share.
  • No fixed diploid assumption — variable gene content and copy number (as in KIR) are first-class.
  • Lossless — exact original-text round-tripping on request, distinct from structural serialization and from (explicit) canonicalization.
  • Precise errors — syntax errors carry the offending character position and a caret pointer.
  • Nomenclature-independent — no IPD-IMGT/HLA, KIR database, pyard, HFX, or internet access required. Syntax validity and allele existence are separate.
  • Zero runtime dependencies for the parser and AST. Python 3.12+.

Installation

uv pip install -e ".[dev]"   # from a checkout, with dev tools

Quick start

from glstring import GLString

gl = GLString.parse(
    "HLA-A*01:01+HLA-A*02:01^HLA-B*07:02+HLA-B*08:01"
)

print(gl.to_string())        # structural serialization
print(gl.is_valid)           # classmethod: GLString.is_valid(text)
print(gl.loci)               # ('HLA-A', 'HLA-B')
print(gl.is_ambiguous())     # False
print(gl.is_fully_phased())  # False (two unphased '^' blocks)

Command line

glstring validate "HLA-A*01:01+HLA-A*02:01"
glstring parse "HLA-A*01:01+HLA-A*02:01"
glstring canonicalize "HLA-A*02:01+HLA-A*01:01"

validate exits 0 for valid input, 1 for invalid (with a diagnostic on stderr). Pass - as the argument to read the GL String from stdin.

The six operators

Operator Precedence Meaning Node
? 0 Possible gene locations (1.1) PossibleLoci
^ 1 Unphased genes (loci) LocusBlockList
| 2 Possible genotypes GenotypeList
+ 3 Copies of genes Genotype
~ 4 Genes known to be in phase PhasedGenotype
/ 5 Possible alleles AlleleList

Delimiters are evaluated in numerical precedence order: ? is evaluated first (outermost / loosest binding) and / last (innermost / tightest binding). See GRAMMAR.md.

Three kinds of round-trip

These are different guarantees and are not conflated (see DESIGN.md):

  1. Lexical — gl.original() reproduces the source text exactly.
  2. Structural — parse(serialize(ast)) == ast.
  3. Semantic — two different ASTs can describe the same genetic states. glstring does not claim semantic equivalence from lexical or structural normalization alone. Canonicalization is restricted to a documented, provably safe subset.

Scope (milestone M1)

This release implements M1: grammar and object model — the parser, AST, serialization, safe canonicalization, a public API, and a CLI, with a comprehensive test suite. Compatibility checking, lazy enumeration, phase transformations, and semantic equivalence are planned for later milestones; see DEVELOPMENT_PLAN.md.

Documentation

Citing the standard

  • Milius RP, et al. Genotype List String: a grammar for describing HLA and KIR genotyping results in a text string. Tissue Antigens. 2013;82(2):106–112. doi:10.1111/tan.12150
  • Mack SJ, et al. Genotype List String 1.1: Extending the Genotype List String Grammar for Describing HLA and Killer-cell Immunoglobulin-like Receptor Genotypes. HLA. 2023;102(2):206–212. doi:10.1111/tan.15126

License

Apache License 2.0. See LICENSE. No source code was copied from any GPL/LGPL-licensed project; see DESIGN.md for provenance notes.

Metadata

Release files for glstring 0.1.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 glstring 0.1.0
File Size Uploaded Provenance Actions
glstring-0.1.0.tar.gz 52.2 kB With provenance Details for glstring-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for glstring 0.1.0
File Interpreter ABI Platform Actions
glstring-0.1.0-py3-none-any.whl Python 3 none any Details for glstring-0.1.0-py3-none-any.whl

Total release size: 79.0 kB

Release files / glstring-0.1.0.tar.gz

Download URL glstring-0.1.0.tar.gz
Size 52.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1c1ab0d624d769b214fdf51bbe0b839cafd56a3a36b9b0f12a414dc761959bf1
BLAKE2b-256 checksum
How to use checksums
1cc1a941885361e644b32a328e35e92236b494350ca3c9668c7341f659c1ad2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Release files / glstring-0.1.0-py3-none-any.whl

Download URL glstring-0.1.0-py3-none-any.whl
Size 26.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
768adf277c0f51598c8c124b8e135166f3aa24e4c8469ff1bd5406a3b49de117
BLAKE2b-256 checksum
How to use checksums
ca0c95021578ad8b4a3a57bc1a9738c4527aeeab74fb40bf689b081bdaf0c999
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Security for glstring 0.1.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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for glstring-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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