Esempio n. 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
Esempio n. 2
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
Esempio n. 3
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'])
Esempio n. 5
0
 def test_add_node_types_in_network_to_report_empty_network(self):
     loader = NDExNciPidLoader(None)
     report = NetworkIssueReport('foo')
     net = NiceCXNetwork()
     net.set_name('foo')
     loader._add_node_types_in_network_to_report(net, report)
     self.assertEqual(set(), report.get_nodetypes())
    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'))
Esempio n. 7
0
 def test_set_normalizationversion(self):
     net = NiceCXNetwork()
     net.set_name('foo')
     loader = NDExNciPidLoader(None)
     loader._set_normalization_version(net)
     norm_attr = loadndexncipidloader.NORMALIZATIONVERSION_ATTRIB
     self.assertEqual('0.1', net.get_network_attribute(norm_attr)['v'])
    def test_bad_node_attr(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Test Network")
        fox_node = niceCx_creatures.create_node(node_name=453)
        niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values=['Red',"tree"])

        upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
 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)
Esempio n. 10
0
 def test_context(self):
     niceCx_creatures = NiceCXNetwork()
     niceCx_creatures.set_name("Test")
     niceCx_creatures.create_node(node_name="Fox")
     try:
         self.assertRaises(Exception, niceCx_creatures.set_context("hkjhnk"))
     except Exception as e:
         print('Bad context exception was caught')
Esempio n. 11
0
    def test_bad_node_attr(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Test Network")
        fox_node = niceCx_creatures.create_node(node_name=453)
        niceCx_creatures.add_node_attribute(property_of=fox_node, name='Color', values=['Red',"tree"])

        upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)
Esempio n. 12
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)
Esempio n. 13
0
 def test_set_generatedby_in_network_attributes(self):
     net = NiceCXNetwork()
     net.set_name('foo')
     loader = NDExNciPidLoader(None)
     loader._set_generatedby_in_network_attributes(net)
     norm_attr = ndexloadncipid.GENERATED_BY_ATTRIB
     self.assertTrue(' ' + str(ndexncipidloader.__version__) in
                     net.get_network_attribute(norm_attr)['v'])
Esempio n. 14
0
 def test_context(self):
     niceCx_creatures = NiceCXNetwork()
     niceCx_creatures.set_name("Test")
     niceCx_creatures.create_node(node_name="Fox")
     try:
         self.assertRaises(Exception, niceCx_creatures.set_context("hkjhnk"))
     except Exception as e:
         print('Bad context exception was caught')
Esempio n. 15
0
    def test_add_bad_net_attrs(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Test")
        niceCx_creatures.create_node(node_name="Fox")
        niceCx_creatures.set_network_attribute(name="version", values="1.0")
        print(niceCx_creatures)
        upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)

        self.assertTrue(upload_message)
Esempio n. 16
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))
Esempio n. 17
0
    def test_add_bad_net_attrs(self):
        niceCx_creatures = NiceCXNetwork()
        niceCx_creatures.set_name("Test")
        niceCx_creatures.create_node(node_name="Fox")
        niceCx_creatures.set_network_attribute(name="version", values="1.0")
        print(niceCx_creatures)
        upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)

        self.assertTrue(upload_message)
