-
Notifications
You must be signed in to change notification settings - Fork 51
/
analysis_of_genome.html
1175 lines (1101 loc) · 68 KB
/
analysis_of_genome.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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Analysis of genome data</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/sandstone.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/navigation-1.1/tabsets.js"></script>
<script src="site_libs/accessible-code-block-0.0.1/empty-anchor.js"></script>
<link href="site_libs/anchor-sections-1.0/anchor-sections.css" rel="stylesheet" />
<script src="site_libs/anchor-sections-1.0/anchor-sections.js"></script>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-107144798-3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'UA-107144798-3');
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<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;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ background-color: #f8f8f8; }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* 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="styles.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: 61px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 66px;
margin-top: -66px;
}
.section h2 {
padding-top: 66px;
margin-top: -66px;
}
.section h3 {
padding-top: 66px;
margin-top: -66px;
}
.section h4 {
padding-top: 66px;
margin-top: -66px;
}
.section h5 {
padding-top: 66px;
margin-top: -66px;
}
.section h6 {
padding-top: 66px;
margin-top: -66px;
}
.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 -->
</head>
<body>
<div class="container-fluid main-container">
<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">Population genetics and genomics in R</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="TOC.html">Table of contents</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Part I
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Introduction.html">Introduction</a>
</li>
<li>
<a href="Getting_ready_to_use_R.html">Getting ready to use R</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Part II
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Data_Preparation.html">Data preparation</a>
</li>
<li>
<a href="First_Steps.html">First steps</a>
</li>
<li>
<a href="Population_Strata.html">Population strata and clone correction</a>
</li>
<li>
<a href="Locus_Stats.html">Locus-based statistics and missing data</a>
</li>
<li>
<a href="Genotypic_EvenRichDiv.html">Genotypic evenness, richness, and diversity</a>
</li>
<li>
<a href="Linkage_disequilibrium.html">Linkage disequilibrium</a>
</li>
<li>
<a href="Pop_Structure.html">Population structure</a>
</li>
<li>
<a href="Minimum_Spanning_Networks.html">Minimum Spanning Networks</a>
</li>
<li>
<a href="AMOVA.html">AMOVA</a>
</li>
<li>
<a href="DAPC.html">Discriminant analysis of principal components (DAPC)</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Part III
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="intro_vcf.html">Population genomics and HTS</a>
</li>
<li>
<a href="reading_vcf.html">Reading VCF data</a>
</li>
<li>
<a href="analysis_of_genome.html">Analysis of genomic data</a>
</li>
<li>
<a href="gbs_analysis.html">Analysis of GBS data</a>
</li>
<li>
<a href="clustering_plot.html">Clustering plot</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Workshops
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-submenu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">ICPP</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="workshop_icpp.html">Preparation</a>
</li>
<li>
<a href="intro_vcf.html">Introduction</a>
</li>
<li>
<a href="reading_vcf.html">VCF data</a>
</li>
<li>
<a href="quality_control.html">Quality control</a>
</li>
<li>
<a href="gbs_analysis.html">Analysis of GBS data</a>
</li>
<li>
<a href="analysis_of_genome.html">Analysis of genome data</a>
</li>
</ul>
</li>
<li class="dropdown-submenu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">APS Southern Division</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="workshop_southernAPS.html">Preparation</a>
</li>
<li>
<a href="intro_vcf.html">Introduction</a>
</li>
<li>
<a href="reading_vcf.html">VCF data</a>
</li>
<li>
<a href="quality_control.html">Quality control</a>
</li>
<li>
<a href="gbs_analysis.html">Analysis of GBS data</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
About
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Authors.html">Authors</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Appendices
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="intro_to_R.html">Introduction to R</a>
</li>
<li>
<a href="Data_sets.html">Data sets</a>
</li>
<li>
<a href="funpendix.html">Function glossary</a>
</li>
<li>
<a href="background_functions.html">Background_functions</a>
</li>
<li>
<a href="https://github.com/grunwaldlab/Population_Genetics_in_R/">Source Code</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Analysis of genome data</h1>
<h3 class="subtitle"><em>BJ Knaus, JF Tabima, and NJ Grünwald</em></h3>
</div>
<div id="TOC">
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#opening-and-examining-the-dataset">Opening and examining the dataset</a></li>
<li><a href="#converting-vcf-data-to-a-genlight-object">Converting VCF data to a genlight object</a></li>
<li><a href="#chromr-objects">chromR objects</a></li>
<li><a href="#genetic-differentiation">Genetic differentiation</a></li>
<li><a href="#exercises">Exercises</a></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>Analysis of genome data for populations can be seen as similar to the analyses of other marker systems discussed in previous chapters of this book, except that genome data analyses include larger quantities of data. For example, VCF data (discussed in ‘<a href="reading_vcf.html">reading VCF data</a>’) can be read into R using <em>vcfR</em> <span class="citation">(Knaus & Grünwald, 2017)</span> to create a <em>vcfR</em> object. This object can be converted into a genlight object <span class="citation">(Jombart, 2008)</span> and then a snpclone object <span class="citation">(Kamvar, Tabima & Grünwald, 2014; Kamvar, Brooks & Grünwald, 2015)</span> if deemed necessary. Analysis on these objects has been covered in previous sections. Genome scale data provides additional analytical options as well. For example, when assumptions about the neutrality of the majority of the genome are appropriate, this can be used as a null hypothesis and used to help identify markers that differentiate from this assumption. Here we’ll provide examples of how genomic data may be analyzed.</p>
<p>For genomics examples we’ll use the pinfsc50 dataset. The <a href="https://CRAN.R-project.org/package=pinfsc50">pinfsc50</a> dataset is from a number of published <em>P. infestans</em> genomics projects where the data has been subset here to supercontig_1.50. This dataset is available as a stand alone R package <span class="citation">(Knaus & Grünwald, 2017)</span>. By subsetting the data to one supercontig it creates a dataset of a size that can be conveniently used for examples. This dataset illustrates some important strengths and weaknesses of these studies. A strength is the amount of data we have for each individual. Among the weaknesses are that the samples are ‘opportunistic’ in that we have no control over the design of the experiment. Also, because of the large investment in data per sample, there is a relatively small number of samples.</p>
</div>
<div id="opening-and-examining-the-dataset" class="section level2">
<h2>Opening and examining the dataset</h2>
<p>We’ll read our VCF data into R using the function <code>read.vcfR()</code>. This is data from the pinfsc50 data set that we filtered for quality in the section <a href="reading_vcf.html">reading VCF data</a>. Once the file is read in we can validate its contents using the <code>show</code> method which is implemented by executing the object’s name at the prompt.</p>
<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>(<span class="st">'vcfR'</span>)</span></code></pre></div>
<pre><code>##
## ***** *** vcfR *** *****
## This is vcfR 1.12.0
## browseVignettes('vcfR') # Documentation
## citation('vcfR') # Citation
## ***** ***** ***** *****</code></pre>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1"></a>vcf <-<span class="st"> </span><span class="kw">read.vcfR</span>(<span class="st">"pinfsc50_filtered.vcf.gz"</span>)</span></code></pre></div>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1"></a>vcf</span></code></pre></div>
<pre class="r-output"><code>## ***** Object of Class vcfR *****
## 18 samples
## 1 CHROMs
## 2,190 variants
## Object size: 2.9 Mb
## 0 percent missing data
## ***** ***** *****
</code></pre>
<p>The <code>show</code> method reports that we have 18 samples and 2,190 variants. If this matches our expectation then we can proceed.</p>
</div>
<div id="converting-vcf-data-to-a-genlight-object" class="section level2">
<h2>Converting VCF data to a genlight object</h2>
<p>Different R packages have created different data structures to hold your data when it is imported into R. This is analagous to the different file formats you may have used to analyze your data in software outside of R. We’ve tried to engineer a suite of functions to convert data structures among the various R packages we typically use. The R package adegenet is a popular R package used for population genetic analysis and it works on data structures called ‘genlight’ objects. Here we use the function <code>vcfR2genlight()</code> to convert our vcfR object to a genlight object. This makes our VCF data available to the analyses in adegenet.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1"></a>x <-<span class="st"> </span><span class="kw">vcfR2genlight</span>(vcf)</span></code></pre></div>
<pre><code>## Warning in vcfR2genlight(vcf): Found 44 loci with more than two alleles.
## Objects of class genlight only support loci with two alleles.
## 44 loci will be omitted from the genlight object.</code></pre>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1"></a>x</span></code></pre></div>
<pre class="r-output"><code>## /// GENLIGHT OBJECT /////////
##
## // 18 genotypes, 2,146 binary SNPs, size: 240.4 Kb
## 0 (0 %) missing data
##
## // Basic content
## @gen: list of 18 SNPbin
##
## // Optional content
## @ind.names: 18 individual labels
## @loc.names: 2146 locus labels
## @chromosome: factor storing chromosomes of the SNPs
## @position: integer storing positions of the SNPs
## @other: a list containing: elements without names
</code></pre>
<p>A genlight object only supports biallelic, or binary, variants. That is, variants with no more than two alleles. However, variant call format data can include multiple alleles. When we created our genlight object we recieved a warning message indicating that our vcfR object had variants with more than two alleles and that it was being subset to only biallelic variants. This is one of several important differences in how data is handled in VCF data versus genlight objects.</p>
<p>Another important difference among VCF and genlight data is how the genotypes are stored. In VCF data the alleles are delimited by either a pipe or a forward slash (‘|’, ‘/’ respectively). Because genlight objects only use biallelic loci the genotypes can be recoded as 0, 1 and 2. These correspond to homozygous for the reference or zero allele, heterozygote or homozygous for the first alternate allele. We can validate this by checking a few select genotypes from both the vcfR object and the genlight object.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1"></a><span class="co"># vcfR</span></span>
<span id="cb8-2"><a href="#cb8-2"></a>gt <-<span class="st"> </span><span class="kw">extract.gt</span>(vcf, <span class="dt">element =</span> <span class="st">"GT"</span>)</span>
<span id="cb8-3"><a href="#cb8-3"></a>gt[<span class="kw">c</span>(<span class="dv">2</span>,<span class="dv">6</span>,<span class="dv">18</span>), <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>]</span></code></pre></div>
<pre class="r-output"><code>## BL2009P4_us23 DDR7602 IN2009T1_us22
## Supercontig_1.50_80063 "1|0" "1|0" "0|1"
## Supercontig_1.50_80089 "0|0" "1|0" "0|1"
## Supercontig_1.50_94108 "0|1" "0|1" "1|1"
</code></pre>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1"></a><span class="co"># genlight</span></span>
<span id="cb9-2"><a href="#cb9-2"></a><span class="kw">t</span>(<span class="kw">as.matrix</span>(x))[<span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">5</span>,<span class="dv">17</span>), <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>]</span></code></pre></div>
<pre class="r-output"><code>## BL2009P4_us23 DDR7602 IN2009T1_us22
## Supercontig_1.50_80063 1 1 1
## Supercontig_1.50_80089 0 1 1
## Supercontig_1.50_94108 1 1 2
</code></pre>
<p>Note that in VCF data the samples are in columns and the variants are in rows. In genlight objects, and many other R objects, the samples are in rows while the variants are in columns. We can use the transpose function (<code>t()</code>) to convert between these two states.</p>
<p>Yet another difference among VCF data and genlight objects is that in VCF data there is no concept of ‘population.’ The package adegenet was designed specifically for the analysis of population data, so its genlight object has a place (a ‘slot’) to hold this information. Because there is no population data in VCF data, if we want population data we’ll have to set it ourselves.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1"></a><span class="kw">library</span>(adegenet)</span>
<span id="cb10-2"><a href="#cb10-2"></a><span class="kw">pop</span>(x) <-<span class="st"> </span><span class="kw">as.factor</span>(<span class="kw">c</span>(<span class="st">"us"</span>, <span class="st">"eu"</span>, <span class="st">"us"</span>, <span class="st">"af"</span>, <span class="st">"eu"</span>, <span class="st">"us"</span>, <span class="st">"mx"</span>, <span class="st">"eu"</span>, <span class="st">"eu"</span>, <span class="st">"sa"</span>, <span class="st">"mx"</span>, <span class="st">"sa"</span>, <span class="st">"us"</span>, <span class="st">"sa"</span>, <span class="st">"Pmir"</span>, <span class="st">"us"</span>, <span class="st">"eu"</span>, <span class="st">"eu"</span>))</span>
<span id="cb10-3"><a href="#cb10-3"></a><span class="kw">popNames</span>(x)</span></code></pre></div>
<pre class="r-output"><code>## [1] "af" "eu" "mx" "Pmir" "sa" "us"
</code></pre>
<p>Our population designation consists of a vector, that is the same length as the number of samples we have, where each element indicates which population each sample belongs to. By using the <code>as.factor()</code> function we transform the “vector” into a “factor”. A factor understands that all of the elements that are named “us” or “eu” are all part of the same group. This is why when we ask for the <code>popNames</code> we get a vector where each population is represented only once.</p>
<p>Yet another difference among VCF data and genlight objects is the concept of ploidy. In VCF data each variant is treated independently. This means that in theory VCF data may contain data that is of mixed ploidy. In a genlight object different samples may be of different ploidy levels, but within each sample all of its loci must be of the same ploidy level. Here we’ll set the ploidy of all the samples in the genlight object to the same ploidy.</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="kw">ploidy</span>(x) <-<span class="st"> </span><span class="dv">2</span></span></code></pre></div>
<div id="distance-matrices" class="section level3">
<h3>Distance matrices</h3>
<p>Let’s create a pairwise genetic distance matrix for individuals or populations (i.e., groups of individuals).</p>
<p>To summarize, we can create a distance matrix from a genlight object using <code>dist()</code>:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1"></a>x.dist <-<span class="st"> </span><span class="kw">dist</span>(x)</span></code></pre></div>
<p>Note, that we have not specified what the variable x is. We can find documentation for this function with <code>?dist</code>.</p>
<p>There are also functions to create distance matrices from genlight objects that exist in other packages. The function <code>bitwise.dist()</code> in the package <em>poppr</em> is an example. We can find documentation for this function with <code>?poppr::bitwise.dist</code>. Again, you need to know where to look for this information or you may not find it. We can use this function as follows.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1"></a>x.dist <-<span class="st"> </span>poppr<span class="op">::</span><span class="kw">bitwise.dist</span>(x)</span></code></pre></div>
<p>Note, that the variable x has not yet been specified. Lastly, because you can use <code>as.matrix()</code> on your genlight object, and most distance algorithms can use this matrix as input, you can use this as an intermediate step to create a matrix from your genlight object and pass it to your distance algorithm of choice. Options include <em>ade4</em>, <code>vegdist()</code> in <em>vegan</em>, or <code>daisy()</code> in <em>cluster.</em> Note that it is up to you to determine which distance metric is best for your particular analysis. A number of options therefore exist for creating distance matrices from genlight objects.</p>
</div>
</div>
<div id="chromr-objects" class="section level2">
<h2>chromR objects</h2>
<div id="using-chromr-to-locate-unusual-features-in-a-genome" class="section level3">
<h3>Using chromR to locate unusual features in a genome</h3>
<p>Genomic projects frequently incorporate several types of data. For example, the reference sequence may be stored as a FASTA format file, variants (SNPs, indels, etc.) may be stored in a variant call format (VCF) file while annotations may be stored as a GFF or BED format (tablular data). Genome browsers can be used to integrate these different data types. However, genome browsers typically lack a manipulation environment, they simply display existing files. The R environment includes a tremendous amount of statistical support that is both specific to genetics and genomics as well as more general tools (e.g., the linear model and its extensions). The R package vcfR provides a link between VCF data and the R environment and it includes a simple genome browser to help visualize the effect of manipulations. Here we explore how we can use vcfR to survey genomic data for interesting features.</p>
</div>
<div id="creating-chromr-objects" class="section level3">
<h3>Creating chromR objects</h3>
<p>In this example we will begin by locating the example data from the pinfsc50 package. This is a separate package from vcfR that you will need to install. If you haven’t installed it already, you can install it with <code>install.packages('pinfsc50')</code>. For data from your own research activities you may wany to omit the <code>system.file()</code> steps and directly use your filenames in the input steps.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1"></a><span class="kw">library</span>(vcfR)</span>
<span id="cb14-2"><a href="#cb14-2"></a></span>
<span id="cb14-3"><a href="#cb14-3"></a><span class="co"># Find the files.</span></span>
<span id="cb14-4"><a href="#cb14-4"></a>vcf_file <-<span class="st"> </span><span class="kw">system.file</span>(<span class="st">"extdata"</span>, <span class="st">"pinf_sc50.vcf.gz"</span>, <span class="dt">package =</span> <span class="st">"pinfsc50"</span>)</span>
<span id="cb14-5"><a href="#cb14-5"></a>dna_file <-<span class="st"> </span><span class="kw">system.file</span>(<span class="st">"extdata"</span>, <span class="st">"pinf_sc50.fasta"</span>, <span class="dt">package =</span> <span class="st">"pinfsc50"</span>)</span>
<span id="cb14-6"><a href="#cb14-6"></a>gff_file <-<span class="st"> </span><span class="kw">system.file</span>(<span class="st">"extdata"</span>, <span class="st">"pinf_sc50.gff"</span>, <span class="dt">package =</span> <span class="st">"pinfsc50"</span>)</span>
<span id="cb14-7"><a href="#cb14-7"></a></span>
<span id="cb14-8"><a href="#cb14-8"></a><span class="co"># Input the files.</span></span>
<span id="cb14-9"><a href="#cb14-9"></a>vcf <-<span class="st"> </span><span class="kw">read.vcfR</span>(vcf_file, <span class="dt">verbose =</span> <span class="ot">FALSE</span>)</span>
<span id="cb14-10"><a href="#cb14-10"></a>dna <-<span class="st"> </span>ape<span class="op">::</span><span class="kw">read.dna</span>(dna_file, <span class="dt">format =</span> <span class="st">"fasta"</span>)</span>
<span id="cb14-11"><a href="#cb14-11"></a>gff <-<span class="st"> </span><span class="kw">read.table</span>(gff_file, <span class="dt">sep=</span><span class="st">"</span><span class="ch">\t</span><span class="st">"</span>, <span class="dt">quote=</span><span class="st">""</span>)</span>
<span id="cb14-12"><a href="#cb14-12"></a></span>
<span id="cb14-13"><a href="#cb14-13"></a><span class="co"># Create a chromR object.</span></span>
<span id="cb14-14"><a href="#cb14-14"></a>chrom <-<span class="st"> </span><span class="kw">create.chromR</span>(<span class="dt">name=</span><span class="st">"Supercontig"</span>, <span class="dt">vcf=</span>vcf, <span class="dt">seq=</span>dna, <span class="dt">ann=</span>gff, <span class="dt">verbose=</span><span class="ot">TRUE</span>)</span></code></pre></div>
<pre><code>## Names in vcf:</code></pre>
<pre><code>## Supercontig_1.50</code></pre>
<pre><code>## Names of sequences:</code></pre>
<pre><code>## Supercontig_1.50 of Phytophthora infestans T30-4</code></pre>
<pre><code>## Warning in create.chromR(name = "Supercontig", vcf = vcf, seq = dna, ann = gff, :
## Names in variant data and sequence data do not match perfectly.
## If you choose to proceed, we'll do our best to match the data.
## But prepare yourself for unexpected results.</code></pre>
<pre><code>## Names in annotation:</code></pre>
<pre><code>## Supercontig_1.50</code></pre>
<pre><code>## Initializing var.info slot.</code></pre>
<pre><code>## var.info slot initialized.</code></pre>
<p>Note that a warning message indicates that the names in all of the data sources do not match pefectly. It has been my experience that this is a frequent occurrence in genome projects. Instead of asking the user to create duplicate files that have the same data but standardized names, vcfR allows the user to exercise some judgement. If you see this message and feel the names are correct you can ignore this and proceed. In this case we see that a chromosome is named ‘Supercontig_1.50’ in the VCF data but named ‘Supercontig_1.50 of Phytophthora infestans T30-4’ in the FASTA (sequence) file. Because we know that for this specific project these are synonyms we can safely ignore the warning and proceed.</p>
<p>Once we have created our chromR object we can verify that its contents are what we expect. By executing the object’s name at the console, with no other arguments, we invoke the object’s ‘show’ method. The show method for chromR objects presents a summary of the object’s contents.</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1"></a>chrom</span></code></pre></div>
<pre class="r-output"><code>## ***** Class chromR, method Show *****
## Name: Supercontig
## Chromosome length: 1,042,442 bp
## Chromosome labels: Supercontig_1.50 of Phytophthora infestans T30-4
## Annotation (@ann) count: 223
## Annotation chromosome names: Supercontig_1.50
## Variant (@vcf) count: 22,031
## Variant (@vcf) chromosome names: Supercontig_1.50
## Object size: 24.1 Mb
## Use head(object) for more details.
## ***** End Show (chromR) *****
</code></pre>
<p>There at least two ways to graphically view the chromR object. The first is plot() which plots histograms of some of data summaries.</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1"></a><span class="kw">plot</span>(chrom)</span></code></pre></div>
<p><img src="analysis_of_genome_files/figure-html/plot_chrom-1.png" width="1152" style="display: block; margin: auto;" /></p>
<p>The read depth here is a sum over all samples. We see a peak that represents the depth where most of our genomes were sequenced at. Low regions of sequence depth may indicate variants where we may be concerned that there may not be enough information to call a genotype. Variants of high coverage may represent repetetive regions of genomes where the reference may not contain all the copies so the reads pile up on the fraction of repeats that were successfully assembled. These regions may violate the ploidy assumptions made by variant callers and therefore may be considered a target for quality filtering. Mapping quality is very peaked at 60 but also contains variants that deviate from this common value. Quality (QUAL) is less easily interpreted. It appears that most of our variants are of a low quality with very few of them being of high quality. It is important to remember that while everyone would like high quality, quality is frequently difficult to measure. The simplest interpretation here is that QUAL may not be a good parameter to use to judge your variants. The last panel for SNP densities is empty because this data is created during the processing of chromR objects, which we will discuss below.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1"></a><span class="kw">chromoqc</span>(chrom, <span class="dt">dp.alpha =</span> <span class="dv">66</span>)</span></code></pre></div>
<p><img src="analysis_of_genome_files/figure-html/chromoqc-1.png" width="1152" style="display: block; margin: auto;" /></p>
<p>Our second plot, called chromo plot, displays the same information as the plot method only it distributes the data along its chomosomal coordinates. It also includes a representation of the annotation data. The contents of this plot are somewhat flexible in that it depends on what data is present in the chromR object.</p>
</div>
<div id="processing-chromr-objects" class="section level3">
<h3>Processing chromR objects</h3>
<p>Creation and processing of a chromR object has been divided into separate tasks. Creation loads the data into the chromR object and should typically only be required once. Processing the chromR object generates summaries of the data. Some of these summaries will need to be updated as the chromR object is updated. For example, if the size of the sliding window used to summarize variant density and GC content is changed the chromR object will need to be processed to update this information.</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1"></a>chrom <-<span class="st"> </span><span class="kw">proc.chromR</span>(chrom, <span class="dt">verbose =</span> <span class="ot">TRUE</span>)</span></code></pre></div>
<pre><code>## Nucleotide regions complete.</code></pre>
<pre><code>## elapsed time: 0.25</code></pre>
<pre><code>## N regions complete.</code></pre>
<pre><code>## elapsed time: 0.237</code></pre>
<pre><code>## Population summary complete.</code></pre>
<pre><code>## elapsed time: 0.203</code></pre>
<pre><code>## window_init complete.</code></pre>
<pre><code>## elapsed time: 0.001</code></pre>
<pre><code>## windowize_fasta complete.</code></pre>
<pre><code>## elapsed time: 0.095</code></pre>
<pre><code>## windowize_annotations complete.</code></pre>
<pre><code>## elapsed time: 0.015</code></pre>
<pre><code>## windowize_variants complete.</code></pre>
<pre><code>## elapsed time: 0.001</code></pre>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1"></a><span class="kw">plot</span>(chrom)</span></code></pre></div>
<p><img src="analysis_of_genome_files/figure-html/unnamed-chunk-23-1.png" width="1152" style="display: block; margin: auto;" /></p>
<p>Subsequent to processing, our plot function is identical to its previous presentation except that we now have variant densities. When we observe the chromoqc plot we see that we now have variant densities, nucleotide content as well as a representation of where in our reference we have nucleotides (A, C, G or T) or where we have ambiguous nucleotides.</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1"></a><span class="kw">chromoqc</span>(chrom, <span class="dt">dp.alpha =</span> <span class="dv">66</span>)</span></code></pre></div>
<p><img src="analysis_of_genome_files/figure-html/unnamed-chunk-24-1.png" width="1152" style="display: block; margin: auto;" /></p>
<p>The above data is an example of visualizing raw data that has come from a variant caller and other automated sources. In our section on quality control we presented methods on how to filter variants on various parameters as an attempt to omit low quality variants. We can use this data to create a chromR object and compare it to the above data.</p>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1"></a><span class="co">#vcf <- read.vcfR("pinfsc50_qc.vcf.gz", verbose = FALSE)</span></span>
<span id="cb44-2"><a href="#cb44-2"></a>vcf <-<span class="st"> </span><span class="kw">read.vcfR</span>(<span class="st">"pinfsc50_filtered.vcf.gz"</span>, <span class="dt">verbose =</span> <span class="ot">FALSE</span>)</span>
<span id="cb44-3"><a href="#cb44-3"></a>chrom <-<span class="st"> </span><span class="kw">create.chromR</span>(<span class="dt">name=</span><span class="st">"Supercontig"</span>, <span class="dt">vcf=</span>vcf, <span class="dt">seq=</span>dna, <span class="dt">ann=</span>gff, <span class="dt">verbose=</span><span class="ot">FALSE</span>)</span>
<span id="cb44-4"><a href="#cb44-4"></a>chrom <-<span class="st"> </span><span class="kw">proc.chromR</span>(chrom, <span class="dt">verbose =</span> <span class="ot">FALSE</span>)</span>
<span id="cb44-5"><a href="#cb44-5"></a><span class="kw">chromoqc</span>(chrom, <span class="dt">dp.alpha =</span> <span class="dv">66</span>)</span></code></pre></div>
<p><img src="analysis_of_genome_files/figure-html/unnamed-chunk-25-1.png" width="1152" style="display: block; margin: auto;" /></p>
<p>We have a smaller quantity of data after our quality control steps. However, there do appear to be a few improvements. First, the read depth is now fairly uniform and lacks the large variation in depth we saw in the raw data. In genomics projects our naive assumption is that we would sequence all regions of the genome at the same depth. So this change in the data allows it to approach our expectation. Second, the mapping quality appear relatively constant and the variants with low mapping quality have been omitted. If we feel that ‘mapping quality’ is a reasonable assessment of quality, we may interpret this as an improvement. These are methods we feel improve the quality of our datasets prior to analysis.</p>
</div>
<div id="tabular-summaries" class="section level3">
<h3>Tabular summaries</h3>
<p>When we process a chromR object, two forms of tabular data are created. First, summaries are made on a per variant basis. This includes sample size (minus missing data), allele counts, heterozygosity and effective size. Second, summaries are made on a per window basis. Window size can be changed with the <code>win.size</code> parameter of the function <code>proc.chromR()</code>. Window based summaries include nucleotide content per window (including missing data so you can adjust window size for analyses if necessary), the number of genic sites per window (when annotation information was provided) and the number of variants per window.</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1"></a><span class="kw">head</span>(chrom<span class="op">@</span>var.info)</span></code></pre></div>
<pre class="r-output"><code>## CHROM POS MQ DP mask n Allele_counts He Ne
## 1 Supercontig_1.50 80058 58.96 508 TRUE 18 25,10,1 0.64364712 2.806207
## 2 Supercontig_1.50 80063 58.95 514 TRUE 18 25,11 0.42438272 1.737265
## 3 Supercontig_1.50 80067 58.88 499 TRUE 18 23,13 0.46141975 1.856734
## 4 Supercontig_1.50 80073 58.77 490 TRUE 18 35,1 0.05401235 1.057096
## 5 Supercontig_1.50 80074 58.75 482 TRUE 18 26,10 0.40123457 1.670103
## 6 Supercontig_1.50 80089 58.80 481 TRUE 18 25,11 0.42438272 1.737265
</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1"></a><span class="kw">head</span>(chrom<span class="op">@</span>win.info)</span></code></pre></div>
<pre class="r-output"><code>## CHROM window start end length A C G T N other genic
## 1 Supercontig_1.50 1 1 1000 1000 267 213 293 227 0 0 0
## 2 Supercontig_1.50 2 1001 2000 1000 283 206 309 202 0 0 0
## 3 Supercontig_1.50 3 2001 3000 1000 229 213 235 177 146 0 0
## 4 Supercontig_1.50 4 3001 4000 1000 0 0 0 0 1000 0 0
## 5 Supercontig_1.50 5 4001 5000 1000 0 0 0 0 1000 0 0
## 6 Supercontig_1.50 6 5001 6000 1000 0 0 0 0 1000 0 0
## variants
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
</code></pre>
<p>While loading entire genomes into memory may not be practical due to resource limitations, it is frequently practical to break a genome up into fractions that can be processed given the resources available on any system. By processing a genome by chromosomes, or some other fraction, and saving this tabular data to file you can perform genome scans in an attempt to identify interesting features.</p>
</div>
</div>
<div id="genetic-differentiation" class="section level2">
<h2>Genetic differentiation</h2>
<p>A fundamental question to most population studies is whether populations are diverse and whether this diversity is shared among the populations? To address the question of within population diversity geneticists typically report heterozygosity. This is the probability that two alleles randomly chosen from a population will be different <span class="citation">(Nei, 1973)</span>. Ecologists may know this as Simpson’s Index <span class="citation">(Simpson, 1949)</span>. To address differentiation population geneticists typically utilize <span class="math inline">\(F_{ST}\)</span> or one of its analogues. Population differentiation measured by <span class="math inline">\(F_{ST}\)</span> was originally proposed by Sewall Wright <span class="citation">(Wright, 1949)</span>. This was later extended to a method based on diversity by Masatoshi Nei <span class="citation">(Nei, 1973)</span>. As researchers applied these metrics to microsatellites, genetic markers with a large number of alleles, it became clear that Nei’s measure would not correctly range from zero to one, so Philip Hedrick proposed a correction <span class="citation">(Hedrick, 2005)</span>. More recently, Lou Jost proposed another alternative <span class="citation">(Jost, 2008)</span>. You can tell a topic is popular when so many variants of it are generated. And there are more variants than mentioned here. A nice discussion as to which measure may be appropriate for your data was posteed to teh Molecular Ecologist blog titled <a href="http://www.molecularecologist.com/2011/03/should-i-use-fst-gst-or-d-2/">should I use <span class="math inline">\(F_{ST}\)</span>, <span class="math inline">\(G_{ST}\)</span> or <span class="math inline">\(D\)</span>?</a>.</p>
<p>In <code>vcfR</code>, the function <code>genetic_diff()</code> was implemented to measure population diversity and differentiation. Because VCF data typically do not include population information we’ll have to supply it as a factor. The method ‘nei’ employed here is based on the methods reported by Hedrick <span class="citation">(Hedrick, 2005)</span>. The exception is that the heterozygosities are weighted by the number of alleles observed in each population. This was inspired by <code>hierfstat::pairwise.fst()</code> which uses the number of individuals observed in each population to weight the heterozygosities. By using the number of alleles observed instead of the number of individuals we remove an assumption about how many alleles each individual may contribute. That is, we should be able to accomodate samples of mixed ploidy.</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1"></a><span class="kw">library</span>(vcfR)</span>
<span id="cb47-2"><a href="#cb47-2"></a><span class="kw">data</span>(vcfR_example)</span>
<span id="cb47-3"><a href="#cb47-3"></a>pop <-<span class="st"> </span><span class="kw">as.factor</span>(<span class="kw">c</span>(<span class="st">"us"</span>, <span class="st">"eu"</span>, <span class="st">"us"</span>, <span class="st">"af"</span>, <span class="st">"eu"</span>, <span class="st">"us"</span>, <span class="st">"mx"</span>, <span class="st">"eu"</span>, <span class="st">"eu"</span>, <span class="st">"sa"</span>, <span class="st">"mx"</span>, <span class="st">"sa"</span>, <span class="st">"us"</span>, <span class="st">"sa"</span>, <span class="st">"Pmir"</span>, <span class="st">"us"</span>, <span class="st">"eu"</span>, <span class="st">"eu"</span>))</span>
<span id="cb47-4"><a href="#cb47-4"></a>myDiff <-<span class="st"> </span><span class="kw">genetic_diff</span>(vcf, <span class="dt">pops =</span> pop, <span class="dt">method =</span> <span class="st">'nei'</span>)</span>
<span id="cb47-5"><a href="#cb47-5"></a>knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">head</span>(myDiff[,<span class="dv">1</span><span class="op">:</span><span class="dv">15</span>]))</span></code></pre></div>
<table>
<thead>
<tr class="header">
<th align="left">CHROM</th>
<th align="left">POS</th>
<th align="right">Hs_af</th>
<th align="right">Hs_eu</th>
<th align="right">Hs_mx</th>
<th align="right">Hs_Pmir</th>
<th align="right">Hs_sa</th>
<th align="right">Hs_us</th>
<th align="right">Ht</th>
<th align="right">n_af</th>
<th align="right">n_eu</th>
<th align="right">n_mx</th>
<th align="right">n_Pmir</th>
<th align="right">n_sa</th>
<th align="right">n_us</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Supercontig_1.50</td>
<td align="left">2</td>
<td align="right">0</td>
<td align="right">0.0</td>
<td align="right">0.000</td>
<td align="right">0.5</td>
<td align="right">0.000</td>
<td align="right">0.00</td>
<td align="right">0.0798611</td>
<td align="right">2</td>
<td align="right">4</td>
<td align="right">4</td>
<td align="right">2</td>
<td align="right">4</td>
<td align="right">8</td>
</tr>
<tr class="even">
<td align="left">Supercontig_1.50</td>
<td align="left">246</td>
<td align="right">NaN</td>
<td align="right">0.0</td>
<td align="right">0.375</td>
<td align="right">NaN</td>
<td align="right">0.000</td>
<td align="right">0.50</td>
<td align="right">0.3512397</td>
<td align="right">0</td>
<td align="right">4</td>
<td align="right">4</td>
<td align="right">0</td>
<td align="right">6</td>
<td align="right">8</td>
</tr>
<tr class="odd">
<td align="left">Supercontig_1.50</td>
<td align="left">549</td>
<td align="right">NaN</td>
<td align="right">0.0</td>
<td align="right">NaN</td>
<td align="right">NaN</td>
<td align="right">NaN</td>
<td align="right">0.50</td>
<td align="right">0.4444444</td>
<td align="right">0</td>
<td align="right">2</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">4</td>
</tr>
<tr class="even">
<td align="left">Supercontig_1.50</td>
<td align="left">668</td>
<td align="right">NaN</td>
<td align="right">0.5</td>
<td align="right">0.000</td>
<td align="right">NaN</td>
<td align="right">0.000</td>
<td align="right">0.50</td>
<td align="right">0.5000000</td>
<td align="right">0</td>
<td align="right">4</td>
<td align="right">2</td>
<td align="right">0</td>
<td align="right">2</td>
<td align="right">8</td>
</tr>
<tr class="odd">
<td align="left">Supercontig_1.50</td>
<td align="left">765</td>
<td align="right">0</td>
<td align="right">0.0</td>
<td align="right">0.000</td>
<td align="right">0.0</td>
<td align="right">0.000</td>
<td align="right">0.00</td>
<td align="right">0.1107266</td>
<td align="right">2</td>
<td align="right">12</td>
<td align="right">4</td>
<td align="right">2</td>
<td align="right">4</td>
<td align="right">10</td>
</tr>
<tr class="even">
<td align="left">Supercontig_1.50</td>
<td align="left">780</td>
<td align="right">0</td>
<td align="right">0.0</td>
<td align="right">0.000</td>
<td align="right">0.0</td>
<td align="right">0.375</td>
<td align="right">0.18</td>
<td align="right">0.1244444</td>
<td align="right">2</td>
<td align="right">8</td>
<td align="right">4</td>
<td align="right">2</td>
<td align="right">4</td>
<td align="right">10</td>
</tr>
</tbody>
</table>
<p>The function returns the chromosome and position of each variant as provided in the VCF data. This should allow you to align its output with the VCF data. The heterozygosities for each population are reported as well as the total heterozygosity, followed by the number of alleles observed in each population. Note that in some populations zero alleles were observed. Populations with zero alleles reported heterozygosities of ‘NaN’ because of this absence of data.</p>
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1"></a>knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">head</span>(myDiff[,<span class="dv">16</span><span class="op">:</span><span class="dv">19</span>]))</span></code></pre></div>
<table>
<thead>
<tr class="header">
<th align="right">Gst</th>
<th align="right">Htmax</th>
<th align="right">Gstmax</th>
<th align="right">Gprimest</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">0.4782609</td>
<td align="right">0.7951389</td>
<td align="right">0.9475983</td>
<td align="right">0.5047085</td>
</tr>
<tr class="even">
<td align="right">NaN</td>
<td align="right">0.8057851</td>
<td align="right">NaN</td>
<td align="right">NaN</td>
</tr>
<tr class="odd">
<td align="right">NaN</td>
<td align="right">0.6666667</td>
<td align="right">NaN</td>
<td align="right">NaN</td>
</tr>
<tr class="even">
<td align="right">NaN</td>
<td align="right">0.8125000</td>
<td align="right">NaN</td>
<td align="right">NaN</td>
</tr>
<tr class="odd">
<td align="right">1.0000000</td>
<td align="right">0.7543253</td>
<td align="right">1.0000000</td>
<td align="right">1.0000000</td>
</tr>
<tr class="even">
<td align="right">0.1160714</td>
<td align="right">0.8000000</td>
<td align="right">0.8625000</td>
<td align="right">0.1345756</td>
</tr>
</tbody>
</table>
<p>The remaining columns contain <span class="math inline">\(G_{ST}\)</span>, the maximum heterozygosity, the maximum <span class="math inline">\(G_{ST}\)</span> and finally <span class="math inline">\(G'_{ST}\)</span>. The maximum heterozygosity and the maximum <span class="math inline">\(G_{ST}\)</span> are intermediary values used to calculate <span class="math inline">\(G'_{ST}\)</span>. They are typically not reported but provide values to help validate that <span class="math inline">\(G'_{ST}\)</span> was calculated correctly. Note that the populations that had zero alleles, and therefore a heterozygosity of ‘NaN’, contributed to <span class="math inline">\(G_{ST}\)</span>s that were also ‘NaN’. To avoid this you may want to consider omitting populations with a small sample size or that contain a large amount of missing data.</p>
<p>We now have information for each variant in the VCF data. Because this is typically a large quantity of information, we’ll want to summarize it. One way is to take averages of the data.</p>
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1"></a>knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">round</span>(<span class="kw">colMeans</span>(myDiff[,<span class="kw">c</span>(<span class="dv">3</span><span class="op">:</span><span class="dv">9</span>,<span class="dv">16</span>,<span class="dv">19</span>)], <span class="dt">na.rm =</span> <span class="ot">TRUE</span>), <span class="dt">digits =</span> <span class="dv">3</span>))</span></code></pre></div>
<table>
<thead>
<tr class="header">
<th align="left"></th>
<th align="right">x</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Hs_af</td>
<td align="right">0.176</td>
</tr>
<tr class="even">
<td align="left">Hs_eu</td>
<td align="right">0.188</td>
</tr>
<tr class="odd">
<td align="left">Hs_mx</td>
<td align="right">0.168</td>
</tr>
<tr class="even">
<td align="left">Hs_Pmir</td>
<td align="right">0.052</td>
</tr>
<tr class="odd">
<td align="left">Hs_sa</td>
<td align="right">0.198</td>
</tr>
<tr class="even">
<td align="left">Hs_us</td>
<td align="right">0.155</td>
</tr>
<tr class="odd">
<td align="left">Ht</td>
<td align="right">0.247</td>
</tr>