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

Consistent list annotation #959

Open
wants to merge 8 commits into
base: devel
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added test to check consistency in annotation
  • Loading branch information
Hemant27031999 committed May 31, 2020
commit 0132f171040f28d3fea723fa4392b8538ed03a6e
18 changes: 18 additions & 0 deletions cobra/io/dict.py
Original file line number Diff line number Diff line change
@@ -53,6 +53,14 @@
}


def _fix_annotation(annotation):
# Convert single annotation values which are represented as
# as strings as list to have a consistent format
for key in annotation.keys():
if isinstance(annotation[key], string_types) and key != "sbo":
annotation[key] = [annotation[key]]


def _fix_type(value):
"""convert possible types to str, float, and bool"""
# Because numpy floats can not be pickled to json
@@ -82,6 +90,8 @@ def _update_optional(cobra_object, new_dict, optional_attribute_dict,
value = getattr(cobra_object, key)
if value is None or value == default:
continue
if key == "annotation":
_fix_annotation(value)
new_dict[key] = _fix_type(value)


@@ -98,6 +108,8 @@ def metabolite_from_dict(metabolite):
new_metabolite = Metabolite()
for k, v in iteritems(metabolite):
setattr(new_metabolite, k, v)
if k == "annotation":
_fix_annotation(v)
return new_metabolite


@@ -114,6 +126,8 @@ def gene_from_dict(gene):
new_gene = Gene(gene["id"])
for k, v in iteritems(gene):
setattr(new_gene, k, v)
if k == "annotation":
_fix_annotation(v)
return new_gene


@@ -143,6 +157,8 @@ def reaction_from_dict(reaction, model):
for met, coeff in iteritems(v)))
else:
setattr(new_reaction, k, v)
if k == "annotation":
_fix_annotation(v)
return new_reaction


@@ -226,4 +242,6 @@ def model_from_dict(obj):
for k, v in iteritems(obj):
if k in {'id', 'name', 'notes', 'compartments', 'annotation'}:
setattr(model, k, v)
if k == "annotation":
_fix_annotation(v)
return model
Binary file added cobra/test/data/newmini.pickle
Binary file not shown.
17 changes: 17 additions & 0 deletions cobra/test/test_io/test_json.py
Original file line number Diff line number Diff line change
@@ -39,3 +39,20 @@ def test_save_json_model(tmpdir, mini_model):
with open(output_file, "r") as infile:
loaded = json.load(infile)
assert jsonschema.validate(loaded, cio.json.json_schema)


