Skip to main content

Terminology functions

The library also provides a set of functions for querying a FHIR terminology server from within your queries and transformations.

Value set membership

The member_of function can be used to test the membership of a code within a FHIR value set. This can be used with both explicit value sets (i.e. those that have been pre-defined and loaded into the terminology server) and implicit value sets (e.g. SNOMED CT Expression Constraint Language).

In this example, we take a list of SNOMED CT diagnosis codes and create a new column which shows which are viral infections. We use an ECL expression to define viral infection as a disease with a pathological process of "Infectious process", and a causative agent of "Virus".

To use the Pathling encoders from Python, install the pathling package using pip. Note that Java 11 or later is required, with yourJAVA_HOME properly set.

from pathling import PathlingContext
from pathling.functions import to_coding, to_ecl_value_set

pc = PathlingContext.create()
csv = pc.spark.read.csv("conditions.csv")

result = pc.member_of(csv, to_coding(csv.CODE, 'http://snomed.info/sct'),
to_ecl_value_set("""
<< 64572001|Disease| : (
<< 370135005|Pathological process| = << 441862004|Infectious process|,
<< 246075003|Causative agent| = << 49872002|Virus|
)
"""), 'VIRAL_INFECTION')
result.select('CODE', 'DESCRIPTION', 'VIRAL_INFECTION').show()

Results in:

CODEDESCRIPTIONVIRAL_INFECTION
65363002Otitis mediafalse
16114001Fracture of anklefalse
444814009Viral sinusitistrue
444814009Viral sinusitistrue
43878008Streptococcal sore throatfalse

Concept translation

The translate function can be used to translate codes from one code system to another using maps that are known to the terminology server. In this example, we translate our SNOMED CT diagnosis codes into Read CTV3.

To use the Pathling encoders from Python, install the pathling package using pip. Note that Java 11 or later is required, with yourJAVA_HOME properly set.

from pathling import PathlingContext
from pathling.functions import to_coding

pc = PathlingContext.create()
csv = pc.spark.read.csv("conditions.csv")

result = pc.translate(csv, to_coding(csv.CODE, 'http://snomed.info/sct'),
'http://snomed.info/sct/900000000000207008?fhir_cm='
'900000000000497000',
output_column_name='READ_CODE')
result = result.withColumn('READ_CODE', result.READ_CODE.code)
result.select('CODE', 'DESCRIPTION', 'READ_CODE').show()

Results in:

CODEDESCRIPTIONREAD_CODE
65363002Otitis mediaX00ik
16114001Fracture of ankleS34..
444814009Viral sinusitisXUjp0
444814009Viral sinusitisXUjp0
43878008Streptococcal sore throatA340.

Subsumption testing

Subsumption test is a fancy way of saying "is this code equal or a subtype of this other code".

For example, a code representing "ankle fracture" is subsumed by another code representing "fracture". The "fracture" code is more general, and using it with subsumption can help us find other codes representing different subtypes of fracture.

The subsumes function allows us to perform subsumption testing on codes within our data. The order of the left and right operands can be reversed to query whether a code is "subsumed by" another code.

To use the Pathling encoders from Python, install the pathling package using pip. Note that Java 11 or later is required, with yourJAVA_HOME properly set.

from pathling import PathlingContext
from pathling.coding import Coding
from pathling.functions import to_coding

pc = PathlingContext.create()
csv = pc.spark.read.csv("conditions.csv")

# 232208008 |Ear, nose and throat disorder|
left_coding = Coding('http://snomed.info/sct', '232208008')
right_coding_column = to_coding(csv.CODE, 'http://snomed.info/sct')

result = pc.subsumes(csv, 'SUBSUMES',
left_coding=left_coding,
right_coding_column=right_coding_column)

result.select('CODE', 'DESCRIPTION', 'IS_ENT').show()

Results in:

CODEDESCRIPTIONIS_ENT
65363002Otitis mediatrue
16114001Fracture of anklefalse
444814009Viral sinusitistrue

Authentication

Pathling can be configured to connect to a protected terminology server by supplying a set of OAuth2 client credentials and a token endpoint.

Here is an example of how to authenticate to the NHS terminology server:

To use the Pathling encoders from Python, install the pathling package using pip. Note that Java 11 or later is required, with yourJAVA_HOME properly set.

from pathling import PathlingContext

pc = PathlingContext.create(
terminology_server_url='https://ontology.nhs.uk/production1/fhir',
token_endpoint='https://ontology.nhs.uk/authorisation/auth/realms/nhs-digital-terminology/protocol/openid-connect/token',
client_id='[client ID]',
client_secret='[client secret]'
)