Programming Using Pep-Calc API

Libraries for parsing JSON data and URL encoding are available in many languages, making it easy to integrate calculations using Pep-Calc API into your programs. Language support for JSON data structures is summarized at JSON.org. Specific examples illustrating the use of Pep-Calc API in various languages are also given below.

For integration of the features of Pep-Calc API into Microsoft Excel see the section on Visual Basic for Applications (VBA).

Python

The Python Standard Library includes everything needed to use Pep-Calc API resources in Python programs. The following example illustrates usage of the Basic Properties resource, using httplib for HTTP requests, urllib for query string encoding and json for decoding the returned JSON object into a Python dictionary.

HTTP requests can also be handled in Python using Requests.

import httplib
import urllib
import json

def peptideBasics(sequence, Nterm, Cterm):
  seq = urllib.quote(sequence, safe='')
  N_term = urllib.quote(Nterm, safe='')
  C_term = urllib.quote(Cterm, safe='')
  conn = httplib.HTTPConnection("api.pep-calc.com")
  conn.request("GET", "/peptide?seq=" + seq + "&N_term=" + N_term + "&C_term=" + C_term)
  data = conn.getresponse().read()
  return json.loads(data)

R

HTTP requests are easily handled in R using the httr package (which also has support for JSON parsing). The jsonlite package is used in the following example, which demonstrates access to the Basic Properties resource.

library(httr)
library(jsonlite)

peptideBasics <- function(sequence, Nterm, Cterm){
  r <- GET("http://api.pep-calc.com/peptide",
        query = list(seq = sequence, N_term = Nterm, C_term = Cterm)
  )
  body = content(r, "text")
  return(fromJSON(body))
}

Visual Basic for Applications (VBA)

JSON parsing can be handled in VBA using VBA-JSON. The following example adds the Excel function PTIDEPROP(), which can return any of the properties calculated by the Basic Properties resource. URL encoding can be carried out using the URLEncode() function found here.

For a more comprehensive example see the Pep-Calc Microsoft Excel Add-In.

Function ptideProp(property As String, sequence As String, nTerm As String, cTerm As String)

  Dim Request As Object
  Dim seq, N_term, C_term As String
  Dim Json As Dictionary

  seq = URLEncode(sequence)
  N_term = URLEncode(nTerm)
  C_term = URLEncode(cTerm)

  Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
  Request.Open "GET", _
  "http://api.pep-calc.com/peptide?seq=" + seq + "&N_term=" + N_term + "&C_term=" + C_term
  Request.Send
  Set Json = JsonConverter.ParseJson(Request.ResponseText)

  ptideProp = Json(property)

End Function