From dd49302bb6f8b2503cc8c9b7151ed852858e10a1 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Thu, 16 Jan 2014 17:23:31 -0800 Subject: [PATCH] Implement a encoded search to json-ld wrapper. An open question is encoded returns terms like "foo.bar" where bar is an attribute of an object name for foo. However since foo has no id associated with it, at best I would have a term with a blank node. For the moment I'm just deleting those terms. I'm not sure what a better solution might be. --- htsworkflow/submission/encoded.py | 22 ++++++++++ htsworkflow/submission/test/test_encoded.py | 45 +++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/htsworkflow/submission/encoded.py b/htsworkflow/submission/encoded.py index 05813fd..1228d38 100644 --- a/htsworkflow/submission/encoded.py +++ b/htsworkflow/submission/encoded.py @@ -277,6 +277,28 @@ class ENCODED: url = urlunparse(url.values()) return url + def search_jsonld(self, term, **kwargs): + '''Send search request to ENCODED + ''' + url = self.prepare_url('/search/') + result = self.get_json(url, searchTerm=term, **kwargs) + self.convert_search_to_jsonld(result) + return result + + def convert_search_to_jsonld(self, result): + '''Add the context to search result + + Also remove hard to handle nested attributes + e.g. remove object.term when we have no id + ''' + graph = result['@graph'] + for i, obj in enumerate(graph): + # suppress nested attributes + graph[i] = {k: v for k, v in obj.items() if '.' not in k} + + self.add_jsonld_context(result, self.prepare_url(result['@id'])) + return result + def validate(self, obj): obj_type = self.get_object_type(obj) schema_url = self.get_schema_url(obj) diff --git a/htsworkflow/submission/test/test_encoded.py b/htsworkflow/submission/test/test_encoded.py index 5483f6f..675d944 100644 --- a/htsworkflow/submission/test/test_encoded.py +++ b/htsworkflow/submission/test/test_encoded.py @@ -122,6 +122,51 @@ class TestEncoded(TestCase): self.assertEqual(obj['@context']['OBO'], 'http://purl.obolibrary.org/obo/') + def test_convert_search_to_jsonld(self): + example = {'count': {'biosamples': 2}, + 'portal_title': 'ENCODE', + 'title': 'Search', + 'notification': 'Success', + 'filters': [], + '@id': '/search/?searchTerm=wold', + '@type': ['search'], + 'facets': [], + '@graph': [{ + u'@id': u'/biosamples/ENCBS125ENC/', + u'@type': [u'biosample', u'item'], + u'accession': u'ENCBS125ENC', + u'award.rfa': u'ENCODE2-Mouse', + u'biosample_term_name': u'myocyte', + u'biosample_type': u'in vitro differentiated cells', + u'characterizations.length': [], + u'constructs.length': [], + u'lab.title': u'Barbara Wold, Caltech', + u'life_stage': u'unknown', + u'organism.name': u'mouse', + u'source.title': u'Barbara Wold', + u'status': u'CURRENT', + u'treatments.length': []}, + {u'@id': u'/biosamples/ENCBS126ENC/', + u'@type': [u'biosample', u'item'], + u'accession': u'ENCBS126ENC', + u'award.rfa': u'ENCODE2-Mouse', + u'biosample_term_name': u'myocyte', + u'biosample_type': u'in vitro differentiated cells', + u'characterizations.length': [], + u'constructs.length': [], + u'lab.title': u'Barbara Wold, Caltech', + u'life_stage': u'unknown', + u'organism.name': u'mouse', + u'source.title': u'Barbara Wold', + u'status': u'CURRENT', + u'treatments.length': []}, + ]} + + encode = ENCODED('test.encodedcc.org') + result = encode.convert_search_to_jsonld(example) + for obj in result['@graph']: + self.assertNotIn('award.rfa', obj) + def _verify_context(self, context, obj_type): for context_key in [None, obj_type]: for k in ENCODED_CONTEXT[context_key]: -- 2.30.2