Skip to main content

A lightweight, dependency-free GTF parser returning a structured Gene/Transcript/Interval object model.

Project description

gtf

A lightweight, dependency-free Python library for parsing GTF (Gene Transfer Format) files into a structured object model.

Why gtf?

Most GTF parsing libraries are either too heavy (requiring pandas, SQLite, or large C extensions) or return flat data structures that don't reflect the natural hierarchy of genomic annotation. gtf gives you a clean Gene → Transcript → features object model with no dependencies beyond the Python standard library.

Curently working on direct acces indexing by name and position.

Installation

pip install gtf

Quick start

import gtf

genes = gtf.parse_gtf("Homo_sapiens.GRCh38.gtf")

gene = genes["ENSG00000139618"]
print(gene.symbol, gene.start, gene.end)

for transcript_id, transcript in gene.transcript.items():
    exons = transcript.features.get("exon", [])
    introns = gtf.get_intron(exons)
    print(f"  {transcript_id}: {len(exons)} exons, {len(introns)} introns")

Coordinate convention

All coordinates are 0-based half-open (start inclusive, end exclusive), consistent with BED and BAM format. GTF files are 1-based inclusive; the conversion is applied automatically during parsing.

API

Parsing

gtf.parse_gtf(gtf_file, primary_key="gene_id")

Parse a GTF file into a dictionary of Gene objects.

genes = gtf.parse_gtf("annotation.gtf")
genes = gtf.parse_gtf("annotation.gtf", primary_key="gene_name")
  • gtf_file — path to the GTF file
  • primary_key — attribute used to key the returned dictionary (default: "gene_id")
  • Returnsdict[str, Gene]

gtf.get_attr(string)

Parse a raw GTF attribute string into a key-value dictionary. Useful when processing GTF lines outside of the full parser.

attrs = gtf.get_attr('gene_id "ENSG00000139618"; gene_name "BRCA2";')
# {"gene_id": "ENSG00000139618", "gene_name": "BRCA2"}

Derived features

gtf.get_intron(exons)

Derive intron intervals from a list of exon Interval objects belonging to a single transcript. Introns are numbered biologically: from 1 upward on the + strand, and from n down to 1 on the - strand.

exons = transcript.features.get("exon", [])
introns = gtf.get_intron(exons)
  • exons — list of Interval, need not be pre-sorted
  • Returnslist[Interval], empty if fewer than two exons are provided

Data classes

Interval

Immutable genomic coordinate record. All Interval objects are frozen — they are replaced rather than mutated when coordinates need updating.

Attribute Type Description
chr str Chromosome or sequence name
start int 0-based start (inclusive)
end int 0-based end (exclusive)
strand str "+", "-", or "." for unstranded
phase int or str Reading frame (0, 1, 2), or "." if not applicable
attribute dict Key-value pairs from the GTF attribute column

Transcript

Groups all GTF records sharing a transcript_id. The genomic span always reflects the union of all features seen so far.

Attribute Type Description
transcript_id str Ensembl transcript ID or equivalent
transcript_symbol str or None Human-readable transcript symbol
interval Interval Current genomic span of the transcript
features dict[str, list[Interval]] Feature type → list of intervals

Convenience properties: start, end, phase, attribute.

Gene

A gene locus containing one or more transcript isoforms.

Attribute Type Description
gene_id str Primary identifier (e.g. Ensembl gene ID)
symbol str Gene symbol, resolved from gene_symbol, gene_name, or gene_id
interval Interval Genomic span from the GTF gene record
transcript dict[str, Transcript] Transcript ID → Transcript

Convenience properties: start, end, phase, attribute, biotype, has_transcript.

Logging

Progress and error messages are emitted under the "gtf" logger. To suppress informational output:

import logging
logging.getLogger("gtf").setLevel(logging.WARNING)

License

CC-BY-4 Citation aknowledge : Romain Lannes 2026

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gtf_pyparser-0.2.0b1.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gtf_pyparser-0.2.0b1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file gtf_pyparser-0.2.0b1.tar.gz.

File metadata

  • Download URL: gtf_pyparser-0.2.0b1.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for gtf_pyparser-0.2.0b1.tar.gz
Algorithm Hash digest
SHA256 57635a815b772624b3bb5d96dfc950a2e86aa43501bb4742ab4134770ba796fb
MD5 224b07b6afaf30ba8a5bce1fb4473869
BLAKE2b-256 c47a5b4328cc3339eb7082c026083f20a970741b468265ed6337da2eae9d5c0d

See more details on using hashes here.

File details

Details for the file gtf_pyparser-0.2.0b1-py3-none-any.whl.

File metadata

  • Download URL: gtf_pyparser-0.2.0b1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for gtf_pyparser-0.2.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 206d1e281d369e9246ac73eaadf4b0637f6095cd44ff105082c2ed033feee9bb
MD5 840c82f09d81b0ed729caea0a270c248
BLAKE2b-256 b9f7be1ed8679066605731f1e11c1827bb978da1e190b991ff1e939787785213

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page