o
    Rŀg{                     @   s6   d Z ddlmZ G dd dZd
ddZd
dd	ZdS )z3get/set abstraction for multi-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 )$
MultiGraphz5A directed multigraph abstraction with labeled edges. c                 C   s&   i | _ |D ]}t | j |< qi | _dS )z#Initialize a new MultiGraph object.N)_adjacency_listset
_label_map)selfnodesnr   r   N/var/www/html/myenv/lib/python3.10/site-packages/Bio/Pathway/Rep/MultiGraph.py__init__   s   
zMultiGraph.__init__c                 C   s"   t |to| j|jko| j|jkS )z(Return true if g is equal to this graph.)
isinstancer   r   r   )r   gr   r   r   __eq__   s
   


zMultiGraph.__eq__c              	   C   sP   d}t | jD ]}t | j| }|d|dddd |D  d7 }q|d S )	z4Return a unique string representation of this graph.<MultiGraph: (z: ,c                 s   s    | ]}t |V  qd S N)repr.0vr   r   r   	<genexpr>&   s    z&MultiGraph.__repr__.<locals>.<genexpr>)>)sortedr   join)r   skeyvaluesr   r   r   __repr__!   s
   (zMultiGraph.__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   )xyr   r   r   <lambda>-   s    z$MultiGraph.__str__.<locals>.<lambda>c                 S   s   g | ]}t |qS r   )lenr   r   r   r   
<listcomp>-       z&MultiGraph.__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MultiGraph.__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MultiGraph.add_nodeNc                 C   sz   || j vrtdt| || j vrtdt| ||f}| j | | || jvr1t | j|< | j| ||f dS )zAdd an edge to this graph.zUnknown <from> node: zUnknown <to> node: N)r   
ValueErrorr'   addr   r   )r   sourcetolabeledger   r   r   add_edge?   s   


zMultiGraph.add_edgec                 C   (   || j vrtdt| t| j | S )z1Return a list of (child, label) pairs for parent.zUnknown <parent> node: )r   r.   r'   r   r   parentr   r   r   child_edgesK      
zMultiGraph.child_edgesc                 C      t dd | |D S )z,Return a list of unique children for parent.c                 S      h | ]}|d  qS r   r   r   r!   r   r   r   	<setcomp>S   r&   z&MultiGraph.children.<locals>.<setcomp>)r   r8   r6   r   r   r   childrenQ      zMultiGraph.childrenc                 C   r5   )z/Return a list of all the edges with this label.zUnknown label: )r   r.   r'   r   )r   r2   r   r   r   edgesU   r9   zMultiGraph.edgesc                 C      t | j S )z3Return a list of all the edge labels in this graph.)r   r   keysr   r   r   r   labels[      zMultiGraph.labelsc                 C   rB   )z)Return a list of the nodes in this graph.)listr   rC   rD   r   r   r   r	   _   rF   zMultiGraph.nodesc                 C   sb   || j vrtdt| g }| j  D ]\}}|D ]}|d |kr+|||d f qqt|S )z1Return a list of (parent, label) pairs for child.zUnknown <child> node: r      )r   r.   r'   itemsappendr   )r   childparentsr7   r?   r!   r   r   r   parent_edgesc   s   
zMultiGraph.parent_edgesc                 C   r:   )z*Return a list of unique parents for child.c                 S   r;   r<   r   r=   r   r   r   r>   p   r&   z%MultiGraph.parents.<locals>.<setcomp>)r   rM   )r   rK   r   r   r   rL   n   r@   zMultiGraph.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 }|rC|| j|< q-| j|= q-dS )z*Remove node and all edges connected to it.zUnknown node: c                    s   h | ]
}|d   kr|qS r<   r   r=   r,   r   r   r>   z   s    z)MultiGraph.remove_node.<locals>.<setcomp>c                    s(   h | ]}|d   kr|d  kr|qS )r   rH   r   r=   rN   r   r   r>      s    "N)r   r.   r'   rG   r   rC   )r   r,   r
   r2   lmr   rN   r   remove_noder   s   




zMultiGraph.remove_nodec                 C   s   t d)zRemove edge (NOT IMPLEMENTED).z"remove_edge is not yet implemented)NotImplementedError)r   r7   rK   r2   r   r   r   remove_edge   s   zMultiGraph.remove_edge)r   r   )__name__
__module____qualname____doc__r   r   r    r+   r-   r4   r8   r?   rA   rE   r	   rM   rL   rP   rR   r   r   r   r   r      s"    

r   Nc                 C   s   i }g }t |  dk r|S |du r|  d }d||< || | |}t |dkrP|d }|dd }||vrJ|| d||< | || }t |dks,|S )zDepth first search of g.

    Returns a list of all nodes that can be reached from the root node
    in depth-first order.

    If root is not given, the search will be rooted at an arbitrary node.
    rH   Nr   )r$   r	   rJ   r?   graphrootseensearchcurrentr,   r   r   r   	df_search   s$   


r]   c                 C   s   i }g }t |  dk r|S |du r|  d }d||< || | |}t |dkrQ|d }|dd }||vrK|| d||< || | t |dks,|S )zBreadth first search of g.

    Returns a list of all nodes that can be reached from the root node
    in breadth-first order.

    If root is not given, the search will be rooted at an arbitrary node.
    rH   Nr   )r$   r	   rJ   r?   extendrW   r   r   r   	bf_search   s$   


r_   r   )rV   	functoolsr   r   r]   r_   r   r   r   r   <module>   s    
