示例#1
0
def get_nice_cx_network_from_clusterfile(inputfile):
    """

    :param inputfile:
    :return:
    """
    network = NiceCXNetwork()
    network.set_name('clustertocx from ' + str(os.path.basename(os.path.dirname(inputfile))))
    with open(inputfile, 'r') as f:
        data = f.read()

    node_map = {}
    protein_map = {}
    for line in data.split(';'):
        slist = line.split(',')
        if len(slist) != 3:
            sys.stderr.write(line + ' does not have appropriate number of columns. skipping\n')
            continue

        if slist[2].startswith('c-c'):
            target_is_protein = False
        else:
            target_is_protein = True

        if slist[0] not in node_map:
            source_node_id = network.create_node(slist[0])
            node_map[slist[0]] = source_node_id
            network.add_node_attribute(property_of=source_node_id, name='suid', values=int(slist[0]), type='long')
            network.add_node_attribute(property_of=source_node_id, name='type', values='term', type='string')
        else:
            source_node_id = node_map[slist[0]]

        if target_is_protein:
            if slist[0] not in protein_map:
                protein_map[slist[0]] = set()
                protein_map[slist[0]].add(slist[1])
            else:
                if slist[1] not in protein_map[slist[0]]:
                    protein_map[slist[0]].add(slist[1])
        else:
            target_node_id = network.create_node(slist[1])
            network.create_edge(source_node_id, target_node_id, 'Child-Parent')
            network.add_node_attribute(property_of=target_node_id, name='suid', values=int(slist[1]), type='long')
            network.add_node_attribute(property_of=target_node_id, name='type', values='term', type='string')
            node_map[slist[1]] = target_node_id

    for nodename in protein_map:
        genelist = protein_map[nodename]
        if len(genelist) > 0:
            genesymbol_list = []
            for entry in genelist:
                genesymbol_list.append(entry)
            network.add_node_attribute(property_of=node_map[nodename], name='member', values=genesymbol_list,
                                       type='list_of_string')
            network.add_node_attribute(property_of=node_map[nodename], name='type', values='complex', type='string',
                                       overwrite=True)
    del node_map
    del protein_map

    return network
示例#2
0
    def test_build_edge_map_single_edge_of_each_type(self):
        adjud = RedundantEdgeAdjudicator()
        net = NiceCXNetwork()

        # try empty network
        neighbor, controls, other = adjud._build_edge_map(net)
        self.assertEqual({}, neighbor)
        self.assertEqual({}, controls)
        self.assertEqual({}, other)

        # try network with single neighbor, controls, and an another edge
        nid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='neighbor-of')
        cid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='controls-state-change-of')
        oid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='somethingelse')
        neighbor, controls, other = adjud._build_edge_map(net)
        self.assertEqual(nid, neighbor[0][1])
        self.assertEqual(nid, neighbor[1][0])
        self.assertEqual({cid}, controls[0][1])
        self.assertEqual({oid}, other[0][1])
