Пример #1
0
    def test_apply_style_with_node_and_edge_specific_visual_values(self):
        wntcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.WNT_SIGNAL_FILE)
        darkcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.DARKTHEMENODE_FILE)

        wntcx.apply_style_from_network(darkcx)
        wnt_vis_aspect = wntcx.get_opaque_aspect(NiceCXNetwork.CY_VISUAL_PROPERTIES)
        self.assertEqual(3, len(wnt_vis_aspect))
    def test_diffusion_local_and_remote(self):
        net_cx = ndex2.create_nice_cx_from_file(
            TestIntegrationHeatDiffusion.TEST_NETWORK)
        diffuser = HeatDiffusion()
        diffuser.add_seed_nodes_by_node_name(net_cx, seed_nodes=['E', 'M'])
        local_diffuse_cx = diffuser.run_diffusion(net_cx)

        local_node_dict = self.get_dict_of_node_name_to_diffusion_rank(
            local_diffuse_cx)

        r2_cx = ndex2.create_nice_cx_from_file(
            TestIntegrationHeatDiffusion.TEST_NETWORK)
        diffuser.add_seed_nodes_by_node_name(r2_cx, seed_nodes=['E', 'M'])
        remote_diffuse_cx = diffuser.run_diffusion(r2_cx, via_service=True)

        remote_node_dict = self.get_dict_of_node_name_to_diffusion_rank(
            remote_diffuse_cx)

        # Couple issues found
        # there is some precision conversion issues going from str to float
        # when calling the remote service and the rank is indeterminate when
        # heat values are the same which is the case for the TEST_NETWORK
        # used here so we are testing rank only for the seeds 'E' and 'M'
        self.assertEqual(local_node_dict['E'][0], remote_node_dict['E'][0])
        self.assertEqual(local_node_dict['M'][0], remote_node_dict['M'][0])
        for node_name in local_node_dict.keys():
            self.assertAlmostEqual(float(local_node_dict[node_name][1]),
                                   float(remote_node_dict[node_name][1]),
                                   places=10)
Пример #3
0
 def test_apply_style_on_network_from_old_visual_aspect_network(self):
     glypy = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.GLYPICAN_FILE)
     wntcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork
                                            .WNT_SIGNAL_FILE)
     wntcx.apply_style_from_network(glypy)
     wnt_aspect = wntcx.get_opaque_aspect(NiceCXNetwork
                                          .CY_VISUAL_PROPERTIES)
     self.assertEqual(3, len(wnt_aspect))
Пример #4
0
 def test_apply_style_from_network_no_style(self):
     wntcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.WNT_SIGNAL_FILE)
     wntcx.remove_opaque_aspect(NiceCXNetwork.CY_VISUAL_PROPERTIES)
     darkcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.DARKTHEME_FILE)
     try:
         darkcx.apply_style_from_network(wntcx)
         self.fail('Expected NDexError')
     except NDExError as ne:
         self.assertEqual('No visual style found in network', str(ne))
Пример #5
0
    def test_apply_style_from_wnt_network_to_dark_network(self):
        darkcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.DARKTHEME_FILE)
        dark_vis_aspect = darkcx.get_opaque_aspect(NiceCXNetwork.CY_VISUAL_PROPERTIES)
        self.assertEqual(9, len(dark_vis_aspect))
        wntcx = ndex2.create_nice_cx_from_file(TestNiceCXNetwork.WNT_SIGNAL_FILE)
        wnt_vis_aspect = wntcx.get_opaque_aspect(NiceCXNetwork.CY_VISUAL_PROPERTIES)
        self.assertEqual(3, len(wnt_vis_aspect))

        darkcx.apply_style_from_network(wntcx)
        new_dark_vis_aspect = darkcx.get_opaque_aspect(NiceCXNetwork.CY_VISUAL_PROPERTIES)
        self.assertEqual(3, len(new_dark_vis_aspect))
Пример #6
0
    def test_creating_network(self):
        temp_dir = tempfile.mkdtemp()
        try:
            here = os.path.dirname(__file__)
            tsvfile = os.path.join(here, 'ctd_test.tsv')
            with open(tsvfile, 'r') as tsvfile:
                tmpcx = 'out.cx'
                with open(tmpcx, "w") as out:
                    nicecx = ndex2.create_nice_cx_from_file(
                        os.path.join(here, 'gene-disease-style.cx'))
                    loader = StreamTSVLoader(
                        os.path.join(
                            here,
                            'ctd-gene-disease-2019-norm-plan-collapsed.json'),
                        nicecx)
                    loader.write_cx_network(
                        tsvfile,
                        out, [{
                            'n': 'name',
                            'v': "CTD: gene-disease association (Human)"
                        }, {
                            'n': 'version',
                            'v': "0.0.1"
                        }],
                        batchsize=4)

                nicecx = ndex2.create_nice_cx_from_file(tmpcx)
                self.assertEqual(len(nicecx.networkAttributes), 3)
                self.assertEqual(len(nicecx.edges), 49)
                self.assertEqual(len(nicecx.nodes), 50)
                node_attr_cnt = 0
                for key, value in nicecx.nodeAttributes.items():
                    node_attr_cnt += len(value)
                self.assertEqual(node_attr_cnt, 50)
                edge_attr_cnt = 0
                for key, value in nicecx.edgeAttributes.items():
                    edge_attr_cnt += len(value)
                self.assertEqual(edge_attr_cnt, 147)
                self.assertEqual(
                    len(nicecx.opaqueAspects.get("cyVisualProperties")), 3)
                with open(
                        os.path.join(
                            here, 'ctd-gene-disease-2019-'
                            'norm-plan-collapsed.json'), 'r') as f:
                    load_plan = json.load(f)
                cdata = json.loads(
                    nicecx.get_network_attribute('@context')['v'])

                self.assertEqual(load_plan['context'], cdata)
        finally:
            shutil.rmtree(temp_dir)
 def _get_style_network_from_file(self):
     try:
         self._style_network = ndex2.create_nice_cx_from_file(
             self._style_file)
     except Exception as e:
         print(e)
         print("Error while loading style network from file. "
               "Default style network will be used instead.")
         try:
             self._style_network = ndex2.create_nice_cx_from_file(
                 _get_default_style_file_name())
         except Exception as e:
             print(e)
             print("Error while loading default style network from file. "
                   "No style will be applied.")
