API code exmaples

Below are code snippets for using API from R and python languages.

R framework

Data can be imported directly as CSV or JSON formats. Please refer to the API documentation for instructions on how to specify the output format.

Fetch data as CSV

The code below downloads all Protein data, and plot a histogram of the experiment count.
  protein_data = read.csv(url("http://127.0.0.1:8000/uniprot/?format=csv"))
  hist(protein_data$experiment_count)

Fetch data as JSON

Similar to the code above, we can use jsonlite library to read JSON data into R.
  library("jsonlite")
  json_data = fromJSON(url("http://127.0.0.1:8000/uniprot/?format=json"))
  protein_data = json_data$results
  hist(protein_data$experiment_count)

Python

The easiest way to import data into python is through JSON format, which is natively supported. CSV and XML formats can also be parsed using specialized modules, but will require more elaborate data processing, and thus omitted from this guide. For details refer to CSV and XML.

Fetch data as JSON

We can read JSON data from urls in different ways into python. Here, we use built-in requests library.

  import requests
  r = requests.get('http://127.0.0.1:8000/uniprot/?format=json')
  protein_data = r.json()["results"]
  len(protein_data) # print the number of proteins in the database