Пример #1
0
 def get(self, name):
     """
     Return the dag of a pipeline
     """
     cy_network = cy.from_networkx(
         Pipeline.create_dag(pipeline_specs[name]))
     return cy_network
Пример #2
0
    def test_networkx_matrix(self):
        print('\n---------- Matrix Test Start -----------\n')

        g = nx.barabasi_albert_graph(30, 2)
        nodes = g.nodes()
        edges = g.edges()
        print(edges)

        mx1 = nx.adjacency_matrix(g)
        fp = tempfile.NamedTemporaryFile()
        file_name = fp.name
        sp.savetxt(file_name, mx1.toarray(), fmt='%d')

        # Load it back to matrix
        mx2 = sp.loadtxt(file_name)
        fp.close()

        g2 = nx.from_numpy_matrix(mx2)
        cyjs_g = util.from_networkx(g2)

        #print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual(len(nodes), len(cyjs_g['elements']['nodes']))
        self.assertEqual(len(edges), len(cyjs_g['elements']['edges']))

        # Make sure all edges are reproduced
        print(set(edges))
        diff = compare_edge_sets(set(edges), cyjs_g['elements']['edges'])
        self.assertEqual(0, len(diff))
Пример #3
0
    def test_networkx_roundtrip(self):
        print('\n---------- NetworkX Data Roundtrip Test Start -----------\n')

        g = nx.newman_watts_strogatz_graph(100, 3, 0.5)
        nodes = g.nodes()
        edges = g.edges()

        # Add some attributes
        g.graph['name'] = 'original'
        g.graph['density'] = nx.density(g)

        nx.set_node_attributes(g, 'betweenness', nx.betweenness_centrality(g))
        nx.set_node_attributes(g, 'degree', nx.degree(g))
        nx.set_node_attributes(g, 'closeness', nx.closeness_centrality(g))

        nx.set_edge_attributes(g, 'eb', nx.edge_betweenness(g))

        cyjs1 = util.from_networkx(g)
        g2 = util.to_networkx(cyjs1)

        self.assertEqual(len(g2.nodes()), len(nodes))
        self.assertEqual(len(g2.edges()), len(edges))

        edge_set = set(list(map(lambda x: (int(x[0]), int(x[1])), g2.edges())))
        self.assertEqual(0, len(edge_set.difference(set(edges))))

        node_original = g.node[1]
        node_generated = g2.node['1']

        print(node_original)
        print(node_generated)

        self.assertEqual(node_original['degree'], node_generated['degree'])
        self.assertEqual(node_original['betweenness'], node_generated['betweenness'])
        self.assertEqual(node_original['closeness'], node_generated['closeness'])
Пример #4
0
    def test_networkx_matrix(self):
        print('\n---------- Matrix Test Start -----------\n')

        g = nx.barabasi_albert_graph(30, 2)
        nodes = g.nodes()
        edges = g.edges()
        print(edges)

        mx1 = nx.adjacency_matrix(g)
        fp = tempfile.NamedTemporaryFile()
        file_name = fp.name
        sp.savetxt(file_name, mx1.toarray(), fmt='%d')

        # Load it back to matrix
        mx2 = sp.loadtxt(file_name)
        fp.close()

        g2 = nx.from_numpy_matrix(mx2)
        cyjs_g = util.from_networkx(g2)

        #print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual(len(nodes), len(cyjs_g['elements']['nodes']))
        self.assertEqual(len(edges), len(cyjs_g['elements']['edges']))

        # Make sure all edges are reproduced
        print(set(edges))
        diff = compare_edge_sets(set(edges), cyjs_g['elements']['edges'])
        self.assertEqual(0, len(diff))