Пример #8
0
    def test_network_permissions(self):
        client = self.get_ndex2_client()
        # create network and add it
        net = ndex2.create_nice_cx_from_file(TestClient.WNT_SIGNAL_FILE)
        netname = 'ndex2-client integration test network' + str(datetime.now())
        net.set_name(netname)
        res = client.save_new_network(net.to_cx(), visibility='PUBLIC')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)

            # verify network was uploaded
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(netid, netsum['externalId'])
            self.assertEqual(netname, netsum['name'])
            self.assertEqual('PUBLIC', netsum['visibility'])
            self.assertEqual(False, netsum['isReadOnly'])
            self.assertEqual(74, netsum['edgeCount'])
            self.assertEqual(32, netsum['nodeCount'])
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('NONE', netsum['indexLevel'])

            # make network private
            self.assertEqual('', client.make_network_private(netid))
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual('PRIVATE', netsum['visibility'])

            # make network public
            self.assertEqual('', client.make_network_public(netid))
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual('PUBLIC', netsum['visibility'])

            # make network readonly
            self.assertEqual('', client.set_read_only(netid, True))
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(True, netsum['isReadOnly'])

            self.assertEqual('', client.set_read_only(netid, False))
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(False, netsum['isReadOnly'])

            # make network indexed and showcased
            netperm = {'index_level': 'ALL',
                       'showcase': True}
            self.assertEqual('', client.set_network_system_properties(netid,
                                                                      netperm))
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(True, netsum['isShowcase'])
            self.assertEqual('ALL', netsum['indexLevel'])

            netperm = {'index_level': 'META',
                       'showcase': False}
            self.assertEqual('', client.set_network_system_properties(netid,
                                                                      netperm))
            netsum = client.get_network_summary(network_id=netid)
            self.assertEqual(False, netsum['isShowcase'])
            self.assertEqual('META', netsum['indexLevel'])
        finally:
            # delete network
            client.delete_network(netid)
Пример #9
0
    def test_communityToColors(self):
        filelist = glob.glob(os.path.dirname(__file__) + '/test_cx/*')
        for a in filelist:
            testcx_out = ndex2.create_nice_cx_from_file(a)

            v_color = [
                str(testcx_out.get_node_attribute_value(i, 'colors_community'))
                for i in range(len(testcx_out.get_nodes()))
            ]

            e_color = [
                str(testcx_out.get_edge_attribute_value(i, 'colors_community'))
                for i in range(len(testcx_out.get_edges()))
            ]

            v_community = [
                int(testcx_out.get_node_attribute_value(i, 'community'))
                for i in range(len(testcx_out.get_nodes()))
            ]

            e_community = [
                int(testcx_out.get_edge_attribute_value(i, 'community'))
                for i in range(len(testcx_out.get_edges()))
            ]

            self.assertEqual(v_color, communityToColors('hls', v_community))
            self.assertEqual(e_color, communityToColors('hls', e_community))
Пример #10
0
    def get_human_hiv_as_nice_cx(self):
        """

        :return:
        """
        return ndex2.create_nice_cx_from_file(os.path.join(self.get_data_dir(),
                                                           'hiv_human_ppi.cx'))
Пример #11
0
    def test_network_without_represent(self):
        temp_dir = tempfile.mkdtemp()
        try:
            here = os.path.dirname(__file__)
            tsvfile = os.path.join(here,
                                   'BRCA-2012-TP53-pathway.txt_with_a_b.csv')
            with open(tsvfile, 'r') as tsvfile:
                #with open (os.path.join ( temp_dir, "out.cx"),"w") as out:
                tmpcx = 'out.cx'
                with open(tmpcx, "w") as out:
                    loader = StreamTSVLoader(
                        os.path.join(here, 'BRCA-2012-loadplan.json'), None)
                    loader.write_cx_network(tsvfile, out, [{
                        'n': 'name',
                        'v': "test pathway"
                    }, {
                        'n': 'version',
                        'v': "0.0.1"
                    }])

                nicecx = ndex2.create_nice_cx_from_file(tmpcx)
                self.assertEqual(len(nicecx.networkAttributes), 3)
                self.assertEqual(len(nicecx.edges), 8)
                self.assertEqual(len(nicecx.nodes), 9)
                self.assertEqual(nicecx.nodes[0].get('r'), None)
                node_attr_cnt = 0
                for key, value in nicecx.nodeAttributes.items():
                    node_attr_cnt += len(value)
                self.assertEqual(node_attr_cnt, 25)

        finally:
            shutil.rmtree(temp_dir)
