Skip to content

Commit

Permalink
update posts
Browse files Browse the repository at this point in the history
  • Loading branch information
tlint101 committed Feb 27, 2024
1 parent b5b3f8a commit dffeadb
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ <h5 class="quarto-listing-category-title">Categories</h5><div class="quarto-list
</div>
</div>
<div class="list quarto-listing-default">
<div class="quarto-post image-right" data-index="0" data-categories="RDKit,Drawing" data-listing-date-sort="1708963200000" data-listing-file-modified-sort="1709029006255" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="4">
<div class="quarto-post image-right" data-index="0" data-categories="RDKit,Drawing" data-listing-date-sort="1708963200000" data-listing-file-modified-sort="1709029155710" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="4">
<div class="thumbnail">
<p><a href="./posts/10-Draw-Molecules.html"> <p class="card-img-top"><img src="posts/10-Draw-Molecules_files/figure-html/cell-3-output-1.png" class="thumbnail-image card-img"/></p> </a></p>
</div>
Expand Down
45 changes: 33 additions & 12 deletions docs/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,29 @@
<section id="set-up-data" class="level2">
<h2 class="anchored" data-anchor-id="set-up-data">Set Up Data</h2>
<p>In this quick tutorial, we will draw 2 molecules. We’ll start small and get more complicated. I want to focus on drawing molecular structures for two drugs many of us may know - Acetylsalicylic Acid and Ibuprofen. Or for anyone well verse with the American market, aspirin and advil, respectively. The data is set up as follows:</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.232667Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.228057Z&quot;}" data-execution_count="2">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">data <span class="op" style="color: #5E5E5E;
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.596122Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.115058Z&quot;}" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rdkit <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Chem</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rdkit.Chem <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Draw</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rdkit.Chem.Draw <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MolsToGridImage</span>
<span id="cb1-5"></span>
<span id="cb1-6">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
Expand All @@ -39,20 +60,20 @@ background-color: null;
font-style: inherit;">'Acetylsalicylic Acid'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Ibuprofen'</span>],</span>
<span id="cb1-2"> <span class="st" style="color: #20794D;
<span id="cb1-7"> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'smiles'</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'O=C(C)Oc1ccccc1C(=O)O'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CC(C)Cc1ccc(cc1)[C@@H](C)C(=O)O'</span>]}</span>
<span id="cb1-3">df <span class="op" style="color: #5E5E5E;
<span id="cb1-8">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data)</span>
<span id="cb1-4">df</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="2">
<span id="cb1-9">df</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<div>
Expand Down Expand Up @@ -86,7 +107,7 @@ font-style: inherit;">=</span>data)</span>
<h2 class="anchored" data-anchor-id="drawing-the-molecule">Drawing the Molecule</h2>
<p>Because I loaded up my dataset as a DataFrame, I will need to “slice” the table into a format for drawing. In this case, the important information is the smiles string. My brain works in Tables, but if you prefer, you can write out the smiles string directly for drawing the molecule, i.e.&nbsp;aspirin = ‘O=C(C)Oc1ccccc1C(=O)O’</p>
<p>Next, the smiles string will need to be converted an rdkit molecule object. This is done using <a href="https://www.rdkit.org/docs/GettingStartedInPython.html#reading-single-molecules">MolFromSmiles()</a> function.</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.246999Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.237599Z&quot;}" data-execution_count="3">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.630071Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.611834Z&quot;}" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">aspirin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[<span class="dv" style="color: #AD0000;
Expand All @@ -101,7 +122,7 @@ font-style: inherit;">=</span> Chem.MolFromSmiles(aspirin)</span>
background-color: null;
font-style: inherit;">=</span> Draw.MolToImage(mol)</span>
<span id="cb2-4">img</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<div class="cell-output cell-output-display" data-execution_count="2">
<p><img src="https://tlint101.github.io/practice-in-code/posts/10-Draw-Molecules_files/figure-html/cell-3-output-1.png" class="img-fluid"></p>
</div>
</div>
Expand All @@ -112,7 +133,7 @@ font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(image))</span></code></pre></div>
<p>Another cool feature is that you can add a legend to the individual images. In this case we can test it out for aspirin.</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.265162Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.250650Z&quot;}" data-execution_count="4">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.665627Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.623309Z&quot;}" data-execution_count="3">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">img <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Draw.MolToImage(mol, legend<span class="op" style="color: #5E5E5E;
Expand All @@ -121,7 +142,7 @@ font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'aspirin'</span>)</span>
<span id="cb4-2">img</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<div class="cell-output cell-output-display" data-execution_count="3">
<p><img src="https://tlint101.github.io/practice-in-code/posts/10-Draw-Molecules_files/figure-html/cell-4-output-1.png" class="img-fluid"></p>
</div>
</div>
Expand All @@ -132,7 +153,7 @@ font-style: inherit;">'aspirin'</span>)</span>
<h2 class="anchored" data-anchor-id="drawing-molecules-in-a-grid">Drawing Molecules in a Grid</h2>
<p>Those examples above are for single compounds. It is a great tool and, when we grasp more complex script, we can quickly draw multiple molecules by looping through a list of smiles strings. That is great, but depending on our aims, individual drawings might not cut it. Enter a neat tool by RDKit - drawing molecules in a grid!</p>
<p>It is as simple as drawing the individual molecules. All that is needed is a list of smiles strings. Like the previous example, more extra information can be added to the image. Here I kept it simple and passed a list of molecule names.</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.289599Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.262418Z&quot;}" data-execution_count="5">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.666125Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.640479Z&quot;}" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">mols <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [Chem.MolFromSmiles(mol) <span class="cf" style="color: #003B4F;
Expand All @@ -149,7 +170,7 @@ background-color: null;
font-style: inherit;">'aspirin'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Ibuprofen'</span>])</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<div class="cell-output cell-output-display" data-execution_count="4">
<p><img src="https://tlint101.github.io/practice-in-code/posts/10-Draw-Molecules_files/figure-html/cell-5-output-1.png" class="img-fluid"></p>
</div>
</div>
Expand Down
29 changes: 17 additions & 12 deletions docs/posts/10-Draw-Molecules.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,17 @@ <h2 class="anchored" data-anchor-id="starting-small">Starting Small</h2>
<section id="set-up-data" class="level2">
<h2 class="anchored" data-anchor-id="set-up-data">Set Up Data</h2>
<p>In this quick tutorial, we will draw 2 molecules. We’ll start small and get more complicated. I want to focus on drawing molecular structures for two drugs many of us may know - Acetylsalicylic Acid and Ibuprofen. Or for anyone well verse with the American market, aspirin and advil, respectively. The data is set up as follows:</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.232667Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.228057Z&quot;}" data-execution_count="2">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>data <span class="op">=</span> {<span class="st">'name'</span>: [<span class="st">'Acetylsalicylic Acid'</span>, <span class="st">'Ibuprofen'</span>],</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> <span class="st">'smiles'</span>: [<span class="st">'O=C(C)Oc1ccccc1C(=O)O'</span>, <span class="st">'CC(C)Cc1ccc(cc1)[C@@H](C)C(=O)O'</span>]}</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.DataFrame(data<span class="op">=</span>data)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>df</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="2">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.596122Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.115058Z&quot;}" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> rdkit <span class="im">import</span> Chem</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> rdkit.Chem <span class="im">import</span> Draw</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> rdkit.Chem.Draw <span class="im">import</span> MolsToGridImage</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>data <span class="op">=</span> {<span class="st">'name'</span>: [<span class="st">'Acetylsalicylic Acid'</span>, <span class="st">'Ibuprofen'</span>],</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="st">'smiles'</span>: [<span class="st">'O=C(C)Oc1ccccc1C(=O)O'</span>, <span class="st">'CC(C)Cc1ccc(cc1)[C@@H](C)C(=O)O'</span>]}</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.DataFrame(data<span class="op">=</span>data)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>df</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<div>


