-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItalyWorkshop.html
947 lines (857 loc) · 33.8 KB
/
ItalyWorkshop.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="SKaszubinski & JReceveur" />
<title>Italy Stream Microbiome Workshop</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/flatly.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<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>
<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: 60px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 65px;
margin-top: -65px;
}
.section h2 {
padding-top: 65px;
margin-top: -65px;
}
.section h3 {
padding-top: 65px;
margin-top: -65px;
}
.section h4 {
padding-top: 65px;
margin-top: -65px;
}
.section h5 {
padding-top: 65px;
margin-top: -65px;
}
.section h6 {
padding-top: 65px;
margin-top: -65px;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Benbow Lab</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="ItalyWorkshop.html">16S Workshop</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Other Tutorials
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Phylogeny.html">Phylogeny from VCF file</a>
</li>
<li>
<a href="Hippo.html">16S Analysis Insect communities</a>
</li>
<li>
<a href="Bswab.html">Spatial changes in human microbiome</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">Italy Stream Microbiome Workshop</h1>
<h4 class="author">SKaszubinski & JReceveur</h4>
</div>
<div id="getting-started" class="section level1">
<h1>Getting Started</h1>
<p>These example materials were developed for the Bridging Ecology and Applications Through High Throughput Sequencing Technology Workshop (14 March 2019) using data from a ongoing project investigating the microbial communities associated with stream macroinvertebrates in the Po River. For contact information and questions see our <a href="https://ericbenbow.wixsite.com/website">lab webpage</a> or <a href="https://benbowlab.github.io/">Github site</a>.</p>
<div id="install-packages" class="section level2">
<h2>Install Packages</h2>
<p>The first step is to install and load the packages you will need. The package <a href="https://joey711.github.io/phyloseq/">phyloseq</a> is a widely used package for analyzing microbial data and has a number of excellent tutorials if you would like more information. The other packages contain more general functions such as statistical testing and plotting that we will use as well.</p>
<pre class="r"><code>install.packages(c("vegan","ggplot2","RCurl","plyr","dplyr"))
source('http://bioconductor.org/biocLite.R')
biocLite('phyloseq')</code></pre>
</div>
<div id="loading-packages" class="section level2">
<h2>Loading packages</h2>
<p>After the packages are installed, they will have to be loaded each time you start a new R session. You will most likely see several messages when you run the code below but as long as there are not any errors you can move on to the next steps.</p>
<pre class="r"><code>library(vegan)
library(ggplot2)
library(phyloseq)
library(RCurl)
library(plyr)
library(dplyr)</code></pre>
</div>
</div>
<div id="data-import" class="section level1">
<h1>Data Import</h1>
<p>To download and import the data into R, the lines below will download the sample information.</p>
<pre class="r"><code>x<-getURL("https://raw.githubusercontent.com/BenbowLab/BenbowLab.github.io/master/ItalyStreamMicrobiomeMetadata.csv")
metadata<-read.csv(text= x,header=T)
head(metadata) # To see the variables in the metadata file</code></pre>
<p>The file containing the sequencing information is too large to directly download into R from Github easily (1,026 KB). You will have to open a browser to download the data. The first line below will take you to the page with the data. You will see a download button, click that.</p>
<p>After the file is downloaded, run the next line to import the data into R. This command will open a window where you will navigate to the file and click open to select the file and load it into R.</p>
<pre class="r"><code>browseURL("https://github.com/BenbowLab/BenbowLab.github.io/blob/master/ItalyInvert2018WTax.biom")
biom<-import_biom(file.choose(),parseFunction= parse_taxonomy_greengenes) </code></pre>
<p>The next section of code will combine together the metadata and sequencing data into a single object in R. The rarifying step accounts for differing numbers of sequencing reads per samples by randomly choosing reads up to the chosen value.</p>
<pre class="r"><code>sampdat=sample_data(metadata)
sample_names(sampdat)=metadata$id
CombinedData=merge_phyloseq(biom,sampdat)
CombinedData=rarefy_even_depth(CombinedData, 3000, replace = TRUE, trimOTUs = TRUE, verbose = TRUE,rngseed = TRUE)</code></pre>
<pre><code>## `set.seed(TRUE)` was used to initialize repeatable random subsampling.</code></pre>
<pre><code>## Please record this for your records so others can reproduce.</code></pre>
<pre><code>## Try `set.seed(TRUE); .Random.seed` for the full vector</code></pre>
<pre><code>## ...</code></pre>
<pre><code>## 504OTUs were removed because they are no longer
## present in any sample after random subsampling</code></pre>
<pre><code>## ...</code></pre>
<div id="data-overview" class="section level2 tabset">
<h2>Data overview</h2>
<p>Sampling and methodologies:</p>
<p>The data that we will be looking at are the internal microbiome of aquatic insects from different functional feeding groups at two locations with different riparian conditions (Forest vs Alpine Prairie).</p>
<div class="figure">
<img src="MapOfSampleSites.jpg" alt="Locations of sample sites within the Upper Po river" />
<p class="caption">Locations of sample sites within the Upper Po river</p>
</div>
<div class="figure">
<img src="SampleSitePhotos.jpg" alt="A) Alpine Prairie (Pian della Regina) B) Forested site (Ostana)" />
<p class="caption">A) Alpine Prairie (Pian della Regina) B) Forested site (Ostana)</p>
</div>
<p>The data consist of an Operational Taxonomic Unit (OTU) table, sample data, and taxonomy table. The OTU table contains the number of sequence variants. The taxonomy table includes the taxonomy information for each sequence variant, from Kingdom down to Genus. The sample data contains metadata for the study.</p>
<pre class="r"><code>CombinedData</code></pre>
<pre><code>## phyloseq-class experiment-level object
## otu_table() OTU Table: [ 2442 taxa and 26 samples ]
## sample_data() Sample Data: [ 26 samples by 8 sample variables ]
## tax_table() Taxonomy Table: [ 2442 taxa by 14 taxonomic ranks ]</code></pre>
<p>These are the most abundant taxon by functional feeding group and sampling station.</p>
<pre><code>## Sampling_station FFG Taxon_name N meanWeight sd
## 4 Ostana Shredder Tipula_(Acutitipula) 3 534.9333 425.43289
## 2 Ostana Predator Perla 2 190.5000 174.51395
## 7 PDR Shredder Tipula_(Acutitipula) 4 87.7000 42.81799
## 5 PDR Predator Dictyogenus 4 83.9500 16.27073
## 1 Ostana Filterer Hydropsyche 4 41.4500 33.70762
## se
## 4 245.623793
## 2 123.400000
## 7 21.408993
## 5 8.135365
## 1 16.853808</code></pre>
</div>
</div>
<div id="taxonomic-composition" class="section level1">
<h1>Taxonomic Composition</h1>
<p>The next section of code filters out bacterial taxa that occured at a low abundance and makes additional files where the samples are group together at different levels (Phylum, Family, Genus).</p>
<pre class="r"><code>GPr = transform_sample_counts(CombinedData, function(x) x / sum(x) ) #transform samples based on relative abundance
GPr = filter_taxa(GPr, function(x) mean(x) > 1e-5, TRUE) #Remve very low abundance samples
PhylumAll=tax_glom(GPr, "Phylum")# Group samples at the phylum level
PhylumLevel = filter_taxa(PhylumAll, function(x) mean(x) > 1e-2, TRUE) #filter out any taxa lower tha 1%
FamilyAll=tax_glom(GPr,"Family")
FamilyLevel = filter_taxa(FamilyAll, function(x) mean(x) > 2e-2, TRUE) #filter out any taxa lower than 2%
GenusAll=tax_glom(GPr,"Genus")
GenusLevel = filter_taxa(GenusAll, function(x) mean(x) > 2e-2, TRUE) #filter out any taxa lower than 2%</code></pre>
<p>The next section of code summarizes the phylum level relative abundance by a variable, in this case Functional feeding group, which we will use to plot the data.</p>
<pre class="r"><code>df <- psmelt(PhylumLevel)
df$Abundance=df$Abundance*100
Trtdata <- ddply(df, c("Phylum", "FFG"), summarise, #To look at other variables change "FFG", to look at other taxanomic levels change Phylum and use the coorosponding file (e.g. FamilyLevel)
N = length(Abundance),
mean = mean(Abundance),
sd = sd(Abundance),
se = sd / sqrt(N)
)
head(Trtdata)</code></pre>
<pre><code>## Phylum FFG N mean sd se
## 1 Actinobacteria Filterer 4 0.3583333 0.4085884 0.2042942
## 2 Actinobacteria Predator 6 1.2888889 0.5467751 0.2232200
## 3 Actinobacteria Scraper 9 1.1592593 1.1989321 0.3996440
## 4 Actinobacteria Shredder 7 1.3857143 1.8113692 0.6846332
## 5 Bacteroidetes Filterer 4 22.6333333 4.9739320 2.4869660
## 6 Bacteroidetes Predator 6 33.1388889 9.1587825 3.7390573</code></pre>
<div id="plotting-phylum-level-relative-bacterial-abundance" class="section level2">
<h2>Plotting phylum level relative bacterial abundance</h2>
<p>The next section of code will take the summarized data from above and plot the relative abundance by FFG at the phylum level.</p>
<pre class="r"><code>PhylumLevelPlot=ggplot(Trtdata, aes(x=FFG,y=mean))+geom_bar(aes(fill = Phylum),colour="black", stat="identity")+xlab("FFG")+ylab("Relative Abundance (> 1%)")
PhylumLevelPlot</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-13-1.png" width="1344" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="comparing-alpha-diversity-between-sample-groups" class="section level1">
<h1>Comparing Alpha diversity between sample groups</h1>
<p>Shannon and Faith's Phylogenetic diversity have already been calculated for each sample and are already in the metadata file.</p>
<p>Faith's diversity takes into account richness, evenness, as well as the phylogenetic difference between samples. Bacterial species which are further apart on a phylogenetic tree are weighted differently than samples which are closer together.</p>
<pre class="r"><code>ggplot(metadata,aes(x=FFG,y=shannon,fill=FFG))+ geom_boxplot()+ylab("Shannon Diversity")</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-14-1.png" width="1344" style="display: block; margin: auto;" /></p>
<pre class="r"><code>ggplot(metadata,aes(x=FFG,y=faith_pd,fill=FFG))+ geom_boxplot()+ylab("Faith's PD")</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-14-2.png" width="1344" style="display: block; margin: auto;" /></p>
<div id="splitting-samples-by-site" class="section level2">
<h2>Splitting samples by site</h2>
<p>Below is an example of how you would subset the data by site. For example, if you were interested in looking at the differences in alpha diversity between the bacterial communities only at the forested site.</p>
<pre class="r"><code>OstanaMetadata<-subset(metadata, Sampling_station == "Ostana")#If you wanted to look at a different variable (e.g. shredders from both locations you would change the code to FFG == "Shredders")
ggplot(OstanaMetadata,aes(x=FFG,y=shannon,fill=FFG))+ geom_boxplot()+ylab("Shannon Diversity Ostana")</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-15-1.png" width="1344" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="beta-diversity" class="section level1">
<h1>Beta diversity</h1>
<p>In addition to alpha diversity, we are also interested in looking at the differences in beta diversity between sample groups. To visualize differences in beta diversity, the code below uses PCoA plots with ovals repersenting the 95% confidence intervals for the mean of each group.</p>
<p>While this example uses jaccard distance, there are a number of other distance methods which can be used depending on the situation.</p>
<pre class="r"><code>ord=ordinate(CombinedData,"PCoA", "jaccard") #To use a differetn metric change "jaccard" to the desired metric. For example "wunifrac" or "bray"
ordplot=plot_ordination(CombinedData, ord,"samples", color="FFG")+geom_point(size=4)
ordplot+ stat_ellipse(type= "norm",geom = "polygon", alpha = 1/4, aes(fill = FFG))</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-16-1.png" width="1344" style="display: block; margin: auto;" /></p>
<pre class="r"><code>#If you were only interested in looking within a single location you could use the code Ostana<-subset_samples(physeq,Sampling_site =="Ostana")</code></pre>
</div>
<div id="questions-for-further-understanding" class="section level1">
<h1>Questions for further understanding</h1>
<ul>
<li><p>What happens if you choose a different level to filter out low abundance bacteria before plotting? (e.g. PhylumLevel = filter_taxa(PhylumAll, function(x) mean(x) > 1e-4, TRUE))</p></li>
<li><p>How would you change the code above to plot the family level relative abundance rather than the phylum level?</p></li>
<li><p>Do the two locations show the same pattern in alpha diversity by functional feeding group? (Seperate the two locations and plot them both individually)</p></li>
<li><p>How does using a different distance metric change the beta diversity plots? (e.g using "bray" or "gower" instead of "jaccard")</p></li>
</ul>
</div>
<div id="other-functions" class="section level1">
<h1>Other functions</h1>
<div id="random-forest" class="section level2">
<h2>Random Forest</h2>
<p>Random forest is a modeling technique which uses machine learning to identify important predictors for groups of samples. It also tests how well the model it creates is able to classify samples based on the data and the variable chosen.</p>
<pre class="r"><code>#install.packages("randomForest")
#install.packages("knitr")
library(knitr)</code></pre>
<pre><code>## Warning: package 'knitr' was built under R version 4.0.2</code></pre>
<pre class="r"><code>library(randomForest)</code></pre>
<pre><code>## randomForest 4.6-14</code></pre>
<pre><code>## Type rfNews() to see new features/changes/bug fixes.</code></pre>
<pre><code>##
## Attaching package: 'randomForest'</code></pre>
<pre><code>## The following object is masked from 'package:dplyr':
##
## combine</code></pre>
<pre><code>## The following object is masked from 'package:ggplot2':
##
## margin</code></pre>
<pre class="r"><code>ForestData=GenusAll#Change this one so you dont have to rewrite all variables
predictors=t(otu_table(ForestData))
response <- as.factor(sample_data(ForestData)$FFG)
rf.data <- data.frame(response, predictors)
FeedingGroupForest <- randomForest(response~., data = rf.data, ntree = 1000)
print(FeedingGroupForest)#returns overall Random Forest results</code></pre>
<pre><code>##
## Call:
## randomForest(formula = response ~ ., data = rf.data, ntree = 1000)
## Type of random forest: classification
## Number of trees: 1000
## No. of variables tried at each split: 12
##
## OOB estimate of error rate: 3.85%
## Confusion matrix:
## Filterer Predator Scraper Shredder class.error
## Filterer 3 1 0 0 0.25
## Predator 0 6 0 0 0.00
## Scraper 0 0 9 0 0.00
## Shredder 0 0 0 7 0.00</code></pre>
<pre class="r"><code>imp <- importance(FeedingGroupForest)#all the steps that are imp or imp. are building a dataframe that contains info about the taxa used by the Random Forest testto classify treatment
imp <- data.frame(predictors = rownames(imp), imp)
imp.sort <- arrange(imp, desc(MeanDecreaseGini))
imp.sort$predictors <- factor(imp.sort$predictors, levels = imp.sort$predictors)
imp.20 <- imp.sort[1:23, ]#22
ggplot(imp.20, aes(x = predictors, y = MeanDecreaseGini)) +
geom_bar(stat = "identity", fill = "indianred") +
coord_flip() +
ggtitle("Most important genera for classifying samples\n by Feeding Group")#\n in a string tells it to start a new line</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-17-1.png" width="1344" style="display: block; margin: auto;" /></p>
<pre class="r"><code>#imp.20$MeanDecreaseGini
otunames <- imp.20$predictors
r <- rownames(tax_table(ForestData)) %in% otunames
otunames</code></pre>
<pre><code>## [1] acccb7cec4d146864bc11d37da55dcd0 X2b05a6e9e6c580f7fc168a5bbb29de2d
## [3] ad07e5885874179952e16bf2f7bb57b3 a7c725b951d62e6f6814fb8ca64a356e
## [5] X4367180b0dc40f77a063355ebab6a4bb X0fbb434b42e7241efa6451d8f1b429d0
## [7] b672de3b577f55a4350eabdce0f18904 dc7d0d97c9604c01c99c972b878e09a0
## [9] X43dc593942c5c838cdcb10dac0a39474 a046edb519c4e3cbdf77ada497c4d743
## [11] X5ba8d1715c2080f633d2e3de3ddae03a e32f997a31db6624fc4f67c269121d4c
## [13] X1036caae06a5e5c605ab8120e5b5b7bc X1e994910b44683e96c37da4cd04862a4
## [15] X07705f7fffe33ad226eab098040fbb75 X6b78b6e57cad2b7f9420cee2c46db2aa
## [17] X237f7729fcc16a8a864b826f36f307c3 d945dd1c4d20347007f6d0c0d0ec581e
## [19] ee705bbea43c68a47a9c2ba41efa2067 X5e419280ad42e77b964625f286da0f08
## [21] X6ea12a2a6eb7f0d377858aaeca4664e4 X33d90d9f698362388259881cb6ec497c
## [23] de46410fbc643aeaed03d6aa3878d71a
## 165 Levels: acccb7cec4d146864bc11d37da55dcd0 ...</code></pre>
<pre class="r"><code>PredictorTable<-kable(tax_table(ForestData)[r, ])#returns a list of the most important predictors for Random Forest Classification
PredictorTable</code></pre>
<table style="width:100%;">
<colgroup>
<col width="17%" />
<col width="5%" />
<col width="8%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
<col width="8%" />
<col width="4%" />
<col width="3%" />
<col width="3%" />
<col width="3%" />
<col width="3%" />
<col width="3%" />
<col width="3%" />
<col width="3%" />
</colgroup>
<thead>
<tr class="header">
<th align="left"></th>
<th align="left">Kingdom</th>
<th align="left">Phylum</th>
<th align="left">Class</th>
<th align="left">Order</th>
<th align="left">Family</th>
<th align="left">Genus</th>
<th align="left">Species</th>
<th align="left">Rank5</th>
<th align="left">Rank6</th>
<th align="left">Rank7</th>
<th align="left">Rank4</th>
<th align="left">Rank2</th>
<th align="left">Rank3</th>
<th align="left">Rank1</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">e32f997a31db6624fc4f67c269121d4c</td>
<td align="left">Bacteria</td>
<td align="left">Bacteroidetes</td>
<td align="left">Cytophagia</td>
<td align="left">Cytophagales</td>
<td align="left">Cytophagaceae</td>
<td align="left">Leadbetterella</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="even">
<td align="left">d945dd1c4d20347007f6d0c0d0ec581e</td>
<td align="left">Bacteria</td>
<td align="left">Deferribacteres</td>
<td align="left">Deferribacteres</td>
<td align="left">Deferribacterales</td>
<td align="left">Deferribacteraceae</td>
<td align="left">Mucispirillum</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="odd">
<td align="left">ee705bbea43c68a47a9c2ba41efa2067</td>
<td align="left">Bacteria</td>
<td align="left">Armatimonadetes</td>
<td align="left">Armatimonadia</td>
<td align="left">Armatimonadales</td>
<td align="left">Armatimonadaceae</td>
<td align="left">Armatimonas</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="even">
<td align="left">de46410fbc643aeaed03d6aa3878d71a</td>
<td align="left">Bacteria</td>
<td align="left">Verrucomicrobia</td>
<td align="left">Verrucomicrobiae</td>
<td align="left">Verrucomicrobiales</td>
<td align="left">Verrucomicrobiaceae</td>
<td align="left">Luteolibacter</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="odd">
<td align="left">dc7d0d97c9604c01c99c972b878e09a0</td>
<td align="left">Bacteria</td>
<td align="left">Firmicutes</td>
<td align="left">Clostridia</td>
<td align="left">Clostridiales</td>
<td align="left">Ruminococcaceae</td>
<td align="left">Ruminococcus</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="even">
<td align="left">a046edb519c4e3cbdf77ada497c4d743</td>
<td align="left">Bacteria</td>
<td align="left">Proteobacteria</td>
<td align="left">Betaproteobacteria</td>
<td align="left">Burkholderiales</td>
<td align="left">Comamonadaceae</td>
<td align="left">Rhodoferax</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="odd">
<td align="left">b672de3b577f55a4350eabdce0f18904</td>
<td align="left">Bacteria</td>
<td align="left">Proteobacteria</td>
<td align="left">Gammaproteobacteria</td>
<td align="left">Pseudomonadales</td>
<td align="left">Moraxellaceae</td>
<td align="left">Perlucidibaca</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="even">
<td align="left">acccb7cec4d146864bc11d37da55dcd0</td>
<td align="left">Bacteria</td>
<td align="left">Proteobacteria</td>
<td align="left">Betaproteobacteria</td>
<td align="left">Methylophilales</td>
<td align="left">Methylophilaceae</td>
<td align="left">Methylotenera</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="odd">
<td align="left">a7c725b951d62e6f6814fb8ca64a356e</td>
<td align="left">Bacteria</td>
<td align="left">Firmicutes</td>
<td align="left">Clostridia</td>
<td align="left">Clostridiales</td>
<td align="left">Lachnospiraceae</td>
<td align="left">Clostridium</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
<tr class="even">
<td align="left">ad07e5885874179952e16bf2f7bb57b3</td>
<td align="left">Bacteria</td>
<td align="left">Bacteroidetes</td>
<td align="left">Cytophagia</td>
<td align="left">Cytophagales</td>
<td align="left">Cytophagaceae</td>
<td align="left">Emticicia</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="left">NA</td>
</tr>
</tbody>
</table>
</div>
<div id="statistical-testing" class="section level2">
<h2>Statistical testing</h2>
<p>There are a wide variety of statistical test that can be used to compare microbiome data. Below we will show an example of comparing beta diveristy using a PERMANOVA test.</p>
<p>This test will test whether the variablility within groups is significantly different (Are one group of samples more similar to each other than a different group.</p>
<div id="variance-in-beta-diversity" class="section level3">
<h3>Variance in beta diversity</h3>
<pre class="r"><code>GPdist=phyloseq::distance(CombinedData, "jaccard")
beta=betadisper(GPdist, sample_data(CombinedData)$FFG)
permutest(beta)</code></pre>
<pre><code>##
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 999
##
## Response: Distances
## Df Sum Sq Mean Sq F N.Perm Pr(>F)
## Groups 3 0.045819 0.0152730 3.9334 999 0.014 *
## Residuals 22 0.085424 0.0038829
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<pre class="r"><code>boxplot(beta)</code></pre>
<p><img src="ItalyWorkshop_files/figure-html/unnamed-chunk-18-1.png" width="1344" style="display: block; margin: auto;" /></p>
</div>
<div id="testing-for-differences-in-beta-diversity-between-groups" class="section level3">
<h3>Testing for differences in beta diversity between groups</h3>
<p>The following test will compare whether the differences seen in the PCoA plots from above are significant, using a Permutational Analysis of Variance (PERMANOVA) test.</p>
<pre class="r"><code>GPdist=phyloseq::distance(CombinedData, "jaccard")
adonis(GPdist ~ FFG*Sampling_station, as(sample_data(CombinedData), "data.frame"))</code></pre>
<pre><code>##
## Call:
## adonis(formula = GPdist ~ FFG * Sampling_station, data = as(sample_data(CombinedData), "data.frame"))
##
## Permutation: free
## Number of permutations: 999
##
## Terms added sequentially (first to last)
##
## Df SumsOfSqs MeanSqs F.Model R2 Pr(>F)
## FFG 3 3.0623 1.02075 3.0631 0.27091 0.001 ***
## Sampling_station 1 0.7067 0.70674 2.1208 0.06252 0.001 ***
## FFG:Sampling_station 2 1.2032 0.60158 1.8052 0.10644 0.001 ***
## Residuals 19 6.3316 0.33324 0.56013
## Total 25 11.3038 1.00000
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open')
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>