Пример #5
0
    def test_networkx_roundtrip(self):
        print('\n---------- NetworkX Data Roundtrip Test Start -----------\n')

        g = nx.newman_watts_strogatz_graph(100, 3, 0.5)
        nodes = g.nodes()
        edges = g.edges()

        # Add some attributes
        g.graph['name'] = 'original'
        g.graph['density'] = nx.density(g)

        nx.set_node_attributes(g, 'betweenness', nx.betweenness_centrality(g))
        nx.set_node_attributes(g, 'degree', nx.degree(g))
        nx.set_node_attributes(g, 'closeness', nx.closeness_centrality(g))

        nx.set_edge_attributes(g, 'eb', nx.edge_betweenness(g))

        cyjs1 = util.from_networkx(g)
        g2 = util.to_networkx(cyjs1)

        self.assertEqual(len(g2.nodes()), len(nodes))
        self.assertEqual(len(g2.edges()), len(edges))

        edge_set = set(list(map(lambda x: (int(x[0]), int(x[1])), g2.edges())))
        self.assertEqual(0, len(edge_set.difference(set(edges))))

        node_original = g.node[1]
        node_generated = g2.node['1']

        print(node_original)
        print(node_generated)

        self.assertEqual(node_original['degree'], node_generated['degree'])
        self.assertEqual(node_original['betweenness'], node_generated['betweenness'])
        self.assertEqual(node_original['closeness'], node_generated['closeness'])
Пример #6
0
    def test_networkx_roundtrip(self):
        print("\n---------- NetworkX Data Roundtrip Test Start -----------\n")

        g = nx.newman_watts_strogatz_graph(100, 3, 0.5)
        nodes = g.nodes()
        edges = g.edges()

        # Add some attributes
        g.graph["name"] = "original"
        g.graph["density"] = nx.density(g)

        nx.set_node_attributes(g, "betweenness", nx.betweenness_centrality(g))
        nx.set_node_attributes(g, "degree", nx.degree(g))
        nx.set_node_attributes(g, "closeness", nx.closeness_centrality(g))

        nx.set_edge_attributes(g, "eb", nx.edge_betweenness(g))

        cyjs1 = util.from_networkx(g)
        g2 = util.to_networkx(cyjs1)

        self.assertEqual(len(g2.nodes()), len(nodes))
        self.assertEqual(len(g2.edges()), len(edges))

        edge_set = set(list(map(lambda x: (int(x[0]), int(x[1])), g2.edges())))
        self.assertEqual(0, len(edge_set.difference(set(edges))))

        node_original = g.node[1]
        node_generated = g2.node["1"]

        print(node_original)
        print(node_generated)

        self.assertEqual(node_original["degree"], node_generated["degree"])
        self.assertEqual(node_original["betweenness"], node_generated["betweenness"])
        self.assertEqual(node_original["closeness"], node_generated["closeness"])
Пример #7
0
def createNetwork(network,layout='hierarchical'):
    cytoscapeNetwork=util.from_networkx(network)
    res1=requests.post(BASE + 'networks', data=json.dumps(cytoscapeNetwork), headers=HEADERS)
    res1_dict = json.loads(res1.content)
    print res1.content
    #print res1_dict['networkSUID']
    new_suid = res1_dict['networkSUID']
    requests.get(BASE + 'apply/layouts/'+layout+'/' + str(new_suid))
    requests.get(BASE + 'apply/styles/SRALandscape/' + str(new_suid))
Пример #8
0
    def test_networkx_emptynetwork(self):
        g = nx.Graph()
        cyjs_g = util.from_networkx(g)

        print('\n---------- Empty Test Start -----------\n')
        print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual(0, len(cyjs_g['elements']['nodes']))
        self.assertEqual(0, len(cyjs_g['elements']['edges']))
Пример #9
0
    def test_networkx_gml(self):
        g = nx.read_gml('tests/data/galFiltered.gml')
        cyjs_g = util.from_networkx(g)

        print('\n---------- Test Start -----------\n')
        print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual(331, len(cyjs_g['elements']['nodes']))
        self.assertEqual(362, len(cyjs_g['elements']['edges']))