Пример #12
0
 def _load_style_template(self):
     """
     Loads the CX network specified by self._args.style into self._template
     :return:
     """
     self._template = ndex2.create_nice_cx_from_file(
         os.path.abspath(self._args.style))
    def test_glypican_network_legacyfalse_and_multigraph_passed_in(self):
        net = ndex2.create_nice_cx_from_file(
            TestDefaultNetworkXFactory.GLYPICAN_FILE)
        fac = DefaultNetworkXFactory()
        g = fac.get_graph(net, networkx_graph=networkx.MultiGraph())
        self.assertEqual('Glypican 2 network', g.graph['name'])
        self.assertEqual('', g.graph['reference'])
        self.assertEqual('Mirko von Elstermann', g.graph['author'])
        self.assertEqual('Jorge Filmus', g.graph['reviewers'])
        self.assertEqual('glypican_2pathway', g.graph['labels'])
        self.assertEqual('APR-2018', g.graph['version'])
        self.assertEqual('human', g.graph['organism'])

        self.assertEqual(
            '<i>Glypican 2 network</i> was derived from '
            'the latest BioPAX3 version of the Pathway '
            'Interaction Database (PID) curated by NCI/Nature. '
            'The BioPAX was first converted to Extended Binary '
            'SIF (EBS) by the PAXTools v5 utility. It was then '
            'processed to remove redundant edges, to add a '
            '\'directed flow\' layout, and to add a graphic '
            'style using Cytoscape Visual Properties. This '
            'network can be found in searches using its original '
            'PID accession id, present in the \'labels\' '
            'property.', g.graph['description'])

        self.assertEqual(2, len(g))
        self.assertEqual(1, g.number_of_edges())
        self.assertTrue(0 in g)
        self.assertTrue(1 in g)
        if float(networkx.__version__) >= 2:
            nodelist = list(g.nodes(data=True))
            edgelist = list(g.edges(data=True))
        else:
            nodelist = g.nodes(data=True)
            edgelist = g.edges(data=True)

        self.assertEqual('MDK', nodelist[0][1]['name'])
        self.assertEqual('Protein', nodelist[0][1]['type'])
        aliaslist = nodelist[0][1]['alias']
        self.assertEqual(2, len(aliaslist))
        self.assertTrue('uniprot knowledgebase:Q2LEK4' in aliaslist)
        self.assertTrue('uniprot knowledgebase:Q9UCC7' in aliaslist)

        self.assertEqual('GPC2', nodelist[1][1]['name'])
        self.assertEqual('Protein', nodelist[1][1]['type'])
        aliaslist = nodelist[1][1]['alias']
        self.assertEqual(1, len(aliaslist))
        self.assertTrue('uniprot knowledgebase:Q8N158' in aliaslist)

        self.assertEqual(0, edgelist[0][0])
        self.assertEqual(1, edgelist[0][1])
        self.assertEqual('in-complex-with', edgelist[0][2]['interaction'])
        self.assertEqual('false', edgelist[0][2]['directed'])

        # check coordinates
        self.assertTrue((g.pos[0][0] + 398.3) < 1.0)
        self.assertTrue((g.pos[0][1] - 70.71) < 1.0)
        self.assertTrue((g.pos[1][0] + 353.49) < 1.0)
        self.assertTrue((g.pos[1][1] - 70.71) < 1.0)
    def test_glypican_network_legacymode_true(self):
        net = ndex2.create_nice_cx_from_file(TestDefaultNetworkXFactory.GLYPICAN_FILE)
        fac = DefaultNetworkXFactory(legacymode=True)
        g = fac.get_graph(net)
        self.assertTrue(isinstance(g, networkx.Graph))
        self.assertEqual('Glypican 2 network', g.graph['name'])
        self.assertEqual('', g.graph['reference'])
        self.assertEqual('Mirko von Elstermann', g.graph['author'])
        self.assertEqual('Jorge Filmus', g.graph['reviewers'])
        self.assertEqual('glypican_2pathway', g.graph['labels'])
        self.assertEqual('APR-2018', g.graph['version'])
        self.assertEqual('human', g.graph['organism'])

        self.assertEqual('<i>Glypican 2 network</i> was derived from '
                         'the latest BioPAX3 version of the Pathway '
                         'Interaction Database (PID) curated by NCI/Nature. '
                         'The BioPAX was first converted to Extended Binary '
                         'SIF (EBS) by the PAXTools v5 utility. It was then '
                         'processed to remove redundant edges, to add a '
                         '\'directed flow\' layout, and to add a graphic '
                         'style using Cytoscape Visual Properties. This '
                         'network can be found in searches using its original '
                         'PID accession id, present in the \'labels\' '
                         'property.', g.graph['description'])

        self.assertEqual(2, len(g))
        self.assertEqual(1, g.number_of_edges())
        self.assertTrue(0 in g)
        self.assertTrue(1 in g)

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

        self.assertEqual('MDK', nodelist[0][1]['name'])
        self.assertEqual('Protein', nodelist[0][1]['type'])
        aliaslist = nodelist[0][1]['alias']
        self.assertEqual(2, len(aliaslist))
        self.assertTrue('uniprot knowledgebase:Q2LEK4' in aliaslist)
        self.assertTrue('uniprot knowledgebase:Q9UCC7' in aliaslist)

        self.assertEqual('GPC2', nodelist[1][1]['name'])
        self.assertEqual('Protein', nodelist[1][1]['type'])
        aliaslist = nodelist[1][1]['alias']
        self.assertEqual(1, len(aliaslist))
        self.assertTrue('uniprot knowledgebase:Q8N158' in aliaslist)

        self.assertEqual(0, edgelist[0][0])
        self.assertEqual(1, edgelist[0][1])
        self.assertEqual('in-complex-with', edgelist[0][2]['interaction'])
        self.assertEqual('false', edgelist[0][2]['directed'])

        # check coordinates
        self.assertTrue((g.pos[0][0] + 398.3) < 1.0)
        self.assertTrue((g.pos[0][1] - 70.71) < 1.0)
        self.assertTrue((g.pos[1][0] + 353.49) < 1.0)
        self.assertTrue((g.pos[1][1] - 70.71) < 1.0)
