Skip to content

Commit

Permalink
fixing #332
Browse files Browse the repository at this point in the history
  • Loading branch information
Zigur committed Jan 21, 2021
1 parent e2ef151 commit 15f33c7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion isatools/create/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def loads_ontology_annotation(ontology_annotation_dict):
if isinstance(ontology_annotation_dict.get("termSource", None), dict):
term_source = OntologySource(**ontology_annotation_dict["termSource"])
return OntologyAnnotation(
term=ontology_annotation_dict["term"], term_accession=ontology_annotation_dict["termAccession"],
term=ontology_annotation_dict["term"], term_accession=ontology_annotation_dict.get("termAccession", ''),
term_source=term_source
)

Expand Down
8 changes: 3 additions & 5 deletions isatools/isajson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,15 +1645,14 @@ def get_source(obj):
)

def get_characteristic(obj):
res = clean_nulls(
return clean_nulls(
{
"category": {"@id": id_gen(obj.category)} if obj.category else None,
# "category": get_value(o.category) if o.category else None,
"value": get_value(obj.value),
"unit": {"@id": id_gen(obj.unit)} if obj.unit else None
}
)
return res

def get_characteristics(obj):
return list(map(lambda x: get_characteristic(x), obj))
Expand All @@ -1667,11 +1666,10 @@ def get_value(obj):
raise ValueError("Unexpected value type found: " + type(obj))

def get_characteristic_category(obj): # TODO: Deal with Material Type
ont_ann = OntologyAnnotation(term=obj) if isinstance(obj, str) else obj
res = clean_nulls(
{
"@id": id_gen(ont_ann),
"characteristicType": get_ontology_annotation(ont_ann)
"@id": id_gen(obj),
"characteristicType": get_ontology_annotation(obj)
}
)
return res
Expand Down
16 changes: 10 additions & 6 deletions isatools/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def value(self):
def value(self, val):
if isinstance(val, str):
self.__value = val
raise ISAModelAttributeError('Comment.value must be a string')
else:
raise ISAModelAttributeError('Comment.value must be a string')

def __repr__(self):
return "isatools.model.Comment(name='{comment.name}', " \
Expand Down Expand Up @@ -2690,7 +2691,8 @@ def __init__(self, category=None, value=None, unit=None, comments=None):
self.__value = None
self.__unit = None

self.category = category
if category is not None:
self.category = category
self.value = value
self.unit = unit

Expand All @@ -2702,12 +2704,14 @@ def category(self):

@category.setter
def category(self, val):
if val is not None and not isinstance(val, (str, OntologyAnnotation)):
raise ISAModelAttributeError(
if isinstance(val, OntologyAnnotation) or val is None:
self.__category = val
elif isinstance(val, str):
self.__category = OntologyAnnotation(term=val)
else:
raise AttributeError(
'Characteristic.category must be either a string ot an OntologyAnnotation,'
' or None; got {0}:{1}'.format(val, type(val)))
else:
self.__category = val

@property
def value(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_create_models_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ def test_with_strings(self):
# log.info('Characteristic is {0}'.format(characteristic))
actual_json_characteristic = json.loads(json.dumps(characteristic, cls=CharacteristicEncoder))
expected_json_characteristic = {
'category': characteristic.category,
'category': {
'term': characteristic.category.term
},
'value': characteristic.value
}
self.assertEqual(ordered(actual_json_characteristic), ordered(expected_json_characteristic))
Expand Down
7 changes: 3 additions & 4 deletions tests/test_isajson.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ def test_isajson_with_strings_as_characteristic_category(self):
self.assertIsInstance(isa_j, str)
with open(os.path.join(utils.JSON_DATA_DIR, 'ISA-1', 'isa-test2.json'), 'w') as out_fp:
out_fp.write(isa_j)
# FIXME fix this
# with open(os.path.join(utils.JSON_DATA_DIR, 'ISA-1', 'isa-test2.json')) as in_fp:
# reverse_test_isa_investigation = isajson.load(in_fp)
# self.assertIsInstance(reverse_test_isa_investigation, Investigation)
with open(os.path.join(utils.JSON_DATA_DIR, 'ISA-1', 'isa-test2.json')) as in_fp:
reverse_test_isa_investigation = isajson.load(in_fp)
self.assertIsInstance(reverse_test_isa_investigation, Investigation)

0 comments on commit 15f33c7

Please sign in to comment.