def gsea_compute_tensor(data, gmt, n, weighted_score_type, permutation_type,<EOL>method, pheno_pos, pheno_neg, classes, ascending,<EOL>processes=<NUM_LIT:1>, seed=None, single=False, scale=False):

w = weighted_score_type<EOL>subsets = sorted(gmt.keys())<EOL>rs = np.random.RandomState(seed)<EOL>genes_mat, cor_mat = data.index.values, data.values<EOL>base = <NUM_LIT:5> if data.shape[<NUM_LIT:0>] >= <NUM_LIT> else <NUM_LIT:10><EOL>block = ceil(len(subsets) / base)<EOL>if permutation_type == "<STR_LIT>":<EOL><INDENT>logging.debug("<STR_LIT>")<EOL>genes_ind = []<EOL>cor_mat = []<EOL>temp_rnk = []<EOL>pool_rnk = Pool(processes=processes)<EOL>i=<NUM_LIT:1><EOL>while i <= block:<EOL><INDENT>rs = np.random.RandomState(seed)<EOL>temp_rnk.append(pool_rnk.apply_async(ranking_metric_tensor,<EOL>args=(data, method, base, pheno_pos, pheno_neg, classes,<EOL>ascending, rs)))<EOL>i +=<NUM_LIT:1><EOL><DEDENT>pool_rnk.close()<EOL>pool_rnk.join()<EOL>for k, temp in enumerate(temp_rnk):<EOL><INDENT>gi, cor = temp.get()<EOL>if k+<NUM_LIT:1> == block:<EOL><INDENT>genes_ind.append(gi)<EOL>cor_mat.append(cor)<EOL><DEDENT>else:<EOL><INDENT>genes_ind.append(gi[:-<NUM_LIT:1>])<EOL>cor_mat.append(cor[:-<NUM_LIT:1>])<EOL><DEDENT><DEDENT>genes_ind, cor_mat = np.vstack(genes_ind), np.vstack(cor_mat)<EOL>genes_mat = (data.index.values, genes_ind)<EOL><DEDENT>logging.debug("<STR_LIT>")<EOL>es = []<EOL>RES = []<EOL>hit_ind = []<EOL>esnull = []<EOL>temp_esnu = []<EOL>pool_esnu = Pool(processes=processes)<EOL>i, m = <NUM_LIT:1>, <NUM_LIT:0><EOL>while i <= block:<EOL><INDENT>rs = np.random.RandomState(seed)<EOL>gmtrim = {k: gmt.get(k) for k in subsets[m:base * i]}<EOL>temp_esnu.append(pool_esnu.apply_async(enrichment_score_tensor,<EOL>args=(genes_mat, cor_mat,<EOL>gmtrim, w, n, rs,<EOL>single, scale)))<EOL>m = base * i<EOL>i += <NUM_LIT:1><EOL><DEDENT>pool_esnu.close()<EOL>pool_esnu.join()<EOL>for si, temp in enumerate(temp_esnu):<EOL><INDENT>e, enu, hit, rune = temp.get()<EOL>esnull.append(enu)<EOL>es.append(e)<EOL>RES.append(rune)<EOL>hit_ind += hit<EOL><DEDENT>es, esnull, RES = np.hstack(es), np.vstack(esnull), np.vstack(RES)<EOL>return gsea_significance(es, esnull), hit_ind, RES, subsets<EOL>

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


def gsea_compute(data, gmt, n, weighted_score_type, permutation_type,<EOL>method, pheno_pos, pheno_neg, classes, ascending,<EOL>processes=<NUM_LIT:1>, seed=None, single=False, scale=False):

w = weighted_score_type<EOL>subsets = sorted(gmt.keys())<EOL>es = []<EOL>RES=[]<EOL>hit_ind=[]<EOL>esnull = [ [] for a in range(len(subsets)) ]<EOL>logging.debug("<STR_LIT>")<EOL>if permutation_type == "<STR_LIT>":<EOL><INDENT>logging.debug("<STR_LIT>")<EOL>rs = np.random.RandomState(seed)<EOL>genes_mat, cor_mat = ranking_metric_tensor(exprs=data, method=method,<EOL>permutation_num=n,<EOL>pos=pheno_pos, neg=pheno_neg,<EOL>classes=classes,<EOL>ascending=ascending, rs=rs)<EOL>logging.debug("<STR_LIT>")<EOL>es, esnull, hit_ind, RES = enrichment_score_tensor(gene_mat=genes_mat,<EOL>cor_mat=cor_mat,<EOL>gene_sets=gmt,<EOL>weighted_score_type=w,<EOL>nperm=n, rs=rs,<EOL>single=False, scale=False,)<EOL><DEDENT>else:<EOL><INDENT>gl, cor_vec = data.index.values, data.values<EOL>logging.debug("<STR_LIT>")<EOL>temp_esnu=[]<EOL>pool_esnu = Pool(processes=processes)<EOL>for subset in subsets:<EOL><INDENT>rs = np.random.RandomState(seed)<EOL>temp_esnu.append(pool_esnu.apply_async(enrichment_score,<EOL>args=(gl, cor_vec, gmt.get(subset), w,<EOL>n, rs, single, scale)))<EOL><DEDENT>pool_esnu.close()<EOL>pool_esnu.join()<EOL>for si, temp in enumerate(temp_esnu):<EOL><INDENT>e, enu, hit, rune = temp.get()<EOL>esnull[si] = enu<EOL>es.append(e)<EOL>RES.append(rune)<EOL>hit_ind.append(hit)<EOL><DEDENT><DEDENT>return gsea_significance(es, esnull), hit_ind, RES, subsets<EOL>


<EOL>condlist = [ es < <NUM_LIT:0>, es >=<NUM_LIT:0>]<EOL>choicelist = [np.sum(esnull < es.reshape(len(es),<NUM_LIT:1>), axis=<NUM_LIT:1>)/ np.sum(esnull < <NUM_LIT:0>, axis=<NUM_LIT:1>),<EOL>np.sum(esnull >= es.reshape(len(es),<NUM_LIT:1>), axis=<NUM_LIT:1>)/ np.sum(esnull >= <NUM_LIT:0>, axis=<NUM_LIT:1>)]<EOL>pval = np.select(condlist, choicelist)<EOL>return pval<EOL>

Compute nominal p-value.

    From article (PNAS):
    estimate nominal p-value for S from esnull by using the positive
    or negative portion of the distribution corresponding to the sign
    of the observed ES(S).


nEnrichmentScores =np.zeros(es.shape)<EOL>nEnrichmentNulls=np.zeros(esnull.shape)<EOL>esnull_pos = (esnull * (esnull >= <NUM_LIT:0>)).mean(axis=<NUM_LIT:1>)<EOL>esnull_neg = (esnull * (esnull < <NUM_LIT:0>)).mean(axis=<NUM_LIT:1>)<EOL>for i in range(esnull.shape[<NUM_LIT:0>]):<EOL><INDENT>if es[i] >= <NUM_LIT:0>:<EOL><INDENT>nEnrichmentScores[i] = es[i] / esnull_pos[i]<EOL><DEDENT>else:<EOL><INDENT>nEnrichmentScores[i] = - es[i] / esnull_neg[i]<EOL><DEDENT>for j in range(esnull.shape[<NUM_LIT:1>]):<EOL><INDENT>if esnull[i,j] >= <NUM_LIT:0>:<EOL><INDENT>nEnrichmentNulls[i,j] = esnull[i,j] / esnull_pos[i]<EOL><DEDENT>else:<EOL><INDENT>nEnrichmentNulls[i,j] = - esnull[i,j] / esnull_neg[i]<EOL><DEDENT><DEDENT><DEDENT>return nEnrichmentScores, nEnrichmentNulls<EOL>

normalize the ES(S,pi) and the observed ES(S), separately rescaling
       the positive and negative scores by dividing the mean of the ES(S,pi).

       return: NES, NESnull


<EOL>np.seterr(divide='<STR_LIT:ignore>', invalid='<STR_LIT:ignore>')<EOL>es = np.array(enrichment_scores)<EOL>esnull = np.array(enrichment_nulls)<EOL>logging.debug("<STR_LIT>")<EOL>enrichmentPVals = gsea_pval(es, esnull).tolist()<EOL>logging.debug("<STR_LIT>")<EOL>esnull_pos = (esnull*(esnull>=<NUM_LIT:0>)).mean(axis=<NUM_LIT:1>)<EOL>esnull_neg = (esnull*(esnull<<NUM_LIT:0>)).mean(axis=<NUM_LIT:1>)<EOL>nEnrichmentScores  = np.where(es>=<NUM_LIT:0>, es/esnull_pos, -es/esnull_neg)<EOL>nEnrichmentNulls = np.where(esnull>=<NUM_LIT:0>, esnull/esnull_pos[:,np.newaxis],<EOL>-esnull/esnull_neg[:,np.newaxis])<EOL>logging.debug("<STR_LIT>")<EOL>nvals = np.sort(nEnrichmentNulls.flatten())<EOL>nnes = np.sort(nEnrichmentScores)<EOL>fdrs = []<EOL>for i in range(len(enrichment_scores)):<EOL><INDENT>nes = nEnrichmentScores[i]<EOL>if nes >= <NUM_LIT:0>:<EOL><INDENT>allPos = int(len(nvals) - np.searchsorted(nvals, <NUM_LIT:0>, side="<STR_LIT:left>"))<EOL>allHigherAndPos = int(len(nvals) - np.searchsorted(nvals, nes, side="<STR_LIT:left>"))<EOL>nesPos = len(nnes) - int(np.searchsorted(nnes, <NUM_LIT:0>, side="<STR_LIT:left>"))<EOL>nesHigherAndPos = len(nnes) - int(np.searchsorted(nnes, nes, side="<STR_LIT:left>"))<EOL><DEDENT>else:<EOL><INDENT>allPos = int(np.searchsorted(nvals, <NUM_LIT:0>, side="<STR_LIT:left>"))<EOL>allHigherAndPos = int(np.searchsorted(nvals, nes, side="<STR_LIT:right>"))<EOL>nesPos = int(np.searchsorted(nnes, <NUM_LIT:0>, side="<STR_LIT:left>"))<EOL>nesHigherAndPos = int(np.searchsorted(nnes, nes, side="<STR_LIT:right>"))<EOL><DEDENT>try:<EOL><INDENT>pi_norm = allHigherAndPos/float(allPos) <EOL>pi_obs = nesHigherAndPos/float(nesPos)<EOL>fdr =  pi_norm / pi_obs<EOL>fdrs.append(fdr if fdr < <NUM_LIT:1> else <NUM_LIT:1.0>)<EOL><DEDENT>except:<EOL><INDENT>fdrs.append(<NUM_LIT>)<EOL><DEDENT><DEDENT>logging.debug("<STR_LIT>")<EOL>return zip(enrichment_scores, nEnrichmentScores, enrichmentPVals, fdrs)<EOL>

Compute nominal pvals, normalized ES, and FDR q value.

        For a given NES(S) = NES* >= 0. The FDR is the ratio of the percentage of all (S,pi) with
        NES(S,pi) >= 0, whose NES(S,pi) >= NES*, divided by the percentage of
        observed S wih NES(S) >= 0, whose NES(S) >= NES*, and similarly if NES(S) = NES* <= 0.


if isinstance(cls, list) :<EOL><INDENT>classes = cls<EOL>sample_name= unique(classes)<EOL><DEDENT>elif isinstance(cls, str) :<EOL><INDENT>with open(cls) as c:<EOL><INDENT>file = c.readlines()<EOL><DEDENT>classes = file[<NUM_LIT:2>].strip('<STR_LIT:\n>').split("<STR_LIT:U+0020>")<EOL>sample_name = file[<NUM_LIT:1>].lstrip("<STR_LIT>").strip('<STR_LIT:\n>').split("<STR_LIT:U+0020>")<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>return sample_name[<NUM_LIT:0>], sample_name[<NUM_LIT:1>], classes<EOL>

Extract class(phenotype) name from .cls file.

    :param cls: the a class list instance or .cls file which is identical to GSEA input .
    :return: phenotype name and a list of class vector.


from bs4 import BeautifulSoup<EOL>soup = BeautifulSoup(open(results_path), features='<STR_LIT>')<EOL>tag = soup.findAll('<STR_LIT>')<EOL>term = dict(tag[index].attrs)<EOL>enrich_term = term.get('<STR_LIT>').split("<STR_LIT:#>")[<NUM_LIT:1>]<EOL>es_profile = term.get('<STR_LIT>').split("<STR_LIT:U+0020>")<EOL>hit_ind =term.get('<STR_LIT>').split("<STR_LIT:U+0020>")<EOL>es_profile = [float(i) for i in es_profile ]<EOL>hit_ind = [float(i) for i in hit_ind ]<EOL>nes = term.get('<STR_LIT>')<EOL>pval = term.get('<STR_LIT>')<EOL>fdr =  term.get('<STR_LIT>')<EOL>logging.debug("<STR_LIT>"+ enrich_term)<EOL>return enrich_term, hit_ind, nes, pval, fdr<EOL>

Parse results.edb file stored under **edb** file folder.

    :param results_path: the .results file located inside edb folder.
    :param index: gene_set index of gmt database, used for iterating items.
    :return: enrichment_term, hit_index,nes, pval, fdr.


def gsea_gmt_parser(gmt, min_size = <NUM_LIT:3>, max_size = <NUM_LIT:1000>, gene_list=None):

