diff --git a/isatools/isajson/load.py b/isatools/isajson/load.py index 8468245b..ad763e4c 100644 --- a/isatools/isajson/load.py +++ b/isatools/isajson/load.py @@ -473,6 +473,7 @@ def get_parameter_value(p_val_dict): ) units_dict[unit.id] = unit assay.units.append(unit) + data_dict = dict() for data_json in assay_json["dataFiles"]: data_file = DataFile( @@ -487,9 +488,11 @@ def get_parameter_value(p_val_dict): except KeyError: data_file.derives_from = None assay.data_files.append(data_file) + for sample_json in assay_json["materials"]["samples"]: sample = samples_dict[sample_json["@id"]] assay.samples.append(sample) + for assay_characteristics_category_json in assay_json["characteristicCategories"]: characteristic_category = OntologyAnnotation( # id_=assay_characteristics_category_json["characteristicType"]["@id"], @@ -501,6 +504,7 @@ def get_parameter_value(p_val_dict): ) study.characteristic_categories.append(characteristic_category) categories_dict[characteristic_category.id] = characteristic_category + other_materials_dict = dict() for other_material_json in assay_json["materials"]["otherMaterials"]: material_name = other_material_json["name"].replace("labeledextract-", "").replace("extract-", "") @@ -542,6 +546,7 @@ def get_parameter_value(p_val_dict): material.characteristics.append(characteristic) assay.other_material.append(material) other_materials_dict[material.id] = material + for assay_process_json in assay_json["processSequence"]: process = Process( id_=assay_process_json["@id"], diff --git a/isatools/model/assay.py b/isatools/model/assay.py index faafa7ea..c0ef808c 100644 --- a/isatools/model/assay.py +++ b/isatools/model/assay.py @@ -187,7 +187,7 @@ def to_dict(self): "unitCategories": [unit.to_dict() for unit in self.units], "comments": [comment.to_dict() for comment in self.comments], "materials": { - "samples": [sample.to_dict() for sample in self.samples], + "samples": [{"@id": sample.id} for sample in self.samples], "otherMaterials": [mat.to_dict() for mat in self.other_material] }, "dataFiles": [file.to_dict() for file in self.data_files], @@ -219,3 +219,22 @@ def from_dict(self, assay): unit.from_dict(unit_data) self.units.append(unit) indexes.add_unit(unit) + + # data files + indexes.data_files = {} + for data_file_data in assay.get('dataFiles', []): + data_file = DataFile() + data_file.from_dict(data_file_data) + self.data_files.append(data_file) + indexes.add_data_file(data_file) + + # samples + for sample_data in assay.get('materials', {}).get('samples', []): + self.samples.append(indexes.get_sample(sample_data['@id'])) + + # characteristic categories + for characteristic_category_data in assay.get('characteristicCategories', []): + characteristic_category = OntologyAnnotation() + characteristic_category.from_dict(characteristic_category_data['characteristicType']) + self.characteristic_categories.append(characteristic_category) + indexes.add_characteristic_category(characteristic_category) diff --git a/isatools/model/datafile.py b/isatools/model/datafile.py index 2e9b3444..bd001999 100644 --- a/isatools/model/datafile.py +++ b/isatools/model/datafile.py @@ -105,6 +105,14 @@ def to_dict(self): "comments": [comment.to_dict() for comment in self.comments] } + def from_dict(self, data_file): + self.id = data_file.get('@id', '') + self.filename = data_file.get('name', '') + self.label = data_file.get('type', '') + self.load_comments(data_file.get('comments', [])) + + # TODO : missing generated_from property in dump/load methods + class RawDataFile(DataFile): """Represents a raw data file in an experimental graph.""" diff --git a/isatools/model/loader_indexes.py b/isatools/model/loader_indexes.py index c552d7d9..b1645d37 100644 --- a/isatools/model/loader_indexes.py +++ b/isatools/model/loader_indexes.py @@ -34,6 +34,7 @@ def init(self): self.sources = {} self.processes = {} self.term_sources = {} + self.data_files = {} return init @@ -48,7 +49,8 @@ def to_str(self): "samples: {indexes.samples},\n\t" "sources: {indexes.sources},\n\t" "processes: {indexes.processes},\n\t" - "term_sources: {indexes.term_sources}").format(indexes=self) + "term_sources: {indexes.term_sources},\n\t" + "data_files: {indexes.data_files}").format(indexes=self) return to_str @@ -85,7 +87,6 @@ def add_term_source(self, item): 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 @@ -98,7 +99,8 @@ def get_term_source(self, name): "unit": "units", "sample": "samples", "source": "sources", - 'process': 'processes' + 'process': 'processes', + 'data_file': 'data_files' } methods = { @@ -117,10 +119,10 @@ def get_term_source(self, name): methods['add_%s' % field_name] = make_add_resolver(field) # parameters of type are 1. class name 2. inheritance as tuple 3. methods and attributes -Store = type('LoaderStore', (), methods) -loader_states = Store() +LoaderStore = type('LoaderStore', (), methods) +loader_states = LoaderStore() def new_store(): - return Store() + return LoaderStore() diff --git a/isatools/model/sample.py b/isatools/model/sample.py index 914237f4..ef7c5154 100644 --- a/isatools/model/sample.py +++ b/isatools/model/sample.py @@ -156,7 +156,7 @@ def to_dict(self): def from_dict(self, sample): self.id = sample.get('@id', '') - self.name = sample.get('name', '') + self.name = sample.get('name', '').replace('sample-', '-') self.load_comments(sample.get('comments', [])) # characteristics diff --git a/isatools/model/source.py b/isatools/model/source.py index 7d5edec5..db99bd01 100644 --- a/isatools/model/source.py +++ b/isatools/model/source.py @@ -105,7 +105,7 @@ def to_dict(self): def from_dict(self, source): self.id = source.get('@id', '') - self.name = source.get('name', '') + self.name = source.get('name', '').replace("source-", "") self.load_comments(source.get('comments', [])) # characteristics diff --git a/tests/model/test_assay.py b/tests/model/test_assay.py index 46571ab0..8b3b21bb 100644 --- a/tests/model/test_assay.py +++ b/tests/model/test_assay.py @@ -3,6 +3,9 @@ from isatools.model.assay import Assay from isatools.model.datafile import DataFile from isatools.model.ontology_annotation import OntologyAnnotation +from isatools.model.ontology_source import OntologySource +from isatools.model.sample import Sample +from isatools.model.loader_indexes import loader_states as indexes class TestAssay(TestCase): @@ -153,28 +156,40 @@ def test_to_dict(self): assay.from_dict(expected_dict) self.assertEqual(assay.to_dict(), expected_dict) + expected_dict['materials']['samples'] = [{"@id": 'my_sample'}] + indexes.samples = {'my_sample': Sample(id_='my_sample')} + assay = Assay() + assay.from_dict(expected_dict) + self.assertEqual(assay.to_dict(), expected_dict) + # Data Files + expected_dict['dataFiles'] = [ + { + "@id": 'my_data_file', + "name": "filename", + "type": "RawDataFile", + "comments": [] + } + ] + assay = Assay() + assay.from_dict(expected_dict) + self.assertEqual(assay.to_dict(), expected_dict) - - - - - - - - - - - - - - - - - - - - - - + # Characteristic Categories + expected_dict['characteristicCategories'] = [ + { + '@id': '#characteristic_category/test_id', + 'characteristicType': { + '@id': 'test_id', + 'annotationValue': 'test_term', + 'termSource': 'term_source1', + 'termAccession': '', + 'comments': [] + } + } + ] + indexes.term_sources = {'term_source1': OntologySource(name='term_source1')} + assay = Assay() + assay.from_dict(expected_dict) + self.assertEqual(assay.to_dict(), expected_dict) diff --git a/tests/model/test_datafile.py b/tests/model/test_datafile.py index 2329a7b2..3755596d 100644 --- a/tests/model/test_datafile.py +++ b/tests/model/test_datafile.py @@ -1,5 +1,5 @@ from unittest import TestCase -from re import sub, compile +from re import sub from isatools.model.datafile import * from isatools.model.sample import Sample @@ -67,6 +67,23 @@ def test_equalities(self): self.assertTrue(first_datafile == second_datafile) self.assertTrue(self.datafile != first_datafile) + def test_from_dict(self): + expected_dict = { + "@id": 'my_data_file', + "name": "filename", + "type": "RawDataFile", + "comments": [] + } + data_file = DataFile() + data_file.from_dict(expected_dict) + self.assertEqual(data_file.to_dict(), expected_dict) + + raw_data_file = RawDataFile() + raw_data_file.from_dict(expected_dict) + self.assertEqual(raw_data_file.to_dict(), expected_dict) + self.assertEqual(raw_data_file.to_dict(), data_file.to_dict()) + self.assertNotEqual(raw_data_file, data_file) + class TestSubDataFile(TestCase): diff --git a/tests/model/test_load_indexes.py b/tests/model/test_load_indexes.py index 42a8ddb5..d6f68f78 100644 --- a/tests/model/test_load_indexes.py +++ b/tests/model/test_load_indexes.py @@ -57,5 +57,6 @@ def test_methods(self): "samples: {},\n\t" "sources: {},\n\t" "processes: {},\n\t" - "term_sources: {}") + "term_sources: {},\n\t" + "data_files: {}") self.assertEqual(expected_string, str(indexes)) diff --git a/tests/model/test_study.py b/tests/model/test_study.py index 8d809607..0bfd18fc 100644 --- a/tests/model/test_study.py +++ b/tests/model/test_study.py @@ -449,20 +449,28 @@ def test_dict(self): { "characteristicCategories": [ { - "@id": "my_cat", - "annotationValue": "value", - "termAccession": "123", - "comments": [] + '@id': '#characteristic_category/test_id', + 'characteristicType': { + '@id': 'test_id', + 'annotationValue': 'test_term', + 'termSource': '', + 'termAccession': '', + 'comments': [] + } } ] }, { "characteristicCategories": [ { - "@id": "my_cat2", - "annotationValue": "value2", - "termAccession": "456", - "comments": [] + '@id': '#characteristic_category/test_id', + 'characteristicType': { + '@id': 'my_cat2', + 'annotationValue': 'value2', + 'termSource': '', + 'termAccession': '456', + 'comments': [] + } } ] }