In this notebook we show how to query the Genenetwork API as well as how to query data declaratively with glom.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

147 lines
3.2 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GeneNetwork REST API Demo\n",
"\n",
"![Binderlite](https://raw.githubusercontent.com/jgarte/binderlite-example/main/deploy-with-guix-badge.svg)\n",
"\n",
"[notebook source code](https://git.genenetwork.org/jgart/genenetwork-jupyter-notebook-example)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"from pprint import pprint"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fetching Datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"data = requests.get(\"http://genenetwork.org/api/v_pre1/species\")\n",
"pprint(data.json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Getting information about a single species\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = requests.get(\"http://genenetwork.org/api/v_pre1/species/mouse\")\n",
"print(data.json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fetching Genotypes for Group/RISet\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"api = 'http://genenetwork.org/api/v_pre1/genotypes/bimbam/BXD'\n",
"\n",
"response = requests.get(api)\n",
"\n",
"if response.status_code != 200:\n",
" print('Failed to get data:', response.status_code)\n",
"else:\n",
" print(\"Group/RISet Genotype Data: \")\n",
" print(response.text[:50000])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting Datasets Declaratively with glom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"from glom import glom\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"r = requests.get(\"http://genenetwork.org/api/v_pre1/datasets/bxd\")\n",
"\n",
"input_data = r.json()\n",
"\n",
"spec = [{\"AvgID\": \"AvgID\", \n",
" \"Probe Freeze Id\": \"ProbeFreezeId\"}]\n",
"\n",
"output_data = glom(input_data, spec)\n",
"\n",
"df = pd.DataFrame(output_data,columns=['AvgID','Probe Freeze Id'])\n",
"\n",
"x = df['AvgID']\n",
"y = df['Probe Freeze Id']\n",
"\n",
"plt.figure(figsize=(20,10))\n",
"plt.xlabel(\"AvgID\")\n",
"plt.ylabel(\"Probe Freeze Id\")\n",
"plt.scatter(x, y)\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "/gnu/store/nckjv3ccwdi6096j478gvns43ssbls2p-python-wrapper-3.8.2/bin/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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}