Skip to content

Commit

Permalink
Merge pull request #4 from cdisc-org/add-cosmos-endpoints
Browse files Browse the repository at this point in the history
Add COSMoS endpoints to the CDISC library client
  • Loading branch information
lexjansen authored Dec 12, 2023
2 parents 8d5c653 + b5806aa commit d6e3c7a
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# VSCode
.vscode/
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
# cdisc-library-client

Python client for accessing the CDISC library. Deployed to PYPI

# operations
## operations

```
get_sdtm - get an sdtm standard
get_adam - get an adam or adamig standard
get_cdash - get a cdash standard
get_sdtmig - gets sdtmig standard
get_sendig - gets sendig standard
get_cdashig - gets cdashig standard
get_terminology_package - get a ct package given the version
get_codelist_terms - returns an array of all terms in a codelist
get_codelist_term - returns the library representation of a codelist term
get_rule_catalogs - returns a list of links to all rule catalogs in the CDISC Library
get_rules_catalog - returns all rules in a single rules catalog.
get_rule - gets a rule definition given standard, version, and rule id
get_bc_packages - get biomedical concept package list
get_bc_package_biomedicalconcepts - get biomedical concept list in a package
get_bc_package_biomedicalconcept - get biomedical concept in a package
get_bc_latest_biomedicalconcepts - get biomedical concept list (latest versions)
get_bc_latest_biomedicalconcept - get biomedical concept (latest version)
get_bc_categories - get biomedical concept categories list
get_bc_latest_biomedicalconcepts_category - get biomedical concepts for a given category (latest version)
get_sdtm_packages - get sdtm dataset specialization package list
get_sdtm_package_datasetspecializations - get sdtm dataset specialization list in a package
get_sdtm_package_datasetspecialization - get sdtm dataset specialization in a package
get_sdtm_latest_sdtm_datasetspecializations - get sdtm dataset specialization list (latest versions)
get_sdtm_latest_sdtm_datasetspecialization - get sdtm dataset specialization (latest version)
get_sdtm_domains - get sdtm dataset specialization domain list
get_sdtm_latest_sdtm_datasetspecializations_domain - get a list of sdtm dataset specializations for a given domain (latest version)
get_biomedicalconcept_latest_datasetspecializations - get a list of dataset specializations that specialize a biomedical concept (latest versions)
```
More info on CDISC Library API endpoints can be found here: https://www.cdisc.org/cdisc-library/api-documentation