示例#3
0
    def test_update_network_with_empty_network(self):
        client = self.get_ndex2_client()
        # create network and add it
        net = NiceCXNetwork()
        oneid = net.create_node('node1')
        twoid = net.create_node('node2')
        net.create_edge(oneid, twoid, 'hello')
        netname = 'NiceCXNetwork upload_to() test network' + str(
            datetime.now())
        net.set_name(netname)
        creds = self.get_ndex_credentials_as_tuple()
        res = net.upload_to(creds['server'],
                            creds['user'],
                            creds['password'],
                            user_agent=creds['user_agent'])
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)
            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            self.assertEqual(netid, netsum['externalId'])
            self.assertTrue('name' in netsum, msg=str(netsum))
            self.assertEqual(netname, netsum['name'], str(netsum))
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(1, netsum['edgeCount'])
            self.assertEqual(2, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])
            newnet = NiceCXNetwork()

            newres = newnet.update_to(netid,
                                      creds['server'],
                                      creds['user'],
                                      creds['password'],
                                      user_agent=creds['user_agent'])
            self.assertEqual('', newres)
            netsum = self.wait_for_network_to_be_ready(client,
                                                       netid,
                                                       num_retries=5,
                                                       retry_weight=1)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            self.assertEqual(netid, netsum['externalId'])
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(0, netsum['edgeCount'])
            self.assertEqual(0, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])
        finally:
            client.delete_network(netid)
            try:
                client.get_network_as_cx_stream(netid)
                self.fail('Expected exception')
            except HTTPError:
                pass
    def test_simple_create(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Food Web")
        fox_node = niceCx_creatures.create_node(node_name='Fox')
        mouse_node = niceCx_creatures.create_node(node_name='Mouse')
        bird_node = niceCx_creatures.create_node(node_name='Bird')

        fox_bird_edge = niceCx_creatures.create_edge(
            edge_source=fox_node,
            edge_target=bird_node,
            edge_interaction='interacts-with')

        fox_mouse_edge = niceCx_creatures.create_edge(
            edge_source=fox_node,
            edge_target=mouse_node,
            edge_interaction='interacts-with')

        niceCx_creatures.add_node_attribute(property_of=fox_node,
                                            name='Color',
                                            values='Red')

        niceCx_creatures.add_node_attribute(property_of=mouse_node,
                                            name='Color',
                                            values='Gray')

        niceCx_creatures.add_node_attribute(property_of=bird_node,
                                            name='Color',
                                            values='Blue')

        niceCx_creatures.add_edge_attribute(property_of=fox_mouse_edge,
                                            name='Hunted',
                                            values='On the ground')

        print(niceCx_creatures.get_node_attribute(fox_node, 'Color'))
示例#5
0
 def test_create_edge_with_int_for_edge_ids(self):
     net = NiceCXNetwork()
     net.create_edge(edge_source=0, edge_target=1)
     res = net.get_edge(0)
     self.assertEqual(0, res[constants.EDGE_ID])
     self.assertEqual(0, res[constants.EDGE_SOURCE])
     self.assertEqual(1, res[constants.EDGE_TARGET])
    def test_update(self):
        collapser = RedundantEdgeCollapser()
        net = NiceCXNetwork()
        ctext = {'pubmed': 'http://p/'}
        net.set_network_attribute('@context', values=json.dumps(ctext))
        collapser._set_pubmedurl_from_network(net)

        eid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='something')

        net.set_edge_attribute(eid, 'sentence', 'sent1', type='string')
        net.set_edge_attribute(eid, 'direct', True, type='boolean')
        net.set_edge_attribute(eid, 'citation', 'pubmed:123', type='string')

        eidtwo = net.create_edge(edge_source=0,
                                 edge_target=1,
                                 edge_interaction='something')
        net.set_edge_attribute(eidtwo, 'sentence', 'sent2', type='string')
        net.set_edge_attribute(eidtwo, 'direct', True, type='boolean')
        net.set_edge_attribute(eidtwo, 'citation', 'pubmed:456', type='string')
        res = collapser.update(net)

        self.assertEqual([], res)

        edata = net.get_edge_attribute(eid, 'sentence')
        self.assertEqual(2, len(edata['v']))

        self.assertTrue('<a target="_blank" href="http://p/456">'
                        'pubmed:456</a>  sent2' in edata['v'])
        self.assertTrue('<a target="_blank" href="http://p/123">'
                        'pubmed:123</a> sent1' in edata['v'])
    def test_remove_edge(self):
        collapser = RedundantEdgeCollapser()
        net = NiceCXNetwork()

        # test remove on non existant edge
        collapser._remove_edge(net, 10)

        # test removing edge with no attributes
        eid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='needs')
        self.assertEqual('needs', net.get_edge(eid)['i'])
        collapser._remove_edge(net, eid)
        self.assertEqual(None, net.get_edge(eid))

        # test removing edge with attributes
        eid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='needs')

        net.set_edge_attribute(eid, 'foo', 'someval')
        net.set_edge_attribute(eid, 'foo2', 'someval2')

        self.assertEqual('needs', net.get_edge(eid)['i'])
        self.assertEqual('someval', net.get_edge_attribute(eid, 'foo')['v'])
        self.assertEqual('someval2', net.get_edge_attribute(eid, 'foo2')['v'])
        collapser._remove_edge(net, eid)
        self.assertEqual(None, net.get_edge(eid))
        self.assertEqual((None, None), net.get_edge_attribute(eid, 'foo'))
        self.assertEqual((None, None), net.get_edge_attribute(eid, 'foo2'))