if gmt.lower().endswith("<STR_LIT>"):<EOL><INDENT>logging.info("<STR_LIT>")<EOL>with open(gmt) as genesets:<EOL><INDENT>genesets_dict = { line.strip().split("<STR_LIT:\t>")[<NUM_LIT:0>]: line.strip().split("<STR_LIT:\t>")[<NUM_LIT:2>:]<EOL>for line in genesets.readlines()}<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>")<EOL>if gmt in DEFAULT_LIBRARY:<EOL><INDENT>names = DEFAULT_LIBRARY<EOL><DEDENT>else:<EOL><INDENT>names = get_library_name()<EOL><DEDENT>if gmt in names:<EOL><INDENT>"""<STR_LIT>"""<EOL>s = requests.Session()<EOL>retries = Retry(total=<NUM_LIT:5>, backoff_factor=<NUM_LIT:0.1>,<EOL>status_forcelist=[ <NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT> ])<EOL>s.mount('<STR_LIT>', HTTPAdapter(max_retries=retries))<EOL>ENRICHR_URL = '<STR_LIT>'<EOL>query_string = '<STR_LIT>'<EOL>response = s.get( ENRICHR_URL + query_string % gmt, timeout=None)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if not response.ok:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>genesets_dict = { line.strip().split("<STR_LIT:\t>")[<NUM_LIT:0>]:<EOL>list(map(lambda x: x.split("<STR_LIT:U+002C>")[<NUM_LIT:0>], line.strip().split("<STR_LIT:\t>")[<NUM_LIT:2>:]))<EOL>for line in response.iter_lines(chunk_size=<NUM_LIT>, decode_unicode='<STR_LIT:utf-8>')}<EOL><DEDENT>if sys.version_info[<NUM_LIT:0>] >= <NUM_LIT:3> :<EOL><INDENT>genesets_filter =  {k: v for k, v in genesets_dict.items() if len(v) >= min_size and len(v) <= max_size}<EOL><DEDENT>elif sys.version_info[<NUM_LIT:0>] == <NUM_LIT:2>:<EOL><INDENT>genesets_filter =  {k: v for k, v in genesets_dict.iteritems() if len(v) >= min_size and len(v) <= max_size}<EOL><DEDENT>else:<EOL><INDENT>logging.error("<STR_LIT>")<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>if gene_list is not None:<EOL><INDENT>subsets = sorted(genesets_filter.keys())<EOL>for subset in subsets:<EOL><INDENT>tag_indicator = in1d(gene_list, genesets_filter.get(subset), assume_unique=True)<EOL>tag_len = sum(tag_indicator)<EOL>if tag_len <= min_size or tag_len >= max_size:<EOL><INDENT>del genesets_filter[subset]<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT>filsets_num = len(genesets_dict) - len(genesets_filter)<EOL>logging.info("<STR_LIT>"%(filsets_num, max_size, min_size))<EOL>if filsets_num == len(genesets_dict):<EOL><INDENT>logging.error("<STR_LIT>" +"<STR_LIT>" )<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>return genesets_filter<EOL><DEDENT>

Parse gene_sets.gmt(gene set database) file or download from enrichr server.

    :param gmt: the gene_sets.gmt file of GSEA input or an enrichr library name.
                checkout full enrichr library name here: http://amp.pharm.mssm.edu/Enrichr/#stats

    :param min_size: Minimum allowed number of genes from gene set also the data set. Default: 3.
    :param max_size: Maximum allowed number of genes from gene set also the data set. Default: 5000.
    :param gene_list: Used for filtering gene set. Only used this argument for :func:`call` method.
    :return: Return a new filtered gene set database dictionary.

    **DO NOT** filter gene sets, when use :func:`replot`. Because ``GSEA`` Desktop have already
    done this for you.


<EOL>if database not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>sys.stderr.write("""<STR_LIT>""")<EOL>return <EOL><DEDENT>if database in ['<STR_LIT>', '<STR_LIT>']: database='<STR_LIT>'<EOL>lib_url='<STR_LIT>'%database<EOL>libs_json = json.loads(requests.get(lib_url).text)<EOL>libs = [lib['<STR_LIT>'] for lib in libs_json['<STR_LIT>']]<EOL>return sorted(libs)<EOL>

return enrichr active enrichr library name.
    :param str database: Select one from { 'Human', 'Mouse', 'Yeast', 'Fly', 'Fish', 'Worm' }


def __init__(self, host="<STR_LIT>", verbose=False):

super(Biomart, self).__init__(host=host, verbose=verbose)<EOL>hosts=["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL>i=<NUM_LIT:0><EOL>while (self.host is None) and (i < <NUM_LIT:3>):<EOL><INDENT>self.host = hosts[i]<EOL>i +=<NUM_LIT:1><EOL><DEDENT>

A wrapper of BioMart() from bioseverices.

        How to query validated dataset, attributes, filters:
        example:
        >>> from gseapy.parser import Biomart
        >>> bm = Biomart(verbose=False, host="asia.ensembl.org")
        >>> ## view validated marts
        >>> marts = bm.get_marts()
        >>> ## view validated dataset
        >>> datasets = bm.get_datasets(mart='ENSEMBL_MART_ENSEMBL')
        >>> ## view validated attributes
        >>> attrs = bm.get_attributes(dataset='hsapiens_gene_ensembl')
        >>> ## view validated filters
        >>> filters = bm.get_filters(dataset='hsapiens_gene_ensembl')
        >>> ## query results
        >>> results = bm.query(dataset='hsapiens_gene_ensembl',
                               attributes=['entrezgene', ‘go_id'],
                               filters={'ensembl_gene_id': [your input list]}
                              )


mart_names = pd.Series(self.names, name="<STR_LIT:Name>")<EOL>mart_descriptions = pd.Series(self.displayNames, name="<STR_LIT>")<EOL>return pd.concat([mart_names, mart_descriptions], axis=<NUM_LIT:1>)<EOL>


datasets = self.datasets(mart, raw=True)<EOL>return pd.read_csv(StringIO(datasets), header=None, usecols=[<NUM_LIT:1>, <NUM_LIT:2>],<EOL>names = ["<STR_LIT:Name>", "<STR_LIT>"],sep="<STR_LIT:\t>")<EOL>

Get available datasets from mart you've selected


attributes = self.attributes(dataset)<EOL>attr_ = [ (k, v[<NUM_LIT:0>]) for k, v in attributes.items()]<EOL>return pd.DataFrame(attr_, columns=["<STR_LIT>","<STR_LIT>"])<EOL>

Get available attritbutes from dataset you've selected


filters = self.filters(dataset)<EOL>filt_ = [ (k, v[<NUM_LIT:0>]) for k, v in filters.items()]<EOL>return pd.DataFrame(filt_, columns=["<STR_LIT>", "<STR_LIT>"])<EOL>

Get available filters from dataset you've selected


def query(self, dataset='<STR_LIT>', attributes=[], <EOL>filters={}, filename=None):

if not attributes: <EOL><INDENT>attributes = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'] <EOL><DEDENT>self.new_query()<EOL>self.add_dataset_to_xml(dataset)<EOL>for at in attributes:<EOL><INDENT>self.add_attribute_to_xml(at)<EOL><DEDENT>if filters:<EOL><INDENT>for k, v in filters.items(): <EOL><INDENT>if isinstance(v, list): v = "<STR_LIT:U+002C>".join(v)<EOL>self.add_filter_to_xml(k, v)<EOL><DEDENT><DEDENT>xml_query = self.get_xml()<EOL>results = super(Biomart, self).query(xml_query)<EOL>df = pd.read_csv(StringIO(results), header=None, sep="<STR_LIT:\t>",<EOL>names=attributes, index_col=None)<EOL>if filename is None: <EOL><INDENT>mkdirs(DEFAULT_CACHE_PATH)<EOL>filename = os.path.join(DEFAULT_CACHE_PATH, "<STR_LIT>".format(dataset))<EOL><DEDENT>df.to_csv(filename, sep="<STR_LIT:\t>", index=False)<EOL>return df<EOL>

mapping ids using BioMart.

        :param dataset: str, default: 'hsapiens_gene_ensembl'
        :param attributes: str, list, tuple
        :param filters: dict, {'filter name': list(filter value)}
        :param host: www.ensembl.org, asia.ensembl.org, useast.ensembl.org
        :return: a dataframe contains all attributes you selected.

        **Note**: it will take a couple of minutes to get the results.
        A xml template for querying biomart. (see https://gist.github.com/keithshep/7776579)

        exampleTaxonomy = "mmusculus_gene_ensembl"
        exampleGene = "ENSMUSG00000086981,ENSMUSG00000086982,ENSMUSG00000086983"
        urlTemplate = \
        '''http://ensembl.org/biomart/martservice?query=''' \
        '''<?xml version="1.0" encoding="UTF-8"?>''' \
        '''<!DOCTYPE Query>''' \
        '''<Query virtualSchemaName="default" formatter="CSV" header="0" uniqueRows="0" count="" datasetConfigVersion="0.6">''' \
        '''<Dataset name="%s" interface="default"><Filter name="ensembl_gene_id" value="%s"/>''' \
        '''<Attribute name="ensembl_gene_id"/><Attribute name="ensembl_transcript_id"/>''' \
        '''<Attribute name="transcript_start"/><Attribute name="transcript_end"/>''' \
        '''<Attribute name="exon_chrom_start"/><Attribute name="exon_chrom_end"/>''' \
        '''</Dataset>''' \
        '''</Query>'''

        exampleURL = urlTemplate % (exampleTaxonomy, exampleGene)
        req = requests.get(exampleURL, stream=True)


if axis is None:<EOL><INDENT>return data2d<EOL><DEDENT>assert axis in [<NUM_LIT:0>,<NUM_LIT:1>]<EOL>z_scored = data2d.apply(lambda x: (x-x.mean())/x.std(ddof=<NUM_LIT:1>), <EOL>axis=operator.xor(<NUM_LIT:1>, axis))<EOL>return z_scored<EOL>

Standardize the mean and variance of the data axis Parameters.

    :param data2d: DataFrame to normalize.
    :param axis: int, Which axis to normalize across. If 0, normalize across rows,
                  if 1, normalize across columns. If None, don't change data

    :Returns: Normalized DataFrame. Normalized data with a mean of 0 and variance of 1
              across the specified axis.


def heatmap(df, z_score=None, title='<STR_LIT>', figsize=(<NUM_LIT:5>,<NUM_LIT:5>), cmap='<STR_LIT>', <EOL>xticklabels=True, yticklabels=True, ofname=None, **kwargs):

df = zscore(df, axis=z_score)<EOL>df = df.iloc[::-<NUM_LIT:1>]<EOL>ny, nx = df.shape<EOL>xticks = np.arange(<NUM_LIT:0>, nx, <NUM_LIT:1>) + <NUM_LIT><EOL>yticks = np.arange(<NUM_LIT:0>, ny, <NUM_LIT:1>) + <NUM_LIT><EOL>if hasattr(sys, '<STR_LIT>') and (ofname is None): <EOL><INDENT>fig = plt.figure(figsize=figsize)<EOL><DEDENT>else:<EOL><INDENT>fig = Figure(figsize=figsize)<EOL>canvas = FigureCanvas(fig)<EOL><DEDENT>ax = fig.add_subplot(<NUM_LIT>)<EOL>vmin = np.percentile(df.min(), <NUM_LIT:2>)<EOL>vmax =  np.percentile(df.max(), <NUM_LIT>)<EOL>matrix = ax.pcolormesh(df.values, cmap=cmap, vmin=vmin, vmax=vmax)<EOL>ax.set_ylim([<NUM_LIT:0>,len(df)])<EOL>ax.set(xticks=xticks, yticks=yticks)<EOL>ax.set_xticklabels(df.columns.values if xticklabels else '<STR_LIT>', fontsize=<NUM_LIT>, rotation=<NUM_LIT>)<EOL>ax.set_yticklabels(df.index.values if yticklabels else '<STR_LIT>',  fontsize=<NUM_LIT>)<EOL>ax.set_title("<STR_LIT>"%title, fontsize=<NUM_LIT:20>)<EOL>ax.tick_params(axis='<STR_LIT>', which='<STR_LIT>', bottom=False, top=False,<EOL>right=False, left=False)<EOL>cbar = colorbar(matrix)<EOL>cbar.ax.tick_params(axis='<STR_LIT>', which='<STR_LIT>', bottom=False, top=False,<EOL>right=False, left=False)<EOL>for side in ["<STR_LIT>", "<STR_LIT:right>", "<STR_LIT:left>", "<STR_LIT>"]:<EOL><INDENT>ax.spines[side].set_visible(False)<EOL>cbar.ax.spines[side].set_visible(False)<EOL><DEDENT>if ofname is not None: <EOL><INDENT>fig.savefig(ofname, bbox_inches='<STR_LIT>', dpi=<NUM_LIT>)<EOL><DEDENT>return<EOL>

Visualize the dataframe.

    :param df: DataFrame from expression table.
    :param z_score: z_score axis{0, 1}. If None, don't normalize data.
    :param title: gene set name.
    :param outdir: path to save heatmap.
    :param figsize: heatmap figsize.
    :param cmap: matplotlib colormap.
    :param ofname: output file name. If None, don't save figure


def gseaplot(rank_metric, term, hits_indices, nes, pval, fdr, RES,<EOL>pheno_pos='<STR_LIT>', pheno_neg='<STR_LIT>', figsize=(<NUM_LIT:6>,<NUM_LIT>), <EOL>cmap='<STR_LIT>', ofname=None, **kwargs):

<EOL>norm = _MidpointNormalize(midpoint=<NUM_LIT:0>)<EOL>x = np.arange(len(rank_metric))<EOL>rankings = rank_metric.values<EOL>phenoP_label = pheno_pos + '<STR_LIT>'<EOL>phenoN_label = pheno_neg + '<STR_LIT>'<EOL>zero_score_ind = np.abs(rankings).argmin()<EOL>z_score_label = '<STR_LIT>' + str(zero_score_ind)<EOL>nes_label = '<STR_LIT>'+ "<STR_LIT>".format(float(nes))<EOL>pval_label = '<STR_LIT>'+ "<STR_LIT>".format(float(pval))<EOL>fdr_label = '<STR_LIT>'+ "<STR_LIT>".format(float(fdr))<EOL>im_matrix = np.tile(rankings, (<NUM_LIT:2>,<NUM_LIT:1>))<EOL>plt.rcParams.update({'<STR_LIT>':<NUM_LIT>,'<STR_LIT>':<NUM_LIT>})<EOL>gs = plt.GridSpec(<NUM_LIT:16>,<NUM_LIT:1>)<EOL>if hasattr(sys, '<STR_LIT>') and (ofname is None):<EOL><INDENT>fig = plt.figure(figsize=figsize)<EOL><DEDENT>else:<EOL><INDENT>fig = Figure(figsize=figsize)<EOL>canvas = FigureCanvas(fig)<EOL><DEDENT>ax1 =  fig.add_subplot(gs[<NUM_LIT:11>:])<EOL>module = '<STR_LIT>' if ofname is  None else ofname.split("<STR_LIT:.>")[-<NUM_LIT:2>]<EOL>if module == '<STR_LIT>':<EOL><INDENT>nes_label = '<STR_LIT>'+ "<STR_LIT>".format(float(nes))<EOL>pval_label='<STR_LIT>'<EOL>fdr_label='<STR_LIT>'<EOL>ax1.fill_between(x, y1=np.log(rankings), y2=<NUM_LIT:0>, color='<STR_LIT>')<EOL>ax1.set_ylabel("<STR_LIT>", fontsize=<NUM_LIT>)<EOL><DEDENT>else:<EOL><INDENT>ax1.fill_between(x, y1=rankings, y2=<NUM_LIT:0>, color='<STR_LIT>')<EOL>ax1.set_ylabel("<STR_LIT>", fontsize=<NUM_LIT>)<EOL><DEDENT>ax1.text(<NUM_LIT>, <NUM_LIT>, phenoP_label, color='<STR_LIT>',<EOL>horizontalalignment='<STR_LIT:left>', verticalalignment='<STR_LIT>',<EOL>transform=ax1.transAxes)<EOL>ax1.text(<NUM_LIT>, <NUM_LIT>, phenoN_label, color='<STR_LIT>',<EOL>horizontalalignment='<STR_LIT:right>', verticalalignment='<STR_LIT>',<EOL>transform=ax1.transAxes)<EOL>trans1 = transforms.blended_transform_factory(ax1.transData, ax1.transAxes)<EOL>if module != '<STR_LIT>':<EOL><INDENT>ax1.vlines(zero_score_ind, <NUM_LIT:0>, <NUM_LIT:1>, linewidth=<NUM_LIT>, transform=trans1, linestyles='<STR_LIT>', color='<STR_LIT>')<EOL>ax1.text(zero_score_ind, <NUM_LIT:0.5>, z_score_label,<EOL>horizontalalignment='<STR_LIT>',<EOL>verticalalignment='<STR_LIT>',<EOL>transform=trans1)<EOL><DEDENT>ax1.set_xlabel("<STR_LIT>", fontsize=<NUM_LIT>)<EOL>ax1.spines['<STR_LIT>'].set_visible(False)<EOL>ax1.tick_params(axis='<STR_LIT>', which='<STR_LIT>', top=False, right=False, left=False)<EOL>ax1.locator_params(axis='<STR_LIT:y>', nbins=<NUM_LIT:5>)<EOL>ax1.yaxis.set_major_formatter(plt.FuncFormatter(lambda tick_loc,tick_num :  '<STR_LIT>'.format(tick_loc) ))<EOL>ax2 = fig.add_subplot(gs[<NUM_LIT:8>:<NUM_LIT:10>], sharex=ax1)<EOL>trans2 = transforms.blended_transform_factory(ax2.transData, ax2.transAxes)<EOL>ax2.vlines(hits_indices, <NUM_LIT:0>, <NUM_LIT:1>,linewidth=<NUM_LIT>,transform=trans2)<EOL>ax2.spines['<STR_LIT>'].set_visible(False)<EOL>ax2.tick_params(axis='<STR_LIT>', which='<STR_LIT>', bottom=False, top=False,<EOL>labelbottom=False, right=False, left=False, labelleft=False)<EOL>ax3 =  fig.add_subplot(gs[<NUM_LIT:10>], sharex=ax1)<EOL>ax3.imshow(im_matrix, aspect='<STR_LIT>', norm=norm, cmap=cmap, interpolation='<STR_LIT:none>') <EOL>ax3.spines['<STR_LIT>'].set_visible(False)<EOL>ax3.tick_params(axis='<STR_LIT>', which='<STR_LIT>', bottom=False, top=False,<EOL>labelbottom=False, right=False, left=False,labelleft=False)<EOL>ax4 = fig.add_subplot(gs[:<NUM_LIT:8>], sharex=ax1)<EOL>ax4.plot(x, RES, linewidth=<NUM_LIT:4>, color ='<STR_LIT>')<EOL>ax4.text(<NUM_LIT>, <NUM_LIT>, fdr_label, transform=ax4.transAxes)<EOL>ax4.text(<NUM_LIT>, <NUM_LIT>, pval_label, transform=ax4.transAxes)<EOL>ax4.text(<NUM_LIT>, <NUM_LIT>, nes_label, transform=ax4.transAxes)<EOL>trans4 = transforms.blended_transform_factory(ax4.transAxes, ax4.transData)<EOL>ax4.hlines(<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>, linewidth=<NUM_LIT>, transform=trans4, color='<STR_LIT>')<EOL>ax4.set_ylabel("<STR_LIT>", fontsize=<NUM_LIT>)<EOL>ax4.set_xlim(min(x), max(x))<EOL>ax4.tick_params(axis='<STR_LIT>', which='<STR_LIT>', bottom=False, top=False, labelbottom=False, right=False)<EOL>ax4.locator_params(axis='<STR_LIT:y>', nbins=<NUM_LIT:5>)<EOL>ax4.yaxis.set_major_formatter(plt.FuncFormatter(lambda tick_loc,tick_num :  '<STR_LIT>'.format(tick_loc)) )<EOL>fig.suptitle(term, fontsize=<NUM_LIT:16>, fontweight='<STR_LIT>')<EOL>fig.subplots_adjust(hspace=<NUM_LIT:0>)<EOL>if ofname is not None: <EOL><INDENT>fig.savefig(ofname, bbox_inches='<STR_LIT>', dpi=<NUM_LIT>)<EOL><DEDENT>return<EOL>

This is the main function for reproducing the gsea plot.

    :param rank_metric: pd.Series for rankings, rank_metric.values.
    :param term: gene_set name
    :param hits_indices: hits indices of rank_metric.index presented in gene set S.
    :param nes: Normalized enrichment scores.
    :param pval: nominal p-value.
    :param fdr: false discovery rate.
    :param RES: running enrichment scores.
    :param pheno_pos: phenotype label, positive correlated.
    :param pheno_neg: phenotype label, negative correlated.
    :param figsize: matplotlib figsize.
    :param ofname: output file name. If None, don't save figure


def dotplot(df, column='<STR_LIT>', title='<STR_LIT>', cutoff=<NUM_LIT>, top_term=<NUM_LIT:10>, <EOL>sizes=None, norm=None, legend=True, figsize=(<NUM_LIT:6>, <NUM_LIT>), <EOL>cmap='<STR_LIT>', ofname=None, **kwargs):

colname = column    <EOL>if colname in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>can_be_coerced = df[colname].map(isfloat)<EOL>if np.sum(~can_be_coerced) > <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'%colname)<EOL><DEDENT>else:<EOL><INDENT>df.loc[:, colname] = df[colname].map(float)<EOL><DEDENT>df = df[df[colname] <= cutoff]<EOL>if len(df) < <NUM_LIT:1>: <EOL><INDENT>msg = "<STR_LIT>"%cutoff<EOL>return msg<EOL><DEDENT>df = df.assign(logAP=lambda x: - x[colname].apply(np.log10))<EOL>colname='<STR_LIT>'<EOL><DEDENT>df = df.sort_values(by=colname).iloc[-top_term:,:]<EOL>temp = df['<STR_LIT>'].str.split("<STR_LIT:/>", expand=True).astype(int)<EOL>df = df.assign(Hits=temp.iloc[:,<NUM_LIT:0>], Background=temp.iloc[:,<NUM_LIT:1>])<EOL>df = df.assign(Hits_ratio=lambda x:x.Hits / x.Background)<EOL>x = df.loc[:, colname].values<EOL>combined_score = df['<STR_LIT>'].round().astype('<STR_LIT:int>')<EOL>y = [i for i in range(<NUM_LIT:0>,len(df))]<EOL>ylabels = df['<STR_LIT>'].values<EOL>levels = numbers = np.sort(df.Hits.unique())<EOL>if norm is None:<EOL><INDENT>norm = Normalize()<EOL><DEDENT>elif isinstance(norm, tuple):<EOL><INDENT>norm = Normalize(*norm)<EOL><DEDENT>elif not isinstance(norm, Normalize):<EOL><INDENT>err = ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise ValueError(err)<EOL><DEDENT>min_width, max_width = np.r_[<NUM_LIT:20>, <NUM_LIT:100>] * plt.rcParams["<STR_LIT>"]<EOL>norm.clip = True<EOL>if not norm.scaled():<EOL><INDENT>norm(np.asarray(numbers))<EOL><DEDENT>size_limits = norm.vmin, norm.vmax<EOL>scl = norm(numbers)<EOL>widths = np.asarray(min_width + scl * (max_width - min_width))<EOL>if scl.mask.any():<EOL><INDENT>widths[scl.mask] = <NUM_LIT:0><EOL><DEDENT>sizes = dict(zip(levels, widths))<EOL>df['<STR_LIT>'] = df.Hits.map(sizes)<EOL>area = df['<STR_LIT>'].values<EOL>if hasattr(sys, '<STR_LIT>') and (ofname is None):<EOL><INDENT>fig, ax = plt.subplots(figsize=figsize)<EOL><DEDENT>else:<EOL><INDENT>fig = Figure(figsize=figsize)<EOL>canvas = FigureCanvas(fig)<EOL>ax = fig.add_subplot(<NUM_LIT>)<EOL><DEDENT>vmin = np.percentile(combined_score.min(), <NUM_LIT:2>)<EOL>vmax =  np.percentile(combined_score.max(), <NUM_LIT>)<EOL>sc = ax.scatter(x=x, y=y, s=area, edgecolors='<STR_LIT>', c=combined_score,<EOL>cmap=cmap, vmin=vmin, vmax=vmax)<EOL>if column in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>xlabel = "<STR_LIT>"%column<EOL><DEDENT>else:<EOL><INDENT>xlabel = column <EOL><DEDENT>ax.set_xlabel(xlabel, fontsize=<NUM_LIT>, fontweight='<STR_LIT>')<EOL>ax.yaxis.set_major_locator(plt.FixedLocator(y))<EOL>ax.yaxis.set_major_formatter(plt.FixedFormatter(ylabels))<EOL>ax.set_yticklabels(ylabels, fontsize=<NUM_LIT:16>)<EOL>ax.grid()<EOL>cax=fig.add_axes([<NUM_LIT>,<NUM_LIT>,<NUM_LIT>,<NUM_LIT>])<EOL>cbar = fig.colorbar(sc, cax=cax,)<EOL>cbar.ax.tick_params(right=True)<EOL>cbar.ax.set_title('<STR_LIT>',loc='<STR_LIT:left>', fontsize=<NUM_LIT:12>)<EOL>if len(df) >= <NUM_LIT:3>:<EOL><INDENT>idx = [area.argmax(), np.abs(area - area.mean()).argmin(), area.argmin()]<EOL>idx = unique(idx)<EOL><DEDENT>else:<EOL><INDENT>idx = df.index.values<EOL><DEDENT>label = df.iloc[idx, df.columns.get_loc('<STR_LIT>')]<EOL>if legend:<EOL><INDENT>handles, _ = ax.get_legend_handles_labels()<EOL>legend_markers = []<EOL>for ix in idx: <EOL><INDENT>legend_markers.append(ax.scatter([],[], s=area[ix], c='<STR_LIT:b>'))<EOL><DEDENT>ax.legend(legend_markers, label, title='<STR_LIT>')<EOL><DEDENT>ax.set_title(title, fontsize=<NUM_LIT:20>, fontweight='<STR_LIT>')<EOL>if ofname is not None: <EOL><INDENT>fig.savefig(ofname, bbox_inches='<STR_LIT>', dpi=<NUM_LIT>)<EOL>return<EOL><DEDENT>return ax<EOL>

Visualize enrichr results.

    :param df: GSEApy DataFrame results.
    :param column: which column of DataFrame to show. Default: Adjusted P-value
    :param title: figure title
    :param cutoff: p-adjust cut-off.
    :param top_term: number of enriched terms to show.
    :param ascending: bool, the order of y axis.
    :param sizes: tuple, (min, max) scatter size. Not functional for now
    :param norm: maplotlib.colors.Normalize object.
    :param legend: bool, whether to show legend.
    :param figsize: tuple, figure size.
    :param cmap: matplotlib colormap
    :param ofname: output file name. If None, don't save figure


def barplot(df, column='<STR_LIT>', title="<STR_LIT>", cutoff=<NUM_LIT>, top_term=<NUM_LIT:10>,<EOL>figsize=(<NUM_LIT>,<NUM_LIT:6>), color='<STR_LIT>', ofname=None, **kwargs):

colname = column   <EOL>if colname in ['<STR_LIT>', '<STR_LIT>']: <EOL><INDENT>df = df[df[colname] <= cutoff]<EOL>if len(df) < <NUM_LIT:1>: <EOL><INDENT>msg = "<STR_LIT>"%(title, cutoff)<EOL>return msg<EOL><DEDENT>df = df.assign(logAP = lambda x: - x[colname].apply(np.log10))<EOL>colname = '<STR_LIT>' <EOL><DEDENT>dd = df.sort_values(by=colname).iloc[-top_term:,:]<EOL>if hasattr(sys, '<STR_LIT>') and (ofname is None):<EOL><INDENT>fig = plt.figure(figsize=figsize)<EOL><DEDENT>else:<EOL><INDENT>fig = Figure(figsize=figsize)<EOL>canvas = FigureCanvas(fig)<EOL><DEDENT>ax = fig.add_subplot(<NUM_LIT>)<EOL>bar = dd.plot.barh(x='<STR_LIT>', y=colname, color=color, <EOL>alpha=<NUM_LIT>, fontsize=<NUM_LIT:16>, ax=ax)<EOL>if column in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>xlabel = "<STR_LIT>"%column<EOL><DEDENT>else:<EOL><INDENT>xlabel = column <EOL><DEDENT>bar.set_xlabel(xlabel, fontsize=<NUM_LIT:16>, fontweight='<STR_LIT>')<EOL>bar.set_ylabel("<STR_LIT>")<EOL>bar.set_title(title, fontsize=<NUM_LIT>, fontweight='<STR_LIT>')<EOL>bar.xaxis.set_major_locator(MaxNLocator(integer=True))<EOL>bar.legend_.remove()<EOL>adjust_spines(ax, spines=['<STR_LIT:left>','<STR_LIT>'])<EOL>if ofname is not None: <EOL><INDENT>fig.savefig(ofname, bbox_inches='<STR_LIT>', dpi=<NUM_LIT>)<EOL>return<EOL><DEDENT>return ax<EOL>

Visualize enrichr results.

    :param df: GSEApy DataFrame results.
    :param column: which column of DataFrame to show. Default: Adjusted P-value
    :param title: figure title.
    :param cutoff: cut-off of the cloumn you've chosen.
    :param top_term: number of top enriched terms to show.
    :param figsize: tuple, matplotlib figsize.
    :param color: color for bars.
    :param ofname: output file name. If None, don't save figure


for loc, spine in ax.spines.items():<EOL><INDENT>if loc in spines:<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>spine.set_color('<STR_LIT:none>')  <EOL><DEDENT><DEDENT>if '<STR_LIT:left>' in spines:<EOL><INDENT>ax.yaxis.set_ticks_position('<STR_LIT:left>')<EOL><DEDENT>else:<EOL><INDENT>ax.yaxis.set_ticks([])<EOL><DEDENT>if '<STR_LIT>' in spines:<EOL><INDENT>ax.xaxis.set_ticks_position('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>ax.xaxis.set_ticks([])<EOL><DEDENT>

function for removing spines and ticks.

    :param ax: axes object
    :param spines: a list of spines names to keep. e.g [left, right, top, bottom]
                    if spines = []. remove all spines and ticks.


def gsea(data, gene_sets, cls, outdir='<STR_LIT>', min_size=<NUM_LIT:15>, max_size=<NUM_LIT>, permutation_num=<NUM_LIT:1000>,<EOL>weighted_score_type=<NUM_LIT:1>,permutation_type='<STR_LIT>', method='<STR_LIT>',<EOL>ascending=False, processes=<NUM_LIT:1>, figsize=(<NUM_LIT>,<NUM_LIT:6>), format='<STR_LIT>',<EOL>graph_num=<NUM_LIT:20>, no_plot=False, seed=None, verbose=False):

gs = GSEA(data, gene_sets, cls, outdir, min_size, max_size, permutation_num,<EOL>weighted_score_type, permutation_type, method, ascending, processes,<EOL>figsize, format, graph_num, no_plot, seed, verbose)<EOL>gs.run()<EOL>return gs<EOL>

Run Gene Set Enrichment Analysis.

    :param data: Gene expression data table, Pandas DataFrame, gct file.
    :param gene_sets: Enrichr Library name or .gmt gene sets file or dict of gene sets. Same input with GSEA.
    :param cls: A list or a .cls file format required for GSEA.
    :param str outdir: Results output directory.
    :param int permutation_num: Number of permutations for significance computation. Default: 1000.
    :param str permutation_type: Permutation type, "phenotype" for phenotypes, "gene_set" for genes.
    :param int min_size: Minimum allowed number of genes from gene set also the data set. Default: 15.
    :param int max_size: Maximum allowed number of genes from gene set also the data set. Default: 500.
    :param float weighted_score_type: Refer to :func:`algorithm.enrichment_score`. Default:1.
    :param method:  The method used to calculate a correlation or ranking. Default: 'log2_ratio_of_classes'.
                   Others methods are:

                   1. 'signal_to_noise'

                      You must have at least three samples for each phenotype to use this metric.
                      The larger the signal-to-noise ratio, the larger the differences of the means (scaled by the standard deviations);
                      that is, the more distinct the gene expression is in each phenotype and the more the gene acts as a “class marker.”

                   2. 't_test'

                      Uses the difference of means scaled by the standard deviation and number of samples.
                      Note: You must have at least three samples for each phenotype to use this metric.
                      The larger the tTest ratio, the more distinct the gene expression is in each phenotype
                      and the more the gene acts as a “class marker.”

                   3. 'ratio_of_classes' (also referred to as fold change).

                      Uses the ratio of class means to calculate fold change for natural scale data.

                   4. 'diff_of_classes'

                      Uses the difference of class means to calculate fold change for nature scale data

                   5. 'log2_ratio_of_classes'

                      Uses the log2 ratio of class means to calculate fold change for natural scale data.
                      This is the recommended statistic for calculating fold change for log scale data.

    :param bool ascending: Sorting order of rankings. Default: False.
    :param int processes: Number of Processes you are going to use. Default: 1.
    :param list figsize: Matplotlib figsize, accept a tuple or list, e.g. [width,height]. Default: [6.5,6].
    :param str format: Matplotlib figure format. Default: 'pdf'.
    :param int graph_num: Plot graphs for top sets of each phenotype.
    :param bool no_plot: If equals to True, no figure will be drawn. Default: False.
    :param seed: Random seed. expect an integer. Default:None.
    :param bool verbose: Bool, increase output verbosity, print out progress of your job, Default: False.

    :return: Return a GSEA obj. All results store to a dictionary, obj.results,
             where contains::

                 | {es: enrichment score,
                 |  nes: normalized enrichment score,
                 |  p: P-value,
                 |  fdr: FDR,
                 |  size: gene set size,
                 |  matched_size: genes matched to the data,
                 |  genes: gene names from the data set
                 |  ledge_genes: leading edge genes}


def ssgsea(data, gene_sets, outdir="<STR_LIT>", sample_norm_method='<STR_LIT>', min_size=<NUM_LIT:15>, max_size=<NUM_LIT>,<EOL>permutation_num=<NUM_LIT:0>, weighted_score_type=<NUM_LIT>, scale=True, ascending=False, processes=<NUM_LIT:1>,<EOL>figsize=(<NUM_LIT:7>,<NUM_LIT:6>), format='<STR_LIT>', graph_num=<NUM_LIT:20>, no_plot=False, seed=None, verbose=False):

ss = SingleSampleGSEA(data, gene_sets, outdir, sample_norm_method, min_size, max_size,<EOL>permutation_num, weighted_score_type, scale, ascending,<EOL>processes, figsize, format, graph_num, no_plot, seed, verbose)<EOL>ss.run()<EOL>return ss<EOL>

Run Gene Set Enrichment Analysis with single sample GSEA tool

    :param data: Expression table, pd.Series, pd.DataFrame, GCT file, or .rnk file format.
    :param gene_sets: Enrichr Library name or .gmt gene sets file or dict of gene sets. Same input with GSEA.
    :param outdir: Results output directory.
    :param str sample_norm_method: "Sample normalization method. Choose from {'rank', 'log', 'log_rank'}. Default: rank.

               1. 'rank': Rank your expression data, and transform by 10000*rank_dat/gene_numbers
               2. 'log' : Do not rank, but transform data by log(data + exp(1)), while data = data[data<1] =1.
               3. 'log_rank': Rank your expression data, and transform by log(10000*rank_dat/gene_numbers+ exp(1))
               4. 'custom': Do nothing, and use your own rank value to calculate enrichment score.

    see here: https://github.com/GSEA-MSigDB/ssGSEAProjection-gpmodule/blob/master/src/ssGSEAProjection.Library.R, line 86

    :param int min_size: Minimum allowed number of genes from gene set also the data set. Default: 15.
    :param int max_size: Maximum allowed number of genes from gene set also the data set. Default: 2000.
    :param int permutation_num: Number of permutations for significance computation. Default: 0.
    :param str weighted_score_type: Refer to :func:`algorithm.enrichment_score`. Default:0.25.
    :param bool scale: If True, normalize the scores by number of genes in the gene sets.
    :param bool ascending: Sorting order of rankings. Default: False.
    :param int processes: Number of Processes you are going to use. Default: 1.
    :param list figsize: Matplotlib figsize, accept a tuple or list, e.g. [width,height]. Default: [7,6].
    :param str format: Matplotlib figure format. Default: 'pdf'.
    :param int graph_num: Plot graphs for top sets of each phenotype.
    :param bool no_plot: If equals to True, no figure will be drawn. Default: False.
    :param seed: Random seed. expect an integer. Default:None.
    :param bool verbose: Bool, increase output verbosity, print out progress of your job, Default: False.

    :return: Return a ssGSEA obj.
             All results store to  a dictionary, access enrichment score by obj.resultsOnSamples,
             and normalized enrichment score by obj.res2d.
             if permutation_num > 0, additional results contain::

                 | {es: enrichment score,
                 |  nes: normalized enrichment score,
                 |  p: P-value,
                 |  fdr: FDR,
                 |  size: gene set size,
                 |  matched_size: genes matched to the data,
                 |  genes: gene names from the data set
                 |  ledge_genes: leading edge genes, if permutation_num >0}


def prerank(rnk, gene_sets, outdir='<STR_LIT>', pheno_pos='<STR_LIT>', pheno_neg='<STR_LIT>',<EOL>min_size=<NUM_LIT:15>, max_size=<NUM_LIT>, permutation_num=<NUM_LIT:1000>, weighted_score_type=<NUM_LIT:1>,<EOL>ascending=False, processes=<NUM_LIT:1>, figsize=(<NUM_LIT>,<NUM_LIT:6>), format='<STR_LIT>',<EOL>graph_num=<NUM_LIT:20>, no_plot=False, seed=None, verbose=False):

pre = Prerank(rnk, gene_sets, outdir, pheno_pos, pheno_neg,<EOL>min_size, max_size, permutation_num, weighted_score_type,<EOL>ascending, processes, figsize, format, graph_num, no_plot, seed, verbose)<EOL>pre.run()<EOL>return pre<EOL>

Run Gene Set Enrichment Analysis with pre-ranked correlation defined by user.

    :param rnk: pre-ranked correlation table or pandas DataFrame. Same input with ``GSEA`` .rnk file.
    :param gene_sets: Enrichr Library name or .gmt gene sets file or dict of gene sets. Same input with GSEA.
    :param outdir: results output directory.
    :param int permutation_num: Number of permutations for significance computation. Default: 1000.
    :param int min_size: Minimum allowed number of genes from gene set also the data set. Default: 15.
    :param int max_size: Maximum allowed number of genes from gene set also the data set. Defaults: 500.
    :param str weighted_score_type: Refer to :func:`algorithm.enrichment_score`. Default:1.
    :param bool ascending: Sorting order of rankings. Default: False.
    :param int processes: Number of Processes you are going to use. Default: 1.
    :param list figsize: Matplotlib figsize, accept a tuple or list, e.g. [width,height]. Default: [6.5,6].
    :param str format: Matplotlib figure format. Default: 'pdf'.
    :param int graph_num: Plot graphs for top sets of each phenotype.
    :param bool no_plot: If equals to True, no figure will be drawn. Default: False.
    :param seed: Random seed. expect an integer. Default:None.
    :param bool verbose: Bool, increase output verbosity, print out progress of your job, Default: False.

    :return: Return a Prerank obj. All results store to  a dictionary, obj.results,
             where contains::

                 | {es: enrichment score,
                 |  nes: normalized enrichment score,
                 |  p: P-value,
                 |  fdr: FDR,
                 |  size: gene set size,
                 |  matched_size: genes matched to the data,
                 |  genes: gene names from the data set
                 |  ledge_genes: leading edge genes}


def replot(indir, outdir='<STR_LIT>', weighted_score_type=<NUM_LIT:1>,<EOL>min_size=<NUM_LIT:3>, max_size=<NUM_LIT:1000>, figsize=(<NUM_LIT>,<NUM_LIT:6>), graph_num=<NUM_LIT:20>, format='<STR_LIT>', verbose=False):

rep = Replot(indir, outdir, weighted_score_type,<EOL>min_size, max_size, figsize, graph_num, format, verbose)<EOL>rep.run()<EOL>return<EOL>

The main function to reproduce GSEA desktop outputs.

    :param indir: GSEA desktop results directory. In the sub folder, you must contain edb file folder.
    :param outdir: Output directory.
    :param float weighted_score_type: weighted score type. choose from {0,1,1.5,2}. Default: 1.
    :param list figsize: Matplotlib output figure figsize. Default: [6.5,6].
    :param str format: Matplotlib output figure format. Default: 'pdf'.
    :param int min_size: Min size of input genes presented in Gene Sets. Default: 3.
    :param int max_size: Max size of input genes presented in Gene Sets. Default: 5000.
                     You are not encouraged to use min_size, or max_size argument in :func:`replot` function.
                     Because gmt file has already been filtered.
    :param verbose: Bool, increase output verbosity, print out progress of your job, Default: False.

    :return: Generate new figures with selected figure format. Default: 'pdf'.


self._outdir = self.outdir<EOL>if self._outdir is None:<EOL><INDENT>self._tmpdir = TemporaryDirectory()<EOL>self.outdir = self._tmpdir.name<EOL><DEDENT>elif isinstance(self.outdir, str):<EOL><INDENT>mkdirs(self.outdir)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>"%type(self.outdir))<EOL><DEDENT>if isinstance(self.gene_sets, str):<EOL><INDENT>_gset = os.path.split(self.gene_sets)[-<NUM_LIT:1>].lower().rstrip("<STR_LIT>")<EOL><DEDENT>elif isinstance(self.gene_sets, dict):<EOL><INDENT>_gset = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>logfile = os.path.join(self.outdir, "<STR_LIT>" % (self.module, _gset))<EOL>return logfile<EOL>


cpu_num = cpu_count()-<NUM_LIT:1><EOL>if self._processes > cpu_num:<EOL><INDENT>cores = cpu_num<EOL><DEDENT>elif self._processes < <NUM_LIT:1>:<EOL><INDENT>cores = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>cores = self._processes<EOL><DEDENT>self._processes = int(cores)<EOL>


<EOL>if isinstance(rnk, pd.DataFrame):<EOL><INDENT>rank_metric = rnk.copy()<EOL>if rnk.shape[<NUM_LIT:1>] == <NUM_LIT:1>: rank_metric = rnk.reset_index()<EOL><DEDENT>elif isinstance(rnk, pd.Series):<EOL><INDENT>rank_metric = rnk.reset_index()<EOL><DEDENT>elif os.path.isfile(rnk):<EOL><INDENT>rank_metric = pd.read_csv(rnk, header=None, comment='<STR_LIT:#>', sep="<STR_LIT:\t>")<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>rank_metric.sort_values(by=rank_metric.columns[<NUM_LIT:1>], ascending=self.ascending, inplace=True)<EOL>if rank_metric.isnull().any(axis=<NUM_LIT:1>).sum() ><NUM_LIT:0>:<EOL><INDENT>self._logger.warning("<STR_LIT>")<EOL>NAs = rank_metric[rank_metric.isnull().any(axis=<NUM_LIT:1>)]<EOL>self._logger.debug('<STR_LIT>'+NAs.to_string())<EOL>rank_metric.dropna(how='<STR_LIT>', inplace=True)<EOL><DEDENT>if rank_metric.duplicated(subset=rank_metric.columns[<NUM_LIT:0>]).sum() ><NUM_LIT:0>:<EOL><INDENT>self._logger.warning("<STR_LIT>")<EOL>dups = rank_metric[rank_metric.duplicated(subset=rank_metric.columns[<NUM_LIT:0>])]<EOL>self._logger.debug('<STR_LIT>'+dups.to_string())<EOL>rank_metric.drop_duplicates(subset=rank_metric.columns[<NUM_LIT:0>], inplace=True, keep='<STR_LIT>')<EOL><DEDENT>rank_metric.reset_index(drop=True, inplace=True)<EOL>rank_metric.columns = ['<STR_LIT>','<STR_LIT>']<EOL>rankser = rank_metric.set_index('<STR_LIT>')['<STR_LIT>']<EOL>self.ranking = rankser<EOL>return rankser<EOL>

Parse ranking file. This file contains ranking correlation vector( or expression values)
           and gene names or ids.

            :param rnk: the .rnk file of GSEA input or a Pandas DataFrame, Series instance.
            :return: a Pandas Series with gene name indexed rankings


if isinstance(gmt, dict):<EOL><INDENT>genesets_dict = gmt<EOL><DEDENT>elif isinstance(gmt, str):<EOL><INDENT>genesets_dict = self.parse_gmt(gmt)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>subsets = list(genesets_dict.keys())<EOL>self.n_genesets = len(subsets)<EOL>for subset in subsets:<EOL><INDENT>subset_list = genesets_dict.get(subset)<EOL>if isinstance(subset_list, set):<EOL><INDENT>subset_list = list(subset_list)<EOL>genesets_dict[subset] = subset_list<EOL><DEDENT>tag_indicator = np.in1d(gene_list, subset_list, assume_unique=True)<EOL>tag_len = tag_indicator.sum()<EOL>if  self.min_size <= tag_len <= self.max_size: continue<EOL>del genesets_dict[subset]<EOL><DEDENT>filsets_num = len(subsets) - len(genesets_dict)<EOL>self._logger.info("<STR_LIT>"%(filsets_num, self.max_size, self.min_size))<EOL>if filsets_num == len(subsets):<EOL><INDENT>self._logger.error("<STR_LIT>" +"<STR_LIT>" )<EOL>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>self._gmtdct=genesets_dict<EOL>return genesets_dict<EOL>


if gmt.lower().endswith("<STR_LIT>"):<EOL><INDENT>with open(gmt) as genesets:<EOL><INDENT>genesets_dict = { line.strip().split("<STR_LIT:\t>")[<NUM_LIT:0>]: line.strip().split("<STR_LIT:\t>")[<NUM_LIT:2>:]<EOL>for line in genesets.readlines()}<EOL><DEDENT>return genesets_dict<EOL><DEDENT>elif gmt in DEFAULT_LIBRARY:<EOL><INDENT>pass<EOL><DEDENT>elif gmt in self.get_libraries():<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>self._logger.error("<STR_LIT>"%gmt)<EOL>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>tmpname = "<STR_LIT>" + gmt + "<STR_LIT>"<EOL>tempath = os.path.join(DEFAULT_CACHE_PATH, tmpname)<EOL>if os.path.isfile(tempath):<EOL><INDENT>self._logger.info("<STR_LIT>"%DEFAULT_CACHE_PATH)<EOL>return self.parse_gmt(tempath)<EOL><DEDENT>else:<EOL><INDENT>return self._download_libraries(gmt)<EOL><DEDENT>


lib_url='<STR_LIT>'%database<EOL>libs_json = json.loads(requests.get(lib_url).text)<EOL>libs = [lib['<STR_LIT>'] for lib in libs_json['<STR_LIT>']]<EOL>return sorted(libs)<EOL>

return active enrichr library name.Offical API


self._logger.info("<STR_LIT>")<EOL>s = retry(<NUM_LIT:5>)<EOL>ENRICHR_URL = '<STR_LIT>'<EOL>query_string = '<STR_LIT>'<EOL>response = s.get( ENRICHR_URL + query_string % libname, timeout=None)<EOL>if not response.ok:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>mkdirs(DEFAULT_CACHE_PATH)<EOL>genesets_dict = {}<EOL>outname = "<STR_LIT>"%libname<EOL>gmtout = open(os.path.join(DEFAULT_CACHE_PATH, outname), "<STR_LIT:w>")<EOL>for line in response.iter_lines(chunk_size=<NUM_LIT>, decode_unicode='<STR_LIT:utf-8>'):<EOL><INDENT>line=line.strip()<EOL>k = line.split("<STR_LIT:\t>")[<NUM_LIT:0>]<EOL>v = list(map(lambda x: x.split("<STR_LIT:U+002C>")[<NUM_LIT:0>], line.split("<STR_LIT:\t>")[<NUM_LIT:2>:]))<EOL>genesets_dict.update({ k: v})<EOL>outline = "<STR_LIT>"%(k, "<STR_LIT:\t>".join(v))<EOL>gmtout.write(outline)<EOL><DEDENT>gmtout.close()<EOL>return genesets_dict<EOL>


def _heatmat(self, df, classes, pheno_pos, pheno_neg):

width = len(classes) if len(classes) >= <NUM_LIT:6> else  <NUM_LIT:5><EOL>cls_booA =list(map(lambda x: True if x == pheno_pos else False, classes))<EOL>cls_booB =list(map(lambda x: True if x == pheno_neg else False, classes))<EOL>datA = df.loc[:, cls_booA]<EOL>datB = df.loc[:, cls_booB]<EOL>datAB=pd.concat([datA,datB], axis=<NUM_LIT:1>)<EOL>self._width = width<EOL>self.heatmat = datAB<EOL>return<EOL>


def _plotting(self, rank_metric, results, graph_num, outdir, <EOL>format, figsize, pheno_pos='<STR_LIT>', pheno_neg='<STR_LIT>'):

<EOL>if self._outdir is None: return<EOL>top_term = self.res2d.index[:graph_num]<EOL>pool = Pool(self._processes)<EOL>for gs in top_term:<EOL><INDENT>hit = results.get(gs)['<STR_LIT>']<EOL>NES = '<STR_LIT>' if self.module != '<STR_LIT>' else '<STR_LIT>'<EOL>term = gs.replace('<STR_LIT:/>','<STR_LIT:_>').replace("<STR_LIT::>","<STR_LIT:_>")<EOL>outfile = '<STR_LIT>'.format(self.outdir, term, self.module, self.format)<EOL>pool.apply_async(gseaplot, args=(rank_metric, term, hit, results.get(gs)[NES],<EOL>results.get(gs)['<STR_LIT>'],results.get(gs)['<STR_LIT>'],<EOL>results.get(gs)['<STR_LIT>'],<EOL>pheno_pos, pheno_neg, <EOL>figsize, '<STR_LIT>', outfile))<EOL>if self.module == '<STR_LIT>':<EOL><INDENT>outfile2 = "<STR_LIT>".format(self.outdir, term, self.format)<EOL>pool.apply_async(heatmap, args=(self.heatmat.iloc[hit, :], <NUM_LIT:0>, term, <EOL>(self._width, len(hit)/<NUM_LIT:2>+<NUM_LIT:2>), '<STR_LIT>',<EOL>True, True, outfile2))<EOL><DEDENT><DEDENT>pool.close()<EOL>pool.join()<EOL>

Plotting API.
            :param rank_metric: sorted pd.Series with rankings values.
            :param results: self.results
            :param data: preprocessed expression table


def _save_results(self, zipdata, outdir, module, gmt, rank_metric, permutation_type):

res = OrderedDict()<EOL>for gs, gseale, ind, RES in zipdata:<EOL><INDENT>rdict = OrderedDict()<EOL>rdict['<STR_LIT>'] = gseale[<NUM_LIT:0>]<EOL>rdict['<STR_LIT>'] = gseale[<NUM_LIT:1>]<EOL>rdict['<STR_LIT>'] = gseale[<NUM_LIT:2>]<EOL>rdict['<STR_LIT>'] = gseale[<NUM_LIT:3>]<EOL>rdict['<STR_LIT>'] = len(gmt[gs])<EOL>rdict['<STR_LIT>'] = len(ind)<EOL>_genes = rank_metric.index.values[ind]<EOL>rdict['<STR_LIT>'] = "<STR_LIT:;>".join([ str(g).strip() for g in _genes ])<EOL>if self.module != '<STR_LIT>':<EOL><INDENT>if rdict['<STR_LIT>'] > <NUM_LIT:0>:<EOL><INDENT>idx = RES.argmax()<EOL>ldg_pos = list(filter(lambda x: x<= idx, ind))<EOL><DEDENT>elif rdict['<STR_LIT>'] < <NUM_LIT:0>:<EOL><INDENT>idx = RES.argmin()<EOL>ldg_pos = list(filter(lambda x: x >= idx, ind))<EOL><DEDENT>else:<EOL><INDENT>ldg_pos = ind <EOL><DEDENT>rdict['<STR_LIT>'] = '<STR_LIT:;>'.join(list(map(str,rank_metric.iloc[ldg_pos].index)))<EOL><DEDENT>rdict['<STR_LIT>'] = RES<EOL>rdict['<STR_LIT>'] = ind<EOL>res[gs] = rdict<EOL><DEDENT>self.results  = res<EOL>res_df = pd.DataFrame.from_dict(res, orient='<STR_LIT:index>')<EOL>res_df.index.name = '<STR_LIT>'<EOL>res_df.drop(['<STR_LIT>','<STR_LIT>'], axis=<NUM_LIT:1>, inplace=True)<EOL>res_df.sort_values(by=['<STR_LIT>','<STR_LIT>'], inplace=True)<EOL>self.res2d = res_df<EOL>if self._outdir is None: return<EOL>out = os.path.join(outdir,'<STR_LIT>'.format(b=module, c=permutation_type))<EOL>if self.module == '<STR_LIT>':<EOL><INDENT>out = out.replace("<STR_LIT>","<STR_LIT>")<EOL>with open(out, '<STR_LIT:a>') as f:<EOL><INDENT>f.write('<STR_LIT>')<EOL>f.write("<STR_LIT>")<EOL>res_df.to_csv(f, sep='<STR_LIT:\t>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>res_df.to_csv(out)<EOL><DEDENT>return<EOL>


<EOL>if isinstance(self.data, pd.DataFrame) :<EOL><INDENT>exprs = self.data.copy()<EOL>if exprs.index.dtype == '<STR_LIT:O>':<EOL><INDENT>exprs = exprs.reset_index()<EOL><DEDENT><DEDENT>elif os.path.isfile(self.data) :<EOL><INDENT>if self.data.endswith("<STR_LIT>"):<EOL><INDENT>exprs = pd.read_csv(self.data, skiprows=<NUM_LIT:1>, comment='<STR_LIT:#>',sep="<STR_LIT:\t>")<EOL><DEDENT>else:<EOL><INDENT>exprs = pd.read_csv(self.data, comment='<STR_LIT:#>',sep="<STR_LIT:\t>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if exprs.iloc[:,<NUM_LIT:0>].duplicated().sum() > <NUM_LIT:0>:<EOL><INDENT>self._logger.warning("<STR_LIT>")<EOL>exprs.drop_duplicates(subset=exprs.columns[<NUM_LIT:0>], inplace=True) <EOL><DEDENT>if exprs.isnull().any().sum() > <NUM_LIT:0>:<EOL><INDENT>self._logger.warning("<STR_LIT>")<EOL>exprs.dropna(how='<STR_LIT:all>', inplace=True) <EOL>exprs = exprs.fillna(<NUM_LIT:0>)<EOL><DEDENT>exprs.set_index(keys=exprs.columns[<NUM_LIT:0>], inplace=True)<EOL>df = exprs.select_dtypes(include=[np.number])<EOL>df_std =  df.groupby(by=cls_vec, axis=<NUM_LIT:1>).std()<EOL>df =  df[~df_std.isin([<NUM_LIT:0>]).any(axis=<NUM_LIT:1>)]<EOL>df = df + <NUM_LIT> <EOL>return df<EOL>

pre-processed the data frame.new filtering methods will be implement here.


assert self.permutation_type in ["<STR_LIT>", "<STR_LIT>"]<EOL>assert self.min_size <= self.max_size<EOL>self._logger.info("<STR_LIT>")<EOL>phenoPos, phenoNeg, cls_vector = gsea_cls_parser(self.classes)<EOL>dat = self.load_data(cls_vector)<EOL>assert len(dat) > <NUM_LIT:1><EOL>dat2 = ranking_metric(df=dat, method=self.method, pos=phenoPos, neg=phenoNeg,<EOL>classes=cls_vector, ascending=self.ascending)<EOL>self.ranking = dat2<EOL>gmt = self.load_gmt(gene_list=dat2.index.values, gmt=self.gene_sets)<EOL>self._logger.info("<STR_LIT>"% len(gmt))<EOL>self._logger.info("<STR_LIT>")<EOL>self._set_cores()<EOL>dataset = dat if self.permutation_type =='<STR_LIT>' else dat2<EOL>gsea_results,hit_ind,rank_ES, subsets = gsea_compute_tensor(data=dataset, gmt=gmt, n=self.permutation_num,<EOL>weighted_score_type=self.weighted_score_type,<EOL>permutation_type=self.permutation_type,<EOL>method=self.method,<EOL>pheno_pos=phenoPos, pheno_neg=phenoNeg,<EOL>classes=cls_vector, ascending=self.ascending,<EOL>processes=self._processes, seed=self.seed)<EOL>self._logger.info("<STR_LIT>")<EOL>res_zip = zip(subsets, list(gsea_results), hit_ind, rank_ES)<EOL>self._save_results(zipdata=res_zip, outdir=self.outdir, module=self.module,<EOL>gmt=gmt, rank_metric=dat2, permutation_type=self.permutation_type)<EOL>self._heatmat(df=dat.loc[dat2.index], classes=cls_vector, <EOL>pheno_pos=phenoPos, pheno_neg=phenoNeg)<EOL>if not self._noplot:<EOL><INDENT>self._plotting(rank_metric=dat2, results=self.results,<EOL>graph_num=self.graph_num, outdir=self.outdir,<EOL>figsize=self.figsize, format=self.format,<EOL>pheno_pos=phenoPos, pheno_neg=phenoNeg)<EOL><DEDENT>self._logger.info("<STR_LIT>")<EOL>if self._outdir is None:<EOL><INDENT>self._tmpdir.cleanup()<EOL><DEDENT>return<EOL>


assert self.min_size <= self.max_size<EOL>dat2 = self._load_ranking(self.rnk)<EOL>assert len(dat2) > <NUM_LIT:1><EOL>self._set_cores()<EOL>self._logger.info("<STR_LIT>")<EOL>gmt = self.load_gmt(gene_list=dat2.index.values, gmt=self.gene_sets)<EOL>self._logger.info("<STR_LIT>"% len(gmt))<EOL>self._logger.info("<STR_LIT>")<EOL>gsea_results, hit_ind,rank_ES, subsets = gsea_compute(data=dat2, n=self.permutation_num, gmt=gmt,<EOL>weighted_score_type=self.weighted_score_type,<EOL>permutation_type='<STR_LIT>', method=None,<EOL>pheno_pos=self.pheno_pos, pheno_neg=self.pheno_neg,<EOL>classes=None, ascending=self.ascending,<EOL>processes=self._processes, seed=self.seed)<EOL>self._logger.info("<STR_LIT>")<EOL>res_zip = zip(subsets, list(gsea_results), hit_ind, rank_ES)<EOL>self._save_results(zipdata=res_zip, outdir=self.outdir, module=self.module,<EOL>gmt=gmt, rank_metric=dat2, permutation_type="<STR_LIT>")<EOL>if not self._noplot:<EOL><INDENT>self._plotting(rank_metric=dat2, results=self.results,<EOL>graph_num=self.graph_num, outdir=self.outdir,<EOL>figsize=self.figsize, format=self.format,<EOL>pheno_pos=self.pheno_pos, pheno_neg=self.pheno_neg)<EOL><DEDENT>self._logger.info("<STR_LIT>")<EOL>if self._outdir is None:<EOL><INDENT>self._tmpdir.cleanup()<EOL><DEDENT>return<EOL>


ranked genes' location plot
        TODO


if self.sample_norm_method == '<STR_LIT>':<EOL><INDENT>data = dat.rank(axis=<NUM_LIT:0>, method='<STR_LIT>', na_option='<STR_LIT>')<EOL>data = <NUM_LIT>*data / data.shape[<NUM_LIT:0>]<EOL><DEDENT>elif self.sample_norm_method == '<STR_LIT>':<EOL><INDENT>data = dat.rank(axis=<NUM_LIT:0>, method='<STR_LIT>', na_option='<STR_LIT>')<EOL>data = log(<NUM_LIT>*data / data.shape[<NUM_LIT:0>] + exp(<NUM_LIT:1>))<EOL><DEDENT>elif self.sample_norm_method == '<STR_LIT>':<EOL><INDENT>dat[dat < <NUM_LIT:1>] = <NUM_LIT:1><EOL>data = log(dat + exp(<NUM_LIT:1>))<EOL><DEDENT>elif self.sample_norm_method == '<STR_LIT>':<EOL><INDENT>self._logger.info("<STR_LIT>")<EOL>data = dat<EOL><DEDENT>else:<EOL><INDENT>sys.stderr.write("<STR_LIT>"%self.sample_norm_method)<EOL>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>return data<EOL>

normalization samples
           see here: http://rowley.mit.edu/caw_web/ssGSEAProjection/ssGSEAProjection.Library.R


self._logger.info("<STR_LIT>")<EOL>data = self.load_data()<EOL>normdat = self.norm_samples(data)<EOL>gmt = self.load_gmt(gene_list=normdat.index.values, gmt=self.gene_sets)<EOL>self._logger.info("<STR_LIT>"% len(gmt))<EOL>self._set_cores()<EOL>self._logger.info("<STR_LIT>")<EOL>if self.permutation_num == <NUM_LIT:0> :<EOL><INDENT>self.runSamples(df=normdat, gmt=gmt)<EOL><DEDENT>else:<EOL><INDENT>self._logger.warning("<STR_LIT>")<EOL>self.runSamplesPermu(df=normdat, gmt=gmt)<EOL><DEDENT>if self._outdir is None:<EOL><INDENT>self._tmpdir.cleanup()<EOL><DEDENT>


def runSamplesPermu(self, df, gmt=None):

assert self.min_size <= self.max_size<EOL>mkdirs(self.outdir)<EOL>self.resultsOnSamples = OrderedDict()<EOL>outdir = self.outdir<EOL>for name, ser in df.iteritems():<EOL><INDENT>self.outdir = os.path.join(outdir, str(name))<EOL>self._logger.info("<STR_LIT>" % name)<EOL>mkdirs(self.outdir)<EOL>dat2 = ser.sort_values(ascending=self.ascending)<EOL>gsea_results, hit_ind,rank_ES, subsets = gsea_compute(data=dat2, n=self.permutation_num, gmt=gmt,<EOL>weighted_score_type=self.weighted_score_type,<EOL>permutation_type='<STR_LIT>', method=None,<EOL>pheno_pos='<STR_LIT>', pheno_neg='<STR_LIT>',<EOL>classes=None, ascending=self.ascending,<EOL>processes=self._processes,<EOL>seed=self.seed, single=True, scale=self.scale)<EOL>res_zip = zip(subsets, list(gsea_results), hit_ind, rank_ES)<EOL>self._save_results(zipdata=res_zip, outdir=self.outdir, module=self.module,<EOL>gmt=gmt, rank_metric=dat2, permutation_type="<STR_LIT>")<EOL>self.resultsOnSamples[name] = self.res2d.es<EOL>if self._noplot: continue<EOL>self._logger.info("<STR_LIT>" % name)<EOL>self._plotting(rank_metric=dat2, results=self.results,<EOL>graph_num=self.graph_num, outdir=self.outdir,<EOL>figsize=self.figsize, format=self.format)<EOL><DEDENT>self._save(outdir)<EOL>return<EOL>

Single Sample GSEA workflow with permutation procedure


<EOL>self.resultsOnSamples = OrderedDict()<EOL>outdir = self.outdir<EOL>subsets = sorted(gmt.keys())<EOL>tempes=[]<EOL>names=[]<EOL>rankings=[]<EOL>pool = Pool(processes=self._processes)<EOL>for name, ser in df.iteritems():<EOL><INDENT>dat = ser.sort_values(ascending=self.ascending)<EOL>rankings.append(dat)<EOL>names.append(name)<EOL>genes_sorted, cor_vec = dat.index.values, dat.values<EOL>rs = np.random.RandomState(self.seed)<EOL>tempes.append(pool.apply_async(enrichment_score_tensor,<EOL>args=(genes_sorted, cor_vec, gmt,<EOL>self.weighted_score_type,<EOL>self.permutation_num, rs, True,<EOL>self.scale)))<EOL><DEDENT>pool.close()<EOL>pool.join()<EOL>for i, temp in enumerate(tempes):<EOL><INDENT>name, rnk = names[i], rankings[i]<EOL>self._logger.info("<STR_LIT>"%name)<EOL>es, esnull, hit_ind, RES = temp.get()<EOL>self.outdir= os.path.join(outdir, str(name))<EOL>mkdirs(self.outdir)<EOL>self.resultsOnSamples[name] = pd.Series(data=es, index=subsets, name=name)<EOL>if self._noplot: continue<EOL>self._logger.info("<STR_LIT>" % name)<EOL>for i, term in enumerate(subsets):<EOL><INDENT>term = term.replace('<STR_LIT:/>','<STR_LIT:_>').replace("<STR_LIT::>","<STR_LIT:_>")<EOL>outfile = '<STR_LIT>'.format(self.outdir, term, self.module, self.format)<EOL>gseaplot(rank_metric=rnk, term=term, <EOL>hits_indices=hit_ind[i], nes=es[i], pval=<NUM_LIT:1>, fdr=<NUM_LIT:1>, <EOL>RES=RES[i], pheno_pos='<STR_LIT>', pheno_neg='<STR_LIT>', <EOL>figsize=self.figsize, ofname=outfile)<EOL><DEDENT><DEDENT>self._save(outdir)<EOL>return<EOL>

Single Sample GSEA workflow.
           multiprocessing utility on samples.


<EOL>samplesRawES = pd.DataFrame(self.resultsOnSamples)<EOL>samplesRawES.index.name = '<STR_LIT>'<EOL>samplesNES = samplesRawES / (samplesRawES.values.max() - samplesRawES.values.min())<EOL>samplesNES = samplesNES.copy()<EOL>samplesNES.index.rename('<STR_LIT>', inplace=True)<EOL>self.res2d = samplesNES<EOL>self._logger.info("<STR_LIT>")<EOL>if self._outdir is None: return<EOL>outESfile = os.path.join(outdir, "<STR_LIT>")<EOL>with open(outESfile, '<STR_LIT:a>') as f:<EOL><INDENT>if self.scale:<EOL><INDENT>f.write('<STR_LIT>')<EOL>f.write('<STR_LIT>' +'<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>f.write('<STR_LIT>')<EOL>f.write('<STR_LIT>')<EOL><DEDENT>samplesRawES.to_csv(f, sep='<STR_LIT:\t>')<EOL><DEDENT>outNESfile = os.path.join(outdir, "<STR_LIT>")<EOL>with open(outNESfile, '<STR_LIT:a>') as f:<EOL><INDENT>f.write('<STR_LIT>')<EOL>f.write('<STR_LIT>')<EOL>samplesNES.to_csv(f, sep='<STR_LIT:\t>')<EOL><DEDENT>return<EOL>


assert self.min_size <= self.max_size<EOL>assert self.fignum > <NUM_LIT:0><EOL>import glob<EOL>from bs4 import BeautifulSoup<EOL>try:<EOL><INDENT>results_path = glob.glob(self.indir+'<STR_LIT>')[<NUM_LIT:0>]<EOL>rank_path =  glob.glob(self.indir+'<STR_LIT>')[<NUM_LIT:0>]<EOL>gene_set_path =  glob.glob(self.indir+'<STR_LIT>')[<NUM_LIT:0>]<EOL><DEDENT>except IndexError as e:<EOL><INDENT>sys.stderr.write("<STR_LIT>")<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>cls_path = glob.glob(self.indir+'<STR_LIT>')<EOL>if cls_path:<EOL><INDENT>pos, neg, classes = gsea_cls_parser(cls_path[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>pos, neg = '<STR_LIT>','<STR_LIT>'<EOL><DEDENT>self.gene_sets=gene_set_path<EOL>gene_set_dict = self.parse_gmt(gmt=gene_set_path)<EOL>rank_metric = self._load_ranking(rank_path)<EOL>correl_vector =  rank_metric.values<EOL>gene_list = rank_metric.index.values<EOL>database = BeautifulSoup(open(results_path), features='<STR_LIT>')<EOL>length = len(database.findAll('<STR_LIT>'))<EOL>fig_num = self.fignum if self.fignum <= length else length<EOL>for idx in range(fig_num):<EOL><INDENT>enrich_term, hit_ind, nes, pval, fdr= gsea_edb_parser(results_path, index=idx)<EOL>gene_set = gene_set_dict.get(enrich_term)<EOL>RES = enrichment_score(gene_list=gene_list, <EOL>correl_vector=correl_vector,<EOL>gene_set=gene_set, <EOL>weighted_score_type=self.weighted_score_type,<EOL>nperm=<NUM_LIT:0>)[-<NUM_LIT:1>]<EOL>term = enrich_term.replace('<STR_LIT:/>','<STR_LIT:_>').replace("<STR_LIT::>","<STR_LIT:_>")<EOL>outfile = '<STR_LIT>'.format(self.outdir, term, self.module, self.format)<EOL>gseaplot(rank_metric=rank_metric, term=enrich_term, <EOL>hits_indices=hit_ind, nes=nes, pval=pval, fdr=fdr, <EOL>RES=RES, pheno_pos=pos, pheno_neg=neg, <EOL>figsize=self.figsize, ofname=outfile)<EOL><DEDENT>self._logger.info("<STR_LIT>")<EOL>


<EOL>argparser = prepare_argparser()<EOL>args = argparser.parse_args()<EOL>subcommand = args.subcommand_name<EOL>if subcommand == "<STR_LIT>":<EOL><INDENT>from .gsea import Replot<EOL>rep = Replot(indir=args.indir, outdir=args.outdir, weighted_score_type=args.weight,<EOL>figsize=args.figsize, graph_num=args.graph,<EOL>format=args.format, verbose=args.verbose)<EOL>rep.run()<EOL><DEDENT>elif subcommand == "<STR_LIT>":<EOL><INDENT>from .gsea import GSEA<EOL>gs = GSEA(args.data, args.gmt, args.cls, args.outdir,<EOL>args.mins, args.maxs, args.n, args.weight,<EOL>args.type, args.method, args.ascending, args.threads,<EOL>args.figsize, args.format, args.graph, args.noplot, args.seed, args.verbose)<EOL>gs.run()<EOL><DEDENT>elif subcommand == "<STR_LIT>":<EOL><INDENT>from .gsea import Prerank<EOL>pre = Prerank(args.rnk, args.gmt, args.outdir, args.label[<NUM_LIT:0>], args.label[<NUM_LIT:1>],<EOL>args.mins, args.maxs, args.n, args.weight, args.ascending, args.threads,<EOL>args.figsize, args.format, args.graph, args.noplot, args.seed, args.verbose)<EOL>pre.run()<EOL><DEDENT>elif subcommand == "<STR_LIT>":<EOL><INDENT>from .gsea import SingleSampleGSEA<EOL>ss = SingleSampleGSEA(data=args.data, gene_sets=args.gmt, outdir=args.outdir,<EOL>sample_norm_method=args.norm,<EOL>min_size=args.mins, max_size=args.maxs, permutation_num=args.n,<EOL>weighted_score_type=args.weight, scale=args.scale,<EOL>ascending=args.ascending, processes=args.threads,<EOL>figsize=args.figsize, format=args.format, graph_num=args.graph,<EOL>no_plot=args.noplot, seed=args.seed, verbose=args.verbose)<EOL>ss.run()<EOL><DEDENT>elif subcommand == "<STR_LIT>":<EOL><INDENT>from .enrichr import Enrichr<EOL>enr = Enrichr(gene_list=args.gene_list, descriptions=args.descrip,<EOL>gene_sets=args.library, organism=args.organism,<EOL>outdir=args.outdir, format=args.format, cutoff=args.thresh, <EOL>background=args.bg, figsize=args.figsize,<EOL>top_term=args.term, no_plot=args.noplot, verbose=args.verbose)<EOL>enr.run()<EOL><DEDENT>elif subcommand == "<STR_LIT>":<EOL><INDENT>from .parser import Biomart<EOL>name, value = args.filter<EOL>if os.path.isfile(value):<EOL><INDENT>with open(value, '<STR_LIT:r>') as val:<EOL><INDENT>lines = val.readlines()<EOL><DEDENT>value = [ l.strip() for l in lines]<EOL><DEDENT>bm = Biomart(host=args.host, verbose=args.verbose)<EOL>bm.query(dataset=args.bg, attributes=args.attrs.split("<STR_LIT:U+002C>"), <EOL>filters={name : value}, filename=args.ofile)<EOL><DEDENT>else:<EOL><INDENT>argparser.print_help()<EOL>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>


description = "<STR_LIT>"<EOL>epilog = "<STR_LIT>"<EOL>argparser = ap.ArgumentParser(description=description, epilog=epilog)<EOL>argparser.add_argument("<STR_LIT>", action="<STR_LIT:version>", version="<STR_LIT>"+ __version__)<EOL>subparsers = argparser.add_subparsers(dest='<STR_LIT>') <EOL>add_gsea_parser(subparsers)<EOL>add_prerank_parser(subparsers)<EOL>add_singlesample_parser(subparsers)<EOL>add_plot_parser(subparsers)<EOL>add_enrichr_parser(subparsers)<EOL>add_biomart_parser(subparsers)<EOL>return argparser<EOL>

Prepare argparser object. New options will be added in this function first.


parser.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", type=str, default='<STR_LIT>',<EOL>metavar='<STR_LIT>', action="<STR_LIT:store>",<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", type=str, metavar='<STR_LIT>', action="<STR_LIT:store>",<EOL>choices=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"), default="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', nargs=<NUM_LIT:2>, dest='<STR_LIT>',<EOL>metavar=('<STR_LIT:width>', '<STR_LIT>'),type=float, default=(<NUM_LIT>, <NUM_LIT:6>),<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:20>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", action='<STR_LIT:store_true>', dest='<STR_LIT>', default=False,<EOL>help="<STR_LIT>"+"<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>", default=False, dest='<STR_LIT>',<EOL>help="<STR_LIT>", )<EOL>


output_group = parser.add_mutually_exclusive_group(required=required)<EOL>output_group.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", type=str, default='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>output_group.add_argument("<STR_LIT>", dest="<STR_LIT>", type=str, default='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>


argparser_gsea = subparsers.add_parser("<STR_LIT>", help="<STR_LIT>")<EOL>group_input = argparser_gsea.add_argument_group("<STR_LIT>")<EOL>group_input.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:data>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>group_input.add_argument("<STR_LIT:-c>", "<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>group_input.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>group_input.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT:type>", type=str, metavar='<STR_LIT>',<EOL>choices=("<STR_LIT>", "<STR_LIT>"), default="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>group_output = argparser_gsea.add_argument_group("<STR_LIT>")<EOL>add_output_option(group_output)<EOL>group_opt = argparser_gsea.add_argument_group("<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT:n>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:1000>, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>",  dest="<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:15>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', dest='<STR_LIT>', default=<NUM_LIT:1.0>, type=float, metavar='<STR_LIT:float>',<EOL>help='<STR_LIT>',)<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, metavar='<STR_LIT>',<EOL>choices=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"),<EOL>default="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store_true>', dest='<STR_LIT>', default=False,<EOL>help='<STR_LIT>')<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=None, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:1>, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>return<EOL>

Add main function 'gsea' argument parsers.


argparser_prerank = subparsers.add_parser("<STR_LIT>", help="<STR_LIT>")<EOL>prerank_input = argparser_prerank.add_argument_group("<STR_LIT>")<EOL>prerank_input.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>prerank_input.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>prerank_input.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', nargs=<NUM_LIT:2>, dest='<STR_LIT:label>',<EOL>metavar=('<STR_LIT>', '<STR_LIT>'), type=str, default=('<STR_LIT>','<STR_LIT>'),<EOL>help="<STR_LIT>")<EOL>prerank_output = argparser_prerank.add_argument_group("<STR_LIT>")<EOL>add_output_option(prerank_output)<EOL>prerank_opt = argparser_prerank.add_argument_group("<STR_LIT>")<EOL>prerank_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT:n>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:1000>, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>prerank_opt.add_argument("<STR_LIT>",  dest="<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:15>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>prerank_opt.add_argument("<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>prerank_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', dest='<STR_LIT>', default=<NUM_LIT:1.0>, type=float, metavar='<STR_LIT:float>',<EOL>help='<STR_LIT>',)<EOL>prerank_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store_true>', dest='<STR_LIT>', default=False,<EOL>help='<STR_LIT>')<EOL>prerank_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=None, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>prerank_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:1>, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>return<EOL>

Add function 'prerank' argument parsers.


argparser_gsea = subparsers.add_parser("<STR_LIT>", help="<STR_LIT>")<EOL>group_input = argparser_gsea.add_argument_group("<STR_LIT>")<EOL>group_input.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:data>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>group_input.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=str, required=True,<EOL>help="<STR_LIT>")<EOL>group_output = argparser_gsea.add_argument_group("<STR_LIT>")<EOL>add_output_option(group_output)<EOL>group_opt = argparser_gsea.add_argument_group("<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=str,<EOL>default='<STR_LIT>', metavar='<STR_LIT>',<EOL>choices=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"),<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT>', dest='<STR_LIT>', default=True,<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT:n>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:0>, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:15>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT>,metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', dest='<STR_LIT>', default=<NUM_LIT>, type=float, metavar='<STR_LIT>',<EOL>help='<STR_LIT>',)<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store_true>', dest='<STR_LIT>', default=False,<EOL>help='<STR_LIT>')<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=None, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>group_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest = "<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:1>, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>return<EOL>

Add function 'singlesample' argument parsers.


argparser_replot = subparsers.add_parser("<STR_LIT>", help="<STR_LIT>")<EOL>group_replot = argparser_replot.add_argument_group("<STR_LIT>")<EOL>group_replot.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", required=True, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>add_output_option(group_replot)<EOL>group_replot.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', dest='<STR_LIT>', default=<NUM_LIT:1.0>, type=float, metavar='<STR_LIT:float>',<EOL>help='<STR_LIT>',)<EOL>return<EOL>


argparser_enrichr = subparsers.add_parser("<STR_LIT>", help="<STR_LIT>")<EOL>enrichr_opt = argparser_enrichr.add_argument_group("<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, required=True, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, required=True, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, default='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, default='<STR_LIT>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", metavar='<STR_LIT:float>', type=float, default=<NUM_LIT>,<EOL>help="<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", default='<STR_LIT>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>enrichr_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", action="<STR_LIT:store>", type=int, default=<NUM_LIT:10>, metavar='<STR_LIT:int>',<EOL>help="<STR_LIT>")<EOL>enrichr_output = argparser_enrichr.add_argument_group("<STR_LIT>")<EOL>add_output_option(enrichr_output)<EOL>return<EOL>

Add function 'enrichr' argument parsers.


argparser_biomart = subparsers.add_parser("<STR_LIT>", help="<STR_LIT>")<EOL>biomart_opt = argparser_biomart.add_argument_group("<STR_LIT>")<EOL>biomart_opt.add_argument("<STR_LIT>", "<STR_LIT>", action='<STR_LIT:store>', nargs=<NUM_LIT:2>, dest='<STR_LIT>',<EOL>required=True, metavar=('<STR_LIT>', '<STR_LIT>'),<EOL>help="""<STR_LIT>""")                             <EOL>biomart_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, required=True, metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>biomart_opt.add_argument("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", type=str, required=True, help="<STR_LIT>")                               <EOL>biomart_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, default='<STR_LIT>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>biomart_opt.add_argument("<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT:host>", type=str, default='<STR_LIT>', metavar='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>biomart_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store>", dest="<STR_LIT>", type=str, metavar='<STR_LIT>',<EOL>default="<STR_LIT>", help="<STR_LIT>")<EOL>biomart_opt.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>", default=False, dest='<STR_LIT>',<EOL>help="<STR_LIT>", )<EOL>

Add function 'biomart' argument parsers.


def calc_pvalues(query, gene_sets, background=<NUM_LIT>, **kwargs):

<EOL>k = len(query) <EOL>query = set(query)<EOL>vals = []<EOL>if isinstance(background, set): <EOL><INDENT>bg = len(background) <EOL>query = query.intersection(background)<EOL><DEDENT>elif isinstance(background, int):<EOL><INDENT>bg = background<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>subsets = sorted(gene_sets.keys())<EOL>for s in subsets:<EOL><INDENT>category = gene_sets.get(s)<EOL>m = len(category)<EOL>hits = query.intersection(set(category))<EOL>x = len(hits)<EOL>if x < <NUM_LIT:1> : continue<EOL>vals.append((s, hypergeom.sf(x-<NUM_LIT:1>, bg, m, k), x, m, hits))<EOL><DEDENT>return zip(*vals)<EOL>

calculate pvalues for all categories in the graph

    :param set query: set of identifiers for which the p value is calculated
    :param dict gene_sets: gmt file dict after background was set
    :param set background: total number of genes in your annotated database.
    :returns: pvalues
              x: overlapped gene number
              n: length of gene_set which belongs to each terms
              hits: overlapped gene names.

    For 2*2 contingency table:
    =============================================================================
                         |   in  query  |  not in query |    row total
    =>      in gene_set  |        a     |       b       |       a+b
    =>  not in gene_set  |        c     |       d       |       c+d
           column total                                 | a+b+c+d = anno database
    =============================================================================
    background genes number = a + b + c + d.

    Then, in R
        x=a     the number of white balls drawn without replacement
                from an urn which contains both black and white balls.
        m=a+b   the number of white balls in the urn
        n=c+d   the number of black balls in the urn
        k=a+c   the number of balls drawn from the urn

    In Scipy:
    for args in scipy.hypergeom.sf(k, M, n, N, loc=0):
        M: the total number of objects,
        n: the total number of Type I objects.
        k: the random variate represents the number of Type I objects in N drawn
           without replacement from the total population.

    Therefore, these two functions are the same when using parameters from 2*2 table:
    R:     >   phyper(x-1, m, n, k, lower.tail=FALSE)
    Scipy: >>> hypergeom.sf(x-1, m+n, m, k)


<EOL>pvals = np.asarray(pvals)<EOL>pvals_sortind = np.argsort(pvals)<EOL>pvals_sorted = np.take(pvals, pvals_sortind)<EOL>ecdffactor = _ecdf(pvals_sorted)<EOL>reject = pvals_sorted <= ecdffactor*alpha<EOL>if reject.any():<EOL><INDENT>rejectmax = max(np.nonzero(reject)[<NUM_LIT:0>])<EOL>reject[:rejectmax] = True<EOL><DEDENT>pvals_corrected_raw = pvals_sorted / ecdffactor<EOL>pvals_corrected = np.minimum.accumulate(pvals_corrected_raw[::-<NUM_LIT:1>])[::-<NUM_LIT:1>]<EOL>del pvals_corrected_raw<EOL>pvals_corrected[pvals_corrected><NUM_LIT:1>] = <NUM_LIT:1><EOL>pvals_corrected_ = np.empty_like(pvals_corrected)<EOL>pvals_corrected_[pvals_sortind] = pvals_corrected<EOL>del pvals_corrected<EOL>reject_ = np.empty_like(reject)<EOL>reject_[pvals_sortind] = reject<EOL>return reject_, pvals_corrected_<EOL>

benjamini hocheberg fdr correction. inspired by statsmodels


seen = set()<EOL>seen_add = seen.add<EOL>"""<STR_LIT>"""<EOL>return [x for x in seq if x not in seen and not seen_add(x)]<EOL>

Remove duplicates from a list in Python while preserving order.

    :param seq: a python list object.
    :return: a list without duplicates while preserving order.


<EOL>logging.getLogger("<STR_LIT>").handlers = []<EOL>logging.basicConfig(level    = logging.DEBUG,<EOL>format   = '<STR_LIT>',<EOL>filename = outlog,<EOL>filemode = '<STR_LIT:w>')<EOL>console = logging.StreamHandler()<EOL>console.setLevel(log_level)<EOL>formatter = logging.Formatter('<STR_LIT>')<EOL>console.setFormatter(formatter)<EOL>logging.getLogger("<STR_LIT>").addHandler(console)<EOL>logger = logging.getLogger("<STR_LIT>")<EOL>return logger<EOL>


handlers = logger.handlers[:]<EOL>for handler in handlers:<EOL><INDENT>handler.close()<EOL>logger.removeHandler(handler)<EOL><DEDENT>


s = requests.Session()<EOL>retries = Retry(total=num, backoff_factor=<NUM_LIT:0.1>,<EOL>status_forcelist=[<NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>])<EOL>s.mount('<STR_LIT>', HTTPAdapter(max_retries=retries))<EOL>return s<EOL>

retry connection.

        define max tries num
        if the backoff_factor is 0.1, then sleep() will sleep for
        [0.1s, 0.2s, 0.4s, ...] between retries.
        It will also force a retry if the status code returned is 500, 502, 503 or 504.


def __init__(self, offensive=None, path=None, equal=False,<EOL>lang=None, include=False):

<EOL>self.files = files = []<EOL>count = <NUM_LIT:0><EOL>for fortune in load_all(offensive, path, lang, include):<EOL><INDENT>count += <NUM_LIT:1> if equal else fortune.size<EOL>files.append((fortune, count))<EOL><DEDENT>self.count = count<EOL>self.keys = [i[<NUM_LIT:1>] for i in self.files]<EOL>

Initialize based on all available fortune files


@classmethod<EOL><INDENT>def fromlist(cls, files, equal=False, offensive=False, lang=None):<DEDENT>

self = cls.__new__(cls)<EOL>self.files = fortunes = []<EOL>count = <NUM_LIT:0><EOL>for file in files:<EOL><INDENT>fortune = load_fortune(file, offensive=offensive, lang=lang)<EOL>if fortune is None:<EOL><INDENT>logger.warn("<STR_LIT>", file)<EOL>continue<EOL><DEDENT>count += <NUM_LIT:1> if equal else fortune.size<EOL>fortunes.append((fortune, count))<EOL><DEDENT>if not fortunes:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.count = count<EOL>self.keys = [i[<NUM_LIT:1>] for i in self.files]<EOL>return self<EOL>

Initialize based on a list of fortune files


@classmethod<EOL><INDENT>def set_chance(cls, files, equal=False, offensive=False, lang=None): <DEDENT>

self = cls.__new__(cls)<EOL>total = <NUM_LIT:0.><EOL>file = []<EOL>leftover = []<EOL>for name, chance in files:<EOL><INDENT>if total >= <NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT>fortune = load_fortune(name, offensive=offensive, lang=lang)<EOL>if fortune is None or not fortune.size:<EOL><INDENT>continue<EOL><DEDENT>if chance:<EOL><INDENT>file.append((fortune, chance))<EOL>total += chance<EOL><DEDENT>else:<EOL><INDENT>leftover.append(fortune)<EOL><DEDENT><DEDENT>if leftover and total < <NUM_LIT:1>:<EOL><INDENT>left = <NUM_LIT:1> - total<EOL>if equal:<EOL><INDENT>perfile = left / len(leftover)<EOL>for fortune in leftover:<EOL><INDENT>file.append((fortune, perfile))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>entries = sum(map(attrgetter('<STR_LIT:size>'), leftover))<EOL>logger.debug('<STR_LIT>', entries)<EOL>for fortune in leftover:<EOL><INDENT>chance = left * fortune.size / entries<EOL>file.append((fortune, chance))<EOL><DEDENT><DEDENT><DEDENT>self.count = count = <NUM_LIT><EOL>bound = <NUM_LIT:0><EOL>self.files = fortunes = []<EOL>for file, chance in file:<EOL><INDENT>bound += int(chance * count)<EOL>fortunes.append((file, bound))<EOL><DEDENT>self.keys = [i[<NUM_LIT:1>] for i in self.files]<EOL>return self<EOL>

Initialize based on a list of fortune files with set chances


if isinstance(package_or_set_of_packages, types.ModuleType):<EOL><INDENT>packages = [package_or_set_of_packages]<EOL><DEDENT>elif isinstance(package_or_set_of_packages, Iterable) and not isinstance(package_or_set_of_packages, (dict, str)):<EOL><INDENT>packages = package_or_set_of_packages<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>imported = packages.copy()<EOL>for package in packages:<EOL><INDENT>if not hasattr(package, '<STR_LIT>'):<EOL><INDENT>raise Exception(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>for module_finder, name, ispkg in pkgutil.walk_packages(package.__path__):<EOL><INDENT>module_name = '<STR_LIT>'.format(package.__name__, name)<EOL>current_module = importlib.import_module(module_name)<EOL>imported.append(current_module)<EOL>if ispkg:<EOL><INDENT>imported += load_all_modules_in_packages(current_module)<EOL><DEDENT><DEDENT><DEDENT>for module in imported:<EOL><INDENT>dir(module)<EOL><DEDENT>return list(<EOL>{<EOL>module.__name__: module<EOL>for module in imported<EOL>}.values()<EOL>)<EOL>

Recursively loads all modules from a package object, or set of package objects

:param package_or_set_of_packages: package object, or iterable of package objects
:return: list of all unique modules discovered by the function


return _discover_objects(packages, base_classes, True)<EOL>

Find objects in a set of packages that are class definitions that are subclasses of a set of base classes.

:param packages: List of package references to the packages you're interested in.
:param base_classes: List of classes that you want to find subclasses for.
:return: List of objects mapping to the discovered subclasses


return _discover_objects(packages, base_classes, False)<EOL>

Find objects in a set of packages that are instances of a set of base classes, or any subclasses.

:param packages: List of package references to the packages you're interested in.
:param base_classes: List of classes that you want to find subclasses for.
:return: List of all discovered instances


usage = "<STR_LIT>"<EOL>description = "<STR_LIT>"<EOL>argparser = argparse.ArgumentParser(<EOL>usage=usage, description=description)<EOL>argparser.add_argument(<EOL>'<STR_LIT>', type=argparse.FileType('<STR_LIT:r>'),<EOL>help="<STR_LIT>")<EOL>argparser.add_argument(<EOL>'<STR_LIT>', nargs='<STR_LIT:?>', type=argparse.FileType('<STR_LIT:w>'),<EOL>default=sys.stdout, help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>argparser.add_argument(<EOL>'<STR_LIT>', nargs="<STR_LIT:?>", const=str, help="<STR_LIT>")<EOL>args = argparser.parse_args()<EOL>return args<EOL>

Parses command line args using argparse library


weight_fn = "<STR_LIT>"<EOL>return patch(weight_fn, lambda self, x: x)(patch("<STR_LIT>",<EOL>lambda: <NUM_LIT:1.0>)(f))<EOL>

Decorator that patches ExponentiallyDecayingSample class such that the
timestamp->priority function is a no-op.


self._count += value<EOL>

Increment the counter. By default it will increment by 1.

        :param value: value to increment the counter.


self._count -= value<EOL>

Decrement the counter. By default it will decrement by 1.

        :param value: value to decrement the counter.


return self._count.value<EOL>

Return the current value of the counter.


if duration >= <NUM_LIT:0>:<EOL><INDENT>self.meter.mark()<EOL>self.histogram.update(duration)<EOL><DEDENT>


return self.histogram.total<EOL>


return self.histogram.count<EOL>

Returns the number of measurements that have been made.


return self.meter.one_minute_rate<EOL>


return self.meter.five_minute_rate<EOL>


return self.meter.fifteen_minute_rate<EOL>

Returns the fifteen-minute average rate.


return self.meter.mean_rate<EOL>

Returns the mean rate of the events since the start of the process.


return self.histogram.min<EOL>

Returns the minimum amount of time spent in the operation.


return self.histogram.max<EOL>

Returns the maximum amount of time spent in the operation.


return self.histogram.mean<EOL>

Returns the mean time spent in the operation.


return self.histogram.stddev<EOL>

Returns the standard deviation of the mean spent in the operation.


return self.duration_meter.one_minute_rate<EOL>

Returns the one-minute average utilization as a percentage.


return self.duration_meter.five_minute_rate<EOL>

Returns the five-minute average utilization as a percentage.


return self.duration_meter.fifteen_minute_rate<EOL>

Returns the fifteen-minute average utilization as a percentage.


return self.duration_meter.mean_rate<EOL>

Returns the mean (average) utilization as a percentage since the
process started.


return self.counter.value<EOL>

Returns the total number of events that have been recorded.


self.counter += value<EOL>self.m1_rate.update(value)<EOL>self.m5_rate.update(value)<EOL>self.m15_rate.update(value)<EOL>

Record an event with the meter. By default it will record one event.

        :param value: number of event to record


return self.m1_rate.rate<EOL>


return self.m5_rate.rate<EOL>


return self.m15_rate.rate<EOL>


if self.counter.value == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>else:<EOL><INDENT>elapsed = time() - self.start_time<EOL>return self.counter.value / elapsed<EOL><DEDENT>


return self.counter.value<EOL>


return self.sum.value<EOL>


if self.counter.value > <NUM_LIT:0>:<EOL><INDENT>return self.sum.value / self.counter.value<EOL><DEDENT>return <NUM_LIT:0.0><EOL>


if self.counter.value > <NUM_LIT:0>:<EOL><INDENT>return self.variance ** <NUM_LIT><EOL><DEDENT>return <NUM_LIT:0.0><EOL>


if self.counter.value <= <NUM_LIT:1>:<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>return self.var.value[<NUM_LIT:1>] / (self.counter.value - <NUM_LIT:1>)<EOL>


raise NotImplementedError<EOL>

Returns True if what is being checked is healthy


last = self.last.get_and_set(value)<EOL>if last <= value:<EOL><INDENT>value = value - last<EOL><DEDENT>super(Derive, self).mark(value)<EOL>

Record an event with the derive.

        :param value: counter value to record


if sys.version_info[<NUM_LIT:0>] > <NUM_LIT:2>:<EOL><INDENT>return [i for i in map(func, iterable)]<EOL><DEDENT>else:<EOL><INDENT>return map(func, iterable)<EOL><DEDENT>

Wrapper to make map() behave the same on Py2 and Py3.


def serialize_metric(self, metric, m_name, keys, m_type):

return [<EOL>self.format_metric_string(m_name, getattr(metric, key), m_type)<EOL>for key in keys<EOL>]<EOL>

Serialize and send available measures of a metric.


def format_metric_string(self, name, value, m_type):

<EOL>template = '<STR_LIT>'<EOL>if self.prefix:<EOL><INDENT>name = "<STR_LIT>".format(prefix=self.prefix, m_name=name)<EOL><DEDENT>return template.format(name=name, value=value, m_type=m_type)<EOL>

Compose a statsd compatible string for a metric's measurement.


self.batch_count += <NUM_LIT:1><EOL>self.batch_buffer += metric_str<EOL>if self.batch_count >= self.batch_size:<EOL><INDENT>self._send()<EOL><DEDENT>


def set_group(self, metric_name, group_name):

self.groups[metric_name] = group_name<EOL>

Override the group name for certain metrics.


def wrapped(*args, **kwargs):<EOL><INDENT>check_name = func.__name__<EOL>arg_name = None<EOL>if args:<EOL><INDENT>arg_name = args[<NUM_LIT:0>]<EOL><DEDENT>try:<EOL><INDENT>if arg_name:<EOL><INDENT>logger.debug("<STR_LIT>", check_name, arg_name)<EOL><DEDENT>else:<EOL><INDENT>logger.debug("<STR_LIT>", check_name)<EOL><DEDENT>response = func(*args, **kwargs)<EOL><DEDENT>except Exception as e:<EOL><INDENT>message = str(e)<EOL>response = {<EOL>"<STR_LIT>": False,<EOL>"<STR_LIT:error>": message,<EOL>"<STR_LIT>": traceback.format_exc(),<EOL>}<EOL>if arg_name:<EOL><INDENT>response = {arg_name: response}<EOL>logger.exception(<EOL>"<STR_LIT>",<EOL>check_name,<EOL>arg_name,<EOL>message<EOL>)<EOL><DEDENT>else:<EOL><INDENT>logger.exception(<EOL>"<STR_LIT>",<EOL>check_name,<EOL>message<EOL>)<EOL><DEDENT><DEDENT>return response<EOL><DEDENT>return wrapped<EOL>

Decorator which wraps checks and returns an error response on failure.


def _parse_auth_header(auth_header):<EOL><INDENT>"""<STR_LIT>"""<EOL>reg = re.compile('<STR_LIT>')<EOL>header_dict = dict(reg.findall(auth_header))<EOL>return header_dict['<STR_LIT>']<EOL><DEDENT>def _get_passed_token(request):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>auth_header = request.META['<STR_LIT>']<EOL>token = _parse_auth_header(auth_header)<EOL><DEDENT>except KeyError:<EOL><INDENT>token = request.GET.get(settings.WATCHMAN_TOKEN_NAME)<EOL><DEDENT>return token<EOL><DEDENT>def _validate_token(request):<EOL><INDENT>if settings.WATCHMAN_TOKENS:<EOL><INDENT>watchman_tokens = settings.WATCHMAN_TOKENS.split('<STR_LIT:U+002C>')<EOL><DEDENT>elif settings.WATCHMAN_TOKEN:<EOL><INDENT>watchman_tokens = [settings.WATCHMAN_TOKEN, ]<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>return _get_passed_token(request) in watchman_tokens<EOL><DEDENT>@csrf_exempt<EOL>@wraps(view_func)<EOL>def _wrapped_view(request, *args, **kwargs):<EOL><INDENT>if _validate_token(request):<EOL><INDENT>return view_func(request, *args, **kwargs)<EOL><DEDENT>return HttpResponseForbidden()<EOL><DEDENT>return _wrapped_view<EOL>

Decorator which ensures that one of the WATCHMAN_TOKENS is provided if set.

WATCHMAN_TOKEN_NAME can also be set if the token GET parameter must be
customized.


def __init__(self, *args, url=None, timeout=None, sslopt=None,<EOL>http_proxy_host=None, http_proxy_port=None, http_proxy_auth=None, http_no_proxy=None,<EOL>reconnect_interval=None, log_level=None, **kwargs):

<EOL>self.q = Queue()<EOL>self.socket = None<EOL>self.url = url if url else '<STR_LIT>'<EOL>self.sslopt = sslopt if sslopt else {}<EOL>self.http_proxy_host = http_proxy_host<EOL>self.http_proxy_port = http_proxy_port<EOL>self.http_proxy_auth = http_proxy_auth<EOL>self.http_no_proxy = http_no_proxy<EOL>self.channel_configs = OrderedDict()<EOL>self.connected = Event()<EOL>self.disconnect_called = Event()<EOL>self.reconnect_required = Event()<EOL>self.reconnect_interval = reconnect_interval if reconnect_interval else <NUM_LIT:10><EOL>self.paused = Event()<EOL>self.ping_timer = None<EOL>self.ping_interval = <NUM_LIT><EOL>self.connection_timer = None<EOL>self.connection_timeout = timeout if timeout else <NUM_LIT:10><EOL>self.pong_timer = None<EOL>self.pong_received = False<EOL>self.pong_timeout = <NUM_LIT:30><EOL>self.log = logging.getLogger(self.__module__)<EOL>if log_level == logging.DEBUG:<EOL><INDENT>websocket.enableTrace(True)<EOL><DEDENT>self.log.setLevel(level=log_level if log_level else logging.INFO)<EOL>Thread.__init__(self)<EOL>self.daemon = True<EOL>self.bitfinex_config = None<EOL>

Initialize a WebSocketConnection Instance.

        :param data_q: Queue(), connection to the Client Class
        :param args: args for Thread.__init__()
        :param url: websocket address, defaults to v2 websocket.
        :param http_proxy_host: proxy host name.
        :param http_proxy_port: http proxy port. If not set, set to 80.
        :param http_proxy_auth: http proxy auth information.
                                tuple of username and password.
        :param http_no_proxy: host names, which doesn't use proxy.
        :param timeout: timeout for connection; defaults to 10s
        :param reconnect_interval: interval at which to try reconnecting;
                                   defaults to 10s.
        :param log_level: logging level for the connection Logger. Defaults to
                          logging.INFO.
        :param kwargs: kwargs for Thread.__ini__()


self.log.debug("<STR_LIT>")<EOL>self.reconnect_required.clear()<EOL>self.disconnect_called.set()<EOL>if self.socket:<EOL><INDENT>self.socket.close()<EOL><DEDENT>self.join(timeout=<NUM_LIT:1>)<EOL>

Disconnects from the websocket connection and joins the Thread.

        :return: