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_pyparser

Quick start

import gtf_pyparser

genes = gtf_pyparser.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_pyparser.parse_gtf("annotation.gtf")
genes = gtf_pyparser.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_pyparser.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_pyparser.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_pyparser" logger. To suppress informational output:

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

License

MIT 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.0.0.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.0.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file gtf_pyparser-0.0.0.tar.gz.

File metadata

  • Download URL: gtf_pyparser-0.0.0.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.0.0.tar.gz
Algorithm Hash digest
SHA256 6e6e3d5b1cbbd15c3a1f57446e96511448f2f5140a33a55b1995c5c80b33621a
MD5 1f31c140998bbc7cd77a308da8350543
BLAKE2b-256 80d0da16dd0f8968dbfe5808d2f4f15c488063f5f877f8d7c742176ba6ad36b3

See more details on using hashes here.

File details

Details for the file gtf_pyparser-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: gtf_pyparser-0.0.0-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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8597a9e53352436345db3b9ca824c2a4522add48ee3145a9dd75e1b98ab84623
MD5 eca59ff52b75a0d276d56c5be511b943
BLAKE2b-256 b52be5a1bb5f43a02a19d6585aa386416d152c9af1c5dc1d6c7fa81f025e37d7

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