Example #1
0
    def test_nice_cx_from_networkx_using_graph_from_jupyternotebook(self):
        g = nx.Graph()
        g.add_node('ABC')
        g.add_node('DEF')
        g.add_node('GHI')
        g.add_node('JKL')
        g.add_node('MNO')
        g.add_node('PQR')
        g.add_node('XYZ')
        g.add_edges_from([('ABC', 'DEF'), ('DEF', 'GHI'), ('GHI', 'JKL'),
                          ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'),
                          ('MNO', 'XYZ'), ('DEF', 'PQR')])

        short_path = nx.shortest_path(g, source='ABC', target="MNO")

        path_subgraph = g.subgraph(short_path)

        g.name = 'Created from NetworkX (full)'
        nice_cx_full = ndex2.create_nice_cx_from_networkx(g)
        self.assertEqual(7, len(nice_cx_full.get_nodes()))
        self.assertEqual(8, len(nice_cx_full.get_edges()))
        self.assertEqual(g.name, nice_cx_full.get_name())
        g.name = 'Created from NetworkX (shortest path)'
        nice_cx_short = ndex2.create_nice_cx_from_networkx(path_subgraph)
        self.assertEqual(3, len(nice_cx_short.get_nodes()))
        self.assertEqual(2, len(nice_cx_short.get_edges()))
        self.assertEqual(g.name, nice_cx_short.get_name())
    def test_netx_plot(self):
        my_ndex = ndex2.client.Ndex2('http://test.ndexbio.org', 'scratch',
                                     'scratch')
        my_ndex.update_status()

        test1 = my_ndex.get_network_ids_for_user('scratch')

        nx_my_graph = nx.read_edgelist("edge_list_network_adrian.txt",
                                       nodetype=str)
        cx_my_graph = NdexGraph(networkx_G=nx_my_graph)

        cx_my_graph.upload_to('http://dev.ndexbio.org', 'scratch', 'scratch')

        G = nx.Graph()
        G.add_node('ABC')
        G.add_node('DEF')
        G.add_node('GHI')
        G.add_node('JKL')
        G.add_node('MNO')
        G.add_node('PQR')
        G.add_node('XYZ')
        G.add_edges_from([('ABC', 'DEF'), ('DEF', 'GHI'), ('GHI', 'JKL'),
                          ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'),
                          ('MNO', 'XYZ'), ('DEF', 'PQR')])

        niceCx_full = ndex2.create_nice_cx_from_networkx(G)
        niceCx_full_networkx = niceCx_full.to_networkx()

        names = nx.get_node_attributes(niceCx_full_networkx, 'name')
        for n in niceCx_full_networkx.nodes():
            print(n)
        print(niceCx_full_networkx.nodes)
        print(names)
    def test_create_from_networkx(self):
        path_to_network = os.path.join(path_this, 'SIMPLE3.txt')

        with open(path_to_network, 'rU') as tsvfile:
            reader = csv.DictReader(filter(lambda row: row[0] != '#', tsvfile),
                                    dialect='excel-tab',
                                    fieldnames=['s', 't', 'e'])

            #===========================
            # BUILD NETWORKX GRAPH
            #===========================
            G = nx.Graph(name='loaded from Simple3.txt')
            for row in reader:
                G.add_node(row.get('s'), test1='test1_s', test2='test2_s')
                G.add_node(row.get('t'), test1='test1_t', test2='test2_t')
                G.add_edge(row.get('s'), row.get('t'), {
                    'interaction': 'controls-production-of',
                    'test3': 'test3'
                })

            #====================================
            # BUILD NICECX FROM NETWORKX GRAPH
            #====================================
            niceCx = ndex2.create_nice_cx_from_networkx(
                G)  #NiceCXNetwork(networkx_G=G)
            niceCx.apply_template('public.ndexbio.org',
                                  '72ef5c3a-caff-11e7-ad58-0ac135e8bacf')
            upload_message = niceCx.upload_to(upload_server, upload_username,
                                              upload_password)
            self.assertTrue(upload_message)
    def test_create_from_tsv_manipulate_and_save(self):
        path_to_network = os.path.join(path_this, 'mgdb_mutations.txt')

        with open(path_to_network, 'rU') as tsvfile:
            header = [h.strip() for h in tsvfile.readline().split('\t')]

            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',names=header)

            niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='CDS Mutation', target_field='Gene Symbol', source_node_attr=['Primary Tissue', 'Histology', 'Genomic Locus'], target_node_attr=['Gene ID'], edge_interaction='variant-gene-relationship') #NiceCXNetwork()

            nice_networkx = niceCx.to_networkx()
            nice_pandas = niceCx.to_pandas_dataframe()
            my_csv = nice_pandas.to_csv(sep='\t')

            with open("pandas_to_cx_to_tsv_results.txt", "w") as text_file:
                text_file.write(my_csv)

            niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx)

            # Restore template
            niceCx_from_netx.apply_template('public.ndexbio.org', '72ef5c3a-caff-11e7-ad58-0ac135e8bacf')
            niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

            upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
