API#
Init keggtools module
Models#
KEGG pathway models to parse object relational
- class keggtools.models.Relation(entry1: str, entry2: str, type: str)#
Relation model class.
- static parse(item: Element) Relation #
Parse XML element into Relation instance.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Relation instance.
- Return type:
- class keggtools.models.Subtype(name: str, value: str)#
Subtype model class.
- static parse(item: Element) Subtype #
Parse Subtype XML element.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element.
- Returns:
Parsed Subtype instance.
- Return type:
- class keggtools.models.Component(id: str)#
Component model.
- static parse(item: Element) Component #
Parsing ElementTree into Component.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Parsed Component instance.
- Return type:
- class keggtools.models.Graphics(x: str | None = None, y: str | None = None, width: str | None = None, height: str | None = None, coords: str | None = None, name: str | None = None, type: str | None = None, fgcolor: str | None = None, bgcolor: str | None = None)#
Graphics information for rendering.
- __init__(x: str | None = None, y: str | None = None, width: str | None = None, height: str | None = None, coords: str | None = None, name: str | None = None, type: str | None = None, fgcolor: str | None = None, bgcolor: str | None = None) None #
Init Graphics model instance.
- static parse(item: Element) Graphics #
Parse XML element into Graphics instance.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Parsed Graphics instance.
- Return type:
- class keggtools.models.Entry(id: str, name: str, type: str, link: str | None = None, reaction: str | None = None)#
Entry model class.
- __init__(id: str, name: str, type: str, link: str | None = None, reaction: str | None = None) None #
Init entry model instance.
- property has_multiple_names: bool#
Checks if entry has multiple names that are space seperated.
- Returns:
Retruns True if entry has multiple names.
- Return type:
- static parse(item: Element) Entry #
Parsing XML element into Entry instance.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Parsed Entry instance.
- Return type:
- class keggtools.models.Pathway(name: str, org: str, number: str, title: str | None = None, image: str | None = None, link: str | None = None)#
KEGG Pathway object. The KEGG pathway object stores graphics information and related objects.
- __init__(name: str, org: str, number: str, title: str | None = None, image: str | None = None, link: str | None = None) None #
Init KEGG Pathway model.
- Parameters:
- static parse(data: Element | str) Pathway #
Parsing XML string or element in Pathway instance.
- Parameters:
data (Union[xml.etree.ElementTree.Element, str]) – String or XML element to parse.
- Returns:
Parsed Pathway instance.
- Return type:
- to_xml() Element #
Generate XML element from Pathway instance and its children.
- Returns:
XML element in KGML format.
- Return type:
- to_xml_string() str #
Generate XML string from pathway instance.
- Returns:
XML string in KGML format.
- Return type:
- class keggtools.models.Reaction(id: str, name: str, type: str)#
Reaction model.
- static parse(item: Element) Reaction #
Parse XML element instance to Reaction model instance.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Parsed Reaction model.
- Return type:
- class keggtools.models.Product(id: str, name: str, alt: Alt | None = None)#
Reaction Product model.
- static parse(item: Element) Product #
Parse XML element instance to Product model instance.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Parsed Product model.
- Return type:
- class keggtools.models.Substrate(id: str, name: str, alt: Alt | None = None)#
reaction Substrate model
- static parse(item: Element) Substrate #
Parse XML element instance to Substrate model instance.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Returns:
Parsed Substrate model.
- Return type:
- class keggtools.models.Alt(name: str)#
Alt model.
- static parse(item: Element) Alt #
Parse Alt instance from XML element.
- Parameters:
item (xml.etree.ElementTree.Element) – XML element to parse.
- Return type:
- Returns:
Parsed Alt element.
Resolver#
Resolve requests to KEGG data Api
- class keggtools.resolver.Resolver(cache: Storage | str | None = None)#
KEGG pathway resolver class. Request interface for KEGG API endpoint.
- get_pathway_list(organism: str, **kwargs: Any) Dict[str, str] #
Request list of pathways linked to organism.
- get_pathway(organism: str, code: str, **kwargs: Any) Pathway #
Load and parse KGML pathway by identifier.
- get_compounds(**kwargs: Any) Dict[str, str] #
Get dict of components. Request from KEGG API if not in cache.
Storage#
Storage of KEGG data. Caching downloaded files from API to local file system.
- class keggtools.storage.Storage(cachedir: str | None = None)#
Storage handler class.
- check_cache_dir() None #
Checks if cache dir exist. Raises “NotADirectoryError” of caching folder not found.
- Raises:
NotADirectoryError – Error if cache folder does not exist.
- save(filename: str, data: str) str #
Save string as file in local storage. Returns absolute filename of save file.
- save_dump(filename: str, data: Any) str #
Save binary dump as file in local storage. Returns absolute filename of save file.
Analysis#
KEGG Enrichment analysis core
- class keggtools.analysis.Enrichment(pathways: List[Pathway])#
KEGG pathway enrichment analysis.
- get_subset(subset: List[str], inplace: bool = False) List[EnrichmentResult] #
Create subset of analysis result by list of pathway ids
- Parameters:
- Returns:
Subset of enrichment results.
- Return type:
- run_analysis(gene_list: List[str]) List[EnrichmentResult] #
List of gene ids. Return list of EnrichmentResult instances
- Parameters:
- Returns:
List of enrichment result instances.
- Return type:
- to_csv(file_obj: str | IOBase | Any, delimiter: str = '\t', overwrite: bool = False) None #
Save result summary as file.
- class keggtools.analysis.EnrichmentResult(org: str, pathway_id: str, pathway_name: str, found_genes: list, pathway_genes: list, pathway_title: str | None = None)#
Results of KEGG pathway enrichment analysis.
- __init__(org: str, pathway_id: str, pathway_name: str, found_genes: list, pathway_genes: list, pathway_title: str | None = None) None #
Init Result of KEGG pathway enrichment analysis.
- property pathway_genes_count: int#
Count of pathway genes.
- Return type:
- Returns:
Number of genes in pathway.
- property study_count: int#
Count of study genes.
- Return type:
- Returns:
Number of genes found in analysis of pathway.
Renderer#
Render object
- class keggtools.render.Renderer(kegg_pathway: Pathway, gene_dict: Dict[str, float] | None = None, cache_or_resolver: Storage | str | Resolver | None = None, upper_color: Tuple[int, int, int] = (255, 0, 0), lower_color: Tuple[int, int, int] = (0, 0, 255))#
Renderer for KEGG Pathway.
- __init__(kegg_pathway: Pathway, gene_dict: Dict[str, float] | None = None, cache_or_resolver: Storage | str | Resolver | None = None, upper_color: Tuple[int, int, int] = (255, 0, 0), lower_color: Tuple[int, int, int] = (0, 0, 255)) None #
Init Renderer instance for KEGG Pathway.
- Parameters:
kegg_pathway (Pathway) – Pathway instance to render.
gene_dict (Optional[Dict[str, float]]) – Dict to specify overlay color gradient to rendered entries.
cache (Optional[Union[Storage, str, cache_or_resolver]]) – Specify cache for resolver instance or pass resolver. Resolver is needed to get compound data needed for rendering.
upper_color (Tuple[int, int, int]) – Color for upper bound of color gradient.
lower_color (Tuple[int, int, int]) – Color for lower bound of color gradient.
- property cmap_upreg: List[str]#
Generated color map as list of hexadecimal strings for upregulated genes in gene dict.
- property cmap_downreg: List[str]#
Generated color map as list of hexadecimal strings for downregulated genes in gene dict.
- get_gene_color(gene_id: str, default_color: Tuple[int, int, int] = (255, 255, 255)) str #
Get overlay color for given gene.
- render(display_unlabeled_genes: bool = True) None #
Render KEGG pathway.
- Parameters:
display_unlabeled_genes (bool) – Entries in the KGML format can have space-seperated entry names. Set this parameter to False to hide the entries.
- to_string() str #
pydot graph instance to dot string.
- Returns:
Generated dot string of pathway.
- Return type:
- keggtools.render.generate_embedded_html_table(items: Dict[str, str], border: int = 0, cellborder: int = 1, truncate: int | None = None) str #
Generate HTML table in insert into label of dot node.
generate_embedded_html_table({“gene1”: “#ffffff”, “gene2”: “#454545”})
- Parameters:
items (Dict[str, str]) – Items are dicts with have format {name: hex_color}.
border (int) – Thickness of table border. (Default: 0)
cellborder (int) – Thickness of cell border within the table. (Default: 1)
truncate (Optional[int]) – Maximal number of items in table. Set to None to disable trunaction. (Default: None)
- Returns:
Returns html string of table.
- Return type:
Utils#
Basic utils for HTTP requests, parsing and rendering
- class keggtools.utils.ColorGradient(start: tuple, stop: tuple, steps: int = 100)#
Create color gradient.
- keggtools.utils.get_attribute(element: Element, key: str) str #
Get attribute from XML Element object. Raises KeyError is Attribute is not found or not valid.
- Parameters:
element (xml.etree.ElementTree.Element) – XML element to get attribute from.
key (str) – Name of attribute.
- Returns:
Value of attribute.
- Return type:
- Raises:
ValueError – Error if attribute does not exist or is wrong type.
- keggtools.utils.get_numeric_attribute(element: Element, key: str) str #
Get attribute from XML Element object. Raises KeyError is Attribute is not found or not valid.
- Parameters:
element (Element) – XML element to get attribute from.
key (str) – Name of attribute.
- Returns:
Value of attribute. ValueError is raised if value is not a numeric string.
- Return type:
- Raises:
ValueError – Error is attribute is not a digit (numberic string), does not exist or is wrong type.
- keggtools.utils.parse_tsv_to_dict(data: str, col_keys: int = 0, col_values: int = 1) Dict[str, str] #
Parse .tsv file from string and build dict from first two columns. Other columns are ignored.
- keggtools.utils.parse_xml(xml_object_or_string: str | Element) Element #
Returns XML Element object from string or XML Element.
- Parameters:
xml_object_or_string (Union[str, xml.etree.ElementTree.Element]) – Input parameter to check.
- Returns:
XML element instance.
- Return type:
- keggtools.utils.is_valid_pathway_name(value: str) bool #
Check if combined pathway identifer is valid. String must match “path:<org><number>”.
- keggtools.utils.is_valid_pathway_number(value: str) bool #
Check if pathway number has correct 5 digit format.