Calculate exact mass for lipid species using MW REST API

Import Python modules...

In [ ]:
from __future__ import print_function

import os
import sys
import time
import re

import requests

from IPython import __version__ as ipyVersion

print("Python: %s.%s.%s" % sys.version_info[:3])
print("IPython: %s" % ipyVersion)

print()
print(time.asctime())

The URL PATH

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/):

https://www.metabolomicsworkbench.org/rest/context/input_specification/output_specification

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:

context = study | compound | refmet | gene | protein | moverz | exactmass

Part 2: The input specification consists of two required parameters describing the REST request:

input_specification = input_item/input_value

Part 3: The output specification consists of two parameters describing the output generated by the REST request:

output_specification = output_item/(output_format)

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.

Setup MW REST base URL...

In [ ]:
MWBaseURL = "https://www.metabolomicsworkbench.org/rest"

The “exactmass” context

The context "exactmass" calculates the exact mass of a lipid species by specifying an appropriate lipid abbreviation and ion type(adduct).

context = exactmass

input_value1 = LIPID_abbreviation

input_value2 = ion_type_value

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

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

Retrieve and process data from a an exactmass calculation in text format

Setup REST URL to calculate the exact mass (m/z) of the M+H ion for the lipid abbreviation PC(34:1)...

In [ ]:
MWDataURL = MWBaseURL + "/exactmass/PC(34:1)/M+H"

Execute REST request using "request" module...

In [ ]:
print("Initiating request: %s" % MWDataURL)
    
Response = requests.get(MWDataURL)

Check "request" status...

In [ ]:
print("\nStatus Code: %d" % (Response.status_code))

if Response.status_code != 200:
    print("Request failed: status_code: %d" % Response.status_code)

Proess calcuation results...

In [ ]:
print("\nAvailable data from an exact mass calculation:\n")

Results = Response.text
Results = re.sub("</br>", "", Results, re.I)

for Result in Results.split("\n"):
    if len(Result) == 0:
        continue
    
    print("%s" % Result)

print("\nAvailable data from an exact mass calculation:\n")

ResultLines = Results.split("\n")
print("LipidAbbreviation: %s\nIonType: %s\nExactMass: %s\nMolecularFormula: %s\n" % (ResultLines[0], ResultLines[1], ResultLines[2], ResultLines[3]))