Skip to content

Commit

Permalink
Add background option and change API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
choang committed Jul 13, 2023
1 parent 01e6035 commit 819c8ec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
6 changes: 4 additions & 2 deletions gget/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
MUSCLE_GITHUB_LINK = "https://github.com/rcedgar/muscle.git"

# Enrichr API endpoints
POST_ENRICHR_URL = "https://maayanlab.cloud/Enrichr/addList"
GET_ENRICHR_URL = "https://maayanlab.cloud/Enrichr/enrich"
POST_ENRICHR_URL = "https://maayanlab.cloud/speedrichr/api/addList"
GET_ENRICHR_URL = "https://maayanlab.cloud/speedrichr/api/enrich"
POST_BACKGROUND_ID_ENRICHR_URL = "https://maayanlab.cloud/speedrichr/api/addbackground"
GET_BACKGROUND_ENRICHR_URL = "https://maayanlab.cloud/speedrichr/api/backgroundenrich"

# ARCHS4 API endpoints
GENECORR_URL = "https://maayanlab.cloud/matrixapi/coltop"
Expand Down
69 changes: 36 additions & 33 deletions gget/gget_enrichr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
from .gget_info import info

# Constants
from .constants import POST_ENRICHR_URL, GET_ENRICHR_URL
from .constants import POST_ENRICHR_URL, GET_ENRICHR_URL, POST_BACKGROUND_ID_ENRICHR_URL, GET_BACKGROUND_ENRICHR_URL


def enrichr(
genes,
database,
background=None,
ensembl=False,
plot=False,
figsize=(10, 10),
Expand All @@ -42,6 +43,7 @@ def enrichr(
Args:
- genes List of Entrez gene symbols to perform enrichment analysis on, passed as a list of strings, e.g. ['PHF14', 'RBM3', 'MSL1', 'PHF21A'].
Set 'ensembl = True' to input a list of Ensembl gene IDs, e.g. ['ENSG00000106443', 'ENSG00000102317', 'ENSG00000188895'].
- background List of background genes
- database Database to use as reference for the enrichment analysis.
Supported shortcuts (and their default database):
'pathway' (KEGG_2021_Human)
Expand All @@ -50,7 +52,7 @@ def enrichr(
'diseases_drugs' (GWAS_Catalog_2019)
'celltypes' (PanglaoDB_Augmented_2021)
'kinase_interactions' (KEA_2015)
or any database listed under Gene-set Library at: https://maayanlab.cloud/Enrichr/#libraries
or any database listed under Gene-set Library at: https://maayanlab.cloud/Enrichr/#libraries
- ensembl Define as 'True' if 'genes' is a list of Ensembl gene IDs. (Default: False)
- plot True/False whether to provide a graphical overview of the first 15 results. (Default: False)
- figsize (width, height) of plot in inches. (Default: (10,10))
Expand All @@ -68,48 +70,32 @@ def enrichr(
Please note that there might a more appropriate database for your application.
Go to https://maayanlab.cloud/Enrichr/#libraries for a full list of supported databases.
"""
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

if database == "pathway":
database = "KEGG_2021_Human"
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

elif database == "transcription":
database = "ChEA_2016"
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

elif database == "ontology":
database = "GO_Biological_Process_2021"
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

elif database == "diseases_drugs":
database = "GWAS_Catalog_2019"
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

elif database == "celltypes":
database = "PanglaoDB_Augmented_2021"
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

elif database == "kinase_interactions":
database = "KEA_2015"
if verbose:
logging.info(
f"Performing Enichr analysis using database {database}. " + db_message
)

else:
database = database
if verbose:
logging.info(f"Performing Enichr analysis using database {database}.")


# If single gene passed as string, convert to list
if type(genes) == str:
genes = [genes]
Expand Down Expand Up @@ -171,6 +157,19 @@ def enrichr(

r1 = requests.post(POST_ENRICHR_URL, files=args_dict)

# Submit background list to Enrichr API to get background id
if background:
## Submit gene list to Enrichr API
args_dict = {
"backgroundid": (None, background),
}

request_background_id = requests.post(POST_BACKGROUND_ID_ENRICHR_URL, files=args_dict)

# Get background ID
post_results_background= request_background_id.json()
background_list_id = post_results_background["backgroundid"]

if not r1.ok:
raise RuntimeError(
f"Enrichr HTTP POST response status code: {r1.status_code}. "
Expand All @@ -181,19 +180,23 @@ def enrichr(
post_results = r1.json()
userListId = post_results["userListId"]

## Fetch results from Enrichr API
# Build query with user ID
query_string = f"?userListId={userListId}&backgroundType={database}"
if background is None:
query_string = f"?userListId={userListId}&backgroundType={database}"
r2 = requests.get(GET_ENRICHR_URL + query_string)
else:
query_string = f"?userListId={userListId}&backgroundid={background_list_id}&backgroundType={database}"
r2 = requests.get(GET_BACKGROUND_ENRICHR_URL + query_string)

r2 = requests.get(GET_ENRICHR_URL + query_string)
if not r2.ok:
raise RuntimeError(
f"Enrichr HTTP GET response status code: {r2.status_code}. "
"Please double-check arguments and try again.\n"
)


enrichr_results = r2.json()


# Return error if no results were found
if len(enrichr_results) > 1:
logging.error(
Expand Down

0 comments on commit 819c8ec

Please sign in to comment.