def test_none_passed_into_get_graph(self): fac = DefaultNetworkXFactory() try: fac.get_graph(None) self.fail('Expected NDExError') except NDExError as ne: self.assertEqual('input network is None', str(ne))
def test_none_passed_into_get_graph(self): fac = DefaultNetworkXFactory() try: fac.get_graph(None) self.fail('Expected NDExError') except NDExError as ne: self.assertEqual('input network is None', str(ne))
def test_empty_network_passed_in_with_various_legacy_modes(self): net = NiceCXNetwork() fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.MultiGraph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) fac = DefaultNetworkXFactory(legacymode=True) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.Graph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) fac = DefaultNetworkXFactory(legacymode=False) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.MultiGraph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) fac = DefaultNetworkXFactory(legacymode=None) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.MultiGraph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) try: DefaultNetworkXFactory(legacymode='blah') self.fail('Expected NDExError') except NDExError as ne: self.assertEqual('blah not a valid value for ' 'legacymode parameter', str(ne))
def test_empty_network_passed_in_with_various_legacy_modes(self): net = NiceCXNetwork() fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.MultiGraph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) fac = DefaultNetworkXFactory(legacymode=True) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.Graph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) fac = DefaultNetworkXFactory(legacymode=False) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.MultiGraph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) fac = DefaultNetworkXFactory(legacymode=None) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.MultiGraph)) self.assertEqual(0, len(g)) self.assertEqual(0, g.number_of_edges()) try: DefaultNetworkXFactory(legacymode='blah') self.fail('Expected NDExError') except NDExError as ne: self.assertEqual('blah not a valid value for ' 'legacymode parameter', str(ne))
def test_glypican_network_legacymode_true(self): net = ndex2.create_nice_cx_from_file(TestDefaultNetworkXFactory.GLYPICAN_FILE) fac = DefaultNetworkXFactory(legacymode=True) g = fac.get_graph(net) self.assertTrue(isinstance(g, networkx.Graph)) self.assertEqual('Glypican 2 network', g.graph['name']) self.assertEqual('', g.graph['reference']) self.assertEqual('Mirko von Elstermann', g.graph['author']) self.assertEqual('Jorge Filmus', g.graph['reviewers']) self.assertEqual('glypican_2pathway', g.graph['labels']) self.assertEqual('APR-2018', g.graph['version']) self.assertEqual('human', g.graph['organism']) self.assertEqual('<i>Glypican 2 network</i> was derived from ' 'the latest BioPAX3 version of the Pathway ' 'Interaction Database (PID) curated by NCI/Nature. ' 'The BioPAX was first converted to Extended Binary ' 'SIF (EBS) by the PAXTools v5 utility. It was then ' 'processed to remove redundant edges, to add a ' '\'directed flow\' layout, and to add a graphic ' 'style using Cytoscape Visual Properties. This ' 'network can be found in searches using its original ' 'PID accession id, present in the \'labels\' ' 'property.', g.graph['description']) self.assertEqual(2, len(g)) self.assertEqual(1, g.number_of_edges()) self.assertTrue(0 in g) self.assertTrue(1 in g) if float(networkx.__version__) >= 2: nodelist = list(g.nodes(data=True)) edgelist = list(g.edges(data=True)) else: nodelist = g.nodes(data=True) edgelist = g.edges(data=True) self.assertEqual('MDK', nodelist[0][1]['name']) self.assertEqual('Protein', nodelist[0][1]['type']) aliaslist = nodelist[0][1]['alias'] self.assertEqual(2, len(aliaslist)) self.assertTrue('uniprot knowledgebase:Q2LEK4' in aliaslist) self.assertTrue('uniprot knowledgebase:Q9UCC7' in aliaslist) self.assertEqual('GPC2', nodelist[1][1]['name']) self.assertEqual('Protein', nodelist[1][1]['type']) aliaslist = nodelist[1][1]['alias'] self.assertEqual(1, len(aliaslist)) self.assertTrue('uniprot knowledgebase:Q8N158' in aliaslist) self.assertEqual(0, edgelist[0][0]) self.assertEqual(1, edgelist[0][1]) self.assertEqual('in-complex-with', edgelist[0][2]['interaction']) self.assertEqual('false', edgelist[0][2]['directed']) # check coordinates self.assertTrue((g.pos[0][0] + 398.3) < 1.0) self.assertTrue((g.pos[0][1] - 70.71) < 1.0) self.assertTrue((g.pos[1][0] + 353.49) < 1.0) self.assertTrue((g.pos[1][1] - 70.71) < 1.0)
def test_two_node_one_edge_network(self): net = NiceCXNetwork() net.create_node('first') net.create_node('second') net.create_edge(edge_source=0, edge_target=1) net.set_name('bob') fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertEqual('bob', g.graph['name']) self.assertEqual(2, len(g)) self.assertEqual(1, g.number_of_edges()) self.assertTrue(0 in g) if float(networkx.__version__) >= 2: nodelist = list(g.nodes(data=True)) edgelist = list(g.edges(data=True)) else: nodelist = g.nodes(data=True) edgelist = g.edges(data=True) self.assertEqual('first', nodelist[0][1]['name']) self.assertEqual('second', nodelist[1][1]['name']) self.assertEqual(0, edgelist[0][0]) self.assertEqual(1, edgelist[0][1]) self.assertEqual(None, edgelist[0][2]['interaction'])
def test_glypican_network_legacyfalse_and_multigraph_passed_in(self): net = ndex2.create_nice_cx_from_file( TestDefaultNetworkXFactory.GLYPICAN_FILE) fac = DefaultNetworkXFactory() g = fac.get_graph(net, networkx_graph=networkx.MultiGraph()) self.assertEqual('Glypican 2 network', g.graph['name']) self.assertEqual('', g.graph['reference']) self.assertEqual('Mirko von Elstermann', g.graph['author']) self.assertEqual('Jorge Filmus', g.graph['reviewers']) self.assertEqual('glypican_2pathway', g.graph['labels']) self.assertEqual('APR-2018', g.graph['version']) self.assertEqual('human', g.graph['organism']) self.assertEqual( '<i>Glypican 2 network</i> was derived from ' 'the latest BioPAX3 version of the Pathway ' 'Interaction Database (PID) curated by NCI/Nature. ' 'The BioPAX was first converted to Extended Binary ' 'SIF (EBS) by the PAXTools v5 utility. It was then ' 'processed to remove redundant edges, to add a ' '\'directed flow\' layout, and to add a graphic ' 'style using Cytoscape Visual Properties. This ' 'network can be found in searches using its original ' 'PID accession id, present in the \'labels\' ' 'property.', g.graph['description']) self.assertEqual(2, len(g)) self.assertEqual(1, g.number_of_edges()) self.assertTrue(0 in g) self.assertTrue(1 in g) if float(networkx.__version__) >= 2: nodelist = list(g.nodes(data=True)) edgelist = list(g.edges(data=True)) else: nodelist = g.nodes(data=True) edgelist = g.edges(data=True) self.assertEqual('MDK', nodelist[0][1]['name']) self.assertEqual('Protein', nodelist[0][1]['type']) aliaslist = nodelist[0][1]['alias'] self.assertEqual(2, len(aliaslist)) self.assertTrue('uniprot knowledgebase:Q2LEK4' in aliaslist) self.assertTrue('uniprot knowledgebase:Q9UCC7' in aliaslist) self.assertEqual('GPC2', nodelist[1][1]['name']) self.assertEqual('Protein', nodelist[1][1]['type']) aliaslist = nodelist[1][1]['alias'] self.assertEqual(1, len(aliaslist)) self.assertTrue('uniprot knowledgebase:Q8N158' in aliaslist) self.assertEqual(0, edgelist[0][0]) self.assertEqual(1, edgelist[0][1]) self.assertEqual('in-complex-with', edgelist[0][2]['interaction']) self.assertEqual('false', edgelist[0][2]['directed']) # check coordinates self.assertTrue((g.pos[0][0] + 398.3) < 1.0) self.assertTrue((g.pos[0][1] - 70.71) < 1.0) self.assertTrue((g.pos[1][0] + 353.49) < 1.0) self.assertTrue((g.pos[1][1] - 70.71) < 1.0)
def test_two_node_one_edge_network(self): net = NiceCXNetwork() net.create_node('first') net.create_node('second') net.create_edge(edge_source=0, edge_target=1) net.set_name('bob') fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertEqual('bob', g.graph['name']) self.assertEqual(2, len(g)) self.assertEqual(1, g.number_of_edges()) self.assertTrue(0 in g) if NETWORKX_MAJOR_VERSION >= 2: nodelist = list(g.nodes(data=True)) edgelist = list(g.edges(data=True)) else: nodelist = g.nodes(data=True) edgelist = g.edges(data=True) self.assertEqual('first', nodelist[0][1]['name']) self.assertEqual('second', nodelist[1][1]['name']) self.assertEqual(0, edgelist[0][0]) self.assertEqual(1, edgelist[0][1]) self.assertEqual(None, edgelist[0][2]['interaction'])
def test_darktheme_network_legacytrue(self): net = ndex2\ .create_nice_cx_from_file(TestDefaultNetworkXFactory .DARKTHEME_FILE) fac = DefaultNetworkXFactory(legacymode=True) g = fac.get_graph(net) self.assertEqual('Dark theme final version', g.graph['name']) self.assertTrue('Perfetto L.,' in g.graph['reference']) self.assertEqual('Theodora Pavlidou', g.graph['author']) self.assertEqual('SIGNOR-EGF', g.graph['labels']) self.assertEqual('18-Jan-2019', g.graph['version']) self.assertEqual('Human, 9606, H**o sapiens', g.graph['organism']) self.assertEqual('SIGNOR-EGF', g.graph['labels']) self.assertTrue('epidermal growth factor' in g.graph['description']) self.assertEqual(34, len(g)) self.assertEqual(50, g.number_of_edges()) self.assertTrue(1655 in g) self.assertTrue(1622 in g) if NETWORKX_MAJOR_VERSION >= 2: nodelist = list(g.nodes(data=True)) edgelist = list(g.edges(data=True)) else: nodelist = g.nodes(data=True) edgelist = g.edges(data=True) stat_three_index = -1 for i in range(0, len(nodelist)): if nodelist[i][1]['name'] == 'STAT3': stat_three_index = i break self.assertEqual('STAT3', nodelist[stat_three_index][1]['name']) self.assertEqual('protein', nodelist[stat_three_index][1]['type']) self.assertTrue('represents' not in nodelist[stat_three_index][1]) sixteenfiftyfiveedge = -1 for i in range(len(edgelist)): if edgelist[i][0] == 1655 and edgelist[i][1] == 1654: sixteenfiftyfiveedge = i break self.assertTrue((1655 == edgelist[sixteenfiftyfiveedge][0] and 1654 == edgelist[sixteenfiftyfiveedge][1]) or (1654 == edgelist[sixteenfiftyfiveedge][0] and 1655 == edgelist[sixteenfiftyfiveedge][1])) self.assertEqual('form complex', edgelist[sixteenfiftyfiveedge][2]['interaction']) self.assertEqual('true', edgelist[sixteenfiftyfiveedge][2]['directed']) self.assertEqual('"pubmed:15284024"', edgelist[sixteenfiftyfiveedge][2]['citation']) # check coordinates self.assertTrue((g.pos[1655][0] + 90.96) < 1.0) self.assertTrue((g.pos[1655][1] - 145.72) < 1.0)
def test_one_node_network(self): net = NiceCXNetwork() node_one = net.create_node('first') net.set_node_attribute(node_one, 'booleanattrib', 'false', type='boolean') net.set_node_attribute(node_one, 'integerattrib', '1', type='integer') net.set_node_attribute(node_one, 'doubleattrib', '2.0', type='double') net.set_node_attribute(node_one, 'longattrib', '3', type='long') net.set_node_attribute(node_one, 'stringattrib', 'false', type='string') net.set_node_attribute(node_one, 'list_of_booleanattrib', ['True'], type='list_of_boolean') net.set_node_attribute(node_one, 'list_of_doubleattrib', ['4.0'], type='list_of_double') net.set_node_attribute(node_one, 'list_of_integerattrib', ['5'], type='list_of_integer') net.set_node_attribute(node_one, 'list_of_longattrib', ['6'], type='list_of_long') net.set_node_attribute(node_one, 'list_of_stringattrib', ['false'], type='list_of_string') net.set_name('bob') fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertEqual('bob', g.graph['name']) self.assertEqual(1, len(g)) self.assertEqual(0, g.number_of_edges()) self.assertTrue(0 in g) if NETWORKX_MAJOR_VERSION >= 2: nodelist = list(g.nodes(data=True)) else: nodelist = g.nodes(data=True) self.assertEqual('false', nodelist[0][1]['booleanattrib']) self.assertEqual('1', nodelist[0][1]['integerattrib']) self.assertEqual('2.0', nodelist[0][1]['doubleattrib']) self.assertEqual('3', nodelist[0][1]['longattrib']) self.assertEqual('false', nodelist[0][1]['stringattrib']) self.assertEqual(['True'], nodelist[0][1]['list_of_booleanattrib']) self.assertEqual(['4.0'], nodelist[0][1]['list_of_doubleattrib']) self.assertEqual(['5'], nodelist[0][1]['list_of_integerattrib']) self.assertEqual(['6'], nodelist[0][1]['list_of_longattrib']) self.assertEqual(['false'], nodelist[0][1]['list_of_stringattrib'])
def test_darktheme_network_legacytrue(self): net = ndex2.create_nice_cx_from_file(TestDefaultNetworkXFactory.DARKTHEME_FILE) fac = DefaultNetworkXFactory(legacymode=True) g = fac.get_graph(net) self.assertEqual('Dark theme final version', g.graph['name']) self.assertTrue('Perfetto L.,' in g.graph['reference']) self.assertEqual('Theodora Pavlidou', g.graph['author']) self.assertEqual('SIGNOR-EGF', g.graph['labels']) self.assertEqual('18-Jan-2019', g.graph['version']) self.assertEqual('Human, 9606, H**o sapiens', g.graph['organism']) self.assertEqual('SIGNOR-EGF', g.graph['labels']) self.assertTrue('epidermal growth factor' in g.graph['description']) self.assertEqual(34, len(g)) self.assertEqual(50, g.number_of_edges()) self.assertTrue(1655 in g) self.assertTrue(1622 in g) if float(networkx.__version__) >= 2: nodelist = list(g.nodes(data=True)) edgelist = list(g.edges(data=True)) else: nodelist = g.nodes(data=True) edgelist = g.edges(data=True) stat_three_index = -1 for i in range(0, len(nodelist)): if nodelist[i][1]['name'] == 'STAT3': stat_three_index = i break self.assertEqual('STAT3', nodelist[stat_three_index][1]['name']) self.assertEqual('protein', nodelist[stat_three_index][1]['type']) self.assertTrue('represents' not in nodelist[stat_three_index][1]) sixteenfiftyfiveedge = -1 for i in range(len(edgelist)): if edgelist[i][0] == 1655 and edgelist[i][1] == 1654: sixteenfiftyfiveedge = i break self.assertTrue((1655 == edgelist[sixteenfiftyfiveedge][0] and 1654 == edgelist[sixteenfiftyfiveedge][1]) or (1654 == edgelist[sixteenfiftyfiveedge][0] and 1655 == edgelist[sixteenfiftyfiveedge][1])) self.assertEqual('form complex', edgelist[sixteenfiftyfiveedge][2]['interaction']) self.assertEqual('true', edgelist[sixteenfiftyfiveedge][2]['directed']) self.assertEqual('"pubmed:15284024"', edgelist[sixteenfiftyfiveedge][2]['citation']) # check coordinates self.assertTrue((g.pos[1655][0] + 90.96) < 1.0) self.assertTrue((g.pos[1655][1] - 145.72) < 1.0)
def apply_layout_to_network(network): """ applies layout to network :param network: :return: """ fac = DefaultNetworkXFactory() netx = fac.get_graph(network, None) num_nodes = len(network.get_nodes()) netx.pos = nx.drawing.spring_layout(netx, scale=num_nodes, k=1.8, iterations=50) network.set_opaque_aspect("cartesianLayout", cartesian(netx))
def test_one_node_no_edge_network(self): net = NiceCXNetwork() net.create_node('first') net.set_name('bob') fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertEqual('bob', g.graph['name']) self.assertEqual(1, len(g)) self.assertEqual(0, g.number_of_edges()) self.assertTrue(0 in g) nodelist = g.nodes(data=True) if NETWORKX_MAJOR_VERSION >= 2: self.assertEqual('first', nodelist[0]['name']) else: self.assertEqual('first', nodelist[0][1]['name'])
def test_one_node_no_edge_network(self): net = NiceCXNetwork() net.create_node('first') net.set_name('bob') fac = DefaultNetworkXFactory() g = fac.get_graph(net) self.assertEqual('bob', g.graph['name']) self.assertEqual(1, len(g)) self.assertEqual(0, g.number_of_edges()) self.assertTrue(0 in g) nodelist = g.nodes(data=True) if float(networkx.__version__) >= 2: self.assertEqual('first', nodelist[0]['name']) else: self.assertEqual('first', nodelist[0][1]['name'])
def test_one_node_no_edge_network_legacytrue(self): net = NiceCXNetwork() net.create_node('first') net.set_name('bob') fac = DefaultNetworkXFactory(legacymode=True) g = fac.get_graph(net) self.assertEqual('bob', g.graph['name']) self.assertEqual(1, len(g)) self.assertEqual(0, g.number_of_edges()) self.assertTrue(0 in g) if float(networkx.__version__) >= 2: nodelist = list(g.nodes(data=True)) else: nodelist = g.nodes(data=True) self.assertEqual('first', nodelist[0][1]['name'])
class NiceCXNetworkXConvertor(object): """ """ def __init__(self): """ Constructor """ self._netxfac = DefaultNetworkXFactory() def get_networkx(self, network): """ :param network: :type network: :py:class:`~ndex2.nice_cx_network.NiceCXNetwork` :return: """ return self._netxfac.get_graph(network)
def apply_layout_to_network(network, theargs, target_node_degree): """ applies layout to network :param network: :return: """ fac = DefaultNetworkXFactory() netx = fac.get_graph(network, None) num_nodes = len(network.get_nodes()) # sys.stderr.write(str(list(netx.nodes(data=True))) + '\n') # sys.stderr.write(str(list(netx.edges(data=True))) + '\n') posdict, fixedlist = get_pos_values(target_node_degree) netx.pos = nx.drawing.spring_layout(netx, scale=theargs.scale, k=theargs.k, iterations=theargs.iterations, weight=theargs.weight, seed=theargs.seed, pos=posdict, fixed=fixedlist) network.set_opaque_aspect("cartesianLayout", cartesian(netx))
def run_diffusion(self, cxnetwork, time_param=0.1, normalize_laplacian=False, input_col_name=constants.DEFAULT_INPUT, output_prefix=constants.DEFAULT_OUTPUT_PREFIX, correct_rank=False, via_service=False, service_read_timeout=360): """ Runs diffusion annotating the 'cxnetwork' passed in with new node attributes 'outputprefix'_heat and 'output_prefix'_rank added to 'cxnetwork' in place. :param cxnetwork: network to run diffusion on :type cxnetwork: :py:class:`~ndex2.nice_cx_network.NiceCXNetwork` :param time_param: stop time passed to :py:func:`scipy.sparse.linalg.expm_multiply` :type time_param: int :param normalize_laplacian: If `True`, will create a normalized laplacian matrix for diffusion. :type normalize_laplacian: bool :param input_col_name: Name of node attribute that contains diffusion heat inputs which should be double values between `0.0` and `1.0` where `1.0` is maximum heat (seed) and 0.0 is minimum. :type input_col_name: str :param output_prefix: Prefix name for diffusion output attached to each node. Each node will get <PREFIX>_rank and <PREFIX>_heat. :type output_prefix: str :param correct_rank: If True, multiple nodes that have same heat will have same rank. NOTE: This only works with local invocation of diffusion at the moment and will raise a :py:class:`~networkheatdiffusion.base.HeatDiffusionError` if set to True and invoked with `via_service` set to `True` :type correct_rank: bool :param via_service: if `True` run diffusion via remote service :param service_read_timeout: Seconds to wait for a response from service. Only used when 'via_service' is set to `True` :type service_read_timeout: int :raises HeatDiffusionError: If network has no nodes and/or edges or there is an error :return: network passed in with diffusion columns added :rtype: :py:class:`~ndex2.nice_cx_network.NiceCXNetwork` """ if via_service is not None and via_service is True: if correct_rank is True: raise HeatDiffusionError( 'correct_rank flag only works with local invocation' 'of diffusion') return self._run_diffusion_via_service( cxnetwork, time_param=time_param, normalize_laplacian=normalize_laplacian, input_col_name=input_col_name, output_prefix=output_prefix, service_read_timeout=service_read_timeout) if cxnetwork is None: raise HeatDiffusionError('No network found') if len(cxnetwork.get_nodes()) == 0: raise HeatDiffusionError('No nodes found in network') if len(cxnetwork.get_edges()) == 0: raise HeatDiffusionError('No edges found in network') netx_fac = DefaultNetworkXFactory() netx_graph = netx_fac.get_graph(cxnetwork, networkx_graph=networkx.MultiGraph()) matrix = self._create_sparse_matrix(netx_graph, normalize_laplacian) heat_array = self._find_heat(netx_graph, input_col_name) diffused_heat_array = self._diffuse(matrix, heat_array, time_param) node_heat, node_rank = self._add_heat(netx_graph, diffused_heat_array, correct_rank=correct_rank) return self._add_diffusion_dict_to_network(cxnetwork, node_heat, node_rank)