{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate exact mass for lipid species using MW REST API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import Python modules..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function\n", "\n", "import os\n", "import sys\n", "import time\n", "import re\n", "\n", "import requests\n", "\n", "from IPython import __version__ as ipyVersion\n", "\n", "print(\"Python: %s.%s.%s\" % sys.version_info[:3])\n", "print(\"IPython: %s\" % ipyVersion)\n", "\n", "print()\n", "print(time.asctime())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The URL PATH**\n", "\n", "The MW REST URL consists of three main parts, separated by forward slashes, after the common prefix specifying the invariant base URL (https://www.metabolomicsworkbench.org/rest/):\n", "\n", "https://www.metabolomicsworkbench.org/rest/context/input_specification/output_specification\n", " \n", "Part 1: The context determines the type of data to be accessed from the Metabolomics Workbench, such as metadata or results related to the submitted studies, data from metabolites, genes/proteins and analytical chemistry databases as well as other services related to mass spectrometry and metabolite identification:\n", "\n", "context = study | compound | refmet | gene | protein | moverz | exactmass\n", "\n", "Part 2: The input specification consists of two required parameters describing the REST request:\n", "\n", "input_specification = input_item/input_value\n", "\n", "Part 3: The output specification consists of two parameters describing the output generated by the REST request:\n", "\n", "output_specification = output_item/(output_format)\n", "\n", "The first parameter is required in most cases. The second parameter is optional. The input and output specifications are context sensitive. The context determines the values allowed for the remaining parameters in the input and output specifications as detailed in the sections below.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setup MW REST base URL..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MWBaseURL = \"https://www.metabolomicsworkbench.org/rest\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The “exactmass” context**\n", "\n", "The context \"exactmass\" calculates the exact mass of a lipid species by specifying an appropriate lipid abbreviation and ion type(adduct).\n", "\n", "context = exactmass\n", "\n", "input_value1 = LIPID_abbreviation\n", "\n", "input_value2 = ion_type_value\n", "\n", "The following head groups are currently supported as abbreviations for lipids: ArthroCer, asialo-GM2Cer, CAR, CE, Cer, CerP, CoA, DG, DGDG, FA, GalCer, GB3Cer, GlcCer, GM3Cer, GM4Cer, iGB3Cer, LacCer, Lc3Cer, Manb1-4GlcCer, MG, MGDG, MolluCer, PA, PC, PE, PE-Cer, PG, PGP, PI, PI-Cer, PIP, PIP2, PIP3, PS, SM, SQDG, TG\n", "\n", "The following ion types (adducts) are currently supported: Neutral, M+H, M+H-H2O, M+2H, M+3H, M+4H, M+K, M+2K, M+2K-H, M+Na, M+2Na, M+2Na-H, M+Li, M+2Li, M+Ag, M+NH4, M-H, M-CH3, M-2H, M-3H, M-4H, M.Cl, M.OAc, M.Formate\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Retrieve and process data from a an exactmass calculation in text format**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setup REST URL to calculate the exact mass (m/z) of the M+H ion for the lipid abbreviation PC(34:1)..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MWDataURL = MWBaseURL + \"/exactmass/PC(34:1)/M+H\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute REST request using \"request\" module..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Initiating request: %s\" % MWDataURL)\n", " \n", "Response = requests.get(MWDataURL)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check \"request\" status..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"\\nStatus Code: %d\" % (Response.status_code))\n", "\n", "if Response.status_code != 200:\n", " print(\"Request failed: status_code: %d\" % Response.status_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Proess calcuation results..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"\\nAvailable data from an exact mass calculation:\\n\")\n", "\n", "Results = Response.text\n", "Results = re.sub(\"
\", \"\", Results, re.I)\n", "\n", "for Result in Results.split(\"\\n\"):\n", " if len(Result) == 0:\n", " continue\n", " \n", " print(\"%s\" % Result)\n", "\n", "print(\"\\nAvailable data from an exact mass calculation:\\n\")\n", "\n", "ResultLines = Results.split(\"\\n\")\n", "print(\"LipidAbbreviation: %s\\nIonType: %s\\nExactMass: %s\\nMolecularFormula: %s\\n\" % (ResultLines[0], ResultLines[1], ResultLines[2], ResultLines[3]))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }