/usr/share/doc/rdkit/html/Cookbook.html is in rdkit-doc 201503-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RDKit Cookbook — The RDKit 2015.03.1 documentation</title>
<link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2015.03.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="The RDKit 2015.03.1 documentation" href="index.html" />
<link rel="next" title="The RDKit database cartridge" href="Cartridge.html" />
<link rel="prev" title="The RDKit Book" href="RDKit_Book.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="Cartridge.html" title="The RDKit database cartridge"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="RDKit_Book.html" title="The RDKit Book"
accesskey="P">previous</a> |</li>
<li><a href="index.html">The RDKit 2015.03.1 documentation</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/logo.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="Overview.html">An overview of the RDKit</a></li>
<li class="toctree-l1"><a class="reference internal" href="Install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="GettingStartedInPython.html">Getting Started with the RDKit in Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="RDKit_Book.html">The RDKit Book</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">RDKit Cookbook</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#what-is-this">What is this?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#miscellaneous-topics">Miscellaneous Topics</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#using-a-different-aromaticity-model">Using a different aromaticity model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#manipulating-molecules">Manipulating Molecules</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#cleaning-up-heterocycles">Cleaning up heterocycles</a></li>
<li class="toctree-l3"><a class="reference internal" href="#parallel-conformation-generation">Parallel conformation generation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#neutralizing-charged-molecules">Neutralizing Charged Molecules</a></li>
<li class="toctree-l3"><a class="reference internal" href="#d-functionality-in-the-rdkit">3D functionality in the RDKit</a></li>
<li class="toctree-l3"><a class="reference internal" href="#using-scikit-learn-with-rdkit">Using scikit-learn with RDKit</a></li>
<li class="toctree-l3"><a class="reference internal" href="#using-custom-mcs-atom-types">Using custom MCS atom types</a></li>
<li class="toctree-l3"><a class="reference internal" href="#clustering-molecules">Clustering molecules</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#license">License</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="Cartridge.html">The RDKit database cartridge</a></li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="RDKit_Book.html"
title="previous chapter">The RDKit Book</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="Cartridge.html"
title="next chapter">The RDKit database cartridge</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/Cookbook.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="rdkit-cookbook">
<h1>RDKit Cookbook<a class="headerlink" href="#rdkit-cookbook" title="Permalink to this headline">¶</a></h1>
<div class="section" id="what-is-this">
<h2>What is this?<a class="headerlink" href="#what-is-this" title="Permalink to this headline">¶</a></h2>
<p>This document provides examples of how to carry out particular tasks
using the RDKit functionality from Python. The contents have been
contributed by the RDKit community.</p>
<p>If you find mistakes, or have suggestions for improvements, please
either fix them yourselves in the source document (the .rst file) or
send them to the mailing list: <a class="reference external" href="mailto:rdkit-discuss%40lists.sourceforge.net">rdkit-discuss<span>@</span>lists<span>.</span>sourceforge<span>.</span>net</a>
(you will need to subscribe first)</p>
</div>
<div class="section" id="miscellaneous-topics">
<h2>Miscellaneous Topics<a class="headerlink" href="#miscellaneous-topics" title="Permalink to this headline">¶</a></h2>
<div class="section" id="using-a-different-aromaticity-model">
<h3>Using a different aromaticity model<a class="headerlink" href="#using-a-different-aromaticity-model" title="Permalink to this headline">¶</a></h3>
<p>By default, the RDKit applies its own model of aromaticity (explained
in the RDKit Theory Book) when it reads in molecules. It is, however,
fairly easy to override this and use your own aromaticity model.</p>
<p>The easiest way to do this is it provide the molecules as SMILES with
the aromaticity set as you would prefer to have it. For example,
consider indole:</p>
<img alt="_images/indole1.png" src="_images/indole1.png" />
<p>By default the RDKit considers both rings to be aromatic:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'N1C=Cc2ccccc12'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">GetSubstructMatches</span><span class="p">(</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmarts</span><span class="p">(</span><span class="s">'c'</span><span class="p">))</span>
<span class="go">((1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,))</span>
</pre></div>
</div>
<p>If you’d prefer to treat the five-membered ring as aliphatic, which is
how the input SMILES is written, you just need to do a partial
sanitization that skips the kekulization and aromaticity perception
steps:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'N1C=Cc2ccccc12'</span><span class="p">,</span><span class="n">sanitize</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeMol</span><span class="p">(</span><span class="n">m2</span><span class="p">,</span><span class="n">sanitizeOps</span><span class="o">=</span><span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeFlags</span><span class="o">.</span><span class="n">SANITIZE_ALL</span><span class="o">^</span><span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeFlags</span><span class="o">.</span><span class="n">SANITIZE_KEKULIZE</span><span class="o">^</span><span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeFlags</span><span class="o">.</span><span class="n">SANITIZE_SETAROMATICITY</span><span class="p">)</span>
<span class="go">rdkit.Chem.rdmolops.SanitizeFlags.SANITIZE_NONE</span>
<span class="gp">>>> </span><span class="n">m2</span><span class="o">.</span><span class="n">GetSubstructMatches</span><span class="p">(</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmarts</span><span class="p">(</span><span class="s">'c'</span><span class="p">))</span>
<span class="go">((3,), (4,), (5,), (6,), (7,), (8,))</span>
</pre></div>
</div>
<p>It is, of course, also possible to write your own aromaticity
perception function, but that is beyond the scope of this document.</p>
</div>
</div>
<div class="section" id="manipulating-molecules">
<h2>Manipulating Molecules<a class="headerlink" href="#manipulating-molecules" title="Permalink to this headline">¶</a></h2>
<div class="section" id="cleaning-up-heterocycles">
<h3>Cleaning up heterocycles<a class="headerlink" href="#cleaning-up-heterocycles" title="Permalink to this headline">¶</a></h3>
<p>Mailing list discussions:</p>
<ul class="simple">
<li><a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01185.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01185.html</a></li>
<li><a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01162.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01162.html</a></li>
<li><a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01900.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01900.html</a></li>
<li><a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01901.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg01901.html</a></li>
</ul>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="sd">""" sanifix4.py</span>
<span class="sd"> Contribution from James Davidson</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="k">def</span> <span class="nf">_FragIndicesToMol</span><span class="p">(</span><span class="n">oMol</span><span class="p">,</span><span class="n">indices</span><span class="p">):</span>
<span class="n">em</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">EditableMol</span><span class="p">(</span><span class="n">Chem</span><span class="o">.</span><span class="n">Mol</span><span class="p">())</span>
<span class="n">newIndices</span><span class="o">=</span><span class="p">{}</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">idx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">indices</span><span class="p">):</span>
<span class="n">em</span><span class="o">.</span><span class="n">AddAtom</span><span class="p">(</span><span class="n">oMol</span><span class="o">.</span><span class="n">GetAtomWithIdx</span><span class="p">(</span><span class="n">idx</span><span class="p">))</span>
<span class="n">newIndices</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span><span class="o">=</span><span class="n">i</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">idx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">indices</span><span class="p">):</span>
<span class="n">at</span> <span class="o">=</span> <span class="n">oMol</span><span class="o">.</span><span class="n">GetAtomWithIdx</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
<span class="k">for</span> <span class="n">bond</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">GetBonds</span><span class="p">():</span>
<span class="k">if</span> <span class="n">bond</span><span class="o">.</span><span class="n">GetBeginAtomIdx</span><span class="p">()</span><span class="o">==</span><span class="n">idx</span><span class="p">:</span>
<span class="n">oidx</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">GetEndAtomIdx</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">oidx</span> <span class="o">=</span> <span class="n">bond</span><span class="o">.</span><span class="n">GetBeginAtomIdx</span><span class="p">()</span>
<span class="c"># make sure every bond only gets added once:</span>
<span class="k">if</span> <span class="n">oidx</span><span class="o"><</span><span class="n">idx</span><span class="p">:</span>
<span class="k">continue</span>
<span class="n">em</span><span class="o">.</span><span class="n">AddBond</span><span class="p">(</span><span class="n">newIndices</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span><span class="n">newIndices</span><span class="p">[</span><span class="n">oidx</span><span class="p">],</span><span class="n">bond</span><span class="o">.</span><span class="n">GetBondType</span><span class="p">())</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">em</span><span class="o">.</span><span class="n">GetMol</span><span class="p">()</span>
<span class="n">res</span><span class="o">.</span><span class="n">ClearComputedProps</span><span class="p">()</span>
<span class="n">Chem</span><span class="o">.</span><span class="n">GetSymmSSSR</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">res</span><span class="o">.</span><span class="n">UpdatePropertyCache</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="n">res</span><span class="o">.</span><span class="n">_idxMap</span><span class="o">=</span><span class="n">newIndices</span>
<span class="k">return</span> <span class="n">res</span>
<span class="k">def</span> <span class="nf">_recursivelyModifyNs</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span><span class="n">matches</span><span class="p">,</span><span class="n">indices</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="k">if</span> <span class="n">indices</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">indices</span><span class="o">=</span><span class="p">[]</span>
<span class="n">res</span><span class="o">=</span><span class="bp">None</span>
<span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">matches</span><span class="p">)</span> <span class="ow">and</span> <span class="n">res</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">tIndices</span><span class="o">=</span><span class="n">indices</span><span class="p">[:]</span>
<span class="n">nextIdx</span> <span class="o">=</span> <span class="n">matches</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">tIndices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nextIdx</span><span class="p">)</span>
<span class="n">nm</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">Mol</span><span class="p">(</span><span class="n">mol</span><span class="p">)</span>
<span class="n">nm</span><span class="o">.</span><span class="n">GetAtomWithIdx</span><span class="p">(</span><span class="n">nextIdx</span><span class="p">)</span><span class="o">.</span><span class="n">SetNoImplicit</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="n">nm</span><span class="o">.</span><span class="n">GetAtomWithIdx</span><span class="p">(</span><span class="n">nextIdx</span><span class="p">)</span><span class="o">.</span><span class="n">SetNumExplicitHs</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">cp</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">Mol</span><span class="p">(</span><span class="n">nm</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeMol</span><span class="p">(</span><span class="n">cp</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="n">res</span><span class="p">,</span><span class="n">indices</span> <span class="o">=</span> <span class="n">_recursivelyModifyNs</span><span class="p">(</span><span class="n">nm</span><span class="p">,</span><span class="n">matches</span><span class="p">,</span><span class="n">indices</span><span class="o">=</span><span class="n">tIndices</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">indices</span><span class="o">=</span><span class="n">tIndices</span>
<span class="n">res</span><span class="o">=</span><span class="n">cp</span>
<span class="k">return</span> <span class="n">res</span><span class="p">,</span><span class="n">indices</span>
<span class="k">def</span> <span class="nf">AdjustAromaticNs</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">nitrogenPattern</span><span class="o">=</span><span class="s">'[n&D2&H0;r5,r6]'</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> default nitrogen pattern matches Ns in 5 rings and 6 rings in order to be able</span>
<span class="sd"> to fix: O=c1ccncc1</span>
<span class="sd"> """</span>
<span class="n">Chem</span><span class="o">.</span><span class="n">GetSymmSSSR</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="n">m</span><span class="o">.</span><span class="n">UpdatePropertyCache</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="c"># break non-ring bonds linking rings:</span>
<span class="n">em</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">EditableMol</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="n">linkers</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">GetSubstructMatches</span><span class="p">(</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmarts</span><span class="p">(</span><span class="s">'[r]!@[r]'</span><span class="p">))</span>
<span class="n">plsFix</span><span class="o">=</span><span class="nb">set</span><span class="p">()</span>
<span class="k">for</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="ow">in</span> <span class="n">linkers</span><span class="p">:</span>
<span class="n">em</span><span class="o">.</span><span class="n">RemoveBond</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
<span class="n">plsFix</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">plsFix</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="n">nm</span> <span class="o">=</span> <span class="n">em</span><span class="o">.</span><span class="n">GetMol</span><span class="p">()</span>
<span class="k">for</span> <span class="n">at</span> <span class="ow">in</span> <span class="n">plsFix</span><span class="p">:</span>
<span class="n">at</span><span class="o">=</span><span class="n">nm</span><span class="o">.</span><span class="n">GetAtomWithIdx</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>
<span class="k">if</span> <span class="n">at</span><span class="o">.</span><span class="n">GetIsAromatic</span><span class="p">()</span> <span class="ow">and</span> <span class="n">at</span><span class="o">.</span><span class="n">GetAtomicNum</span><span class="p">()</span><span class="o">==</span><span class="mi">7</span><span class="p">:</span>
<span class="n">at</span><span class="o">.</span><span class="n">SetNumExplicitHs</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">at</span><span class="o">.</span><span class="n">SetNoImplicit</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="c"># build molecules from the fragments:</span>
<span class="n">fragLists</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">GetMolFrags</span><span class="p">(</span><span class="n">nm</span><span class="p">)</span>
<span class="n">frags</span> <span class="o">=</span> <span class="p">[</span><span class="n">_FragIndicesToMol</span><span class="p">(</span><span class="n">nm</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">fragLists</span><span class="p">]</span>
<span class="c"># loop through the fragments in turn and try to aromatize them:</span>
<span class="n">ok</span><span class="o">=</span><span class="bp">True</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">frag</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">frags</span><span class="p">):</span>
<span class="n">cp</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">Mol</span><span class="p">(</span><span class="n">frag</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeMol</span><span class="p">(</span><span class="n">cp</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="n">matches</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">frag</span><span class="o">.</span><span class="n">GetSubstructMatches</span><span class="p">(</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmarts</span><span class="p">(</span><span class="n">nitrogenPattern</span><span class="p">))]</span>
<span class="n">lres</span><span class="p">,</span><span class="n">indices</span><span class="o">=</span><span class="n">_recursivelyModifyNs</span><span class="p">(</span><span class="n">frag</span><span class="p">,</span><span class="n">matches</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">lres</span><span class="p">:</span>
<span class="c">#print 'frag %d failed (%s)'%(i,str(fragLists[i]))</span>
<span class="n">ok</span><span class="o">=</span><span class="bp">False</span>
<span class="k">break</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">revMap</span><span class="o">=</span><span class="p">{}</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">frag</span><span class="o">.</span><span class="n">_idxMap</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
<span class="n">revMap</span><span class="p">[</span><span class="n">v</span><span class="p">]</span><span class="o">=</span><span class="n">k</span>
<span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">:</span>
<span class="n">oatom</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">GetAtomWithIdx</span><span class="p">(</span><span class="n">revMap</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span>
<span class="n">oatom</span><span class="o">.</span><span class="n">SetNoImplicit</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
<span class="n">oatom</span><span class="o">.</span><span class="n">SetNumExplicitHs</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">ok</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">None</span>
<span class="k">return</span> <span class="n">m</span>
</pre></div>
</div>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">smis</span><span class="o">=</span> <span class="p">(</span><span class="s">'O=c1ccc2ccccc2n1'</span><span class="p">,</span>
<span class="s">'Cc1nnnn1C'</span><span class="p">,</span>
<span class="s">'CCc1ccc2nc(=O)c(cc2c1)Cc1nnnn1C1CCCCC1'</span><span class="p">,</span>
<span class="s">'c1cnc2cc3ccnc3cc12'</span><span class="p">,</span>
<span class="s">'c1cc2cc3ccnc3cc2n1'</span><span class="p">,</span>
<span class="s">'O=c1ccnc(c1)-c1cnc2cc3ccnc3cc12'</span><span class="p">,</span>
<span class="s">'O=c1ccnc(c1)-c1cc1'</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">for</span> <span class="n">smi</span> <span class="ow">in</span> <span class="n">smis</span><span class="p">:</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="n">smi</span><span class="p">,</span><span class="bp">False</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">m</span><span class="o">.</span><span class="n">UpdatePropertyCache</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="n">cp</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">Mol</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeMol</span><span class="p">(</span><span class="n">cp</span><span class="p">)</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">cp</span>
<span class="k">print</span> <span class="s">'fine:'</span><span class="p">,</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolToSmiles</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="n">nm</span><span class="o">=</span><span class="n">AdjustAromaticNs</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="k">if</span> <span class="n">nm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">Chem</span><span class="o">.</span><span class="n">SanitizeMol</span><span class="p">(</span><span class="n">nm</span><span class="p">)</span>
<span class="k">print</span> <span class="s">'fixed:'</span><span class="p">,</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolToSmiles</span><span class="p">(</span><span class="n">nm</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span> <span class="s">'still broken:'</span><span class="p">,</span><span class="n">smi</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>fixed: O=c1ccc2ccccc2[nH]1
fine: Cc1nnnn1C
fixed: CCc1ccc2[nH]c(=O)c(Cc3nnnn3C3CCCCC3)cc2c1
fine: C1=Cc2cc3c(cc2=N1)C=CN=3
fine: C1=Cc2cc3c(cc2=N1)N=CC=3
fixed: O=c1cc[nH]c(C2=CN=c3cc4c(cc32)=NC=C4)c1
still broken: O=c1ccnc(c1)-c1cc1
</pre></div>
</div>
</div>
<div class="section" id="parallel-conformation-generation">
<h3>Parallel conformation generation<a class="headerlink" href="#parallel-conformation-generation" title="Permalink to this headline">¶</a></h3>
<p>Mailing list discussion:
<a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg02648.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg02648.html</a></p>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="sd">""" contribution from Andrew Dalke """</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="c"># Download this from http://pypi.python.org/pypi/futures</span>
<span class="kn">from</span> <span class="nn">concurrent</span> <span class="kn">import</span> <span class="n">futures</span>
<span class="c"># Download this from http://pypi.python.org/pypi/progressbar</span>
<span class="kn">import</span> <span class="nn">progressbar</span>
<span class="c">## On my machine, it takes 39 seconds with 1 worker and 10 seconds with 4.</span>
<span class="c">## 29.055u 0.102s 0:28.68 101.6% 0+0k 0+3io 0pf+0w</span>
<span class="c">#max_workers=1</span>
<span class="c">## With 4 threads it takes 11 seconds.</span>
<span class="c">## 34.933u 0.188s 0:10.89 322.4% 0+0k 125+1io 0pf+0w</span>
<span class="n">max_workers</span><span class="o">=</span><span class="mi">4</span>
<span class="c"># (The "u"ser time includes time spend in the children processes.</span>
<span class="c"># The wall-clock time is 28.68 and 10.89 seconds, respectively.)</span>
<span class="c"># This function is called in the subprocess.</span>
<span class="c"># The parameters (molecule and number of conformers) are passed via a Python</span>
<span class="k">def</span> <span class="nf">generateconformations</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">AddHs</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="n">ids</span><span class="o">=</span><span class="n">AllChem</span><span class="o">.</span><span class="n">EmbedMultipleConfs</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">numConfs</span><span class="o">=</span><span class="n">n</span><span class="p">)</span>
<span class="k">for</span> <span class="nb">id</span> <span class="ow">in</span> <span class="n">ids</span><span class="p">:</span>
<span class="n">AllChem</span><span class="o">.</span><span class="n">UFFOptimizeMolecule</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">confId</span><span class="o">=</span><span class="nb">id</span><span class="p">)</span>
<span class="c"># EmbedMultipleConfs returns a Boost-wrapped type which</span>
<span class="c"># cannot be pickled. Convert it to a Python list, which can.</span>
<span class="k">return</span> <span class="n">m</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">ids</span><span class="p">)</span>
<span class="n">smi_input_file</span><span class="p">,</span> <span class="n">sdf_output_file</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
<span class="n">writer</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">SDWriter</span><span class="p">(</span><span class="n">sdf_output_file</span><span class="p">)</span>
<span class="n">suppl</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">SmilesMolSupplier</span><span class="p">(</span><span class="n">smi_input_file</span><span class="p">,</span> <span class="n">titleLine</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="k">with</span> <span class="n">futures</span><span class="o">.</span><span class="n">ProcessPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="n">max_workers</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
<span class="c"># Submit a set of asynchronous jobs</span>
<span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">mol</span> <span class="ow">in</span> <span class="n">suppl</span><span class="p">:</span>
<span class="k">if</span> <span class="n">mol</span><span class="p">:</span>
<span class="n">job</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">generateconformations</span><span class="p">,</span> <span class="n">mol</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="n">jobs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
<span class="n">widgets</span> <span class="o">=</span> <span class="p">[</span><span class="s">"Generating conformations; "</span><span class="p">,</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">Percentage</span><span class="p">(),</span> <span class="s">" "</span><span class="p">,</span>
<span class="n">progressbar</span><span class="o">.</span><span class="n">ETA</span><span class="p">(),</span> <span class="s">" "</span><span class="p">,</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">Bar</span><span class="p">()]</span>
<span class="n">pbar</span> <span class="o">=</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">ProgressBar</span><span class="p">(</span><span class="n">widgets</span><span class="o">=</span><span class="n">widgets</span><span class="p">,</span> <span class="n">maxval</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">jobs</span><span class="p">))</span>
<span class="k">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">pbar</span><span class="p">(</span><span class="n">futures</span><span class="o">.</span><span class="n">as_completed</span><span class="p">(</span><span class="n">jobs</span><span class="p">)):</span>
<span class="n">mol</span><span class="p">,</span><span class="n">ids</span><span class="o">=</span><span class="n">job</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
<span class="k">for</span> <span class="nb">id</span> <span class="ow">in</span> <span class="n">ids</span><span class="p">:</span>
<span class="n">writer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">confId</span><span class="o">=</span><span class="nb">id</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="neutralizing-charged-molecules">
<h3>Neutralizing Charged Molecules<a class="headerlink" href="#neutralizing-charged-molecules" title="Permalink to this headline">¶</a></h3>
<p>Mailing list discussion:
<a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg02648.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg02648.html</a></p>
<p>Wiki page:
<a class="reference external" href="http://code.google.com/p/rdkit/wiki/NeutralisingCompounds">http://code.google.com/p/rdkit/wiki/NeutralisingCompounds</a></p>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="sd">""" contribution from Hans de Winter """</span>
<span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="k">def</span> <span class="nf">_InitialiseNeutralisationReactions</span><span class="p">():</span>
<span class="n">patts</span><span class="o">=</span> <span class="p">(</span>
<span class="c"># Imidazoles</span>
<span class="p">(</span><span class="s">'[n+;H]'</span><span class="p">,</span><span class="s">'n'</span><span class="p">),</span>
<span class="c"># Amines</span>
<span class="p">(</span><span class="s">'[N+;!H0]'</span><span class="p">,</span><span class="s">'N'</span><span class="p">),</span>
<span class="c"># Carboxylic acids and alcohols</span>
<span class="p">(</span><span class="s">'[$([O-]);!$([O-][#7])]'</span><span class="p">,</span><span class="s">'O'</span><span class="p">),</span>
<span class="c"># Thiols</span>
<span class="p">(</span><span class="s">'[S-;X1]'</span><span class="p">,</span><span class="s">'S'</span><span class="p">),</span>
<span class="c"># Sulfonamides</span>
<span class="p">(</span><span class="s">'[$([N-;X2]S(=O)=O)]'</span><span class="p">,</span><span class="s">'N'</span><span class="p">),</span>
<span class="c"># Enamines</span>
<span class="p">(</span><span class="s">'[$([N-;X2][C,N]=C)]'</span><span class="p">,</span><span class="s">'N'</span><span class="p">),</span>
<span class="c"># Tetrazoles</span>
<span class="p">(</span><span class="s">'[n-]'</span><span class="p">,</span><span class="s">'[nH]'</span><span class="p">),</span>
<span class="c"># Sulfoxides</span>
<span class="p">(</span><span class="s">'[$([S-]=O)]'</span><span class="p">,</span><span class="s">'S'</span><span class="p">),</span>
<span class="c"># Amides</span>
<span class="p">(</span><span class="s">'[$([N-]C=O)]'</span><span class="p">,</span><span class="s">'N'</span><span class="p">),</span>
<span class="p">)</span>
<span class="k">return</span> <span class="p">[(</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmarts</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="bp">False</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="n">patts</span><span class="p">]</span>
<span class="n">_reactions</span><span class="o">=</span><span class="bp">None</span>
<span class="k">def</span> <span class="nf">NeutraliseCharges</span><span class="p">(</span><span class="n">smiles</span><span class="p">,</span> <span class="n">reactions</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="k">global</span> <span class="n">_reactions</span>
<span class="k">if</span> <span class="n">reactions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="k">if</span> <span class="n">_reactions</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">_reactions</span><span class="o">=</span><span class="n">_InitialiseNeutralisationReactions</span><span class="p">()</span>
<span class="n">reactions</span><span class="o">=</span><span class="n">_reactions</span>
<span class="n">mol</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="n">smiles</span><span class="p">)</span>
<span class="n">replaced</span> <span class="o">=</span> <span class="bp">False</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,(</span><span class="n">reactant</span><span class="p">,</span> <span class="n">product</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">reactions</span><span class="p">):</span>
<span class="k">while</span> <span class="n">mol</span><span class="o">.</span><span class="n">HasSubstructMatch</span><span class="p">(</span><span class="n">reactant</span><span class="p">):</span>
<span class="n">replaced</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">rms</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">ReplaceSubstructs</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">reactant</span><span class="p">,</span> <span class="n">product</span><span class="p">)</span>
<span class="n">mol</span> <span class="o">=</span> <span class="n">rms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="n">replaced</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolToSmiles</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span><span class="bp">True</span><span class="p">),</span> <span class="bp">True</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="n">smiles</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">smis</span><span class="o">=</span><span class="p">(</span><span class="s">"c1cccc[nH+]1"</span><span class="p">,</span>
<span class="s">"C[N+](C)(C)C"</span><span class="p">,</span><span class="s">"c1ccccc1[NH3+]"</span><span class="p">,</span>
<span class="s">"CC(=O)[O-]"</span><span class="p">,</span><span class="s">"c1ccccc1[O-]"</span><span class="p">,</span>
<span class="s">"CCS"</span><span class="p">,</span>
<span class="s">"C[N-]S(=O)(=O)C"</span><span class="p">,</span>
<span class="s">"C[N-]C=C"</span><span class="p">,</span><span class="s">"C[N-]N=C"</span><span class="p">,</span>
<span class="s">"c1ccc[n-]1"</span><span class="p">,</span>
<span class="s">"CC[N-]C(=O)CC"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">smi</span> <span class="ow">in</span> <span class="n">smis</span><span class="p">:</span>
<span class="p">(</span><span class="n">molSmiles</span><span class="p">,</span> <span class="n">neutralised</span><span class="p">)</span> <span class="o">=</span> <span class="n">NeutraliseCharges</span><span class="p">(</span><span class="n">smi</span><span class="p">)</span>
<span class="k">print</span> <span class="n">smi</span><span class="p">,</span><span class="s">"->"</span><span class="p">,</span><span class="n">molSmiles</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>c1cccc[nH+]1 -> c1ccncc1
C[N+](C)(C)C -> C[N+](C)(C)C
c1ccccc1[NH3+] -> Nc1ccccc1
CC(=O)[O-] -> CC(=O)O
c1ccccc1[O-] -> Oc1ccccc1
CCS -> CCS
C[N-]S(=O)(=O)C -> CNS(C)(=O)=O
C[N-]C=C -> C=CNC
C[N-]N=C -> C=NNC
c1ccc[n-]1 -> c1cc[nH]c1
CC[N-]C(=O)CC -> CCNC(=O)CC
</pre></div>
</div>
</div>
<div class="section" id="d-functionality-in-the-rdkit">
<h3>3D functionality in the RDKit<a class="headerlink" href="#d-functionality-in-the-rdkit" title="Permalink to this headline">¶</a></h3>
<p>The RDKit contains a range of 3D functionalities such as:</p>
<table border="1" class="docutils">
<colgroup>
<col width="100%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Shape alignment</td>
</tr>
<tr class="row-even"><td>RMS calculation</td>
</tr>
<tr class="row-odd"><td>Shape Tanimoto Distance</td>
</tr>
<tr class="row-even"><td>Shape Protrude Distance</td>
</tr>
<tr class="row-odd"><td>3D pharmacophore fingerprint</td>
</tr>
<tr class="row-even"><td>Torsion fingerprint (deviation)</td>
</tr>
</tbody>
</table>
<p>There are two alignment methods currently available in the RDKit.
As an example we use two crystal structures from the PDB of the same molecule.</p>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span><span class="p">,</span> <span class="n">RDConfig</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span><span class="p">,</span> <span class="n">rdMolAlign</span>
<span class="c"># The reference molecule</span>
<span class="n">ref</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="c"># The PDB conformations</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1DWD_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol1</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1PPC_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="c"># Align them</span>
<span class="n">rms</span> <span class="o">=</span> <span class="n">rdMolAlign</span><span class="o">.</span><span class="n">AlignMol</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="k">print</span> <span class="n">rms</span>
<span class="c"># Align them with OPEN3DAlign</span>
<span class="n">pyO3A</span> <span class="o">=</span> <span class="n">rdMolAlign</span><span class="o">.</span><span class="n">GetO3A</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="n">score</span> <span class="o">=</span> <span class="n">pyO3A</span><span class="o">.</span><span class="n">Align</span><span class="p">()</span>
<span class="k">print</span> <span class="n">score</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>1.55001955728
0.376459885045
</pre></div>
</div>
<p>If a molecule contains more than one conformer, they can be aligned
with respect to the first conformer. If a list is provided to the
option RMSlist, the RMS value from the alignment are stored.
The RMS value of two conformers of a molecule can also be calculated
separately, either with or without alignment (using the flag prealigned).</p>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="n">mol</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="n">cids</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">EmbedMultipleConfs</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">numConfs</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">maxAttempts</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">pruneRmsThresh</span><span class="o">=</span><span class="mf">0.1</span><span class="p">)</span>
<span class="k">print</span> <span class="nb">len</span><span class="p">(</span><span class="n">cids</span><span class="p">)</span>
<span class="c"># align the conformers</span>
<span class="n">rmslist</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">AllChem</span><span class="o">.</span><span class="n">AlignMolConformers</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">RMSlist</span><span class="o">=</span><span class="n">rmslist</span><span class="p">)</span>
<span class="k">print</span> <span class="nb">len</span><span class="p">(</span><span class="n">rmslist</span><span class="p">)</span>
<span class="c"># calculate RMS of confomers 1 and 9 separately</span>
<span class="n">rms</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">GetConformerRMS</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="n">prealigned</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>50
49
</pre></div>
</div>
<p>For shape comparison, the RDKit provides two Shape-based distances
that can be calculated for two prealigned molecules or conformers.
Shape protrude distance focusses on the volume mismatch, while
Shape Tanimoto distance takes the volume overlay overall into account.</p>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span><span class="p">,</span> <span class="n">RDConfig</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span><span class="p">,</span> <span class="n">rdMolAlign</span><span class="p">,</span> <span class="n">rdShapeHelpers</span>
<span class="n">ref</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1DWD_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol1</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1PPC_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="n">rms</span> <span class="o">=</span> <span class="n">rdMolAlign</span><span class="o">.</span><span class="n">AlignMol</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="n">tani</span> <span class="o">=</span> <span class="n">rdShapeHelpers</span><span class="o">.</span><span class="n">ShapeTanimotoDist</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="n">prtr</span> <span class="o">=</span> <span class="n">rdShapeHelpers</span><span class="o">.</span><span class="n">ShapeProtrudeDist</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="k">print</span> <span class="n">rms</span><span class="p">,</span> <span class="n">tani</span><span class="p">,</span> <span class="n">prtr</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>1.55001955728 0.18069102331 0.0962800875274
</pre></div>
</div>
<p>A 3D pharmacophore fingerprint can be calculated using the RDKit
by feeding a 3D distance matrix to the 2D-pharmacophore machinery.</p>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span><span class="p">,</span> <span class="n">DataStructs</span><span class="p">,</span> <span class="n">RDConfig</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem.Pharm2D</span> <span class="kn">import</span> <span class="n">Gobbi_Pharm2D</span><span class="p">,</span> <span class="n">Generate</span>
<span class="n">ref</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1DWD_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol1</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1PPC_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="c"># pharmacophore fingerprint</span>
<span class="n">factory</span> <span class="o">=</span> <span class="n">Gobbi_Pharm2D</span><span class="o">.</span><span class="n">factory</span>
<span class="n">fp1</span> <span class="o">=</span> <span class="n">Generate</span><span class="o">.</span><span class="n">Gen2DFingerprint</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">factory</span><span class="p">,</span> <span class="n">dMat</span><span class="o">=</span><span class="n">Chem</span><span class="o">.</span><span class="n">Get3DDistanceMatrix</span><span class="p">(</span><span class="n">mol1</span><span class="p">))</span>
<span class="n">fp2</span> <span class="o">=</span> <span class="n">Generate</span><span class="o">.</span><span class="n">Gen2DFingerprint</span><span class="p">(</span><span class="n">mol2</span><span class="p">,</span> <span class="n">factory</span><span class="p">,</span> <span class="n">dMat</span><span class="o">=</span><span class="n">Chem</span><span class="o">.</span><span class="n">Get3DDistanceMatrix</span><span class="p">(</span><span class="n">mol2</span><span class="p">))</span>
<span class="c"># Tanimoto similarity</span>
<span class="n">tani</span> <span class="o">=</span> <span class="n">DataStructs</span><span class="o">.</span><span class="n">TanimotoSimilarity</span><span class="p">(</span><span class="n">fp1</span><span class="p">,</span> <span class="n">fp2</span><span class="p">)</span>
<span class="k">print</span> <span class="n">tani</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>0.451665312754
</pre></div>
</div>
<p>The RDKit provides an implementation of the torsion fingerprint
deviation (TFD) approach developed by Schulz-Gasch et al.
(J. Chem. Inf. Model, 52, 1499, 2012). The RDKit implementation
allows the user to customize the torsion fingerprints:</p>
<blockquote>
<div><ul class="simple">
<li>In the original approach, the torsions are weighted based on
their distance to the center of the molecule. By default, this
weighting is performed, but can be turned off using the flag
useWeights=False</li>
<li>The similarity between atoms is determined by comparing the
hash codes from the Morgan algorithm at a given radius (default:
radius = 2).</li>
<li>In the original approach, the maximal deviation used for
normalization is 180.0 degrees for all torsions (default). If
maxDev=’spec’, a torsion-type dependent maximal deviation is
used for the normalization.</li>
</ul>
</div></blockquote>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span><span class="p">,</span> <span class="n">RDConfig</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span><span class="p">,</span> <span class="n">TorsionFingerprints</span>
<span class="n">ref</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1DWD_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol1</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1PPC_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="n">tfd1</span> <span class="o">=</span> <span class="n">TorsionFingerprints</span><span class="o">.</span><span class="n">GetTFDBetweenMolecules</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">)</span>
<span class="n">tfd2</span> <span class="o">=</span> <span class="n">TorsionFingerprints</span><span class="o">.</span><span class="n">GetTFDBetweenMolecules</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">,</span> <span class="n">useWeights</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">tfd3</span> <span class="o">=</span> <span class="n">TorsionFingerprints</span><span class="o">.</span><span class="n">GetTFDBetweenMolecules</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">mol2</span><span class="p">,</span> <span class="n">maxDev</span><span class="o">=</span><span class="s">'spec'</span><span class="p">)</span>
<span class="k">print</span> <span class="n">tfd1</span><span class="p">,</span> <span class="n">tfd2</span><span class="p">,</span> <span class="n">tfd3</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>[0.0645...] [0.168...] [0.0675...]
</pre></div>
</div>
<p>If the TFD between conformers of the same molecule is to be
calculated, the function GetTFDBetweenConformers() should be
used for performance reasons.</p>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span><span class="p">,</span> <span class="n">RDConfig</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span><span class="p">,</span> <span class="n">TorsionFingerprints</span>
<span class="n">ref</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1DWD_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol1</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">AssignBondOrdersFromTemplate</span><span class="p">(</span><span class="n">ref</span><span class="p">,</span> <span class="n">mol1</span><span class="p">)</span>
<span class="n">mol2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromPDBFile</span><span class="p">(</span><span class="n">RDConfig</span><span class="o">.</span><span class="n">RDBaseDir</span><span class="o">+</span><span class="s">'/rdkit/Chem/test_data/1PPC_ligand.pdb'</span><span class="p">)</span>
<span class="n">mol1</span><span class="o">.</span><span class="n">AddConformer</span><span class="p">(</span><span class="n">mol2</span><span class="o">.</span><span class="n">GetConformer</span><span class="p">(),</span> <span class="n">assignId</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">tfd</span> <span class="o">=</span> <span class="n">TorsionFingerprints</span><span class="o">.</span><span class="n">GetTFDBetweenConformers</span><span class="p">(</span><span class="n">mol1</span><span class="p">,</span> <span class="n">confIds1</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">confIds2</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="k">print</span> <span class="n">tfd</span>
</pre></div>
</div>
<p>This produces:</p>
<div class="highlight-none"><div class="highlight"><pre>[0.0645...]
</pre></div>
</div>
<p>For the conformer RMS and TFD values, the RDKit provides
convenience functions that calculated directly the symmetric
matrix which can be fed into a clustering algorithm such
as Butina clustering. The flag reordering ensures that the
number of neighbors of the unclustered molecules is updated
every time a cluster is created.</p>
<p>Examples of using it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span><span class="p">,</span> <span class="n">TorsionFingerprints</span>
<span class="kn">from</span> <span class="nn">rdkit.ML.Cluster</span> <span class="kn">import</span> <span class="n">Butina</span>
<span class="n">mol</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'NC(=[NH2+])c1ccc(C[C@@H](NC(=O)CNS(=O)(=O)c2ccc3ccccc3c2)C(=O)N2CCCCC2)cc1'</span><span class="p">)</span>
<span class="n">cids</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">EmbedMultipleConfs</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">numConfs</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">maxAttempts</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">pruneRmsThresh</span><span class="o">=</span><span class="mf">0.1</span><span class="p">)</span>
<span class="c"># RMS matrix</span>
<span class="n">rmsmat</span> <span class="o">=</span> <span class="n">AllChem</span><span class="o">.</span><span class="n">GetConformerRMSMatrix</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span> <span class="n">prealigned</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="c"># TFD matrix</span>
<span class="n">tfdmat</span> <span class="o">=</span> <span class="n">TorsionFingerprints</span><span class="o">.</span><span class="n">GetTFDMatrix</span><span class="p">(</span><span class="n">mol</span><span class="p">)</span>
<span class="c"># clustering</span>
<span class="n">num</span> <span class="o">=</span> <span class="n">mol</span><span class="o">.</span><span class="n">GetNumConformers</span><span class="p">()</span>
<span class="n">rms_clusters</span> <span class="o">=</span> <span class="n">Butina</span><span class="o">.</span><span class="n">ClusterData</span><span class="p">(</span><span class="n">rmsmat</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">isDistData</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">reordering</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">tfd_clusters</span> <span class="o">=</span> <span class="n">Butina</span><span class="o">.</span><span class="n">ClusterData</span><span class="p">(</span><span class="n">tfdmat</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="n">isDistData</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">reordering</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-scikit-learn-with-rdkit">
<h3>Using scikit-learn with RDKit<a class="headerlink" href="#using-scikit-learn-with-rdkit" title="Permalink to this headline">¶</a></h3>
<p>scikit-learn is a machine-learning library for Python
containing a variety of supervised and unsupervised methods.
The documention can be found here:
<a class="reference external" href="http://scikit-learn.org/stable/user_guide.html">http://scikit-learn.org/stable/user_guide.html</a></p>
<p>RDKit fingerprints can be used to train machine-learning
models from scikit-learn. Here is an example for random forest:</p>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span><span class="p">,</span> <span class="n">DataStructs</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="kn">from</span> <span class="nn">sklearn.ensemble</span> <span class="kn">import</span> <span class="n">RandomForestClassifier</span>
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="c"># generate four molecules</span>
<span class="n">m1</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'c1ccccc1'</span><span class="p">)</span>
<span class="n">m2</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'c1ccccc1CC'</span><span class="p">)</span>
<span class="n">m3</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'c1ccncc1'</span><span class="p">)</span>
<span class="n">m4</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'c1ccncc1CC'</span><span class="p">)</span>
<span class="n">mols</span> <span class="o">=</span> <span class="p">[</span><span class="n">m1</span><span class="p">,</span> <span class="n">m2</span><span class="p">,</span> <span class="n">m3</span><span class="p">,</span> <span class="n">m4</span><span class="p">]</span>
<span class="c"># generate fingeprints: Morgan fingerprint with radius 2</span>
<span class="n">fps</span> <span class="o">=</span> <span class="p">[</span><span class="n">AllChem</span><span class="o">.</span><span class="n">GetMorganFingerprintAsBitVect</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mols</span><span class="p">]</span>
<span class="c"># convert the RDKit explicit vectors into numpy arrays</span>
<span class="n">np_fps</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">fp</span> <span class="ow">in</span> <span class="n">fps</span><span class="p">:</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1</span><span class="p">,))</span>
<span class="n">DataStructs</span><span class="o">.</span><span class="n">ConvertToNumpyArray</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">arr</span><span class="p">)</span>
<span class="n">np_fps</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="c"># get a random forest classifiert with 100 trees</span>
<span class="n">rf</span> <span class="o">=</span> <span class="n">RandomForestClassifier</span><span class="p">(</span><span class="n">n_estimators</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">1123</span><span class="p">)</span>
<span class="c"># train the random forest</span>
<span class="c"># with the first two molecules being actives (class 1) and</span>
<span class="c"># the last two being inactives (class 0)</span>
<span class="n">ys_fit</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">rf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">np_fps</span><span class="p">,</span> <span class="n">ys_fit</span><span class="p">)</span>
<span class="c"># use the random forest to predict a new molecule</span>
<span class="n">m5</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'c1ccccc1O'</span><span class="p">)</span>
<span class="n">fp</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1</span><span class="p">,))</span>
<span class="n">DataStructs</span><span class="o">.</span><span class="n">ConvertToNumpyArray</span><span class="p">(</span><span class="n">AllChem</span><span class="o">.</span><span class="n">GetMorganFingerprintAsBitVect</span><span class="p">(</span><span class="n">m5</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">fp</span><span class="p">)</span>
<span class="k">print</span> <span class="n">rf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
<span class="k">print</span> <span class="n">rf</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
</pre></div>
</div>
<p>The output with scikit-learn version 0.13 is:</p>
<blockquote>
<div><p>[1]</p>
<p>[[ 0.14 0.86]]</p>
</div></blockquote>
<p>Generating a similarity map for this model.</p>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit.Chem.Draw</span> <span class="kn">import</span> <span class="n">SimilarityMaps</span>
<span class="c"># helper function</span>
<span class="k">def</span> <span class="nf">getProba</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">predictionFunction</span><span class="p">):</span>
<span class="k">return</span> <span class="n">predictionFunction</span><span class="p">(</span><span class="n">fp</span><span class="p">)[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
<span class="n">m5</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="s">'c1ccccc1O'</span><span class="p">)</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">maxweight</span> <span class="o">=</span> <span class="n">SimilarityMaps</span><span class="o">.</span><span class="n">GetSimilarityMapForModel</span><span class="p">(</span><span class="n">m5</span><span class="p">,</span> <span class="n">SimilarityMaps</span><span class="o">.</span><span class="n">GetMorganFingerprint</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">getProba</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">rf</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">))</span>
</pre></div>
</div>
<p>This produces:</p>
<img alt="_images/similarity_map_rf.png" src="_images/similarity_map_rf.png" />
</div>
<div class="section" id="using-custom-mcs-atom-types">
<h3>Using custom MCS atom types<a class="headerlink" href="#using-custom-mcs-atom-types" title="Permalink to this headline">¶</a></h3>
<p>Mailing list discussion:
<a class="reference external" href="http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg03676.html">http://www.mail-archive.com/rdkit-discuss@lists.sourceforge.net/msg03676.html</a></p>
<p>IPython notebook:
<a class="reference external" href="http://nbviewer.ipython.org/gist/greglandrum/8351725">http://nbviewer.ipython.org/gist/greglandrum/8351725</a>
<a class="reference external" href="https://gist.github.com/greglandrum/8351725">https://gist.github.com/greglandrum/8351725</a></p>
<p>The goal is to be able to use custom atom types in the MCS code, yet
still be able to get a readable SMILES for the MCS. We will use the
MCS code’s option to use isotope information in the matching and then
set bogus isotope values that contain our isotope information.</p>
<p>The code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">rdFMCS</span>
<span class="c"># our test molecules:</span>
<span class="n">smis</span><span class="o">=</span><span class="p">[</span><span class="s">"COc1ccc(C(Nc2nc3c(ncn3COCC=O)c(=O)[nH]2)(c2ccccc2)c2ccccc2)cc1"</span><span class="p">,</span>
<span class="s">"COc1ccc(C(Nc2nc3c(ncn3COC(CO)(CO)CO)c(=O)[nH]2)(c2ccccc2)c2ccccc2)cc1"</span><span class="p">]</span>
<span class="n">ms</span> <span class="o">=</span> <span class="p">[</span><span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">smis</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">label</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
<span class="s">" a simple hash combining atom number and hybridization "</span>
<span class="k">return</span> <span class="mi">100</span><span class="o">*</span><span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">GetHybridization</span><span class="p">())</span><span class="o">+</span><span class="n">a</span><span class="o">.</span><span class="n">GetAtomicNum</span><span class="p">()</span>
<span class="c"># copy the molecules, since we will be changing them</span>
<span class="n">nms</span> <span class="o">=</span> <span class="p">[</span><span class="n">Chem</span><span class="o">.</span><span class="n">Mol</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">ms</span><span class="p">]</span>
<span class="k">for</span> <span class="n">nm</span> <span class="ow">in</span> <span class="n">nms</span><span class="p">:</span>
<span class="k">for</span> <span class="n">at</span> <span class="ow">in</span> <span class="n">nm</span><span class="o">.</span><span class="n">GetAtoms</span><span class="p">():</span>
<span class="n">at</span><span class="o">.</span><span class="n">SetIsotope</span><span class="p">(</span><span class="n">label</span><span class="p">(</span><span class="n">at</span><span class="p">))</span>
<span class="n">mcs</span><span class="o">=</span><span class="n">rdFMCS</span><span class="o">.</span><span class="n">FindMCS</span><span class="p">(</span><span class="n">nms</span><span class="p">,</span><span class="n">atomCompare</span><span class="o">=</span><span class="n">rdFMCS</span><span class="o">.</span><span class="n">AtomCompare</span><span class="o">.</span><span class="n">CompareIsotopes</span><span class="p">)</span>
<span class="k">print</span> <span class="n">mcs</span><span class="o">.</span><span class="n">smartsString</span>
</pre></div>
</div>
<p>This generates the following output:</p>
<div class="highlight-none"><div class="highlight"><pre>[406*]-[308*]-[306*]1:[306*]:[306*]:[306*](:[306*]:[306*]:1)-[406*](-[307*]-[306*]1:[307*]:[306*]2:[306*](:[306*](:[307*]:1)=[308*]):[307*]:[306*]:[307*]:2-[406*]-[408*]-[406*])(-[306*]1:[306*]:[306*]:[306*]:[306*]:[306*]:1)-[306*]1:[306*]:[306*]:[306*]:[306*]:[306*]:1
</pre></div>
</div>
<p>That’s what we asked for, but it’s not exactly readable. We can get to a more readable form in a two step process:</p>
<blockquote>
<div><ol class="arabic simple">
<li>Do a substructure match of the MCS onto a copied molecule</li>
<li>Generate SMILES for the original molecule, using only the atoms that matched in the copy.</li>
</ol>
</div></blockquote>
<p>This works because we know that the atom indices in the copies and the original molecules are the same.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">getMCSSmiles</span><span class="p">(</span><span class="n">mol</span><span class="p">,</span><span class="n">labelledMol</span><span class="p">,</span><span class="n">mcs</span><span class="p">):</span>
<span class="n">mcsp</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmarts</span><span class="p">(</span><span class="n">mcs</span><span class="o">.</span><span class="n">smartsString</span><span class="p">)</span>
<span class="n">match</span> <span class="o">=</span> <span class="n">labelledMol</span><span class="o">.</span><span class="n">GetSubstructMatch</span><span class="p">(</span><span class="n">mcsp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFragmentToSmiles</span><span class="p">(</span><span class="n">ms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">atomsToUse</span><span class="o">=</span><span class="n">match</span><span class="p">,</span>
<span class="n">isomericSmiles</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">canonical</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="k">print</span> <span class="n">getMCSSmiles</span><span class="p">(</span><span class="n">ms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">nms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">mcs</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-none"><div class="highlight"><pre>COc1ccc(C(Nc2nc3c(ncn3COC)c(=O)[nH]2)(c2ccccc2)c2ccccc2)cc1
</pre></div>
</div>
<p>That’s what we were looking for.</p>
</div>
<div class="section" id="clustering-molecules">
<h3>Clustering molecules<a class="headerlink" href="#clustering-molecules" title="Permalink to this headline">¶</a></h3>
<p>For large sets of molecules (more than 1000-2000), it’s most efficient to use the Butina clustering algorithm.</p>
<p>Here’s some code for doing that for a set of fingerprints:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">ClusterFps</span><span class="p">(</span><span class="n">fps</span><span class="p">,</span><span class="n">cutoff</span><span class="o">=</span><span class="mf">0.2</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">DataStructs</span>
<span class="kn">from</span> <span class="nn">rdkit.ML.Cluster</span> <span class="kn">import</span> <span class="n">Butina</span>
<span class="c"># first generate the distance matrix:</span>
<span class="n">dists</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">nfps</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">fps</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">nfps</span><span class="p">):</span>
<span class="n">sims</span> <span class="o">=</span> <span class="n">DataStructs</span><span class="o">.</span><span class="n">BulkTanimotoSimilarity</span><span class="p">(</span><span class="n">fps</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">fps</span><span class="p">[:</span><span class="n">i</span><span class="p">])</span>
<span class="n">dists</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="mi">1</span><span class="o">-</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">sims</span><span class="p">])</span>
<span class="c"># now cluster the data:</span>
<span class="n">cs</span> <span class="o">=</span> <span class="n">Butina</span><span class="o">.</span><span class="n">ClusterData</span><span class="p">(</span><span class="n">dists</span><span class="p">,</span><span class="n">nfps</span><span class="p">,</span><span class="n">cutoff</span><span class="p">,</span><span class="n">isDistData</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">cs</span>
</pre></div>
</div>
<p>The return value is a tuple of clusters, where each cluster is a tuple of ids.</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">from</span> <span class="nn">rdkit.Chem</span> <span class="kn">import</span> <span class="n">AllChem</span>
<span class="kn">import</span> <span class="nn">gzip</span>
<span class="n">ms</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">Chem</span><span class="o">.</span><span class="n">ForwardSDMolSupplier</span><span class="p">(</span><span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">'zdd.sdf.gz'</span><span class="p">))</span> <span class="k">if</span> <span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">]</span>
<span class="n">fps</span> <span class="o">=</span> <span class="p">[</span><span class="n">AllChem</span><span class="o">.</span><span class="n">GetMorganFingerprintAsBitVect</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1024</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">ms</span><span class="p">]</span>
<span class="n">clusters</span><span class="o">=</span><span class="n">ClusterFps</span><span class="p">(</span><span class="n">fps</span><span class="p">,</span><span class="n">cutoff</span><span class="o">=</span><span class="mf">0.4</span><span class="p">)</span>
</pre></div>
</div>
<p>The variable <cite>clusters</cite> contains the results:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="n">clusters</span><span class="p">[</span><span class="mi">200</span><span class="p">]</span>
<span class="go">(6164, 1400, 1403, 1537, 1543, 6575, 6759)</span>
</pre></div>
</div>
<p>That cluster contains 7 points, the centroid is point 6164.</p>
</div>
</div>
<div class="section" id="license">
<h2>License<a class="headerlink" href="#license" title="Permalink to this headline">¶</a></h2>
<p>This document is copyright (C) 2012-2015 by Greg Landrum</p>
<p>This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 License.
To view a copy of this license, visit <a class="reference external" href="http://creativecommons.org/licenses/by-sa/3.0/">http://creativecommons.org/licenses/by-sa/3.0/</a> or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.</p>
<p>The intent of this license is similar to that of the RDKit itself.
In simple words: “Do whatever you want with it, but please give us some credit.”</p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="Cartridge.html" title="The RDKit database cartridge"
>next</a> |</li>
<li class="right" >
<a href="RDKit_Book.html" title="The RDKit Book"
>previous</a> |</li>
<li><a href="index.html">The RDKit 2015.03.1 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2014, Greg Landrum.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
</div>
</body>
</html>
|