Пример #10
0
    def test_networkx_empty_edge_attribute(self):
        print('\n---------- Edge Att Test 2 Start -----------\n')
        g = nx.scale_free_graph(5)
        cyjs = util.from_networkx(g)

        # print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs['elements']['edges'][0]
        print(json.dumps(edge, indent=4))
        self.assertEqual(3, len(edge['data']))
Пример #11
0
def makeGraph(d):
    global g
    g = nx.Graph(d)
    nx.write_graphml(g, 'g.xml')
    authorMap = from_networkx(g)
    authorNet = requests.post(server + '/networks',
                              data=json.dumps(authorMap),
                              headers={'Content-Type': 'application/json'})
    net_id = authorNet.json()['networkSUID']
    requests.get('%s/apply/layouts/force-directed/%d' % (server, net_id))
    Image('%s/networks/%d/views/first.png' % (server, net_id))
Пример #12
0
    def test_networkx_emptynetwork(self):
        g = nx.Graph()
        cyjs_g = util.from_networkx(g)

        print('\n---------- Empty Test Start -----------\n')
        print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual(0, len(cyjs_g['elements']['nodes']))
        self.assertEqual(0, len(cyjs_g['elements']['edges']))
Пример #13
0
    def test_networkx_emptynetwork(self):
        g = nx.Graph()
        cyjs_g = util.from_networkx(g)

        print("\n---------- Empty Test Start -----------\n")
        print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g["data"])
        self.assertEqual(0, len(cyjs_g["elements"]["nodes"]))
        self.assertEqual(0, len(cyjs_g["elements"]["edges"]))
Пример #14
0
    def test_networkx_empty_edge_attribute(self):
        print('\n---------- Edge Att Test 2 Start -----------\n')
        g = nx.scale_free_graph(5)
        cyjs = util.from_networkx(g)

        # print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs['elements']['edges'][0]
        print(json.dumps(edge, indent=4))
        self.assertEqual(3, len(edge['data']))
Пример #15
0
 def get(self, run_id):
     """
     Return the dag of the given run
     """
     pipeline = db.pipelines.find_one({'run_id': run_id}, {'config':1, 'single_step':1, '_id':0})
     config = json.loads(pipeline['config'])
     if 'single_step' in pipeline:
         dag = Pipeline.create_dag(config, one_step=True)
     else:
         dag = Pipeline.create_dag(config)
     cy_network = cy.from_networkx(dag)
     return cy_network
Пример #16
0
    def test_networkx_digraph_edge_attr(self):
        print('\n---------- Digraph Edge Att Test Start -----------\n')
        g = nx.DiGraph()
        g.add_path([0, 1, 2, 3, 4])
        eb = nx.edge_betweenness(g)
        nx.set_edge_attributes(g, 'eb', eb)
        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs['elements']['edges'][0]
        self.assertEqual(3, len(edge['data']))
Пример #17
0
    def test_networkx_digraph_edge_attr(self):
        print('\n---------- Digraph Edge Att Test Start -----------\n')
        g = nx.DiGraph()
        g.add_path([0, 1, 2, 3, 4])
        eb = nx.edge_betweenness(g)
        nx.set_edge_attributes(g, 'eb', eb)
        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs['elements']['edges'][0]
        self.assertEqual(3, len(edge['data']))
Пример #18
0
    def test_networkx_edge_attribute(self):
        print('\n---------- Edge Att Test Start -----------\n')
        g = nx.Graph()
        g.add_edge(1, 2, interaction='itr1',  score=0.1)
        original_edge = g[1][2]
        print(original_edge.keys())
        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs['elements']['edges'][0]
        print(json.dumps(edge, indent=4))

        self.assertEqual('itr1', cyjs['elements']['edges'][0]['data']['interaction'])
Пример #19
0
    def test_networkx_edge_attribute(self):
        print("\n---------- Edge Att Test Start -----------\n")
        g = nx.Graph()
        g.add_edge(1, 2, interaction="itr1", score=0.1)
        original_edge = g[1][2]
        print(original_edge.keys())
        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs["elements"]["edges"][0]
        print(json.dumps(edge, indent=4))

        self.assertEqual("itr1", cyjs["elements"]["edges"][0]["data"]["interaction"])
