o
    Rŀg                     @   s"   d Z ddlmZ G dd dZdS )z-get/set abstraction for graph representation.    )reducec                   @   s   e Zd ZdZd"ddZdd Zdd Zd	d
 Zdd Zd#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S )$Graphz0A directed graph abstraction with labeled edges. c                 C   s,   i | _ |D ]}t | j |< qi | _i | _dS )zInitialize a new Graph object.N)_adjacency_listset
_label_map	_edge_map)selfnodesnr   r   I/var/www/html/myenv/lib/python3.10/site-packages/Bio/Pathway/Rep/Graph.py__init__   s
   
zGraph.__init__c                 C   s.   t |to| j|jko| j|jko| j|jkS )z(Return true if g is equal to this graph.)
isinstancer   r   r   r   )r	   gr   r   r   __eq__   s   



zGraph.__eq__c              	      sd   d}t jD ]& t  fddtj  D }|d dddd |D  d7 }q|d	 S )
z4Return a unique string representation of this graph.<Graph: c                 3   s"    | ]}|j  |f fV  qd S Nr   .0xkeyr	   r   r   	<genexpr>&   s    
z!Graph.__repr__.<locals>.<genexpr>(z: ,c                 s   s    | ]}t |V  qd S r   )reprr   vr   r   r   r   )   s    )>)sortedr   listjoin)r	   svaluesr   r   r   __repr__"   s   (zGraph.__repr__c                 C   sZ   t | j}tdd dd | j D }t | j}dt| d t| d t| d S )	z2Return a concise string description of this graph.c                 S   s   | | S r   r   )r   yr   r   r   <lambda>0   s    zGraph.__str__.<locals>.<lambda>c                 S   s   g | ]}t |qS r   )lenr   r   r   r   
<listcomp>0       z!Graph.__str__.<locals>.<listcomp>r   z
 node(s), z
 edge(s), z unique label(s)>)r)   r   r   r%   r   str)r	   nodenumedgenumlabelnumr   r   r   __str__,   s&   

zGraph.__str__c                 C   s   || j vrt | j |< dS dS )zAdd a node to this graph.N)r   r   )r	   noder   r   r   add_node=   s   
zGraph.add_nodeNc                 C   s   || j vrtdt| || j vrtdt| ||f| jv r/tt|d t| d | j | | || jvrBt | j|< | j| ||f || j||f< dS )zAdd an edge to this graph.zUnknown <from> node: zUnknown <to> node: z -> z existsN)r   
ValueErrorr,   r   addr   r   )r	   sourcetolabelr   r   r   add_edgeB   s   


zGraph.add_edgec                    s8    j vrtdt   fddtj   D S )z1Return a list of (child, label) pairs for parent.zUnknown <parent> node: c                    s   g | ]}|j  |f fqS r   r   r   parentr	   r   r   r*   T   s    z%Graph.child_edges.<locals>.<listcomp>)r   r3   r,   r!   r	   r:   r   r9   r   child_edgesP   s
   
zGraph.child_edgesc                 C   s   t | j| S )z,Return a list of unique children for parent.)r!   r   r;   r   r   r   childrenY      zGraph.childrenc                 C   s(   || j vrtdt| t| j | S )z/Return a list of all the edges with this label.zUnknown label: )r   r3   r,   r!   )r	   r7   r   r   r   edges]   s   
zGraph.edgesc                 C      t | j S )z3Return a list of all the edge labels in this graph.)r!   r   keysr	   r   r   r   labelsc   r>   zGraph.labelsc                 C   r@   )z)Return a list of the nodes in this graph.)r"   r   rA   rB   r   r   r   r
   g   r>   zGraph.nodesc                 C   sd   || j vrtdt| g }| j  D ]\}}|D ]}||kr,||| j||f f qqt|S )z1Return a list of (parent, label) pairs for child.zUnknown <child> node: )r   r3   r,   itemsappendr   r!   )r	   childparentsr:   r=   r   r   r   r   parent_edgesk   s   
zGraph.parent_edgesc                 C   s   t dd | |D S )z*Return a list of unique parents for child.c                 S   s   h | ]}|d  qS )r   r   r   r   r   r   	<setcomp>x   r+   z Graph.parents.<locals>.<setcomp>)r!   rH   )r	   rF   r   r   r   rG   v   s   zGraph.parentsc                    s    | j vrtdt  | j  = | j  D ]} fdd| j | D | j |< qt| j D ]} fdd| j| D }|rE|| j|< q/| j|= q/t| j D ]}|d  ks_|d  krc| j|= qQdS )z*Remove node and all edges connected to it.zUnknown node: c                    s   h | ]}| kr|qS r   r   r   r1   r   r   rI      s    z$Graph.remove_node.<locals>.<setcomp>c                    s(   h | ]}|d   kr|d  kr|qS )r      r   r   rJ   r   r   rI      s    "r   rK   N)r   r3   r,   rA   r"   r   r   )r	   r1   r   r7   lmedger   rJ   r   remove_nodez   s"   
 

zGraph.remove_nodec                 C   s   t d)zRemove edge (NOT IMPLEMENTED).z"remove_edge is not yet implemented)NotImplementedError)r	   r:   rF   r7   r   r   r   remove_edge   s   zGraph.remove_edge)r   r   )__name__
__module____qualname____doc__r   r   r&   r0   r2   r8   r<   r=   r?   rC   r
   rH   rG   rN   rP   r   r   r   r   r      s"    
	

	r   N)rT   	functoolsr   r   r   r   r   r   <module>   s   