def gsea_compute(data, gmt, n, weighted_score_type, permutation_type,
                 method, pheno_pos, pheno_neg, classes, ascending,
                 processes=1, seed=None, single=False, scale=False):
    """compute enrichment scores and enrichment nulls.

        :param data: preprocessed expression dataframe or a pre-ranked file if prerank=True.
        :param dict gmt: all gene sets in .gmt file. need to call load_gmt() to get results.
        :param int n: permutation number. default: 1000.
        :param str method: ranking_metric method. see above.
        :param str pheno_pos: one of labels of phenotype's names.
        :param str pheno_neg: one of labels of phenotype's names.
        :param list classes: a list of phenotype labels, to specify which column of dataframe belongs to what category of phenotype.
        :param float weighted_score_type: default:1
        :param bool ascending: sorting order of rankings. Default: False.
        :param seed: random seed. Default: np.random.RandomState()
        :param bool scale: if true, scale es by gene number.

        :return: a tuple contains::

                | zipped results of es, nes, pval, fdr.
                | nested list of hit indices of input gene_list.
                | nested list of ranked enrichment score of each input gene_sets.
                | list of enriched terms

    """
    
    w = weighted_score_type
    subsets = sorted(gmt.keys())
    es = []
    RES=[]
    hit_ind=[]
    esnull = [ [] for a in range(len(subsets)) ]

    logging.debug("Start to compute enrichment scores......................")

    if permutation_type == "phenotype":
        logging.debug("Start to permutate classes..............................")
        # shuffling classes and generate random correlation rankings
        rs = np.random.RandomState(seed)
        genes_mat, cor_mat = ranking_metric_tensor(exprs=data, method=method,
                                                   permutation_num=n,
                                                   pos=pheno_pos, neg=pheno_neg,
                                                   classes=classes,
                                                   ascending=ascending, rs=rs)

        # compute es, esnulls. hits, RES
        logging.debug("Start to compute enrichment nulls.......................")
        es, esnull, hit_ind, RES = enrichment_score_tensor(gene_mat=genes_mat,
                                                           cor_mat=cor_mat,
                                                           gene_sets=gmt,
                                                           weighted_score_type=w,
                                                           nperm=n, rs=rs,
                                                           single=False, scale=False,)

    else:
        # Prerank, ssGSEA, GSEA with gene_set permutation
        gl, cor_vec = data.index.values, data.values
        logging.debug("Start to compute es and esnulls........................")
        # es, esnull, hit_ind, RES = enrichment_score_tensor(gene_mat=gl,
        #                                                    cor_mat=cor_mat,
        #                                                    gene_sets=gmt,
        #                                                    weighted_score_type=w,
        #                                                    nperm=n, rs=rs
        #                                                    single=single, scale=scale)

        # split large array into smaller blocks to avoid memory overflow
        temp_esnu=[]
        pool_esnu = Pool(processes=processes)
        for subset in subsets:
            # you have to reseed, or all your processes are sharing the same seed value
            rs = np.random.RandomState(seed)
            temp_esnu.append(pool_esnu.apply_async(enrichment_score,
                                                   args=(gl, cor_vec, gmt.get(subset), w,
                                                         n, rs, single, scale)))

        pool_esnu.close()
        pool_esnu.join()
        # esn is a list, don't need to use append method.
        for si, temp in enumerate(temp_esnu):
            e, enu, hit, rune = temp.get()
            esnull[si] = enu
            es.append(e)
            RES.append(rune)
            hit_ind.append(hit)

    return gsea_significance(es, esnull), hit_ind, RES, subsets