示例#8
0
    def test_remove_if_redundant_with_multiple_edges_same_citations(self):
        net = NiceCXNetwork()
        adjud = RedundantEdgeAdjudicator()
        nid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='neighbor-of')

        net.set_edge_attribute(nid,
                               RedundantEdgeAdjudicator.CITATION,
                               ['pubmed:4', 'pubmed:123'],
                               type='list_of_string')

        cid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='controls-state-change-of')

        net.set_edge_attribute(cid,
                               RedundantEdgeAdjudicator.CITATION,
                               ['pubmed:555', 'pubmed:4'],
                               type='list_of_string')

        cidtwo = net.create_edge(edge_source=0,
                                 edge_target=1,
                                 edge_interaction='controls-state-change-of')

        net.set_edge_attribute(cidtwo,
                               RedundantEdgeAdjudicator.CITATION,
                               ['pubmed:4', 'pubmed:123'],
                               type='list_of_string')

        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
        adjud._remove_if_redundant(net, nid, [cid, cidtwo])
        self.assertEqual(None, net.get_edge(nid))
示例#9
0
    def test_basic_network(self):
        net = NiceCXNetwork()
        adjud = RedundantEdgeAdjudicator()
        nid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='neighbor-of')

        cid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='controls-state-change-of')

        net.set_edge_attribute(cid,
                               RedundantEdgeAdjudicator.CITATION,
                               ['pubmed:123'],
                               type='list_of_string')

        oid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='someother')
        net.set_edge_attribute(oid,
                               RedundantEdgeAdjudicator.CITATION,
                               ['pubmed:123'],
                               type='list_of_string')

        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
        self.assertEqual('controls-state-change-of', net.get_edge(cid)['i'])
        self.assertEqual('someother', net.get_edge(oid)['i'])

        self.assertEqual([], adjud.update(net))
        self.assertEqual(None, net.get_edge(nid))
        self.assertEqual(None, net.get_edge(cid))
        self.assertEqual('someother', net.get_edge(oid)['i'])
示例#10
0
    def test_basic_network_where_neighbor_of_citations_merges_disabled(self):
        net = NiceCXNetwork()
        adjud = RedundantEdgeAdjudicator(disablcitededgemerge=True)
        nid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='neighbor-of')

        net.set_edge_attribute(nid,
                               RedundantEdgeAdjudicator.CITATION, ['pubmed:5'],
                               type='list_of_string')
        cid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='controls-state-change-of')

        oid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='someother')

        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
        self.assertEqual('controls-state-change-of', net.get_edge(cid)['i'])
        self.assertEqual('someother', net.get_edge(oid)['i'])

        self.assertEqual([], adjud.update(net))
        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
        self.assertEqual(None, net.get_edge(cid))
        self.assertEqual('someother', net.get_edge(oid)['i'])
        res = net.get_edge_attribute(nid, RedundantEdgeAdjudicator.CITATION)
        res['v'].sort()
        self.assertEqual(['pubmed:5'], res['v'])
        res = net.get_edge_attribute(oid, RedundantEdgeAdjudicator.CITATION)

        self.assertEqual((None, None), res)
示例#11
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_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'])
示例#13
0
 def test_bad_nodes_add_edge(self):
     niceCx_creatures = NiceCXNetwork()
     niceCx_creatures.set_name("Test Network")
     node1_id = niceCx_creatures.create_node(node_name=324)
     node2_id = niceCx_creatures.create_node(node_name=453)
     niceCx_creatures.create_edge(edge_source=node1_id, edge_target=node2_id, edge_interaction=['inte'])
     upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
     self.assertTrue(upload_message)