Пример #15
0
    def test_write_nice_cx_to_file(self):
        temp_dir = tempfile.mkdtemp()
        try:
            p = MagicMock()
            p.datadir = temp_dir
            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._network = NiceCXNetwork()
            loader._network.create_node('hi')
            loader._network.set_name('name')
            cxfile = os.path.join(temp_dir, 'some.cx')
            loader._write_nice_cx_to_file(cxfile)

            self.assertTrue(os.path.isfile(cxfile))

            checknet = ndex2.create_nice_cx_from_file(cxfile)
            self.assertEqual('name', checknet.get_name())

        finally:
            shutil.rmtree(temp_dir)
Пример #16
0
 def get_nice_cx_network(self):
     """
     Gets :py:class:`~ndex2.nice_cx_network.NiceCXNetwork`
     generated from CX file passed into constructor of this object
     :return:
     :rtype: :py:class:`~ndex2.nice_cx_network.NiceCXNetwork`
     """
     return ndex2.create_nice_cx_from_file(self._inputcxfile)
Пример #17
0
    def test_main(self):
        """Tests main function"""

        # try where loading config is successful
        try:
            #temp_dir = tempfile.mkdtemp()
            #confile = os.path.join(temp_dir, 'some.conf')
            #with open(confile, 'w') as f:
            #    f.write("""[hi]
            #    {user} = bob
            #    {pw} = smith
            #    {server} = dev.ndexbio.org""".format(user=NDExUtilConfig.USER,
            #                                         pw=NDExUtilConfig.PASSWORD,
            #                                         server=NDExUtilConfig.SERVER))

            #res = ndexloadtcga.main(['myprog.py', '--profile', 'ndextcgaloader'])
            #self.assertEqual(res, 0)

            with open(self._networklistfile, 'r') as networks:
                list_of_network_files = networks.read().splitlines()
                list_of_network_files_in_cx = [
                    network.replace('.txt', '.cx')
                    for network in list_of_network_files
                ]

            with open(self._loadplan_path, 'r') as f:
                self._loadplan = json.load(f)

            count = 1

            self.NDExTCGALoader.parse_load_plan()
            self.NDExTCGALoader.prepare_report_directory()

            for network_file in list_of_network_files:
                network_file_in_cx = network_file.replace('.txt', '.cx')

                # generate NiceCX from network_file
                df, network_description, id_to_gene_dict = self.NDExTCGALoader.get_pandas_dataframe(
                    network_file)

                network = self.NDExTCGALoader.generate_nice_cx_from_panda_df(
                    df, network_file, network_description, id_to_gene_dict)

                path_to_sample_network = os.path.join(
                    self._sample_networks_in_tests_dir, network_file_in_cx)

                network_sample_in_cx = ndex2.create_nice_cx_from_file(
                    path_to_sample_network)

                self.validate_network(network, network_sample_in_cx,
                                      network_file_in_cx)

                print('{}) network {} passed'.format(
                    count, network_file.replace('.txt', '')))
                count += 1

        finally:
            print('done')
Пример #18
0
def get_passthrough_style():
    global passthrough_style
    if passthrough_style is None:
        top_level = os.path.dirname(os.path.abspath(inspect.getfile(ddot)))
        #with io.open(os.path.join(top_level, 'passthrough_style.cx')) as f:
        #   passthrough_style = ndex2.create_nice_cx_from_file(f)
        passthrough_style = ndex2.create_nice_cx_from_file(
            os.path.join(top_level, 'passthrough_style.cx'))
    return passthrough_style
