File size: 23,707 Bytes
c8e9e79 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Convert the DrugBank XML databse to JSON and extract features"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run using Python 3 to avoid a non-ascii character error when writing to file with the csv module."
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"import csv\n",
"import gzip\n",
"import collections\n",
"import re\n",
"import io\n",
"import json\n",
"import xml.etree.ElementTree as ET\n",
"import requests\n",
"import pandas\n",
"import xmltodict\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"xml_path = \"data/full_database.xml\"\n",
"json_path = \"data/full_database.json\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Read the XML file\n",
"\n",
"with open('data/full_database.xml', encoding=\"UTF8\") as f:\n",
" db = xmltodict.parse(f.read())\n",
"\n",
"json_obj = json.dumps(db, indent=4)\n",
"\n",
"# output as json\n",
"with open(\"data/full_database.json\", \"w\") as outfile:\n",
" outfile.write(json_obj)"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [],
"source": [
"desired_props_exp = set([\"Water Solubility\",\n",
" \"Melting Point\",\n",
" \"Boiling Point\",\n",
" \"logP\",\n",
" \"logS\",\n",
" \"Hydrophobicity\",\n",
" \"Isoelectric Point\",\n",
" \"caco2 Permeability\",\n",
" \"pKa\",\n",
" \"Molecular Weight\",\n",
" \"Radioactivity\"])\n",
"\n",
"desired_props_calc = set([\"logP\",\n",
" \"logS\",\n",
" \"Water Solubility\",\n",
" \"Molecular Weight\",\n",
" \"Monoisotopic Weight\",\n",
" \"Polar Surface Area (PSA)\",\n",
" \"Refractivity\",\n",
" \"Polarizability\",\n",
" \"Rotatable Bond Count\",\n",
" \"H Bond Acceptor Count\",\n",
" \"H Bond Donor Count\",\n",
" \"pKa (strongest acidic)\",\n",
" \"pKa (strongest basic)\",\n",
" \"Physiological Charge\",\n",
" \"Number of Rings\",\n",
" \"Bioavailability\",\n",
" \"Rule of Five\",\n",
" \"Ghose Filter\",\n",
" \"MDDR-Like Rule\",\n",
" \"Veber's Rule\"])\n",
"\n",
"def getProperties(desired_props, props, row):\n",
" for prop in desired_props:\n",
" if prop not in row:\n",
" row[prop] = None\n",
"\n",
" try:\n",
" for prop in props:\n",
" if(prop['kind'] in desired_props):\n",
" match = re.search(r\"[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?\", prop['value'])\n",
" row[prop['kind']] = float(match.group(0))\n",
" except:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"with open(json_path) as f:\n",
" data = json.load(f)"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [],
"source": [
"rows = []\n",
"for i in range(15235):\n",
" row = {}\n",
" drug = data['drugbank']['drug'][i]\n",
" row['name'] = drug['name']\n",
" row['state'] = drug.get('state', None)\n",
" atc_code = None\n",
" try:\n",
" atc_code = drug.get('atc-codes', dict()).get('atc-code', None)\n",
" atc_code = atc_code[0]\n",
" except:\n",
" pass\n",
"\n",
" row['level4'] = None\n",
" row['level3'] = None\n",
" row['level2'] = None\n",
" row['level1'] = None\n",
" try:\n",
" row['level4'] = atc_code['level'][0]['@code']\n",
" row['level3'] = atc_code['level'][1]['@code']\n",
" row['level2'] = atc_code['level'][2]['@code']\n",
" row['level1'] = atc_code['level'][3]['@code']\n",
" except:\n",
" pass\n",
"\n",
" \n",
" try:\n",
" exp_props = drug['experimental-properties']['property']\n",
" except:\n",
" exp_props = None\n",
" getProperties(desired_props_exp, exp_props, row)\n",
"\n",
" try:\n",
" calc_props = drug['calculated-properties']['property']\n",
" except:\n",
" calc_props = None\n",
" getProperties(desired_props_calc, calc_props, row)\n",
"\n",
" rows.append(row)"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>state</th>\n",
" <th>level4</th>\n",
" <th>level3</th>\n",
" <th>level2</th>\n",
" <th>level1</th>\n",
" <th>Hydrophobicity</th>\n",
" <th>Boiling Point</th>\n",
" <th>Molecular Weight</th>\n",
" <th>Isoelectric Point</th>\n",
" <th>...</th>\n",
" <th>Polar Surface Area (PSA)</th>\n",
" <th>Veber's Rule</th>\n",
" <th>pKa (strongest basic)</th>\n",
" <th>Ghose Filter</th>\n",
" <th>Monoisotopic Weight</th>\n",
" <th>MDDR-Like Rule</th>\n",
" <th>Polarizability</th>\n",
" <th>H Bond Acceptor Count</th>\n",
" <th>Physiological Charge</th>\n",
" <th>Rule of Five</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Lepirudin</td>\n",
" <td>solid</td>\n",
" <td>B01AE</td>\n",
" <td>B01A</td>\n",
" <td>B01</td>\n",
" <td>B</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Cetuximab</td>\n",
" <td>liquid</td>\n",
" <td>L01FE</td>\n",
" <td>L01F</td>\n",
" <td>L01</td>\n",
" <td>L</td>\n",
" <td>-0.413</td>\n",
" <td>NaN</td>\n",
" <td>145781.6000</td>\n",
" <td>8.48</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Dornase alfa</td>\n",
" <td>liquid</td>\n",
" <td>R05CB</td>\n",
" <td>R05C</td>\n",
" <td>R05</td>\n",
" <td>R</td>\n",
" <td>-0.083</td>\n",
" <td>NaN</td>\n",
" <td>29253.9000</td>\n",
" <td>4.58</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Denileukin diftitox</td>\n",
" <td>liquid</td>\n",
" <td>L01XX</td>\n",
" <td>L01X</td>\n",
" <td>L01</td>\n",
" <td>L</td>\n",
" <td>-0.301</td>\n",
" <td>NaN</td>\n",
" <td>57647.3000</td>\n",
" <td>5.45</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Etanercept</td>\n",
" <td>liquid</td>\n",
" <td>L04AB</td>\n",
" <td>L04A</td>\n",
" <td>L04</td>\n",
" <td>L</td>\n",
" <td>-0.529</td>\n",
" <td>NaN</td>\n",
" <td>51234.9000</td>\n",
" <td>7.89</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15230</th>\n",
" <td>AUM-601</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15231</th>\n",
" <td>FN-1501</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>431.5040</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>431.218206</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15232</th>\n",
" <td>Tinengotinib</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>394.8600</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>394.130887</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15233</th>\n",
" <td>Lipotecan</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>850.7100</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>850.183062</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15234</th>\n",
" <td>Xenon Xe-129</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>128.9048</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>None</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>128.904781</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>15235 rows × 33 columns</p>\n",
"</div>"
],
"text/plain": [
" name state level4 level3 level2 level1 \\\n",
"0 Lepirudin solid B01AE B01A B01 B \n",
"1 Cetuximab liquid L01FE L01F L01 L \n",
"2 Dornase alfa liquid R05CB R05C R05 R \n",
"3 Denileukin diftitox liquid L01XX L01X L01 L \n",
"4 Etanercept liquid L04AB L04A L04 L \n",
"... ... ... ... ... ... ... \n",
"15230 AUM-601 None None None None None \n",
"15231 FN-1501 None None None None None \n",
"15232 Tinengotinib None None None None None \n",
"15233 Lipotecan None None None None None \n",
"15234 Xenon Xe-129 None None None None None \n",
"\n",
" Hydrophobicity Boiling Point Molecular Weight Isoelectric Point \\\n",
"0 NaN NaN NaN NaN \n",
"1 -0.413 NaN 145781.6000 8.48 \n",
"2 -0.083 NaN 29253.9000 4.58 \n",
"3 -0.301 NaN 57647.3000 5.45 \n",
"4 -0.529 NaN 51234.9000 7.89 \n",
"... ... ... ... ... \n",
"15230 NaN NaN NaN NaN \n",
"15231 NaN NaN 431.5040 NaN \n",
"15232 NaN NaN 394.8600 NaN \n",
"15233 NaN NaN 850.7100 NaN \n",
"15234 NaN NaN 128.9048 NaN \n",
"\n",
" ... Polar Surface Area (PSA) Veber's Rule pKa (strongest basic) \\\n",
"0 ... NaN None NaN \n",
"1 ... NaN None NaN \n",
"2 ... NaN None NaN \n",
"3 ... NaN None NaN \n",
"4 ... NaN None NaN \n",
"... ... ... ... ... \n",
"15230 ... NaN None NaN \n",
"15231 ... NaN None NaN \n",
"15232 ... NaN None NaN \n",
"15233 ... NaN None NaN \n",
"15234 ... NaN None NaN \n",
"\n",
" Ghose Filter Monoisotopic Weight MDDR-Like Rule Polarizability \\\n",
"0 NaN NaN NaN NaN \n",
"1 NaN NaN NaN NaN \n",
"2 NaN NaN NaN NaN \n",
"3 NaN NaN NaN NaN \n",
"4 NaN NaN NaN NaN \n",
"... ... ... ... ... \n",
"15230 NaN NaN NaN NaN \n",
"15231 NaN 431.218206 NaN NaN \n",
"15232 NaN 394.130887 NaN NaN \n",
"15233 NaN 850.183062 NaN NaN \n",
"15234 NaN 128.904781 NaN NaN \n",
"\n",
" H Bond Acceptor Count Physiological Charge Rule of Five \n",
"0 NaN NaN NaN \n",
"1 NaN NaN NaN \n",
"2 NaN NaN NaN \n",
"3 NaN NaN NaN \n",
"4 NaN NaN NaN \n",
"... ... ... ... \n",
"15230 NaN NaN NaN \n",
"15231 NaN NaN NaN \n",
"15232 NaN NaN NaN \n",
"15233 NaN NaN NaN \n",
"15234 NaN NaN NaN \n",
"\n",
"[15235 rows x 33 columns]"
]
},
"execution_count": 175,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"drugbank_df = pandas.DataFrame.from_dict(rows)\n",
"drugbank_df.to_csv(\"data/full_database.csv\")\n",
"drugbank_df"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [],
"source": [
"threshold = 10\n",
"df = drugbank_df.dropna(thresh=drugbank_df.shape[1] - threshold + 1)\n",
"df = df.dropna(axis=1, thresh=df.shape[0]-1000+1)"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"name 0\n",
"state 549\n",
"level4 35\n",
"level3 35\n",
"level2 35\n",
"level1 35\n",
"Molecular Weight 0\n",
"logP 0\n",
"Water Solubility 5\n",
"logS 27\n",
"Bioavailability 0\n",
"pKa (strongest acidic) 394\n",
"Refractivity 0\n",
"Number of Rings 0\n",
"H Bond Donor Count 0\n",
"Rotatable Bond Count 0\n",
"Polar Surface Area (PSA) 0\n",
"pKa (strongest basic) 110\n",
"Ghose Filter 0\n",
"Monoisotopic Weight 0\n",
"MDDR-Like Rule 0\n",
"Polarizability 0\n",
"H Bond Acceptor Count 0\n",
"Physiological Charge 0\n",
"Rule of Five 0\n",
"dtype: int64"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.isna().sum()"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv('data/filtered_dataset.csv')"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [],
"source": [
"interactions = {}\n",
"# get the set of drugs in the filtered df\n",
"drugs = set(df[\"name\"])\n",
"\n",
"for i in range(15235):\n",
" drug = data['drugbank']['drug'][i]\n",
" \n",
" if drug.get(\"name\", None) in drugs:\n",
" try:\n",
" interactions[drug['name']] = [x['name'] for x in drug['drug-interactions'][\"drug-interaction\"] if x['name'] in drugs]\n",
" except:\n",
" interactions[drug['name']] = []"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [],
"source": [
"json_obj = json.dumps(interactions, indent=4)\n",
"\n",
"# output as json\n",
"with open(\"data/interactions.json\", \"w\") as outfile:\n",
" outfile.write(json_obj)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|