Esempio n. 18
0
    def test_one_node_network(self):
        net = NiceCXNetwork()
        node_one = net.create_node('first')

        net.set_node_attribute(node_one,
                               'booleanattrib',
                               'false',
                               type='boolean')
        net.set_node_attribute(node_one, 'integerattrib', '1', type='integer')
        net.set_node_attribute(node_one, 'doubleattrib', '2.0', type='double')
        net.set_node_attribute(node_one, 'longattrib', '3', type='long')
        net.set_node_attribute(node_one,
                               'stringattrib',
                               'false',
                               type='string')

        net.set_node_attribute(node_one,
                               'list_of_booleanattrib', ['True'],
                               type='list_of_boolean')
        net.set_node_attribute(node_one,
                               'list_of_doubleattrib', ['4.0'],
                               type='list_of_double')
        net.set_node_attribute(node_one,
                               'list_of_integerattrib', ['5'],
                               type='list_of_integer')
        net.set_node_attribute(node_one,
                               'list_of_longattrib', ['6'],
                               type='list_of_long')
        net.set_node_attribute(node_one,
                               'list_of_stringattrib', ['false'],
                               type='list_of_string')

        net.set_name('bob')
        fac = DefaultNetworkXFactory()
        g = fac.get_graph(net)
        self.assertEqual('bob', g.graph['name'])
        self.assertEqual(1, len(g))
        self.assertEqual(0, g.number_of_edges())
        self.assertTrue(0 in g)

        if NETWORKX_MAJOR_VERSION >= 2:
            nodelist = list(g.nodes(data=True))
        else:
            nodelist = g.nodes(data=True)

        self.assertEqual('false', nodelist[0][1]['booleanattrib'])
        self.assertEqual('1', nodelist[0][1]['integerattrib'])
        self.assertEqual('2.0', nodelist[0][1]['doubleattrib'])
        self.assertEqual('3', nodelist[0][1]['longattrib'])
        self.assertEqual('false', nodelist[0][1]['stringattrib'])

        self.assertEqual(['True'], nodelist[0][1]['list_of_booleanattrib'])
        self.assertEqual(['4.0'], nodelist[0][1]['list_of_doubleattrib'])
        self.assertEqual(['5'], nodelist[0][1]['list_of_integerattrib'])
        self.assertEqual(['6'], nodelist[0][1]['list_of_longattrib'])
        self.assertEqual(['false'], nodelist[0][1]['list_of_stringattrib'])
Esempio n. 19
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)
Esempio n. 20
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)
Esempio n. 21
0
 def test_set_wasderivedfrom(self):
     net = NiceCXNetwork()
     net.set_name('foo')
     loader = NDExNciPidLoader(None)
     loader._set_wasderivedfrom(net)
     derived_attr = ndexloadncipid.DERIVED_FROM_ATTRIB
     self.assertEqual('<a href="ftp://' +
                      ndexloadncipid.DEFAULT_FTP_HOST +
                      '/' + ndexloadncipid.DEFAULT_FTP_DIR + '/' +
                      'foo.owl.gz">foo.owl.gz</a>',
                      net.get_network_attribute(derived_attr)['v'])
Esempio n. 22
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
Esempio n. 23
0
 def test_update_network_system_properties_success(self):
     net = NiceCXNetwork()
     net.set_name('foo')
     loader = NDExNciPidLoader(None)
     loader._ndex = MagicMock()
     loader._ndex.set_network_system_properties = MagicMock(return_value='')
     perm_dict = {'index_level': 'ALL', 'showcase': True}
     loader._update_network_system_properties('http://public.ndexbio.org'
                                              '/v2/network/someid')
     loader._ndex.set_network_system_properties.assert_called_with(
         'someid', perm_dict)
 def test_one_node_no_edge_network(self):
     net = NiceCXNetwork()
     net.create_node('first')
     net.set_name('bob')
     fac = LegacyNetworkXVersionTwoPlusFactory()
     g = fac.get_graph(net)
     self.assertEqual('bob', g.graph['name'])
     self.assertEqual(1, len(g))
     self.assertEqual(0, g.number_of_edges())
     self.assertTrue('first' in g)
     nodelist = g.nodes(data=True)
     self.assertEqual('first', nodelist['first']['represents'])
Esempio n. 25
0
 def test_one_node_no_edge_network(self):
     net = NiceCXNetwork()
     net.create_node('first')
     net.set_name('bob')
     fac = LegacyNetworkXVersionTwoPlusFactory()
     g = fac.get_graph(net)
     self.assertEqual('bob', g.graph['name'])
     self.assertEqual(1, len(g))
     self.assertEqual(0, g.number_of_edges())
     self.assertTrue('first' in g)
     nodelist = g.nodes(data=True)
     self.assertEqual('first', nodelist['first']['represents'])
