o
    Rŀgk                     @   s   d Z ddlZddlm  mZ ddlmZ ddlm	Z	 G dd dZ
G dd dZG d	d
 d
ZG dd dZG dd dZG dd dZdS )a'  Classes to represent a KGML Pathway Map.

The KGML definition is as of release KGML v0.7.2
(http://www.kegg.jp/kegg/xml/docs/)

Classes:
 - Pathway - Specifies graph information for the pathway map
 - Relation - Specifies a relationship between two proteins or KOs,
   or protein and compound. There is an implied direction to the
   relationship in some cases.
 - Reaction - A specific chemical reaction between a substrate and
   a product.
 - Entry - A node in the pathway graph
 - Graphics - Entry subelement describing its visual representation

    N)chain)minidomc                   @   s  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZeeeedZdd Zdd Zdd  Zeeeed!Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zd4S )5Pathwaya  Represents a KGML pathway from KEGG.

    Specifies graph information for the pathway map, as described in
    release KGML v0.7.2 (http://www.kegg.jp/kegg/xml/docs/)

    Attributes:
     - name - KEGGID of the pathway map
     - org - ko/ec/[org prefix]
     - number - map number (integer)
     - title - the map title
     - image - URL of the image map for the pathway
     - link - URL of information about the pathway
     - entries - Dictionary of entries in the pathway, keyed by node ID
     - reactions - Set of reactions in the pathway

    The name attribute has a restricted format, so we make it a property and
    enforce the formatting.

    The Pathway object is the only allowed route for adding/removing
    Entry, Reaction, or Relation elements.

    Entries are held in a dictionary and keyed by the node ID for the
    pathway graph - this allows for ready access via the Reaction/Relation
    etc. elements.  Entries must be added before reference by any other
    element.

    Reactions are held in a dictionary, keyed by node ID for the path.
    The elements referred to in the reaction must be added before the
    reaction itself.

    c                 C   s<   d| _ d| _d| _d| _d| _d| _i | _i | _t | _	dS Initialize the class. N)
_nameorg_numbertitleimagelinkentries
_reactionsset
_relationsself r   N/var/www/html/myenv/lib/python3.10/site-packages/Bio/KEGG/KGML/KGML_pathway.py__init__A   s   zPathway.__init__c                 C   sH   d dddt  dg}|t| jd  }t|}|j	ddS )	z9Return the pathway as a string in prettified KGML format.
z<?xml version="1.0"?>zJ<!DOCTYPE pathway SYSTEM "http://www.genome.jp/kegg/xml/KGML_v0.7.2_.dtd">z <!-- Created by KGML_Pathway.py z -->zutf-8z  )indent)
jointimeasctimeETtostringelementdecoder   parseStringtoprettyxml)r   header	rough_xmlreparsedr   r   r   get_KGMLM   s   	
zPathway.get_KGMLc                 C   s@   t |jtstdt|j d|j d| |_|| j|j< dS )z$Add an Entry element to the pathway. Node ID must be an integer, got  ()N)
isinstanceidint	TypeErrortype_pathwayr   r   entryr   r   r   	add_entry\   s   zPathway.add_entryc                 C   8   t |jtstdt|j d|j d| j|j= dS )z)Remove an Entry element from the pathway.r&   r'   r(   N)r)   r*   r+   r,   r-   r   r/   r   r   r   remove_entryf   
   zPathway.remove_entryc                 C   sZ   t |jtstdt|j d|j d|j| jvr"td|j | |_|| j|j< dS )z&Add a Reaction element to the pathway.r&   r'   r(   z)Reaction ID %d has no corresponding entryN)r)   r*   r+   
ValueErrorr-   r   r.   r   r   reactionr   r   r   add_reactionq   s   zPathway.add_reactionc                 C   r2   )z+Remove a Reaction element from the pathway.r&   r'   r(   N)r)   r*   r+   r,   r-   r   r6   r   r   r   remove_reaction}   r4   zPathway.remove_reactionc                 C   s   | |_ | j| dS )z&Add a Relation element to the pathway.N)r.   r   addr   relationr   r   r   add_relation   s   zPathway.add_relationc                 C      | j | dS )z+Remove a Relation element from the pathway.N)r   remover;   r   r   r   remove_relation      zPathway.remove_relationc                    s   d| j  d| j d| j d| j dt| j dg}dD ]  fdd	| j D }t|r<|d
 t|f  qd|d S )z-Return a readable summary description string.z	Pathway: z	KEGG ID: zImage file: z