Expand Down Expand Up @@ -255,22 +260,22 @@ <h2 class="anchored" data-anchor-id="set-up-data">Set Up Data</h2>
<h2 class="anchored" data-anchor-id="drawing-the-molecule">Drawing the Molecule</h2>
<p>Because I loaded up my dataset as a DataFrame, I will need to “slice” the table into a format for drawing. In this case, the important information is the smiles string. My brain works in Tables, but if you prefer, you can write out the smiles string directly for drawing the molecule, i.e.&nbsp;aspirin = ‘O=C(C)Oc1ccccc1C(=O)O’</p>
<p>Next, the smiles string will need to be converted an rdkit molecule object. This is done using <a href="https://www.rdkit.org/docs/GettingStartedInPython.html#reading-single-molecules">MolFromSmiles()</a> function.</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.246999Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.237599Z&quot;}" data-execution_count="3">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.630071Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.611834Z&quot;}" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>aspirin <span class="op">=</span> df.iloc[<span class="dv">0</span>, <span class="dv">1</span>]</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>mol <span class="op">=</span> Chem.MolFromSmiles(aspirin)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>img <span class="op">=</span> Draw.MolToImage(mol)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>img</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<div class="cell-output cell-output-display" data-execution_count="2">
<p><img src="10-Draw-Molecules_files/figure-html/cell-3-output-1.png" class="img-fluid"></p>
</div>
</div>
<p><strong>NOTE</strong> If you print out the type of the img object, you will see that it is a PIL object. As such, it can be manipulated using the <a href="https://pillow.readthedocs.io/en/stable/reference/Image.html">PIL Image module</a>. I do not have much experience with that as I am normally quite happy with the default outcomes.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(image))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Another cool feature is that you can add a legend to the individual images. In this case we can test it out for aspirin.</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.265162Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.250650Z&quot;}" data-execution_count="4">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.665627Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.623309Z&quot;}" data-execution_count="3">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>img <span class="op">=</span> Draw.MolToImage(mol, legend<span class="op">=</span><span class="st">'aspirin'</span>)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>img</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<div class="cell-output cell-output-display" data-execution_count="3">
<p><img src="10-Draw-Molecules_files/figure-html/cell-4-output-1.png" class="img-fluid"></p>
</div>
</div>
Expand All @@ -281,10 +286,10 @@ <h2 class="anchored" data-anchor-id="drawing-the-molecule">Drawing the Molecule<
<h2 class="anchored" data-anchor-id="drawing-molecules-in-a-grid">Drawing Molecules in a Grid</h2>
<p>Those examples above are for single compounds. It is a great tool and, when we grasp more complex script, we can quickly draw multiple molecules by looping through a list of smiles strings. That is great, but depending on our aims, individual drawings might not cut it. Enter a neat tool by RDKit - drawing molecules in a grid!</p>
<p>It is as simple as drawing the individual molecules. All that is needed is a list of smiles strings. Like the previous example, more extra information can be added to the image. Here I kept it simple and passed a list of molecule names.</p>
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-23T15:00:18.289599Z&quot;,&quot;start_time&quot;:&quot;2024-02-23T15:00:18.262418Z&quot;}" data-execution_count="5">
<div class="cell" data-executetime="{&quot;end_time&quot;:&quot;2024-02-27T10:19:15.666125Z&quot;,&quot;start_time&quot;:&quot;2024-02-27T10:19:15.640479Z&quot;}" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>mols <span class="op">=</span> [Chem.MolFromSmiles(mol) <span class="cf">for</span> mol <span class="kw">in</span> df[<span class="st">'smiles'</span>]]</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>MolsToGridImage(mols, legends<span class="op">=</span>[<span class="st">'aspirin'</span>, <span class="st">'Ibuprofen'</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<div class="cell-output cell-output-display" data-execution_count="4">
<p><img src="10-Draw-Molecules_files/figure-html/cell-5-output-1.png" class="img-fluid"></p>
</div>
</div>
Expand Down
Loading

0 comments on commit dffeadb

Please sign in to comment.