Пример #20
0
    def test_networkx_edge_attribute(self):
        print('\n---------- Edge Att Test Start -----------\n')
        g = nx.Graph()
        g.add_edge(1, 2, interaction='itr1',  score=0.1)
        original_edge = g[1][2]
        print(original_edge.keys())
        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        # There is only one edge, so this should be OK...
        edge = cyjs['elements']['edges'][0]
        print(json.dumps(edge, indent=4))

        self.assertEqual('itr1', cyjs['elements']['edges'][0]['data']['interaction'])
Пример #21
0
 def get(self, run_id):
     """
     Return the dag of the given run
     """
     pipeline = db.pipelines.find_one({'run_id': run_id}, {
         'config': 1,
         'single_step': 1,
         '_id': 0
     })
     config = json.loads(pipeline['config'])
     if 'single_step' in pipeline:
         dag = Pipeline.create_dag(config, one_step=True)
     else:
         dag = Pipeline.create_dag(config)
     cy_network = cy.from_networkx(dag)
     return cy_network
Пример #22
0
    def test_networkx_ba(self):
        g = nx.barabasi_albert_graph(100, 3)
        nodes = g.nodes()
        edges = g.edges()

        g.graph["name"] = "ba test"
        cyjs_g = util.from_networkx(g)

        print("\n---------- BA graph Test Start -----------\n")

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g["data"])
        self.assertEqual("ba test", cyjs_g["data"]["name"])
        self.assertEqual(len(nodes), len(cyjs_g["elements"]["nodes"]))
        self.assertEqual(len(edges), len(cyjs_g["elements"]["edges"]))

        diff = compare_edge_sets(set(edges), cyjs_g["elements"]["edges"])
        self.assertEqual(0, len(diff))
Пример #23
0
    def test_networkx_ba(self):
        g = nx.barabasi_albert_graph(100, 3)
        nodes = g.nodes()
        edges = g.edges()

        g.graph['name'] = 'ba test'
        cyjs_g = util.from_networkx(g)

        print('\n---------- BA graph Test Start -----------\n')

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual('ba test', cyjs_g['data']['name'])
        self.assertEqual(len(nodes), len(cyjs_g['elements']['nodes']))
        self.assertEqual(len(edges), len(cyjs_g['elements']['edges']))

        diff = compare_edge_sets(set(edges), cyjs_g['elements']['edges'])
        self.assertEqual(0, len(diff))
Пример #24
0
def export_to_cytoscape(G, export_file):
    '''
    Exports networkX graph to JSON file in a Cytoscape compatible format.

    Inputs:
        - G: networkX graph
        - export_file: JSON file name to export graph to

    Returns:
        - None

    Side Effect:
        - Creates a JSON file of the name export_file.
    '''

    G_json = util.from_networkx(G)
    with open(export_file, 'w') as outfile:
        json.dump(G_json, outfile)
Пример #25
0
    def test_networkx_ba(self):
        g = nx.barabasi_albert_graph(100, 3)
        nodes = g.nodes()
        edges = g.edges()

        g.graph['name'] = 'ba test'
        cyjs_g = util.from_networkx(g)

        print('\n---------- BA graph Test Start -----------\n')

        self.assertIsNotNone(cyjs_g)
        self.assertIsNotNone(cyjs_g['data'])
        self.assertEqual('ba test', cyjs_g['data']['name'])
        self.assertEqual(len(nodes), len(cyjs_g['elements']['nodes']))
        self.assertEqual(len(edges), len(cyjs_g['elements']['edges']))

        diff = compare_edge_sets(set(edges), cyjs_g['elements']['edges'])
        self.assertEqual(0, len(diff))
