{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Using POST\n\nPOST allows you to run a query with multiple inputs at once. The output will be a dictionary of dictionaries." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import requests, sys\nfrom pprint import pprint\n\nserver = \"http://rest.ensembl.org\"\next = \"/lookup/id\"\nheaders={ \"Content-Type\" : \"application/json\", \"Accept\" : \"application/json\"}\nr = requests.post(server+ext, headers=headers, data='{ \"ids\" : [\"ENSG00000157764\", \"ENSG00000248378\" ] }')\n\n# error checking removed for space\n \ndecoded = r.json()\npprint (decoded)", "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": "def fetch_endpoint_POST(server, request, data, content_type='application/json'):\n\n r = requests.post(server+request,\n headers={ \"Accept\" : content_type},\n data=data )\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", "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 Python using the [JSON module](https://docs.python.org/3/library/json.html):\n\n `data = json.dumps({ \"ids\" : my_list })`\n\n### Output\n\nThe Output from POST queries will be a dictionary of dictionaries. To access items, you could use your input list as your keys, or you could move through the dictionary with:\n\n\t\t`for key, value in post_query.items():`\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": "import requests, sys, 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\ndef fetch_endpoint_POST(server, request, data, content_type):\n\n r = requests.post(server+request,\n headers={ \"Accept\" : content_type},\n data=data )\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# 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 = [\"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 = json.dumps({ \"hgvs_notations\" : hgvs })\n\n# run the query\npost_vep = fetch_endpoint_POST(server, vep_ext, hgvs_json, con)\n\n# move through the results\nfor variant in post_vep:\n \n # get the data\n input = variant['input']\n colocated_list = []\n for colocated in variant['colocated_variants']:\n colocated_list.append(colocated['id']) \n print (input + \": \" + (', '.join(colocated_list)))", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "# Exercises 6\n\n1\\. Fetch the all the transcripts of *ESPN* using the lookup endpoint. 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": "python3", "display_name": "Python 3", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "3.5.4", "pygments_lexer": "ipython3", "codemirror_mode": { "version": 3, "name": "ipython" } } }, "nbformat": 4, "nbformat_minor": 2 }