示例#14
0
 def test_bad_nodes_add_edge(self):
     niceCx_creatures = NiceCXNetwork()
     niceCx_creatures.set_name("Test Network")
     node1_id = niceCx_creatures.create_node(node_name=324)
     node2_id = niceCx_creatures.create_node(node_name=453)
     niceCx_creatures.create_edge(edge_source=node1_id, edge_target=node2_id, edge_interaction=['inte'])
     upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
     self.assertTrue(upload_message)
示例#15
0
 def test_to_networkx_simple_graph_default_mode(self):
     net = NiceCXNetwork(mode='default')
     net.set_name('mynetwork')
     net.create_node('node0')
     net.create_node('node1')
     net.create_edge(edge_source=0, edge_target=1)
     g = net.to_networkx()
     self.assertEqual(g.graph['name'], 'mynetwork')
     self.assertEqual(2, len(g))
示例#16
0
    def test_upload_network_with_no_network_attributes(self):
        """
        So if one uploads a network with NO network attributes
        to NDEx and then edits the network (setting name visibility, description)
        via [updateNetworkSummary]	[/v2/network/6b89b286-e142-11ec-b397-0ac135e8bacf/summary
        the CX2 endpoint shows the changed network attributes, but the CX1
        is missing the network attributes
        :return:
        """
        client = self.get_ndex2_client()
        # create network and add it
        net = NiceCXNetwork()
        oneid = net.create_node('node1')
        twoid = net.create_node('node2')
        net.create_edge(oneid, twoid, 'hello')

        res = client.save_new_network(net.to_cx(), visibility='PRIVATE')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)
            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            self.assertEqual(netid, netsum['externalId'])
            self.assertTrue('name' not in netsum, msg=str(netsum))

            # okay now we have the network, lets update the name
            # and description and then get the network back again
            # via cx1 and cx2 endpoints
            netname = 'ndex2-client integration test network' + str(
                datetime.now())
            client.update_network_profile(netid, {'name': netname})
            netsum = self.wait_for_network_to_be_ready(client, netid)
            self.assertIsNotNone(
                netsum, 'Network is still not ready,'
                ' maybe server is busy?')
            cx2_resp = client.get_network_as_cx2_stream(network_id=netid)
            cx2_json = json.loads(cx2_resp.content)
            net_attrs = None
            for aspect in cx2_json:
                print(aspect)
                if 'networkAttributes' in aspect:
                    net_attrs = aspect['networkAttributes']
                    break
            self.assertEqual([{'name': netname}], net_attrs)

            client_resp = client.get_network_as_cx_stream(network_id=netid)
            cx1_net = ndex2.create_nice_cx_from_raw_cx(
                json.loads(client_resp.content))
            self.assertEqual(netname, cx1_net.get_name(),
                             'Special test to expose '
                             'bug in NDEx server')
        finally:
            client.delete_network(netid)
示例#17
0
    def test_update_network(self):
        client = self.get_ndex2_client()
        # create network and add it
        net = NiceCXNetwork()
        oneid = net.create_node('node1')
        twoid = net.create_node('node2')
        net.create_edge(oneid, twoid, 'hello')
        netname = 'ndex2-client integration test network' + str(datetime.now())
        net.set_name(netname)
        res = client.save_new_network(net.to_cx(), visibility='PRIVATE')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)

            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(netid, netsum['externalId'])
            self.assertEqual(netname, netsum['name'])
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(1, netsum['edgeCount'])
            self.assertEqual(2, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])

            net.create_node(node_name='hello', node_represents='something')
            net.create_node(node_name='hoho', node_represents='heheh')
            newnetname = 'update ' + netname
            self.assertEqual(4, len(net.get_nodes()))
            net.set_name(newnetname)
            if sys.version_info.major == 3:
                stream = io.BytesIO(json.dumps(net.to_cx(),
                                               cls=DecimalEncoder)
                                    .encode('utf-8'))
            else:
                stream = io.BytesIO(json.dumps(net.to_cx(),
                                               cls=DecimalEncoder))
            newres = client.update_cx_network(stream, netid)
            self.assertEqual('', newres)
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(netid, netsum['externalId'])
            self.assertEqual(newnetname, netsum['name'])
            self.assertEqual('PRIVATE', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(1, netsum['edgeCount'])
            self.assertEqual(4, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])
        finally:
            client.delete_network(netid)
            try:
                client.get_network_as_cx_stream(netid)
                self.fail('Expected exception')
            except HTTPError:
                pass
