<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Jan Stanstrup</title>
<link>https://stanstrup-personal.gitlab.io/blog/</link>
<atom:link href="https://stanstrup-personal.gitlab.io/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Jan Stanstrup — metabolomics bioinformatician and Assistant Professor at the University of Copenhagen. Specializes in LC-MS data preprocessing, untargeted metabolomics, XCMS, compound annotation, retention time prediction, and reproducible R pipelines.</description>
<generator>quarto-1.8.25</generator>
<lastBuildDate>Fri, 27 Nov 2015 00:00:00 GMT</lastBuildDate>
<item>
  <title>Heatmaps the right way</title>
  <dc:creator>Jan Stanstrup</dc:creator>
  <link>https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/</link>
  <description><![CDATA[ 





<p>In this post I will go through some of the pitfalls of heatmap generation in R. I found that at least for me the heatmap functions do not always do what you think they do. When working with my own data I got convinced that something was wrong and investigated. I found that scaling was not done the way it would seem from the documentation of several functions. I eventually ended in on <a href="http://stackoverflow.com/questions/17924828/differences-in-heatmap-clustering-defaults-in-r-heatplot-versus-heatmap-2">stackoverflow</a> where Thomas W. Leja had explained what was going on.<br></p>
<p>Asking several colleagues no-one was aware of the issues I had found. I have therefore written up my findings here and in this post I will show in detail what to me doesn’t work as I expected and how do gain control of what precisely is done when you make a heatmap. <br> If you are surprised as well by the below please pass this on to everybody you know that might not be making heatmaps the way they think they are.</p>
<p><br></p>
<section id="heatplot-from-made4" class="level1">
<h1>heatplot from made4</h1>
<p>Lets start with heatplot from the made4 package just using the default settings.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(made4)</span>
<span id="cb1-2"></span>
<span id="cb1-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatplot</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 1</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 1. </strong>Using heatplot defaults.</em></p>
<p>Yikes! This is not good for this data. This is because <code>heatplot</code> by default does row scaling only. Fair enough. Lets use column scaling instead. <br><br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatplot</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 2</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 2. </strong>Heatplot with column scaling for the heat data only.</em></p>
<p>Much better! But wait! Did you see it? The colors in the plot changed. But the dendrogram did not! It turns out that the <code>scale</code> argument only refers to the scaling of the heat data, NOT what happens to the scaling before calculation of the dendrograms.<br> So how is the data for the dendrogram actually scaled now??? It turns out that if you look at the code for the function <code>dualScale</code> it does NOT do what it says. <code>dualScale</code> applies row scaling before dendrogram calculations… And that is it. So now we have row scaling for the dendrograms and have <em>added</em> additional column scaling for the heat data. Hmmmm… not what we want either.<br></p>
<p>So lets turn off the row scaling and we do the column scaling before we give the data to <code>heatplot</code>.<br><br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars))</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatplot</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dualScale=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 3</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 3. </strong>Column scaled data and then heatplot. No row scaling in heatplot.</em></p>
<p>Good then. That seems better, all is well. Well… There is a thing more that probably did not work as you had thought but is not very visible in this case. <br><code>heatplot</code> has the argument zlim which by default reassigns all values smaller than -3 and all values above 3 to -3 and 3 respectively. This makes a lot of sense when you use unit variance scaling. If you don’t do that most of your values will be in the middle of the distribution which gives you very pale colors since only the very extreme values will use the full range of the color gradient. <br> The problem here is that <code>zlim</code> is actually turned off when <code>dualScale=FALSE</code> which was probably not obvious to you either. In this case it doesn’t do much damage but if the destribution of you data is tailed this won’t look nice. <br></p>
<p>Pheww… Now we understand <code>heatplot</code>.</p>
<p><br><br></p>
</section>
<section id="heatmap-from-stats-and-heatmap.2-from-gplots" class="level1">
<h1>heatmap from stats and heatmap.2 from gplots</h1>
<p>Now lets see if we can do the same plot with <code>heatmap</code> from <code>stats</code>. <code>heatmap</code> uses different defaults for distance calculation and clustering so lets change <code>heatmap</code> to use the same calculations and also make the color the same.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RColorBrewer)</span>
<span id="cb4-2"></span>
<span id="cb4-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars))</span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap</span>(x,</span>
<span id="cb4-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb4-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb4-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun   =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb4-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>)</span>
<span id="cb4-9">        )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 4</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 4. </strong>Column scaled data and then heatmap. No other scaling.</em></p>
<p>Oh no! Both dendrogram and the heat data is different. It turns out there are two reasons for this. For the dendrogram it turns out that <code>heatplot</code> uses the dendrogram directly whereas <code>heatmap</code> reorders the dendrogram based on row means. As far as I could find you cannot change this without calculating the dendrograms seperately.<br></p>
<p>For the colors it turns out that heatplot uses so-called symmetric breaks which means that 0 is put in the middle of the distribution of the data.<br> <strong>Btw.: And this is very important, <code>heatmap</code> and <code>heatmap.2</code> (we will get to that one) has the same “feature” as <code>heatplot</code>: <code>scale</code> refers ONLY to the heat data, NOT the dendrogram calculation.</strong></p>
<p>Lets jump right to <code>heatmap.2</code> which is basically a version of <code>heatmap</code> with more options.<br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars))</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(x,</span>
<span id="cb5-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb5-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb5-5">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb5-6">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun   =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb5-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>),</span>
<span id="cb5-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb5-9">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 5</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 5. </strong>Column scaled data and then heatmap.2. No other scaling.</em></p>
<p>This looks similar to <code>heatplot</code> but <code>heatmap.2</code> reorders as above while <code>heatplot</code> does not. <br><br></p>
</section>
<section id="gaining-full-controls" class="level1">
<h1>Gaining full controls</h1>
<p>If you are not busy hyperventilating at this point I will offer here a solution that will give the control we seek over when exactly what happens to the data.<br></p>
<p>I have created (well actually I have stolen and expanded the <a href="http://stackoverflow.com/questions/17924828/differences-in-heatmap-clustering-defaults-in-r-heatplot-versus-heatmap-2">code provided by Thomas W. Leja</a>) a function, <code>heat.clust</code>, that will generate scaled data and dendrograms that can be passed to <code>heatmap.2</code>so that it can be controlled what happens at which point. You can find the function in my package <a href="https://github.com/stanstrup/massageR">massageR</a>.<br> Lets jump into an example.</p>
<p><br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(massageR)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gplots)</span>
<span id="cb6-3"></span>
<span id="cb6-4">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb6-5">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,</span>
<span id="cb6-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,</span>
<span id="cb6-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb6-8">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim_select =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dend"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"outdata"</span>),</span>
<span id="cb6-9">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>),</span>
<span id="cb6-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb6-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"complete"</span>),</span>
<span id="cb6-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb6-13"></span>
<span id="cb6-14"></span>
<span id="cb6-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb6-16">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb6-17">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb6-18">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb6-19">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb6-20">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb6-21">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb6-22">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb6-23">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 6</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 6. </strong>Preparing data using heat.clust and then heatplot.2.</em></p>
<p><br> The parameters for <code>heat.clust</code> do the following:</p>
<ul>
<li>scaledim: Selects to scale “column” or “row” or both (<code>scaledim = c("row","column")</code>).</li>
<li>zlim: Selects the limits of the data after scaling. Note that if you use anything else than unit variance scaling you should consider these limits. -3 to 3 probably doesn’t make sense in any other case. You can disable the limits using <code>c(-Inf,Inf)</code>.</li>
<li>zlim_select: Selects when to apply <code>zlim</code>. For the dendrogram and/or for the outputted (scaled) data that is passed to <code>heatmap.2</code>.</li>
<li>reorder: Selects if to reorder the dendrogram by “column” and/or “row” means.</li>
<li>distfun: The function for distance calculation.</li>
<li>hclustfun: The function for clustering.</li>
<li>scalefun: the function to use for scaling.</li>
</ul>
<p><br></p>
<p>Next we simply pass on the data and row and column dendrograms to <code>heatmap.2</code>. We can then do additional scaling and set symmetric breaks if we want.<br> I hope you enjoyed the read and now feel you know exactly what the heatmap funcions do. <br><br></p>
</section>
<section id="exemplifying-why-it-matters-when-you-do-scaling" class="level1">
<h1>Exemplifying why it matters when you do scaling</h1>
<p>If you are not convinced that the above is very important I will explain here why scaling is really really important. <br> Lets take the same data as before and do no scaling at all.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb7-2"></span>
<span id="cb7-3">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,</span>
<span id="cb7-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb7-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb7-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim_select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb7-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>),</span>
<span id="cb7-8">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb7-9">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"complete"</span>),</span>
<span id="cb7-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb7-11"></span>
<span id="cb7-12"></span>
<span id="cb7-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb7-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb7-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb7-16">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb7-17">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb7-18">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb7-19">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb7-20">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb7-21">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 7</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 7. </strong>Preparing data using heat.clust with no scaling and then heatplot.2.</em></p>
<p>We can clearly see that this is no good. Now if we do the same just with scaling for the heat data we get. <br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb8-2"></span>
<span id="cb8-3">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,</span>
<span id="cb8-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb8-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb8-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim_select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb8-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>),</span>
<span id="cb8-8">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb8-9">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"complete"</span>),</span>
<span id="cb8-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb8-11"></span>
<span id="cb8-12"></span>
<span id="cb8-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb8-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb8-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb8-16">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb8-17">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,</span>
<span id="cb8-18">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb8-19">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb8-20">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb8-21">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 8</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 8. </strong>Preparing data using heat.clust with no scaling and then heatplot.2 with column scaling of heat data.</em></p>
<p>Doesn’t look so bad right? Wrong! Compared to the last plot we did in the previous section you might think the clustering doesn’t look that different. But lets look at what actually happened to the correlation matrix for the rows when the columns are not scaled. <br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(corrplot)</span>
<span id="cb9-2"></span>
<span id="cb9-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corrplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 9</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 9. </strong>Correlation plot of rows in the mtcars data.</em></p>
<p>Dang! Everything is super correlated! And if you plot two rows against each other we see why.</p>
<p><br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,],x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,])</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 10</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 10. </strong>Variable 7 vs.&nbsp;variable 6 of the mtcars data. Data not scaled.</em></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,],x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9814781</code></pre>
</div>
</div>
<p><br> Because the variables are on so different scales fitting a line through seems pretty linear and the correlation is high… <br> But if we do the scaling we see the truth.</p>
<p><br></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars))</span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,],x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,])</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 11</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 11. </strong>Variable 7 vs.&nbsp;variable 6 of the mtcars data. Data column scaled first.</em></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,],x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.1641199</code></pre>
</div>
</div>
<p><br> Not correlated at all it turns out.</p>
<p><br><br></p>
</section>
<section id="equivalent-plots-using-heat.clust-heatmap.2" class="level1">
<h1>Equivalent plots using heat.clust + heatmap.2</h1>
<p>In this section I replicate all the plots above so you can see exactly what each command do when using full control of what happens. <br><br></p>
<p>Fig. 1 equivalent (click to show/hide)</p>
<div id="fig1eq" style="display: none;" data-markdown="1">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(massageR)</span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gplots)</span>
<span id="cb16-3"></span>
<span id="cb16-4">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb16-5">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb16-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb16-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>),</span>
<span id="cb16-8">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb16-9"></span>
<span id="cb16-10"></span>
<span id="cb16-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb16-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb16-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb16-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb16-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb16-16">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb16-17">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb16-18">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb16-19">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 1 equivalent</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 1 equivalent. </strong>Using heat.clust and heatmap.2.</em></p>
</div>
<p>Fig. 2 equivalent (click to show/hide)</p>
<div id="fig2eq" style="display: none;" data-markdown="1">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb17-2">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb17-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb17-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>),</span>
<span id="cb17-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb17-6"></span>
<span id="cb17-7"></span>
<span id="cb17-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb17-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb17-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb17-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb17-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,</span>
<span id="cb17-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb17-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb17-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb17-16">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 2 equivalent</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 2 equivalent. </strong>Using heat.clust and heatmap.2.</em></p>
</div>
<p>Fig. 3 equivalent (click to show/hide)</p>
<div id="fig3eq" style="display: none;" data-markdown="1">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb18-2">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb18-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb18-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>),</span>
<span id="cb18-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb18-6"></span>
<span id="cb18-7"></span>
<span id="cb18-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb18-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb18-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb18-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb18-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb18-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb18-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb18-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb18-16">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 3 equivalent</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 3 equivalent. </strong>Using heat.clust and heatmap.2.</em></p>
</div>
<p>Fig. 4 equivalent (click to show/hide)</p>
<div id="fig4eq" style="display: none;" data-markdown="1">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb19-2">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>),</span>
<span id="cb19-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb19-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>),</span>
<span id="cb19-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb19-6"></span>
<span id="cb19-7"></span>
<span id="cb19-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb19-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb19-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb19-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb19-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb19-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb19-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb19-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb19-16">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 4 equivalent</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 4 equivalent. </strong>Using heat.clust and heatmap.2.</em></p>
</div>
<p>Fig. 5 equivalent (click to show/hide)</p>
<div id="fig5eq" style="display: none;" data-markdown="1">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(mtcars)</span>
<span id="cb20-2">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heat.clust</span>(x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scaledim=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zlim=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reorder=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>),</span>
<span id="cb20-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distfun  =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.dist</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(x))), </span>
<span id="cb20-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hclustfun=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ave"</span>),</span>
<span id="cb20-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scalefun =</span> scale)</span>
<span id="cb20-6"></span>
<span id="cb20-7"></span>
<span id="cb20-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">heatmap.2</span>(z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data,</span>
<span id="cb20-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rowv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Rowv, </span>
<span id="cb20-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Colv=</span>z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Colv,</span>
<span id="cb20-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb20-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb20-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbreaks =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb20-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>)),</span>
<span id="cb20-15">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margins=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb20-16">          )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Fig. 5 equivalent</figcaption>
</figure>
</div>
</div>
</div>
<p><em><strong>Figure 5 equivalent. </strong>Using heat.clust and heatmap.2.</em></p>
</div>


</section>

 ]]></description>
  <category>R</category>
  <category>data visualization</category>
  <category>heatmaps</category>
  <guid>https://stanstrup-personal.gitlab.io/blog/2015-11-25-heatmaps/</guid>
  <pubDate>Fri, 27 Nov 2015 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Annotating LC-MS data with PredRet in R</title>
  <dc:creator>Jan Stanstrup</dc:creator>
  <link>https://stanstrup-personal.gitlab.io/blog/2015-11-22-annotate_with_PredRet/</link>
  <description><![CDATA[ 





<p>In this tutorial I will show you how you can use the PredRet database to annotate your LC-MS metabolomics data directly in R. The annotation function I will be using is general-purpose, though, so you can use it to annotate any data where you have a list of compounds with known <em>m/z</em>’s and retention times (RTs).</p>
<section id="what-is-predret" class="level2">
<h2 class="anchored" data-anchor-id="what-is-predret">What is PredRet?</h2>
<p>In short PredRet is a user-driven database of compound retention times. The purpose of PredRet is to be able to predict the RT of a compound in one (your!) chromatographic system if it has been experimentally determined in another chromatographic system by someone, somewhere in the world. You can download the paper <a href="../../static/material/papers/Stanstrup, Neumann, Vrhovsek - 2015 - PredRet Prediction of Retention Time by Direct Mapping between Multiple Chromatographic Systems.pdf">here</a> and visit the project’s home page at <a href="http://PredRet.org">PredRet.org</a>.</p>
</section>
<section id="pulling-data-from-predret" class="level2">
<h2 class="anchored" data-anchor-id="pulling-data-from-predret">Pulling data from PredRet</h2>
<p>So lets get going.</p>
<p>First we will download the PredRet database with the <a href="https://github.com/stanstrup/PredRet">PredRetR package</a>. We will get both the experimental and the predicted values for the chromatographic system “LIFE_old”.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(PredRetR)</span>
<span id="cb1-2"></span>
<span id="cb1-3">predret_db <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">PredRet_get_db</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exp_pred =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exp"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pred"</span>),</span>
<span id="cb1-4">                             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">systems =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LIFE_old"</span></span>
<span id="cb1-5">                            )</span></code></pre></div></div>
</div>
<p>Then lets take a look at the structure of the <code>data.frame</code> we have retrieved. If the <code>recorded_rt</code> column has data we have an experimentally determined RT, if the <code>predicted_rt</code> column has data we have a RT predicted with the PredRet systems. The <code>ci_lower</code> and <code>ci_upper</code> columns show the prediction interval for the predictions.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(knitr)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(predret_db)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3367   12</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(predret_db) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">table.attr =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'class="nicetable"'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="nicetable caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">system</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">name</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">pubchem</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">inchi</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">date added</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">username</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">predicted</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">suspect</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">recorded_rt</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">predicted_rt</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">ci_lower</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">ci_upper</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">LIFE_old</td>
<td style="text-align: left;">(DL)-p-hydroxyphenyllactic acid</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">InChI=1S/C9H10O4/c10-7-3-1-6(2-4-7)5-8(11)9(12)13/h1-4,8,10-11H,5H2,(H,12,13)</td>
<td style="text-align: left;">2019-09-06 17:29:46</td>
<td style="text-align: left;">jan</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">1.544350</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: left;">LIFE_old</td>
<td style="text-align: left;">(R)-2-hydroxybutyric acid</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">InChI=1S/C4H8O3/c1-2-3(5)4(6)7/h3,5H,2H2,1H3,(H,6,7)</td>
<td style="text-align: left;">2019-09-06 17:29:46</td>
<td style="text-align: left;">jan</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">1.399400</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="odd">
<td style="text-align: left;">LIFE_old</td>
<td style="text-align: left;">1-O-1'-(Z)-octadecenyl-2-hydroxy-sn-glycero-3-phosphocholine (LysoPC(P-18:0))</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">InChI=1S/C26H54NO6P/c1-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-22-31-24-26(28)25-33-34(29,30)32-23-21-27(2,3)4/h20,22,26,28H,5-19,21,23-25H2,1-4H3</td>
<td style="text-align: left;">2019-09-06 17:29:46</td>
<td style="text-align: left;">jan</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">4.947717</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: left;">LIFE_old</td>
<td style="text-align: left;">2-Hydroxy-2-methylbutyric acid</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">InChI=1S/C5H10O3/c1-3-5(2,8)4(6)7/h8H,3H2,1-2H3,(H,6,7)</td>
<td style="text-align: left;">2019-09-20 02:26:09</td>
<td style="text-align: left;">jan</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">1.625783</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="odd">
<td style="text-align: left;">LIFE_old</td>
<td style="text-align: left;">2-Hydroxy-3-methylbutyric acid</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">InChI=1S/C5H10O3/c1-3(2)4(6)5(7)8/h3-4,6H,1-2H3,(H,7,8)</td>
<td style="text-align: left;">2019-09-20 02:26:09</td>
<td style="text-align: left;">jan</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">1.685467</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: left;">LIFE_old</td>
<td style="text-align: left;">2-methylacetoacetic acid</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">InChI=1S/C5H8O3/c1-3(4(2)6)5(7)8/h3H,1-2H3,(H,7,8)</td>
<td style="text-align: left;">2019-09-06 17:29:46</td>
<td style="text-align: left;">jan</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">1.612850</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We have the RT directly in the database above but we do not have the <em>m/z</em>. Since we have the InChI the easiest way to get the <em>m/z</em> is to extract the molecular formula and then use the <a href="https://bioconductor.org/packages/release/bioc/html/Rdisop.html">Rdisop</a> package to get the mass.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(Rdisop)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb5-3"></span>
<span id="cb5-4">formulas   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_split_fixed</span>(predret_db[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inchi"</span>],<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb5-5">masses     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(formulas, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMass</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMolecule</span>(x)))</span>
<span id="cb5-6"></span>
<span id="cb5-7">predret_db <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind.data.frame</span>(predret_db, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mz_pos =</span> masses <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0073</span>)</span></code></pre></div></div>
</div>
<p>We can then split the database in two. One for the experimental RTs and one for the predicted. Here I have used some pipe/dplyr style code and even a forward assign. If you are not familiar with this type of R code I urge you to look into it. It really makes code much more readable.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">predret_db <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(recorded_rt)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> predret_db_exp</span>
<span id="cb6-2">predret_db <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(recorded_rt))  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> predret_db_pred</span></code></pre></div></div>
</div>
</section>
<section id="annotating-a-dataset" class="level2">
<h2 class="anchored" data-anchor-id="annotating-a-dataset">Annotating a dataset</h2>
<p>We now have the database ready for annotation.</p>
<p>So we can load a dataset/peaklist. This peaklist was previously created with XCMS and fragments/adducts annotated with CAMERA. But again any peaklist will do.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span>
<span id="cb7-2"></span>
<span id="cb7-3">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../static/material/data/peaklist_pos.xlsx"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(.), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-5">        as.data.frame</span></code></pre></div></div>
</div>
<p>Lets take a look at the interesting columns we have in the dataset.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">info_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mz"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rt"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"isotopes"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adduct"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pcgroup"</span>)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(data)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4232  470</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data[,info_cols])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>         mz       rt   isotopes              adduct pcgroup
1  62.06013 273.8827            [M+H-C5H8O]+ 145.11       1
2  95.08600 271.5567                                      1
3  98.09642 268.1695                                      1
4 104.10687 271.2400   [12][M]+ [M+H-COCH2]+ 145.11       1
5 105.11030 271.2415 [12][M+1]+                           1
6 114.09119 268.9771            [M+H-CH3OH]+ 145.11       1</code></pre>
</div>
</div>
<p>Now we can use <code>db.comp.assign</code> from my <a href="https://github.com/stanstrup/chemhelper">chemhelper</a> package to annotate the dataset.</p>
<p>We would probably want one column in our peaklist for the RTs we have determined experimentally and one for the predicted RTs. So we do the annotation twice.</p>
<p>The first two arguments to the function are the <em>m/z</em> and RT of the dataset. The next three are the name, <em>m/z</em> and RT of the database of known compounds. Lastly we give the tolerance for the <em>m/z</em> and RT for a database match.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(chemhelper)</span>
<span id="cb12-2"></span>
<span id="cb12-3">anno_exp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db.comp.assign</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mz           =</span> data[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mz"</span>],</span>
<span id="cb12-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rt           =</span> data[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rt"</span>],</span>
<span id="cb12-5">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comp_name_db =</span> predret_db_exp[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>],</span>
<span id="cb12-6">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mz_db        =</span> predret_db_exp[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mz_pos"</span>],</span>
<span id="cb12-7">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rt_db        =</span> predret_db_exp[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"recorded_rt"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># *60 since XCMS works in seconds but PredRet is in minutes.</span></span>
<span id="cb12-8">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mzabs        =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>,</span>
<span id="cb12-9">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ppm          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,</span>
<span id="cb12-10">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ret_tol      =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb12-11">                           )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                                        [,1]
Unique hits                               89
Non-unique hits                           10
Compounds not found                      201
Markers had multiple compounds assigned   53</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">anno_pred <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">db.comp.assign</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mz          =</span> data[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mz"</span>],</span>
<span id="cb14-2">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rt           =</span> data[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rt"</span>],</span>
<span id="cb14-3">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comp_name_db =</span> predret_db_pred[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>],</span>
<span id="cb14-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mz_db        =</span> predret_db_pred[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mz_pos"</span>],</span>
<span id="cb14-5">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rt_db        =</span> predret_db_pred[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"predicted_rt"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>,</span>
<span id="cb14-6">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mzabs        =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>,</span>
<span id="cb14-7">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ppm          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,</span>
<span id="cb14-8">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ret_tol      =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb14-9">                           )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                                        [,1]
Unique hits                              209
Non-unique hits                           15
Compounds not found                     2843
Markers had multiple compounds assigned   72</code></pre>
</div>
</div>
<p>Now lets put the annotations together with our dataset.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">data_annotated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind.data.frame</span>(data,anno_exp,anno_pred)</span></code></pre></div></div>
</div>
</section>
<section id="the-result" class="level2">
<h2 class="anchored" data-anchor-id="the-result">The result</h2>
<p>Then lets take a look at one of the feature groups (from CAMERA) where we got an annotation. In this example we have a feature that was annotated as tryptophan. The “OR” is because tryptophan was in the database several times. If multiple compounds would fit the <em>m/z</em> and RT they would be written with “OR” between them.</p>
<p>There is also a fragment annotated as adipic acid and 2-methylglutaric acid using a hit from the predicted RTs. In this case we have almost perfect CAMERA annotation suggesting the pseudo-molecular ion is the <em>m/z</em> = 205.0979 feature and the compound is very likely tryptophan.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">data_annotated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(pcgroup<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">one_of</span>(info_cols,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anno_exp"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anno_pred"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">table.attr =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'class="nicetable"'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="nicetable caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">mz</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">rt</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">isotopes</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">adduct</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">pcgroup</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">anno_exp</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">anno_pred</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">118.0657</td>
<td style="text-align: right;">91.31296</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[M+H-NH3-CO-COCH2]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">130.0655</td>
<td style="text-align: right;">91.40512</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">QUINOLINE</td>
</tr>
<tr class="odd">
<td style="text-align: right;">132.0808</td>
<td style="text-align: right;">91.27887</td>
<td style="text-align: left;">[44][M]+</td>
<td style="text-align: left;">[M+H-NH3-CO-CO]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">133.0845</td>
<td style="text-align: right;">91.19346</td>
<td style="text-align: left;">[44][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">142.0647</td>
<td style="text-align: right;">91.20588</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[M+H-NH3-HCOOH]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">144.0416</td>
<td style="text-align: right;">91.25665</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">144.0814</td>
<td style="text-align: right;">91.34230</td>
<td style="text-align: left;">[55][M]+</td>
<td style="text-align: left;">[M+H-NH3-CO2]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">OSI1265 OR OSI1138 (non-unique hit)</td>
</tr>
<tr class="even">
<td style="text-align: right;">145.0846</td>
<td style="text-align: right;">91.28341</td>
<td style="text-align: left;">[55][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">146.0599</td>
<td style="text-align: right;">91.33676</td>
<td style="text-align: left;">[58][M]+</td>
<td style="text-align: left;">[M+H-NH3-COCH2]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">4-Hydroxyquinoline</td>
</tr>
<tr class="even">
<td style="text-align: right;">147.0647</td>
<td style="text-align: right;">91.36894</td>
<td style="text-align: left;">[58][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;">Adipic acid</td>
<td style="text-align: left;">ADIPATE OR 2-METHYLGLUTARATE OR METHYGLUTARATE</td>
</tr>
<tr class="odd">
<td style="text-align: right;">159.0922</td>
<td style="text-align: right;">91.28882</td>
<td style="text-align: left;">[71][M]+</td>
<td style="text-align: left;">[M+H-HCOOH]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">OSI6</td>
</tr>
<tr class="even">
<td style="text-align: right;">160.0851</td>
<td style="text-align: right;">91.35138</td>
<td style="text-align: left;">[71][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">170.0608</td>
<td style="text-align: right;">91.34747</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[M+H-NH3-H2O]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">188.0711</td>
<td style="text-align: right;">91.34805</td>
<td style="text-align: left;">[93][M]+</td>
<td style="text-align: left;">[M+H-NH3]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">189.0743</td>
<td style="text-align: right;">91.28935</td>
<td style="text-align: left;">[93][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">205.0979</td>
<td style="text-align: right;">91.32364</td>
<td style="text-align: left;">[102][M]+</td>
<td style="text-align: left;">[M+H]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;">tryptophan OR Tryptophan OR TRYPTOPHAN</td>
<td style="text-align: left;">L-tryptopan (15N2, 98%)</td>
</tr>
<tr class="odd">
<td style="text-align: right;">206.1034</td>
<td style="text-align: right;">91.31068</td>
<td style="text-align: left;">[102][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">245.1301</td>
<td style="text-align: right;">91.91434</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[M+H+(CH3)2CO-H2O]+ (acetone cond.) 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">276.1823</td>
<td style="text-align: right;">91.41425</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[3-Carboxy-2-(3-hydroxyhexanoyloxy)propyl]-trimethylazanium</td>
</tr>
<tr class="even">
<td style="text-align: right;">409.1873</td>
<td style="text-align: right;">91.28252</td>
<td style="text-align: left;">[265][M]+</td>
<td style="text-align: left;">[2M+H]+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: right;">410.1906</td>
<td style="text-align: right;">91.26287</td>
<td style="text-align: left;">[265][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">447.1336</td>
<td style="text-align: right;">91.29100</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[2M+K]+ 204.09 [4M+2K]2+ 204.09</td>
<td style="text-align: right;">16</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Lets take a look at another feature. This time we have experimental RTs that say the feature is either 1,7-dimethylxanthine OR theobromine but a prediction from the PredRet database also suggest that the feature could be theophylline as well.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">data_annotated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(pcgroup<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">one_of</span>(info_cols,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anno_exp"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anno_pred"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">table.attr =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'class="nicetable"'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="nicetable caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">mz</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">rt</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">isotopes</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">adduct</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">pcgroup</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">anno_exp</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">anno_pred</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">124.0508</td>
<td style="text-align: right;">89.67717</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
<td style="text-align: right;">400</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">181.0725</td>
<td style="text-align: right;">89.56150</td>
<td style="text-align: left;">[86][M]+</td>
<td style="text-align: left;">[M+H-NH3-CO2-NH3-H2O]+ 276.119 [M+H-C4H6-COCH2]+ 276.119</td>
<td style="text-align: right;">400</td>
<td style="text-align: left;">1,7-dimethylxanthine OR theobromine</td>
<td style="text-align: left;">Theobromine OR Theophylline OR Paraxanthine OR OSI101</td>
</tr>
<tr class="odd">
<td style="text-align: right;">182.0761</td>
<td style="text-align: right;">89.59507</td>
<td style="text-align: left;">[86][M+1]+</td>
<td style="text-align: left;"></td>
<td style="text-align: right;">400</td>
<td style="text-align: left;"></td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: right;">315.0806</td>
<td style="text-align: right;">89.70856</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">[M+K]+ 276.119</td>
<td style="text-align: right;">400</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">HMDB0005033</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>That’s it!</p>


</section>

 ]]></description>
  <category>R</category>
  <category>metabolomics</category>
  <category>PredRet</category>
  <guid>https://stanstrup-personal.gitlab.io/blog/2015-11-22-annotate_with_PredRet/</guid>
  <pubDate>Sun, 22 Nov 2015 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
