Last active
May 26, 2021 13:36
-
-
Save reality/fc588f4e8aafd4cea126ec26a8956f44 to your computer and use it in GitHub Desktop.
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
import org.json.* | |
/** | |
* Class that provides a static method for querying SNOMED class descendants from BioPortal. | |
*/ | |
public class SnomedClient { | |
static String API_ROOT = "https://data.bioontology.org/ontologies/SNOMEDCT/classes/"; | |
static String API_KEY = ""; | |
static String PAGE_SIZE = "500"; | |
/** | |
* Query Bioportal to retrieve list of descendants for a given SNOMED code. | |
* | |
* @Param String snomedCode A string identifying a concept in SNOMED-CT. For example: 44054006 | |
* @Return HashMap<String,String> A hashmap that contains all descendants of the given SNOMED term. Keys are the snomed codes, while values is the preferred label of the term. | |
*/ | |
static HashMap<String,String> queryDescendants(String snomedCode) { | |
// Set up variables for query .. | |
URL url; | |
HttpURLConnection conn; | |
BufferedReader rd; | |
String line; | |
String result = ""; | |
String qString = API_ROOT + snomedCode + "/descendants?apikey="+API_KEY+"&expand_class_hierarchy=true&pagesize=" + PAGE_SIZE | |
// Perform query to retrieve SNOMED code descendants | |
url = new URL(qString); | |
conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setRequestProperty("Authorization", "apikey token=" + API_KEY); | |
conn.setRequestProperty("Accept", "application/json"); | |
rd = new BufferedReader( | |
new InputStreamReader(conn.getInputStream())); | |
while ((line = rd.readLine()) != null) { | |
result += line; | |
} | |
rd.close(); | |
// Convert results to JSON | |
JSONTokener tokener = new JSONTokener(result); | |
JSONObject root = new JSONObject(tokener); | |
// Extract a map of SNOMED code -> label | |
HashMap<String, String> descendants = new HashMap<String, String>(); | |
Map<String,Object> outerMap = root.toMap(); | |
for(obj in outerMap["collection"]) { | |
String code = obj["@id"].tokenize('/').last(); | |
descendants[code] = obj["prefLabel"]; | |
} | |
return descendants; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment