Update strain genotypes example
This document is an example of how you can use the Python interaction to edit your strain genotypes.
First we need to find the data of interest, this is done as explained in Interacting with python:
Authorization
We add domain, username and password first
import requests
import json
scifeonCloudDomain = "Insert cloud domain here"
username = "Insert username here"
password = "Insert password here"
Then a token is generated
rqheader = {"Content-Type": "application/json"}
urltoken="https://"+scifeonCloudDomain+"/api/auth/login"
response=requests.request("POST", urltoken,
json={
"username": username,
"password": password},
headers = rqheader)
r = response.json()
access_token = r.get('accessToken')
Querying
Dataset query is now used to access the strain SR124848 with genotypes:
urlcontent = "https://" + scifeonCloudDomain + "/api/query/dataset"
getheaders = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
parameters = [
{
"eClass": "Strain", "entity": "strain", "expand": ["genotypeChange"],
"filters": [
{ "field": "Id", "value": "SR124848"}
],
}]
rdata = requests.request("POST", urlcontent, headers=getheaders, json=parameters)
files = rdata.json()
Querying more than one set of data
If you need to extract more strains. You can use a list with the given strain names together with an empty list for rdata and files:
strains = ["SR124848", "SR124842"]
rdata = []
files = []
urlcontent = "https://" + scifeonCloudDomain + "/api/query/dataset"
for strain in strains:
getheaders = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
parameters = [
{
"eClass": "Strain", "entity": "strain", "expand": ["genotypeChange"],
"filters": [
{ "field": "Id", "value": strain}
],
}]
rdata.append(requests.request("POST", urlcontent, headers=getheaders, json=parameters))
for data in rdata:
files.append(data.json())
Files will be a dictionary or a list of dictionaries containing the entities for the given strain. This can easiely be displayed using the 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(files)
Data manipulation examples:
Several different things can now be done to edit the data:
- Editing
- Deleting
- Adding
In all three cases. The string under the key strain.genotype will get "Edited" added on to the end, to help indicate that the strain has been modified. This is not strictly necessary and can be deleted.
Editing:
When editing in genotypes, the function genotypeEditor can be used. It takes two values: oldvalue & newvalue. oldvalue is the value you want to update, if it is a gene "lacI" could be and example. newvalue is the value you want to replace it with.
The editor function is as follows:
def genotypeEditor(oldvalue, newvalue):
for d in files["strain"]["genotypeChanges"]:
d.update((k, newvalue) for k, v in d.items() if v == oldvalue)
listfunction = files["strain"]["genotype"].split(", ")
listfunction = [w.replace(oldvalue, newvalue) for w in listfunction]
listfunction += ["Edited"]
files["strain"]["genotype"] = ", ".join(listfunction)
return
The editing is not specific for genes, but can be used on any key in genotypeChanges. It will edit all genotypeChanges with the matching value.
Deleting:
When deleting, the following function is used. It takes the value genedeleted: genedeleted is refering to the gene that you want to removed from genotypeChanges.
def genotypeDeletor(genedeleted):
for d in files["strain"]["genotypeChanges"]:
if d['gene'] == genedeleted:
d.update({"Deleted":"true"})
listfunction = files["strain"]["genotype"].split(", ")
listfunction += ["Edited"]
files["strain"]["genotype"] = ", ".join(listfunction)
return
This function only works on genes in genotypeChanges, it can be edited to work on other keys just change d['gene']
.
Be aware that this function doesn't actually delete the genotype from the strain, but just adds a key "Deletion" with the value "true" and DELETED under the description of the genotype.
Adding:
The following functions are used for adding a new genotype
def genotypeAdder(newgene, description):
files["strain"]["genotypeChanges"] += [geneeditor(newgene, description)]
listfunction = files["strain"]["genotype"].split(", ")
listfunction += [description]
listfunction += ["Edited"]
files["strain"]["genotype"] = ", ".join(listfunction)
return (files["strain"]["genotypeChanges"])
def geneeditor(newgene, description):
newgenotype = {}
newgenotype["gene"] = newgene
newgenotype["description"] = description
#Add any: newgenotype[key] = value, you want to edit.
return newgenotype
When adding a new genotype the function is run as shown below, for the given genotypeChanges:
files["strain"]["genotypeChanges"] = genotypeAdder("xxxgene", "xxxDescription")
The new genotype will be added as a new dictionary under genotypeChanges at the end of the list. This new dictionary will only contain the key "gene" and "description". If more keys needs to be added this can be done under the geneeditor function by writing:
newgenotype["The key you want to add"] = "The value you want to add"`
Saving:
Now that the genotype has been edited it can be reuploaded:
urlcontent = "https://" + scifeonCloudDomain + "/api/entity"
getheaders = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
parameters = {
"entities": [files["strain"]]
}
requests.request("POST", urlcontent, headers=getheaders, json=parameters)
The strain and genotypes should now be updated in your Scifeon domain.
If you have added a new genotypeChange the keys that you haven't filled in.
If you are making multiple updates, you can add a loop over parameters and requests.requests:
For i in range(len(files["strain"])):
parameters = {
"entities": [files["strain"][i]]
}
requests.request("POST", UrlEntity, headers = GetHeaders, json = parameters)
#Further editing This tutorial is aiming at giving a rough introduction to how editing of data can be done in Python. Be aware that dataupload of parameters needs to follow the JSON format strictly. The jprint function can be used debug issues on format.