{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Using results\n\nSince json is a dictionary, you can pull out a single datapoint using the key.\n\n```\n{\n \"source\": \"ensembl_havana\",\n \"object_type\": \"Gene\",\n \"logic_name\": \"ensembl_havana_gene\",\n \"version\": 12,\n \"species\": \"homo_sapiens\",\n \"description\": \"B-Raf proto-oncogene, serine/threonine kinase [Source:HGNC Symbol;Acc:HGNC:1097]\",\n \"display_name\": \"BRAF\",\n \"assembly_name\": \"GRCh38\",\n \"biotype\": \"protein_coding\",\n \"end\": 140924764,\n \"seq_region_name\": \"7\",\n \"db_type\": \"core\",\n \"strand\": -1,\n \"id\": \"ENSG00000157764\",\n \"start\": 140719327\n}\n```\n\nWe can add this to our previous script:" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import requests, json\nfrom pprint import pprint\n\ndef fetch_endpoint(server, request, content_type):\n\n r = requests.get(server+request, headers={ \"Accept\" : content_type})\n\n if not r.ok:\n r.raise_for_status()\n sys.exit()\n\n if content_type == 'application/json':\n return r.json()\n else:\n return r.text\n\n\nserver = \"http://rest.ensembl.org/\"\next = \"lookup/id/ENSG00000157764?\"\ncon = \"application/json\"\nget_gene = fetch_endpoint(server, ext, con)\n\nsymbol = get_gene['display_name']\nprint (symbol)", "execution_count": 1, "outputs": [ { "output_type": "stream", "text": "BRAF\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "## Exercises 3\n\n1\\. Write a script to lookup the gene called *ESPN* in human and print the stable ID of this gene." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 3.1", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "2\\. Get all variants that are associated with the phenotype 'Coffee consumption'. For each variant print\n\n a. the p-value for the association\n \n b. the PMID for the publication which describes the association between that variant and ‘Coffee consumption’\n \n c. the risk allele and the associated gene." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 3.2", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "3\\. Get the mouse homologue of the human BRCA2 and print the ID and sequence of both.\n\nNote that the JSON for the endpoint you need is several layers deep, containing nested lists (appear as square brackets [ ] in the JSON) and key value sets (dictionary; appear as curly brackets { } in the JSON). Pretty print (pprint) comes in very useful here for the intermediate stage when you're trying to work out the json." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 3.3", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "[Next page: Exercises 3 – answers](3_Using_results_answers.ipynb)" } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "version": "3.5.4", "file_extension": ".py", "codemirror_mode": { "version": 3, "name": "ipython" } } }, "nbformat": 4, "nbformat_minor": 2 }