def _get_network_attributes_from_uuid(self): response = self._ndex.get_network_as_cx_stream(self._update_uuid) network = ndex2.create_nice_cx_from_raw_cx(response.json()) names = network.get_network_attribute_names() network_attributes = [] for name in names: network_attributes.append(network.get_network_attribute(name)) self._network_attributes = network_attributes
def test_full_core_aspects_cx_file(self): print('Testing: Full_core_aspects') path_to_network = os.path.join(path_this, 'network1.cx') with open(path_to_network, 'r') as ras_cx: nice_cx = ndex2.create_nice_cx_from_raw_cx(cx=json.load(ras_cx)) upload_message = nice_cx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_upload_network_with_no_network_attributes(self): """ So if one uploads a network with NO network attributes to NDEx and then edits the network (setting name visibility, description) via [updateNetworkSummary] [/v2/network/6b89b286-e142-11ec-b397-0ac135e8bacf/summary the CX2 endpoint shows the changed network attributes, but the CX1 is missing the network attributes :return: """ client = self.get_ndex2_client() # create network and add it net = NiceCXNetwork() oneid = net.create_node('node1') twoid = net.create_node('node2') net.create_edge(oneid, twoid, 'hello') res = client.save_new_network(net.to_cx(), visibility='PRIVATE') try: self.assertTrue('http' in res) netid = re.sub('^.*/', '', res) netsum = self.wait_for_network_to_be_ready(client, netid) self.assertIsNotNone( netsum, 'Network is still not ready,' ' maybe server is busy?') self.assertEqual(netid, netsum['externalId']) self.assertTrue('name' not in netsum, msg=str(netsum)) # okay now we have the network, lets update the name # and description and then get the network back again # via cx1 and cx2 endpoints netname = 'ndex2-client integration test network' + str( datetime.now()) client.update_network_profile(netid, {'name': netname}) netsum = self.wait_for_network_to_be_ready(client, netid) self.assertIsNotNone( netsum, 'Network is still not ready,' ' maybe server is busy?') cx2_resp = client.get_network_as_cx2_stream(network_id=netid) cx2_json = json.loads(cx2_resp.content) net_attrs = None for aspect in cx2_json: print(aspect) if 'networkAttributes' in aspect: net_attrs = aspect['networkAttributes'] break self.assertEqual([{'name': netname}], net_attrs) client_resp = client.get_network_as_cx_stream(network_id=netid) cx1_net = ndex2.create_nice_cx_from_raw_cx( json.loads(client_resp.content)) self.assertEqual(netname, cx1_net.get_name(), 'Special test to expose ' 'bug in NDEx server') finally: client.delete_network(netid)
def test_cx_file_with_position(self): print('Testing: network_with_position.cx') path_to_network = os.path.join(path_this, 'network_with_position.cx') with open(path_to_network, 'r') as ras_cx: niceCx = ndex2.create_nice_cx_from_raw_cx(cx=json.load(ras_cx)) #NiceCXNetwork(cx=json.load(ras_cx)) nice_networkx = niceCx.to_networkx() niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx) upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_export_to_cx_file(self): print('Testing: MEDIUM_NETWORK.cx') path_to_network = os.path.join(path_this, 'MEDIUM_NETWORK.cx') with open(path_to_network, 'r') as ras_cx: niceCx = ndex2.create_nice_cx_from_raw_cx(cx=json.load(ras_cx)) #NiceCXNetwork(cx=json.load(ras_cx)) niceCx.apply_template('public.ndexbio.org', '51247435-1e5f-11e8-b939-0ac135e8bacf') nice_networkx = niceCx.to_networkx() upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_create_nice_cx_from_raw_cx_with_glypican(self): with open(TestCreateNiceCXNetworkFromRawCX.GLYPICAN_FILE, 'r') as f: net_cx = ndex2.create_nice_cx_from_raw_cx(json.load(f)) self.assertEqual('Glypican 2 network', net_cx.get_name()) self.assertEqual(2, len(list(net_cx.get_nodes()))) self.assertEqual(1, len(list(net_cx.get_edges()))) # test for issue #60 make sure node/edge counters are # properly set self.assertEqual(2, net_cx.node_int_id_generator) self.assertEqual(1, net_cx.edge_int_id_generator)
def test_create_nice_cx_from_raw_cx_with_wnt_signaling(self): with open(TestCreateNiceCXNetworkFromRawCX.WNT_SIGNAL_FILE, 'r') as f: net_cx = ndex2.create_nice_cx_from_raw_cx(json.load(f)) self.assertEqual('WNT Signaling', net_cx.get_name()) self.assertEqual(32, len(list(net_cx.get_nodes()))) self.assertEqual(74, len(list(net_cx.get_edges()))) # test for issue #60 make sure node/edge counters are # properly set self.assertEqual(32, net_cx.node_int_id_generator) self.assertEqual(74, net_cx.edge_int_id_generator)
def test_create_from_cx_file_with_context(self): print('Testing: Metabolism_of_RNA_data_types.cx') path_to_network = os.path.join(path_this, 'Metabolism_of_RNA_data_types.cx') with open(path_to_network, 'r') as ras_cx: #==================================== # BUILD NICECX FROM CX OBJECT #==================================== niceCx = ndex2.create_nice_cx_from_raw_cx(json.load(ras_cx)) #NiceCXNetwork(cx=json.load(ras_cx)) my_cx = niceCx.to_cx() #print(my_cx) upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def test_create_from_cx_file2(self): print('Testing: CitationsAndSupports.cx') path_to_network = os.path.join(path_this, 'CitationsAndSupports.cx') with open(path_to_network, 'r') as ras_cx: #==================================== # BUILD NICECX FROM CX OBJECT #==================================== niceCx = ndex2.create_nice_cx_from_raw_cx(cx=json.load(ras_cx)) my_cx = niceCx.to_cx() #print(my_cx) upload_message = niceCx.upload_to(upload_server, upload_username, upload_password) self.assertTrue(upload_message)
def generate_viz(self, cx_network): nice_cx_network = ndex2.create_nice_cx_from_raw_cx(cx_network) nice_cx_network.print_summary() # convert nice_cx -> pandas nice_cx_df = nice_cx_network.to_pandas_dataframe() nice_cx_df = nice_cx_df.sort_values( by=([nice_cx_df.columns[0], nice_cx_df.columns[2]])) # convert pandas -> igraph edgelist = nice_cx_df.iloc[:, [0, 2]] tuples = [tuple(x) for x in edgelist.values] g_original = igraph.Graph.TupleList(tuples, directed=False) # Pick largest subgraph subgraphs = g_original.decompose() tmp = [i.vcount() for i in subgraphs] largeset_subgraph = subgraphs[tmp.index(max(tmp))] g_status = graph_status( largeset_subgraph.simplify(multiple=True, loops=True)) self.__compute_stats(g_status, self.options, self.rest_output) positions = self.__apply_layout(g_status.graph, g_status, self.options) # convert igraph -> networkx G_nx = nx.Graph() for i in range(g_status.graph.vcount()): G_nx.add_node(g_status.graph.vs['name'][i]) for i, j in g_status.graph.get_edgelist(): G_nx.add_edge(g_status.graph.vs['name'][i], g_status.graph.vs['name'][j]) # convert position -> certesian certesian = [{ 'node': i, 'x': list(positions)[i][0], 'y': list(positions)[i][1] } for i in range(len(positions))] # convert networkx -> NiceCX ncx_from_x = ndex2.create_nice_cx_from_networkx(G_nx) self.__add_ncxattributes(ncx_from_x, g_status, certesian, self.options) cxobj = ncx_from_x.to_cx() return cxobj
def test_data_types_with_special_chars(self): self.assertFalse(upload_username == 'username') path_to_network = os.path.join(path_this, 'Metabolism_of_RNA_data_types.cx') with open(path_to_network, 'r') as data_types_cx: #============================ # BUILD NICECX FROM CX FILE #============================ niceCx = ndex2.create_nice_cx_from_raw_cx(cx=json.load(data_types_cx)) found_list_of_strings_type = False for id, node in niceCx.get_nodes(): abc_node_attrs = niceCx.get_node_attributes(node) if abc_node_attrs is not None: for node_attr in abc_node_attrs: if node_attr.get('d') == 'list_of_string': found_list_of_strings_type = True self.assertTrue(found_list_of_strings_type)
def test_data_types_with_special_chars(self): self.assertFalse(upload_username == 'username') path_to_network = os.path.join(path_this, 'Metabolism_of_RNA_data_types.cx') with open(path_to_network, 'r') as data_types_cx: #============================ # BUILD NICECX FROM CX FILE #============================ niceCx = ndex2.create_nice_cx_from_raw_cx(cx=json.load(data_types_cx)) found_list_of_strings_type = False for id, node in niceCx.get_nodes(): abc_node_attrs = niceCx.get_node_attributes(node) if abc_node_attrs is not None: for node_attr in abc_node_attrs: if node_attr.get('d') == 'list_of_string': found_list_of_strings_type = True self.assertTrue(found_list_of_strings_type)
def test_create_nice_cx_from_raw_cx_with_invalidstr(self): net_cx = ndex2.create_nice_cx_from_raw_cx('invalid') self.assertEqual(None, net_cx.get_name()) self.assertEqual(0, len(list(net_cx.get_nodes()))) self.assertEqual(0, len(list(net_cx.get_edges())))
def test_create_nice_cx_from_raw_cx_with_emptystr(self): try: ndex2.create_nice_cx_from_raw_cx('') self.fail('Expected Exception') except Exception as e: self.assertEqual('CX is empty', str(e))
# #### *ndex2.create_nice_cx_from_raw_cx(cx) # In[ ]: #this method requests to create a NDEx2 Client object #import ndex2.client as nc #import json #my_account="PUT YOUR USER HERE" #my_password="******" #my_ndex=nc.Ndex2("http://public.ndexbio.org", my_account, my_password) #my_ndex.update_status() #query_result_cx_stream = my_ndex.get_neighborhood_as_cx_stream('c9243cce-2d32-11e8-b939-0ac135e8bacf','XRN1') #raw_cx = query_result_cx_stream.json() NiceCX_from_stream = ndex2.create_nice_cx_from_raw_cx(raw_cx) NiceCX_from_stream.print_summary() # #### *ndex2.create_nice_cx_from_file(path) # In[ ]: nice_cx_from_cx_file = ndex2.create_nice_cx_from_file('SimpleNetwork.cx') # #### *ndex2.create_nice_cx_from_networkx(networkx) # In[ ]: #create an Empty NetworkX G = nx.Graph() nice_cx_from_networkx = ndex2.create_nice_cx_from_networkx(G)
def test_get_network_attributes_with_network_with_context_set(self): p = self.get_dummy_params() p.description = None p.name = None p.t = 'abase' p.copyattribs = True loader = TSVLoader(p) cx = [{ "numberVerification": [{ "longNumber": 281474976710655 }] }, { "metaData": [{ "name": "nodes", "elementCount": 2, "idCounter": 220, "version": "1.0", "properties": [] }, { "name": "networkAttributes", "elementCount": 9, "version": "1.0", "properties": [] }, { "name": "edges", "elementCount": 1, "idCounter": 221, "version": "1.0", "properties": [] }] }, { "nodes": [{ "@id": 220, "n": "alpha" }, { "@id": 219, "n": "omega" }] }, { "networkAttributes": [{ "n": "@context", "v": "{\"hi\": \"bye\"}" }, { "n": "organism", "v": "z" }, { "n": "description", "v": "hhh" }, { "n": "version", "v": "za" }, { "n": "reference", "v": "z" }, { "n": "name", "v": "small" }] }, { "edges": [{ "@id": 221, "s": 220, "t": 219, "i": "interacts with" }] }, { "status": [{ "error": "", "success": True }] }] net = ndex2.create_nice_cx_from_raw_cx(cx) net_attribs = loader._get_network_attributes(net) self.assertEqual(5, len(net_attribs)) for n_a in net_attribs: if n_a['n'] == '@context': self.fail('@context should have been removed, ' 'but here it is: ' + str(n_a))