{ "cells": [ { "metadata": { "collapsed": true }, "cell_type": "markdown", "source": "# Using POST\n\nPOST allows you to run a query with multiple inputs at once. The output will be a list of data-frames." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "library(httr)\nlibrary(jsonlite)\n \nserver <- \"https://rest.ensembl.org\"\next <- \"/lookup/id\"\ngenes <- c(\"ENSG00000157764\", \"ENSG00000248378\")\nbody_values <- toJSON(list(ids=genes))\n\nr <- POST(paste(server, ext, sep = \"\"), content_type(\"application/json\"), accept(\"application/json\"), body = body_values)\n\nprettify(content(r, \"text\", encoding = \"UTF-8\"))", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "There is a helper function in POST. You can specify both helper functions in your script and use whichever one you need." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "fetch_endpoint_POST <- function(server, request, data, content_type='application/json'){\n\n r <- POST(paste(server, request, sep = \"\"), content_type(\"application/json\"), accept(\"application/json\"), body = data)\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}", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "### Optional parameters\n\nIn order to add optional parameters to your POST query, you can just add them onto the extention with a slash. For example if you wanted to mask UTRs when running the [sequence_id_post](http://rest.ensembl.org/documentation/info/sequence_id_post) endpoint, you could specify your extension as:\n\n `ext <- \"sequence/id/mask_feature=1\"`\n\n\n### Input\n\nYour input list for POST queries need to be a JSON list. You can create this from a list in R using the [jsonlite library](https://www.rdocumentation.org/packages/jsonlite/versions/1.5):\n\n `data <- toJSON(list(ids=mylist))`\n\n### Output\n\nThe Output from POST queries will be a list of dataframes.\n\n## Example\n\nThe following scripts inputs a list of variants in HGVS format into the VEP and gets out the IDs of known colocated variants, including failed variants (an optional parameter):" }, { "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}\nfetch_endpoint_POST <- function(server, request, data, content_type='application/json'){\n\n r <- POST(paste(server, request, sep = \"\"), content_type(\"application/json\"), accept(\"application/json\"), body = data)\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 server, extension and content type\nserver <- \"http://rest.ensembl.org/\"\ncon <- \"application/json\"\nvep_ext <- \"vep/homo_sapiens/hgvs/failed=1\"\n\n# create the list of HGVS annotations\nhgvs <- c(\"ENST00000366667.4:c.803T>C\", \"ENST00000335295.4:c.20A>T\", \"ENST00000415952.1:c.-149-34206G>T\")\n\n# convert the list into json format\nhgvs_json <- toJSON(list(hgvs_notations=hgvs))\n\n# run a post query with the list of variants\npost_vep <- fetch_endpoint_POST(server, vep_ext, hgvs_json, con)\n\n#\nfor (i in 1:nrow(post_vep)) {\n variant <- post_vep[i, ]\n input <- variant$input\n colocated <- variant$colocated_variants\n for (col in colocated) {\n variant_ids = col$id\n }\n print (paste(input, variant_ids, sep = \": \"))\n}", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "## Exercises 6\n\n1\\. Fetch the all the transcripts of *ESPN* using the lookup function. Fetch the cDNA sequences of all transcripts using a single POST request, and print in FASTA format." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 6.1", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "2\\. You have the following list of variants:\n```rs1415919662, rs957333053, rs762944488, rs1372123943, rs553810871, rs1451237599, rs751376931```\nGet the variant class, evidence attributes, source and the most_severe_consequence for all variants using the variation POST endpoint." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 6.2", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "[Next page: Exercises 6 – answers](6_Using_POST_answers.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 }