Esempio n. 26
0
 def test_apply_template_network_not_found(self):
     net = NiceCXNetwork()
     net.set_name('foo')
     creds = self.get_ndex_credentials_as_tuple()
     try:
         net.apply_template(creds['server'],
                            str(uuid.uuid4()),
                            username=creds['user'],
                            password=creds['password'])
         self.fail('Expected NDExNotFoundError')
     except NDExNotFoundError as ne:
         self.assertTrue('NDEx_Object_Not_Found_Exception', str(ne))
Esempio n. 27
0
    def test_set_type(self):
        loader = NDExNciPidLoader(None)
        net = NiceCXNetwork()
        net.set_name('foo')
        loader._set_type(net)
        self.assertEqual(['pathway'],
                         net.get_network_attribute('networkType')['v'])

        net.set_name(ndexloadncipid.COMPLETE_INTERACTION_NAME)
        loader._set_type(net)
        self.assertEqual(['pathway', 'interactome'],
                         net.get_network_attribute('networkType')['v'])
    def test_set_type(self):
        fargs = FakeArgs()
        fargs.conf = 'hi'
        fargs.profile = 'profile'
        fargs.datadir = '/foo'
        fargs.visibility = 'PUBLIC'

        loader = LoadSignorIntoNDEx(fargs, None)

        net = NiceCXNetwork()

        # test default
        net.set_name('foo')
        loader._set_type(net)
        res = net.get_network_attribute('networkType')
        self.assertEqual(['pathway', 'Signalling Pathway'], res['v'])

        # test disease pathway
        net.set_name('fsgs')
        loader._set_type(net)
        res = net.get_network_attribute('networkType')
        self.assertEqual(['pathway', 'Disease Pathway'], res['v'])

        # test cancer pathway
        net.set_name('prostate cancer')
        loader._set_type(net)
        res = net.get_network_attribute('networkType')
        self.assertEqual(['pathway', 'Cancer Pathway'], res['v'])

        #test interactome True
        net.set_name('some name')
        loader._set_type(net, is_human_fullpathway=True)
        res = net.get_network_attribute('networkType')
        self.assertEqual(['interactome', 'pathway', 'Signalling Pathway'],
                         res['v'])
 def test_one_node_no_edge_network(self):
     net = NiceCXNetwork()
     net.create_node('first')
     net.set_name('bob')
     fac = DefaultNetworkXFactory()
     g = fac.get_graph(net)
     self.assertEqual('bob', g.graph['name'])
     self.assertEqual(1, len(g))
     self.assertEqual(0, g.number_of_edges())
     self.assertTrue(0 in g)
     nodelist = g.nodes(data=True)
     if float(networkx.__version__) >= 2:
         self.assertEqual('first', nodelist[0]['name'])
     else:
         self.assertEqual('first', nodelist[0][1]['name'])
Esempio n. 30
0
 def test_one_node_no_edge_network(self):
     net = NiceCXNetwork()
     net.create_node('first')
     net.set_name('bob')
     fac = DefaultNetworkXFactory()
     g = fac.get_graph(net)
     self.assertEqual('bob', g.graph['name'])
     self.assertEqual(1, len(g))
     self.assertEqual(0, g.number_of_edges())
     self.assertTrue(0 in g)
     nodelist = g.nodes(data=True)
     if NETWORKX_MAJOR_VERSION >= 2:
         self.assertEqual('first', nodelist[0]['name'])
     else:
         self.assertEqual('first', nodelist[0][1]['name'])