Example #5
0
    def test_create_nice_cx_from_networkx_roundtrip_no_attrs(self):
        net = ndex2.nice_cx_network.NiceCXNetwork()
        node_one = net.create_node('Node 1')
        node_two = net.create_node('Node 2')
        net.create_edge(edge_source=node_one, edge_target=node_two)
        netx_net = net.to_networkx(mode='default')
        net_roundtrip = ndex2.create_nice_cx_from_networkx(netx_net)
        self.assertEqual(
            'created from networkx by '
            'ndex2.create_nice_cx_networkx()', net_roundtrip.get_name())
        self.assertEqual(1, len(net_roundtrip.get_edges()))
        self.assertEqual(2, len(net_roundtrip.get_nodes()))
        self.assertEqual((0, {
            '@id': 0,
            's': 0,
            't': 1,
            'i': 'neighbor-of'
        }),
                         list(net_roundtrip.get_edges())[0])

        for node_id, node_obj in net_roundtrip.get_nodes():
            if node_id == 0:
                self.assertEqual({
                    '@id': 0,
                    'n': 'Node 1',
                    'r': 'Node 1'
                }, node_obj)
            elif node_id == 1:
                self.assertEqual({
                    '@id': 1,
                    'n': 'Node 2',
                    'r': 'Node 2'
                }, node_obj)
            else:
                self.fail('Invalid node: ' + str(node_obj))
    def test_create_from_tsv_manipulate_and_save(self):
        print('Testing: mgdb_mutations.txt')
        path_to_network = os.path.join(path_this, 'mgdb_mutations.txt')

        with open(path_to_network, 'r') as tsvfile:
            header = [h.strip() for h in tsvfile.readline().split('\t')]

            df = pd.read_csv(tsvfile,delimiter='\t',engine='python',names=header)

            niceCx = ndex2.create_nice_cx_from_pandas(df, source_field='CDS Mutation', target_field='Gene Symbol', source_node_attr=['Primary Tissue', 'Histology', 'Genomic Locus'], target_node_attr=['Gene ID'], edge_interaction='variant-gene-relationship') #NiceCXNetwork()

            nice_networkx = niceCx.to_networkx()
            nice_pandas = niceCx.to_pandas_dataframe()
            my_csv = nice_pandas.to_csv(sep='\t')

            with open("pandas_to_cx_to_tsv_results.txt", "w") as text_file:
                text_file.write(my_csv)

            niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx)

            # Restore template
            niceCx_from_netx.apply_template('ndexbio.org', '2e8f9bdc-1e5f-11e8-b939-0ac135e8bacf')
            niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

            upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
Example #7
0
    def test_netx_plot(self):
        print('Testing: Get user networks (user: scratch) and networkx building')
        my_ndex=ndex2.client.Ndex2('http://test.ndexbio.org', 'scratch', 'scratch')
        my_ndex.update_status()

        test1 = my_ndex.get_network_ids_for_user('scratch')

        #nx_my_graph = nx.read_edgelist("edge_list_network_adrian.txt", nodetype=str)

        G = nx.Graph()
        G.add_node('ABC')
        G.add_node('DEF')
        G.add_node('GHI')
        G.add_node('JKL')
        G.add_node('MNO')
        G.add_node('PQR')
        G.add_node('XYZ')
        G.add_edges_from([('ABC','DEF'), ('DEF', 'GHI'),('GHI', 'JKL'),
                          ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'),
                         ('MNO', 'XYZ'), ('DEF', 'PQR')])

        niceCx_full = ndex2.create_nice_cx_from_networkx(G)
        niceCx_full_networkx = niceCx_full.to_networkx()

        names = nx.get_node_attributes(niceCx_full_networkx, 'name')
        for n in niceCx_full_networkx.nodes():
            print(n)
        print(niceCx_full_networkx.nodes)
        niceCx_full.upload_to(upload_server, upload_username, upload_password)
        print(names)
Example #8
0
    def to_ndex(self,
                name="subgraph",
                server="http://test.ndexbio.org",
                username="******",
                password="******"):
        """Uploads graph to NDEx
        Parameters
        ----------
        name : str
            The key in self.graphs that contains the graph
        server: str
            The NDEx server hyperlink
        username : str
            Username of the NDEx account
        password : str
            Password of the NDEx account
        """

        try:
            g = ndex2.create_nice_cx_from_networkx(self.network)
        except KeyError:
            raise KeyError("%s is not in self.graphs dictionary!" % name)

        uuid = g.upload_to(server=server, username=username, password=password)

        return uuid
Example #9
0
    def publish(self, name, graph):
        """ Save a networkx graph to NDEx. """
        assert name, "A name for the network is required."
        """ Serialize node and edge python objects. """
        g = nx.MultiDiGraph()
        print(f"{json.dumps (graph, indent=2)}")

        jsonpath_query = parse("$.[*].node_list.[*].[*]")
        nodes = [match.value for match in jsonpath_query.find(graph)]
        print(f"{json.dumps(nodes, indent=2)}")

        jsonpath_query = parse("$.[*].edge_list.[*].[*]")
        edges = [match.value for match in jsonpath_query.find(graph)]
        print(f"{json.dumps(edges, indent=2)}")

        for n in nodes:
            g.add_node(n['id'], attr_dict=n)
        for e in edges:
            print(f"  s: {json.dumps(e,indent=2)}")
            g.add_edge(e['source_id'], e['target_id'], attr_dict=e)
        """ Convert to CX network. """
        nice_cx = create_nice_cx_from_networkx(g)
        nice_cx.set_name(name)
        print(f" connected: edges: {len(g.edges())} nodes: {len(g.nodes())}")
        print(nice_cx)
        """ Upload to NDEx. """
        upload_message = nice_cx.upload_to(self.uri, self.account,
                                           self.password)
