Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check annotation to be of type dict while reading it from the json #930

Merged
merged 5 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cobra/core/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, id=None, name=""):
self.name = name

self.notes = {}
self.annotation = {}
self._annotation = {}

@property
def id(self):
Expand All @@ -40,6 +40,19 @@ def id(self, value):
def _set_id_with_model(self, value):
self._id = value

@property
def annotation(self):
return getattr(self, "_annotation", None)
Midnighter marked this conversation as resolved.
Show resolved Hide resolved

@annotation.setter
def annotation(self, annotation):
if annotation == self.annotation:
pass
elif not isinstance(annotation, dict):
raise TypeError("Annotation must be a dict")
else:
self._annotation = annotation

def __getstate__(self):
"""To prevent excessive replication during deepcopy."""
state = self.__dict__.copy()
Expand Down
24 changes: 24 additions & 0 deletions cobra/test/data/invalid_annotation_format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"metabolites":[
{
"id":"4crsol_c",
"name":"",
"compartment":"c",
"annotation":[
[
"KEGG Compound",
"http://identifiers.org/kegg.compound/C01468"
],
[
"CHEBI",
"http://identifiers.org/chebi/CHEBI:11981"
]
]
}
],
"reactions":[],
"genes":[],
"id":"tesut",
"compartments":{},
"version": 1
}
19 changes: 19 additions & 0 deletions cobra/test/data/valid_annotation_format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"metabolites":[
{
"id":"4crsol_c",
"name":"",
"compartment":"c",
"annotation": {
"bigg.reaction": [["is", "PFK26"]],
"kegg.reaction": [["is", "R02732"]],
"rhea": [["is", "15656"]]
}
}
],
"reactions":[],
"genes":[],
"id":"tesut",
"compartments":{},
"version": 1
}
44 changes: 44 additions & 0 deletions cobra/test/data/valid_annotation_output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" xmlns:fbc="http://www.sbml.org/sbml/level3/version1/fbc/version2" sboTerm="SBO:0000624" level="3" version="1" fbc:required="false">
<model metaid="meta_tesut" id="tesut" fbc:strict="true">
<listOfUnitDefinitions>
<unitDefinition id="mmol_per_gDW_per_hr">
<listOfUnits>
<unit kind="mole" exponent="1" scale="-3" multiplier="1"/>
<unit kind="gram" exponent="-1" scale="0" multiplier="1"/>
<unit kind="second" exponent="-1" scale="0" multiplier="3600"/>
</listOfUnits>
</unitDefinition>
</listOfUnitDefinitions>
<listOfCompartments>
<compartment id="c" constant="true"/>
</listOfCompartments>
<listOfSpecies>
<species metaid="meta_M_4crsol_c" id="M_4crsol_c" compartment="c" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false">
<annotation>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#" xmlns:vCard4="http://www.w3.org/2006/vcard/ns#" xmlns:bqbiol="http://biomodels.net/biology-qualifiers/" xmlns:bqmodel="http://biomodels.net/model-qualifiers/">
<rdf:Description rdf:about="#meta_M_4crsol_c">
<bqbiol:is>
<rdf:Bag>
<rdf:li rdf:resource="https://identifiers.org/bigg.reaction/PFK26"/>
<rdf:li rdf:resource="https://identifiers.org/kegg.reaction/R02732"/>
<rdf:li rdf:resource="https://identifiers.org/rhea/15656"/>
</rdf:Bag>
</bqbiol:is>
</rdf:Description>
</rdf:RDF>
</annotation>
</species>
</listOfSpecies>
<listOfParameters>
<parameter sboTerm="SBO:0000626" id="cobra_default_lb" value="-1000" constant="true"/>
<parameter sboTerm="SBO:0000626" id="cobra_default_ub" value="1000" constant="true"/>
<parameter sboTerm="SBO:0000626" id="cobra_0_bound" value="0" constant="true"/>
<parameter sboTerm="SBO:0000625" id="minus_inf" value="-INF" constant="true"/>
<parameter sboTerm="SBO:0000625" id="plus_inf" value="INF" constant="true"/>
</listOfParameters>
<fbc:listOfObjectives fbc:activeObjective="obj">
<fbc:objective fbc:id="obj" fbc:type="maximize"/>
</fbc:listOfObjectives>
</model>
</sbml>
25 changes: 25 additions & 0 deletions cobra/test/test_io/test_annotation_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from os.path import join
from cobra.io import load_json_model, write_sbml_model


def test_load_json_model_valid(data_directory):
"""Testing valid annotation format"""
Midnighter marked this conversation as resolved.
Show resolved Hide resolved
path_to_file = join(data_directory, "valid_annotation_format.json")
model = load_json_model(path_to_file)
dict = {
Midnighter marked this conversation as resolved.
Show resolved Hide resolved
'bigg.reaction': [['is', 'PFK26']],
'kegg.reaction': [['is', 'R02732']],
'rhea': [['is', '15656']]
}
for x in model.metabolites:
Midnighter marked this conversation as resolved.
Show resolved Hide resolved
assert x.annotation == dict, "Dictionary is not equal"
Midnighter marked this conversation as resolved.
Show resolved Hide resolved
path_to_output = join(data_directory, 'valid_annotation_output.xml')
write_sbml_model(model, path_to_output)


def test_load_json_model_invalid(data_directory):
"""Testing invalid annotation format"""
Midnighter marked this conversation as resolved.
Show resolved Hide resolved
path = join(data_directory, "invalid_annotation_format.json")
with pytest.raises(TypeError):
model = load_json_model(path)