示例#18
0
    def test__str__(self):
        net = NiceCXNetwork()
        self.assertEqual('nodes: 0 \n edges: 0', str(net))
        net.create_node('foo')
        self.assertEqual('nodes: 1 \n edges: 0', str(net))
        net.create_node('foo2')
        net.create_node('foo3')
        self.assertEqual('nodes: 3 \n edges: 0', str(net))

        net.create_edge(edge_source=0, edge_target=1)
        self.assertEqual('nodes: 3 \n edges: 1', str(net))
    def test_update_no_family_members(self):
        net = NiceCXNetwork()

        node1 = net.create_node('node1')
        node2 = net.create_node('node2')
        net.create_edge(edge_source=node1, edge_target=node2,
                        edge_interaction='activates')
        remover = ProteinFamilyNodeMemberRemover()
        remover.update(net)

        self.assertEqual(2, len(net.get_nodes()))
        self.assertEqual(1, len(net.get_edges()))
示例#20
0
    def test_remove_if_redundant_with_no_citation_on_edge(self):
        net = NiceCXNetwork()
        adjud = RedundantEdgeAdjudicator()
        nid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='neighbor-of')
        cid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='controls-state-change-of')

        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
        adjud._remove_if_redundant(net, nid, [cid])

        self.assertEqual(None, net.get_edge(nid))
    def test_update_edge_with_dict(self):
        collapser = RedundantEdgeCollapser()
        net = NiceCXNetwork()

        eid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='something')
        net.set_edge_attribute(eid, 'sentence', 'hi', type='string')

        edge_dict = {
            'citation': (set(['pubmed:123']), 'list_of_string'),
            'sentence': (set(['sentence1', 'sentence2']), 'string'),
            'direct': (set([True, False]), 'boolean')
        }

        res = collapser._update_edge_with_dict(net, eid, edge_dict)

        self.assertTrue('direct attribute has multiple values:' in res[0])

        edata = net.get_edge_attribute(eid, 'citation')
        self.assertEqual('list_of_string', edata['d'])
        self.assertEqual(['pubmed:123'], edata['v'])

        edata = net.get_edge_attribute(eid, 'sentence')
        self.assertEqual('list_of_string', edata['d'])
        self.assertTrue('sentence1' in edata['v'])
        self.assertTrue('sentence2' in edata['v'])
        self.assertEqual(2, len(edata['v']))

        edata = net.get_edge_attribute(eid, 'direct')
        self.assertEqual('boolean', edata['d'])
        self.assertEqual(False, edata['v'])
示例#22
0
 def test_edge_with_no_edgecitation(self):
     updator = EmptyCitationAttributeRemover()
     net = NiceCXNetwork()
     edgeid = net.create_edge(edge_source=0, edge_target=1,
                              edge_interaction='foo')
     self.assertEqual([], updator.update(net))
     self.assertEqual((None, None), net.get_edge_attribute(edgeid,
                                                           'citation'))
