Skip to content

Commit

Permalink
Started assay deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
terazus committed Jun 10, 2022
1 parent 2201651 commit 052ae27
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 27 deletions.
2 changes: 0 additions & 2 deletions isatools/isajson/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ def get_parameter_value(p_val_dict):
sample.derives_from.append(sources_dict[source_id_ref_json["@id"]])
except KeyError:
sample.derives_from = []

for study_process_json in study_json["processSequence"]:
process = Process(
id_=study_process_json["@id"],
Expand Down Expand Up @@ -434,7 +433,6 @@ def get_parameter_value(p_val_dict):

study.process_sequence.append(process)
process_dict[process.id] = process

for study_process_json in study_json["processSequence"]: # 2nd pass
try:
prev_proc = study_process_json["previousProcess"]["@id"]
Expand Down
27 changes: 27 additions & 0 deletions isatools/model/assay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from isatools.model.mixins import StudyAssayMixin
from isatools.model.ontology_annotation import OntologyAnnotation
from isatools.model.datafile import DataFile
from isatools.model.loader_indexes import loader_states as indexes


class Assay(Commentable, StudyAssayMixin, object):
Expand Down Expand Up @@ -192,3 +193,29 @@ def to_dict(self):
"dataFiles": [file.to_dict() for file in self.data_files],
"processSequence": [process.to_dict() for process in self.process_sequence]
}

def from_dict(self, assay):
self.technology_platform = assay.get('technologyPlatform', '')
self.filename = assay.get('filename', '')
self.load_comments(assay.get('comments', []))

# measurement type
measurement_type_data = assay.get('measurementType', None)
if measurement_type_data:
measurement_type = OntologyAnnotation()
measurement_type.from_dict(measurement_type_data)
self.measurement_type = measurement_type

# technology type
technology_type_data = assay.get('technologyType', None)
if technology_type_data:
technology_type = OntologyAnnotation()
technology_type.from_dict(technology_type_data)
self.technology_type = technology_type

# units categories
for unit_data in assay.get('unitCategories', []):
unit = OntologyAnnotation()
unit.from_dict(unit_data)
self.units.append(unit)
indexes.add_unit(unit)
2 changes: 2 additions & 0 deletions isatools/model/investigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from isatools.model.person import Person
from isatools.model.publication import Publication
from isatools.graphQL.models import IsaSchema
from isatools.model.loader_indexes import loader_states as indexes


class Investigation(Commentable, MetadataMixin, Identifiable, object):
Expand Down Expand Up @@ -259,6 +260,7 @@ def from_dict(self, investigation):
ontology_source = OntologySource('')
ontology_source.from_dict(ontology_source_data)
self.ontology_source_references.append(ontology_source)
indexes.add_term_source(ontology_source)

# people
for person_data in investigation.get('people', []):
Expand Down
25 changes: 21 additions & 4 deletions isatools/model/loader_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def init(self):
self.samples = {}
self.sources = {}
self.processes = {}
self.term_sources = {}
return init


Expand All @@ -46,7 +47,8 @@ def to_str(self):
"units: {indexes.units},\n\t"
"samples: {indexes.samples},\n\t"
"sources: {indexes.sources},\n\t"
"processes: {indexes.processes}").format(indexes=self)
"processes: {indexes.processes},\n\t"
"term_sources: {indexes.term_sources}").format(indexes=self)
return to_str


Expand All @@ -70,11 +72,24 @@ def resolve(self, id_):


def make_add_resolver(field_target):
def resolve(self, id_):
self.add_item(field_target, id_)
def resolve(self, item):
self.add_item(field_target, item)
return resolve


def make_add_term_source():
def add_term_source(self, item):
self.term_sources[item.name] = item
return add_term_source


def make_get_term_source():
def get_term_source(self, name):
print(self.term_sources.keys())
return self.term_sources[name]
return get_term_source


FIELDS = {
"characteristic_category": 'characteristic_categories',
"factor": 'factors',
Expand All @@ -91,7 +106,9 @@ def resolve(self, id_):
'reset_store': make_init(),
'add_item': make_add_method(),
'get_item': make_get_method(),
'__str__': make_print()
'__str__': make_print(),
'get_term_source': make_get_term_source(),
'add_term_source': make_add_term_source()
}

