-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR_loops.html
846 lines (816 loc) · 66.6 KB
/
R_loops.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.269">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Solomon Chak">
<meta name="dcterms.date" content="2023-03-12">
<title>R Loops</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > 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;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="R_loops_files/libs/clipboard/clipboard.min.js"></script>
<script src="R_loops_files/libs/quarto-html/quarto.js"></script>
<script src="R_loops_files/libs/quarto-html/popper.min.js"></script>
<script src="R_loops_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="R_loops_files/libs/quarto-html/anchor.min.js"></script>
<link href="R_loops_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="R_loops_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="R_loops_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="R_loops_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="R_loops_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#load-libraries" id="toc-load-libraries" class="nav-link active" data-scroll-target="#load-libraries"><span class="toc-section-number">1</span> Load libraries</a></li>
<li><a href="#what-are-loops" id="toc-what-are-loops" class="nav-link" data-scroll-target="#what-are-loops"><span class="toc-section-number">2</span> What are loops?</a></li>
<li><a href="#give-it-a-try" id="toc-give-it-a-try" class="nav-link" data-scroll-target="#give-it-a-try"><span class="toc-section-number">3</span> Give it a try!</a></li>
<li><a href="#load-prepare-data" id="toc-load-prepare-data" class="nav-link" data-scroll-target="#load-prepare-data"><span class="toc-section-number">4</span> Load & prepare data</a></li>
<li><a href="#analysis" id="toc-analysis" class="nav-link" data-scroll-target="#analysis"><span class="toc-section-number">5</span> Analysis</a></li>
<li><a href="#assignment" id="toc-assignment" class="nav-link" data-scroll-target="#assignment"><span class="toc-section-number">6</span> Assignment</a></li>
</ul>
</nav>
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">R Loops</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Solomon Chak </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">March 12, 2023</p>
</div>
</div>
</div>
</header>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://live.staticflickr.com/65535/48046437498_c1ac440551_b.jpg" class="img-fluid figure-img"></p>
</figure>
</div>
<section id="load-libraries" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Load libraries</h1>
<p>Install a new package</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"plyr"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Load libraries</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">"dplyr"</span>)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">"tidyr"</span>)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">"ggplot2"</span>)</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">"plyr"</span>)</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">"Rmisc"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="what-are-loops" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> What are loops?</h1>
<p>Loops are used in computer languages to repeat the same task many times, typically across files, or lines or sections within a data set.</p>
<p>For example, you could write some code to perform variant calling that maps the raw sequences from a sample against the reference sequence to generate a .vcf file. You can loop the same code across many samples without the need to run each code separately. Most of the time, this kinds of loop is done in the Linus environment.</p>
<p>Here is an example that occurs more often within R. You may want to apply the same function across all rows in a dataframe. This is a loop that we have already used before using <code>mutate</code>.</p>
<p>Let’s first make a dataframe with one <code>Group</code> column and two values columns <code>V1</code> and <code>V2</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>) <span class="co"># this will allow all our "random" data to be the same</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>test_df <span class="ot">=</span> <span class="fu">data.frame</span>(</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="at">Group =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>, <span class="st">"D"</span>), <span class="dv">3</span>), <span class="co"># repeat ABCD 3 times</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="at">V1 =</span> <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">1000</span>, <span class="dv">12</span>), <span class="co"># randomly sample 12 numbers between 1-1000</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="at">V2 =</span> <span class="fu">seq</span>(<span class="dv">10</span>, <span class="dv">120</span>, <span class="dv">10</span>) <span class="co"># Sequence between 10-120, separated by 10</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>test_df</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group V1 V2
1 A 836 10
2 B 679 20
3 C 129 30
4 D 930 40
5 A 509 50
6 B 471 60
7 C 299 70
8 D 270 80
9 A 978 90
10 B 187 100
11 C 307 110
12 D 597 120</code></pre>
</div>
</div>
<p>We can use <code>mutate</code> to “loop” across rows and create a new column <code>V3</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mutate</span>(test_df, <span class="at">V3 =</span> V1 <span class="sc">+</span> V2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group V1 V2 V3
1 A 836 10 846
2 B 679 20 699
3 C 129 30 159
4 D 930 40 970
5 A 509 50 559
6 B 471 60 531
7 C 299 70 369
8 D 270 80 350
9 A 978 90 1068
10 B 187 100 287
11 C 307 110 417
12 D 597 120 717</code></pre>
</div>
</div>
<p>We can also loop across each <code>Group</code> using <code>ddply</code> from the package <code>plyr</code>, which takes in a dataframe and output a dataframe. Arguments for <code>ddply</code> are: dataframe, variable_for_grouping, and a function.</p>
<p>To illustrate, we will apply <code>nrow</code> on each <code>Group</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ddply</span>(test_df, <span class="st">"Group"</span>, nrow)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group V1
1 A 3
2 B 3
3 C 3
4 D 3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Remember that we could do that with table(test_df$Group), but it is quite convoluted as we need to convert it to a dataframe and fix the row names.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>What’s happening behind the scene of <code>ddply(test_df, "Group", nrow)</code> is a process that involves something like this, but in a much more efficient way:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. subset the dataframe "test_df" by Group</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>tmp_A <span class="ot">=</span> <span class="fu">filter</span>(test_df, Group<span class="sc">==</span><span class="st">"A"</span>)</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>tmp_B <span class="ot">=</span> <span class="fu">filter</span>(test_df, Group<span class="sc">==</span><span class="st">"B"</span>)</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>tmp_C <span class="ot">=</span> <span class="fu">filter</span>(test_df, Group<span class="sc">==</span><span class="st">"C"</span>)</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a>tmp_D <span class="ot">=</span> <span class="fu">filter</span>(test_df, Group<span class="sc">==</span><span class="st">"D"</span>)</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="co"># 2. apply the function nrow to each subset</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a>tmp_A_nrow <span class="ot">=</span> <span class="fu">nrow</span>(tmp_A)</span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a>tmp_B_nrow <span class="ot">=</span> <span class="fu">nrow</span>(tmp_B)</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a>tmp_C_nrow <span class="ot">=</span> <span class="fu">nrow</span>(tmp_C)</span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a>tmp_D_nrow <span class="ot">=</span> <span class="fu">nrow</span>(tmp_D)</span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a><span class="co"># 3. Combine the results from 2 to a dataframe.</span></span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a><span class="fu">data.frame</span>(<span class="at">Group =</span> <span class="fu">unique</span>(test_df<span class="sc">$</span>Group), </span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a> <span class="at">V1 =</span> <span class="fu">c</span>(tmp_A_nrow, tmp_B_nrow, tmp_C_nrow, tmp_D_nrow)</span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a><span class="do">## Group V1</span></span>
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true" tabindex="-1"></a><span class="do">## 1 A 3</span></span>
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true" tabindex="-1"></a><span class="do">## 2 B 3</span></span>
<span id="cb10-20"><a href="#cb10-20" aria-hidden="true" tabindex="-1"></a><span class="do">## 3 C 3</span></span>
<span id="cb10-21"><a href="#cb10-21" aria-hidden="true" tabindex="-1"></a><span class="do">## 4 D 3</span></span>
<span id="cb10-22"><a href="#cb10-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-23"><a href="#cb10-23" aria-hidden="true" tabindex="-1"></a><span class="co"># 4. Cean up all the intermediate files that's made here!</span></span>
<span id="cb10-24"><a href="#cb10-24" aria-hidden="true" tabindex="-1"></a><span class="fu">rm</span>(tmp_A_nrow, tmp_B_nrow, tmp_C_nrow, tmp_D_nrow, tmp_A, tmp_B, tmp_C, tmp_D)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The figure below well illustrates what’s going on in <code>ddply</code>. The codes showed were functions from the package <code>dplyr</code> that does the same thing that is best used with pipes <code>%>%</code>, but since we are not using pipes, we will stick with the functions in <code>plyr</code>, which could be less efficient in terms of speed when handling large data sets.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://swcarpentry.github.io/r-novice-gapminder/fig/13-dplyr-fig3.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">An illustration of looping through a dataframe by group (the column a in this example).</figcaption><p></p>
</figure>
</div>
<p>Looping (or aggregating) a dataframe is most powerful when it’s combined with custom functions. For example, we can randomly extract a number from each group based on V2.</p>
<p>In this case we can use <code>function(i){…}</code> in the third argument. In the first iteration, <code>i</code> equals to <code>tmp_A_nrow</code>, and in the second iteration, <code>i</code> equals to <code>tmp_B_nrow.</code></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>) <span class="co"># Again to ensure that we have the same numbers.</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ddply</span>(test_df, <span class="st">"Group"</span>, <span class="cf">function</span>(i){</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample</span>(i<span class="sc">$</span>V2, <span class="dv">1</span>)</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group V1
1 A 10
2 B 100
3 C 30
4 D 80</code></pre>
</div>
</div>
<p>If we want use a custom function across row, we can use <code>adply(dataframe, .margins = 1, .fun)</code></p>
<p>For example, we can get 10 random numbers between V1 and V2 in each row:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="fu">adply</span>(test_df, <span class="dv">1</span>, <span class="cf">function</span>(x){ <span class="co"># You could use something other than i here!</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample</span>(x<span class="sc">$</span>V1<span class="sc">:</span>x<span class="sc">$</span>V2, <span class="dv">10</span>) <span class="co"># It just needs to be consistant here.</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 A 158 708 328 366 538 567 650 530 240 560
2 B 186 350 89 643 575 195 298 79 354 21
3 C 88 92 110 102 32 86 43 60 90 35
4 D 394 556 683 733 553 892 496 121 541 651
5 A 350 496 380 465 108 488 304 280 317 139
6 B 368 146 217 61 60 369 141 459 176 255
7 C 124 211 277 190 216 197 271 159 150 79
8 D 115 163 223 238 226 122 240 169 126 198
9 A 492 124 128 349 481 121 163 360 403 489
10 B 128 137 154 178 187 145 129 162 173 130
11 C 279 156 138 260 269 284 127 268 225 218
12 D 435 555 597 569 520 192 272 314 482 365</code></pre>
</div>
</div>
<p>We can even create dataframes with <code>function</code></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ddply</span>(test_df, <span class="st">"Group"</span>, <span class="cf">function</span>(i){</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">data.frame</span>(<span class="at">N =</span> <span class="fu">nrow</span>(i), </span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> <span class="at">max =</span> <span class="fu">max</span>(i<span class="sc">$</span>V1), </span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a> <span class="at">min =</span> <span class="fu">min</span>(i<span class="sc">$</span>V1), </span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a> <span class="at">sd =</span> <span class="fu">sd</span>(i<span class="sc">$</span>V1), </span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a> <span class="at">se =</span> <span class="fu">sd</span>(i<span class="sc">$</span>V1)<span class="sc">/</span> <span class="fu">sqrt</span>(<span class="fu">nrow</span>(i)) )</span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group N max min sd se
1 A 3 978 509 240.5043 138.85524
2 B 3 679 187 246.9764 142.59188
3 C 3 307 129 100.5385 58.04596
4 D 3 930 270 330.0045 190.52821</code></pre>
</div>
</div>
<p>The code above is very similar to the function behind <code>summary_SE()</code> in the package <code>Rmisc</code> that we have seen previously.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summarySE</span>(<span class="at">data =</span> test_df, <span class="at">measurevar =</span> <span class="st">"V1"</span>, <span class="at">groupvars =</span> <span class="st">"Group"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Group N V1 sd se ci
1 A 3 774.3333 240.5043 138.85524 597.4459
2 B 3 445.6667 246.9764 142.59188 613.5233
3 C 3 245.0000 100.5385 58.04596 249.7516
4 D 3 599.0000 330.0045 190.52821 819.7767</code></pre>
</div>
</div>
<p>Here are some more tutorials about <a href="https://intro2r.com/loops.html">loops in base R</a> and the <a href="https://www.stat.ubc.ca/~jenny/STAT545A/block04_dataAggregation.html">use of ddply and other functions in plyr</a>.</p>
<hr>
</section>
<section id="give-it-a-try" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Give it a try!</h1>
<p>Write a <code>ddply</code> command to get the mean of <code>V2</code> by <code>Group</code>.</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ddply</span>(test_df, <span class="st">"Group"</span>, <span class="cf">function</span>(x){</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mean</span>(x<span class="sc">$</span>V2)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a><span class="do">## Group V1</span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="do">## 1 A 50</span></span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a><span class="do">## 2 B 60</span></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a><span class="do">## 3 C 70</span></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a><span class="do">## 4 D 80</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>Write a <code>ddply</code> command to get, by <code>Group</code>, the larger value of the sd of V2 and sd of <code>V1</code></p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ddply</span>(test_df, <span class="st">"Group"</span>, <span class="cf">function</span>(x){</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">max</span>(<span class="fu">sd</span>(x<span class="sc">$</span>V2), <span class="fu">sd</span>(x<span class="sc">$</span>V1))</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="do">## Group V1</span></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a><span class="do">## 1 A 240.5043</span></span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a><span class="do">## 2 B 246.9764</span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a><span class="do">## 3 C 100.5385</span></span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a><span class="do">## 4 D 330.0045</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<hr>
<p><img src="https://www.publicdomainpictures.net/pictures/410000/nahled/image-16274647219Zs.png" class="img-fluid" alt="Well done!" width="144"></p>
<p>Now we will go back to the previous variant data from the <em>E. coli</em> evolution experiment to illustrate the use of loops in real analysis. We will load and prepare some raw data and then run a few analysis.</p>
<hr>
</section>
<section id="load-prepare-data" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Load & prepare data</h1>
<p>We may not need to run this if you still have <code>SNP</code> in your workspace from last time.</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co"># reload all data needed here</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>variants <span class="ot">=</span> <span class="fu">read.csv</span>(<span class="fu">url</span>(<span class="st">"https://raw.githubusercontent.com/datacarpentry/genomics-r-intro/main/data/combined_tidy_vcf.csv"</span>))</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>SNP <span class="ot">=</span> <span class="fu">filter</span>(variants, REF<span class="sc">==</span><span class="st">"A"</span> <span class="sc">|</span> REF<span class="sc">==</span><span class="st">"T"</span> <span class="sc">|</span> REF<span class="sc">==</span><span class="st">"G"</span> <span class="sc">|</span> REF<span class="sc">==</span><span class="st">"C"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>Load the <em>E. coli</em> annotation. This is the .gff file where the raw sequences from each samples were aligned against to generate the .vcf files we used last time.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>Ecoli_gff <span class="ot">=</span> <span class="fu">read.delim</span>(<span class="fu">url</span>(<span class="st">"https://github.com/barricklab/LTEE-Ecoli/raw/master/reference/REL606.gff3"</span>) , <span class="at">header=</span>F, <span class="at">comment.char=</span><span class="st">"#"</span>, <span class="at">col.names =</span> <span class="fu">c</span>(<span class="st">"seqid"</span>, <span class="st">"source"</span>, <span class="st">"type"</span>, <span class="st">"start"</span> , <span class="st">"end"</span>, <span class="st">"score"</span>, <span class="st">"strand"</span>, <span class="st">"phase"</span>, <span class="st">"attributes"</span>)) <span class="sc">%>%</span> <span class="fu">filter</span>(seqid<span class="sc">==</span><span class="st">"REL606"</span>) <span class="co"># the last part filter for just the seqid we want here.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Write a code to examine the structure of the dataframe <code>Ecoli_gff</code>:</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(Ecoli_gff)</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="do">## 'data.frame': 8894 obs. of 9 variables:</span></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="do">## $ seqid : chr "REL606" "REL606" "REL606" "REL606" ...</span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a><span class="do">## $ source : chr "." "." "." "." ...</span></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a><span class="do">## $ type : chr "region" "source" "gene" "CDS" ...</span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a><span class="do">## $ start : int 1 1 190 190 336 336 2800 2800 3733 3733 ...</span></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="do">## $ end : int 4629812 4629812 255 255 2798 2798 3732 3732 5019 5019 ...</span></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a><span class="do">## $ score : chr "." "." "." "." ...</span></span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a><span class="do">## $ strand : chr "+" "+" "+" "+" ...</span></span>
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true" tabindex="-1"></a><span class="do">## $ phase : chr "." "0" "0" "0" ...</span></span>
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true" tabindex="-1"></a><span class="do">## $ attributes: chr "Is_circular=true;Note=Escherichia coli strain REL606." "Name=unknown" "Alias=ECB_00001;ID=ECB_00001;Name=thrL" "Alias=ECB_00001;ID=ECB_00001;Name=thrL;Note=thr operon leader peptide;transl_table=11" ...</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>Write a code to show the unique entries for the column <code>type:</code></p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">unique</span>(Ecoli_gff<span class="sc">$</span>type)</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] "region" "source" "gene" "CDS" </span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="do">## [5] "repeat_region" "fCDS" "rRNA" "tRNA" </span></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="do">## [9] "misc_feature"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>While there are many types of genomic elements, we are only interested in <code>gene</code> in <code>type</code>.</p>
<p>Write a code to make a new object called <code>Ecoli_gene</code> that has only the entry <code>gene</code> in the column <code>type</code>.</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>Ecoli_gene <span class="ot">=</span> <span class="fu">filter</span>(Ecoli_gff, type <span class="sc">==</span> <span class="st">"gene"</span>)</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="co"># check</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="fu">unique</span>(Ecoli_gene<span class="sc">$</span>type)</span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] "gene"</span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a><span class="co"># this should be true</span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a><span class="fu">nrow</span>(Ecoli_gene)<span class="sc">==</span><span class="dv">4386</span></span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] TRUE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>We will need the length of each gene for later analysis. Write a code to overwrite <code>Ecoli_gene</code> with an additional column called <code>length</code> calculated as <code>abs(start - end)+1</code></p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>Ecoli_gene <span class="ot">=</span> <span class="fu">mutate</span>(Ecoli_gene, <span class="at">length =</span> <span class="fu">abs</span>(start <span class="sc">-</span> end)<span class="sc">+</span><span class="dv">1</span>)</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(Ecoli_gene)</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] "seqid" "source" "type" "start" "end" </span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="do">## [6] "score" "strand" "phase" "attributes" "length"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>Plot a histogram of <code>length</code> using <code>geom_histogram()</code>. It’s clear the the lengths of the genes are not equal. This will be important to our anlaysis below.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(Ecoli_gene, <span class="fu">aes</span>(length)) <span class="sc">+</span> </span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>() <span class="sc">+</span> </span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Gene length"</span>, <span class="at">y =</span> <span class="st">"Count"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="R_loops_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<hr>
</section>
<section id="analysis" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Analysis</h1>
<section id="question-1-are-snp-positions-equally-distributed-across-genes" class="level2" data-number="5.1">
<h2 data-number="5.1" class="anchored" data-anchor-id="question-1-are-snp-positions-equally-distributed-across-genes"><span class="header-section-number">5.1</span> Question 1: Are SNP positions equally distributed across genes?</h2>
<p>To answer this question, we will need the position of a SNP and where this SNP is located relative to the length of the gene. In other words, we will need a relative position that ranges from 0-1: values closer to 0 means that the SNP is located towards the beginning of the gene, while values closer to 1 means that the SNP is located towards the end of the gene. This way, we can compare the position of a SNP across genes of different lengths.</p>
<hr>
<p>Remember from last time, we loaded a vcf file which had three samples. And we extracted only SNP variants as an object <code>SNP</code>.</p>
<p>Remind ourselves what <code>SNP</code> looks like as a .vcf file:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(SNP)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> sample_id CHROM POS ID REF ALT QUAL FILTER INDEL IDV IMF DP
1 SRR2584863 CP000819.1 9972 NA T G 91 NA FALSE NA NA 4
2 SRR2584863 CP000819.1 263235 NA G T 85 NA FALSE NA NA 6
3 SRR2584863 CP000819.1 281923 NA G T 217 NA FALSE NA NA 10
4 SRR2584863 CP000819.1 648692 NA C T 210 NA FALSE NA NA 10
5 SRR2584863 CP000819.1 1331794 NA C A 178 NA FALSE NA NA 8
6 SRR2584863 CP000819.1 1733343 NA G A 225 NA FALSE NA NA 11
VDB RPB MQB BQB MQSB SGB MQ0F ICB HOB AC AN DP4 MQ
1 0.0257451 NA NA NA NA -0.556411 0.000000 NA NA 1 1 0,0,0,4 60
2 0.0961330 1 1 1 NA -0.590765 0.166667 NA NA 1 1 0,1,0,5 33
3 0.7740830 NA NA NA 0.974597 -0.662043 0.000000 NA NA 1 1 0,0,4,5 60
4 0.2680140 NA NA NA 0.916482 -0.670168 0.000000 NA NA 1 1 0,0,7,3 60
5 0.6240780 NA NA NA 0.900802 -0.651104 0.000000 NA NA 1 1 0,0,3,5 60
6 0.9924030 NA NA NA 1.007750 -0.670168 0.000000 NA NA 1 1 0,0,4,6 60
Indiv gt_PL
1 /home/dcuser/dc_workshop/results/bam/SRR2584863.aligned.sorted.bam 121,0
2 /home/dcuser/dc_workshop/results/bam/SRR2584863.aligned.sorted.bam 112,0
3 /home/dcuser/dc_workshop/results/bam/SRR2584863.aligned.sorted.bam 247,0
4 /home/dcuser/dc_workshop/results/bam/SRR2584863.aligned.sorted.bam 240,0
5 /home/dcuser/dc_workshop/results/bam/SRR2584863.aligned.sorted.bam 208,0
6 /home/dcuser/dc_workshop/results/bam/SRR2584863.aligned.sorted.bam 255,0
gt_GT gt_GT_alleles
1 1 G
2 1 T
3 1 T
4 1 T
5 1 A
6 1 A</code></pre>
</div>
</div>
<p>The vcf file has the position <code>POS</code> of each SNP, but it doesn’t have information on the length of the gene.</p>
<p>The length of each gene was calculated in <code>Ecoli_gene</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(Ecoli_gene)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> seqid source type start end score strand phase
1 REL606 . gene 190 255 . + 0
2 REL606 . gene 336 2798 . + 0
3 REL606 . gene 2800 3732 . + 0
4 REL606 . gene 3733 5019 . + 0
5 REL606 . gene 5232 5528 . + 0
6 REL606 . gene 5681 6457 . - 0
attributes length
1 Alias=ECB_00001;ID=ECB_00001;Name=thrL 66
2 Alias=ECB_00002;ID=ECB_00002;Name=thrA 2463
3 Alias=ECB_00003;ID=ECB_00003;Name=thrB 933
4 Alias=ECB_00004;ID=ECB_00004;Name=thrC 1287
5 Alias=ECB_00005;ID=ECB_00005;Name=yaaX 297
6 Alias=ECB_00006;ID=ECB_00006;Name=yaaA 777</code></pre>
</div>
</div>
<p>However, there are no common columns between <code>SNP_SRR2584866</code> and <code>Ecoli_gene</code>. So, we’ll need to be creative to map the two based on the start/end position in <code>Ecoli_gene</code></p>
<p>To do that, we will use <code>Ecoli_gene</code> to create a temporary object that has all possible positions in the genome (i.e., from 1 to <code>max(Ecoli_gene$end)</code>). In each row, we’ll include the start position and the length of the gene that is at that <code>POS</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Use adply to apply a function to each row of a dataframe.</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a>gene_position <span class="ot">=</span> <span class="fu">adply</span>(Ecoli_gene, <span class="dv">1</span>, <span class="co"># apply by row</span></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a> <span class="at">.expand =</span> <span class="cn">FALSE</span>,</span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># write a custom function that makes a df </span></span>
<span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">function</span>(i){</span>
<span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">data.frame</span>(<span class="at">POS =</span> i<span class="sc">$</span>start<span class="sc">:</span>i<span class="sc">$</span>end, </span>
<span id="cb33-8"><a href="#cb33-8" aria-hidden="true" tabindex="-1"></a> <span class="at">start =</span> i<span class="sc">$</span>start,</span>
<span id="cb33-9"><a href="#cb33-9" aria-hidden="true" tabindex="-1"></a> <span class="at">length =</span> i<span class="sc">$</span>length)</span>
<span id="cb33-10"><a href="#cb33-10" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb33-11"><a href="#cb33-11" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(gene_position)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> X1 POS start length
1 1 190 190 66
2 1 191 190 66
3 1 192 190 66
4 1 193 190 66
5 1 194 190 66
6 1 195 190 66</code></pre>
</div>
</div>
<p>Now we have <code>POS</code> in both <code>gene_position</code> and <code>SNP</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="co"># checking</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="fu">nrow</span>(gene_position)</span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] 4128856</span></span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(gene_position)</span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] "X1" "POS" "start" "length"</span></span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a><span class="fu">nrow</span>(SNP)</span>
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] 702</span></span>
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(SNP)</span>
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] "sample_id" "CHROM" "POS" "ID" </span></span>
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a><span class="do">## [5] "REF" "ALT" "QUAL" "FILTER" </span></span>
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a><span class="do">## [9] "INDEL" "IDV" "IMF" "DP" </span></span>
<span id="cb35-12"><a href="#cb35-12" aria-hidden="true" tabindex="-1"></a><span class="do">## [13] "VDB" "RPB" "MQB" "BQB" </span></span>
<span id="cb35-13"><a href="#cb35-13" aria-hidden="true" tabindex="-1"></a><span class="do">## [17] "MQSB" "SGB" "MQ0F" "ICB" </span></span>
<span id="cb35-14"><a href="#cb35-14" aria-hidden="true" tabindex="-1"></a><span class="do">## [21] "HOB" "AC" "AN" "DP4" </span></span>
<span id="cb35-15"><a href="#cb35-15" aria-hidden="true" tabindex="-1"></a><span class="do">## [25] "MQ" "Indiv" "gt_PL" "gt_GT" </span></span>
<span id="cb35-16"><a href="#cb35-16" aria-hidden="true" tabindex="-1"></a><span class="do">## [29] "gt_GT_alleles"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We only want to keep rows in <code>SNP</code>. that shares with <code>gene_position</code>.</p>
<p>How should we set up <code>left_join</code> to do this?</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>SNP_gene <span class="ot">=</span> <span class="fu">left_join</span>(SNP, gene_position, <span class="st">"POS"</span>) <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(length)) <span class="co"># remove NA in length</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>We will now make a new column called <code>rel_POS</code> that is calculated as <code>(POS-start+1)/length</code>. This is the ratio of the length to the SNP relative to the length of the gene.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>SNP_gene <span class="ot">=</span> <span class="fu">mutate</span>(SNP_gene, <span class="at">rel_POS =</span> (POS<span class="sc">-</span>start<span class="sc">+</span><span class="dv">1</span>)<span class="sc">/</span>length, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We can now use a 1-sample t test to test whether the distribution of <code>rel_POS</code> is deviated from a mean of 0.5 (mu = 0.5).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="fu">t.test</span>(SNP_gene<span class="sc">$</span>rel_POS, <span class="at">mu =</span> <span class="fl">0.5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
One Sample t-test
data: SNP_gene$rel_POS
t = 0.37023, df = 619, p-value = 0.7113
alternative hypothesis: true mean is not equal to 0.5
95 percent confidence interval:
0.4817635 0.5267102
sample estimates:
mean of x
0.5042369 </code></pre>
</div>
</div>
<p>Remember that the null in this t.test is that the mean of the given data is 0.5. Now that the p value is > 0.05, we have failed to reject the null. This means that the mean of rel_POS is not statistically different from 0.5. We can write a summary statement with statistical support as:</p>
<p><strong>The mean of the relative position of SNPs across genes is not statistically different from 0.5 (t = 0.370, d.f. = 619, p = 0.711). This suggests that SNPs are equally distributed across genes.</strong></p>
<p>Note that there are better ways to test this, for example by testing whether the distribution fits a normal distribution: <code>shapiro.test(SNP_gene$rel_POS)</code>, in which the null hypothesis is that the data fits the normal distribution. The data likely will not fit because of kurtosis instead of skewness. See <a href="https://www.statisticalaid.com/skewness-and-kurtosis-in-statistics-shape-of-distributions/">here</a>.</p>
<p>We can visualize the data using a histogram:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(SNP_gene, <span class="fu">aes</span>(rel_POS)) <span class="sc">+</span> </span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>() </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="R_loops_files/figure-html/unnamed-chunk-27-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>We can also show it as a boxplot:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="co"># modification is needed as we only have one variable.</span></span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(SNP_gene, <span class="fu">aes</span>(<span class="at">y =</span> rel_POS, <span class="at">x =</span> <span class="st">""</span>)) <span class="sc">+</span> </span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">outlier.shape =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># to make sure outlier are not confused with the points.</span></span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_jitter</span>(<span class="at">color=</span><span class="st">"grey"</span>, <span class="at">height =</span> <span class="dv">0</span>) <span class="sc">+</span> <span class="co"># hieght ensures that on the y is jittered</span></span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">"SNP position relative to gene length"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="R_loops_files/figure-html/unnamed-chunk-28-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<hr>
</section>
<section id="question-2-does-sample-affect-the-distribution-of-snps" class="level2" data-number="5.2">
<h2 data-number="5.2" class="anchored" data-anchor-id="question-2-does-sample-affect-the-distribution-of-snps"><span class="header-section-number">5.2</span> Question 2: Does sample affect the distribution of SNPs?</h2>
<p>We can answer this easily as <code>SNP</code> includes three <code>sample_id</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="fu">unique</span>(SNP<span class="sc">$</span>sample_id)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "SRR2584863" "SRR2584866" "SRR2589044"</code></pre>
</div>
</div>
<p>Write a code to run an ANOVA with <code>rel_POS</code> as the response and <code>sample_id</code> as predictor:</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(<span class="fu">lm</span>(rel_POS<span class="sc">~</span>sample_id, SNP_gene))</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a><span class="do">## Call:</span></span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a><span class="do">## lm(formula = rel_POS ~ sample_id, data = SNP_gene)</span></span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a><span class="do">## Residuals:</span></span>
<span id="cb45-7"><a href="#cb45-7" aria-hidden="true" tabindex="-1"></a><span class="do">## Min 1Q Median 3Q Max </span></span>
<span id="cb45-8"><a href="#cb45-8" aria-hidden="true" tabindex="-1"></a><span class="do">## -0.50586 -0.23886 -0.00494 0.25357 0.49194 </span></span>
<span id="cb45-9"><a href="#cb45-9" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb45-10"><a href="#cb45-10" aria-hidden="true" tabindex="-1"></a><span class="do">## Coefficients:</span></span>
<span id="cb45-11"><a href="#cb45-11" aria-hidden="true" tabindex="-1"></a><span class="do">## Estimate Std. Error t value Pr(>|t|) </span></span>
<span id="cb45-12"><a href="#cb45-12" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept) 0.445958 0.067209 6.635 7.09e-11 ***</span></span>
<span id="cb45-13"><a href="#cb45-13" aria-hidden="true" tabindex="-1"></a><span class="do">## sample_idSRR2584866 0.060788 0.068218 0.891 0.373 </span></span>
<span id="cb45-14"><a href="#cb45-14" aria-hidden="true" tabindex="-1"></a><span class="do">## sample_idSRR2589044 -0.005164 0.127014 -0.041 0.968 </span></span>
<span id="cb45-15"><a href="#cb45-15" aria-hidden="true" tabindex="-1"></a><span class="do">## ---</span></span>
<span id="cb45-16"><a href="#cb45-16" aria-hidden="true" tabindex="-1"></a><span class="do">## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span>
<span id="cb45-17"><a href="#cb45-17" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb45-18"><a href="#cb45-18" aria-hidden="true" tabindex="-1"></a><span class="do">## Residual standard error: 0.2851 on 617 degrees of freedom</span></span>
<span id="cb45-19"><a href="#cb45-19" aria-hidden="true" tabindex="-1"></a><span class="do">## Multiple R-squared: 0.001852, Adjusted R-squared: -0.001384 </span></span>
<span id="cb45-20"><a href="#cb45-20" aria-hidden="true" tabindex="-1"></a><span class="do">## F-statistic: 0.5723 on 2 and 617 DF, p-value: 0.5646</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>Write some codes to plot the data as a boxplot:</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(SNP_gene, <span class="fu">aes</span>(<span class="at">y =</span> rel_POS, <span class="at">x =</span> sample_id)) <span class="sc">+</span> </span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span> </span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_jitter</span>(<span class="at">color=</span><span class="st">"grey"</span>, <span class="at">height =</span> <span class="dv">0</span>) <span class="sc">+</span> </span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">"SNP position relative to length"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<p><img src="R_loops_files/figure-html/unnamed-chunk-31-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Based on these data, write a summary statement with statistical support.</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="co"># This should be in your chain of thought, but it is NOT a summary statement:</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a><span class="co"># The null hypothesis for the ANOVA is that the predictor does not affect the distribution of the response.</span></span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Since the p value is larger than 0.05, we have failed to reject the null hypothesis.</span></span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Therefore, the null is true.</span></span>
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Now this IS the summary statement that you should report in a report:</span></span>
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample ID does not significantly affect the relative position of SNPs across genes (F = 0.572, d.f. = 2 and 617, p= 0.565).</span></span>
<span id="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a><span class="co"># (generally only 3 sigificant figures are needed)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<hr>
</section>
</section>
<section id="assignment" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Assignment</h1>
<p>Apart from the mean, we can also test whether the variability (standard deviation) of <code>rel_POS</code> are different between <code>sample_id</code>.</p>
<p>(1) From <code>SNP_gene</code>, use <code>ddply</code> to get the sd of <code>rel_POS</code> by <code>sample_id</code>. Save the new dataframe as <code>sd_rel_POS</code></p>
<div class="cell">
</div>
<p>(2) Run a chi-square test to test if the calculated sd (should be called “V1” in <code>sd_rel_POS</code> are 1:1:1 across sample_id (i.e., the default). <strong>Write a summary statement with statistic support.</strong></p>
<div class="cell">
</div>
<p>(3) Plot the data</p>
<div class="cell">
</div>
<p><strong>Submission: Once you are done writing your well-annotated code, run everything and copy the code + output in the R console and submit. Plots do not need to be attached.</strong></p>
<hr>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/c/cd/The-end-folks.jpg" class="img-fluid" alt="The end"></p>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>