示例#23
0
    def test_apply_simple_spring_layout(self):
        net = NiceCXNetwork()
        n_one = net.create_node('node1')
        n_two = net.create_node('node2')
        net.create_edge(n_one, n_two, 'links')
        p = MagicMock()
        p.datadir = 'datadir'
        p.profile = 'foo'
        p.organismloadplan = ndexloadbiogrid.get_organism_load_plan()
        p.chemicalloadplan = ndexloadbiogrid.get_chemical_load_plan()
        p.organismstyle = ndexloadbiogrid.get_organism_style()
        p.chemstyle = ndexloadbiogrid.get_chemical_style()
        p.biogridversion = '1.0.0'
        p.skipdownload = False
        loader = NdexBioGRIDLoader(p)
        loader._apply_simple_spring_layout(net)

        aspect = net.get_opaque_aspect('cartesianLayout')
        self.assertEqual(2, len(aspect))

        # try with 19 nodes
        net = NiceCXNetwork()
        for x in range(0, 19):
            net.create_node(str(x))
        loader._apply_simple_spring_layout(net)

        aspect = net.get_opaque_aspect('cartesianLayout')
        self.assertEqual(19, len(aspect))

        # try with 99 nodes
        net = NiceCXNetwork()
        for x in range(0, 99):
            net.create_node(str(x))
        loader._apply_simple_spring_layout(net)

        aspect = net.get_opaque_aspect('cartesianLayout')
        self.assertEqual(99, len(aspect))

        # try with 101 nodes
        net = NiceCXNetwork()
        for x in range(0, 101):
            net.create_node(str(x))
        loader._apply_simple_spring_layout(net)

        aspect = net.get_opaque_aspect('cartesianLayout')
        self.assertEqual(101, len(aspect))
    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 = LegacyNetworkXVersionTwoPlusFactory()
        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('first' in g)
        self.assertTrue('second' in g)
        edgelist = list(g.edges(data=True))

        self.assertTrue(('first' == edgelist[0][0] and 'second' == edgelist[0][1]) or
                        ('second' == edgelist[0][0] and 'first' == edgelist[0][1]))
        self.assertEqual(None, edgelist[0][2]['interaction'])
 def test_with_network_with_edge_not_supposed_to_be_directed(self):
     setter = DirectedEdgeSetter()
     net = NiceCXNetwork()
     edgeid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='foo')
     self.assertEqual([], setter.update(net))
     directed = DirectedEdgeSetter.DIRECTED_ATTRIB
     self.assertEqual(False, net.get_edge_attribute(edgeid, directed)['v'])
示例#26
0
 def test_remove_edge_no_attributes(self):
     adjud = RedundantEdgeAdjudicator()
     net = NiceCXNetwork()
     edgeid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='foo')
     self.assertEqual('foo', net.get_edge(edgeid)['i'])
     adjud._remove_edge(net, edgeid)
     self.assertEqual(None, net.get_edge(edgeid))
示例#27
0
    def test_create_edge_with_interaction(self):
        net = NiceCXNetwork()
        edge_id = net.create_edge(edge_source=10, edge_target=20, edge_interaction='blah')

        res = net.get_edge(edge_id)
        self.assertEqual(edge_id, res[constants.EDGE_ID])
        self.assertEqual(10, res[constants.EDGE_SOURCE])
        self.assertEqual(20, res[constants.EDGE_TARGET])
        self.assertEqual('blah', res[constants.EDGE_INTERACTION])
示例#28
0
    def test_get_edges(self):
        net = NiceCXNetwork()

        # Verify correct operation on empty network
        res = list(net.get_edges())
        self.assertEqual(0, len(res))

        # add an edge
        net.create_edge(edge_source=0, edge_target=1)
        res = list(net.get_edges())
        self.assertEqual(1, len(res))
        self.assertEqual(res[0], (0, {'@id': 0, 's': 0, 't': 1}))

        # add another edge
        net.create_edge(edge_source=1, edge_target=2)
        res = list(net.get_edges())
        self.assertEqual(2, len(res))
        self.assertEqual(res[0], (0, {'@id': 0, 's': 0, 't': 1}))
        self.assertEqual(res[1], (1, {'@id': 1, 's': 1, 't': 2}))
    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 = LegacyNetworkXVersionTwoPlusFactory()
        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('first' in g)
        self.assertTrue('second' in g)
        edgelist = list(g.edges(data=True))

        self.assertTrue(
            ('first' == edgelist[0][0] and 'second' == edgelist[0][1])
            or ('second' == edgelist[0][0] and 'first' == edgelist[0][1]))
        self.assertEqual(None, edgelist[0][2]['interaction'])
示例#30
0
 def test_edge_with_emptystring(self):
     updator = EmptyCitationAttributeRemover()
     net = NiceCXNetwork()
     edgeid = net.create_edge(edge_source=0, edge_target=1,
                              edge_interaction='foo')
     net.set_edge_attribute(edgeid, 'citation', values=[''],
                            type='list_of_string')
     self.assertEqual([], updator.update(net))
     self.assertEqual((None, None), net.get_edge_attribute(edgeid,
                                                           'citation'))
