def test_set_context(self): # set context as list builder = NiceCXBuilder() builder.set_context([{'a': 'a_url', 'b': 'b_url'}]) res = builder.get_nice_cx() self.assertEqual({'a': 'a_url', 'b': 'b_url'}, res.get_context()) # set context as dict builder = NiceCXBuilder() builder.set_context({'a': 'a_url', 'b': 'b_url'}) res = builder.get_nice_cx() self.assertEqual({'a': 'a_url', 'b': 'b_url'}, res.get_context())
def create_nice_cx_from_server(server, username=None, password=None, uuid=None): """ Create a :py:func:`~ndex2.nice_cx_network.NiceCXNetwork` based on a network retrieved from NDEx, specified by its UUID. If the network is not public, then username and password arguments for an account on the server with permission to access the network must be supplied. :param server: the URL of the NDEx server hosting the network. :param username: the user name of an account with permission to access the network. :param password: the password of an account with permission to access the network. :param uuid: the UUID of the network. :return: NiceCXNetwork :rtype: :py:func:`~ndex2.nice_cx_network.NiceCXNetwork` """ niceCxBuilder = NiceCXBuilder() if server and uuid: my_nicecx = NiceCXNetwork() # =================== # METADATA # =================== available_aspects = [] md_aspect_iter = my_nicecx.get_aspect(uuid, 'metaData', server, username, password) if md_aspect_iter: for ae in (o for o in md_aspect_iter): available_aspects.append(ae.get('name')) else: if not username or not password: raise Exception( 'Network is not available. Username and/or password not supplied' ) else: raise Exception('Network not available') opaque_aspects = set(available_aspects).difference(known_aspects_min) # ==================== # NETWORK ATTRIBUTES # ==================== if 'networkAttributes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'networkAttributes', server, username, password) for network_item in objects: niceCxBuilder._add_network_attributes_from_fragment( network_item) #niceCxBuilder.add_network_attribute(network_item.get('n'), network_item.get('v'), network_item.get('d')) # =================== # @CONTEXT # =================== if '@context' in available_aspects: objects = my_nicecx.get_aspect(uuid, '@context', server, username, password) niceCxBuilder.set_context( objects) #nice_cx.set_namespaces(objects) #niceCxBuilder.set_context(objects) # =================== # NODES # =================== if 'nodes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'nodes', server, username, password) for node_item in objects: niceCxBuilder._add_node_from_fragment(node_item) #niceCxBuilder.add_node(node_item.get('n'), node_item.get('r'), id=node_item.get('@id')) # =================== # EDGES # =================== if 'edges' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edges', server, username, password) for edge_item in objects: niceCxBuilder._add_edge_from_fragment(edge_item) #niceCxBuilder.add_edge(source=edge_item.get('s'), target=edge_item.get('t'), # interaction=edge_item.get('i'), id=edge_item.get('@id')) # =================== # NODE ATTRIBUTES # =================== if 'nodeAttributes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'nodeAttributes', server, username, password) for att in objects: niceCxBuilder._add_node_attribute_from_fragment(att) #niceCxBuilder.add_node_attribute(att.get('po'), att.get('n'), att.get('v'), type=att.get('d')) # =================== # EDGE ATTRIBUTES # =================== if 'edgeAttributes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edgeAttributes', server, username, password) for att in objects: niceCxBuilder._add_edge_attribute_from_fragment(att) #niceCxBuilder.add_edge_attribute(property_of=att.get('po'), name=att.get('n'), # values=att.get('v'), type=att.get('d')) # =================== # CITATIONS # =================== if 'citations' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'citations', server, username, password) for cit in objects: niceCxBuilder._add_citation_from_fragment(cit) #my_nicecx.citations[cit.get('@id')] = cit #my_nicecx.add_metadata_stub('citations') # =================== # SUPPORTS # =================== if 'supports' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'supports', server, username, password) for sup in objects: niceCxBuilder._add_supports_from_fragment(sup) #my_nicecx.supports[sup.get('@id')] = sup #my_nicecx.add_metadata_stub('supports') # =================== # EDGE SUPPORTS # =================== if 'edgeSupports' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edgeSupports', server, username, password) for add_this_edge_sup in objects: niceCxBuilder._add_edge_supports_from_fragment( add_this_edge_sup) #for po_id in add_this_edge_sup.get('po'): # my_nicecx.edgeSupports[po_id] = add_this_edge_sup.get('supports') #my_nicecx.add_metadata_stub('edgeSupports') # =================== # NODE CITATIONS # =================== if 'nodeCitations' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'nodeCitations', server, username, password) for node_cit in objects: niceCxBuilder._add_node_citations_from_fragment(node_cit) #for po_id in node_cit.get('po'): # my_nicecx.nodeCitations[po_id] = node_cit.get('citations') #my_nicecx.add_metadata_stub('nodeCitations') # =================== # EDGE CITATIONS # =================== if 'edgeCitations' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edgeCitations', server, username, password) for edge_cit in objects: niceCxBuilder._add_edge_citations_from_fragment(edge_cit) #for po_id in edge_cit.get('po'): # my_nicecx.nodeCitations[po_id] = edge_cit.get('citations') #my_nicecx.add_metadata_stub('edgeCitations') # =================== # OPAQUE ASPECTS # =================== for oa in opaque_aspects: objects = my_nicecx.get_aspect(uuid, oa, server, username, password) niceCxBuilder.add_opaque_aspect(oa, objects) else: raise Exception('Server and uuid not specified') return niceCxBuilder.get_nice_cx()
def create_nice_cx_from_raw_cx(cx): """ Create a :py:func:`~ndex2.nice_cx_network.NiceCXNetwork` from a as a `list` of `dict` objects in `CX format <https://www.home.ndexbio.org/data-model/>`__ Example: .. code-block:: python import json import ndex2 # cx_as_str is a str containing JSON in CX format above net_cx = ndex2.create_nice_cx_from_raw_cx(json.loads(cx_as_str)) :param cx: CX as a `list` of `dict` objects :type cx: list :return: NiceCXNetwork :rtype: :py:func:`~ndex2.nice_cx_network.NiceCXNetwork` """ if not cx: raise Exception('CX is empty') niceCxBuilder = NiceCXBuilder() # =================== # METADATA # =================== available_aspects = [] for ae in ( o for o in niceCxBuilder.get_frag_from_list_by_key(cx, 'metaData')): available_aspects.append(ae.get('name')) opaque_aspects = set(available_aspects).difference(known_aspects_min) # ==================== # NETWORK ATTRIBUTES # ==================== if 'networkAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'networkAttributes') for network_item in objects: niceCxBuilder._add_network_attributes_from_fragment(network_item) # =================== # NODES # =================== if 'nodes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodes') for node_item in objects: niceCxBuilder._add_node_from_fragment(node_item) # =================== # EDGES # =================== if 'edges' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edges') for edge_item in objects: niceCxBuilder._add_edge_from_fragment(edge_item) # =================== # NODE ATTRIBUTES # =================== if 'nodeAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodeAttributes') for att in objects: niceCxBuilder._add_node_attribute_from_fragment(att) # =================== # EDGE ATTRIBUTES # =================== if 'edgeAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edgeAttributes') for att in objects: niceCxBuilder._add_edge_attribute_from_fragment(att) # =================== # CITATIONS # =================== if 'citations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'citations') for cit in objects: niceCxBuilder._add_citation_from_fragment(cit) # =================== # SUPPORTS # =================== if 'supports' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'supports') for sup in objects: niceCxBuilder._add_supports_from_fragment(sup) # =================== # EDGE SUPPORTS # =================== if 'edgeSupports' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edgeSupports') for add_this_edge_sup in objects: niceCxBuilder._add_edge_supports_from_fragment(add_this_edge_sup) # =================== # NODE CITATIONS # =================== if 'nodeCitations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodeCitations') for node_cit in objects: niceCxBuilder._add_node_citations_from_fragment(node_cit) # =================== # EDGE CITATIONS # =================== if 'edgeCitations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edgeCitations') for edge_cit in objects: niceCxBuilder._add_edge_citations_from_fragment(edge_cit) # =================== # OPAQUE ASPECTS # =================== for oa in opaque_aspects: #TODO - Add context to builder if oa == '@context': objects = niceCxBuilder.get_frag_from_list_by_key(cx, oa) niceCxBuilder.set_context( objects) #nice_cx.set_namespaces(objects) else: objects = niceCxBuilder.get_frag_from_list_by_key(cx, oa) niceCxBuilder.add_opaque_aspect(oa, objects) return niceCxBuilder.get_nice_cx()
def create_nice_cx_from_raw_cx(cx): """ Create a NiceCXNetwork from a CX json object. (see http://www.home.ndexbio.org/data-model) :param cx: a valid CX document :return: NiceCXNetwork """ niceCxBuilder = NiceCXBuilder() #my_nicecx = NiceCXNetwork() if cx: # =================== # METADATA # =================== available_aspects = [] for ae in (o for o in niceCxBuilder.get_frag_from_list_by_key( cx, 'metaData')): available_aspects.append(ae.get('name')) opaque_aspects = set(available_aspects).difference(known_aspects_min) # ==================== # NETWORK ATTRIBUTES # ==================== if 'networkAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'networkAttributes') for network_item in objects: niceCxBuilder._add_network_attributes_from_fragment( network_item) # =================== # NODES # =================== if 'nodes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodes') for node_item in objects: niceCxBuilder._add_node_from_fragment(node_item) # =================== # EDGES # =================== if 'edges' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edges') for edge_item in objects: niceCxBuilder._add_edge_from_fragment(edge_item) # =================== # NODE ATTRIBUTES # =================== if 'nodeAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'nodeAttributes') for att in objects: niceCxBuilder._add_node_attribute_from_fragment(att) # =================== # EDGE ATTRIBUTES # =================== if 'edgeAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'edgeAttributes') for att in objects: niceCxBuilder._add_edge_attribute_from_fragment(att) # =================== # CITATIONS # =================== if 'citations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'citations') for cit in objects: niceCxBuilder._add_citation_from_fragment(cit) # =================== # SUPPORTS # =================== if 'supports' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'supports') for sup in objects: niceCxBuilder._add_supports_from_fragment(sup) # =================== # EDGE SUPPORTS # =================== if 'edgeSupports' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'edgeSupports') for add_this_edge_sup in objects: niceCxBuilder._add_edge_supports_from_fragment( add_this_edge_sup) # =================== # NODE CITATIONS # =================== if 'nodeCitations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'nodeCitations') for node_cit in objects: niceCxBuilder._add_node_citations_from_fragment(node_cit) # =================== # EDGE CITATIONS # =================== if 'edgeCitations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key( cx, 'edgeCitations') for edge_cit in objects: niceCxBuilder._add_edge_citations_from_fragment(edge_cit) # =================== # OPAQUE ASPECTS # =================== for oa in opaque_aspects: #TODO - Add context to builder if oa == '@context': objects = niceCxBuilder.get_frag_from_list_by_key(cx, oa) niceCxBuilder.set_context( objects) #nice_cx.set_namespaces(objects) else: objects = niceCxBuilder.get_frag_from_list_by_key(cx, oa) niceCxBuilder.add_opaque_aspect(oa, objects) return niceCxBuilder.get_nice_cx() else: raise Exception('CX is empty')
def test_context(self): print('Testing: context') context = { 'signor': 'http://signor.uniroma2.it/relation_result.php?id=', 'BTO': 'http://identifiers.org/bto/BTO:', 'uniprot': 'http://identifiers.org/uniprot/', 'pubmed': 'http://identifiers.org/pubmed/', 'CID': 'http://identifiers.org/pubchem.compound/', 'SID': 'http://identifiers.org/pubchem.substance/', 'chebi': 'http://identifiers.org/chebi/CHEBI:' } nice_cx_builder = NiceCXBuilder() nice_cx_builder.set_context(context) node_id_1 = nice_cx_builder.add_node(name='ABC', represents='ABC') node_id_2 = nice_cx_builder.add_node(name='DEF', represents='DEF') nice_cx_builder.add_node_attribute(node_id_1, 'testing_attr_double', [1.2, 2.5, 2.7]) nice_cx_builder.add_node_attribute(node_id_1, 'diffusion_output_heat', '1.4599107187941883E-4') nice_cx_builder.add_node_attribute(node_id_2, 'diffusion_output_heat', '1.2387941883E-4') nice_cx_builder.add_node_attribute(node_id_1, 'aliases', [ 'uniprot knowledgebase:Q5I2A4', 'uniprot knowledgebase:Q8N7E5', 'uniprot knowledgebase:Q8TAZ6' ]) nice_cx_builder.add_node_attribute(node_id_1, 'diffusion_output_rank', 1) nice_cx_builder.add_node_attribute(node_id_2, 'diffusion_output_rank', 2) edge_id = nice_cx_builder.add_edge(id=1, source=node_id_1, target=node_id_2, interaction='test-relationship') nice_cx_builder.add_edge_attribute(edge_id, 'citation', ['pubmed:21880741'], type='list_of_string') nice_cx = nice_cx_builder.get_nice_cx() node_attr_to_change = nice_cx.get_node_attribute( node_id_1, 'testing_attr_double') node_attr_to_change['v'] = [1.0, 2.0, 3.0] #for node_id, node in nice_cx.get_nodes(): #node_attr_to_condition = nice_cx.get_node_attribute(node_id, 'diffusion_output_heat') #node_attr_to_condition['v'] = ("%0.15f" % float(node_attr_to_condition.get('v'))) #node_attr_array_to_string = nice_cx.get_node_attribute(node_id, 'aliases') #node_attr_to_condition['v'] = ("%0.15f" % float(node_attr_to_condition.get('v'))) #nice_cx.remove_node_attribute(node_id, 'aliases') R = nice_cx.to_networkx() nos = [] for n in R.nodes(): if R.node[n]['diffusion_output_rank'] < 10: R.node[n]['nid'] = n nos.append(R.node[n]) nos = sorted(nos, key=lambda k: k['diffusion_output_rank']) for no in nos: print("id: " + str(no['nid']) + " name: " + no['nid'] + " rank: " + str(no['diffusion_output_rank']) + " heat: " + str(no['diffusion_output_heat'])) node_list2 = next(iter(nice_cx.nodes)) node_list = list(nice_cx.get_nodes()) print(list(nice_cx.get_nodes())) with open('my_cx.cx', 'w') as file: json.dump(nice_cx.to_cx(), file) upload_message = nice_cx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_context(self): print('Testing: context') context = { 'signor': 'http://signor.uniroma2.it/relation_result.php?id=', 'BTO': 'http://identifiers.org/bto/BTO:', 'uniprot': 'http://identifiers.org/uniprot/', 'pubmed': 'http://identifiers.org/pubmed/', 'CID': 'http://identifiers.org/pubchem.compound/', 'SID': 'http://identifiers.org/pubchem.substance/', 'chebi': 'http://identifiers.org/chebi/CHEBI:' } nice_cx_builder = NiceCXBuilder() nice_cx_builder.set_context(context) node_id_1 = nice_cx_builder.add_node(name='ABC', represents='ABC') node_id_2 = nice_cx_builder.add_node(name='DEF', represents='DEF') nice_cx_builder.add_node_attribute(node_id_1, 'testing_attr_double', [1.2, 2.5, 2.7]) nice_cx_builder.add_node_attribute(node_id_1, 'diffusion_output_heat', '1.4599107187941883E-4') nice_cx_builder.add_node_attribute(node_id_2, 'diffusion_output_heat', '1.2387941883E-4') nice_cx_builder.add_node_attribute(node_id_1, 'aliases', ['uniprot knowledgebase:Q5I2A4', 'uniprot knowledgebase:Q8N7E5', 'uniprot knowledgebase:Q8TAZ6']) nice_cx_builder.add_node_attribute(node_id_1, 'diffusion_output_rank', 1) nice_cx_builder.add_node_attribute(node_id_2, 'diffusion_output_rank', 2) edge_id = nice_cx_builder.add_edge(id=1, source=node_id_1, target=node_id_2, interaction='test-relationship') nice_cx_builder.add_edge_attribute(edge_id, 'citation', ['pubmed:21880741'], type='list_of_string') nice_cx = nice_cx_builder.get_nice_cx() node_attr_to_change = nice_cx.get_node_attribute(node_id_1, 'testing_attr_double') node_attr_to_change['v'] = [1.0, 2.0, 3.0] #for node_id, node in nice_cx.get_nodes(): #node_attr_to_condition = nice_cx.get_node_attribute(node_id, 'diffusion_output_heat') #node_attr_to_condition['v'] = ("%0.15f" % float(node_attr_to_condition.get('v'))) #node_attr_array_to_string = nice_cx.get_node_attribute(node_id, 'aliases') #node_attr_to_condition['v'] = ("%0.15f" % float(node_attr_to_condition.get('v'))) #nice_cx.remove_node_attribute(node_id, 'aliases') R = nice_cx.to_networkx() nos = [] for n in R.nodes(): if R.node[n]['diffusion_output_rank'] < 10: R.node[n]['nid'] = n nos.append(R.node[n]) nos = sorted(nos, key=lambda k: k['diffusion_output_rank']) for no in nos: print("id: " + str(no['nid']) + " name: " + no['nid'] + " rank: " + str( no['diffusion_output_rank']) + " heat: " + str(no['diffusion_output_heat'])) node_list2 = next(iter(nice_cx.nodes)) node_list = list(nice_cx.get_nodes()) print(list(nice_cx.get_nodes())) with open('my_cx.cx', 'w') as file: json.dump(nice_cx.to_cx(), file) upload_message = nice_cx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def create_nice_cx_from_server(server, username=None, password=None, uuid=None): """ Create a NiceCXNetwork based on a network retrieved from NDEx, specified by its UUID. If the network is not public, then username and password arguments for an account on the server with permission to access the network must be supplied. :param server: the URL of the NDEx server hosting the network. :param username: the user name of an account with permission to access the network. :param password: the password of an account with permission to access the network. :param uuid: the UUID of the network. :return: NiceCXNetwork """ niceCxBuilder = NiceCXBuilder() if server and uuid: my_nicecx = NiceCXNetwork() # =================== # METADATA # =================== available_aspects = [] md_aspect_iter = my_nicecx.get_aspect(uuid, 'metaData', server, username, password) if md_aspect_iter: for ae in (o for o in md_aspect_iter): available_aspects.append(ae.get('name')) else: if not username or not password: raise Exception('Network is not available. Username and/or password not supplied') else: raise Exception('Network not available') opaque_aspects = set(available_aspects).difference(known_aspects_min) # ==================== # NETWORK ATTRIBUTES # ==================== if 'networkAttributes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'networkAttributes', server, username, password) for network_item in objects: niceCxBuilder._add_network_attributes_from_fragment(network_item) #niceCxBuilder.add_network_attribute(network_item.get('n'), network_item.get('v'), network_item.get('d')) # =================== # @CONTEXT # =================== if '@context' in available_aspects: objects = my_nicecx.get_aspect(uuid, '@context', server, username, password) niceCxBuilder.set_context(objects) #nice_cx.set_namespaces(objects) #niceCxBuilder.set_context(objects) # =================== # NODES # =================== if 'nodes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'nodes', server, username, password) for node_item in objects: niceCxBuilder._add_node_from_fragment(node_item) #niceCxBuilder.add_node(node_item.get('n'), node_item.get('r'), id=node_item.get('@id')) # =================== # EDGES # =================== if 'edges' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edges', server, username, password) for edge_item in objects: niceCxBuilder._add_edge_from_fragment(edge_item) #niceCxBuilder.add_edge(source=edge_item.get('s'), target=edge_item.get('t'), # interaction=edge_item.get('i'), id=edge_item.get('@id')) # =================== # NODE ATTRIBUTES # =================== if 'nodeAttributes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'nodeAttributes', server, username, password) for att in objects: niceCxBuilder._add_node_attribute_from_fragment(att) #niceCxBuilder.add_node_attribute(att.get('po'), att.get('n'), att.get('v'), type=att.get('d')) # =================== # EDGE ATTRIBUTES # =================== if 'edgeAttributes' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edgeAttributes', server, username, password) for att in objects: niceCxBuilder._add_edge_attribute_from_fragment(att) #niceCxBuilder.add_edge_attribute(property_of=att.get('po'), name=att.get('n'), # values=att.get('v'), type=att.get('d')) # =================== # CITATIONS # =================== if 'citations' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'citations', server, username, password) for cit in objects: niceCxBuilder._add_citation_from_fragment(cit) #my_nicecx.citations[cit.get('@id')] = cit #my_nicecx.add_metadata_stub('citations') # =================== # SUPPORTS # =================== if 'supports' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'supports', server, username, password) for sup in objects: niceCxBuilder._add_supports_from_fragment(sup) #my_nicecx.supports[sup.get('@id')] = sup #my_nicecx.add_metadata_stub('supports') # =================== # EDGE SUPPORTS # =================== if 'edgeSupports' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edgeSupports', server, username, password) for add_this_edge_sup in objects: niceCxBuilder._add_edge_supports_from_fragment(add_this_edge_sup) #for po_id in add_this_edge_sup.get('po'): # my_nicecx.edgeSupports[po_id] = add_this_edge_sup.get('supports') #my_nicecx.add_metadata_stub('edgeSupports') # =================== # NODE CITATIONS # =================== if 'nodeCitations' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'nodeCitations', server, username, password) for node_cit in objects: niceCxBuilder._add_node_citations_from_fragment(node_cit) #for po_id in node_cit.get('po'): # my_nicecx.nodeCitations[po_id] = node_cit.get('citations') #my_nicecx.add_metadata_stub('nodeCitations') # =================== # EDGE CITATIONS # =================== if 'edgeCitations' in available_aspects: objects = my_nicecx.get_aspect(uuid, 'edgeCitations', server, username, password) for edge_cit in objects: niceCxBuilder._add_edge_citations_from_fragment(edge_cit) #for po_id in edge_cit.get('po'): # my_nicecx.nodeCitations[po_id] = edge_cit.get('citations') #my_nicecx.add_metadata_stub('edgeCitations') # =================== # OPAQUE ASPECTS # =================== for oa in opaque_aspects: objects = my_nicecx.get_aspect(uuid, oa, server, username, password) niceCxBuilder.add_opaque_aspect(oa, objects) else: raise Exception('Server and uuid not specified') return niceCxBuilder.get_nice_cx()
def create_nice_cx_from_raw_cx(cx): """ Create a NiceCXNetwork from a CX json object. (see http://www.home.ndexbio.org/data-model) :param cx: a valid CX document :return: NiceCXNetwork """ niceCxBuilder = NiceCXBuilder() #my_nicecx = NiceCXNetwork() if cx: # =================== # METADATA # =================== available_aspects = [] for ae in (o for o in niceCxBuilder.get_frag_from_list_by_key(cx, 'metaData')): available_aspects.append(ae.get('name')) opaque_aspects = set(available_aspects).difference(known_aspects_min) # ==================== # NETWORK ATTRIBUTES # ==================== if 'networkAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'networkAttributes') for network_item in objects: niceCxBuilder._add_network_attributes_from_fragment(network_item) # =================== # NODES # =================== if 'nodes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodes') for node_item in objects: niceCxBuilder._add_node_from_fragment(node_item) # =================== # EDGES # =================== if 'edges' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edges') for edge_item in objects: niceCxBuilder._add_edge_from_fragment(edge_item) # =================== # NODE ATTRIBUTES # =================== if 'nodeAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodeAttributes') for att in objects: niceCxBuilder._add_node_attribute_from_fragment(att) # =================== # EDGE ATTRIBUTES # =================== if 'edgeAttributes' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edgeAttributes') for att in objects: niceCxBuilder._add_edge_attribute_from_fragment(att) # =================== # CITATIONS # =================== if 'citations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'citations') for cit in objects: niceCxBuilder._add_citation_from_fragment(cit) # =================== # SUPPORTS # =================== if 'supports' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'supports') for sup in objects: niceCxBuilder._add_supports_from_fragment(sup) # =================== # EDGE SUPPORTS # =================== if 'edgeSupports' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edgeSupports') for add_this_edge_sup in objects: niceCxBuilder._add_edge_supports_from_fragment(add_this_edge_sup) # =================== # NODE CITATIONS # =================== if 'nodeCitations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'nodeCitations') for node_cit in objects: niceCxBuilder._add_node_citations_from_fragment(node_cit) # =================== # EDGE CITATIONS # =================== if 'edgeCitations' in available_aspects: objects = niceCxBuilder.get_frag_from_list_by_key(cx, 'edgeCitations') for edge_cit in objects: niceCxBuilder._add_edge_citations_from_fragment(edge_cit) # =================== # OPAQUE ASPECTS # =================== for oa in opaque_aspects: #TODO - Add context to builder if oa == '@context': objects = niceCxBuilder.get_frag_from_list_by_key(cx, oa) niceCxBuilder.set_context(objects) #nice_cx.set_namespaces(objects) else: objects = niceCxBuilder.get_frag_from_list_by_key(cx, oa) niceCxBuilder.add_opaque_aspect(oa, objects) return niceCxBuilder.get_nice_cx() else: raise Exception('CX is empty')