Esempio n. 31
0
    def _create_empty_hierarchy_network(self,
                                        docker_image=None,
                                        algo_name=None,
                                        source_network=None,
                                        arguments=None):
        """
        Creates an empty hierarchy network with appropriate network attributes

        :param docker_image: Docker image, used to set value
                             `prov:wasGeneratedBy`
                             network attribute
        :type docker_image: str
        :param algo_name: Name of algorithm, used in `description` network
                          attribute
        :type algo_name: str
        :param source_network: Source network, name is used to set name of
                               network returned by this method
        :type source_network: :py:class:`ndex2.nice_cx_network.NiceCXNetwork`
        :return: Empty network except for network attributes
        :rtype: :py:class:`ndex2.nice_cx_network.NiceCXNetwork`
        """

        cust_params = ''
        if arguments is not None:
            for a in arguments.keys():
                cust_params += a + ' '
                if arguments[a] is not None:
                    cust_params += arguments[a] + ' '

        hier_net = NiceCXNetwork()
        hier_net.set_name(algo_name + '_(none)_' + source_network.get_name())
        hier_net.set_network_attribute('__CD_OriginalNetwork',
                                       values='0',
                                       type='long')
        hier_net.set_network_attribute(
            'description',
            values='Original network: ' + source_network.get_name() + '\n ' +
            'Algorithm used for '
            'community detection: ' + algo_name + '\n ' +
            'Edge table column used '
            'as weight: (none)\n ' + 'CustomParameters: {' + cust_params + '}')
        hier_net.set_network_attribute('prov:wasDerivedFrom',
                                       values=source_network.get_name())
        hier_net.set_network_attribute('prov:wasGeneratedBy',
                                       values='cdapsutil ' +
                                       cdapsutil.__version__ + ' ' +
                                       'Docker image: ' + docker_image)
        return hier_net
Esempio n. 32
0
 def test_update_network_system_properties_failure(self):
     net = NiceCXNetwork()
     net.set_name('foo')
     loader = NDExNciPidLoader(None)
     loader._ndex = MagicMock()
     loader._networksystemproperty_retry = 1
     loader._networksystemproperty_wait = 0
     loader._ndex.set_network_system_properties = MagicMock(return_value='error')
     perm_dict = {'index_level': 'ALL',
                  'showcase': True}
     res = loader._update_network_system_properties('http://public.'
                                                    'ndexbio.org'
                                                    '/v2/network/someid')
     self.assertTrue('After 1 retries' in res)
     loader._ndex.set_network_system_properties.assert_called_with('someid',
                                                                   perm_dict)
Esempio n. 33
0
    def test_set_network_attributes_from_style_network_complete_net(self):
        net = NiceCXNetwork()
        net.set_name(ndexloadncipid.COMPLETE_INTERACTION_NAME)

        templatenet = NiceCXNetwork()
        templatenet.set_name('well')
        templatenet.set_network_attribute('description', values='hi', type='string')
        templatenet.set_network_attribute('organism', values='some', type='string')
        loader = NDExNciPidLoader(None)
        loader._template = templatenet

        res = loader._set_network_attributes_from_style_network(net)
        self.assertEqual(0, len(res))

        self.assertTrue('This network' in net.get_network_attribute('description')['v'])
        self.assertEqual('some', net.get_network_attribute('organism')['v'])
Esempio n. 34
0
    def test_set_network_attributes_from_style_network_description_set_org_not(self):
        net = NiceCXNetwork()
        net.set_name('foo')

        templatenet = NiceCXNetwork()
        templatenet.set_name('well')
        templatenet.set_network_attribute('description', values='hi', type='string')
        loader = NDExNciPidLoader(None)
        loader._template = templatenet

        res = loader._set_network_attributes_from_style_network(net)
        self.assertEqual(1, len(res))
        self.assertTrue('organism network' in res[0])

        self.assertEqual('hi', net.get_network_attribute('description')['v'])
        self.assertEqual(None, net.get_network_attribute('organism'))
    def test_one_node_no_edge_network_legacytrue(self):
        net = NiceCXNetwork()
        net.create_node('first')
        net.set_name('bob')
        fac = DefaultNetworkXFactory(legacymode=True)
        g = fac.get_graph(net)
        self.assertEqual('bob', g.graph['name'])
        self.assertEqual(1, len(g))
        self.assertEqual(0, g.number_of_edges())
        self.assertTrue(0 in g)

        if float(networkx.__version__) >= 2:
            nodelist = list(g.nodes(data=True))
        else:
            nodelist = g.nodes(data=True)

        self.assertEqual('first', nodelist[0][1]['name'])