Organism: zEntries: %dzEntry types:)orthologenzymer7   genegroupcompoundmapc                    s   g | ]	}|j  kr|qS r   r-   .0etr   r   
<listcomp>       z#Pathway.__str__.<locals>.<listcomp>z	%s: %dr   )	r   namer   r	   lenr   valuesappendr   )r   outstretyper   rL   r   __str__   s   



zPathway.__str__c                 C      | j S Nr   r   r   r   r   _getname      zPathway._getnamec                 C   s"   | dstd| || _d S )Nzpath:z,Pathway name should begin with 'path:', got )
startswithr5   r   r   valuer   r   r   _setname   s   

zPathway._setnamec                 C      | ` d S rX   rY   r   r   r   r   _delname      zPathway._delnamezThe KEGGID for the pathway map.c                 C   rW   rX   r
   r   r   r   r   
_getnumber   r[   zPathway._getnumberc                 C      t || _d S rX   )r+   r
   r]   r   r   r   
_setnumber      zPathway._setnumberc                 C   r`   rX   rc   r   r   r   r   
_delnumber   rb   zPathway._delnumberzThe KEGG map number.c                 C      dd | j  D S )z'Get a list of entries of type compound.c                 S      g | ]	}|j d kr|qS )rF   rH   rI   r   r   r   rN      rO   z%Pathway.compounds.<locals>.<listcomp>r   rR   r   r   r   r   	compounds      zPathway.compoundsc                 C   ri   )z"Get a list of entries of type map.c                 S   rj   )rG   rH   rI   r   r   r   rN      rO   z Pathway.maps.<locals>.<listcomp>rk   r   r   r   r   maps   rm   zPathway.mapsc                 C   ri   )z'Get a list of entries of type ortholog.c                 S   rj   )rB   rH   rI   r   r   r   rN      rO   z%Pathway.orthologs.<locals>.<listcomp>rk   r   r   r   r   	orthologs   rm   zPathway.orthologsc                 C   ri   )z#Get a list of entries of type gene.c                 S   rj   )rD   rH   rI   r   r   r   rN      rO   z!Pathway.genes.<locals>.<listcomp>rk   r   r   r   r   genes   rm   zPathway.genesc                 C   s
   | j  S )z'Get a list of reactions in the pathway.)r   rR   r   r   r   r   	reactions      
zPathway.reactionsc                        fdd j D S )z>List of entries corresponding to each reaction in the pathway.c                    s   g | ]} j | qS r   )r   rJ   ir   r   r   rN      s    z,Pathway.reaction_entries.<locals>.<listcomp>r   r   r   r   r   reaction_entries   rm   zPathway.reaction_entriesc                 C   s
   t | jS )z'Get a list of relations in the pathway.)listr   r   r   r   r   	relations   rr   zPathway.relationsc                 C   s   t d}| j| jt| j| j| j| jd|_	t
| j D ]
\}}||j q| jD ]}||j q,t
| j D ]
\}}||j q<|S )z+Return the Pathway as a valid KGML element.pathway)rP   r	   numberr   r   r   )r   Elementr   r	   strr
   r   r   r   attribsortedr   itemsrS   r   r   r   )r   rz   eidr0   r<   r7   r   r   r   r      s   
	
zPathway.elementc                 C   s   g g }}dd | j  D D ] }||d d |d d g ||d d |d d g qt|t|ft|t|fgS )zCoordinate bounds for all Graphics elements in the Pathway.

        Returns the [(xmin, ymin), (xmax, ymax)] coordinates for all
        Graphics elements in the Pathway
        c                 S      g | ]}|j qS r   boundsrJ   gr   r   r   rN          z"Pathway.bounds.<locals>.<listcomp>r      )r   rR   extendminmaxr   xlistylistbr   r   r   r      s
   
  zPathway.boundsN)__name__
