o
    ]ZŽhÚ  ã                   @   s6   d dl ZddgZd
dd„Zejdddd
d	d„ƒZdS )é    NÚcytoscape_dataÚcytoscape_graphÚnameÚidc           
      C   s¤  ||kr	t  d¡‚dt| j ¡ ƒi}|  ¡ |d< |  ¡ |d< g g dœ|d< |d d }|d d }| j ¡ D ]/\}}d| ¡ i}| 	|¡pHt
|ƒ|d d	< ||d d
< | 	|¡p[t
|ƒ|d d< | |¡ q6|  ¡ r¥| jddD ]2}	d| j|	d  |	d  |	d   ¡ i}|	d |d d< |	d |d d< |	d |d d< | |¡ qp|S |  ¡ D ]&}	d| j|	d  |	d   ¡ i}|	d |d d< |	d |d d< | |¡ q©|S )a±  Returns data in Cytoscape JSON format (cyjs).

    Parameters
    ----------
    G : NetworkX Graph
        The graph to convert to cytoscape format
    name : string
        A string which is mapped to the 'name' node element in cyjs format.
        Must not have the same value as `ident`.
    ident : string
        A string which is mapped to the 'id' node element in cyjs format.
        Must not have the same value as `name`.

    Returns
    -------
    data: dict
        A dictionary with cyjs formatted data.

    Raises
    ------
    NetworkXError
        If the values for `name` and `ident` are identical.

    See Also
    --------
    cytoscape_graph: convert a dictionary in cyjs format to a graph

    References
    ----------
    .. [1] Cytoscape user's manual:
       http://manual.cytoscape.org/en/stable/index.html

    Examples
    --------
    >>> G = nx.path_graph(2)
    >>> nx.cytoscape_data(G)  # doctest: +SKIP
    {'data': [],
     'directed': False,
     'multigraph': False,
     'elements': {'nodes': [{'data': {'id': '0', 'value': 0, 'name': '0'}},
       {'data': {'id': '1', 'value': 1, 'name': '1'}}],
      'edges': [{'data': {'source': 0, 'target': 1}}]}}
    ú!name and ident must be different.ÚdataÚdirectedÚ
multigraph)ÚnodesÚedgesÚelementsr
   r   r   Úvaluer   T)Úkeysr   é   é   ÚsourceÚtargetÚkey)ÚnxÚNetworkXErrorÚlistÚgraphÚitemsZis_directedZis_multigraphr
   ÚcopyÚgetÚstrÚappendr   Zadj)
ÚGr   ÚidentZjsondatar
   r   ÚiÚjÚnÚe© r#   úV/var/www/auris/lib/python3.10/site-packages/networkx/readwrite/json_graph/cytoscape.pyr      s8   ,
&ûT)ZgraphsZreturns_graphc                 C   sr  ||kr	t  d¡‚|  d¡}|  d¡}|rt  ¡ }nt  ¡ }|r$| ¡ }t|  d¡ƒ|_| d d D ];}|d  ¡ }|d d }|d  |¡rP|d  |¡||< |d  |¡r`|d  |¡||< | 	|¡ |j
|  |¡ q2| d d D ]B}|d  ¡ }	|d d	 }
|d d
 }|r¦|d  dd¡}|j|
||d |j|
||f  |	¡ qt| |
|¡ |j|
|f  |	¡ qt|S )a  
    Create a NetworkX graph from a dictionary in cytoscape JSON format.

    Parameters
    ----------
    data : dict
        A dictionary of data conforming to cytoscape JSON format.
    name : string
        A string which is mapped to the 'name' node element in cyjs format.
        Must not have the same value as `ident`.
    ident : string
        A string which is mapped to the 'id' node element in cyjs format.
        Must not have the same value as `name`.

    Returns
    -------
    graph : a NetworkX graph instance
        The `graph` can be an instance of `Graph`, `DiGraph`, `MultiGraph`, or
        `MultiDiGraph` depending on the input data.

    Raises
    ------
    NetworkXError
        If the `name` and `ident` attributes are identical.

    See Also
    --------
    cytoscape_data: convert a NetworkX graph to a dict in cyjs format

    References
    ----------
    .. [1] Cytoscape user's manual:
       http://manual.cytoscape.org/en/stable/index.html

    Examples
    --------
    >>> data_dict = {
    ...     "data": [],
    ...     "directed": False,
    ...     "multigraph": False,
    ...     "elements": {
    ...         "nodes": [
    ...             {"data": {"id": "0", "value": 0, "name": "0"}},
    ...             {"data": {"id": "1", "value": 1, "name": "1"}},
    ...         ],
    ...         "edges": [{"data": {"source": 0, "target": 1}}],
    ...     },
    ... }
    >>> G = nx.cytoscape_graph(data_dict)
    >>> G.name
    ''
    >>> G.nodes()
    NodeView((0, 1))
    >>> G.nodes(data=True)[0]
    {'id': '0', 'value': 0, 'name': '0'}
    >>> G.edges(data=True)
    EdgeDataView([(0, 1, {'source': 0, 'target': 1})])
    r   r	   r   r   r   r
   r   r   r   r   r   r   )r   )r   r   r   Z
MultiGraphZGraphZto_directedÚdictr   r   Úadd_noder
   ÚupdateZadd_edger   )r   r   r   r	   r   r   ÚdZ	node_dataÚnodeZ	edge_dataZsourZtargr   r#   r#   r$   r   S   s<   <




)r   r   )Znetworkxr   Ú__all__r   Z_dispatchabler   r#   r#   r#   r$   Ú<module>   s
    
M