Esempio n. 36
0
    def test_update_network_system_properties_exception_then_okay(self):
        net = NiceCXNetwork()
        net.set_name('foo')
        loader = NDExNciPidLoader(None)
        loader._ndex = MagicMock()
        loader._networksystemproperty_wait = 0
        loader._ndex.set_network_system_properties = MagicMock(side_effect=[Exception('foo'), ''])
        perm_dict = {'index_level': 'ALL',
                     'showcase': True}
        res = loader._update_network_system_properties('http://public.'
                                                       'ndexbio.org'
                                                       '/v2/network/someid')
        self.assertEqual(None, res)
        loader._ndex.set_network_system_properties.assert_called_with('someid',
                                                                      perm_dict)

        cnt = loader._ndex.set_network_system_properties.call_count
        self.assertEqual(2, cnt)
    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'])
Esempio n. 38
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)
Esempio n. 39
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)
    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'])
Esempio n. 41
0
    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'))
Esempio n. 42
0
def create_nice_cx_from_pandas(df, source_field=None, target_field=None,
                               source_node_attr=[], target_node_attr=[],
                               edge_attr=[], edge_interaction=None, source_represents=None, target_represents=None):
    """
    Create a NiceCXNetwork from a pandas dataframe in which each row
    specifies one edge in the network.

    If only the df argument is provided the dataframe is treated as 'SIF' format,
    where the first two columns specify the source and target node ids of the edge
    and all other columns are ignored. The edge interaction is defaulted to "interacts-with"

    If both the source_field and target_field arguments are provided, the those and any other
    arguments refer to headers in the dataframe, controlling the mapping of columns to
    the attributes of nodes, and edges in the resulting NiceCXNetwork. If a header is not
    mapped the corresponding column is ignored. If the edge_interaction is not specified it
    defaults to "interacts-with"

    :param df: pandas dataframe to process
    :param source_field: header name specifying the name of the source node.
    :param target_field: header name specifying the name of the target node.
    :param source_node_attr: list of header names specifying attributes of the source node.
    :param target_node_attr: list of header names specifying attributes of the target node.
    :param edge_attr: list of header names specifying attributes of the edge.
    :param edge_interaction: the relationship between the source node and the target node, defaulting to "interacts-with"
    :return: NiceCXNetwork
    """

    my_nicecx = NiceCXNetwork()

    # ====================================================
    # IF NODE FIELD NAME (SOURCE AND TARGET) IS PROVIDED
    # THEN USE THOSE FIELDS OTHERWISE USE INDEX 0 & 1
    # ====================================================
    my_nicecx.set_name('Pandas Upload')
    #my_nicecx.add_metadata_stub('networkAttributes')
    count = 0
    source_predicate = ''
    target_predicate = ''

    niceCxBuilder = NiceCXBuilder()

    if source_field and target_field:
        for index, row in df.iterrows():
            if count % 1000 == 0:
                print(count)
            count += 1
            # =============
            # ADD NODES
            # =============

            if source_represents is not None:
                source_node_id = niceCxBuilder.add_node(name=source_predicate + str(row[source_field]), represents=source_predicate + str(row[source_represents]))
            else:
                source_node_id = niceCxBuilder.add_node(name=source_predicate + str(row[source_field]), represents=source_predicate + str(row[source_field]))

            if target_represents is not None:
                target_node_id = niceCxBuilder.add_node(name=target_predicate + str(row[target_field]), represents=target_predicate + str(row[target_represents]))
            else:
                target_node_id = niceCxBuilder.add_node(name=target_predicate + str(row[target_field]), represents=target_predicate + str(row[target_field]))

            # =============
            # ADD EDGES
            # =============
            if edge_interaction:
                if row.get(edge_interaction):
                    use_this_interaction = row[edge_interaction]
                else:
                    use_this_interaction = edge_interaction
            else:
                use_this_interaction = 'interacts-with'

            niceCxBuilder.add_edge(id=index, source=source_node_id, target=target_node_id, interaction=use_this_interaction)

            # ==============================
            # ADD SOURCE NODE ATTRIBUTES
            # ==============================
            for sp in source_node_attr:

                #TODO - need to be smarter about how data type is inferred
                #row[sp], attr_type = _infer_data_type(row[sp])

                attr_type = None

                #attr_type = None
                #if type(row[sp]) is float and math.isnan(row[sp]):
                #    row[sp] = ''
                #    attr_type = 'float'
                #elif type(row[sp]) is float and math.isinf(row[sp]):
                #    row[sp] = 'Inf'
                #    attr_type = 'float'
                #elif type(row[sp]) is float:
                #    attr_type = 'float'
                #elif isinstance(row[sp], int):
                #    attr_type = 'integer'
                if sp == 'citation' and not isinstance(row[sp], list):
                    row[sp] = [row[sp]]
                    attr_type = 'list_of_string'
                niceCxBuilder.add_node_attribute(source_node_id, sp, str(row[sp]), type=attr_type)

            # ==============================
            # ADD TARGET NODE ATTRIBUTES
            # ==============================
            for tp in target_node_attr:
                #TODO - need to be smarter about how data type is inferred
                #row[tp], attr_type = _infer_data_type(row[tp])

                attr_type = None

                #attr_type = None
                #if type(row[tp]) is float and math.isnan(row[tp]):
                #    row[tp] = ''
                #    attr_type = 'float'
                #elif type(row[tp]) is float and math.isinf(row[tp]):
                #    row[tp] = 'Inf'
                #    attr_type = 'float'
                #elif type(row[tp]) is float:
                #    attr_type = 'float'
                #elif isinstance(row[tp], int):
                #    attr_type = 'integer'

                if tp == 'citation' and not isinstance(row[tp], list):
                    row[tp] = [row[tp]]
                    attr_type = 'list_of_string'
                niceCxBuilder.add_node_attribute(target_node_id, tp, str(row[tp]), type=attr_type)

            # ==============================
            # ADD EDGE ATTRIBUTES
            # ==============================
            for ep in edge_attr:
                #TODO - need to be smarter about how data type is inferred
                #row[ep], attr_type = _infer_data_type(row[ep])

                attr_type = None

                #attr_type = None
                #if type(row[ep]) is float and math.isnan(row[ep]):
                #    row[ep] = ''
                #    attr_type = 'float'
                #elif type(row[ep]) is float and math.isinf(row[ep]):
                #    row[ep] = 'INFINITY'
                #    attr_type = 'float'

                if ep == 'citation' and not isinstance(row[ep], list):
                    row[ep] = [row[ep]]
                    attr_type = 'list_of_string'

                niceCxBuilder.add_edge_attribute(property_of=index, name=ep, values=row[ep], type=attr_type)

    else:
        for index, row in df.iterrows():
            # =============
            # ADD NODES
            # =============
            source_node_id = niceCxBuilder.add_node(name=str(row[0]), represents=str(row[0]))

            target_node_id = niceCxBuilder.add_node(name=str(row[1]), represents=str(row[1]))

            # =============
            # ADD EDGES
            # =============
            if len(row) > 2:
                niceCxBuilder.add_edge(id=index, source=source_node_id, target=target_node_id, interaction=row[2])
            else:
                niceCxBuilder.add_edge(id=index, source=source_node_id, target=target_node_id, interaction='interacts-with')

    return niceCxBuilder.get_nice_cx()  # my_nicecx