Пример #19
0
    def test_darktheme_network_legacyfalse(self):
        net = ndex2.create_nice_cx_from_file(TestDefaultNetworkXFactory
                                             .DARKTHEME_FILE)
        fac = DefaultNetworkXFactory()
        g = fac.get_graph(net)
        self.assertEqual('Dark theme final version', g.graph['name'])
        self.assertTrue('Perfetto L.,' in g.graph['reference'])
        self.assertEqual('Theodora Pavlidou', g.graph['author'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertEqual('18-Jan-2019', g.graph['version'])
        self.assertEqual('Human, 9606, H**o sapiens', g.graph['organism'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertTrue('epidermal growth factor' in g.graph['description'])

        self.assertEqual(34, len(g))
        self.assertEqual(116, g.number_of_edges())
        self.assertTrue(1655 in g)
        self.assertTrue(1622 in g)

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

        stat_three_index = -1
        for i in range(0, len(nodelist)):
            if nodelist[i][1]['name'] == 'STAT3':
                stat_three_index = i
                break
        self.assertEqual('STAT3', nodelist[stat_three_index][1]['name'])
        self.assertEqual('protein', nodelist[stat_three_index][1]['type'])
        self.assertEqual('uniprot:P40763',
                         nodelist[stat_three_index][1]['represents'])

        sixteenfiftyfiveedge = -1
        for i in range(len(edgelist)):
            if edgelist[i][0] == 1655 and edgelist[i][1] == 1654:
                sixteenfiftyfiveedge = i
                break

        self.assertTrue((1655 == edgelist[sixteenfiftyfiveedge][0] and
                        1654 == edgelist[sixteenfiftyfiveedge][1]) or
                        (1654 == edgelist[sixteenfiftyfiveedge][0] and
                         1655 == edgelist[sixteenfiftyfiveedge][1]))

        self.assertEqual('form complex',
                         edgelist[sixteenfiftyfiveedge][2]['interaction'])
        self.assertEqual('true', edgelist[sixteenfiftyfiveedge][2]['directed'])
        self.assertEqual('"pubmed:15284024"',
                         edgelist[sixteenfiftyfiveedge][2]['citation'])

        # check coordinates
        self.assertTrue((g.pos[1655][0] + 90.96) < 1.0)
        self.assertTrue((g.pos[1655][1] - 145.72) < 1.0)
Пример #20
0
    def from_cx(self, file, node_name="name"):
        """Load CX file as network"""

        del self.__dict__

        network = ndex2.create_nice_cx_from_file(file).to_networkx()

        self.__init__(network=network, node_name=node_name)

        return self
Пример #21
0
    def test_set_network_properties(self):
        client = self.get_ndex2_client()
        # create network and add it
        net = ndex2.create_nice_cx_from_file(
            TestClientIntegration.GLYPICAN2_FILE)
        netname = 'ndex2-client integration test network2' + str(
            datetime.now())
        net.set_name(netname)
        res = client.save_new_network(net.to_cx(), visibility='PUBLIC')
        try:
            self.assertTrue('http' in res)
            netid = re.sub('^.*/', '', res)

            # verify network was uploaded
            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, str(netsum))

            # get all network attributes
            res = client.get_network_aspect_as_cx_stream(
                netid, 'networkAttributes')
            net_attrs = res.json()

            # Versions of NDEx server pre 2.5.1 will add a new description attribute
            # but it is a duplicate
            netprops = [{
                'subNetworkId': '',
                'predicateString': 'fookey',
                'dataType': 'string',
                'value': 'foo'
            }]
            res = client.set_network_properties(netid,
                                                network_properties=netprops)
            self.assertTrue(str(res) == '' or str(res) == '1')

            updated_net_attrs = client.get_network_aspect_as_cx_stream(
                netid, 'networkAttributes')
            res_attrs = updated_net_attrs.json()
            found_fookey = False
            found_name = False
            for attr in res_attrs:
                if attr['n'] == 'fookey':
                    self.assertEqual('foo', attr['v'])
                    found_fookey = True
                if attr['n'] == 'name':
                    self.assertEqual(netname, attr['v'])
                    found_name = True
            self.assertTrue(found_fookey)
            self.assertTrue(found_name)
        finally:
            # delete network
            client.delete_network(netid)
 def test_extract_diffused_subnetwork_by_rank(self):
     net_cx = ndex2.create_nice_cx_from_file(TestHeatDiffusion.TEST_NETWORK)
     diffuser = HeatDiffusion()
     diffuser.add_seed_nodes_by_node_name(net_cx, seed_nodes=['E', 'M'])
     res_cx = diffuser.run_diffusion(net_cx)
     self.assertEqual(359, len(res_cx.get_nodes()))
     self.assertEqual(481, len(res_cx.get_edges()))
     filtered_cx = diffuser.extract_diffused_subnetwork_by_rank(
         res_cx, max_rank=5, min_heat=0.0)
     self.assertEqual(6, len(filtered_cx.get_nodes()))
     self.assertEqual(4, len(filtered_cx.get_edges()))
Пример #23
0
    def test_get_network_ids_for_user(self):
        client = self.get_ndex2_client()
        creds = self.get_ndex_credentials_as_tuple()
        netid_one = None
        netid_two = None
        try:
            # query for networks for user in creds
            network_ids = client.get_network_ids_for_user(creds['user'])
            num_network_ids = len(network_ids)
            self.assertTrue(num_network_ids >= 0)

            # set offset to number of network ids and limit 1
            network_ids = client.get_network_ids_for_user(
                creds['user'], offset=num_network_ids, limit=1)
            self.assertTrue(len(network_ids) == 0)

            # add two networks just in case there are none and verify
            # they are returned
            net = ndex2.create_nice_cx_from_file(
                TestClientIntegration.GLYPICAN2_FILE)
            netname = 'ndex2-client integration test network1' + str(
                datetime.now())
            net.set_name(netname)
            res = client.save_new_network(net.to_cx(), visibility='PUBLIC')
            self.assertTrue('http' in res)
            netid_one = re.sub('^.*/', '', res)
            # verify network was uploaded
            netsum = self.wait_for_network_to_be_ready(client, netid_one)
            self.assertEqual(netid_one, netsum['externalId'])

            net.set_name('ndex2-client integration test network2' +
                         str(datetime.now()))
            res = client.save_new_network(net.to_cx(), visibility='PUBLIC')
            self.assertTrue('http' in res)
            netid_two = re.sub('^.*/', '', res)
            # verify network was uploaded
            netsum = self.wait_for_network_to_be_ready(client, netid_two)
            self.assertEqual(netid_two, netsum['externalId'])

            network_ids = client.get_network_ids_for_user(
                creds['user'], limit=num_network_ids + 5)

            self.assertTrue(netid_one in network_ids)
            self.assertTrue(netid_two in network_ids)
            self.assertEqual(num_network_ids + 2, len(network_ids))

            network_ids = client.get_network_ids_for_user(
                creds['user'], offset=num_network_ids + 1, limit=5)
            self.assertTrue(len(network_ids) == 1)
        finally:
            if netid_one is not None:
                client.delete_network(netid_one)
            if netid_two is not None:
                client.delete_network(netid_two)
Пример #24
0
    def _run_step_three(self, network_summaries):
        """

        :return:
        """
        # Step 3 - merge PTM network with PTI network on protein/genes:
        # in essence, we add edges from PTM network to PTI based on node names
        pti_cx_network = ndex2.create_nice_cx_from_file(self._cx_pti)
        ptm_cx_network = ndex2.create_nice_cx_from_file(self._cx_ptm)

        # in this dictionary for pti network, key is protein node name, value is to node id:
        #   pti_node_name_dict: { 'CHD1': 0, 'CKA1': 1, 'CKA2': 2, ...}
        pti_node_name_dict = self._build_pti_node_name_to_node_id_dictionary(
            pti_cx_network)

        # in this dictionary for ptm network, key is protein node name, value is to node id:
        #   pti_node_name_dict: { 'ADK1': 0, 'ADR1': 2, 'AKL1': 40, ...}
        ptm_node_name_dict = self._build_ptm_node_name_to_node_id_dictionary(
            ptm_cx_network)

        # in this dictionary for ptm network, key is protein node id, value is list of ptm ids:
        #   pti_node_name_dict: {0: [1, 3108, 3521, 3522, 3523], 2: [3, 4, 5, 6, 7, 8, 9], 40: [41, 42, 43, 44, 45], ...}
        protein_id_to_ptm_ids_dict = self._build_protein_id_to_ptm_ids_dict(
            ptm_node_name_dict, ptm_cx_network)

        # in this dictionary for ptm network, key is a tuple (source Id, target Id), and
        # value is edge id
        src_target_edge_ptm_ids_dict = self._build_src_target_edge_ptm_ids_dict(
            ptm_cx_network)

        merged_ptm_pti_network = self._merge_ptm_onto_pti(
            pti_node_name_dict, ptm_node_name_dict, pti_cx_network,
            ptm_cx_network, protein_id_to_ptm_ids_dict,
            src_target_edge_ptm_ids_dict)

        self._init_network_attributes(merged_ptm_pti_network, 'merged')
        self._write_nice_cx_to_file(merged_ptm_pti_network, self._cx_merged)
        network_UUID = self._network_exists_on_server(merged_ptm_pti_network,
                                                      network_summaries)
        self._upload_CX(self._cx_merged, network_UUID)
    def test_darktheme_network(self):
        net = ndex2.create_nice_cx_from_file(
            TestLegacyNetworkXVersionTwoPlusFactory.DARKTHEME_FILE)
        fac = LegacyNetworkXVersionTwoPlusFactory()
        g = fac.get_graph(net)
        self.assertEqual('Dark theme final version', g.graph['name'])
        self.assertTrue('Perfetto L.,' in g.graph['reference'])
        self.assertEqual('Theodora Pavlidou', g.graph['author'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertEqual('18-Jan-2019', g.graph['version'])
        self.assertEqual('Human, 9606, H**o sapiens', g.graph['organism'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertTrue('epidermal growth factor' in g.graph['description'])

        self.assertEqual(34, len(g))
        self.assertEqual(50, g.number_of_edges())
        self.assertTrue('STAT3' in g)
        self.assertTrue('MAX' in g)

        nodelist = list(g.nodes(data=True))
        edgelist = list(g.edges(data=True))

        statthree = -1
        for i in range(0, len(nodelist)):
            if nodelist[i][0] == 'STAT3':
                statthree = i
                break

        self.assertEqual('STAT3', nodelist[statthree][0])
        self.assertEqual('protein', nodelist[statthree][1]['type'])
        self.assertEqual('uniprot:P40763',
                         nodelist[statthree][1]['represents'])

        stat_edge = -2
        for i in range(0, len(edgelist)):
            if edgelist[i][0] == 'STAT3' and edgelist[i][
                    1] == 'JAK1/STAT1/STAT3':
                stat_edge = i
                break
            if edgelist[i][0] == 'JAK1/STAT1/STAT3' and edgelist[i][
                    1] == 'STAT3':
                stat_edge = i
                break
        self.assertTrue(stat_edge >= 0)
        self.assertEqual('form complex', edgelist[stat_edge][2]['interaction'])
        self.assertEqual('true', edgelist[stat_edge][2]['directed'])
        self.assertEqual('"pubmed:15284024"',
                         edgelist[stat_edge][2]['citation'])

        # check coordinates
        self.assertTrue((g.pos[1655][0] + 90.96) < 1.0)
        self.assertTrue((g.pos[1655][1] - 145.72) < 1.0)
    def test_darktheme_network_legacytrue(self):
        net = ndex2.create_nice_cx_from_file(TestDefaultNetworkXFactory.DARKTHEME_FILE)
        fac = DefaultNetworkXFactory(legacymode=True)
        g = fac.get_graph(net)
        self.assertEqual('Dark theme final version', g.graph['name'])
        self.assertTrue('Perfetto L.,' in g.graph['reference'])
        self.assertEqual('Theodora Pavlidou', g.graph['author'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertEqual('18-Jan-2019', g.graph['version'])
        self.assertEqual('Human, 9606, H**o sapiens', g.graph['organism'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertTrue('epidermal growth factor' in g.graph['description'])

        self.assertEqual(34, len(g))
        self.assertEqual(50, g.number_of_edges())
        self.assertTrue(1655 in g)
        self.assertTrue(1622 in g)

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

        stat_three_index = -1
        for i in range(0, len(nodelist)):
            if nodelist[i][1]['name'] == 'STAT3':
                stat_three_index = i
                break
        self.assertEqual('STAT3', nodelist[stat_three_index][1]['name'])
        self.assertEqual('protein', nodelist[stat_three_index][1]['type'])
        self.assertTrue('represents' not in nodelist[stat_three_index][1])

        sixteenfiftyfiveedge = -1
        for i in range(len(edgelist)):
            if edgelist[i][0] == 1655 and edgelist[i][1] == 1654:
                sixteenfiftyfiveedge = i
                break

        self.assertTrue((1655 == edgelist[sixteenfiftyfiveedge][0] and
                         1654 == edgelist[sixteenfiftyfiveedge][1]) or
                        (1654 == edgelist[sixteenfiftyfiveedge][0] and
                         1655 == edgelist[sixteenfiftyfiveedge][1]))

        self.assertEqual('form complex', edgelist[sixteenfiftyfiveedge][2]['interaction'])
        self.assertEqual('true', edgelist[sixteenfiftyfiveedge][2]['directed'])
        self.assertEqual('"pubmed:15284024"', edgelist[sixteenfiftyfiveedge][2]['citation'])

        # check coordinates
        self.assertTrue((g.pos[1655][0] + 90.96) < 1.0)
        self.assertTrue((g.pos[1655][1] - 145.72) < 1.0)
 def test_diffusion(self):
     net_cx = ndex2.create_nice_cx_from_file(TestHeatDiffusion.TEST_NETWORK)
     diffuser = HeatDiffusion()
     diffuser.add_seed_nodes_by_node_name(net_cx, seed_nodes=['E', 'M'])
     res_cx = diffuser.run_diffusion(net_cx)
     for node_id, node_obj in res_cx.get_nodes():
         n_attr = res_cx.get_node_attribute(node_id,
                                            'diffusion_output_rank')
         self.assertIsNotNone(n_attr)
         if n_attr['v'] == 0:
             self.assertEqual('E', node_obj['n'])
         n_attr = res_cx.get_node_attribute(node_id,
                                            'diffusion_output_heat')
         self.assertIsNotNone(n_attr)
Пример #28
0
    def test_getCommunityEdge(self):
        filelist = glob.glob(os.path.dirname(__file__) + '/test_cx/*')
        for a in filelist:
            testcx = ndex2.create_nice_cx_from_file(a)
            # convert nice_cx -> pandas
            nice_cx_df = testcx.to_pandas_dataframe()

            # convert pandas -> igraph
            edgelist = nice_cx_df.iloc[:, [0, 2]]
            tuples = [tuple(x) for x in edgelist.values]
            g = igraph.Graph.TupleList(tuples, directed=False)

            # Pick largest subgraph
            subgraphs = g.decompose()
            tmp = [i.vcount() for i in subgraphs]
            largeset_subgraph = subgraphs[tmp.index(max(tmp))]
            g = largeset_subgraph.simplify(multiple=True, loops=True)

            # Match two different labels

            source_node_name_list = [
                value['n'] for key, value in list(testcx.get_nodes())
            ]
            node_id_list = []
            for a in g.vs['name']:
                for i, b in enumerate(source_node_name_list):
                    if a == b:
                        node_id_list.append(i)

            v_community = [
                int(testcx.get_node_attribute_value(a, 'community'))
                for a in node_id_list
            ]

            source_edge_list = [(node_id_list.index(value['s']),
                                 node_id_list.index(value['t']))
                                for key, value in list(testcx.get_edges())]

            edge_id_list = []
            for a in g.get_edgelist():
                for i, b in enumerate(source_edge_list):
                    if sorted(a) == sorted(b):
                        edge_id_list.append(i)

            e_community = [
                int(testcx.get_edge_attribute_value(a, 'community'))
                for a in edge_id_list
            ]

            self.assertEqual(e_community, getCommunityEdge(g, v_community))
Пример #29
0
def run_diffusion(theargs, out_stream=sys.stdout, err_stream=sys.stderr):
    """
    Runs diffusion and converts
    output to CDAPS compatible format that is output to
    standard out.

    :param theargs: Holds attributes from argparse
    :type theargs: `:py:class:`argparse.Namespace`
    :param out_stream: stream for standard output
    :type out_stream: file like object
    :param err_stream: stream for standard error output
    :type err_stream: file like object
    :return: 0 upon success otherwise error
    :rtype: int
    """

    if theargs.input is None or not os.path.isfile(theargs.input):
        err_stream.write(str(theargs.input) + ' is not a file')
        return 3

    if os.path.getsize(theargs.input) == 0:
        err_stream.write(str(theargs.input) + ' is an empty file')
        return 4

    try:
        with redirect_stdout(sys.stderr):
            net = ndex2.create_nice_cx_from_file(theargs.input)

            diffuser = networkheatdiffusion.HeatDiffusion()
            resnet = diffuser.run_diffusion(
                net,
                time_param=theargs.time,
                normalize_laplacian=theargs.normalize_laplacian,
                input_col_name=theargs.input_attribute_name,
                output_prefix=theargs.output_attribute_name,
                correct_rank=theargs.correct_rank,
                via_service=False)
            # write network as CX to output stream
            finalres = {}
            finalres['data'] = resnet.to_cx()
            finalres['errors'] = []
            json.dump(finalres, out_stream)
        return 0
    except networkheatdiffusion.HeatDiffusionError as he:
        err_stream.write(str(he))
        return 5
    finally:
        err_stream.flush()
        out_stream.flush()
Пример #30
0
    def test_creating_network_context_passed_in_net_attribs(self):
        temp_dir = tempfile.mkdtemp()
        try:
            here = os.path.dirname(__file__)
            tsvfile = os.path.join(here, 'ctd_test.tsv')
            with open(tsvfile, 'r') as tsvfile:
                tmpcx = 'out.cx'
                with open(tmpcx, "w") as out:
                    nicecx = ndex2.create_nice_cx_from_file(
                        os.path.join(here, 'gene-disease-style.cx'))
                    loader = StreamTSVLoader(
                        os.path.join(
                            here,
                            'ctd-gene-disease-2019-norm-plan-collapsed.json'),
                        nicecx)
                    loader.write_cx_network(
                        tsvfile,
                        out, [{
                            'n': 'name',
                            'v': "CTD: gene-disease association (Human)"
                        }, {
                            'n': 'version',
                            'v': "0.0.1"
                        }, {
                            'n': '@context',
                            'v': '{"hi": "there"}'
                        }],
                        batchsize=4)

                nicecx = ndex2.create_nice_cx_from_file(tmpcx)
                cdata = json.loads(
                    nicecx.get_network_attribute('@context')['v'])
                self.assertEqual({'hi': 'there'}, cdata)

        finally:
            shutil.rmtree(temp_dir)
Пример #31
0
    def _apply_style(net_cx, style='default_style.cx'):
        """
        Applies default hierarchy style to network

        :param net_cx: Network to update style on
        :type net_cx: :py:class:`ndex2.nice_cx_network.NiceCXNetwork`
        :param style: Path to CX file with style to use
        :type style: str
        :return: None
        """
        if os.path.isfile(style):
            style_file = style
        else:
            style_file = os.path.join(os.path.dirname(__file__), style)
        style_cx = ndex2.create_nice_cx_from_file(style_file)
        net_cx.apply_style_from_network(style_cx)
    def test_darktheme_network(self):
        net = ndex2.create_nice_cx_from_file(TestLegacyNetworkXVersionTwoPlusFactory.DARKTHEME_FILE)
        fac = LegacyNetworkXVersionTwoPlusFactory()
        g = fac.get_graph(net)
        self.assertEqual('Dark theme final version', g.graph['name'])
        self.assertTrue('Perfetto L.,' in g.graph['reference'])
        self.assertEqual('Theodora Pavlidou', g.graph['author'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertEqual('18-Jan-2019', g.graph['version'])
        self.assertEqual('Human, 9606, H**o sapiens', g.graph['organism'])
        self.assertEqual('SIGNOR-EGF', g.graph['labels'])
        self.assertTrue('epidermal growth factor' in g.graph['description'])

        self.assertEqual(34, len(g))
        self.assertEqual(50, g.number_of_edges())
        self.assertTrue('STAT3' in g)
        self.assertTrue('MAX' in g)

        nodelist = list(g.nodes(data=True))
        edgelist = list(g.edges(data=True))

        statthree = -1
        for i in range(0, len(nodelist)):
            if nodelist[i][0] == 'STAT3':
                statthree = i
                break

        self.assertEqual('STAT3', nodelist[statthree][0])
        self.assertEqual('protein', nodelist[statthree][1]['type'])
        self.assertEqual('uniprot:P40763', nodelist[statthree][1]['represents'])

        stat_edge = -2
        for i in range(0, len(edgelist)):
            if edgelist[i][0] == 'STAT3' and edgelist[i][1] == 'JAK1/STAT1/STAT3':
                stat_edge = i
                break
            if edgelist[i][0] == 'JAK1/STAT1/STAT3' and edgelist[i][1] == 'STAT3':
                stat_edge = i
                break
        self.assertTrue(stat_edge >= 0)
        self.assertEqual('form complex', edgelist[stat_edge][2]['interaction'])
        self.assertEqual('true', edgelist[stat_edge][2]['directed'])
        self.assertEqual('"pubmed:15284024"', edgelist[stat_edge][2]['citation'])

        # check coordinates
        self.assertTrue((g.pos[1655][0] + 90.96) < 1.0)
        self.assertTrue((g.pos[1655][1] - 145.72) < 1.0)
Пример #33
0
def load_cx_network(filename,
                    mode='default',
                    network_mode=None,
                    dict_key='name',
                    mapper=None,
                    print_stats=False):
    net = ndex2.create_nice_cx_from_file(filename)
    G = net.to_networkx(mode=mode)
    if network_mode is not None:
        G = change_node_names(G,
                              network_mode=network_mode,
                              dict_key=dict_key,
                              mapper=mapper)

    if print_stats:
        print_network_info(G)
    return G
Пример #34
0
    def test_creating_from_file(self):
        niceCx_from_cx_file = ndex2.create_nice_cx_from_file('WNT.cx')

        upload_message = niceCx_from_cx_file.upload_to(upload_server, upload_username, upload_password)
        self.assertTrue(upload_message)