Пример #26
0
    def test_networkx_gml(self):
        g = nx.read_gml(self.cur_dir + '/data/galFiltered.gml')
        g.graph['name'] = 'gml_test'

        cyjs_g = util.from_networkx(g)

        print('\n---------- GML Test Start -----------\n')
        # print(json.dumps(cyjs_g, indent=4))
        self.assertIsNotNone(cyjs_g)

        net_data = cyjs_g['data']
        self.assertIsNotNone(net_data)
        self.assertEqual('gml_test', net_data['name'])
        self.assertEqual(331, len(cyjs_g['elements']['nodes']))
        self.assertEqual(362, len(cyjs_g['elements']['edges']))

        nodes = cyjs_g['elements']['nodes']
        node0 = nodes[0]
        self.assertEqual(type("1"), type(node0['data']['id']))
Пример #27
0
    def test_networkx_gml(self):
        g = nx.read_gml(self.cur_dir + "/data/galFiltered.gml")
        g.graph["name"] = "gml_test"

        cyjs_g = util.from_networkx(g)

        print("\n---------- GML Test Start -----------\n")
        # print(json.dumps(cyjs_g, indent=4))
        self.assertIsNotNone(cyjs_g)

        net_data = cyjs_g["data"]
        self.assertIsNotNone(net_data)
        self.assertEqual("gml_test", net_data["name"])
        self.assertEqual(331, len(cyjs_g["elements"]["nodes"]))
        self.assertEqual(362, len(cyjs_g["elements"]["edges"]))

        nodes = cyjs_g["elements"]["nodes"]
        node0 = nodes[0]
        self.assertEqual(type("1"), type(node0["data"]["id"]))
Пример #28
0
    def test_networkx_multidigraph_edge_attr(self):
        print('\n---------- Multi-Digraph Edge Att Test Start -----------\n')
        g = nx.MultiDiGraph()
        g.add_node(1)
        g.add_node(2)
        g.add_node(3)
        g.add_edge(1, 2)
        g.add_edge(1, 2, attr_dict={'foo': 'bar'})
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        edges = g.edges(data=True, keys=True)
        for edge in edges:
            print(edge)

        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        edge = cyjs['elements']['edges'][0]
        self.assertTrue(3 <= len(edge['data']))
Пример #29
0
    def test_networkx_multidigraph_edge_attr(self):
        print('\n---------- Multi-Digraph Edge Att Test Start -----------\n')
        g = nx.MultiDiGraph()
        g.add_node(1)
        g.add_node(2)
        g.add_node(3)
        g.add_edge(1, 2)
        g.add_edge(1, 2, attr_dict={'foo': 'bar'})
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        edges = g.edges(data=True, keys=True)
        for edge in edges:
            print(edge)

        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        edge = cyjs['elements']['edges'][0]
        self.assertTrue(3 <= len(edge['data']))
Пример #30
0
    def test_networkx_multidigraph_edge_attr(self):
        print("\n---------- Multi-Digraph Edge Att Test Start -----------\n")
        g = nx.MultiDiGraph()
        g.add_node(1)
        g.add_node(2)
        g.add_node(3)
        g.add_edge(1, 2)
        g.add_edge(1, 2, attr_dict={"foo": "bar"})
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        edges = g.edges(data=True, keys=True)
        for edge in edges:
            print(edge)

        cyjs = util.from_networkx(g)

        print(json.dumps(cyjs, indent=4))

        edge = cyjs["elements"]["edges"][0]
        self.assertTrue(3 <= len(edge["data"]))
Пример #31
0
    def test_networkx_scale_free(self):
        g = nx.scale_free_graph(100)
        edge_count = g.number_of_edges()

        g.graph["name"] = "scale_free_test"

        cyjs_g = util.from_networkx(g)

        print("\n---------- Scale free network Test Start -----------\n")
        print("Edge count = " + str(edge_count))
        # print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)

        net_data = cyjs_g["data"]
        self.assertIsNotNone(net_data)
        self.assertEqual("scale_free_test", net_data["name"])
        self.assertEqual(100, len(cyjs_g["elements"]["nodes"]))
        self.assertEqual(edge_count, len(cyjs_g["elements"]["edges"]))

        nodes = cyjs_g["elements"]["nodes"]
        node0 = nodes[0]
        self.assertEqual(type("1"), type(node0["data"]["id"]))