Esempio n. 43
0
 def test_set_name(self):
     niceCx_creatures = NiceCXNetwork()
     niceCx_creatures.set_name(43)
     niceCx_creatures.create_node(node_name="tree")
     upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
     self.assertTrue(upload_message)
Esempio n. 44
0
 def test_bad_name_add_node(self):
     niceCx_creatures = NiceCXNetwork()
     niceCx_creatures.set_name("Test Network")
     node1_id = niceCx_creatures.create_node(node_name=54, node_represents="Tree")
     upload_message = niceCx_creatures.upload_to(upload_server, upload_username, upload_password)
     self.assertTrue(upload_message)
Esempio n. 45
0
def create_nice_cx_from_networkx(G):
    """
    Creates a NiceCXNetwork based on a networkx graph. The resulting NiceCXNetwork
    contains the nodes edges and their attributes from the networkx graph and also
    preserves the graph 'pos' attribute as a CX cartesian coordinates aspect.
    Node name is taken from the networkx node id. Node 'represents' is
    taken from the networkx node attribute 'represents'

    :param G: networkx graph
    :type G: networkx graph
    :return: NiceCXNetwork
    :rtype: NiceCXNetwork
    """

    niceCxBuilder = NiceCXBuilder()
    if G is None:
        raise Exception('Networkx input is empty')

    my_nicecx = NiceCXNetwork()

    if G.graph.get('name'):
        my_nicecx.set_name(G.graph.get('name'))
    else:
        my_nicecx.set_name('created from networkx')

    my_nicecx.add_metadata_stub('networkAttributes')


    #=========================================
    # Check to see if the node label is same
    # (case insensitive) as 'name' attribute
    #=========================================
    #use_node_label = False
    #for n, d in G.nodes_iter(data=True):
    #    if not isinstance(n, int) and d and d.get('name'):
    #        if n.lower() == d.get('name').lower():
    #            use_node_label = True

    #    break

    for n, d in G.nodes(data=True):
        # =============
        # ADD NODES
        # =============
        #if d and d.get('name'):
        #    if isinstance(n, int):
        #        node_id = niceCxBuilder.add_node(name=d.get('name'),represents=d.get('name'), id=n, map_node_ids=True)
        #    else:
        #        # If networkx node is of type string then maybe the 'name' atribute is no longer accurate
        #        if use_node_label:
        #            node_id = niceCxBuilder.add_node(name=n,represents=n, map_node_ids=True)
        #        else:
        #            node_id = niceCxBuilder.add_node(name=d.get('name'),represents=d.get('name'), map_node_ids=True)
        #else:
        if isinstance(n, int):
            node_id = niceCxBuilder.add_node(name=n,represents=d.get('represents'), id=n, map_node_ids=True)
        else:
            node_id = niceCxBuilder.add_node(name=n, represents=d.get('represents'), map_node_ids=True)

        # ======================
        # ADD NODE ATTRIBUTES
        # ======================
        for k, v in d.items():
            use_this_value, attr_type = niceCxBuilder._infer_data_type(v, split_string=True)

            if k == 'citation' and not isinstance(use_this_value, list):
                use_this_value = [use_this_value]
                attr_type = 'list_of_string'
            if use_this_value is not None:
                niceCxBuilder.add_node_attribute(node_id, k, use_this_value, type=attr_type)

    index = 0
    for u, v, d in G.edges(data=True):
        # =============
        # ADD EDGES
        # =============
        if d.get('interaction') is None or d.get('interaction') == 'null':
            interaction = 'neighbor-of'
        else:
            interaction = d.get('interaction')

        if isinstance(u, int):
            niceCxBuilder.add_edge(source=u, target=v, interaction=interaction, id=index)
        else:
            niceCxBuilder.add_edge(source=niceCxBuilder.node_id_lookup.get(u), target=niceCxBuilder.node_id_lookup.get(v),
                               interaction=interaction, id=index)

        # ==============================
        # ADD EDGE ATTRIBUTES
        # ==============================
        for k, val in d.items():
            if k != 'interaction':
                use_this_value, attr_type = niceCxBuilder._infer_data_type(val, split_string=True)

                if k == 'citation' and not isinstance(use_this_value, list):
                    use_this_value = [use_this_value]
                    attr_type = 'list_of_string'

                if use_this_value is not None:
                    niceCxBuilder.add_edge_attribute(property_of=index, name=k, values=use_this_value, type=attr_type)

        index += 1

    if hasattr(G, 'pos'):
        aspect = _create_cartesian_coordinates_aspect_from_networkx(G)
        niceCxBuilder.add_opaque_aspect('cartesianLayout', aspect)

    return niceCxBuilder.get_nice_cx()