for field_name in FIELDS:
Expand Down
13 changes: 4 additions & 9 deletions isatools/model/ontology_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from isatools.model.comments import Commentable, Comment
from isatools.model.ontology_source import OntologySource
from isatools.model.identifiable import Identifiable
from isatools.model.loader_indexes import loader_states as indexes


class OntologyAnnotation(Commentable, Identifiable):
Expand Down Expand Up @@ -124,12 +125,6 @@ def from_dict(self, ontology_annotation):
self.term_accession = ontology_annotation.get('termAccession', '')
self.load_comments(ontology_annotation.get('comments', []))

if 'termSource' in ontology_annotation:
source = ontology_annotation['termSource']
if isinstance(source, str):
self.term_source = OntologySource(name=source)
else:
term_source = OntologySource('')
term_source.from_dict(source)
self.term_source = term_source

if 'termSource' in ontology_annotation and ontology_annotation['termSource']:
source = indexes.get_term_source(ontology_annotation['termSource'])
self.term_source = source
6 changes: 6 additions & 0 deletions isatools/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,10 @@ def from_dict(self, study):
pass

# Assay
for assay_data in study.get('assays', []):
indexes.processes = []
assay = Assay()
assay.from_dict(assay_data)
self.assays.append(assay)

indexes.reset_store()
40 changes: 40 additions & 0 deletions tests/model/test_assay.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,43 @@ def test_to_dict(self):
'processSequence': []
}
self.assertEqual(expected_dict, assay.to_dict())

assay = Assay()
assay.from_dict(expected_dict)
self.assertEqual(assay.to_dict(), expected_dict)

expected_dict['unitCategories'] = [{
'@id': 'unit_ID',
'annotationValue': 'my_unit',
'termSource': '',
'termAccession': '',
'comments': []
}]
assay.from_dict(expected_dict)
self.assertEqual(assay.to_dict(), expected_dict)


























3 changes: 2 additions & 1 deletion tests/model/test_investigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_dict(self):
'status': {
'@id': '123',
'annotationValue': 'OA',
'termSource': '',
'termSource': 'an ontology source',
'termAccession': '',
'comments': []},
'title': '',
Expand All @@ -196,3 +196,4 @@ def test_dict(self):
}
investigation.from_dict(expected_dict)
self.assertEqual(investigation.to_dict(), expected_dict)
self.assertIsInstance(investigation.publications[0].status.term_source, OntologySource)
3 changes: 2 additions & 1 deletion tests/model/test_load_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ def test_methods(self):
"units: {},\n\t"
"samples: {},\n\t"
"sources: {},\n\t"
"processes: {}")
"processes: {},\n\t"
"term_sources: {}")
self.assertEqual(expected_string, str(indexes))
15 changes: 5 additions & 10 deletions tests/model/test_ontology_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

from isatools.model import OntologySource, OntologyAnnotation, Commentable
from isatools.model.loader_indexes import loader_states as indexes


class TestOntologyAnnotation(TestCase):
Expand Down Expand Up @@ -99,16 +100,10 @@ def test_dict(self):
expected_dict['termSource'] = 'test_source_name'
self.assertEqual(ontology_annotation.to_dict(), expected_dict)

indexes.term_sources = {
'test_source_name': OntologySource('test_source_name')
}
ontology_annotation = OntologyAnnotation()
ontology_annotation.from_dict(expected_dict)
self.assertEqual(ontology_annotation.to_dict(), expected_dict)

expected_dict['termSource'] = {
'name': 'a source',
'file': 'a_file.txt',
'version': '1.0',
'description': "Hello world",
'comments': []
}
ontology_annotation.from_dict(expected_dict)
self.assertEqual(ontology_annotation.to_dict()['termSource'], expected_dict['termSource']['name'])
self.assertIsInstance(ontology_annotation.term_source, OntologySource)

0 comments on commit 052ae27

Please sign in to comment.