Skip to content

Commit 8173fbb

Browse files
author
oerc0042
committed
sanitizing json2jsonld serialization and fixing failing tests resulting from alterations to isatools/model and isatools/database
1 parent 2daeaba commit 8173fbb

13 files changed

Lines changed: 94 additions & 67 deletions

isatools/database/models/assay.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Assay(Base):
5757
def to_json(self):
5858
characteristic_categories = get_characteristic_categories(self.characteristic_categories)
5959
return {
60+
"@id": str(self.assay_id),
6061
"filename": self.filename,
6162
"technologyPlatform": self.technology_platform,
6263
"measurementType": self.measurement_type.to_json(),

isatools/database/models/ontology_annotation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def to_json(self):
5757
return {
5858
"@id": self.ontology_annotation_id,
5959
"annotationValue": self.annotation_value,
60-
"termSource": self.term_source_id if self.term_source_id else None,
60+
"termSource": self.term_source.name if self.term_source else None,
6161
"termAccession": self.term_accession,
6262
"comments": [c.to_json() for c in self.comments],
6363
}

isatools/database/models/ontology_source.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def to_json(self) -> dict:
3333
:return: The dictionary representation of the object taken from the database
3434
"""
3535
return {
36-
"id": self.ontology_source_id,
36+
"@id": self.ontology_source_id,
3737
"name": self.name,
3838
"file": self.file,
3939
"version": self.version,
@@ -56,11 +56,11 @@ def to_sql(self, session) -> OntologySource:
5656
5757
:return: The SQLAlchemy object ready to be committed to the database session.
5858
"""
59-
ontology_source = session.get(OntologySource, self.name)
59+
ontology_source = session.get(OntologySource, self.id)
6060
if ontology_source:
6161
return ontology_source
6262
ontology_source = OntologySource(
63-
ontology_source_id=self.name,
63+
ontology_source_id=self.id,
6464
name=self.name,
6565
file=self.file,
6666
version=self.version,

isatools/database/models/study.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def to_json(self) -> dict:
7575
"""
7676
characteristics_categories = get_characteristic_categories(self.characteristic_categories)
7777
return {
78+
"@id": str(self.study_id),
7879
"title": self.title,
7980
"filename": self.filename,
8081
"identifier": self.identifier,

isatools/model/assay.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from isatools.model.comments import Commentable
22
from isatools.model.datafile import DataFile
3+
from isatools.model.identifiable import Identifiable
34
from isatools.model.loader_indexes import loader_states as indexes
45
from isatools.model.material import Material
56
from isatools.model.mixins import StudyAssayMixin
67
from isatools.model.ontology_annotation import OntologyAnnotation
78
from isatools.model.process import Process
89

910

10-
class Assay(Commentable, StudyAssayMixin, object):
11+
class Assay(Commentable, Identifiable, StudyAssayMixin, object):
1112
"""An Assay represents a test performed either on material taken from a
1213
subject or on a whole initial subject, producing qualitative or
1314
quantitative
@@ -38,6 +39,7 @@ class Assay(Commentable, StudyAssayMixin, object):
3839

3940
def __init__(
4041
self,
42+
id_="",
4143
measurement_type=None,
4244
technology_type=None,
4345
technology_platform="",
@@ -72,6 +74,7 @@ def __init__(
7274
self.__technology_platform = technology_platform
7375
self.data_files = data_files or []
7476

77+
self.id = id_
7578
@property
7679
def measurement_type(self):
7780
""":obj:`OntologyAnnotation: an ontology annotation representing the
@@ -200,6 +203,7 @@ def __ne__(self, other):
200203

201204
def to_dict(self, ld=False):
202205
assay = {
206+
"@id": self.id,
203207
"measurementType": self.measurement_type.to_dict(ld=ld) if self.measurement_type else "",
204208
"technologyType": self.technology_type.to_dict(ld=ld) if self.technology_type else "",
205209
"technologyPlatform": self.technology_platform,
@@ -217,6 +221,7 @@ def to_dict(self, ld=False):
217221
return self.update_isa_object(assay, ld)
218222

219223
def from_dict(self, assay, isa_study):
224+
self.id = assay.get("@id", "")
220225
self.technology_platform = assay.get("technologyPlatform", "")
221226
self.filename = assay.get("filename", "")
222227
self.load_comments(assay.get("comments", []))

isatools/model/mixins.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818

1919

2020
class MetadataMixin(metaclass=ABCMeta):
21-
"""Abstract mixin class to contain metadata fields found in Investigation
21+
""" Abstract mixin class to contain metadata fields found in Investigation
2222
and Study sections of ISA
2323
2424
Attributes:
25-
identifier: An identifier associated with objects of this class.
26-
title: A title associated with objects of this class.
27-
description: A description associated with objects of this class.
28-
submission_date: A submission date associated with objects of this
25+
identifier: An identifier associated with objects of this class.
26+
title: A title associated with objects of this class.
27+
description: A description associated with objects of this class.
28+
submission_date: A submission date associated with objects of this
2929
class.
30-
public_release_date: A submission date associated with objects of this
30+
public_release_date: A submission date associated with objects of this
3131
class.
3232
"""
3333

@@ -161,24 +161,23 @@ def contacts(self, val):
161161

162162

163163
class StudyAssayMixin(metaclass=ABCMeta):
164-
"""Abstract mixin class to contain common fields found in Study
164+
""" Abstract mixin class to contain common fields found in Study
165165
and Assay sections of ISA
166166
167167
Attributes:
168-
filename: A field to specify the file for compatibility with ISA-Tab.
169-
materials: Materials associated with the Study or Assay.
170-
sources: Sources associated with the Study or Assay.
171-
samples: Samples associated with the Study or Assay.
172-
other_material: Other Material types associated with the Study or
173-
Assay.
174-
units: A list of Units used in the annotation of materials.
175-
characteristic_categories-: A list of OntologyAnnotation used in
176-
the annotation of material characteristics.
177-
process_sequence: A list of Process objects representing the
178-
experimental graphs.
179-
comments: Comments associated with instances of this class.
180-
graph: Graph representation of the experimental graph.
181-
168+
filename: A field to specify the file for compatibility with ISA-Tab.
169+
materials: Materials associated with the Study or Assay.
170+
sources: Sources associated with the Study or Assay.
171+
samples: Samples associated with the Study or Assay.
172+
other_material: Other Material types associated with the Study or
173+
Assay.
174+
units: A list of Units used in the annotation of materials.
175+
characteristic_categories-: A list of OntologyAnnotation used in
176+
the annotation of material characteristics.
177+
process_sequence: A list of Process objects representing the
178+
experimental graphs.
179+
comments: Comments associated with instances of this class.
180+
graph: Graph representation of the experimental graph.
182181
"""
183182

184183
def __init__(
@@ -252,7 +251,7 @@ def sources(self, val):
252251
raise AttributeError("{}.sources must be iterable containing Sources".format(type(self).__name__))
253252

254253
def add_source(self, name="", characteristics=None, comments=None):
255-
"""Adds a new source to the source materials list.
254+
""" Adds a new source to the source materials list.
256255
:param string name: Source name
257256
:param list[Characteristics] characteristics: Characteristics about the Source
258257
:param list comments: Comments about the Source
@@ -261,7 +260,7 @@ def add_source(self, name="", characteristics=None, comments=None):
261260
self.sources.append(s)
262261

263262
def yield_sources(self, name=None):
264-
"""Gets an iterator of matching sources for a given name.
263+
""" Gets an iterator of matching sources for a given name.
265264
266265
Args:
267266
name: Source name
@@ -273,7 +272,7 @@ def yield_sources(self, name=None):
273272
return filter(lambda x: x, self.sources) if name is None else filter(lambda x: x.name == name, self.sources)
274273

275274
def get_source(self, name):
276-
"""Gets the first matching source material for a given name.
275+
""" Gets the first matching source material for a given name.
277276
278277
Args:
279278
name: Source name
@@ -288,7 +287,7 @@ def get_source(self, name):
288287
return None
289288

290289
def yield_sources_by_characteristic(self, characteristic=None):
291-
"""Gets an iterator of matching sources for a given characteristic.
290+
""" Gets an iterator of matching sources for a given characteristic.
292291
293292
Args:
294293
characteristic: Source characteristic
@@ -302,7 +301,7 @@ def yield_sources_by_characteristic(self, characteristic=None):
302301
return filter(lambda x: characteristic in x.characteristics, self.sources)
303302

304303
def get_source_by_characteristic(self, characteristic):
305-
"""Gets the first matching source material for a given characteristic.
304+
""" Gets the first matching source material for a given characteristic.
306305
307306
Args:
308307
characteristic: Source characteristic
@@ -318,7 +317,7 @@ def get_source_by_characteristic(self, characteristic):
318317
return None
319318

320319
def get_source_names(self):
321-
"""Gets all of the source names.
320+
""" Gets all the source names.
322321
323322
Returns:
324323
:obj:`list` of str.
@@ -340,7 +339,7 @@ def samples(self, val):
340339
raise AttributeError("{}.samples must be iterable containing Samples".format(type(self).__name__))
341340

342341
def add_sample(self, name="", characteristics=None, factor_values=None, derives_from=None, comments=None):
343-
"""Adds a new sample to the sample materials list.
342+
""" Adds a new sample to the sample materials list.
344343
:param string name: Sample name
345344
:param list[Characteristics] characteristics: Characteristics about the sample
346345
:param list comments: Comments about the sample
@@ -358,14 +357,14 @@ def add_sample(self, name="", characteristics=None, factor_values=None, derives_
358357
self.samples.append(sample)
359358

360359
def yield_samples(self, name=None):
361-
"""Gets an iterator of matching samples for a given name.
360+
""" Gets an iterator of matching samples for a given name.
362361
:param string name: Sample name
363362
:return: object:`filter` of object:`Source` that can be iterated on. If name is None, yields all samples.
364363
"""
365364
return filter(lambda x: x, self.samples) if name is None else filter(lambda x: x.name == name, self.samples)
366365

367366
def get_sample(self, name):
368-
"""Gets the first matching sample material for a given name.
367+
""" Gets the first matching sample material for a given name.
369368
370369
Args:
371370
name: Sample name
@@ -380,7 +379,7 @@ def get_sample(self, name):
380379
return None
381380

382381
def yield_samples_by_characteristic(self, characteristic=None):
383-
"""Gets an iterator of matching samples for a given characteristic.
382+
""" Gets an iterator of matching samples for a given characteristic.
384383
385384
Args:
386385
characteristic: Sample characteristic
@@ -395,7 +394,7 @@ def yield_samples_by_characteristic(self, characteristic=None):
395394
return filter(lambda x: characteristic in x.characteristics, self.samples)
396395

397396
def get_sample_by_characteristic(self, characteristic):
398-
"""Gets the first matching sample material for a given characteristic.
397+
""" Gets the first matching sample material for a given characteristic.
399398
400399
Args:
401400
characteristic: Sample characteristic
@@ -412,7 +411,7 @@ def get_sample_by_characteristic(self, characteristic):
412411
return None
413412

414413
def yield_samples_by_factor_value(self, factor_value=None):
415-
"""Gets an iterator of matching samples for a given factor_value.
414+
""" Gets an iterator of matching samples for a given factor_value.
416415
417416
Args:
418417
factor_value: Sample factor value
@@ -427,7 +426,7 @@ def yield_samples_by_factor_value(self, factor_value=None):
427426
return filter(lambda x: factor_value in x.factor_values, self.samples)
428427

429428
def get_sample_by_factor_value(self, factor_value):
430-
"""Gets the first matching sample material for a given factor_value.
429+
""" Gets the first matching sample material for a given factor_value.
431430
432431
Args:
433432
factor_value: Sample factor value
@@ -444,7 +443,7 @@ def get_sample_by_factor_value(self, factor_value):
444443
return None
445444

446445
def get_sample_names(self):
447-
"""Gets all of the sample names.
446+
""" Gets all the sample names.
448447
449448
Returns:
450449
:obj:`list` of str.
@@ -466,7 +465,7 @@ def other_material(self, val):
466465
raise AttributeError("{}.other_material must be iterable containing Materials".format(type(self).__name__))
467466

468467
def yield_materials_by_characteristic(self, characteristic=None):
469-
"""Gets an iterator of matching materials for a given characteristic.
468+
""" Gets an iterator of matching materials for a given characteristic.
470469
471470
Args:
472471
characteristic: Material characteristic
@@ -481,7 +480,7 @@ def yield_materials_by_characteristic(self, characteristic=None):
481480
return filter(lambda x: characteristic in x.characteristics, self.other_material)
482481

483482
def get_material_by_characteristic(self, characteristic):
484-
"""Gets the first matching material material for a given
483+
""" Gets the first matching material for a given
485484
characteristic.
486485
487486
Args:

isatools/model/ontology_source.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Any, List
22

33
from isatools.model.comments import Comment, Commentable
4+
from isatools.model.identifiable import Identifiable
45

56

6-
class OntologySource(Commentable):
7+
class OntologySource(Commentable, Identifiable):
78
"""An OntologySource describes the resource from which the value of an
89
OntologyAnnotation is derived from.
910
@@ -18,10 +19,10 @@ class OntologySource(Commentable):
1819
"""
1920

2021
def __init__(
21-
self, name: str, file: str = "", version: str = "", description: str = "", comments: List[Comment] = None
22+
self, id_="", name: str = "", file: str = "", version: str = "", description: str = "", comments: List[Comment] = None
2223
):
2324
super().__init__(comments)
24-
25+
self.id = id_
2526
self.__name = name
2627
self.__file = file
2728
self.__version = version
@@ -119,6 +120,7 @@ def __ne__(self, other):
119120

120121
def to_dict(self, ld=False):
121122
ontology_source_ref = {
123+
"@id": self.id,
122124
"name": self.name,
123125
"file": self.file,
124126
"version": self.version,
@@ -128,6 +130,7 @@ def to_dict(self, ld=False):
128130
return self.update_isa_object(ontology_source_ref, ld=ld)
129131

130132
def from_dict(self, ontology_source):
133+
self.id = ontology_source.get("@id","")
131134
self.name = ontology_source["name"] if "name" in ontology_source else ""
132135
self.file = ontology_source["file"] if "file" in ontology_source else ""
133136
self.version = ontology_source["version"] if "version" in ontology_source else ""

isatools/model/study.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from isatools.model.assay import Assay
55
from isatools.model.comments import Commentable
66
from isatools.model.factor_value import StudyFactor
7+
from isatools.model.identifiable import Identifiable
78
from isatools.model.loader_indexes import loader_states as indexes
89
from isatools.model.logger import log
910
from isatools.model.mixins import MetadataMixin, StudyAssayMixin
@@ -17,7 +18,7 @@
1718
from isatools.model.source import Source
1819

1920

20-
class Study(Commentable, StudyAssayMixin, MetadataMixin, object):
21+
class Study(Commentable, Identifiable, StudyAssayMixin, MetadataMixin, object):
2122
"""Study is the central unit, containing information on the subject under
2223
study, its characteristics and any treatments applied.
2324
@@ -396,6 +397,7 @@ def to_dict(self, ld=False):
396397

397398
def from_dict(self, study):
398399
indexes.reset_process()
400+
self.id = study.get("@id", "")
399401
self.filename = study.get("filename", "")
400402
self.identifier = study.get("identifier", "")
401403
self.title = study.get("title", "")

0 commit comments

Comments
 (0)