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.6.0.tar.gz (14.6 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.6.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for keggtangled-0.6.0.tar.gz
Algorithm Hash digest
SHA256 0c816b3a65de797adc2a505a37b28ab2c5efa9931518aad79f30dbbea7deab4e
MD5 4f19d0970cac5f390b275044f17f3b3f
BLAKE2b-256 2054958085bdaa8f59a55a98042309647330e5197bbe927002fa742f12b4cbaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for keggtangled-0.6.0.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.6.0-py3-none-any.whl.

File metadata

  • Download URL: keggtangled-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5a81bd003c56ad81f91b8daf3d64c0d25493165699a036e29e03e7721547996
MD5 f03640cb19e574432107a28d4069e047
BLAKE2b-256 72c8269fd2ac97ca9f73a1fee8549152342305e1b6e8a307b37a2844b87fae73

See more details on using hashes here.

Provenance

The following attestation bundles were made for keggtangled-0.6.0-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