Skip to content

Commit

Permalink
extending assay deserialiser
Browse files Browse the repository at this point in the history
  • Loading branch information
terazus committed Jun 10, 2022
1 parent 052ae27 commit 8c4f51a
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 41 deletions.
5 changes: 5 additions & 0 deletions isatools/isajson/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"],
Expand All @@ -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-", "")
Expand Down Expand Up @@ -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"],
Expand Down
21 changes: 20 additions & 1 deletion isatools/model/assay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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)
8 changes: 8 additions & 0 deletions isatools/model/datafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
14 changes: 8 additions & 6 deletions isatools/model/loader_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def init(self):
self.sources = {}
self.processes = {}
self.term_sources = {}
self.data_files = {}
return init


Expand All @@ -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


Expand Down Expand Up @@ -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

Expand All @@ -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 = {
Expand All @@ -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()

2 changes: 1 addition & 1 deletion isatools/model/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion isatools/model/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 37 additions & 22 deletions tests/model/test_assay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

19 changes: 18 additions & 1 deletion tests/model/test_datafile.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):

Expand Down
3 changes: 2 additions & 1 deletion tests/model/test_load_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
24 changes: 16 additions & 8 deletions tests/model/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': []
}
}
]
}
Expand Down

0 comments on commit 8c4f51a

Please sign in to comment.