Skip to main content

A KEGG tool for managing compounds, reactions, and pathways

Project description

🧬 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.

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

keggtangled-0.4.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

keggtangled-0.4.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file keggtangled-0.4.1.tar.gz.

File metadata

  • Download URL: keggtangled-0.4.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for keggtangled-0.4.1.tar.gz
Algorithm Hash digest
SHA256 37f81575f2b8eacb59f04434d66e17d0c452a4195774c1e9d62e79a5b3daa81f
MD5 d2ab417aa0a701efe8062f7ef976f241
BLAKE2b-256 01e0d420582ace19371cac889ef7dde88a74f5aca7e6b68d77c55922e6ccfc59

See more details on using hashes here.

Provenance

The following attestation bundles were made for keggtangled-0.4.1.tar.gz:

Publisher: python-publish.yml on emarquezz/KEGGtangled

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file keggtangled-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: keggtangled-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for keggtangled-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 227ebfb462bf2cd00c510a85dec2d07ea864b630df994310d0089c329824f585
MD5 1361bcdf7d8d5674b0410e281ca7836d
BLAKE2b-256 95d23648f526a146c747305dd67dafcb3474ede3283356804177f821ed5d1389

See more details on using hashes here.

Provenance

The following attestation bundles were made for keggtangled-0.4.1-py3-none-any.whl:

Publisher: python-publish.yml on emarquezz/KEGGtangled

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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