{ "cells": [ { "metadata": { "collapsed": true }, "cell_type": "markdown", "source": "## Exercises 3 – answers\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": "library(httr)\nlibrary(jsonlite)\n\nfetch_endpoint <- function(server, request, content_type){\n\n r <- GET(paste(server, request, sep = \"\"), accept(content_type))\n\n stop_for_status(r)\n\n if (content_type == 'application/json'){\n return (fromJSON(content(r, \"text\", encoding = \"UTF-8\")))\n } else {\n return (content(r, \"text\", encoding = \"UTF-8\"))\n }\n}\n\n# define the gene name\ngene_name <- \"ESPN\"\n\n# define the general URL parameters\nserver <- \"http://rest.ensembl.org/\"\ncon <- \"application/json\"\n\n# define REST query to get the gene ID from the gene name\next_get_lookup <- paste(\"lookup/symbol/homo_sapiens/\", gene_name, \"?\", sep =\"\")\n\nget_lookup <- fetch_endpoint(server, ext_get_lookup, con)\n\nstable_id <- get_lookup$id\n\ncat(stable_id)", "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.\n\nNote that R cannot cope with spaces in your URL extension, so you will need to write the phenotype as 'coffee%20consumption'." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "library(httr)\nlibrary(jsonlite)\n\nfetch_endpoint <- function(server, request, content_type){\n\n r <- GET(paste(server, request, sep = \"\"), accept(content_type))\n\n stop_for_status(r)\n\n if (content_type == 'application/json'){\n return (fromJSON(content(r, \"text\", encoding = \"UTF-8\")))\n } else {\n return (content(r, \"text\", encoding = \"UTF-8\"))\n }\n}\n\n# define the URL parameters\nserver <- \"http://rest.ensembl.org/\"\ncon <- \"application/json\"\next_phen <- \"/phenotype/term/homo_sapiens/coffee%20consumption?\"\n\nget_phen <- fetch_endpoint(server, ext_phen, con)\n\nflat_get_phen <- flatten(get_phen, recursive = TRUE)\n\nflat_get_phen[, c(\"Variation\", \"attributes.p_value\", \"attributes.external_reference\", \"attributes.risk_allele\", \"attributes.associated_gene\")]", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "3\\. Get the mouse homologue of the human BRCA2 and print the ID and the aligned 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 (appear as curly brackets { } in the JSON)." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "library(httr)\nlibrary(jsonlite)\n\nfetch_endpoint <- function(server, request, content_type){\n\n r <- GET(paste(server, request, sep = \"\"), accept(content_type))\n\n stop_for_status(r)\n\n if (content_type == 'application/json'){\n return (fromJSON(content(r, \"text\", encoding = \"UTF-8\")))\n } else {\n return (content(r, \"text\", encoding = \"UTF-8\"))\n }\n}\n\ngene <- \"BRCA2\"\n\n# define the URL parameters\nserver <- \"http://rest.ensembl.org/\"\ncon <- \"application/json\"\next_hom <- paste(\"homology/symbol/human/\", gene, \"?target_species=mouse\", sep = \"\")\n\nget_hom <- fetch_endpoint(server, ext_hom, con)\n\nhomologies <- get_hom$data$homologies\n\nfor (homology in homologies) {\n homology\n\n source_id <- homology$source$id\n source_seq <- homology$source$align_seq\n source_species <- homology$source$species\n\n target_id <- homology$target$id\n target_seq <- homology$target$align_seq\n target_species <- homology$target$species\n \n cat(\">\", source_id, source_species, \"\\n\", source_seq, \"\\n>\", target_id, target_species, \"\\n\", target_seq)\n }\n\n", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "![3.3_R.png](http://ftp.ebi.ac.uk/pub/databases/ensembl/training/images_for_REST/3.3_R.png)" }, { "metadata": {}, "cell_type": "markdown", "source": "[Next page: Other content types](4_Other_content_types.ipynb)" } ], "metadata": { "kernelspec": { "name": "r", "display_name": "R", "language": "R" }, "language_info": { "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.5.3", "file_extension": ".r", "codemirror_mode": "r" } }, "nbformat": 4, "nbformat_minor": 2 }