def test_consistent_annotation_values(data_directory):
"""Test if annotation are consistently represented as list"""
model = cio.read_sbml_model(join(data_directory, "mini_fbc2.xml"))
# annotation of genes
for gene in model.genes:
for key in list(gene.annotation.keys()):
assert isinstance(gene.annotation[key], list)
# annotation of metabolites
for metabolite in model.metabolites:
for key in list(metabolite.annotation.keys()):
assert isinstance(metabolite.annotation[key], list)
# annotation of reaction
for reaction in model.genes:
for key in list(reaction.annotation.keys()):
assert isinstance(reaction.annotation[key], list)
58 changes: 29 additions & 29 deletions cobra/test/test_io/test_sbml.py
Original file line number Diff line number Diff line change
@@ -31,14 +31,14 @@
IOTrial = namedtuple('IOTrial',
['name', 'reference_file', 'test_file', 'read_function',
'write_function', 'validation_function'])
trials = [IOTrial('fbc2', 'mini.pickle', 'mini_fbc2.xml',
trials = [IOTrial('fbc2', 'newmini.pickle', 'mini_fbc2.xml',
read_sbml_model, write_sbml_model,
validate_sbml_model),
IOTrial('fbc2Gz', 'mini.pickle', 'mini_fbc2.xml.gz',
IOTrial('fbc2Gz', 'newmini.pickle', 'mini_fbc2.xml.gz',
read_sbml_model, write_sbml_model, None),
IOTrial('fbc2Bz2', 'mini.pickle', 'mini_fbc2.xml.bz2',
IOTrial('fbc2Bz2', 'newmini.pickle', 'mini_fbc2.xml.bz2',
read_sbml_model, write_sbml_model, None),
IOTrial('fbc1', 'mini.pickle', 'mini_fbc1.xml',
IOTrial('fbc1', 'newmini.pickle', 'mini_fbc1.xml',
read_sbml_model, write_sbml_model, None),
IOTrial('cobra', None, 'mini_cobra.xml',
read_sbml_model, write_sbml_model, None),
@@ -414,39 +414,39 @@ def test_smbl_with_notes(data_directory, tmp_path):
}
metabolite_annotations = {
'2hb_e': {'sbo': 'SBO:0000247',
'inchi': 'InChI=1S/C4H8O3/c1-2-3(5)4(6)7/h3,5H,2H2,1H3,'
'(H,6,7)',
'chebi': 'CHEBI:1148'},
'inchi': ['InChI=1S/C4H8O3/c1-2-3(5)4(6)7/h3,5H,2H2,1H3,'
'(H,6,7)'],
'chebi': ['CHEBI:1148']},
'nad_e': {'sbo': 'SBO:0000247',
'inchi': 'InChI=1S/C21H27N7O14P2/c22-17-12-19('
'25-7-24-17)28(8-26-12)21-16(32)14(30)11('
'41-21)6-39-44(36,37)42-43(34,35)38-5-10-13(29)15('
'31)20(40-10)27-3-1-2-9(4-27)18('
'23)33/h1-4,7-8,10-11,13-16,20-21,29-32H,5-6H2,'
'(H5-,22,23,24,25,33,34,35,36,37)/p-1/t10-,'
'11-,13-,14-,15-,16-,20-,21-/m1/s1',
'chebi': 'CHEBI:57540'},
'h_e': {'sbo': 'SBO:0000247', 'inchi': 'InChI=1S/p+1/i/hH',
'chebi': 'CHEBI:24636'},
'inchi': ['InChI=1S/C21H27N7O14P2/c22-17-12-19('
'25-7-24-17)28(8-26-12)21-16(32)14(30)11('
'41-21)6-39-44(36,37)42-43(34,35)38-5-10-13(29)15('
'31)20(40-10)27-3-1-2-9(4-27)18('
'23)33/h1-4,7-8,10-11,13-16,20-21,29-32H,5-6H2,'
'(H5-,22,23,24,25,33,34,35,36,37)/p-1/t10-,'
'11-,13-,14-,15-,16-,20-,21-/m1/s1'],
'chebi': ['CHEBI:57540']},
'h_e': {'sbo': 'SBO:0000247', 'inchi': ['InChI=1S/p+1/i/hH'],
'chebi': ['CHEBI:24636']},
'2obut_e': {'sbo': 'SBO:0000247',
'inchi': 'InChI=1S/C4H6O3/c1-2-3(5)4(6)7/h2H2,1H3,(H,6,'
'7)/p-1',
'chebi': 'CHEBI:16763'},
'inchi': ['InChI=1S/C4H6O3/c1-2-3(5)4(6)7/h2H2,1H3,(H,6,'
'7)/p-1'],
'chebi': ['CHEBI:16763']},
'nadh_e': {'sbo': 'SBO:0000247',
'inchi': 'InChI=1S/C21H29N7O14P2/c22-17-12-19('
'25-7-24-17)28(8-26-12)21-16(32)14(30)11('
'41-21)6-39-44(36,37)42-43(34,35)38-5-10-13('
'29)15(31)20(40-10)27-3-1-2-9(4-27)18('
'23)33/h1,3-4,7-8,10-11,13-16,20-21,29-32H,2,'
'5-6H2,(H2,23,33)(H,34,35)(H,36,37)(H2,22,24,'
'25)/p-2/t10-,11-,13-,14-,15-,16-,20-,21-/m1/s1',
'chebi': 'CHEBI:57945'}
'inchi': ['InChI=1S/C21H29N7O14P2/c22-17-12-19('
'25-7-24-17)28(8-26-12)21-16(32)14(30)11('
'41-21)6-39-44(36,37)42-43(34,35)38-5-10-13('
'29)15(31)20(40-10)27-3-1-2-9(4-27)18('
'23)33/h1,3-4,7-8,10-11,13-16,20-21,29-32H,2,'
'5-6H2,(H2,23,33)(H,34,35)(H,36,37)(H2,22,24,'
'25)/p-2/t10-,11-,13-,14-,15-,16-,20-,21-/m1/s1'],
'chebi': ['CHEBI:57945']}
}
reaction_notes = {'CONFIDENCE_LEVEL': '4', 'NOTES': 'NCD',
'SUBSYSTEM': 'Propanoate metabolism',
'GENE_ASSOCIATION': '(HGNC:8546 and HGNC:8548) or'
' (HGNC:8547 and HGNC:8548)'}
reaction_annotations = {'sbo': 'SBO:0000176', 'ec-code': '1.1.1.27',
reaction_annotations = {'sbo': 'SBO:0000176', 'ec-code': ['1.1.1.27'],
'pubmed': ['10108', '21765']}

for met_id in metabolite_notes: