/
session3.html
788 lines (699 loc) · 42.2 KB
/
session3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="Kevin Wang and Garth Tarr" />
<meta name="date" content="2019-05-11" />
<title>session3</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<script defer src="https://pro.fontawesome.com/releases/v5.0.13/js/all.js" integrity="sha384-d84LGg2pm9KhR4mCAs3N29GQ4OYNy+K+FBHX8WhimHpPm86c839++MDABegrZ3gn" crossorigin="anonymous"></script>
<!-- clipboard.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js" integrity="sha256-FiZwavyI2V6+EXO1U+xzLG3IKldpiTFf3153ea9zikQ=" crossorigin="anonymous"></script>
<script src="assets/clipboard.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
background-color: #ffffff;
color: #a0a0a0;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #a0a0a0; padding-left: 4px; }
div.sourceCode
{ color: #1f1c1b; background-color: #ffffff; }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span. { color: #1f1c1b; } /* Normal */
code span.al { color: #bf0303; background-color: #f7e6e6; font-weight: bold; } /* Alert */
code span.an { color: #ca60ca; } /* Annotation */
code span.at { color: #0057ae; } /* Attribute */
code span.bn { color: #b08000; } /* BaseN */
code span.bu { color: #644a9b; font-weight: bold; } /* BuiltIn */
code span.cf { color: #1f1c1b; font-weight: bold; } /* ControlFlow */
code span.ch { color: #924c9d; } /* Char */
code span.cn { color: #aa5500; } /* Constant */
code span.co { color: #898887; } /* Comment */
code span.cv { color: #0095ff; } /* CommentVar */
code span.do { color: #607880; } /* Documentation */
code span.dt { color: #0057ae; } /* DataType */
code span.dv { color: #b08000; } /* DecVal */
code span.er { color: #bf0303; text-decoration: underline; } /* Error */
code span.ex { color: #0095ff; font-weight: bold; } /* Extension */
code span.fl { color: #b08000; } /* Float */
code span.fu { color: #644a9b; } /* Function */
code span.im { color: #ff5500; } /* Import */
code span.in { color: #b08000; } /* Information */
code span.kw { color: #1f1c1b; font-weight: bold; } /* Keyword */
code span.op { color: #1f1c1b; } /* Operator */
code span.ot { color: #006e28; } /* Other */
code span.pp { color: #006e28; } /* Preprocessor */
code span.re { color: #0057ae; background-color: #e0e9f8; } /* RegionMarker */
code span.sc { color: #3daee9; } /* SpecialChar */
code span.ss { color: #ff5500; } /* SpecialString */
code span.st { color: #bf0303; } /* String */
code span.va { color: #0057ae; } /* Variable */
code span.vs { color: #bf0303; } /* VerbatimString */
code span.wa { color: #bf0303; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="assets/kbd.css" type="text/css" />
<link rel="stylesheet" href="assets/style.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">BioinfoSummer - Tidyverse</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="session1.html">Session 1</a>
</li>
<li>
<a href="session2.html">Session 2</a>
</li>
<li>
<a href="session3.html">Session 3</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="community.html">
<span class="fa fa-users"></span>
Community
</a>
</li>
<li>
<a href="tips.html">
<span class="fa fa-star"></span>
Tips
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">session3</h1>
<h4 class="author">Kevin Wang and Garth Tarr</h4>
<h4 class="date">05/11/2019</h4>
</div>
<div id="learning-outcomes-of-this-session" class="section level1">
<h1><span class="header-section-number">1</span> Learning outcomes of this session</h1>
<ul>
<li>Compute differential expression p-values using the <code>broom</code> and <code>purrr</code> packages</li>
<li>Make a boxplot of gene expression values with <code>ggplot2</code></li>
</ul>
</div>
<div id="loading-packages" class="section level1">
<h1><span class="header-section-number">2</span> Loading packages</h1>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">library</span>(tidyverse)</span></code></pre></div>
</div>
<div id="a-quick-word-about-tibble" class="section level1">
<h1><span class="header-section-number">3</span> A quick word about <code>tibble</code></h1>
<p>Most of the time, you can think of a <code>tibble</code> as being identical to a <code>data.frame</code>. In fact, in Session 1 and 2, even though we keep referring to the data as <code>data.frame</code>, but they are actually <code>tibble</code>s. A <code>tibble</code> is the default alternative to <code>data.frame</code> in the <code>tidyverse</code>.</p>
<p>The reason that <code>tibble</code> is so useful is that it allows for <code>list</code> objects as columns. This will come in very handy when performing modelling tasks. Have a look at the picture below:</p>
<p><img src="https://rstudio-education.github.io/tidyverse-cookbook/images/tidyr-nest.png" /></p>
<p>Suppose that we want to perform modelling within each <code>Species</code> of three different iris flowers, it would be much better to utilise this new structure of representation of data in the middle and apply the modelling for each element of the <code>Measurements</code> column. This is indeed what we will try to achieve in this session.</p>
</div>
<div id="loading-cleaned-sample-data" class="section level1">
<h1><span class="header-section-number">4</span> Loading cleaned sample data</h1>
<p>The <code>session3_data.csv</code> data is almost identical to the <code>wide_sample_ge_data.csv</code> file that we just saved in Session 2. Except that we actually used all 54 genes <a href="./session2.html#reading_ge">when reading in the downloaded data</a>. We will take the logarithm of all the gene expression values for convenience.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1"></a>session3_data =<span class="st"> </span><span class="kw">read_csv</span>(<span class="dt">file =</span> <span class="st">"data/session3_data.csv"</span>) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb2-2"><a href="#cb2-2"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">select</span>(<span class="op">-</span>time, <span class="op">-</span>dose)</span>
<span id="cb2-3"><a href="#cb2-3"></a></span>
<span id="cb2-4"><a href="#cb2-4"></a>clean_data =<span class="st"> </span>session3_data <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb2-5"><a href="#cb2-5"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">mutate_if</span>(<span class="dt">.predicate =</span> is.numeric, <span class="dt">.funs =</span> log) </span>
<span id="cb2-6"><a href="#cb2-6"></a></span>
<span id="cb2-7"><a href="#cb2-7"></a>clean_data</span></code></pre></div>
</div>
<div id="performing-a-t-tests" class="section level1">
<h1><span class="header-section-number">5</span> Performing a t-tests</h1>
<p>A typical task in bioinformatics is to perform differential gene expression analysis. That is, we want to identify genes that has a different expression levels between two conditions. While there are many ways that this can be done, we will opt for the simplest approach of using a t-test.</p>
<p>In <code>R</code>, t-test can be performed using the <code>t.test</code> function. Below, we will perform a t-test on the Ahrrr gene between the two strains of mice. (Note: statistically speaking, this test is a bad idea due to the existence of different <code>dose</code> and <code>time</code> could creating confounding. However, since statistics is not the focus of this workshop so we will ignore this fact for now.)</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1"></a><span class="kw">t.test</span>(Ahrr <span class="op">~</span><span class="st"> </span>strain, <span class="dt">data =</span> clean_data)</span></code></pre></div>
<p>The output of the <code>t.test</code> function can be a bit hard to read or manipulate into a data.frame. This is where the <code>tidy()</code> function from the <code>broom</code> package can help.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1"></a><span class="kw">t.test</span>(Ahrr <span class="op">~</span><span class="st"> </span>strain, <span class="dt">data =</span> clean_data) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb4-2"><a href="#cb4-2"></a><span class="st"> </span>broom<span class="op">::</span><span class="kw">tidy</span>()</span></code></pre></div>
<p>Similarly, we can do the same for the Cxcr7 gene.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1"></a><span class="kw">t.test</span>(Cxcr7 <span class="op">~</span><span class="st"> </span>strain, <span class="dt">data =</span> clean_data) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb5-2"><a href="#cb5-2"></a><span class="st"> </span>broom<span class="op">::</span><span class="kw">tidy</span>()</span></code></pre></div>
<p>In the <code>clean_data</code>, we have 54 genes in total. If we want to perform t-test on each gene, should we type out 54 lines of code? The answer is no! This is not the most efficient way to utilise your time and there are fast ways to achieve the same outcome. However, we may need to take a quite diversion to understand loops and iterations.</p>
</div>
<div id="for-loops" class="section level1">
<h1><span class="header-section-number">6</span> <code>for</code> loops</h1>
<p><code>for</code> loops are common in any programming language. It simply list out the instructions and the iterations are specified using an index. For example, the following code prints out 1 to 5.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="dv">5</span>){</span>
<span id="cb6-2"><a href="#cb6-2"></a> <span class="kw">print</span>(i)</span>
<span id="cb6-3"><a href="#cb6-3"></a>}</span></code></pre></div>
<p>We can use the <code>for</code> loop to go through each column in <code>clean_data</code>, calculate the t-test statistics and print them out. However, this output is often not plotting-friendly because we need to merge this output with the original gene expression values before any plotting can be done. We can use a <code>tidyverse</code> framework to achieve the same thing.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1"></a><span class="co">## This code was not executed</span></span>
<span id="cb7-2"><a href="#cb7-2"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">3</span><span class="op">:</span><span class="dv">56</span>){</span>
<span id="cb7-3"><a href="#cb7-3"></a> <span class="kw">t.test</span>(clean_data <span class="op">%>%</span><span class="st"> </span><span class="kw">pull</span>(i) <span class="op">~</span><span class="st"> </span>clean_data<span class="op">$</span>strain) <span class="op">%>%</span></span>
<span id="cb7-4"><a href="#cb7-4"></a><span class="st"> </span>broom<span class="op">::</span><span class="kw">tidy</span>() <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb7-5"><a href="#cb7-5"></a><span class="st"> </span><span class="kw">print</span>()</span>
<span id="cb7-6"><a href="#cb7-6"></a>}</span></code></pre></div>
</div>
<div id="nesting-data" class="section level1">
<h1><span class="header-section-number">7</span> Nesting data</h1>
<p>Now, let’s think about the task of calculating a t-test for each gene. This may seem like a very difficult task, but we can identify its components to achieve simplifications and generalisations.</p>
<p>First, we should run the <code>t.test</code> and the <code>broom::tidy</code> function on gene expression values for a selected gene. Then, we do exactly the same operation for another 53 genes. So the grand overall task is actually very repetitive and we are really just applying the following function to 54 datasets, where each dataset only contains one selected gene.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1"></a>tidy_test =<span class="st"> </span><span class="cf">function</span>(this_data){</span>
<span id="cb8-2"><a href="#cb8-2"></a> <span class="kw">t.test</span>(gene_expression <span class="op">~</span><span class="st"> </span>strain, <span class="dt">data =</span> this_data) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb8-3"><a href="#cb8-3"></a><span class="st"> </span>broom<span class="op">::</span><span class="kw">tidy</span>()</span>
<span id="cb8-4"><a href="#cb8-4"></a>}</span></code></pre></div>
<p>So now, our very difficult task of performing 54 t-tests is simplified to spliting <code>clean_data</code> into 54 datasets, one for each gene.</p>
<p>While there are many ways this can be done in <code>R</code>, we will use a <code>tidyverse</code> framework that has the advantage of readable codes and generalisability.</p>
<p>First, let’s pivot our data into a long format.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1"></a>gene_long =<span class="st"> </span>clean_data <span class="op">%>%</span></span>
<span id="cb9-2"><a href="#cb9-2"></a><span class="st"> </span>tidyr<span class="op">::</span><span class="kw">pivot_longer</span>(</span>
<span id="cb9-3"><a href="#cb9-3"></a> <span class="dt">cols =</span> <span class="op">-</span><span class="kw">c</span>(<span class="st">"strain"</span>, <span class="st">"sample"</span>),</span>
<span id="cb9-4"><a href="#cb9-4"></a> <span class="dt">names_to =</span> <span class="st">"gene_symbol"</span>,</span>
<span id="cb9-5"><a href="#cb9-5"></a> <span class="dt">values_to =</span> <span class="st">"gene_expression"</span>)</span>
<span id="cb9-6"><a href="#cb9-6"></a></span>
<span id="cb9-7"><a href="#cb9-7"></a>gene_long</span></code></pre></div>
<p>A simplification of <code>gene_long</code> is this diagram:</p>
<p><img src="assets/nest1.png" /></p>
<p>Then we will use the <code>group_by</code> function and then apply the <code>nest</code> function from the <code>tidyr</code> package.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1"></a>gene_nest =<span class="st"> </span>gene_long <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb10-2"><a href="#cb10-2"></a><span class="st"> </span><span class="kw">group_by</span>(gene_symbol) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb10-3"><a href="#cb10-3"></a><span class="st"> </span>tidyr<span class="op">::</span><span class="kw">nest</span>(<span class="dt">.key =</span> <span class="st">"small_data"</span>) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb10-4"><a href="#cb10-4"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">ungroup</span>()</span>
<span id="cb10-5"><a href="#cb10-5"></a></span>
<span id="cb10-6"><a href="#cb10-6"></a>gene_nest</span>
<span id="cb10-7"><a href="#cb10-7"></a></span>
<span id="cb10-8"><a href="#cb10-8"></a>gene_nest<span class="op">$</span>small_data[<span class="dv">1</span><span class="op">:</span><span class="dv">2</span>]</span></code></pre></div>
<p>So, a lot of things happened here. Let’s explain.</p>
<p>You might remember the <code>group_by</code> function when we were trying to use the <code>summarise</code> function to calculate summary statistics in Session 1. This is exactly what we achieved with <code>gene_nest</code>, since the <code>gene_long</code> data with 8,316 rows is summarised into a smaller data.frame with 54 rows. The idea is that any reduction of number of rows can considered to be a summary of the long data.</p>
<p>In <code>gene_nest</code>, each gene only has “one row”, but in the <code>small_data</code> column, each row of elment in the <code>small_data</code> column is a <code>data.frame</code> of size 154 rows and 3 columns (indicated by the <code>[154 x 3]</code>). The reason that we have 154 observations is because we have 154 samples (number rows of <code>clean_data</code>) to begin with. The reason that we have 3 columns is because these are all the columns that we didn’t put into <code>group_by</code>.</p>
<p>So why are we doing this complicated structure? This is because our original aim is to split the <code>gene_long</code> data into 54 smaller data, and we have in fact achived that here with the <code>small_data</code> column.</p>
<p>A simplification of <code>small_data</code> is this diagram:</p>
<p><img src="assets/nest2.png" /></p>
<p>Now, we simply need to apply the <code>tidy_test</code> function onto the <code>small_data</code> column in this <code>gene_nest</code> data.frame. Again, we could do that with a for loop, but the same difficult issue of visualisation still applies.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a><span class="co">## This code is not executed.</span></span>
<span id="cb11-2"><a href="#cb11-2"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="dv">54</span>){</span>
<span id="cb11-3"><a href="#cb11-3"></a> <span class="kw">tidy_test</span>(gene_nest<span class="op">$</span>small_data[[i]])</span>
<span id="cb11-4"><a href="#cb11-4"></a>}</span></code></pre></div>
</div>
<div id="map-function-in-purrr" class="section level1">
<h1><span class="header-section-number">8</span> <code>map</code> function in <code>purrr</code></h1>
<p>The <code>map</code> function from the <code>purrr</code> package is a tremendously powerful function. In simply terms, it allows us to write a <code>for</code> loop is a very simple and readable way.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1"></a><span class="co">## This code is not executed</span></span>
<span id="cb12-2"><a href="#cb12-2"></a>purrr<span class="op">::</span><span class="kw">map</span>(<span class="dt">.x =</span> gene_nest<span class="op">$</span>small_data,</span>
<span id="cb12-3"><a href="#cb12-3"></a> <span class="dt">.f =</span> tidy_test)</span></code></pre></div>
<p>Why is this code better than the <code>for</code> loop even though they achieved the same output? The biggest reason is that <strong>we don’t have to read and understand individual lines</strong> inside the <code>for</code> loop. We simply need to know:</p>
<ul>
<li><code>.x</code>, the input</li>
<li><code>.f</code>, the function</li>
</ul>
<p>Then the function <code>.f</code> will be applied onto every element of the <code>.x</code> input.</p>
<p>Another reason why the <code>map</code> function is superior to a <code>for</code> loop is how easy it can work with the <code>mutate</code> function in <code>dplyr</code>. The following code can be read as:</p>
<ul>
<li>In the <code>gene_nest</code> data, we want to <code>mutate</code> (create) a new column</li>
<li>This new column will be called <code>tidy_result</code> and it is defined as mapping the <code>tidy_test</code> function to every element if the <code>small_data</code> column.</li>
</ul>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1"></a>gene_test =<span class="st"> </span>gene_nest <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb13-2"><a href="#cb13-2"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">mutate</span>(</span>
<span id="cb13-3"><a href="#cb13-3"></a> <span class="dt">tidy_result =</span> purrr<span class="op">::</span><span class="kw">map</span>(</span>
<span id="cb13-4"><a href="#cb13-4"></a> <span class="dt">.x =</span> small_data,</span>
<span id="cb13-5"><a href="#cb13-5"></a> <span class="dt">.f =</span> tidy_test)</span>
<span id="cb13-6"><a href="#cb13-6"></a> )</span>
<span id="cb13-7"><a href="#cb13-7"></a></span>
<span id="cb13-8"><a href="#cb13-8"></a>gene_test</span></code></pre></div>
<p>A simplification of <code>gene_test</code> is this diagram:</p>
<p><img src="assets/nest3.png" /></p>
<p>This is a very powerful framework, because it allows us to create new statistics horizonally inside a <code>data.frame</code>, keeping track of the matching genes/observations. You may not experience this yourself, but one of the most common mistake in data analysis is the mis-matching of rows when combining datasets and statistics. The use of the <code>mutate</code> and <code>map</code> functions allows all intermediate steps to be stored as a column inside a data.frame, preventing any mis-matchings.</p>
</div>
<div id="plotting-t-test-results" class="section level1">
<h1><span class="header-section-number">9</span> Plotting t-test results</h1>
<p>The <code>tidy_result</code> column in <code>gene_test</code> is now a list of tibbles (think of them as data.frames). Each of these tibbles contains t-test statistic and p-value associated with each gene.</p>
<p>We will now need to expand this column. The function we will use is <code>unnest</code> from the <code>tidyr</code> package. As you may already guessed, this function undoes the <code>nest</code> operation. In this case, we want to undo the <code>tidy_result</code> column only.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1"></a>gene_test<span class="op">$</span>tidy_result[<span class="dv">1</span><span class="op">:</span><span class="dv">2</span>]</span>
<span id="cb14-2"><a href="#cb14-2"></a></span>
<span id="cb14-3"><a href="#cb14-3"></a>gene_test_long =<span class="st"> </span>gene_test <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb14-4"><a href="#cb14-4"></a><span class="st"> </span>tidyr<span class="op">::</span><span class="kw">unnest</span>(tidy_result)</span>
<span id="cb14-5"><a href="#cb14-5"></a></span>
<span id="cb14-6"><a href="#cb14-6"></a>gene_test_long</span></code></pre></div>
<p>Armed with all the t-test statistics and p-values, we may want to perform some quick visualisations.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1"></a>gene_test_long <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb15-2"><a href="#cb15-2"></a><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> p.value)) <span class="op">+</span></span>
<span id="cb15-3"><a href="#cb15-3"></a><span class="st"> </span><span class="kw">geom_histogram</span>()</span></code></pre></div>
<p><img src="session3_files/figure-html/unnamed-chunk-15-1.png" width="768" /></p>
<p>This histogram shows that we actually have about 1/3 of genes significantly differentially expressed (DE) between the two strains. This is usually a problem with gene expression analysis because if all t-test assumptions are met, the p-values should be uniformly distributed over the [0,1] interval. This inflation is could be attributed to normalisation issues with the data since we have only taken the most naive approach of taking the logarithm. Additionally, we should also note that this is a <em>targeted</em> gene expression data where genes are typically selected due to differential patterns they showed in pilot studies. We will not commenton the biological implication of this histogram further, but proceed with another common analysis, which is to create boxplots of the top differentially expressed genes.</p>
<div id="boxplots-of-top-differentially-expressed-genes" class="section level2">
<h2><span class="header-section-number">9.1</span> Boxplots of top differentially expressed genes</h2>
<p>In this section, we will first extract the top 6 DE genes and make a boxplot for each of them between the two strains.</p>
<p>The following code allows us to subset the data.frame to the 6 genes with smallest p-values (i.e. most differentially expressed). However, we shouldn’t forget that all the gene expression values are still stored in the <code>small_data</code> column. We need to apply the <code>unnest</code> operation to extract these values.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1"></a>gene_test_long <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb16-2"><a href="#cb16-2"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">filter</span>(<span class="kw">rank</span>(p.value) <span class="op"><=</span><span class="st"> </span><span class="dv">6</span>) </span>
<span id="cb16-3"><a href="#cb16-3"></a></span>
<span id="cb16-4"><a href="#cb16-4"></a>boxplot_data =<span class="st"> </span>gene_test_long <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb16-5"><a href="#cb16-5"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">filter</span>(<span class="kw">rank</span>(p.value) <span class="op"><=</span><span class="st"> </span><span class="dv">6</span>) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb16-6"><a href="#cb16-6"></a><span class="st"> </span>tidyr<span class="op">::</span><span class="kw">unnest</span>(small_data)</span>
<span id="cb16-7"><a href="#cb16-7"></a></span>
<span id="cb16-8"><a href="#cb16-8"></a>boxplot_data</span></code></pre></div>
<p>And now, let’s perform the final plotting!</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1"></a>boxplot_data <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb17-2"><a href="#cb17-2"></a><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> strain, </span>
<span id="cb17-3"><a href="#cb17-3"></a> <span class="dt">y =</span> gene_expression)) <span class="op">+</span></span>
<span id="cb17-4"><a href="#cb17-4"></a><span class="st"> </span><span class="kw">geom_boxplot</span>() <span class="op">+</span></span>
<span id="cb17-5"><a href="#cb17-5"></a><span class="st"> </span><span class="kw">facet_wrap</span>(<span class="op">~</span><span class="st"> </span>gene_symbol, <span class="dt">scales =</span> <span class="st">"free"</span>)</span></code></pre></div>
<p><img src="session3_files/figure-html/unnamed-chunk-17-1.png" width="768" /></p>
<p>We can also prettify the plots by creating a <code>label</code> column in the data, which combines the gene symbols and p-values.</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1"></a>boxplot_data <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb18-2"><a href="#cb18-2"></a><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">mutate</span>(<span class="dt">label =</span> <span class="kw">paste0</span>(</span>
<span id="cb18-3"><a href="#cb18-3"></a> gene_symbol, <span class="st">", p-value = "</span>, <span class="kw">signif</span>(p.value, <span class="dv">2</span>))) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb18-4"><a href="#cb18-4"></a><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> strain, </span>
<span id="cb18-5"><a href="#cb18-5"></a> <span class="dt">y =</span> gene_expression)) <span class="op">+</span></span>
<span id="cb18-6"><a href="#cb18-6"></a><span class="st"> </span><span class="kw">geom_boxplot</span>() <span class="op">+</span></span>
<span id="cb18-7"><a href="#cb18-7"></a><span class="st"> </span><span class="kw">facet_wrap</span>(<span class="op">~</span><span class="st"> </span>label, <span class="dt">scales =</span> <span class="st">"free"</span>) <span class="op">+</span></span>
<span id="cb18-8"><a href="#cb18-8"></a><span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Strains"</span>, </span>
<span id="cb18-9"><a href="#cb18-9"></a> <span class="dt">y =</span> <span class="st">"Gene expression values"</span>)</span></code></pre></div>
<p><img src="session3_files/figure-html/unnamed-chunk-18-1.png" width="768" /></p>
<p>Alternatively, the <code>ggpubr</code> package also offers some default graphics that are similar to <code>ggplot2</code>, though with a different syntax.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1"></a><span class="kw">library</span>(ggpubr)</span></code></pre></div>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1"></a><span class="kw">ggboxplot</span>(</span>
<span id="cb20-2"><a href="#cb20-2"></a> <span class="dt">data =</span> boxplot_data,</span>
<span id="cb20-3"><a href="#cb20-3"></a> <span class="dt">x =</span> <span class="st">"strain"</span>, </span>
<span id="cb20-4"><a href="#cb20-4"></a> <span class="dt">y =</span> <span class="st">"gene_expression"</span>,</span>
<span id="cb20-5"><a href="#cb20-5"></a> <span class="dt">facet.by =</span> <span class="st">"gene_symbol"</span>,</span>
<span id="cb20-6"><a href="#cb20-6"></a> <span class="dt">scales =</span> <span class="st">"free"</span>) <span class="op">+</span></span>
<span id="cb20-7"><a href="#cb20-7"></a><span class="st"> </span>ggpubr<span class="op">::</span><span class="kw">stat_compare_means</span>()</span></code></pre></div>
<p><img src="session3_files/figure-html/unnamed-chunk-20-1.png" width="768" /></p>
</div>
</div>
<div id="references" class="section level1">
<h1><span class="header-section-number">10</span> References</h1>
<p>Wickham et al., (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686, <a href="https://doi.org/10.21105/joss.01686" class="uri">https://doi.org/10.21105/joss.01686</a></p>
</div>
<div id="session-info" class="section level1">
<h1><span class="header-section-number">11</span> Session Info</h1>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1"></a><span class="kw">sessionInfo</span>()</span></code></pre></div>
<pre><code>## R version 3.6.1 (2019-07-05)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggpubr_0.2.4 magrittr_1.5 visdat_0.5.3 readxl_1.3.1
## [5] janitor_1.2.0 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
## [9] purrr_0.3.3 readr_1.3.1 tidyr_1.0.0 tibble_2.1.3
## [13] ggplot2_3.2.1 tidyverse_1.3.0
##
## loaded via a namespace (and not attached):
## [1] tidyselect_0.2.5 xfun_0.11 haven_2.2.0 lattice_0.20-38
## [5] snakecase_0.11.0 colorspace_1.4-1 vctrs_0.2.0 generics_0.0.2
## [9] htmltools_0.4.0 yaml_2.2.0 utf8_1.1.4 rlang_0.4.2
## [13] pillar_1.4.2 withr_2.1.2 glue_1.3.1 DBI_1.0.0
## [17] dbplyr_1.4.2 modelr_0.1.5 lifecycle_0.1.0 ggsignif_0.6.0
## [21] munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.5
## [25] evaluate_0.14 labeling_0.3 knitr_1.26 fansi_0.4.0
## [29] broom_0.5.2 Rcpp_1.0.3 backports_1.1.5 scales_1.1.0
## [33] jsonlite_1.6 farver_2.0.1 fs_1.3.1 hms_0.5.2
## [37] digest_0.6.23 stringi_1.4.3 grid_3.6.1 cli_1.1.0
## [41] tools_3.6.1 lazyeval_0.2.2 crayon_1.3.4 pkgconfig_2.0.3
## [45] zeallot_0.1.0 ellipsis_0.3.0 xml2_1.2.2 reprex_0.3.0
## [49] lubridate_1.7.4 assertthat_0.2.1 rmarkdown_1.18 httr_1.4.1
## [53] rstudioapi_0.10 R6_2.4.1 icon_0.1.0 nlme_3.1-142
## [57] compiler_3.6.1</code></pre>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open')
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>