Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_graphml fails to open graphml file generated by Cytoscape #2197

Closed
manihamidi opened this issue Jul 26, 2016 · 3 comments
Closed

read_graphml fails to open graphml file generated by Cytoscape #2197

manihamidi opened this issue Jul 26, 2016 · 3 comments

Comments

@manihamidi
Copy link

manihamidi commented Jul 26, 2016

networkx.read_graphml fails to import a graphml file generated by Cytoscape returning the following error:

Traceback (most recent call last):

  File "<ipython-input-323-63e903596d6f>", line 1, in <module>
    G = nx.read_graphml(path_to_graphml_file)

  File "<decorator-gen-327>", line 2, in read_graphml

  File "C:\Users\Mani\Anaconda\lib\site-packages\networkx\utils\decorators.py", line 220, in _open_file
    result = func(*new_args, **kwargs)

  File "C:\Users\Mani\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 154, in read_graphml
    return glist[0]

  • Cytoscape is a popular graph visualization tool.
@manihamidi
Copy link
Author

manihamidi commented Jul 26, 2016

Temporary way of getting around the problem:

Manually add these variables to the start-tag of the graphml file generated by cytoscape:

xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"

i.e. open the graphml file in a text editor, and replace the second line ("< graphml >") with this:

 <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

@manihamidi manihamidi changed the title read_graphml fils to open graphml file generated by Cytoscape read_graphml fails to open graphml file generated by Cytoscape Jul 26, 2016
@progval
Copy link
Contributor

progval commented Jul 30, 2016

Can you provide an example file?

@hagberg hagberg added this to the networkx-future milestone Jan 15, 2017
@manihamidi
Copy link
Author

manihamidi commented Jan 18, 2017

Here is the content of the graphml file that I exported from Cytoscape 3.4.0 (used cytoscape itself to make a simple 2-node, 2-attribute network with 1 edge):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<graphml>
    <key attr.name="SUID" attr.type="long" for="node" id="SUID"/>
    <key attr.name="shared name" attr.type="string" for="node" id="shared name"/>
    <key attr.name="name" attr.type="string" for="node" id="name"/>
    <key attr.name="selected" attr.type="boolean" for="node" id="selected"/>
    <key attr.name="first_attr" attr.type="int" for="node" id="first_attr"/>
    <key attr.name="second_attr" attr.type="string" for="node" id="second_attr"/>
    <key attr.name="SUID" attr.type="long" for="node" id="SUID"/>
    <key attr.name="shared name" attr.type="string" for="node" id="shared name"/>
    <key attr.name="shared interaction" attr.type="string" for="node" id="shared interaction"/>
    <key attr.name="name" attr.type="string" for="node" id="name"/>
    <key attr.name="selected" attr.type="boolean" for="node" id="selected"/>
    <key attr.name="interaction" attr.type="string" for="node" id="interaction"/>
    <key attr.name="SUID" attr.type="long" for="node" id="SUID"/>
    <key attr.name="shared name" attr.type="string" for="node" id="shared name"/>
    <key attr.name="name" attr.type="string" for="node" id="name"/>
    <key attr.name="selected" attr.type="boolean" for="node" id="selected"/>
    <key attr.name="__Annotations" attr.type="string" for="node" id="__Annotations"/>
    <graph edgedefault="directed" id="Network">
        <node id="63">
            <data key="SUID">63</data>
            <data key="shared name">Node 1</data>
            <data key="name">Node 1</data>
            <data key="selected">false</data>
            <data key="second_attr">'hello'</data>
        </node>
        <node id="62">
            <data key="SUID">62</data>
            <data key="shared name">Node 2</data>
            <data key="name">Node 2</data>
            <data key="selected">false</data>
            <data key="first_attr">1</data>
        </node>
        <edge source="63" target="62">
            <data key="SUID">64</data>
            <data key="shared name">Node 1 (interacts with) Node 2</data>
            <data key="shared interaction">interacts with</data>
            <data key="name">Node 1 (interacts with) Node 2</data>
            <data key="selected">false</data>
            <data key="interaction">interacts with</data>
        </edge>
    </graph>
</graphml>

and then I tried to open it with networkx and got this:

G_t = nx.read_graphml(path_t)
Traceback (most recent call last):

  File "<ipython-input-21-4a0389e1bd47>", line 1, in <module>
    G_t = nx.read_graphml(path_t)

  File "<decorator-gen-328>", line 2, in read_graphml

  File "C:\Users\Mani\Anaconda\lib\site-packages\networkx\utils\decorators.py", line 220, in _open_file
    result = func(*new_args, **kwargs)

  File "C:\Users\Mani\Anaconda\lib\site-packages\networkx\readwrite\graphml.py", line 154, in read_graphml
    return glist[0]

IndexError: list index out of range

As I explained in previous comment above, if you replace <graphml> with xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd", in the graphml file using your text editor, nx.read_graphml will function as expected.

dschult added a commit to dschult/networkx that referenced this issue Jul 15, 2017
dschult added a commit to dschult/networkx that referenced this issue Jul 17, 2017
dschult added a commit that referenced this issue Jul 19, 2017
* Added graphmlxml class

* Allow graphml to read Cytoscape files with incomplete headers

Fixes #2197

* graphml pep8 changes

* Add a test and some docstring for graphml json unicode treatment

addresses #1880

* Change read_graphml and parse_graphml docstring

Fixes #1398

* Update LXML writer to include attributes, work with py36 etc.

Set LXML as default write_graphml if present.
Fixes #448  using code from #1219

* Attempt to get travis to load lxml.

* Correct testing both xml and lxml writers in graphml

* skip docstring test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants