o
    Rŀg                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ d
d Zdd ZG dd dZedkrRddlmZ edd dS dS )a  Classes and functions to parse a KGML pathway map.

The KGML pathway map is parsed into the object structure defined in
KGML_Pathway.py in this module.

Classes:
 - KGMLParser - Parses KGML file

Functions:
 - read - Returns a single Pathway object, using KGMLParser internally

    )StringIO)ElementTree)	Component)Entry)Graphics)Pathway)Reaction)Relationc                 C   sT   t | }zt|}W n ty   tddw zt| td ty)   Y |S w )zParse a single KEGG Pathway from given file handle.

    Returns a single Pathway object.  There should be one and only
    one pathway in each file, but there may well be pathological
    examples out there.
    zNo pathways found in handleNz%More than one pathway found in handle)parsenextStopIteration
ValueError)handlepathwayspathway r   M/var/www/html/myenv/lib/python3.10/site-packages/Bio/KEGG/KGML/KGML_parser.pyread    s   
r   c                 c   s    z|  d W n ty$   zt| } W n ty!   tddw Y nw tj| ddD ]\}}|dkrD|jdkrDt| V  |	  q,dS )zReturn an iterator over Pathway elements.

    Arguments:
     - handle - file handle to a KGML file for parsing, or a KGML string

    This is a generator for the return of multiple Pathway objects.

    r   z:An XML-containing handle or an XML string must be providedN)startend)eventsr   r   )
r   AttributeErrorr   	TypeErrorr   	iterparsetag
KGMLParserr
   clear)r   eventelemr   r   r   r
   4   s*   
r
   c                   @   s    e Zd ZdZdd Zdd ZdS )r   a  Parses a KGML XML Pathway entry into a Pathway object.

    Example: Read and parse large metabolism file

    >>> from Bio.KEGG.KGML.KGML_parser import read
    >>> pathway = read(open('KEGG/ko01100.xml', 'r'))
    >>> print(len(pathway.entries))
    3628
    >>> print(len(pathway.reactions))
    1672
    >>> print(len(pathway.maps))
    149

    >>> pathway = read(open('KEGG/ko00010.xml', 'r'))
    >>> print(pathway) #doctest: +NORMALIZE_WHITESPACE
    Pathway: Glycolysis / Gluconeogenesis
    KEGG ID: path:ko00010
    Image file: http://www.kegg.jp/kegg/pathway/ko/ko00010.png
    Organism: ko
    Entries: 99
    Entry types:
        ortholog: 61
        compound: 31
        map: 7

    c                 C   s
   || _ dS )zInitialize the class.N)entry)selfr   r   r   r   __init__j   s   
zKGMLParser.__init__c                    s   fdd} fdd}dd dd  fd	d
}fdd}t  _|jj jD ]5}|jdkr;|| q/|jdkrE|| q/|jdkrO|| q/ddl}ddlm} |d|j d| q/jS )zParse the input elements.c                    s$   |   D ]\}} j|| qd S N)itemsr   __setattr__)attribkvr    r   r   _parse_pathwayq   s   z(KGMLParser.parse.<locals>._parse_pathwayc                    sj   t  }| j D ]
\}}||| q| D ]}|jdkr"|| q|jdkr, || qj| d S )Ngraphics	component)r   r%   r#   r$   r   r   	add_entry)element	new_entryr&   r'   
subelement_parse_component_parse_graphicsr    r   r   _parse_entryu   s   


z&KGMLParser.parse.<locals>._parse_entryc                 S   6   t |}| j D ]
\}}||| q	|| d S r"   )r   r%   r#   r$   add_graphics)r-   r   new_graphicsr&   r'   r   r   r   r2         z)KGMLParser.parse.<locals>._parse_graphicsc                 S   r4   r"   )r   r%   r#   r$   add_component)r-   r   new_componentr&   r'   r   r   r   r1      r7   z*KGMLParser.parse.<locals>._parse_componentc                    s~   t  }| j D ]
\}}||| q| D ]!}|jdkr'|t|jd  q|jdkr6|t|jd  q j	| d S )N	substrateidproduct)
r   r%   r#   r$   r   add_substrateintadd_productr   add_reaction)r-   new_reactionr&   r'   r/   r(   r   r   _parse_reaction   s   

z)KGMLParser.parse.<locals>._parse_reactionc                    s   t  }t| jd |_t| jd |_| jd |_| D ]$}|jd |jd }}|dv r7|j|t|f q|j||f q j	| d S )Nentry1entry2typenamevalue)compoundzhidden compound)
r	   r>   r%   rC   rD   rE   subtypesappendr   add_relation)r-   new_relationsubtyperF   rG   r(   r   r   _parse_relation   s   z)KGMLParser.parse.<locals>._parse_relationr   reactionrelationr   N)BiopythonParserWarningzWarning: tag z not implemented in parser)	r   r   r   r%   r   warningsBiorQ   warn)r    r)   r3   rB   rN   r-   rR   rQ   r   r0   r   r
   n   s,   






zKGMLParser.parseN)__name__
__module____qualname____doc__r!   r
   r   r   r   r   r   N   s    r   __main__)run_doctest)verboseN)rX   ior   	xml.etreer   Bio.KEGG.KGML.KGML_pathwayr   r   r   r   r   r	   r   r
   r   rU   
Bio._utilsrZ   r   r   r   r   <module>   s    p