Skip to main content

🧬 KEGGtangled

PyPI version Python 3.8+ License: MIT

Tie all your KEGG data together. Untangle it your way.

keggtangled is a Python library that lets you download, cache, and interrogate the entire metabolic network of any organism in the KEGG database.
It builds a rich, in‑memory knowledge graph connecting genes → KOs → reactions → compounds, so you can answer biological questions with clean, Pythonic code.


📦 Installation

pip install keggtangled

keggtangled requires Python 3.8 or later and depends on biopython.
Optionally, install tqdm for progress bars during bulk downloads:

pip install tqdm

🚀 Quick start

import keggtangled as kt

# Create an organism – first run fetches and caches all KO‑gene links and reactions
mta = kt.Organism("mta", cache_dir="kegg_cache")

# Load a pathway (downloads + parses KGML, caches everything)
glycolysis = mta.load_pathway("mta00010")
print(glycolysis)
# Pathway(mta00010, 44 genes, 23 reactions)

# From gene → KOs → reactions → compounds
gene = "Moth_0033"
kos = mta.get_kos_for_gene(gene)            # frozenset of ko:K...
rxns = mta.get_reactions_for_gene(gene)     # frozenset of reaction IDs

# Get a specific reaction and its readable formula
rxn = mta.get_reaction("R00200")
print(rxn.formula_per_pathway["mta00010"]["formula_read"])
# e.g., "ATP + D-Glucose --> ADP + D-Glucose 6-phosphate"

# Work with a compound
atp = mta.get_compound("C00074")
print(f"{atp.name} – {atp.formula} – mass {atp.mass}")
# ATP – C10H16N5O13P3 – mass 507.181

# Which genes are linked to ATP?
print(atp.get_genes())    # frozenset of locus tags

🧠 What can you do with KEGGtangled?

  • Fetch & cache all KEGG data for any organism code (e.g., eco, hsa, mta) with a single line.
    No more manual downloads or API wrangling.

  • Trace metabolic connections effortlessly:

    • Gene → KOs → Reactions → Compounds (and back)
    • Built‑in walkers: get_reactions_for_gene(), get_compounds_for_reaction(), get_genes_for_reaction()
  • Inspect pathways in detail:

    • Readable reaction formulas (substrate / product names)
    • Reversible vs. irreversible arrows (<=> / -->)
    • Gene lists per pathway, pathways per gene, etc.
  • Save & resume your work:

    • organism.save("my_eco.pkl") snapshots the fully‑loaded state
    • Organism.load("my_eco.pkl") restores it instantly – no network calls, no recomputation
  • Iterate over everything:

    for comp in mta.compounds:
        print(comp.id, comp.name)
    for rxn in mta.all_reactions:
        print(rxn.reaction_id)
    
  • Get a quick overview with mta.summary() or just print(mta).


🔍 Key API at a glance

Method / Property Description
Organism(org_code, cache_dir) Create or load a KEGG organism
.load_pathway(pathway_id) Load a pathway (genes, reactions, compounds, formulas)
.load_all_cached_pathways() Load every pathway already cached on disk
.get_compound("C00022") Retrieve a compound (fetches details if needed)
.get_reaction("R00200") Retrieve a reaction
.get_genes_for_ko(ko) All genes annotated with a KO
.get_reactions_for_ko(ko) All reactions linked to a KO
.get_kos_for_gene(locus_tag) KOs linked to a gene
.get_reactions_for_gene(locus_tag) Reactions linked to a gene (via KOs)
.get_compounds_for_reaction(rxn_id) Compound IDs involved in a reaction
.get_genes_for_reaction(rxn_id) Genes linked to a reaction
.get_pathways_for_gene(locus_tag) Pathways containing a gene
.get_pathways_for_reaction(rxn_id) Pathways containing a reaction
.save(filename) Pickle the entire organism with version check
Organism.load(filename) Load a previously saved organism
.compounds / .all_reactions / .all_pathways Iterate over loaded data
.summary() Dictionary with counts of pathways, reactions, compounds, genes, KOs

📂 Caching & performance

keggtangled stores everything it downloads in the cache_dir folder:

  • KO‑gene and KO‑reaction mappings (JSON)
  • Pathway flat files and parsed KGML (both raw XML and pickled parsed objects)
  • Compound details (name, formula, mass)

After the first run, subsequent Organism() creations will load from cache in seconds.
When you save an organism with .save(), all in‑memory computed relations are preserved, so loading is nearly instant.


🧪 Example: discover all compounds linked to a gene

# Find every compound that 'Moth_0033' touches
reactions = mta.get_reactions_for_gene("Moth_0033")
all_compounds = set()
for rxn in reactions:
    all_compounds.update(mta.get_compounds_for_reaction(rxn))

for cid in sorted(all_compounds):
    comp = mta.get_compound(cid)
    print(f"{comp.id:10s} {comp.name or 'unknown'}")

📚 Requirements & dependencies

  • Python ≥ 3.8
  • Biopython (for KEGG API access and KGML parsing)
  • Optional: tqdm – shows progress bars during large fetches.

🤝 Contributing

Bug reports, feature requests, and pull requests are welcome!
Please check the issues page first.


📄 License

keggtangled is released under the MIT License.
KEGG data is provided by Kanehisa Laboratories – please see their terms of use.

Metadata

Release files for keggtangled 0.6.1

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

Source distribution (sdist)

Source distribution for keggtangled 0.6.1
File Size Uploaded Provenance Actions
keggtangled-0.6.1.tar.gz 14.7 kB With provenance Details for keggtangled-0.6.1.tar.gz

Built distribution (wheel)

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

Total release size: 27.6 kB

Release files / keggtangled-0.6.1.tar.gz

Download URL keggtangled-0.6.1.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3308ae9e918a34da6f33bcd8c54e948b4539ace2dd8f9a4c35f02c7a765f9048
BLAKE2b-256 checksum
How to use checksums
3665ce051fa54f3ab1093fb9d2ae90585300804f93363862b570032a9c61f311
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 May 28, 2026.

Transparency log

Release files / keggtangled-0.6.1-py3-none-any.whl

Download URL keggtangled-0.6.1-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b256e0187555173ff77dd2c31eac8fde568e0afff36e1ec853cfbdd7404f58a8
BLAKE2b-256 checksum
How to use checksums
6c3e4e5c40f30904bb6de4ad1b00454b934905655d60564e94d8fb377f2aa809
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 May 28, 2026.

Transparency log

Security for keggtangled 0.6.1

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for keggtangled-0.6.1.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 May 28, 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.

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