<p><strong>Previously in this series:</strong></p>
<ul><li><a href="http://varianceexplained.org/r/tidy-genomics/">Cleaning and visualizing genomic data: a case study in tidy analysis</a></li>
<li><a href="http://varianceexplained.org/r/tidy-genomics-broom/">Modeling gene expression with broom: a case study in tidy analysis</a></li>
</ul><p>In previous posts, I’ve examined the benefits of the tidy data framework in cleaning, visualizing, and modeling in exploratory data analysis on a molecular biology experiment. We’re using <a href="http://www.molbiolcell.org/content/19/1/352.abstract">Brauer et al 2008</a> as our case study, which explores the effects of nutrient starvation on gene expression in yeast.</p>
<p>From the posts so far, one might get the impression that I think data must be tidy at every stage of an analysis. Not true! That would be an absurd and unnecessary constraint. Lots of mathematical operations are faster on matrices, such as singular value decomposition or linear regression. Jeff Leek <a href="http://simplystatistics.org/2016/02/17/non-tidy-data/">rightfully points this out</a> as an issue with my <a href="http://varianceexplained.org/r/tidy-genomics-broom/">previous modeling gene expression post</a>, where he remarks that the <a href="https://bioconductor.org/packages/release/bioc/html/limma.html">limma package</a> is both faster and takes more statistical considerations (pooling variances among genes) into account.</p>
<p>Isn’t it contradictory to do these kinds of operations in a tidy analysis? Not at all. My general recommendation is laid out as follows:</p>
<p></p>
<p>As long as you’re in that <strong>Models</strong> “cloud”, you can store your data in whatever way is computationally and practically easiest. However:</p>
<ul><li>Before you model, you should use tidy tools to clean, process and wrangle your data (as shown in previous posts)</li>
<li>After you’ve performed your modeling, you should turn the model into a tidy output for interpretation, visualization, and communication</li>
</ul><p>This requires a new and important tool in our series on tidy bioinformatics analysis: the <a href="http://bioconductor.org/packages/release/bioc/html/biobroom.html">biobroom package</a>, written and maintained by my former colleagues, particularly Andy Bass and John Storey. In this post I’ll show how to use the limma and biobroom packages in combination to continue a tidy analysis, and consider when and how to use non-tidy data in an analysis.</p>
<h3>Setup</h3>
<p>Here’s the code to catch up with our previous posts:</p>
<pre><code><span>library</span><span>(</span><span>readr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>url</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://varianceexplained.org/files/Brauer2008_DataSet1.tds&quot;</span><span>
</span><span>nutrient_names</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>G</span><span> </span><span>=</span><span> </span><span>&quot;Glucose&quot;</span><span>,</span><span> </span><span>L</span><span> </span><span>=</span><span> </span><span>&quot;Leucine&quot;</span><span>,</span><span> </span><span>P</span><span> </span><span>=</span><span> </span><span>&quot;Phosphate&quot;</span><span>,</span><span>
</span><span>S</span><span> </span><span>=</span><span> </span><span>&quot;Sulfate&quot;</span><span>,</span><span> </span><span>N</span><span> </span><span>=</span><span> </span><span>&quot;Ammonia&quot;</span><span>,</span><span> </span><span>U</span><span> </span><span>=</span><span> </span><span>&quot;Uracil&quot;</span><span>)</span><span>
</span><span>cleaned_data</span><span> </span><span>&lt;-</span><span> </span><span>read_delim</span><span>(</span><span>url</span><span>,</span><span> </span><span>delim</span><span> </span><span>=</span><span> </span><span>&quot;\t&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>separate</span><span>(</span><span>NAME</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;name&quot;</span><span>,</span><span> </span><span>&quot;BP&quot;</span><span>,</span><span> </span><span>&quot;MF&quot;</span><span>,</span><span> </span><span>&quot;systematic_name&quot;</span><span>,</span><span> </span><span>&quot;number&quot;</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;\\|\\|&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate_each</span><span>(</span><span>funs</span><span>(</span><span>trimws</span><span>),</span><span> </span><span>name</span><span>:</span><span>systematic_name</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>number</span><span>,</span><span> </span><span>-</span><span>GID</span><span>,</span><span> </span><span>-</span><span>YORF</span><span>,</span><span> </span><span>-</span><span>GWEIGHT</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>sample</span><span>,</span><span> </span><span>expression</span><span>,</span><span> </span><span>G</span><span>0.05</span><span>:</span><span>U</span><span>0.3</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>separate</span><span>(</span><span>sample</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;nutrient&quot;</span><span>,</span><span> </span><span>&quot;rate&quot;</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>convert</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>nutrient</span><span> </span><span>=</span><span> </span><span>plyr</span><span>::</span><span>revalue</span><span>(</span><span>nutrient</span><span>,</span><span> </span><span>nutrient_names</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>expression</span><span>),</span><span> </span><span>systematic_name</span><span> </span><span>!=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>systematic_name</span><span>,</span><span> </span><span>nutrient</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>n</span><span>()</span><span> </span><span>==</span><span> </span><span>6</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span></code></pre><p>In our last analysis, we performed linear models using broom and the built-in <code>lm</code> function:</p>
<pre><code><span>library</span><span>(</span><span>broom</span><span>)</span><span>
</span><span>linear_models</span><span> </span><span>&lt;-</span><span> </span><span>cleaned_data</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>name</span><span>,</span><span> </span><span>systematic_name</span><span>,</span><span> </span><span>nutrient</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>do</span><span>(</span><span>tidy</span><span>(</span><span>lm</span><span>(</span><span>expression</span><span> </span><span>~</span><span> </span><span>rate</span><span>,</span><span> </span><span>.</span><span>)))</span></code></pre><p>The above approach is useful and flexible. But <a href="http://simplystatistics.org/2016/02/17/non-tidy-data/">as Jeff notes</a>, it’s not how a computational biologist would typically run a gene expression analysis, for two reasons.</p>
<ul><li><strong>Performing thousands of linear regressions with separate <code>lm</code> calls is slow</strong>. It takes about a minute on my computer. There are computational shortcuts we can take when all of our data is in the form of a gene-by-sample matrix.</li>
<li><strong>We’re not taking statistical advantage of the shared information.</strong> Modern bioinformatics approaches often “share power” across genes, by pooling variance estimates. The approach in the <a href="http://nar.oxfordjournals.org/content/early/2015/01/20/nar.gkv007.long">limma package</a> is one notable example for microarray data, and RNA-Seq tools like edgeR and DESeq2 take a similar approach in their negative binomial models.</li>
</ul><p>We’d like to take advantage of the sophisticated biological modeling tools in Bioconductor. We’re thus going to convert our data into a non-tidy format (a gene by sample matrix), and run it through limma to create a linear model for each gene. Then when we want to visualize, compare, or otherwise manipulate our models, we’ll tidy the model output using biobroom.</p>
<h3>Limma</h3>
<p>Most gene expression packages in Bioconductor expect data to be in a matrix with one row per gene and one column per sample. In <a href="http://varianceexplained.org/r/tidy-genomics-broom">the last post</a> we fit one model for each gene and nutrient combination. So let’s set it up that way using reshape2’s <code>acast()</code>.<a href="http://varianceexplained.org/#fn:spread">1</a></p>
<pre><code><span>library</span><span>(</span><span>reshape2</span><span>)</span><span>
</span><span>exprs</span><span> </span><span>&lt;-</span><span> </span><span>acast</span><span>(</span><span>cleaned_data</span><span>,</span><span> </span><span>systematic_name</span><span> </span><span>+</span><span> </span><span>nutrient</span><span> </span><span>~</span><span> </span><span>rate</span><span>,</span><span>
</span><span>value.var</span><span> </span><span>=</span><span> </span><span>&quot;expression&quot;</span><span>)</span><span>
</span><span>head</span><span>(</span><span>exprs</span><span>)</span></code></pre><pre><code>##                  0.05   0.1  0.15   0.2  0.25  0.3
## Q0017_Ammonia    0.18  0.73  0.05 -0.14 -0.06 0.24
## Q0017_Glucose   -0.21 -0.26 -0.17  0.00  0.10 0.10
## Q0017_Leucine    0.46  0.05  0.42  0.35  0.36 0.09
## Q0017_Phosphate -0.24 -0.35 -0.45 -0.24 -0.35 0.11
## Q0017_Sulfate    2.44  0.64  0.21  0.40 -0.07 0.31
## Q0017_Uracil     1.88  0.03  0.15  0.04  0.14 0.32</code></pre><p>We then need to extract the experiment design, which in this case is just the growth rate:</p>
<pre><code><span>rate</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>colnames</span><span>(</span><span>exprs</span><span>))</span><span>
</span><span>rate</span></code></pre><pre><code>## [1] 0.05 0.10 0.15 0.20 0.25 0.30</code></pre><p><a href="https://bioconductor.org/packages/release/bioc/html/limma.html">limma</a> (“linear modeling of microarrays”) is one of the most popular Bioconductor packages for performing linear-model based differential expression analyses on microarray data. With the data in this matrix form, we’re ready to use it:</p>
<pre><code><span>library</span><span>(</span><span>limma</span><span>)</span><span>
</span><span>fit</span><span> </span><span>&lt;-</span><span> </span><span>lmFit</span><span>(</span><span>exprs</span><span>,</span><span> </span><span>model.matrix</span><span>(</span><span>~</span><span>rate</span><span>))</span><span>
</span><span>eb</span><span> </span><span>&lt;-</span><span> </span><span>eBayes</span><span>(</span><span>fit</span><span>)</span></code></pre><p>This performs a linear regression for each gene. This operation is both faster and more statistically sophisticated than our original use of <code>lm</code> for each gene.</p>
<p>So now we’ve performed our regression. What output do we get?</p>
<pre><code><span>class</span><span>(</span><span>eb</span><span>)</span></code></pre><pre><code>## [1] &quot;MArrayLM&quot;
## attr(,&quot;package&quot;)
## [1] &quot;limma&quot;</code></pre><pre><code><span>summary</span><span>(</span><span>eb</span><span>)</span></code></pre><pre><code>##                  Length Class  Mode     
## coefficients     65074  -none- numeric  
## rank                 1  -none- numeric  
## assign               2  -none- numeric  
## qr                   5  qr     list     
## df.residual      32537  -none- numeric  
## sigma            32537  -none- numeric  
## cov.coefficients     4  -none- numeric  
## stdev.unscaled   65074  -none- numeric  
## pivot                2  -none- numeric  
## Amean            32537  -none- numeric  
## method               1  -none- character
## design              12  -none- numeric  
## df.prior             1  -none- numeric  
## s2.prior             1  -none- numeric  
## var.prior            2  -none- numeric  
## proportion           1  -none- numeric  
## s2.post          32537  -none- numeric  
## t                65074  -none- numeric  
## df.total         32537  -none- numeric  
## p.value          65074  -none- numeric  
## lods             65074  -none- numeric  
## F                32537  -none- numeric  
## F.p.value        32537  -none- numeric</code></pre><p>That’s a lot of outputs, and many of them are matrices of varying shapes. If you want to work with this using tidy tools (and if you’ve been listening, you hopefully do), we need to tidy it:</p>
<pre><code><span>library</span><span>(</span><span>biobroom</span><span>)</span><span>
</span><span>tidy</span><span>(</span><span>eb</span><span>,</span><span> </span><span>intercept</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre><pre><code>## # A tibble: 65,074 x 6
##               gene        term   estimate statistic     p.value        lod
##              &lt;chr>       &lt;chr>      &lt;dbl>     &lt;dbl>       &lt;dbl>      &lt;dbl>
## 1    Q0017_Ammonia (Intercept)  0.3926667  1.591294 0.155459427 -6.3498508
## 2    Q0017_Glucose (Intercept) -0.3533333 -3.172498 0.015599597 -4.0295194
## 3    Q0017_Leucine (Intercept)  0.3873333  2.339366 0.051800996 -5.2780422
## 4  Q0017_Phosphate (Intercept) -0.4493333 -2.732529 0.029158254 -4.6871409
## 5    Q0017_Sulfate (Intercept)  1.9140000  3.937061 0.005595522 -2.9298317
## 6     Q0017_Uracil (Intercept)  1.1846667  2.476906 0.042315927 -5.0718352
## 7    Q0045_Ammonia (Intercept) -1.5060000 -5.842285 0.000629533 -0.5396668
## 8    Q0045_Glucose (Intercept) -0.8513333 -5.235045 0.001195959 -1.2455443
## 9    Q0045_Leucine (Intercept) -0.4440000 -1.807071 0.113591371 -6.0543398
## 10 Q0045_Phosphate (Intercept) -0.7546667 -4.488406 0.002819261 -2.1846537
## # ... with 65,064 more rows</code></pre><p>Notice that this is now in one-row-per-coefficient-per-gene form, much like the output of broom’s <code>tidy()</code> on linear models.</p>
<p>Like broom, biobroom always returns a table<a href="http://varianceexplained.org/#fn:tbl_df">2</a> without rownames that we can feed into standard tidy tools like dplyr and ggplot2. (Note that unlike broom, biobroom requires an <code>intercept = TRUE</code> argument to leave the intercept term, simply because in many genomic datasets- though not ours- the intercept term is almost meaningless). biobroom can also tidy model objects from other tools like edgeR or DESeq2, always giving a consistent format similar to this one.</p>
<p>Now all we’ve got to do split the systematic name and nutrient back up. tidyr’s <code>separate()</code> can do this:</p>
<pre><code><span>td</span><span> </span><span>&lt;-</span><span> </span><span>tidy</span><span>(</span><span>eb</span><span>,</span><span> </span><span>intercept</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>separate</span><span>(</span><span>gene</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;systematic_name&quot;</span><span>,</span><span> </span><span>&quot;nutrient&quot;</span><span>),</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>)</span><span>
</span><span>td</span></code></pre><pre><code>## # A tibble: 65,074 x 7
##    systematic_name  nutrient        term   estimate statistic     p.value
## *            &lt;chr>     &lt;chr>       &lt;chr>      &lt;dbl>     &lt;dbl>       &lt;dbl>
## 1            Q0017   Ammonia (Intercept)  0.3926667  1.591294 0.155459427
## 2            Q0017   Glucose (Intercept) -0.3533333 -3.172498 0.015599597
## 3            Q0017   Leucine (Intercept)  0.3873333  2.339366 0.051800996
## 4            Q0017 Phosphate (Intercept) -0.4493333 -2.732529 0.029158254
## 5            Q0017   Sulfate (Intercept)  1.9140000  3.937061 0.005595522
## 6            Q0017    Uracil (Intercept)  1.1846667  2.476906 0.042315927
## 7            Q0045   Ammonia (Intercept) -1.5060000 -5.842285 0.000629533
## 8            Q0045   Glucose (Intercept) -0.8513333 -5.235045 0.001195959
## 9            Q0045   Leucine (Intercept) -0.4440000 -1.807071 0.113591371
## 10           Q0045 Phosphate (Intercept) -0.7546667 -4.488406 0.002819261
## # ... with 65,064 more rows, and 1 more variables: lod &lt;dbl></code></pre><h3>Analyzing a tidied model</h3>
<p>We can now use the tidy approaches to visualization and interpretation that were explored in previous posts. We could create a <a href="http://varianceexplained.org/interpreting-pvalue-histogram">p-value histogram</a></p>
<pre><code><span>ggplot</span><span>(</span><span>td</span><span>,</span><span> </span><span>aes</span><span>(</span><span>p.value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_histogram</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_grid</span><span>(</span><span>term</span><span> </span><span>~</span><span> </span><span>nutrient</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span></code></pre><p></p>
<p>Or make a volcano plot, comparing statistical significance to effect size (here let’s say just on the slope terms):</p>
<pre><code><span>td</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>term</span><span> </span><span>==</span><span> </span><span>&quot;rate&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>estimate</span><span>,</span><span> </span><span>p.value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span> </span><span>nutrient</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_log10</span><span>()</span><span> </span></code></pre><p></p>
<p>We could easily perform for multiple hypothesis testing within each group, and filter for significant (say, FDR &lt; 1%) changes:</p>
<pre><code><span>td_filtered</span><span> </span><span>&lt;-</span><span> </span><span>td</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>term</span><span>,</span><span> </span><span>nutrient</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>fdr</span><span> </span><span>=</span><span> </span><span>p.adjust</span><span>(</span><span>p.value</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;fdr&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>fdr</span><span> </span><span>&lt;</span><span> </span><span>.01</span><span>)</span></code></pre><p>Or finding the top few significant changes in each group using dplyr’s <code>top_n</code>:</p>
<pre><code><span>top_3</span><span> </span><span>&lt;-</span><span> </span><span>td_filtered</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>term</span><span> </span><span>==</span><span> </span><span>&quot;rate&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>nutrient</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>top_n</span><span>(</span><span>3</span><span>,</span><span> </span><span>abs</span><span>(</span><span>estimate</span><span>))</span><span>
</span><span>top_3</span></code></pre><pre><code>## Source: local data frame [18 x 8]
## Groups: nutrient [6]
## 
##    systematic_name  nutrient  term  estimate  statistic      p.value
##              &lt;chr>     &lt;chr> &lt;chr>     &lt;dbl>      &lt;dbl>        &lt;dbl>
## 1          YAL061W   Sulfate  rate -13.40000 -15.374369 1.159000e-06
## 2          YBR054W Phosphate  rate -23.80571 -12.675600 4.306351e-06
## 3          YBR072W    Uracil  rate -27.01143 -11.870314 6.702423e-06
## 4          YBR116C    Uracil  rate -25.12000  -9.931703 2.199911e-05
## 5          YBR294W Phosphate  rate  24.56571   9.953214 2.168703e-05
## 6          YFL014W   Leucine  rate -18.92000  -6.884237 2.318928e-04
## 7          YHR096C   Ammonia  rate -23.71429 -20.700901 1.496359e-07
## 8          YHR096C   Glucose  rate -20.31429  -7.890506 9.816693e-05
## 9          YHR096C   Leucine  rate -23.18286  -6.324890 3.904599e-04
## 10         YHR096C Phosphate  rate -26.12000 -25.405139 3.616210e-08
## 11         YHR137W   Glucose  rate -21.45143  -9.798727 2.404524e-05
## 12         YIL160C   Sulfate  rate -13.50286 -16.838241 6.214232e-07
## 13         YIL169C   Ammonia  rate -24.99429  -8.439007 6.375875e-05
## 14         YLR327C   Leucine  rate -18.43429 -17.547502 4.679809e-07
## 15         YLR327C    Uracil  rate -28.13714  -7.434840 1.432175e-04
## 16         YMR303C   Glucose  rate -22.60000  -6.101140 4.856028e-04
## 17         YOL155C   Ammonia  rate -28.13714 -11.530132 8.147212e-06
## 18         YPL223C   Sulfate  rate -18.42857 -12.883494 3.857832e-06
## # ... with 2 more variables: lod &lt;dbl>, fdr &lt;dbl></code></pre><p>We could join this with our original data, which would let us visualize the trends for only the most significant genes:</p>
<pre><code><span>top_3</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>significant_nutrient</span><span> </span><span>=</span><span> </span><span>nutrient</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>inner_join</span><span>(</span><span>cleaned_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;systematic_name&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>highlight</span><span> </span><span>=</span><span> </span><span>nutrient</span><span> </span><span>==</span><span> </span><span>significant_nutrient</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>rate</span><span>,</span><span> </span><span>expression</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>nutrient</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>aes</span><span>(</span><span>lty</span><span> </span><span>=</span><span> </span><span>!</span><span>highlight</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;lm&quot;</span><span>,</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>show.legend</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>significant_nutrient</span><span> </span><span>~</span><span> </span><span>systematic_name</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span></code></pre><p></p>
<p>In short, you can once again use the suite of “tidy tools” that we’ve found powerful in genomic analyses.</p>
<h3>Conclusion: Data is wrangled for you, not you for the data</h3>
<p>There’s a classic proverb of computer science from <a href="https://mitpress.mit.edu/sicp/full-text/sicp/book/node3.html">Abelman &amp; Sussman</a>: <strong>“Programs must be written for people to read, and only incidentally for machines to execute.”</strong> I’d say this is even more true for data it is for code. Data scientists need to be very comfortable engaging with their data, not fighting with the representation.</p>
<p>I agree with a lot in <a href="http://simplystatistics.org/2016/02/17/non-tidy-data/">Jeff’s “Non-tidy data” post</a>, but there’s one specific statement I disagree with:</p>
<blockquote>
<p>…you might not use tidy data because many functions require a different, also very clean and useful data format, and you don’t want to have to constantly be switching back and forth.</p>
</blockquote>
<p>I’d counter that switching is a small cost, because switching can be automated. Note that in the above analysis, reshaping the data required only two lines of code and two functions (<code>acast()</code> and <code>tidy()</code>). In contrast, there’s no way to automate critical thinking. Any challenge in plotting, filtering, or merging your data will get directly in the way of answering scientific questions.</p>
<blockquote class="twitter-tweet"><p dir="ltr" lang="en"><a href="https://twitter.com/jtleek">@jtleek</a> <a href="https://twitter.com/mark_scheuerell">@mark_scheuerell</a> <a href="https://twitter.com/hadleywickham">@hadleywickham</a> solution is put energy into tidying first. Otherwise, you will pay off energy 10x in plot/manip</p>— David Robinson (@drob) <a href="https://twitter.com/drob/status/698246351415476224">February 12, 2016</a></blockquote>
<p>It’s thus the job of tool developers to make these “switches” as easy as possible. broom and biobroom play a role in this, as do reshape2 and tidyr. Jeff lists natural language as a type of data that’s best left un-tidy, but since that post Julia Silge and have I developed the <a href="https://github.com/juliasilge/tidytext">tidytext</a> package, and we’ve found it useful for performing text analyses using ggplot2 and dplyr (<a href="http://tidytextmining.com/">see here for examples</a>).</p>
<p>Other examples of operations that are better performed on matrices include correlations and distance calculations, and for those purposes I’m currently working on the <a href="http://github.com/dgrtwo/widyr">widyr</a> package, which wraps these operations to allow a tidy input and tidy output (for example, see <a href="http://varianceexplained.org/r/seven-fav-packages/">this application of the <code>pairwise_cor</code> function</a>).</p>
<p></p>
<h3>Next time: gene set enrichment analysis</h3>
<p>(<strong>Programming note</strong>: this was <a href="http://varianceexplained.org/r/tidy-genomics-broom/">originally</a> my plan for this post, but I decided to preempt it for biobroom!)</p>
<p>These per-gene models can still be difficult to interpret biologically if you’re not familiar with the functions of specific genes. What we really want is a way to summarize the results into “genes involved in this biological process changed their expression.” This is where annotations of gene sets become useful.</p>
<pre><code><span>gene_sets</span><span> </span><span>&lt;-</span><span> </span><span>distinct</span><span>(</span><span>cleaned_data</span><span>,</span><span> </span><span>systematic_name</span><span>,</span><span> </span><span>BP</span><span>,</span><span> </span><span>MF</span><span>)</span><span>
</span><span>td</span><span> </span><span>%>%</span><span>
</span><span>inner_join</span><span>(</span><span>gene_sets</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>BP</span><span> </span><span>==</span><span> </span><span>&quot;leucine biosynthesis&quot;</span><span>,</span><span> </span><span>term</span><span> </span><span>==</span><span> </span><span>&quot;(Intercept)&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>nutrient</span><span> </span><span>=</span><span> </span><span>reorder</span><span>(</span><span>nutrient</span><span>,</span><span> </span><span>estimate</span><span>,</span><span> </span><span>median</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>nutrient</span><span>,</span><span> </span><span>estimate</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>systematic_name</span><span>),</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Limiting nutrient&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Intercept (expression at low concentration)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Genes involved in leucine biosynthesis&quot;</span><span>)</span></code></pre><p></p>
<p>Notice how clear it is that these genes respond to leucine starvation in particular. This can be applied to gene sets containing dozens or even hundreds of genes while still making the general trend apparent. Furthermore, we could use these summaries to look at many gene sets at once, and even use statistical tests to discover new gene sets that respond to starvation.</p>
<p>Thus, in my next post in this series, we’ll apply our “tidy modeling” approach to a new problem. Instead of testing whether each gene responds to starvation in an interesting way, we’ll test functional groups of genes (a general method called <a href="http://www.nature.com/nrg/journal/v17/n6/full/nrg.2016.29.html">gene set analysis</a>) in order to develop higher-level biological insights. And we’ll do it with these same set of tidy tools we’ve been using so far.</p>
<div>
<ol><li>
<p>We could have used tidyr’s <code>spread</code> function, but <code>acast</code> actually saves us a few steps by giving us a matrix with rownames, rather than a data frame, right away. <a href="http://varianceexplained.org/#fnref:spread">↩</a></p>
</li>
<li>
<p>Note that by default biobroom returns a <code>tbl_df</code> rather than a <code>data.frame</code>. This is because tidy genomic output is usually many thousands of rows, so printing it is usually not practical. The class it returns can be set (to be a tbl_df, data.frame, or data.table) through the <code>biobroom.return</code> global option. <a href="http://varianceexplained.org/#fnref:tbl_df">↩</a></p>
</li>
</ol></div>