Created
March 3, 2012 11:41
-
-
Save jgkim/1965685 to your computer and use it in GitHub Desktop.
Getting the Immunization Coding System for Indivo X v1.0 or above
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NCBO BioPortal API Key | |
api_key = '[API Key]' | |
# UMLS Terminology Services (UTS) Username and Password | |
username = '[Username]' | |
password = '[Password]' | |
# lxml for Parsing HL7 Ontology from NCBO BioPortal: http://lxml.de/ | |
from lxml.etree import iterparse | |
from urllib2 import urlopen | |
from StringIO import StringIO | |
# SUDS for consuming Web Services: https://fedorahosted.org/suds/ | |
from suds.client import Client | |
from suds.xsd.doctor import Import, ImportDoctor | |
# Logging for Debugging | |
import logging | |
logging.basicConfig(format = '%(levelname)s:%(message)s', level = logging.INFO) | |
# LexGrid Namespaces | |
namespaces = {'lgCon': 'http://LexGrid.org/schema/2009/01/LexGrid/concepts', | |
'lgCommon': 'http://LexGrid.org/schema/2009/01/LexGrid/commonTypes'} | |
# URLs for UTS Web Services | |
cas_wsdl = 'https://uts-ws.nlm.nih.gov/authorization/services/AuthorizationPort?WSDL' | |
umlsks_wsdl = 'https://uts-ws.nlm.nih.gov/UMLSKS/services/UMLSKSService?wsdl' | |
umlsks_url = 'http://umlsks.nlm.nih.gov' | |
# Instantiating Client Class for Consuming Web Services | |
schema_import = Import('http://schemas.xmlsoap.org/soap/encoding/') | |
schema_doctor = ImportDoctor(schema_import) | |
cas_client = Client(cas_wsdl, doctor = schema_doctor) | |
umlsks_client = Client(umlsks_wsdl, autoblend = True) | |
# Obtaining a Proxy Granting Ticket | |
pgt = cas_client.service.getProxyGrantTicket(username, password) | |
logging.info("UMLS Terminology Services Authenticated with User '%s'" % username) | |
# Getting the Current UMLS Release | |
proxy_ticket = cas_client.service.getProxyTicket(pgt, umlsks_url) | |
request = umlsks_client.factory.create('{http://query.kss.nlm.nih.gov}CurrentUMLSRequest') | |
request.casTicket = proxy_ticket | |
umls_release = umlsks_client.service.getCurrentUMLSVersion(request) | |
logging.info("Current UMLS Release: %s" % umls_release) | |
# Downloading HL7 Ontology from NCBO BioPortal | |
ontology = urlopen('http://rest.bioontology.org/bioportal/virtual/download/1343?apikey=%s' % api_key).read() | |
logging.info("HL7 Ontology Downloaded (%d bytes)" % len(ontology)) | |
# Instantiating iterparse Class with HL7 Ontology | |
context = iterparse(StringIO(ontology), tag = '{%s}entity' % namespaces['lgCon'], huge_tree = True) | |
# Preparing a Request Message for ConceptIdExactRequest | |
request = umlsks_client.factory.create('{http://meta.query.kss.nlm.nih.gov}ConceptIdExactRequest') | |
request.release = umls_release | |
request.SABs = ['HL7V3.0'] | |
request.CVF = 0x00000000L | |
request.includeSuppressibles = False | |
# Iterating the Entire Tree | |
count = 0 | |
for event, element in context: | |
sources = element.xpath("./lgCon:presentation[@propertyId='T0']/lgCommon:source", namespaces = namespaces) | |
if sources and (sources[0].text == 'VaccineType'): | |
value1 = element.xpath("./lgCon:presentation[@propertyId='T0']/lgCommon:propertyQualifier[@propertyQualifierName='source-code']/lgCommon:value", namespaces = namespaces)[0].text | |
value2 = element.xpath("./lgCommon:entityDescription", namespaces = namespaces)[0].text | |
value3 = element.xpath("./lgCon:definition[@propertyId='D0']/lgCommon:value", namespaces = namespaces)[0].text | |
# Removing Some Multiple Spaces | |
value2 = ' '.join(value2.split()) | |
value3 = ' '.join(value3.split()) | |
# Finding UMLS CUI by Exact Match | |
proxy_ticket = cas_client.service.getProxyTicket(pgt, umlsks_url) | |
request.casTicket = proxy_ticket | |
request.searchString = value2 | |
response = umlsks_client.service.findCUIByExact(request) | |
value4 = response.contents[0].CUI | |
# Printing out a Line | |
print '%s|%s|%s|%s' % (value1, value2, value3, value4) | |
count += 1 | |
logging.info("%d - %s" % (count, value2)) |
The script cann't apply to the last edition of HL7 ontology on BioPortal. The namespaces are changed.
We can modify the URL to get the old edition of HL7 ontology :
ontology = urlopen('http://rest.bioontology.org/bioportal/ontologies/download/46442?apikey=%s' % api_key).read()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Best script ever! Thanks so much.