def _to_json(rec: etl.Record): """Converts a record to a formatted `Relationship` in JSON form. Args: rec: A table record Returns: A new record containing a single field, which is the JSON object """ coding = Coding( system=_SYSTEM, code=rec['source.CODE'], display=rec['source.CODE_TEXT'], version=rec.get('version') ) parent = Coding( system=_SYSTEM, code=rec['source.IMMEDIATE_PARENT'], display=rec['target.CODE_TEXT'], version=rec.get('version') ) relationships = [ Relationship('subsumes', coding, parent), Relationship('specializes', parent, coding) ] schema = RelationshipSchema() return map(lambda relationship: [json.dumps(schema.dump(relationship))], relationships)
def _to_json(rec): """ Convert record in table to Relationship as a JSON object Record is expected to have the following fields: [ source.CODE, source.STR, target.CODE, target.STR] Args: rec: A table record Returns: A new record containing a single field, which is the JSON object """ source = Coding(system="http://snomed.info/sct", code=rec['sourceId'], display=rec['source.term'], version=rec['effectiveTime'] if rec['versioned'] else None) target = Coding(system="http://snomed.info/sct", code=rec['destinationId'], display=rec['target.term'], version=rec['effectiveTime'] if rec['versioned'] else None) relationship = Relationship('subsumes', source, target) schema = RelationshipSchema() return [json.dumps(schema.dump(relationship))]
def test_to_relationship(): "A record is converted to an index" rec = ['target', 'source'] idx = 1 relationship = _to_relationship(rec, idx, equivalence='subsumes') eq_( relationship, Relationship( equivalence='subsumes', source=Coding(system="http://www.broadinstitute.org/gsea/msigdb", code='source', display='source'), target=Coding(system="http://www.broadinstitute.org/gsea/msigdb", code='target', display='target')))
def _to_coding(term, system): """Converts a term into a `Coding`. Args: term: A `pronto.Term` Returns: a `termlink.models.Coding` """ _id = term.id if ':' in _id: if _id.endswith(':') and validators.url(_id[:-1]): system = _id[:-1] code = None else: parts = _id.rsplit(':', 1) if validators.url(parts[0]): system = parts[0] code = parts[1] else: system = system code = parts[1] else: system = system code = _id return Coding(system=system, code=code, display=term.name)
def test_to_json(): """Checks that a source, equivalence and target and properly converted""" system = "http://snomed.info/sct" source = Term(id='SNOMEDCT_US:735938006', name='Acute headache') equivalence = 'subsumes' target = Term(id='SNOMEDCT_US:25064002', name='Headache') res = _to_relationship(source, equivalence, target, system) exp = Relationship(equivalence='subsumes', source=Coding(system=system, code='735938006', display='Acute headache'), target=Coding(system=system, code='25064002', display='Headache')) eq_(exp, res)
def test_to_relationship(): """Checks that a source, equivalence and target and properly converted""" source = Term(id='HP:0000107', name='Renal cyst') equivalence = 'subsumes' target = Term(id='HP:0000003', name='Multicystic kidney dysplasia') res = _to_relationship(source, equivalence, target) exp = Relationship( equivalence='subsumes', source=Coding(system='http://www.human-phenotype-ontology.org/', code='0000107', display='Renal cyst'), target=Coding(system='http://www.human-phenotype-ontology.org/', code='0000003', display='Multicystic kidney dysplasia')) eq_(exp, res)
def test_to_coding_without_colon(): """Checks that a term without a ':' is properly converted""" system = "http://snomed.info/sct" term = Term(id='25064002', name='Headache') res = _to_coding(term, system) exp = Coding(system=system, code='25064002', display='Headache') eq_(exp, res)
def test_to_coding_with_system_and_code(): """Checks that a term with a system and code identifier is properly converted""" system = "http://snomed.info/sct" term = Term(id='http://snomed.info/sct:25064002', name='Headache') res = _to_coding(term, system) exp = Coding(system=system, code='25064002', display='Headache') eq_(exp, res)
def test_to_coding_with_system(): """Checks that a term with a system identifier is properly converted""" system = "http://snomed.info/sct" term = Term(id='http://snomed.info/sct:', name='SNOMED-CT') res = _to_coding(term, system) exp = Coding(system=system, display='SNOMED-CT') eq_(exp, res)
def test_to_coding(): """Checks that a term is properly converted to a coding""" term = Term(id='HP:0000001', name='All') res = _to_coding(term) exp = Coding(system='http://www.human-phenotype-ontology.org/', code='0000001', display='All') ok_(exp == res)
def _to_coding(term): """Converts a term into a `Coding`. Args: term: A `pronto.Term` Returns: a `termlink.models.Coding` """ (abbreviation, code) = term.id.split(':') return Coding(system=_to_system(abbreviation), code=code, display=term.name)
def _to_relationship(rec, index, equivalence='subsumes'): """ Convert record in table to Relationship as a JSON object Record is expected to have the following fields: [ source.CODE, source.STR, target.CODE, target.STR] Args: rec: A table record Returns: A new record containing a single field, which is the JSON object """ source = Coding(system="http://www.broadinstitute.org/gsea/msigdb", code=rec[index], display=rec[index]) target = Coding(system="http://www.broadinstitute.org/gsea/msigdb", code=rec[0], display=rec[0]) return Relationship(equivalence, source, target)
def _to_relationship(rec): """Convert record in table to `Relationship` Record is expected to have the following fields: [source.CODE, source.STR, source.SAB, target.CODE, target.STR, target.SAB] Args: rec: A table record Returns: A `Relationship` """ equivalence = _to_equivalence(rec['REL']) source = Coding(system=_to_system(rec['source.SAB']), code=rec['source.CODE'], display=rec['source.STR']) target = Coding(system=_to_system(rec['target.SAB']), code=rec['target.CODE'], display=rec['target.STR']) return Relationship(equivalence, source, target)
def test_convert_relationship_to_json(): """Checks converting `Relationship` to JSON""" equivalence = 'equivalence' system = 'system' version = 'version' code = 'code' display = 'display' source = Coding(system=system, version=version, code=code, display=display) target = Coding(system=system, version=version, code=code, display=display) relationship = Relationship(equivalence, source, target) exp = { 'equivalence': equivalence, 'source': { 'type': 'coding', system: system, version: version, code: code, display: display }, 'target': { 'type': 'coding', system: system, version: version, code: code, display: display } } schema = RelationshipSchema() res = schema.dump(relationship) eq_(exp, res)
def test_convert_empty_coding_to_json(): """Checks converting `Coding` with no values to JSON""" coding = Coding() exp = { 'type': 'coding', 'system': None, 'version': None, 'code': None, 'display': None } schema = CodingSchema() res = schema.dump(coding) eq_(exp, res)
def test_convert_partial_coding_to_json(): """Checks converting `Coding` with some values to JSON""" system = 'system' coding = Coding(system=system) exp = { 'type': 'coding', 'system': system, 'version': None, 'code': None, 'display': None } schema = CodingSchema() res = schema.dump(coding) eq_(exp, res)
def test_convert_coding_to_json(): """Checks converting `Coding` to JSON""" system = 'system' version = 'version' code = 'code' display = 'display' coding = Coding(system=system, version=version, code=code, display=display) exp = { 'type': 'coding', 'system': system, 'version': version, 'code': code, 'display': display } schema = CodingSchema() res = schema.dump(coding) eq_(exp, res)