Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notebook to fetch Pharos class information for genes in Agora #158

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create a Pharos class input file\n",
"\n",
"This notebook creates a file that contains gene symbols, Ensembl IDs, UniProt IDs, and Pharos class for each gene. The steps are:\n",
"1. Query Pharos for all targets in their database\n",
"2. Parse the JSON response as a data frame\n",
"3. Merge this data with Agora's existing UniProt -> Ensembl ID map file\n",
"\n",
"Step 3 simultaneously maps the IDs given by Pharos (UniProt IDs and gene symbols only) to the corresponding Ensembl IDs, and narrows the data down to only genes that exist in Agora's data files."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"import pandas as pd\n",
"import synapseclient"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Query Pharos for all targets in the database\n",
"\n",
"Pharos uses GraphQL for its API, so the query is formatted as JSON that matches their \"DownloadResult\" schema. The lone \"data\" value inside signifies to return the data only, not any status or metadata about the request."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url = \"https://pharos-api.ncats.io/graphql\"\n",
"\n",
"query_body = \"\"\"\n",
"{\n",
" download(model: \"Targets\", fields: [\"UniProt\", \"Symbol\", \"Target Development Level\"], sqlOnly: false) {\n",
" data\n",
" }\n",
"}\n",
"\"\"\"\n",
"\n",
"response = requests.post(url=url, json={\"query\": query_body})\n",
"\n",
"if not response.ok:\n",
" print(\"Error querying Pharos\")\n",
" response.raise_for_status()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Turn the response into a DataFrame\n",
"\n",
"The response is a JSON string with the following structure:\n",
"\n",
"```\n",
"data {\n",
" download {\n",
" data {\n",
" [list of dictionary items with fields \"id\", \"UniProt\", \"Symbol\", and \"Target Development Level\"]]\n",
" }\n",
" }\n",
"}\n",
"```\n",
"\n",
"Calling `json_normalize` on the inner \"data\" item will turn that list into a data frame. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res_str = json.loads(response.content)\n",
"pharos_df = pd.json_normalize(res_str[\"data\"][\"download\"][\"data\"])\n",
"\n",
"pharos_df = pharos_df.rename(\n",
" columns={\n",
" \"UniProt\": \"uniprot_id\",\n",
" \"Symbol\": \"hgnc_symbol\",\n",
" \"Target Development Level\": \"pharos_class\",\n",
" }\n",
").drop(columns=\"id\")\n",
"\n",
"pharos_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Map UniProt IDs to Ensembl IDs\n",
"\n",
"Uses the UniProt -> Ensembl ID Agora source file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Temporarily hard-coded until this file is in the config.yaml file\n",
"uniprot_syn_id = \"syn54113663\"\n",
"\n",
"syn = synapseclient.Synapse()\n",
"syn.login(silent=True)\n",
"\n",
"uniprot_path = syn.get(uniprot_syn_id)\n",
"uniprot_df = pd.read_table(uniprot_path.path, sep=\"\\t\")\n",
"\n",
"uniprot_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"final_df = pd.merge(\n",
" left=pharos_df,\n",
" right=uniprot_df,\n",
" how=\"inner\",\n",
" left_on=\"uniprot_id\",\n",
" right_on=\"UniProtKB_accession\",\n",
" validate=\"one_to_many\",\n",
").rename(columns={\"RESOURCE_IDENTIFIER\": \"ensembl_gene_id\"})\n",
"\n",
"final_df = final_df[\n",
" [\"ensembl_gene_id\", \"uniprot_id\", \"hgnc_symbol\", \"pharos_class\"]\n",
"].sort_values(by=\"ensembl_gene_id\")\n",
"\n",
"final_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Save to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"final_df.to_csv(\"../../output/pharos_classes.csv\", index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "agora-data-tools-ywFp1Gf9",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}