Пример #32
0
    def test_networkx_scale_free(self):
        g = nx.scale_free_graph(100)
        edge_count = g.number_of_edges()

        g.graph['name'] = 'scale_free_test'

        cyjs_g = util.from_networkx(g)

        print('\n---------- Scale free network Test Start -----------\n')
        print('Edge count = ' + str(edge_count))
        # print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)

        net_data = cyjs_g['data']
        self.assertIsNotNone(net_data)
        self.assertEqual('scale_free_test', net_data['name'])
        self.assertEqual(100, len(cyjs_g['elements']['nodes']))
        self.assertEqual(edge_count, len(cyjs_g['elements']['edges']))

        nodes = cyjs_g['elements']['nodes']
        node0 = nodes[0]
        self.assertEqual(type("1"), type(node0['data']['id']))
Пример #33
0
    def test_networkx_scale_free(self):
        g = nx.scale_free_graph(100)
        edge_count = g.number_of_edges()

        g.graph['name'] = 'scale_free_test'

        cyjs_g = util.from_networkx(g)

        print('\n---------- Scale free network Test Start -----------\n')
        print('Edge count = ' + str(edge_count))
        # print(json.dumps(cyjs_g, indent=4))

        self.assertIsNotNone(cyjs_g)

        net_data = cyjs_g['data']
        self.assertIsNotNone(net_data)
        self.assertEqual('scale_free_test', net_data['name'])
        self.assertEqual(100, len(cyjs_g['elements']['nodes']))
        self.assertEqual(edge_count, len(cyjs_g['elements']['edges']))

        nodes = cyjs_g['elements']['nodes']
        node0 = nodes[0]
        self.assertEqual(type("1"), type(node0['data']['id']))
Пример #34
0
def generate_genome_local(otus, loc=None):
    genome_table = load_data_table([i[0] for i in otus])
    genomes = list()
    for otu_id, taxonomy in otus:
        print(os.getpid(), otu_id)
        nsti = genome_table.metadata(otu_id)['NSTI']
        genome = genome_table.ids(
            axis="observation")[genome_table.data(otu_id) > 0]
        genome = [str(i) for i in genome]
        print(os.getpid(), otu_id, "genome length", len(genome))
        reactome = mna.get_reactome_local(genome, loc)
        print(os.getpid(), otu_id, "reactome length", len(reactome))
        rxns = mna.get_rxns_local(reactome, loc)
        print(os.getpid(), otu_id, "reaction count", len(rxns))
        metab_network = mna.make_metabolic_network(rxns, only_giant=True)
        print(os.getpid(), otu_id, "network made")
        metab_network_json = cy.from_networkx(metab_network)
        genome = Genome(name=int(otu_id),
                        nsti=float(nsti),
                        metab_net=json.dumps(metab_network_json),
                        genome=','.join(genome),
                        taxonomy=taxonomy)
        genomes.append(genome)
    return genomes
 def setUp(self):
     # Generate a sample graph
     self.graph = util.from_networkx(nx.barabasi_albert_graph(100, 2))