__module____qualname____doc__r   r%   r1   r3   r8   r9   r=   r@   rV   rZ   r_   ra   propertyrP   rd   rf   rh   r{   rl   rn   ro   rp   rq   rw   ry   r   r   r   r   r   r   r       sJ     








r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zee
eedZdd Zdd Zdd ZeeeedZdd Zdd Zd d! Zeeeed"Zed#d$ Zed%d& Zed'd( Zd)S )*Entrya  Represent an Entry from KGML.

    Each Entry element is a node in the pathway graph, as described in
    release KGML v0.7.2 (http://www.kegg.jp/kegg/xml/docs/)

    Attributes:
     - id - The ID of the entry in the pathway map (integer)
     - names - List of KEGG IDs for the entry
     - type - The type of the entry
     - link - URL of information about the entry
     - reaction - List of KEGG IDs of the corresponding reactions
       (integer)
     - graphics -    List of Graphics objects describing the Entry's visual
       representation
     - components - List of component node ID for this Entry ('group')
     - alt - List of alternate names for the Entry

    NOTE: The alt attribute represents a subelement of the substrate and
    product elements in the KGML file

    c                 C   sB   d| _ g | _d| _d| _d| _g | _t | _g | _d| _	g | _
dS r   Nr   )_id_namesr-   r   r   graphicsr   
componentsaltr.   r   r   r   r   r   r     s   
zEntry.__init__c                 C   sT   d| j  d| j d| j d| j d| j dt| j| jf g}d|d S )z#Return readable descriptive string.zEntry node ID: %dzNames: Type: zComponents: zReactions: zGraphics elements: %d %sr   )r*   rP   r-   r   r7   rQ   r   r   r   rT   r   r   r   rV   '  s   



zEntry.__str__c                 C   s:   | j dur|j| j jvrtd|j d| j| dS )zAdd an element to the entry.

        If the Entry is already part of a pathway, make sure
        the component already exists.
        Nz
Component z is not an entry in the pathway)r.   r*   r   r5   r   r:   )r   r   r   r   r   add_component3  s   
zEntry.add_componentc                 C   r>   )z3Remove the entry with the passed ID from the group.N)r   r?   r]   r   r   r   remove_component@  rA   zEntry.remove_componentc                 C   r>   )zAdd the Graphics entry.N)r   rS   r/   r   r   r   add_graphicsD  rA   zEntry.add_graphicsc                 C   r>   )z<Remove the Graphics entry with the passed ID from the group.N)r   r?   r/   r   r   r   remove_graphicsH  rA   zEntry.remove_graphicsc                 C      d | jS N r   r   r   r   r   r   rZ   M     zEntry._getnamec                 C      |  | _d S rX   )splitr   r]   r   r   r   r_   P  rg   zEntry._setnamec                 C   
   g | _ d S rX   )r   r   r   r   r   ra   S     
zEntry._delnamez'List of KEGG identifiers for the Entry.c                 C   r   r   )r   r   r   r   r   r   _getreaction[  r   zEntry._getreactionc                 C   r   rX   )r   r   r]   r   r   r   _setreaction^  rg   zEntry._setreactionc                 C   r   rX   rv   r   r   r   r   _delreactiona  r   zEntry._delreactionz)List of reaction KEGG IDs for this Entry.c                 C   rW   rX   r   r   r   r   r   _getidl  r[   zEntry._getidc                 C   re   rX   r+   r   r]   r   r   r   _setido  rg   zEntry._setidc                 C   r`   rX   r   r   r   r   r   _delidr  rb   zEntry._delidz(The pathway graph node ID for the Entry.c                 C   s   t d}t| j| j| j| jd|_t| j	r| j
|jd< t| jr/| jD ]}||j q&t| jr@| jD ]}||j q7|S )z)Return the Entry as a valid KGML element.r0   )r*   rP   r   r-   r7   )r   r|   r}   r   rP   r   r-   r~   rQ   r   r7   r   rS   r   r   )r   r0   r   cr   r   r   r   w  s   





zEntry.elementc                 C   s|   g g }}dd | j D D ] }||d d |d d g ||d d |d d g qt|t|ft|t|fgS )zCoordinate bounds for all Graphics elements in the Entry.

        Return the [(xmin, ymin), (xmax, ymax)] coordinates for the Entry
        Graphics elements.
        c                 S   r   r   r   r   r   r   r   rN     r   z Entry.bounds.<locals>.<listcomp>r   r   )r   r   r   r   r   r   r   r   r     s
   
  zEntry.boundsc                 C   s$   | j jD ]}| j|jv r dS qdS )zMReturn true if this Entry participates in any reaction in its parent pathway.TF)r.   rq   r   reactant_ids)r   rxnr   r   r   is_reactant  s
   zEntry.is_reactantN)r   r   r   r   r   rV   r   r   r   r   rZ   r_   ra   r   rP   r   r   r   r7   r   r   r   r*   r   r   r   r   r   r   r   r     sB    