示例#31
0
    def test_remove_if_redundant_with_citation_and_other_lacks_citation(self):
        net = NiceCXNetwork()
        adjud = RedundantEdgeAdjudicator()
        nid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='neighbor-of')

        net.set_edge_attribute(nid,
                               RedundantEdgeAdjudicator.CITATION,
                               ['pubmed:123'],
                               type='list_of_string')

        cid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='controls-state-change-of')

        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
        adjud._remove_if_redundant(net, nid, [cid])
        self.assertEqual('neighbor-of', net.get_edge(nid)['i'])
示例#32
0
 def test_edge_with_no_edgecitation(self):
     updator = EmptyCitationAttributeUpdator()
     net = NiceCXNetwork()
     edgeid = net.create_edge(edge_source=0,
                              edge_target=1,
                              edge_interaction='foo')
     self.assertEqual([], updator.update(net))
     edge = net.get_edge_attribute(edgeid, 'citation')
     self.assertEqual([], edge['v'])
     self.assertEqual('list_of_string', edge['d'])
示例#33
0
    def test_bad_edge_attr(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Test Network")
        node1_id = niceCx_creatures.create_node(node_name=324)
        node2_id = niceCx_creatures.create_node(node_name=453)
        edge = niceCx_creatures.create_edge(edge_source=node1_id, edge_target=node2_id, edge_interaction='inte')
        niceCx_creatures.add_edge_attribute(property_of=edge, name= "djks", values= "jfkl")

        upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
示例#34
0
    def test_bad_edge_attr(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Test Network")
        node1_id = niceCx_creatures.create_node(node_name=324)
        node2_id = niceCx_creatures.create_node(node_name=453)
        edge = niceCx_creatures.create_edge(edge_source=node1_id, edge_target=node2_id, edge_interaction='inte')
        niceCx_creatures.add_edge_attribute(property_of=edge, name= "djks", values= "jfkl")

        upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
    def test_simple_create(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Food Web")
        fox_node = niceCx_creatures.create_node(node_name='Fox')
        mouse_node = niceCx_creatures.create_node(node_name='Mouse')
        bird_node = niceCx_creatures.create_node(node_name='Bird')

        fox_bird_edge = niceCx_creatures.create_edge(edge_source=fox_node, edge_target=bird_node, edge_interaction='interacts-with')

        fox_mouse_edge = niceCx_creatures.create_edge(edge_source=fox_node, edge_target=mouse_node, edge_interaction='interacts-with')

        niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values='Red')

        niceCx_creatures.add_node_attribute(property_of=mouse_node, name='Color', values='Gray')

        niceCx_creatures.add_node_attribute(property_of=bird_node, name='Color', values='Blue')

        niceCx_creatures.add_edge_attribute(property_of=fox_mouse_edge, name='Hunted', values='On the ground')

        print(niceCx_creatures.get_node_attribute(fox_node, 'Color'))
示例#36
0
    def test_load_edges(self):
        self.assertFalse(upload_username == 'username')

        nice_cx = NiceCXNetwork()

        node_id_1 = nice_cx.create_node(node_name='node%s' % str(1), node_represents='ABC')
        node_id_2 = nice_cx.create_node(node_name='node%s' % str(2), node_represents='DEF')

        edge_id_1 = nice_cx.create_edge(edge_source=node_id_1, edge_target=node_id_2, edge_interaction='neighbor')

        citation1 = nice_cx.add_citation(id=0, title='Hi 1', identifier='pmid:28947956')
        nice_cx.add_edge_citations(edge_id_1, citation1.get('@id'))

        supports1 = nice_cx.add_support(id=0, text='Hi supports 1')
        nice_cx.add_edge_supports(edge_id_1, supports1.get('@id'))

        nice_cx.set_name('Citation testing')
        upload_message = nice_cx.upload_to(upload_server, upload_username, upload_password)
        print(upload_message)