Example #10
0
    def _publish(self, name, graph):
        """ Save a graph to NDEx. First, validate input. """
        assert name, "Missing required network name."
        assert graph, "Graph must be non null. "
        assert len(graph) > 0, "Graph may not be empty."
        """ TODO: Use graph tools to_nx instead. """
        """ Select pieces of interest. """
        """ May require different handling if this needs to run as a workflow step as opposed to a CLI argument. """
        nodes = graph['nodes'] if 'nodes' in graph\
                else self.jsonkit.select (query="$.[*].node_list.[*].[*]",
                                          graph=graph)
        edges = graph['edges'] if 'edges' in graph \
                else self.jsonkit.select (query="$.[*].edge_list.[*].[*]",
                                          graph=graph)
        """ Create the NetworkX graph. """
        g = nx.MultiDiGraph()
        for n in nodes:
            g.add_node(n['id'], attr_dict=n)
        for e in edges:
            g.add_edge(e['source_id'], e['target_id'], attr_dict=e)

        assert len(g.nodes()) > 0, "Cannot save empty graph."
        logger.debug(
            f" connected: edges: {len(g.edges())} nodes: {len(g.nodes())}")
        """ Convert to CX network. """
        nice_cx = create_nice_cx_from_networkx(g)
        nice_cx.set_name(name)
        """ Upload to NDEx. """
        upload_message = nice_cx.upload_to(self.uri, self.account,
                                           self.password)
        logger.debug("Upload to NDEx complete.")
        logger.debug(f"upload message {upload_message}")
Example #11
0
    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_create_from_server_manipulate_and_save2(self):
        print('public network')
        niceCx = ndex2.create_nice_cx_from_server(server='public.ndexbio.org', uuid='21106ea7-cbba-11e7-ad58-0ac135e8bacf')

        nice_networkx = niceCx.to_networkx()

        niceCx_from_netx   = ndex2.create_nice_cx_from_networkx(nice_networkx)

        # Restore template
        niceCx_from_netx.apply_template('public.ndexbio.org', '72ef5c3a-caff-11e7-ad58-0ac135e8bacf')
        niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

        upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
    def test_cx_file_with_position(self):
        path_to_network = os.path.join(path_this, 'network_with_position.cx')

        with open(path_to_network, 'rU') as ras_cx:
            #====================================
            # BUILD NICECX FROM PANDAS DATAFRAME
            #====================================
            niceCx = ndex2.create_nice_cx_from_cx(cx=json.load(ras_cx)) #NiceCXNetwork(cx=json.load(ras_cx))
            nice_networkx = niceCx.to_networkx()
            #my_cx = niceCx.to_cx()
            niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx)
            #print(nice_networkx)
            upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
Example #14
0
    def test_create_from_server_manipulate_and_save(self):
        print('Testing: Create from server and manupulate (uuid:51247435-1e5f-11e8-b939-0ac135e8bacf)')
        niceCx = ndex2.create_nice_cx_from_server(server='public.ndexbio.org', uuid='51247435-1e5f-11e8-b939-0ac135e8bacf')

        nice_networkx = niceCx.to_networkx()

        niceCx_from_netx = ndex2.create_nice_cx_from_networkx(nice_networkx)

        # Restore template
        niceCx_from_netx.apply_template('public.ndexbio.org', '51247435-1e5f-11e8-b939-0ac135e8bacf')
        niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

        upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
Example #15
0
    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
Example #16
0
    def test_create_from_server_manipulate_and_save2(self):
        print('Testing: Create from server and generate networkx (uuid:51247435-1e5f-11e8-b939-0ac135e8bacf)')
        niceCx = ndex2.create_nice_cx_from_server(server='public.ndexbio.org', uuid='51247435-1e5f-11e8-b939-0ac135e8bacf')

        #serialized = pickle.dumps(niceCx.to_cx(), protocol=0)
        #print('Serialized memory:', sys.getsizeof(serialized))

        nice_networkx = niceCx.to_networkx()

        niceCx_from_netx   = ndex2.create_nice_cx_from_networkx(nice_networkx)

        # Restore template
        niceCx_from_netx.apply_template('public.ndexbio.org', '51247435-1e5f-11e8-b939-0ac135e8bacf')
        niceCx_from_netx.set_name('Round trip from server to networkx to NDEx')

        print(niceCx_from_netx)

        upload_message = niceCx_from_netx.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
Example #17
0
 def save_nx_graph(self, name, graph):
     """ Save a networkx graph to NDEx. """
     assert name, "A name for the network is required."
     """ Serialize node and edge python objects. """
     g = nx.MultiDiGraph()
     nodes = {n.id: i for i, n in enumerate(graph.nodes())}
     for n in graph.nodes():
         g.add_node(n.id, attr_dict=n.n2json())
     for e in graph.edges(data=True):
         edge = e[2]['object']
         g.add_edge(edge.source_id,
                    edge.target_id,
                    attr_dict=e[2]['object'].e2json())
     """ Convert to CX network. """
     nice_cx = create_nice_cx_from_networkx(g)
     nice_cx.set_name(name)
     print(
         f" connected: {nx.is_connected(graph.to_undirected())} edges: {len(graph.edges())} nodes: {len(graph.nodes())}"
     )
     print(nice_cx)
     """ Upload to NDEx. """
     upload_message = nice_cx.upload_to(self.uri, self.account,
                                        self.password)
    def test_create_from_networkx(self):
        print('Testing: SIMPLE3.txt with DictReader')
        path_to_network = os.path.join(path_this, 'SIMPLE3.txt')

        with open(path_to_network, 'r') as tsvfile:
            reader = csv.DictReader(filter(lambda row: row[0] != '#', tsvfile), dialect='excel-tab', fieldnames=['s','t','e'])

            #===========================
            # BUILD NETWORKX GRAPH
            #===========================
            G = nx.Graph(name='loaded from Simple3.txt')
            for row in reader:
                G.add_node(row.get('s'), test1='test1_s', test2='test2_s')
                G.add_node(row.get('t'), test1='test1_t', test2='test2_t')
                G.add_edges_from([(row.get('s'), row.get('t'), {'interaction': 'controls-production-of', 'test3': 'test3',
                                                        'non-number': np.nan})])

            #====================================
            # BUILD NICECX FROM NETWORKX GRAPH
            #====================================
            niceCx = ndex2.create_nice_cx_from_networkx(G) #NiceCXNetwork(networkx_G=G)
            niceCx.apply_template('public.ndexbio.org', '51247435-1e5f-11e8-b939-0ac135e8bacf')
            upload_message = niceCx.upload_to(upload_server, upload_username, upload_password)
            self.assertTrue(upload_message)
