ndexbio / ndex2-client

NDEx2 Client
BSD 3-Clause "New" or "Revised" License
6 stars 6 forks source link

NiceCXNetwork.to_cx doesn't create valid CX for NDEx #20

Closed cthoyt closed 6 years ago

cthoyt commented 6 years ago

I tried uploading a network generated exclusively through the NiceCXNetwork functions and a JSON like

import json
from ndex2 import NiceCXNetwork

cx_network = NiceCXNetwork()

cx_network = NiceCXNetwork()
cx_network.set_name('Test Name')

a, b, c, d, e = [
    cx_network.create_node(node_name=letter)
    for letter in 'ABCDE'
]

e1 = cx_network.create_edge(
    edge_source=a,
    edge_target=b,
)

e2 = cx_network.create_edge(
    edge_source=b,
    edge_target=c,
    edge_interaction='increases',
)

c1 = CitationElement(id=0, title='Hi')
cx_network.add_citation(c1)

s1 = SupportElement(id=0, text='Hi')
cx_network.add_support(s1)

cx_network.add_node_attribute(property_of=a, name='Color', values='Red')
cx_network.add_node_attribute(property_of=b, name='Color', values='Red')
cx_network.add_node_attribute(property_of=c, name='Color', values='Red')
cx_network.add_node_attribute(property_of=d, name='Color', values='Blue')
cx_network.add_node_attribute(property_of=e, name='Color', values='Blue')

cx_network.add_edge_attribute(property_of=e1, name='Color', values='Green')
cx_network.add_edge_attribute(property_of=e2, name='Color', values='Purple')

cx_network.add_edge_citations(edge_id=e1, citation_id=c1.get_id())

edge_support_element_1 = {CX_CONSTANTS.PROPERTY_OF: [e1], CX_CONSTANTS.SUPPORTS: [c1.get_id()]}
cx_network.add_edge_supports(edge_supports_element=edge_support_element_1)

with open('my_cx.cx', 'w') as file:
    json.dump(cx_network.to_cx(), file)

and got the error from NDEx "Error parsing element in CX stream: CX document ends without a Status object defined at line: 1, column: 5182"

It appears that the NiceCXNetwork does not generate the Status aspect that needs to be the last entry in each CX stream. Am I misusing this functionality, or is there a reason to make an update to this exporter?

cthoyt commented 6 years ago

I noticed elsewhere in the code was checking if the last element is a 'status' then do something like cx.append({"status": [{"error": "", "success": True}]}) as in

https://github.com/ndexbio/ndex2-client/blob/11dc2d31b4342fd8859eebad31d03d37c36233a1/nicecxModel/NiceCXNetwork.py#L1543-L1551

agary-ucsd commented 6 years ago

Since the upload_to() and update_to() methods add the status to the stream this bug went unnoticed. I'll add the status to the to_cx() method.

agary-ucsd commented 6 years ago

ndex2 client v2.0.0 produces valid cx in the to_cx() method