r   c                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	 Zeeeed
Z	edd Z
dS )	ComponentaY  An Entry subelement used to represents a complex node.

    A subelement of the Entry element, used when the Entry is a complex
    node, as described in release KGML v0.7.2
    (http://www.kegg.jp/kegg/xml/docs/)

    The Component acts as a collection (with type 'group', and typically
    its own Graphics subelement), having only an ID.
    c                 C   s   d| _ || _dS )r   N)r   _parentr   parentr   r   r   r     s   
zComponent.__init__c                 C   rW   rX   r   r   r   r   r   r     r[   zComponent._getidc                 C   re   rX   r   r]   r   r   r   r     rg   zComponent._setidc                 C   r`   rX   r   r   r   r   r   r     rb   zComponent._delidz'The pathway graph node ID for the Entryc                 C   s   t d}dt| ji|_|S )z-Return the Component as a valid KGML element.	componentr*   )r   r|   r}   r   r~   )r   r   r   r   r   r     s   
zComponent.elementN)r   r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r     s    
r   c                   @   sF  e Zd ZdZdd Zdd Zdd Zdd	 Zeeeed
Z	dd Z
dd Zdd Zee
eedZdd Zdd Zdd ZeeeedZdd Zdd Zdd ZeeeedZd d! Zd"d# Zd$d% Zeeeed&Zd'd( Zd)d* Zd+d, Zeeeed-Zd.d/ Zd0d1 Zd2d3 Z eeee d4Z!ed5d6 Z"ed7d8 Z#ed9d: Z$d;S )<Graphicsa  An Entry subelement used to represents the visual representation.

    A subelement of Entry, specifying its visual representation, as
    described in release KGML v0.7.2 (http://www.kegg.jp/kegg/xml/docs/)

    Attributes:
     - name         Label for the graphics object
     - x            X-axis position of the object (int)
     - y            Y-axis position of the object (int)
     - coords       polyline coordinates, list of (int, int) tuples
     - type         object shape
     - width        object width (int)
     - height       object height (int)
     - fgcolor      object foreground color (hex RGB)
     - bgcolor      object background color (hex RGB)

    Some attributes are present only for specific graphics types.  For
    example, line types do not (typically) have a width.
    We permit non-DTD attributes and attribute settings, such as

    dash         List of ints, describing an on/off pattern for dashes

    c                 C   s@   d| _ d| _d| _d| _d| _d| _d| _d| _d| _|| _	dS r   )
rP   _x_y_coordsr-   _width_heightfgcolorbgcolorr   r   r   r   r   r     s   
zGraphics.__init__c                 C   rW   rX   r   r   r   r   r   _getx  r[   zGraphics._getxc                 C   re   rX   )floatr   r]   r   r   r   _setx  rg   zGraphics._setxc                 C   r`   rX   r   r   r   r   r   _delx  rb   zGraphics._delxz*The X coordinate for the graphics element.c                 C   rW   rX   r   r   r   r   r   _gety  r[   zGraphics._getyc                 C   re   rX   )r   r   r]   r   r   r   _sety  rg   zGraphics._setyc                 C   r`   rX   r   r   r   r   r   _dely  rb   zGraphics._delyz*The Y coordinate for the graphics element.c                 C   rW   rX   r   r   r   r   r   	_getwidth  r[   zGraphics._getwidthc                 C   re   rX   )r   r   r]   r   r   r   	_setwidth	  rg   zGraphics._setwidthc                 C   r`   rX   r   r   r   r   r   	_delwidth  rb   zGraphics._delwidthz"The width of the graphics element.c                 C   rW   rX   r   r   r   r   r   
_getheight  r[   zGraphics._getheightc                 C   re   rX   )r   r   r]   r   r   r   
_setheight  rg   zGraphics._setheightc                 C   r`   rX   r   r   r   r   r   
_delheight  rb   zGraphics._delheightz#The height of the graphics element.c                 C   rW   rX   r   r   r   r   r   
_getcoords!  r[   zGraphics._getcoordsc                    s8   dd | dD   fddtdt dD | _d S )Nc                 S      g | ]}t |qS r   )r+   rI   r   r   r   rN   %      z'Graphics._setcoords.<locals>.<listcomp>,c                    s    g | ]}t  ||d   qS )   )tuplert   clistr   r   rN   &  s     r   r   )r   rangerQ   r   r]   r   r   r   
_setcoords$  s   $zGraphics._setcoordsc                 C   r`   rX   r   r   r   r   r   
_delcoords(  rb   zGraphics._delcoordsz.Polyline coordinates for the graphics element.c                 C   rW   rX   _fgcolorr   r   r   r   _getfgcolor3  r[   zGraphics._getfgcolorc                 C      |dkr	d| _ d S || _ d S Nnonez#000000r   r]   r   r   r   _setfgcolor6     

zGraphics._setfgcolorc                 C   r`   rX   r   r   r   r   r   _delfgcolor<  rb   zGraphics._delfgcolorzForeground color.c                 C   rW   rX   _bgcolorr   r   r   r   _getbgcolorA  r[   zGraphics._getbgcolorc                 C   r   r   r   r]   r   r   r   _setbgcolorD  r   zGraphics._setbgcolorc                 C   r`   rX   r   r   r   r   r   _delbgcolorJ  rb   zGraphics._delbgcolorzBackground color.c                 C   s   t d}t| jtr| j}nd| j dd  }t| jtr$| j}nd| j dd  }| j| j||d|_	dD ]\}}t
| |durPtt
| ||j	|< q;| jdkrgdd	d
 t| jD |j	d< |S )z,Return the Graphics as a valid KGML element.r   #r   N)rP   r-   r   r   ))xr   )yr   )widthr   )heightr   liner   c                 S   r   r   )r}   rI   r   r   r   rN   l  r   z$Graphics.element.<locals>.<listcomp>coords)r   r|   r)   r   r}   hexvalr   rP   r-   r~   getattrr   r   from_iterabler   )r   r   fghexbghexnattrr   r   r   r   O  s*   


zGraphics.elementc                 C   s   | j dkr%dd | jD }dd | jD }t|t|ft|t|fgS | j| jd  | j| jd  f| j| jd  | j| jd  fgS )a,  Coordinate bounds for the Graphics element.

        Return the bounds of the Graphics object as an [(xmin, ymin),
        (xmax, ymax)] tuple.  Coordinates give the centre of the
        circle, rectangle, roundrectangle elements, so we have to
        adjust for the relevant width/height.
        r   c                 S   s   g | ]\}}|qS r   r   rJ   r   r   r   r   r   rN   z  r   z#Graphics.bounds.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r   r   r   r   r   r   rN   {  r         ?)r-   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   p  s   
	 zGraphics.boundsc                 C   s@   d| j d d | j d d   d| j d d | j d d   fS )z<Return the centre of the Graphics object as an (x, y) tuple.r   r   r   r   r   r   r   r   centre  s   zGraphics.centreN)%r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s\    
 
r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
eee	e
dZdd Zdd Zdd ZeeeedZedd Zedd Zedd Zedd Zed d! Zd"S )#Reactiona  A specific chemical reaction with substrates and products.

    This describes a specific chemical reaction between one or more
    substrates and one or more products.

    Attributes:
     - id             Pathway graph node ID of the entry
     - names          List of KEGG identifier(s) from the REACTION database
     - type           String: reversible or irreversible
     - substrate      Entry object of the substrate
     - product        Entry object of the product

    c                 C   s,   d| _ g | _d| _t | _t | _d| _dS r   )r   r   r-   r   _substrates	_productsr.   r   r   r   r   r     s   
zReaction.__init__c              	   C   sd   d| j  d| j d| j dddd | jD  ddd	d | jD  g}d
|d
 S )z,Return an informative human-readable string.zReaction node ID: zReaction KEGG IDs: r   zSubstrates: r   c                 S   r   r   rP   rJ   sr   r   r   rN     r   z$Reaction.__str__.<locals>.<listcomp>z
Products: c                 S   r   r   r  r  r   r   r   rN     r   r   )r*   rP   r-   r   
substratesproductsr   r   r   r   rV     s   


