Package: gsea-api
Pandas API for Gene Set Enrichment Analysis in Python (GSEApy, cudaGSEA,
GSEA)aims to provide a unified API for various GSEA implementations; uses
pandas DataFrames and a hierarchy of Pythonic classes.file exports (exporting input for GSEA) use low-level numpy functions
and are much faster than in pandasaims to allow researchers to easily compare different implementations
of GSEA, and to integrate those in projects which require
high-performance GSEA (e.g. massive screening for drug-repositioning)provides useful utilities for work with GMT files, or gene sets and
pathways in general in PythonInstallationTo install the API use:pip3installgsea_apiSeebelowfor the instructions on
installation of specific GSEA implementations.Example usagefrompandasimportread_tablefromgsea_api.expression_setimportExpressionSetfromgsea_api.gseaimportGSEADesktopfromgsea_api.molecular_signatures_dbimportGeneSetsreactome_pathways=GeneSets.from_gmt('ReactomePathways.gmt')gsea=GSEADesktop()design=['Disease','Disease','Disease','Control','Control','Control']matrix=read_table('expression_data.tsv',index_col='Gene')result=gsea.run(# note: contrast() is not necessary in this simple caseExpressionSet(matrix,design).contrast('Disease','Control'),reactome_pathways,metric='Signal2Noise',permutations=1000)Whereexpression_data.tsvis in the following format:Gene    Patient_1   Patient_2   Patient_3   Patient_4   Patient_5   Patient_6
TACC2   0.2 0.1 0.4 0.6 0.7 2.1
TP53    2.3 0.2 2.1 2.0 0.3 0.6MSigDB integrationMolecular Signatures
Database(MSigDB)
can be downloaded from theBroad Institute GSEA
website. It provides
expert-curated gene set collections, as well as curated subset of
pathway databases (Reactome, KEGG, Biocarta, Gene Ontology) trimmed to
remove redundant, overlapping and and otherwise little-value terms (if
needed).You can download all the pathways collections at once (search forZIPped MSigDBon the download page). After downloading and
un-zipping (e.g. to a local directory namedmsigdb), you can access
the gene sets from MSigDB with:fromgsea_api.molecular_signatures_dbimportMolecularSignaturesDatabasemsigdb=MolecularSignaturesDatabase('msigdb',version=7.1)msigdb.gene_setsmsigdb.gene_setsreturns a list of dictionaries describing
auto-detected pathways:[{'name':'c1.all','id_type':'symbols'},{'name':'c1.all','id_type':'entrez'},{'name':'c2.cp.reactome','id_type':'symbols'},{'name':'c2.cp.reactome','id_type':'entrez'}# etc..]Information about the location on disk and version are avilable inmsigdb.pathandmsigdb.version.msigdb.loadloads the specific collection into aGeneSetsobject:>kegg_pathways=msigdb.load('c2.cp.kegg','symbols')>print(kegg_pathways)<GeneSets'c2.cp.kegg'with186genesets>This object can be passed to any of the supporteed GSEA implementations;
please see below for a detailed description of theGeneSetsobject.GeneSetsobjectsGeneSetsrepresents a collection of sets of genes, where each set is
represented asGeneSetobject.You can check the number of sets contained within a collection with:>len(kegg_pathways)186The gene sets are accessible withgene_sets(tuple) andgene_sets_by_name(dict) properties:>kegg_pathways.gene_sets[:2](<GeneSet'KEGG_TIGHT_JUNCTION'with132genes>,<GeneSet'KEGG_RNA_DEGRADATION'with59genes>)>kegg_pathways.gene_sets_by_name{'KEGG_TIGHT_JUNCTION':<GeneSet'KEGG_TIGHT_JUNCTION'with132genes>,'KEGG_RNA_DEGRADATION':<GeneSet'KEGG_RNA_DEGRADATION'with59genes># etc.}Subseting collectionsSometimes only a subset of genes is measured in an experiment. You can
remove gene sets which do not contain any of the measured genes from the
collection:>measured_genes={'APOE','CYB5R1','FCER1G','PVR','HK2'}>measured_subset=kegg_pathways.subset(measured_genes)>print(measured_subset)<GeneSetswith12genesets>The skipped gene sets are accessible inmeasured_subset.empty_gene_setsfor inspection.Trimmming collections>kegg_pathways.trim(min_genes=10,max_genes=20)<GeneSetswith21genesets>Prettify namesdefprettify_kegg_name(name):returnname.replace('KEGG_','').replace('_',' ')kegg_pathways_pretty=kegg_pathways.format_names(prettify_kegg_name)kegg_pathways_pretty.gene_sets[:2]# (<GeneSet 'TIGHT JUNCTION' with 132 genes>, <GeneSet 'RNA DEGRADATION' with 59 genes>)Other propertiesOther properties and methods offered byGeneSetsinclude: -all_genes: return a set of all genes which are covered by the gene
sets in the collection -name: the name of the collection -to_frame()return a pandasDataFramedescribing membership of
the genes (gene sets = rows, genes = columns), which can be used for
UpSet visualisation (e.g. withComplexUpset) -to_gmt(path: str)exports the gene set to a GMT (Gene Matrix
Transposed) fileInstalling GSEA implementationsFollowing GSEA implementations are supported:GSEA from Broad InstituteLogin/register onthe official GSEA
websiteand
download thegsea_3.0.jarfile (or a newer version).Provide the location of the downloaded file toGSEADesktop()usinggsea_jar_pathargument, e.g.:gsea=GSEADesktop(gsea_jar_path='downloads/gsea_3.0.jar')GSEApyTo use gsea.py please install it with:pip3 install gseapyUse it with:fromgsea_api.gseaimportGSEApygsea=GSEApy()cudaGSEAPlease clone this fork of cudaGSEA to thirdparty directory and compile
the binary version (using the instructions fromthis
repository):gitclonehttps://github.com/krassowski/cudaGSEAor usethe original version,
which does not implement FDR calculations.Use it with:fromgsea_api.gseaimportcudaGSEA# CPU implementation can be used with use_cpu=Truegsea=cudaGSEA(fdr='full',use_cpu=False)CitationPlease also cite the authors of the wrapped tools that you use.ReferencesThe initial version of this code was written for aMaster thesis
projectat Imperial College London.