Example #19
0
    def test_data_types_from_networkx(self):
        self.assertFalse(upload_username == 'username')
        G = nx.Graph()
        G.add_node('ABC')
        G.add_node('DEF')
        G.add_node('GHI')
        G.add_node('JKL')
        G.add_node('MNO')
        G.add_node('PQR')
        G.add_node('XYZ')

        G.add_edges_from([('ABC', 'DEF'), ('DEF', 'GHI'), ('GHI', 'JKL'),
                          ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'),
                          ('MNO', 'XYZ'), ('DEF', 'PQR')])

        G['ABC']['DEF']['ABC'] = 1
        G['DEF']['GHI']['DEF'] = 2
        G['GHI']['JKL']['GHI'] = 3
        G['MNO']['XYZ']['JKL'] = 4
        G['MNO']['XYZ']['MNO'] = 5

        G['ABC']['DEF']['weight'] = 0.321
        G['DEF']['GHI']['weight'] = 0.434
        G['GHI']['JKL']['weight'] = 0.555
        G['MNO']['XYZ']['weight'] = 0.987

        G.nodes['ABC']['attr1'] = 1
        G.nodes['DEF']['attr1'] = 1
        G.nodes['GHI']['attr1'] = 1
        G.nodes['JKL']['attr1'] = 1
        G.nodes['MNO']['attr1'] = 1
        G.nodes['PQR']['attr1'] = 1
        G.nodes['XYZ']['attr1'] = 1

        G.nodes['ABC']['attr2'] = 0.1
        G.nodes['DEF']['attr2'] = 0.1
        G.nodes['GHI']['attr2'] = 0.1
        G.nodes['JKL']['attr2'] = 0.1
        G.nodes['MNO']['attr2'] = 0.1
        G.nodes['PQR']['attr2'] = 0.1
        G.nodes['XYZ']['attr2'] = 0.1

        niceCx_full = ndex2.create_nice_cx_from_networkx(G)

        abc_node_attrs = niceCx_full.get_node_attributes(0)

        found_int_type = False
        found_float_type = False
        if abc_node_attrs is not None:
            for node_attr in abc_node_attrs:
                if node_attr.get('d') == 'integer':
                    found_int_type = True
                if node_attr.get('d') == 'double':
                    found_float_type = True

        found_edge_float_type = False
        for id, edge in niceCx_full.get_edges():
            edge_attrs = niceCx_full.get_edge_attributes(edge)
            if edge_attrs is not None:
                for edge_attr in edge_attrs:
                    if edge_attr.get('d') == 'double':
                        found_edge_float_type = True

        self.assertTrue(found_int_type)
        self.assertTrue(found_float_type)
        self.assertTrue(found_edge_float_type)
Example #20
0
def process(node_list, edge_list, load_plan, network_name, username, password,
            server):
    try:
        here = path.abspath(path.dirname(__file__))

        with open(path.join(here, load_plan), 'r') as lp:
            load_plan = json.load(lp)

        with open(path.join(here, 'loading_plan_schema.json')) as schema_file:
            plan_schema = json.load(schema_file)

        validate(load_plan, plan_schema)

        node_id = load_plan.get('source_plan').get('rep_column')
        node_name = load_plan.get('source_plan').get('node_name_column')
        edge_source = load_plan.get('edge_plan').get('edge_source')
        edge_target = load_plan.get('edge_plan').get('edge_target')
        predicate_id_column = load_plan.get('edge_plan').get(
            'predicate_id_column')
        default_edge_interaction = load_plan.get('edge_plan').get(
            'default_predicate')

    except jsonschema.ValidationError as e1:
        print("Failed to parse the loading plan: " + e1.message)
        print('at path: ' + str(e1.absolute_path))
        print("in block: ")
        print(e1.instance)

    except Exception as ex1:
        print("Failed to parse the loading plan: ")

    G = nx.Graph(name=network_name)

    # ==============================
    # ADD NODES TO NETWORKX GRAPH
    # ==============================
    node_label_id_map = {}

    for n in node_list:
        n_attrs = {}

        node_label_id_map[n.get(node_id)] = n.get(node_name)
        for k, v in n.items():
            if k != node_id and k != node_name:
                n_attrs[k] = v
        G.add_node(n.get(node_id), n_attrs)

    # ==============================
    # ADD EDGES TO NETWORKX GRAPH
    # ==============================
    for e in edge_list:
        e_attrs = {}
        interaction_found = False
        for k, v in e.items():
            if k != edge_source and k != edge_target and k != 'id':
                e_attrs[k] = v
            if predicate_id_column is not None and k == predicate_id_column:
                interaction_found = True
                set_edge_interaction_map(e.get(edge_source),
                                         e.get(edge_target),
                                         e.get(predicate_id_column))

                #if edge_interaction_map.get(e.get(edge_source)) is None:
                #    edge_interaction_map[e.get(edge_source)] = {e.get(edge_target): e.get(predicate_id_column)}
                #else:
                #    edge_interaction_map[e.get(edge_source)][e.get(edge_target)] = e.get(predicate_id_column)

                #if edge_interaction_map.get(e.get(edge_target)) is None:
                #    edge_interaction_map[e.get(edge_target)] = {e.get(edge_source): e.get(predicate_id_column)}
                #else:
                #    edge_interaction_map[e.get(edge_target)][e.get(edge_source)] = e.get(predicate_id_column)

        if not interaction_found and default_edge_interaction is not None:
            set_edge_interaction_map(e.get(edge_source), e.get(edge_target),
                                     default_edge_interaction)

        G.add_edge(e.get(edge_source), e.get(edge_target), e_attrs)

    # ======================
    # RUN NETWORKX LAYOUT
    # ======================
    G.pos = nx.drawing.spring_layout(G, iterations=20, weight='weight')

    # =========================
    # CREATE CX FROM NETWORKX
    # =========================
    niceCx = ndex2.create_nice_cx_from_networkx(G)
    niceCx.apply_template('public.ndexbio.org',
                          '84d64a82-23bc-11e8-b939-0ac135e8bacf')

    # =========================
    # POST-PROCESS NODES
    # SET NODE NAME TO LABEL
    # =========================
    for k, v in niceCx.nodes.items():
        v.set_node_represents(v.get_name())
        v.set_node_name(node_label_id_map.get(v.get_name()))

    for k, v in niceCx.edges.items():
        if predicate_id_column is None:
            v.set_interaction(default_edge_interaction)
        else:
            v.set_interaction(
                edge_interaction_map.get(v.get_source()).get(v.get_target()))

    # =======================
    # UPLOAD TO NDEX SERVER
    # =======================
    if username is not None and password is not None and server is not None:
        message = niceCx.upload_to(server, username, password)
        if 'error' not in message:
            network_uuid = message.split('/')[-1]

            my_ndex = nc.Ndex2(server, username, password)
            my_ndex._make_network_public_indexed(network_uuid)

        return message

    return None
Example #21
0
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)

# #### *ndex2.create_nice_cx_from_pandas(dataframe)

# In[ ]:

#create an Empty Pandas Dataframe
df = pd.DataFrame()
nice_cx_from_pandas = ndex2.create_nice_cx_from_pandas(df)

# #### *ndex2.create_nice_cx_from_server(server, username=None, password=None, uuid=None)

# In[ ]:

