{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests\n", "import xml.etree.ElementTree\n", "import time\n", "import os\n", "import csv\n", "\n", "import pandas" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_rxnorm_ingredients(rxcui):\n", " \"\"\"\n", " Return a generator of RxNorm ingredients related to the provided RxNorm concept.\n", " Each ingredient is a (rxcui, name, umlscui) tuple. This function calls the\n", " RxNormAPI as documented here:\n", " http://rxnav.nlm.nih.gov/RxNormAPIs.html#uLink=RxNorm_REST_getAllRelatedInfo\n", " \"\"\"\n", " base_uri = 'http://rxnav.nlm.nih.gov/REST'\n", " url = '{base_uri}/rxcui/{rxcui}/allrelated'.format(base_uri = base_uri, rxcui = rxcui)\n", " response = requests.get(url)\n", " tree = xml.etree.ElementTree.fromstring(response.text)\n", " xml_ingredients = tree.findall(\"./allRelatedGroup/conceptGroup[tty='IN']/conceptProperties\")\n", " for xml_ingredient in xml_ingredients:\n", " assert xml_ingredient.findtext('tty') == 'IN'\n", " yield tuple(xml_ingredient.findtext(tag) for tag in ['rxcui', 'name', 'umlscui'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
medicationmedication_definition_idindicationsrxcuiscoreranknAtomsnamesynonymttylanguagesuppressumlscui
14-Aminopyridine Powder108483189701867512dalfampridineNaNINENGNC0000477
2Abilify 1 MG/ML Oral Solution123191254441210017aripiprazole 1 MG/ML Oral Solution [Abilify]Abilify 1 MG/ML Oral SolutionSBDENGNC1586223
3Abilify 10 MG Oral Tablet10419711352307100116aripiprazole 10 MG Oral Tablet [Abilify]Abilify 10 MG Oral TabletSBDENGNC1169930
\n", "
" ], "text/plain": [ " medication medication_definition_id indications \\\n", "1 4-Aminopyridine Powder 108483 1 \n", "2 Abilify 1 MG/ML Oral Solution 123191 2 \n", "3 Abilify 10 MG Oral Tablet 104197 11 \n", "\n", " rxcui score rank nAtoms name \\\n", "1 897018 67 5 12 dalfampridine \n", "2 544412 100 1 7 aripiprazole 1 MG/ML Oral Solution [Abilify] \n", "3 352307 100 1 16 aripiprazole 10 MG Oral Tablet [Abilify] \n", "\n", " synonym tty language suppress umlscui \n", "1 NaN IN ENG N C0000477 \n", "2 Abilify 1 MG/ML Oral Solution SBD ENG N C1586223 \n", "3 Abilify 10 MG Oral Tablet SBD ENG N C1169930 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read the rxnorm mappings to ehrlink medications computed by antoine lizee\n", "url = 'https://raw.githubusercontent.com/antoine-lizee/RRxNorm/master/Output/allMatchesWithProperties.csv'\n", "med_df = pandas.read_csv(url, index_col=0)\n", "med_df[:3]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No ingredients found for 1151137 DFG Oral Liquid Product\n", "No ingredients found for 7067 DF Mouthwash\n" ] } ], "source": [ "# Read the rxnorm mappings to ehrlink medications computed by antoine lizee\n", "\n", "# Prepare the write file\n", "path = os.path.join('data', 'rxnorm-as-ingredient.tsv')\n", "write_file = open(path, 'w')\n", "header = ['input_rxcui', 'input_tty', 'input_name', 'n_ingredients',\n", " 'ingredient_rxcui', 'ingredient_name', 'ingredient_umlscui']\n", "writer = csv.DictWriter(write_file, delimiter='\\t', fieldnames=header)\n", "writer.writeheader()\n", "\n", "# Query the RxNorm API for each concept.\n", "for rxcui, tty, name in set(zip(med_df.rxcui, med_df.tty, med_df.name)):\n", " ingredients = list(get_rxnorm_ingredients(rxcui))\n", " input_dict = {'input_rxcui': rxcui, 'input_tty': tty, 'input_name': name, 'n_ingredients': len(ingredients)}\n", " if not ingredients:\n", " print('No ingredients found for', rxcui, tty, name)\n", " for ingredient in ingredients:\n", " ingr_dict = input_dict.copy()\n", " for tag, value in zip(['ingredient_rxcui', 'ingredient_name', 'ingredient_umlscui'], ingredient):\n", " ingr_dict[tag] = value\n", " writer.writerow(ingr_dict)\n", " time.sleep(2)\n", "\n", "# Close the completed file\n", "write_file.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.0" } }, "nbformat": 4, "nbformat_minor": 0 }