Interacting with Scifeon through Python
This guide explains how to extract data from Scifeon via a Python script.
In all of the Python code examples below, remember to replace your-domain
with your own Scifeon cloud domain.
Prerequisites
In order to interact with an API in python the requests library needs to be installed.
This is done by typing in the console:
pip install --upgrade requests
If you are using conda to run your python use:
conda install --upgrade requests
Now you can design your python script.
Authorize
First an accessToken needs to be generated to get access:
import json
import requests
header = {
'Content-Type': "application/json"
}
url = "https://your-domain.scifeon.cloud/api/auth/login"
response = requests.request("POST", url,
json = {
"username": "string",
"password": "string"
},
headers = header)
tokenJson = response.json()
access_token = tokenJson.get('accessToken')
The string in username
and password
needs to be swapped with personal username and password.
Example: Getting an experiment
The access_token
can now be used to extract needed data:
url = "https://your-domain.scifeon.cloud/api/entity/experiment/EX00001"
header = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
response = requests.get(url, headers=header)
experiment = response.json()
The output experiment
will be a dictionary, which you can further manipulate in pandas or your favorite dataframe editor.
If you want to print the data in a JSON comparable format you can use the following function:
def jprint(obj):
# create a formatted string of the Python JSON object
text = json.dumps(obj, sort_keys=True, indent=4)
print(text)
jprint(experiment)
Example: Query all results related to a fermentation
You can also request data using dataset-query. An example of this could be requesting all samples related to the fermentation named GDF1234
:
url = "https://your-domain.scifeon.cloud/api/query/dataset"
header = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
query = [
{
"eClass": "Fermentation",
"entity": "fermentation",
"filters": [{ "field": "name", "value": "GDF1234"}],
},
{
"eClass": "Sample",
"collection": "samples",
"filters": [{ "field": "fermentationID", "value": "$fermentation.id"}],
},
{
"eClass": "ResultValue",
"collection": "resultValues",
"filters": [{ "field": "SubjectID", "in": "samples.ID" }],
}]
response = requests.request("POST", url, headers=header, json=query)
queryResult = response.json()
The format of the queryResult
can be found in datasetQuery documentation here
The queryResult
will again be a dictionary, which can be manipulated into a dataframe through pandas.
Jupyter
There has been created a Jupyter notebook, with a tutorial for importing from scifeon. It can be downloaded here Download Jupyter notebook
Next steps
The HTTP API documentation lists all possibilities for querying and saving data.