More info on CDISC Library API endpoints can be found at the [CDISC Library API Portal](https://api.developer.library.cdisc.org/).
87 changes: 79 additions & 8 deletions cdisc_library_client/library_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ def get_api_json(self, uri):
raise ResourceNotFoundException(f"Resource {self.base_api_url+uri} is not found.")
else:
raise Exception(f"Request to {self.base_api_url+uri} returned unsuccessful {raw_data.status_code} response")

def get_raw_response(self, uri):
headers = {
'Accept': 'application/json',
'api-key': self.api_key,
"User-Agent": "pipeline"
}
return self._session.get(self.base_api_url+uri, headers=headers)

def get_products(self):
href = "/mdr/products"
return self.get_api_json(href)

def get_sdtm(self, version):
href = f"/mdr/sdtm/{version}"
return self.get_api_json(href)
Expand All @@ -59,19 +59,19 @@ def get_sendig(self, version):
def get_adam(self, version):
href = f"/mdr/adam/{version}"
return self.get_api_json(href)

def get_cdash(self, version):
href = f"/mdr/cdash/{version}"
return self.get_api_json(href)

def get_cdashig(self, version):
href = f"/mdr/cdashig/{version}"
return self.get_api_json(href)

def get_terminology_package(self, version):
href = f"/mdr/ct/packages/{version}"
return self.get_api_json(href)

def get_codelist_terms(self, version, codelist):
terms = []
try:
Expand All @@ -94,7 +94,7 @@ def get_codelist_term(self, version, codelist, term):
def get_qrs_instrument(self, instrument, version):
href = f"/mdr/qrs/instruments/{instrument}/versions/{version}"
return self.get_api_json(href)

def get_rule_catalogs(self):
"""
Returns an object containing a mapping of catalog titles to links to the catalogs of the form:
Expand All @@ -109,7 +109,7 @@ def get_rule_catalogs(self):
href = f"/mdr/rules"
response = self.get_api_json(href)
return response.get("_links", {}).get("catalogs", {})

def get_rules_catalog(self, standard: str, version: str):
"""
Returns an object containing a mapping of rule id to rule definitions
Expand All @@ -124,3 +124,74 @@ def get_rule(self, standard: str, version: str, rule_id: str):
"""
href = f"/mdr/rules/{standard}/{version}/rule/{rule_id}"
return self.get_api_json(href)

def get_bc_packages(self, version: str):
href = f"/cosmos/{version}/mdr/bc/packages"
response = self.get_api_json(href)
return response.get("_links", {}).get("packages", [])

def get_bc_package_biomedicalconcepts(self, version: str, package: str):
href = f"/cosmos/{version}/mdr/bc/packages/{package}/biomedicalconcepts"
response = self.get_api_json(href)
return response.get("_links", {}).get("biomedicalConcepts", [])

def get_bc_package_biomedicalconcept(self, version: str, package: str, biomedicalconcept: str):
href = f"/cosmos/{version}/mdr/bc/packages/{package}/biomedicalconcepts/{biomedicalconcept}"
return self.get_api_json(href)

def get_bc_latest_biomedicalconcepts(self, version: str):
href = f"/cosmos/{version}/mdr/bc/biomedicalconcepts"
response = self.get_api_json(href)
return response.get("_links", {}).get("biomedicalConcepts", [])

def get_bc_latest_biomedicalconcept(self, version: str, biomedicalconcept: str):
href = f"/cosmos/{version}/mdr/bc/biomedicalconcepts/{biomedicalconcept}"
return self.get_api_json(href)

def get_bc_categories(self, version: str):
href = f"/cosmos/{version}/mdr/bc/categories"
response = self.get_api_json(href)
return response.get("_links", {}).get("categories", [])

def get_bc_latest_biomedicalconcepts_category(self, version: str, category: str):
href = f"/cosmos/{version}/mdr/bc/biomedicalconcepts?category={category}"
response = self.get_api_json(href)
return response.get("_links", {}).get("biomedicalConcepts", [])

def get_sdtm_packages(self, version: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/packages"
response = self.get_api_json(href)
return response.get("_links", {}).get("packages", [])

def get_sdtm_package_datasetspecializations(self, version: str, package: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/packages/{package}/datasetspecializations"
response = self.get_api_json(href)
return response.get("_links", {}).get("datasetSpecializations", [])

def get_sdtm_package_datasetspecialization(self, version: str, package: str, datasetspecialization: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/packages/{package}/datasetspecializations/{datasetspecialization}"
return self.get_api_json(href)

def get_sdtm_latest_sdtm_datasetspecializations(self, version: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/datasetspecializations"
response = self.get_api_json(href)
return response.get("_links", {}).get("datasetSpecializations", [])

def get_sdtm_latest_sdtm_datasetspecialization(self, version: str, datasetspecialization: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/datasetspecializations/{datasetspecialization}"
return self.get_api_json(href)

def get_sdtm_domains(self, version: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/domains"
response = self.get_api_json(href)
return response.get("domains", [])

def get_sdtm_latest_sdtm_datasetspecializations_domain(self, version: str, domain: str):
href = f"/cosmos/{version}/mdr/specializations/sdtm/datasetspecializations?domain={domain}"
response = self.get_api_json(href)
return response.get("_links", {}).get("datasetSpecializations", [])

def get_biomedicalconcept_latest_datasetspecializations(self, version: str, biomedicalconcept: str):
href = f"/cosmos/{version}/mdr/specializations/datasetspecializations?biomedicalconcept={biomedicalconcept}"
response = self.get_api_json(href)
return response.get("_links", {}).get("datasetSpecializations", [])
109 changes: 109 additions & 0 deletions examples/get_cosmos_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
from cdisc_library_client import CDISCLibraryClient

if __name__ == "__main__":
api_key = os.environ.get("CDISC_LIBRARY_API_KEY")
client = CDISCLibraryClient(api_key=api_key)

# Get BC packages
bc_packages = client.get_bc_packages("v2")
print(f"There are {len(bc_packages)} Biomedical Concept packages")

# Get BCs in package
for package in bc_packages:
title = package['title']
href = package['href']
package = href.split("/")[-2]
biomedicalconcepts = client.get_bc_package_biomedicalconcepts("v2", package)
print(f"{title} has {len(biomedicalconcepts)} Biomedical Concepts")

# Get a specific BC in a package
bc = client.get_bc_package_biomedicalconcept("v2", "2023-12-12", "C25298")
package = bc['_links']['parentPackage']['title']
title = bc['shortName']
print(f"{title} in {package} has {len(bc['dataElementConcepts'])} Data Element Concepts")

# Get latest BCs
biomedicalconcepts = client.get_bc_latest_biomedicalconcepts("v2")
print(f"There are {len(biomedicalconcepts)} Biomedical Concepts in the CDISC Library")

# Get the latest version of a specific BC
bc = client.get_bc_latest_biomedicalconcept("v2", "C25298")
package = bc['_links']['parentPackage']['title']
title = bc['shortName']
print(f"{title} in {package} has {len(bc['dataElementConcepts'])} Data Element Concepts")

# Get BC categories
categories = client.get_bc_categories("v2")
print(f"There are {len(categories)} Biomedical Concept categories")

# Get latest BCs in a category
biomedicalconcepts = client.get_bc_latest_biomedicalconcepts_category("v2", "RECIST 1.1")
print(f"There are {len(biomedicalconcepts)} Biomedical Concepts in the RECIST 1.1 category in the CDISC Library")

# Get latest BCs in a category
biomedicalconcepts = client.get_bc_latest_biomedicalconcepts_category("v2", "")
print(f"There are {len(biomedicalconcepts)} Biomedical Concepts in the CDISC Library")

# Get SDTM Dataset Specialization Packages
sdtm_packages = client.get_sdtm_packages("v2")
print(f"There are {len(sdtm_packages)} SDTM Dataset Specialization packages")

# Get Specializations in package
for package in sdtm_packages:
title = package['title']
href = package['href']
package = href.split("/")[-2]
datasetspecializations = client.get_sdtm_package_datasetspecializations("v2", package)
print(f"{title} has {len(datasetspecializations)} SDTM Dataset Specializations")

# Get a specific SDTM Dataset Specializatio in a package
sdtm = client.get_sdtm_package_datasetspecialization("v2", "2023-12-12", "SYSBP")
package = sdtm['_links']['parentPackage']['title']
title = sdtm['shortName']
print(f"{title} in {package} has {len(sdtm['variables'])} Variables")

# Get SDTM domains
domains = client.get_sdtm_domains("v2")
print(f"There are {len(domains)} SDTM domains")

# Get latest SDTM Dataset Specializations
specializations = client.get_sdtm_latest_sdtm_datasetspecializations("v2")
print(f"There are {len(specializations)} SDTM Dataset Specializations in the CDISC Library")

# Get the latest version of a specific SDTM Dataset Specialization
sdtm = client.get_sdtm_latest_sdtm_datasetspecialization("v2", "SYSBP")
package = sdtm['_links']['parentPackage']['title']
title = sdtm['shortName']
print(f"{title} in {package} has {len(sdtm['variables'])} Variables")

# Get latest SDTM Dataset Specializations in a domain
specializations = client.get_sdtm_latest_sdtm_datasetspecializations_domain("v2", "VS")
print(f"There are {len(specializations)} SDTM Dataset Specializations in the VS domain in the CDISC Library")

# Get latest SDTM Dataset Specializations in a domain
specializations = client.get_sdtm_latest_sdtm_datasetspecializations_domain("v2", "")
print(f"There are {len(specializations)} SDTM Dataset Specializations in the CDISC Library")

# Get latest SDTM Dataset Specializations that specialize a Biomedical Concept
specializations = client.get_biomedicalconcept_latest_datasetspecializations("v2", "")
sdtm_specializations = specializations['sdtm']
print(f"There are {len(sdtm_specializations)} SDTM Dataset Specializations in the CDISC Library")

# Get latest SDTM Dataset Specializations that specialize a Biomedical Concept
biomedicalConcept = "C25298"
specializations = client.get_biomedicalconcept_latest_datasetspecializations("v2", biomedicalConcept)
sdtm_specializations = specializations['sdtm']
print(f"There are {len(sdtm_specializations)} SDTM Dataset Specializations in the CDISC Library that specialize {biomedicalConcept}")

# Get latest SDTM Dataset Specializations that specialize a Biomedical Concept
biomedicalConcept = "C105585"
specializations = client.get_biomedicalconcept_latest_datasetspecializations("v2", biomedicalConcept)
sdtm_specializations = specializations['sdtm']
print(f"There are {len(sdtm_specializations)} SDTM Dataset Specializations in the CDISC Library that specialize {biomedicalConcept}")

# Get latest SDTM Dataset Specializations that specialize a Biomedical Concept
biomedicalConcept = "C123456"
specializations = client.get_biomedicalconcept_latest_datasetspecializations("v2", biomedicalConcept)
sdtm_specializations = specializations['sdtm']
print(f"There are {len(sdtm_specializations)} SDTM Dataset Specializations in the CDISC Library that specialize {biomedicalConcept}")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.1.4'
__version__ = '0.1.5'

import setuptools

Expand Down

0 comments on commit d6e3c7a

Please sign in to comment.