<jupyter_start><jupyter_text>Interaction APIIn this section, we will continue investigating the use of an API published by the National Library of Medicine.  The techniques will be very similar to the spell checker example in the APIRequest notebook, though the data is more elaborate and more challenging to parse.We will be querying drug interactions through teh InteractionAPI.  This api accepts an rxcui for a particular drug and provides information about the known interactions with other medications.  To read more about this API, you can follow the link athttps://rxnav.nlm.nih.gov/InteractionAPIs.htmlFor an overview of the metadata related to this api, you can use the appendix athttps://www.nlm.nih.gov/research/umls/rxnorm/docs/2015/appendix5.html    Note: To find the rxcui for a particular drug by name (string), use:https://rxnav.nlm.nih.gov/api-RxNorm.findRxcuiByString.html We start by loading the required libraries, making a connection to a URI, and converting the JSON response to a python dictionary<jupyter_code>import requests

url = 'https://rxnav.nlm.nih.gov/REST/interaction/interaction.json?rxcui=153165'
resp = requests.get(url)
data = resp.json()<jupyter_output><empty_output><jupyter_text>this cell is commented out because of the amount of data.  To follow along, you can uncomment and run to look at the raw JSON from the URI.  Alternatively, just take a look at the URI directly in your brower.  You may want to install a plug-in to make viewing easier.<jupyter_code>data
type(data)<jupyter_output><empty_output><jupyter_text>This data structure is more elaborate and nested than the spell checker, combining lists and dictionaries.  Suppose you wanted to use this API to extract a list of drugs with a known interaction?  Do do this, you'll need to parse the JSON document to get specifically at the data fields you need.  There are various techniques to do this, so you often don't need to write this by hand.  However, there will be times when you need to apply very specific logic to a JSON tree, so we will investigate how to do this by directly accessing elements of a tree with Python.  To directly access the names of the drugs with interactions, we will need to followinteractionTypeGroup -> interactionType -> interactionPair -> minConceptItem.Some of these are nested dictionaries, and some are lists, so we may need to use different syntax to access them.To get the top level keys for this dictionary, you can use the python keys syntax.  Remember, a JSON document is very similar to a dictionary, and once loaded, you can use all the dict methods available in Python.<jupyter_code>data.keys()<jupyter_output><empty_output><jupyter_text>Becase we are dealing with **keys**, we can use dictionary syntax.<jupyter_code>interactionTypeGroup = data['interactionTypeGroup']
type(interactionTypeGroup)<jupyter_output><empty_output><jupyter_text>We have a key that maps to a list.  Let's check the length.<jupyter_code>len(interactionTypeGroup)<jupyter_output><empty_output><jupyter_text>There's only one item in this list, but we can't assume that will always be the case.  If you want to parse a JSON tree, or a nested dictionary, make sure you iterate over every item in a list, even if in every case so far, you've only seen a single value.  We're dealing with a list here, so instead of using a lookup value in a dictionary (which is not ordered), we can access by positional index for a list, which is ordered.<jupyter_code>interactionTypeGroup = interactionTypeGroup[0]
type(interactionTypeGroup)<jupyter_output><empty_output><jupyter_text>The top level dictionary contained a list.  This list, in turn, contains dictionaries.<jupyter_code>interactionTypeGroup.keys()<jupyter_output><empty_output><jupyter_text>Using this technique, let's continue to descend this tree until we get to the data we need.<jupyter_code>interactionType = interactionTypeGroup['interactionType']
type(interactionType)
len(interactionType)
interactionType = interactionType[0]
interactionType.keys()
interactionPair = interactionType['interactionPair']
#interactionPair
len(interactionPair)<jupyter_output><empty_output><jupyter_text>To avoid ambiguity between terms, let's pluralize this to remember we're dealing with a list.  The naming makes no different to Python, of course, but we (and others) have to read this code!<jupyter_code>interactionPairs = interactionPair
interactionPair[0]
type(interactionPair[0])
interactionPair[0].keys()<jupyter_output><empty_output><jupyter_text>We don't have to assign an intermediary variable if we don't need one - we can grab the key value from the first element of the array.<jupyter_code>interactionConcept = interactionPair[0]['interactionConcept']
interactionConcept
len(interactionConcept)<jupyter_output><empty_output><jupyter_text>Look back at your original tree - the first element of the list is the name of the drug we're researching.  The second element is the name of the drug with an interaction.  To build our list, we'll grab that one.<jupyter_code>minConceptItem = interactionConcept[1]['minConceptItem']
minConceptItem<jupyter_output><empty_output><jupyter_text>Finally, we arrive at a the data we need!<jupyter_code>name = minConceptItem['name']
name<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06382978723404255, 'fraction_numerical': 0.003419452887537994, 'mean_word_length': 6.0013297872340425, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 52, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '344011', 'license': 'MIT', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1421, 'n_words': 633, 'path': 'Interactions.ipynb', 'repo_name': 'geoffswc/Python-JSON-Workshop', 'size': '5264'}