Пример #36
0
    def graph_to_cytoscape(self):

        miR_G = nx.Graph(self.miR_G)  # unfreezing of the graph
        centrality_node = self.centrality_node
        rem_CC = 0

        for CC in list(nx.connected_components(miR_G)):
            if len(CC) < 3:
                miR_G.remove_nodes_from(CC)
                rem_CC += 1

        print(rem_CC,
              ' network components with less than two nodes have been removed',
              end='\n')

        PORT_NUMBER = 1234
        IP = 'localhost'
        BASE = 'http://' + IP + ':' + str(PORT_NUMBER) + '/v1/'

        requests.delete(BASE +
                        'session')  # Delete all networks in current session

        cytoscape_network = cy.from_networkx(miR_G)
        cytoscape_network['data']['name'] = 'miR_Net'
        res1 = requests.post(BASE + 'networks',
                             data=json.dumps(cytoscape_network))
        res1_dict = res1.json()
        new_suid = res1_dict['networkSUID']
        requests.get(BASE + 'apply/layouts/force-directed/' + str(new_suid))

        # load and apply style

        res = requests.get(BASE + 'styles/miR_Net_Styles')
        if res.status_code != 200:

            with open('./options/cytoscape_styles/miR_Net_Styles.json'
                      ) as json_file:
                miR_Net_Styles = json.load(json_file)

            for mapings in range(0, len(miR_Net_Styles['mappings'])):
                if miR_Net_Styles['mappings'][mapings][
                        'visualProperty'] == 'NODE_LABEL_FONT_SIZE':
                    miR_Net_Styles['mappings'][mapings]['points'][1][
                        'value'] = max(centrality_node.values())
                if miR_Net_Styles['mappings'][mapings][
                        'visualProperty'] == 'NODE_SIZE':
                    miR_Net_Styles['mappings'][mapings]['points'][1][
                        'value'] = max(centrality_node.values())
                if miR_Net_Styles['mappings'][mapings][
                        'visualProperty'] == 'NODE_FILL_COLOR':
                    miR_Net_Styles['mappings'][mapings]['points'][1][
                        'value'] = max(centrality_node.values())

            # Create new Visual Style
            res = requests.post(BASE + "styles",
                                data=json.dumps(miR_Net_Styles))

        # Apply it to current network

        requests.get(BASE + 'apply/styles/' + 'miR_Net_Styles' + '/' +
                     str(new_suid))  # !Это говно почему-то не работает
Пример #37
0
 def get(self, name):
     """
     Return the dag of a pipeline
     """
     cy_network = cy.from_networkx(Pipeline.create_dag(pipeline_specs[name]))
     return cy_network
Пример #38
0
                    if p != q:
                        if distance < Graph[p][q]['distance']:
                            Graph[p][q]['distance'] = distance
                        Graph.edge[p][q]['count'] += 1
                except:

                    Graph.add_node(p,
                                   count=1,
                                   refdb=value[-2],
                                   color=COLOR[value[-2]])
                    Graph.add_node(q,
                                   count=1,
                                   refdb=value2[-2],
                                   color=COLOR[value2[-2]])

                    if p != q:
                        if value2[-2] == value[-2]:
                            link_color = COLOR[value[-2]]
                        else:
                            link_color = '#000000'
                        Graph.add_edge(p,
                                       q,
                                       distance=distance,
                                       count=1,
                                       link_color=link_color)

                    #print i,j+i,value,value2,math.log(pdist([[value[-4]],[value2[-4]]])[0])

Gc = cy.from_networkx(Graph)
json.dump(Gc, open('test.json', 'w'))
 def calculate(self, graph, params):
     nx_graph = util.to_networkx(graph)
     return util.from_networkx(nx_graph)
Пример #40
0
 def test_render(self):
     from py2cytoscape.cytoscapejs import viewer as cyjs
     g = nx.scale_free_graph(100)
     g_cyjs = from_networkx(g)
     result = cyjs.render(g_cyjs, layout_algorithm='circle')
     self.assertIsNone(result)
	def addNetwork(self, g):
		"""Convert a NetworkX Graph object and add it to cytoscape""" 
		cytoscape_network = cy.from_networkx(g)
		res1 = requests.post(self.BASE + 'networks', data=json.dumps(cytoscape_network), headers=self.HEADERS)
		res1_dict = json.loads(res1.content)
		self.suid = res1_dict['networkSUID']
Пример #42
0
 def networkx_to_cyjs(g):
     return util.from_networkx(g)
