How to use the gseapy.algorithm.ranking_metric function in gseapy

To help you get started, we’ve selected a few gseapy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github iseekwonderful / PyPathway / pypathway / analysis / gsea / __init__.py View on Github external
def run(data, gmt, cls, permutation_type='phenotype', method='signal_to_noise', permution_num=1000):
        prefix = gp.__name__ + "."
        for importer, modname, ispkg in pkgutil.iter_modules(gp.__path__, prefix):
            if modname == "gseapy.gsea":
                module = __import__(modname, fromlist="dummy")
        vs = gp.__version__.split(".")
        if int(vs[0]) == 0 and int(vs[1]) < 9:
            module.ranking_metric = GSEA._ranking_metric
        else:
            module.ranking_metric = GSEA._ranking_metric2
        gp.algorithm.ranking_metric = GSEA._ranking_metric
        res = gp.gsea(data, gmt, cls, permutation_type=permutation_type, permutation_num=permution_num,
                      outdir=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'images'), method=method)
        return GSEA(res.res2d, data, gmt, cls)
github zqfang / GSEApy / gseapy / gsea.py View on Github external
def run(self):
        """GSEA main procedure"""

        assert self.permutation_type in ["phenotype", "gene_set"]
        assert self.min_size <= self.max_size

        # Start Analysis
        self._logger.info("Parsing data files for GSEA.............................")
        # phenotype labels parsing
        phenoPos, phenoNeg, cls_vector = gsea_cls_parser(self.classes)
        # select correct expression genes and values.
        dat = self.load_data(cls_vector)
        # data frame must have length > 1
        assert len(dat) > 1
        # ranking metrics calculation.
        dat2 = ranking_metric(df=dat, method=self.method, pos=phenoPos, neg=phenoNeg,
                              classes=cls_vector, ascending=self.ascending)
        self.ranking = dat2
        # filtering out gene sets and build gene sets dictionary
        gmt = self.load_gmt(gene_list=dat2.index.values, gmt=self.gene_sets)

        self._logger.info("%04d gene_sets used for further statistical testing....."% len(gmt))
        self._logger.info("Start to run GSEA...Might take a while..................")
        # cpu numbers
        self._set_cores()
        # compute ES, NES, pval, FDR, RES
        dataset = dat if self.permutation_type =='phenotype' else dat2
        gsea_results,hit_ind,rank_ES, subsets = gsea_compute_tensor(data=dataset, gmt=gmt, n=self.permutation_num,
                                                             weighted_score_type=self.weighted_score_type,
                                                             permutation_type=self.permutation_type,
                                                             method=self.method,
                                                             pheno_pos=phenoPos, pheno_neg=phenoNeg,
github zqfang / GSEApy / gseapy / run.py View on Github external
| fdr: FDR, 
        | size: gene set size,
        | matched_size: genes matched to the data, 
        | genes: gene names from the data set }

    """
    assert len(data) > 1
    assert permutation_type in ["phenotype", "gene_set"]
    
    data = pd.read_table(data)
    classes = gsea_cls_parser(cls)[2]
    gmt = gsea_gmt_parser(gene_sets)
    gmt.sort()
    #Ecompute ES, NES, pval, FDR, RES
    if rank_metric is None:
        dat = ranking_metric(data,method= method,classes = classes ,ascending=ascending)
        results,hit_ind,RES = gsea_compute(data = dat, gene_list = None,rankings = None,
                    n=permutation_n,gmt = gmt, weighted_score_type=weighted_score_type,
                    permutation_type=permutation_type)
    else:
        dat = pd.read_table(rank_metric)
        results,hit_ind,RES = gsea_compute(data = None, gene_list = rank_metric['gene_name'],rankings = rank_metric['rank'].values,
                                           n=permutation_n,gmt = gmt, weighted_score_type=weighted_score_type,
                                           permutation_type=permutation_type)
    
    res = {}

    for gs, gseale in zip(gmt.keys(), list(results)):
        rdict = {}
        rdict['es'] = gseale[0]
        rdict['nes'] = gseale[1]
        rdict['pval'] = gseale[2]