def gsea_compute(data, gmt, n, weighted_score_type, permutation_type,
                 method, pheno_pos, pheno_neg, classes, ascending,
                 processes=1, seed=None, single=False, scale=False):
    """compute enrichment scores and enrichment nulls.

        :param data: preprocessed expression dataframe or a pre-ranked file if prerank=True.
        :param dict gmt: all gene sets in .gmt file. need to call load_gmt() to get results.
        :param int n: permutation number. default: 1000.
        :param str method: ranking_metric method. see above.
        :param str pheno_pos: one of labels of phenotype's names.
        :param str pheno_neg: one of labels of phenotype's names.
        :param list classes: a list of phenotype labels, to specify which column of dataframe belongs to what category of phenotype.
        :param float weighted_score_type: default:1
        :param bool ascending: sorting order of rankings. Default: False.
        :param seed: random seed. Default: np.random.RandomState()
        :param bool scale: if true, scale es by gene number.

        :return: a tuple contains::

                | zipped results of es, nes, pval, fdr.
                | nested list of hit indices of input gene_list.
                | nested list of ranked enrichment score of each input gene_sets.
                | list of enriched terms

    """
    
    w = weighted_score_type
    subsets = sorted(gmt.keys())
    es = []
    RES=[]
    hit_ind=[]
    esnull = [ [] for a in range(len(subsets)) ]

    logging.debug("Start to compute enrichment scores......................")

    if permutation_type == "phenotype":
        logging.debug("Start to permutate classes..............................")
        # shuffling classes and generate random correlation rankings
        rs = np.random.RandomState(seed)
        genes_mat, cor_mat = ranking_metric_tensor(exprs=data, method=method,
                                                   permutation_num=n,
                                                   pos=pheno_pos, neg=pheno_neg,
                                                   classes=classes,
                                                   ascending=ascending, rs=rs)

        # compute es, esnulls. hits, RES
        logging.debug("Start to compute enrichment nulls.......................")
        es, esnull, hit_ind, RES = enrichment_score_tensor(gene_mat=genes_mat,
                                                           cor_mat=cor_mat,
                                                           gene_sets=gmt,
                                                           weighted_score_type=w,
                                                           nperm=n, rs=rs,
                                                           single=False, scale=False,)

    else:
        # Prerank, ssGSEA, GSEA with gene_set permutation
        gl, cor_vec = data.index.values, data.values
        logging.debug("Start to compute es and esnulls........................")
        # es, esnull, hit_ind, RES = enrichment_score_tensor(gene_mat=gl,
        #                                                    cor_mat=cor_mat,
        #                                                    gene_sets=gmt,
        #                                                    weighted_score_type=w,
        #                                                    nperm=n, rs=rs
        #                                                    single=single, scale=scale)

        # split large array into smaller blocks to avoid memory overflow
        temp_esnu=[]
        pool_esnu = Pool(processes=processes)
        for subset in subsets:
            # you have to reseed, or all your processes are sharing the same seed value
            rs = np.random.RandomState(seed)
            temp_esnu.append(pool_esnu.apply_async(enrichment_score,
                                                   args=(gl, cor_vec, gmt.get(subset), w,
                                                         n, rs, single, scale)))

        pool_esnu.close()
        pool_esnu.join()
        # esn is a list, don't need to use append method.
        for si, temp in enumerate(temp_esnu):
            e, enu, hit, rune = temp.get()
            esnull[si] = enu
            es.append(e)
            RES.append(rune)
            hit_ind.append(hit)

    return gsea_significance(es, esnull), hit_ind, RES, subsets
def gsea_compute ( data , gmt , n , weighted_score_type , permutation_type , method , pheno_pos , pheno_neg , classes , ascending , processes = 1 , seed = None , single = False , scale = False ) : w = weighted_score_type subsets = sorted ( gmt . keys ( ) ) es = [ ] RES = [ ] hit_ind = [ ] esnull = [ [ ] for a in range ( len ( subsets ) ) ] logging . debug ( "Start to compute enrichment scores......................" ) if permutation_type == "phenotype" : logging . debug ( "Start to permutate classes.............................." ) # shuffling classes and generate random correlation rankings rs = np . random . RandomState ( seed ) genes_mat , cor_mat = ranking_metric_tensor ( exprs = data , method = method , permutation_num = n , pos = pheno_pos , neg = pheno_neg , classes = classes , ascending = ascending , rs = rs ) # compute es, esnulls. hits, RES logging . debug ( "Start to compute enrichment nulls......................." ) es , esnull , hit_ind , RES = enrichment_score_tensor ( gene_mat = genes_mat , cor_mat = cor_mat , gene_sets = gmt , weighted_score_type = w , nperm = n , rs = rs , single = False , scale = False , ) else : # Prerank, ssGSEA, GSEA with gene_set permutation gl , cor_vec = data . index . values , data . values logging . debug ( "Start to compute es and esnulls........................" ) # es, esnull, hit_ind, RES = enrichment_score_tensor(gene_mat=gl, #                                                    cor_mat=cor_mat, #                                                    gene_sets=gmt, #                                                    weighted_score_type=w, #                                                    nperm=n, rs=rs #                                                    single=single, scale=scale) # split large array into smaller blocks to avoid memory overflow temp_esnu = [ ] pool_esnu = Pool ( processes = processes ) for subset in subsets : # you have to reseed, or all your processes are sharing the same seed value rs = np . random . RandomState ( seed ) temp_esnu . append ( pool_esnu . apply_async ( enrichment_score , args = ( gl , cor_vec , gmt . get ( subset ) , w , n , rs , single , scale ) ) ) pool_esnu . close ( ) pool_esnu . join ( ) # esn is a list, don't need to use append method. for si , temp in enumerate ( temp_esnu ) : e , enu , hit , rune = temp . get ( ) esnull [ si ] = enu es . append ( e ) RES . append ( rune ) hit_ind . append ( hit ) return gsea_significance ( es , esnull ) , hit_ind , RES , subsets
compute enrichment scores and enrichment nulls.

        :param data: preprocessed expression dataframe or a pre-ranked file if prerank=True.
        :param dict gmt: all gene sets in .gmt file. need to call load_gmt() to get results.
        :param int n: permutation number. default: 1000.
        :param str method: ranking_metric method. see above.
        :param str pheno_pos: one of labels of phenotype's names.
        :param str pheno_neg: one of labels of phenotype's names.
        :param list classes: a list of phenotype labels, to specify which column of dataframe belongs to what category of phenotype.
        :param float weighted_score_type: default:1
        :param bool ascending: sorting order of rankings. Default: False.
        :param seed: random seed. Default: np.random.RandomState()
        :param bool scale: if true, scale es by gene number.

        :return: a tuple contains::

                | zipped results of es, nes, pval, fdr.
                | nested list of hit indices of input gene_list.
                | nested list of ranked enrichment score of each input gene_sets.
                | list of enriched terms
compute enrichment scores and enrichment nulls .