Пример #43
0
def write_fixed_clustering_as_json(mcmc: BaseMCMC, output_filename: str):
    '''Export the posterior fixed topology as a json usable in Cytoscape

    Parameters
    ----------
    mcmc : BaseMCMC
        This is the chain that contains the traces
    output_filename : str
        This is the path to save the json file
    '''
    import networkx as nx
    from py2cytoscape.util import from_networkx
    import json

    def clusterize(labels: np.ndarray, taxa_list: List[str]) -> Dict[int, List[str]]:
        cluster = {}
        for i in range(len(labels)):
            if labels[i] not in cluster:
                cluster[labels[i]] = []
            cluster[labels[i]].append(taxa_list[i])
        return cluster

    def get_largest_weight(matrix: np.ndarray) -> float:
        '''Returns the largest non infinite weight (value) in the matrix
        '''
        new_matrix = np.where(matrix == np.inf, -np.inf, matrix)
        return np.amax(new_matrix)

    def get_bayes_category(bf: float) -> int:
        '''Classify bayes factor according to strength of evidence

        Parameters
        ----------
        bf : float
            Bayes factor

        Returns
        -------
        int
        '''
        category = 0
        #decisive
        if bf > 10 ** 2 :
            category = 3
        #strong
        elif 10 < bf <= 10 ** 2:
            category = 2
        # substantial + not worth mentioning
        elif 0 < bf <= 10:
            category = 1
        return category

    clustering = mcmc.graph[STRNAMES.CLUSTERING_OBJ]
    consensus_cluster_labels = generate_cluster_assignments_posthoc(clustering=clustering, set_as_value=True)

    taxa_names = []
    taxas = mcmc.graph.data.taxas
    for taxa in taxas:
        taxa_names.append(taxa.name)

    consensus_cluster = clusterize(consensus_cluster_labels, taxa_names)
    M = pl.summary(mcmc.graph[STRNAMES.INTERACTIONS_OBJ], set_nan_to_0=True, section='posterior')['mean']
    M_condensed = condense_fixed_clustering_interaction_matrix(M, clustering=clustering)

    bf = generate_interation_bayes_factors_posthoc(mcmc=mcmc, section='posterior') # (n_taxa, n_taxa)
    bf_condensed = condense_fixed_clustering_interaction_matrix(bf, clustering=clustering)

    columns = np.sort(list(consensus_cluster.keys()))
    # Take the transpose so that rows are the srouce and columns destination
    bayes_df = pd.DataFrame(bf_condensed.T, columns=columns, index=columns)
    graph_bayes = nx.from_pandas_adjacency(bayes_df, create_using=nx.DiGraph())

    largest = get_largest_weight(bf_condensed)
    all_edges = graph_bayes.edges()
    edge_attributes = {}

    for edge in graph_bayes.edges(data=True):
        # Column is the source and row is the destination
        int_strength = M_condensed[edge[1], edge[0]]
        coord = (edge[0], edge[1])
        sign = 0
        weight = edge[2]['weight']

        if np.isint('weight'):
            weight = largest
        if int_strength < 0:
            sign = -1
        else:
            sign = 1

        category = get_bayes_category(weight)
        bend = False
        if (edge[1], edge[0]) in all_edges:
            bend = True
        
        edge_attributes[coord] = {'bayes_fac': category, 'sign': sign, 
            'weight': weight, 'bend': bend}

    nx.set_edge_attributes(graph_bayes, edge_attributes)

    nodes_attributes = {}
    for keys in consensus_cluster:
        nodes_attributes[keys] = {'size': len(consensus_cluster[keys])}

    nx.set_node_attributes(graph_bayes, nodes_attributes)
    data_json = from_networkx(graph_bayes, nodes_attributes)
    if '.json' not in output_filename:
        output_filename += '.json'
    with open(filename, 'w') as f:
        json.dump(data_json, f)
Пример #44
0
 def networkx_to_cyjs(g):
     return util.from_networkx(g)
Пример #45
0
 def test_render(self):
     from py2cytoscape.cytoscapejs import viewer as cyjs
     g = nx.scale_free_graph(100)
     g_cyjs = from_networkx(g)
     result = cyjs.render(g_cyjs, layout_algorithm='circle')
     self.assertIsNone(result)