zReaction.__str__c                 C   s:   | j durt|| j jvrtdt| | j| dS )z<Add a substrate, identified by its node ID, to the reaction.Nz0Couldn't add substrate, no node ID %d in Pathway)r.   r+   r   r5   r  r:   )r   substrate_idr   r   r   add_substrate  s   
zReaction.add_substratec                 C   s:   | j durt|| j jvrtd| | jt| dS )z:Add a product, identified by its node ID, to the reaction.Nz.Couldn't add product, no node ID %d in Pathway)r.   r+   r   r5   r  r:   )r   
product_idr   r   r   add_product  s   
zReaction.add_productc                 C   rW   rX   r   r   r   r   r   r     r[   zReaction._getidc                 C   re   rX   r   r]   r   r   r   r     rg   zReaction._setidc                 C   r`   rX   r   r   r   r   r   r     rb   zReaction._delidzNode ID for the reaction.c                 C   r   r   r   r   r   r   r   	_getnames  r   zReaction._getnamesc                 C   s   | j |  d S rX   )r   r   r   r]   r   r   r   	_setnames  s   zReaction._setnamesc                 C   r`   rX   )namesr   r   r   r   	_delnames  rb   zReaction._delnamesz*List of KEGG identifiers for the reaction.c                    rs   )z(Return list of substrate Entry elements.c                       g | ]} j j| qS r   r.   r   )rJ   sidr   r   r   rN         z'Reaction.substrates.<locals>.<listcomp>)r  r   r   r   r   r    rm   zReaction.substratesc                    rs   )z&Return list of product Entry elements.c                    r  r   r  )rJ   pidr   r   r   rN     r  z%Reaction.products.<locals>.<listcomp>)r  r   r   r   r   r    rm   zReaction.productsc                 C   s   | j j| j S )z0Return the Entry corresponding to this reaction.)r.   r   r   r   r   r   r   r0        zReaction.entryc                 C   s   | j | jS )z4Return a list of substrate and product reactant IDs.)r  unionr  r   r   r   r   r     r  zReaction.reactant_idsc                 C   s   t d}t| j| j| jd|_| jD ]}t d}t||jd< | jj	| j|jd< |
| q| jD ]}t d}t||jd< | jj	| j|jd< |
| q5|S )z,Return KGML element describing the Reaction.r7   )r*   rP   r-   	substrater*   rP   product)r   r|   r}   r*   rP   r-   r~   r  r.   r   rS   r  )r   r7   r  r  pr  r   r   r   r     s   




zReaction.elementN)r   r   r   r   r   rV   r	  r  r   r   r   r   r*   r  r  r  rP   r  r  r0   r   r   r   r   r   r   r     s4    	




r   c                   @   sx   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	eeedZ
dd Zdd Zdd Ze	eeedZe	dd ZdS )Relationa"  A relationship between to products, KOs, or protein and compound.

    This describes a relationship between two products, KOs, or protein
    and compound, as described in release KGML v0.7.2
    (http://www.kegg.jp/kegg/xml/docs/)

    Attributes:
     - entry1 - The first Entry object node ID defining the
       relation (int)
     - entry2 - The second Entry object node ID defining the
       relation (int)
     - type - The relation type
     - subtypes - List of subtypes for the relation, as a list of
       (name, value) tuples

    c                 C   s"   d| _ d| _d| _g | _d| _dS r   )_entry1_entry2r-   subtypesr.   r   r   r   r   r     s
   
zRelation.__init__c                 C   sZ   dt | j dt| jdt| jg}| jD ]}|d|d  t|d g qd|S )z&Return a useful human-readable string.zRelation (subtypes: %d):zEntry1:zEntry2:z	Subtype: r   r   r   )rQ   r  r}   entry1entry2r   r   )r   rT   r  r   r   r   rV   #  s   
"
zRelation.__str__c                 C      | j d ur| j j| j S | jS rX   )r.   r   r  r   r   r   r   
_getentry11     
zRelation._getentry1c                 C   re   rX   )r+   r  r]   r   r   r   
_setentry16  rg   zRelation._setentry1c                 C   r`   rX   )r  r   r   r   r   
_delentry19  rb   zRelation._delentry1zEntry1 of the relation.c                 C   r   rX   )r.   r   r  r   r   r   r   
_getentry2>  r"  zRelation._getentry2c                 C   re   rX   )r+   r  r]   r   r   r   
_setentry2C  rg   zRelation._setentry2c                 C   r`   rX   )r  r   r   r   r   
_delentry2F  rb   zRelation._delentry2zEntry2 of the relation.c                 C   s^   t d}t| jt| j| jd|_| jD ]\}}t d}|t|d|_|| q|S )z,Return KGML element describing the Relation.r<   )r  r  r-   subtype)rP   r^   )	r   r|   r}   r  r  r-   r~   r  rS   )r   r<   rP   r^   r(  r   r   r   r   K  s   

zRelation.elementN)r   r   r   r   r   rV   r!  r#  r$  r   r  r%  r&  r'  r  r   r   r   r   r   r  	  s    r  )r   r   xml.etree.ElementTreeetreeElementTreer   	itertoolsr   xml.domr   r   r   r   r   r   r  r   r   r   r   <module>   s    d !& E|