mgkit.kegg module

Module containing classes and functions to access Kegg data

class mgkit.kegg.KeggClientRest(cache=None)[source]

Bases: object

Changed in version 0.3.1: added a cache attribute for some methods

Kegg REST client

The class includes methods and data to use the REST API provided by Kegg. At the moment it provides methods to for ‘link’, ‘list’ and ‘get’ operations,

Kegg REST API

api_url = 'http://rest.kegg.jp/'
cache = None
contact = None
conv(target_db, source_db, strip=True)[source]

New in version 0.3.1.

Kegg Help:

http://rest.kegg.jp/conv/<target_db>/<source_db>

(<target_db> <source_db>) = (<kegg_db> <outside_db>) | (<outside_db> <kegg_db>)

For gene identifiers: <kegg_db> = <org> <org> = KEGG organism code or T number <outside_db> = ncbi-proteinid | ncbi-geneid | uniprot

For chemical substance identifiers: <kegg_db> = drug | compound | glycan <outside_db> = pubchem | chebi http://rest.kegg.jp/conv/<target_db>/<dbentries>

For gene identifiers: <dbentries> = database entries involving the following <database> <database> = <org> | genes | ncbi-proteinid | ncbi-geneid | uniprot <org> = KEGG organism code or T number

For chemical substance identifiers: <dbentries> = database entries involving the following <database> <database> = drug | compound | glycan | pubchem | chebi

Examples