uuid = "1c69beff-1229-11e6-a039-06603eb7f303"
nice_cx_from_server = ndex2.create_nice_cx_from_server(
Example #22
0
    def test_data_types_from_networkx(self):
        self.assertFalse(upload_username == 'username')
        G = nx.Graph()
        G.add_node('ABC')
        G.add_node('DEF')
        G.add_node('GHI')
        G.add_node('JKL')
        G.add_node('MNO')
        G.add_node('PQR')
        G.add_node('XYZ')

        G.add_edges_from([('ABC','DEF'), ('DEF', 'GHI'),('GHI', 'JKL'),
                          ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'),
                         ('MNO', 'XYZ'), ('DEF', 'PQR')])

        G['ABC']['DEF']['ABC'] = 1
        G['DEF']['GHI']['DEF'] = 2
        G['GHI']['JKL']['GHI'] = 3
        G['MNO']['XYZ']['JKL'] = 4
        G['MNO']['XYZ']['MNO'] = 5

        G['ABC']['DEF']['weight'] = 0.321
        G['DEF']['GHI']['weight'] = 0.434
        G['GHI']['JKL']['weight'] = 0.555
        G['MNO']['XYZ']['weight'] = 0.987

        G.nodes['ABC']['attr1'] = 1
        G.nodes['DEF']['attr1'] = 1
        G.nodes['GHI']['attr1'] = 1
        G.nodes['JKL']['attr1'] = 1
        G.nodes['MNO']['attr1'] = 1
        G.nodes['PQR']['attr1'] = 1
        G.nodes['XYZ']['attr1'] = 1

        G.nodes['ABC']['attr2'] = 0.1
        G.nodes['DEF']['attr2'] = 0.1
        G.nodes['GHI']['attr2'] = 0.1
        G.nodes['JKL']['attr2'] = 0.1
        G.nodes['MNO']['attr2'] = 0.1
        G.nodes['PQR']['attr2'] = 0.1
        G.nodes['XYZ']['attr2'] = 0.1

        niceCx_full = ndex2.create_nice_cx_from_networkx(G)

        abc_node_attrs = niceCx_full.get_node_attributes(0)

        found_int_type = False
        found_float_type = False
        if abc_node_attrs is not None:
            for node_attr in abc_node_attrs:
                if node_attr.get('d') == 'integer':
                    found_int_type = True
                if node_attr.get('d') == 'double':
                    found_float_type = True

        found_edge_float_type = False
        for id, edge in niceCx_full.get_edges():
            edge_attrs = niceCx_full.get_edge_attributes(edge)
            if edge_attrs is not None:
                for edge_attr in edge_attrs:
                    if edge_attr.get('d') == 'double':
                        found_edge_float_type = True

        self.assertTrue(found_int_type)
        self.assertTrue(found_float_type)
        self.assertTrue(found_edge_float_type)
Example #23
0
    def test_create_nice_cx_from_networkx_roundtrip_glypican(self):

        net = ndex2.create_nice_cx_from_file(
            TestCreateNiceCXNetworkFromNetworkX.GLYPICAN_FILE)
        netx_net = net.to_networkx(mode='default')
        net_roundtrip = ndex2.create_nice_cx_from_networkx(netx_net)
        self.assertEqual(2, len(net_roundtrip.get_nodes()))
        self.assertEqual(1, len(net_roundtrip.get_edges()))
        self.assertEqual('Glypican 2 network', net_roundtrip.get_name())
        edge = net_roundtrip.get_edge(0)
        self.assertEqual({
            '@id': 0,
            's': 1,
            't': 0,
            'i': 'in-complex-with'
        }, edge)
        e_a = net_roundtrip.get_edge_attribute(edge['@id'], 'directed')

        # currently DefaultNetworkXFactory blindly
        # migrates the value from NiceCXNetwork to
        # networkx, since values are strings that is
        # what the value is set to so during roundtrip
        # a string is set as the datatype
        self.assertEqual(
            {
                'po': 0,
                'n': 'directed',
                'v': 'false',
                'd': 'string'
            }, e_a)

        n_a = net_roundtrip.get_node_attribute(0, 'type')
        self.assertEqual({
            'po': 0,
            'n': 'type',
            'v': 'Protein',
            'd': 'string'
        }, n_a)
        n_a = net_roundtrip.get_node_attribute(1, 'type')
        self.assertEqual({
            'po': 1,
            'n': 'type',
            'v': 'Protein',
            'd': 'string'
        }, n_a)

        n_a = net_roundtrip.get_node_attribute(0, 'alias')
        self.assertEqual(
            {
                "po":
                0,
                "n":
                "alias",
                "v": [
                    "uniprot knowledgebase:Q2LEK4",
                    "uniprot knowledgebase:Q9UCC7"
                ],
                "d":
                "list_of_string"
            }, n_a)

        n_a = net_roundtrip.get_node_attribute(1, 'alias')
        self.assertEqual(
            {
                "po": 1,
                "n": "alias",
                "v": ["uniprot knowledgebase:Q8N158"],
                "d": "list_of_string"
            }, n_a)

        cart = net_roundtrip.get_opaque_aspect(
            ndex2.constants.CARTESIAN_LAYOUT_ASPECT)

        self.assertEqual(2, len(cart))
        self.assertEqual(0, cart[0]['node'])
        self.assertEqual(-398.3511334928659, cart[0]['x'])
        self.assertEqual(70.71067799518471, cart[0]['y'])

        self.assertEqual(1, cart[1]['node'])
        self.assertEqual(-353.49370090105185, cart[1]['x'])
        self.assertEqual(70.71067822788493, cart[1]['y'])
Example #24
0
 def test_create_nice_cx_from_networkx_with_none(self):
     try:
         ndex2.create_nice_cx_from_networkx(None)
         self.fail('Expected Exception')
     except Exception as e:
         self.assertEqual('Networkx input is empty', str(e))
Example #25
0
 def test_create_nice_cx_from_networkx_with_invalidstr(self):
     try:
         ndex2.create_nice_cx_from_networkx('invalid')
         self.fail('Expected Exception')
     except AttributeError as e:
         self.assertTrue('object has no' in str(e))
Example #26
0
def process_rtx(rtx_json, network_name, username, password, server):
    G = nx.Graph(name=network_name)

    # ===========================
    # CHECK FOR REQUIRED FIELDS
    # ===========================
    result_graph = rtx_json.get('result_graph')
    if result_graph is not None:
        n_data = result_graph.get('node_list')
        e_data = result_graph.get('edge_list')
    else:
        raise Exception('ERROR: No result_graph found in input file')

    # ==============================
    # ADD NODES TO NETWORKX GRAPH
    # ==============================
    node_type_dict = {}
    node_label_id_map = {}
    edge_interaction_map = {}
    layout_nlist = []
    layout_level1_nlist = []
    layout_level2_nlist = []

    layout_other_nlist = []
    for nds in n_data:
        if nds.get('id') is not None and nds.get('type') is not None:
            node_type_dict[nds.get('id')] = nds.get('type')

    for n in n_data:
        n_attrs = {}

        node_label_id_map[n.get('id')] = n.get('name')
        for k, v in n.items():
            if k != 'id' and k != 'name':
                n_attrs[k] = v
        G.add_node(n.get('id'), n_attrs)

        # Shell layout setup
        if len(layout_level1_nlist) < 25:
            layout_level1_nlist.append(n.get('id'))
        elif len(layout_level2_nlist) < 50:
            layout_level2_nlist.append(n.get('id'))
        else:
            layout_other_nlist.append(n.get('id'))

    layout_nlist.append(layout_level1_nlist)
    layout_nlist.append(layout_level2_nlist)
    layout_nlist.append(layout_other_nlist)
    # ==============================
    # ADD EDGES TO NETWORKX GRAPH
    # ==============================
    edge_type_dict = {}
    if e_data is not None:
        for eds in e_data:
            if eds.get('name') is not None and eds.get('type') is not None:
                edge_type_dict[eds.get('name')] = eds.get('type')

        for e in e_data:
            #print(e)
            e_attrs = {}
            for k, v in e.items():
                if k != 'source_id' and k != 'target_id' and k != 'id':
                    e_attrs[k] = v
                if k == 'type':
                    if edge_interaction_map.get(e.get('source_id')) is None:
                        edge_interaction_map[e.get('source_id')] = {
                            e.get('target_id'): e.get('type')
                        }
                    else:
                        edge_interaction_map[e.get('source_id')][e.get(
                            'target_id')] = e.get('type')

                    if edge_interaction_map.get(e.get('target_id')) is None:
                        edge_interaction_map[e.get('target_id')] = {
                            e.get('source_id'): e.get('type')
                        }
                    else:
                        edge_interaction_map[e.get('target_id')][e.get(
                            'source_id')] = e.get('type')

            G.add_edge(e.get('source_id'), e.get('target_id'), e_attrs)
    else:
        n1 = G.nodes()[0]
        G.add_edge(n1, n1, {})
        edge_interaction_map[n1] = {n1: 'self-ref'}

    # ======================
    # RUN NETWORKX LAYOUT
    # ======================
    init_pos = nx.drawing.circular_layout(G)

    G.pos = nx.drawing.spring_layout(G,
                                     iterations=20,
                                     pos=init_pos,
                                     weight='weight')

    # G.pos = nx.drawing.shell_layout(G, nlist=layout_nlist)

    # G.pos = nx.drawing.spring_layout(G, weight='weight')

    # =========================
    # CREATE CX FROM NETWORKX
    # =========================
    niceCx = ndex2.create_nice_cx_from_networkx(G)
    niceCx.apply_template('public.ndexbio.org',
                          '84d64a82-23bc-11e8-b939-0ac135e8bacf')

    # =========================
    # SET NODE NAME TO LABEL
    # =========================
    for k, v in niceCx.nodes.items():
        v.set_node_represents(v.get_name())
        v.set_node_name(node_label_id_map.get(v.get_name()))

    for k, v in niceCx.edges.items():
        v.set_interaction(
            edge_interaction_map.get(v.get_source()).get(v.get_target()))

    # =======================
    # UPLOAD TO NDEX SERVER
    # =======================
    if username is not None and password is not None and server is not None:
        message = niceCx.upload_to(server, username, password)

        network_uuid = message.split('/')[-1]

        my_ndex = nc.Ndex2(server, username, password)
        if 'error' not in message:
            my_ndex._make_network_public_indexed(network_uuid)

        return message

    return None
Example #27
0
    def test_create_nice_cx_from_networkx_roundtrip_with_attrs(self):
        net = ndex2.nice_cx_network.NiceCXNetwork()
        node_one = net.create_node('Node 1')
        node_two = net.create_node('Node 2')
        net.set_node_attribute(node_one, 'somestrfield', 'hi')

        edge_one = net.create_edge(edge_source=node_one,
                                   edge_target=node_two,
                                   edge_interaction='breaks')
        net.set_edge_attribute(edge_one, 'somestrfield', 'bye')
        net.set_opaque_aspect(ndex2.constants.CARTESIAN_LAYOUT_ASPECT,
                              [{
                                  'node': node_one,
                                  'x': 1.0,
                                  'y': 2.0
                              }, {
                                  'node': node_two,
                                  'x': 3.0,
                                  'y': 4.0
                              }])

        net.set_name('mynetwork')
        netx_net = net.to_networkx(mode='default')
        net_roundtrip = ndex2.create_nice_cx_from_networkx(netx_net)

        self.assertEqual(netx_net.pos[0], (1.0, -2.0))
        self.assertEqual(netx_net.pos[1], (3.0, -4.0))

        self.assertEqual('mynetwork', net_roundtrip.get_name())
        self.assertEqual(1, len(net_roundtrip.get_edges()))
        self.assertEqual(2, len(net_roundtrip.get_nodes()))

        self.assertEqual((0, {
            '@id': 0,
            's': 0,
            't': 1,
            'i': 'breaks'
        }),
                         list(net_roundtrip.get_edges())[0])

        for node_id, node_obj in net_roundtrip.get_nodes():
            if node_id == 0:
                self.assertEqual({
                    '@id': 0,
                    'n': 'Node 1',
                    'r': 'Node 1'
                }, node_obj)
            elif node_id == 1:
                self.assertEqual({
                    '@id': 1,
                    'n': 'Node 2',
                    'r': 'Node 2'
                }, node_obj)
            else:
                self.fail('Invalid node: ' + str(node_obj))

        n_a = net_roundtrip.get_node_attribute(node_one, 'somestrfield')
        self.assertEqual(
            {
                'po': 0,
                'n': 'somestrfield',
                'v': 'hi',
                'd': 'string'
            }, n_a)

        n_a = net_roundtrip.get_edge_attribute(node_one, 'somestrfield')
        self.assertEqual(
            {
                'po': 0,
                'n': 'somestrfield',
                'v': 'bye',
                'd': 'string'
            }, n_a)
Example #28
0
def process_tcpa(file_name):
    tcpa_file_path = path.join(current_directory, file_name)
    if path.isfile(tcpa_file_path):
        with open(tcpa_file_path, 'r') as tfp:
            tcpa_json = json.load(tfp)

            G = nx.Graph(name=file_name.replace('.json', ''))

            #===========================
            # CHECK FOR REQUIRED FIELDS
            #===========================
            data_schema = tcpa_json.get('dataSchema')
            if data_schema is not None:
                n_data_schema = data_schema.get('nodes')
                e_data_schema = data_schema.get('edges')
            else:
                raise Exception('ERROR: No data schema found in input file')

            data = tcpa_json.get('data')

            if data is None:
                raise Exception('ERROR: No data found in input file')

            #==============================
            # ADD NODES TO NETWORKX GRAPH
            #==============================
            node_type_dict = {}
            node_label_id_map = {}
            layout_nlist = []
            layout_level1_nlist = []
            layout_level2_nlist = []
            layout_level3_nlist = []
            layout_other_nlist = []
            for nds in n_data_schema:
                if nds.get('name') is not None and nds.get('type') is not None:
                    node_type_dict[nds.get('name')] = nds.get('type')

            for n in data.get('nodes'):
                print(n)
                n_attrs = {}
                node_label_id_map[n.get('id')] = n.get('label')
                for k, v in n.items():
                    if k != 'id':
                        n_attrs[k] = v
                G.add_node(n.get('id'), n_attrs)

                # Shell layout setup
                if len(layout_level1_nlist) < 25:
                    layout_level1_nlist.append(n.get('id'))
                elif len(layout_level2_nlist) < 50:
                    layout_level2_nlist.append(n.get('id'))
                else:
                    layout_other_nlist.append(n.get('id'))

            layout_nlist.append(layout_level1_nlist)
            layout_nlist.append(layout_level2_nlist)
            layout_nlist.append(layout_other_nlist)
            #==============================
            # ADD EDGES TO NETWORKX GRAPH
            #==============================
            edge_type_dict = {}
            for eds in e_data_schema:
                if eds.get('name') is not None and eds.get('type') is not None:
                    edge_type_dict[eds.get('name')] = eds.get('type')

            for e in data.get('edges'):
                print(e)
                e_attrs = {}
                for k, v in e.items():
                    if k != 'source' and k != 'target' and k != 'id':
                        e_attrs[k] = v
                G.add_edge(e.get('source'), e.get('target'), e_attrs)

            #======================
            # RUN NETWORKX LAYOUT
            #======================
            init_pos = nx.drawing.circular_layout(G)

            G.pos = nx.drawing.spring_layout(G,
                                             iterations=20,
                                             pos=init_pos,
                                             weight='weight')

            G.pos = nx.drawing.shell_layout(G, nlist=layout_nlist)

            #G.pos = nx.drawing.spring_layout(G, weight='weight')

            #=========================
            # CREATE CX FROM NETWORKX
            #=========================
            niceCx = ndex2.create_nice_cx_from_networkx(G)
            niceCx.apply_template('public.ndexbio.org',
                                  '84d64a82-23bc-11e8-b939-0ac135e8bacf')

            #=========================
            # SET NODE NAME TO LABEL
            #=========================
            for k, v in niceCx.nodes.items():
                v.set_node_name(node_label_id_map.get(v.get_name()))

            #=======================
            # UPLOAD TO NDEX SERVER
            #=======================
            if upload_username is not None and upload_password is not None and upload_server is not None:
                niceCx.upload_to(upload_server, upload_username,
                                 upload_password)

            with open(data_file.replace('.json', '.cx'), 'w') as outfile:
                json.dump(niceCx.to_cx(), outfile)
    else:
        raise Exception('ERROR: File does not exist ' + data_file)
Example #29
0
    def test_data_types_from_networkx(self):
        G = nx.Graph()
        G.add_node('ABC')
        G.add_node('DEF')
        G.add_node('GHI')
        G.add_node('JKL')
        G.add_node('MNO')
        G.add_node('PQR')
        G.add_node('XYZ')

        G.add_edges_from([('ABC', 'DEF'), ('DEF', 'GHI'), ('GHI', 'JKL'),
                          ('DEF', 'JKL'), ('JKL', 'MNO'), ('DEF', 'MNO'),
                          ('MNO', 'XYZ'), ('DEF', 'PQR')])

        attribute_floats = {
            'ABC': 1.234,
            'DEF': 1.345,
            'GHI': 1.456,
            'JKL': 1.567,
            'MNO': 1.678
        }

        attribute_integers = {'ABC': 1, 'DEF': 2, 'GHI': 3, 'JKL': 4, 'MNO': 5}

        nx.set_node_attributes(G, 'floatLabels', attribute_floats)
        nx.set_node_attributes(G, 'integerLabels', attribute_integers)

        G['ABC']['DEF']['weight'] = 0.321
        G['DEF']['GHI']['weight'] = 0.434
        G['GHI']['JKL']['weight'] = 0.555
        G['MNO']['XYZ']['weight'] = 0.987

        niceCx_full = ndex2.create_nice_cx_from_networkx(G)
        context = [{
            'ncbigene': 'http://identifiers.org/ncbigene/',
            'hgnc.symbol': 'http://identifiers.org/hgnc.symbol/',
            'uniprot': 'http://identifiers.org/uniprot/'
        }]
        niceCx_full.set_context(context)

        #print(niceCx_full.__str__())

        abc_node_attrs = niceCx_full.get_node_attributes('ABC')

        found_int_type = False
        found_float_type = False
        if abc_node_attrs is not None:
            for node_attr in abc_node_attrs:
                if node_attr.get_data_type() == ATTRIBUTE_DATA_TYPE.INTEGER:
                    found_int_type = True
                if node_attr.get_data_type() == ATTRIBUTE_DATA_TYPE.FLOAT:
                    found_float_type = True

        found_edge_float_type = False
        for id, edge in niceCx_full.get_edges():
            edge_attrs = niceCx_full.get_edge_attributes(edge)
            if edge_attrs is not None:
                for edge_attr in edge_attrs:
                    if edge_attr.get_data_type() == ATTRIBUTE_DATA_TYPE.FLOAT:
                        found_edge_float_type = True

        self.assertTrue(found_int_type)
        self.assertTrue(found_float_type)
        self.assertTrue(found_edge_float_type)