>>> kc = KeggClientRest()
>>> kc.conv('ncbi-geneid', 'eco')
{'eco:b0217': {'ncbi-geneid:949009'},
 'eco:b0216': {'ncbi-geneid:947541'},
 'eco:b0215': {'ncbi-geneid:946441'},
 'eco:b0214': {'ncbi-geneid:946955'},
 'eco:b0213': {'ncbi-geneid:944903'},
...
>>> kc.conv('ncbi-proteinid', 'hsa:10458+ece:Z5100')
{'10458': {'NP_059345'}, 'Z5100': {'AAG58814'}}
cpd_desc_re = re.compile("cpd:(C\\d{5})\\t([\\w+, ()\\[\\]'*.-]+);?\\n?")
cpd_re = re.compile('ENTRY\\s+(C\\d{5})\\s+Compound\\nNAME\\s+([,.\\w+ ()-]+);?')
empty_cache(methods=None)[source]

New in version 0.3.1.

Empties the cache completely or for a specific method(s)

Parameters

methods (iterable, str) – string or iterable of strings that are part of the cache. If None the cache is fully emptied

find(query, database, options=None, strip=True)[source]

New in version 0.3.1.

Kegg Help:

http://rest.kegg.jp/find/<database>/<query>

<database> = pathway | module | ko | genome | <org> | compound | glycan |

reaction | rclass | enzyme | disease | drug | dgroup | environ | genes | ligand

<org> = KEGG organism code or T number

http://rest.kegg.jp/find/<database>/<query>/<option>

<database> = compound | drug <option> = formula | exact_mass | mol_weight

Examples

>>> kc = KeggClientRest()
>>> kc.find('CH4', 'compound')
{'C01438': 'Methane; CH4'}
>>> kc.find('K00844', 'genes', strip=False)
{'tped:TPE_0072': 'hexokinase; K00844 hexokinase [EC:2.7.1.1]',
...
>>> kc.find('174.05', 'compound', options='exact_mass')
{'C00493': '174.052823',
 'C04236': '174.052823',
 'C16588': '174.052823',
 'C17696': '174.052823',
 'C18307': '174.052823',
 'C18312': '174.052823',
 'C21281': '174.052823'}
get_entry(k_id, option=None)[source]

Changed in version 0.3.1: this is now cached

The method abstract the use of the ‘get’ operation in the Kegg API

Parameters
  • k_id (str) – kegg id of the resource to get

  • option (str) – optional, to specify a format

get_ids_names(target='ko', strip=True)[source]

New in version 0.1.13.

Changed in version 0.3.1: the call is now cached

Returns a dictionary with the names/description of all the id of a specific target, (ko, path, cpd, etc.)

If strip=True the id will stripped of the module abbreviation (e.g. md:M00002->M00002)

get_ortholog_pathways()[source]

Gets ortholog pathways, replace ‘map’ with ‘ko’ in the id

Returns a dictionary with the mappings KO->compounds for a specific Pathway or module

get_reaction_equations(ids, max_len=10)[source]

Get the equation for the reactions

id_prefix = {'C': 'cpd', 'K': 'ko', 'R': 'rn', 'k': 'map', 'm': 'path'}
ko_desc_re = re.compile("ko:(K\\d{5})\\t.+?;\\s+([\\w+, ()/:'\\[\\]-]+)( \\[EC:)?\\n?")

New in version 0.2.0.

Implements “link” operation in Kegg REST

http://www.genome.jp/linkdb/

Changed in version 0.3.1: removed strip and cached the results

The method abstract the use of the ‘link’ operation in the Kegg API

The target parameter can be one of the following:

pathway | brite | module | disease | drug | environ | ko | genome |
<org> | compound | glycan | reaction | rpair | rclass | enzyme

<org> = KEGG organism code or T number
Parameters
  • target (str) – the target db

  • ids – can be either a single id as a string or a list of ids

  • strip (bool) – if the prefix (e.g. ko:K00601) should be stripped

  • max_len (int) – the maximum number of ids to retrieve with each request, should not exceed 50

Return dict

dictionary mapping requested id to target id(s)

list_ids(k_id)[source]

The method abstract the use of the ‘list’ operation in the Kegg API

The k_id parameter can be one of the following:

pathway | brite | module | disease | drug | environ | ko | genome |
<org> | compound | glycan | reaction | rpair | rclass | enzyme

<org> = KEGG organism code or T number
Parameters

k_id (str) – kegg database to get list of ids

Return list

list of ids in the specified database

load_cache(file_handle)[source]

New in version 0.3.1.

Loads the cache from file

rn_eq_re = re.compile('C\\d{5}')
rn_name_re = re.compile('R\\d{5}')
write_cache(file_handle)[source]

New in version 0.3.1.

Write the cache to file

class mgkit.kegg.KeggModule(entry=None, old=False)[source]

Bases: object

New in version 0.1.13.

Used to extract information from a pathway module entry in Kegg

The entry, as a string, can be either passed at instance creation or with KeggModule.parse_entry()

classes = None
compounds = None
entry = ''
find_submodules()[source]

New in version 0.3.0.

Returns the possible submodules, as a list of tuples where the elements are the first and last compounds in a submodule

property first_cp

Returns the first compound in the module

property last_cp

Returns the first compound in the module

name = ''
parse_entry(entry)[source]

Parses a Kegg module entry and change the instance values. By default the reactions IDs are substituted with the KO IDs

parse_entry2(entry)[source]

New in version 0.3.0.

Parses a Kegg module entry and change the instance values. By default the reactions IDs are NOT substituted with the KO IDs.

static parse_reaction(line, ko_ids=None)[source]

Changed in version 0.3.0: cleaned the parsing

parses the lines with the reactions and substitute reaction IDs with the corresponding KO IDs if provided

reactions = None
to_edges(id_only=None)[source]

Changed in version 0.3.0: added id_only and changed to reflect changes in reactions

Returns the reactions as edges that can be supplied to make graph.

Parameters

id_only (None, iterable) – if None the returned edges are for the whole module, if an iterable (converted to a set), only edges for those reactions are returned

Yields

tuple – the elements are the compounds and reactions in the module

mgkit.kegg.parse_reaction(line, prefix='C', 'G')[source]

New in version 0.3.1.

Parses a reaction equation from Kegg, returning the left and right components. Needs testing

Parameters

line (str) – reaction string

Returns

left and right components as sets

Return type

tuple

Raises

ValueError – if the