-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16S Data Normalisation
182 lines (166 loc) · 7.3 KB
/
16S Data Normalisation
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
```{r Libraries}
library("gridExtra")
library("vegan")
library("metacoder")
library("taxa")
library("phyloseq")
library("ggplot2")
library("dplyr")
library("readr")
library("stringr")
library("agricolae")
library("ape")
library("tidyverse")
library("tidyr")
library("ggpubr")
library("metagMisc")
library("microbiome")
```
```{r Phylo object}
setwd("~/Dropbox/UTS/PhD/Projects/Chapter2_RoseBay/Data_Analysis/Data/16S/Data")
physeq <- readRDS('physeq.RDS')
colnames(tax_table(physeq)) #Check they are good.
sample_data <- read_csv('SMD.csv')
asv_cleaned_long <- read_csv("asv_cleaned_long.csv")
```
```{r CSS - object creation}
library(metagenomeSeq)
obj = phyloseq_to_metagenomeSeq(physeq)
p <- cumNormStatFast(obj)
obj.norm = cumNorm(obj, p = p) #this is running it without a condition (or experimental design)
#condition = obj$Ind1
#obj.norm.cond = wrenchNorm(obj.norm, condition = condition)
```
```{r CSS - Export Data}
mat <- MRcounts(obj.norm, norm = TRUE, log = FALSE)
mat_df <- as.data.frame(mat)
mat_df$ASV <- rownames(mat_df)
mat_df <- cbind(rownames(mat_df), data.frame(mat_df, row.names=NULL))
names(mat_df)[names(mat_df) == "rownames(mat_df)"] <- "ASV_name"
mat_long <- mat_df %>% dplyr::select(-ASV) %>% pivot_longer(-ASV_name, names_to = "SampleID", values_to = "abund_css")
css_df <- asv_cleaned_long %>% left_join(mat_long)
#css_df <- na.omit(css_df)
```
```{r CSS - NMDS Data preparation}
library(tidyverse)
library(vegan)
library(dplyr)
library(reshape2)
abund_css.subset <- css_df %>% dplyr::select(ASV_name, abund_css, SampleID)
abund_css.subset <- abund_css.subset %>% filter(!abund_css ==0)
css.asvs.wide.OFG <- reshape2::dcast(abund_css.subset, SampleID~ASV_name, value.var = "abund_css", fill=0)
rownames(css.asvs.wide.OFG) <- css.asvs.wide.OFG$SampleID
css_nmds_df <- css.asvs.wide.OFG[,-c(1)]
```
```{r CSS - NMDS}
CSS.nMDS <- metaMDS(css_nmds_df, distance = "bray", try =99, trymax=100, autotransform = F)
stressplot(CSS.nMDS)
plot(CSS.nMDS)
names(CSS.nMDS)
CSS.nMDS.points<-cbind(css.asvs.wide.OFG[,c(1)], as.data.frame(CSS.nMDS$points))
colnames(CSS.nMDS.points)[1] <- "SampleID"
CSS.nMDS.points<-CSS.nMDS.points %>% mutate(code=as.character(SampleID)) %>% left_join(sample_data)
CSS.nMDS.points$Transect <- as.character(CSS.nMDS.points$Transect)
```
```{r CSS - MDS}
library(tidyverse)
MDS.CSS <- ggplot(CSS.nMDS.points, aes(x=MDS1, y=MDS2, color = `Transect`)) +
geom_point(aes(size=0.4, alpha=0.5)) +
theme_bw()
MDS.CSS <- MDS.CSS + scale_colour_manual(values=c("0"="#E66101", "1"="#4DAC26" , "2"="#0571B0", "3"="#cc1616", "4"="#b625e3"))
MDS.CSS
```
```{R Rarefaction with vegan - Filter to depth}
Read_Count <- asv_cleaned_long %>% group_by(SampleID) %>% summarise(Total = sum(Abundance))
asv_cleaned_long <- asv_cleaned_long %>% left_join(Read_Count)
Rareify.filt <- asv_cleaned_long %>% filter(Total > 30000)
Rareify.filt <- Rareify.filt %>% filter(!Abundance ==0)
```
```{r Rarefaction with vegan - Data Prep}
library(reshape2)
tableOFG = subset(Rareify.filt, select = c(ASV_name, ASV, SampleID, Abundance))
tableFGID = subset(tableOFG, select = c(ASV_name, SampleID, Abundance))
asvs.wide.OFG <- reshape2::dcast(tableFGID, SampleID~ASV_name, value.var = "Abundance", fill=0)
```
```{r Rareify}
rownames(asvs.wide.OFG)<-asvs.wide.OFG$SampleID
rarefy30k<-rrarefy(asvs.wide.OFG[,-c(1)], 30000)
rarefy30k.df<-as.data.frame(rarefy30k)
rarefy30k.df <- cbind(rownames(rarefy30k.df), data.frame(rarefy30k.df, row.names=NULL))
names(rarefy30k.df)[names(rarefy30k.df) == "rownames(rarefy30k.df)"] <- "ASV_name"
rarefy30k_long <- rarefy30k.df %>% pivot_longer(-ASV_name, names_to = "SampleID", values_to = "Abundance_Vegan_Rarefied")
rarefy30k_long <- rarefy30k_long %>% rename(SampleID. = ASV_name)
rarefy30k_long <- rarefy30k_long %>% rename(ASV_name = SampleID)
rarefy30k_long <- rarefy30k_long %>% rename(SampleID = SampleID.)
asv_rarefy30k_long <- asv_cleaned_long %>% left_join(rarefy30k_long)
Read_Count_rarefy30k <- asv_rarefy30k_long %>% group_by(SampleID) %>% summarise(Total = sum(Abundance_Vegan_Rarefied))
asv_rarefy30k_long <- asv_rarefy30k_long %>% group_by(SampleID) %>% mutate(RA = (Abundance_Vegan_Rarefied/30000)*100)
Read_Count_rarefy30k_RA <- asv_rarefy30k_long %>% group_by(SampleID) %>% summarise(Total = sum(RA))
setwd("~/Dropbox/UTS/PhD/Projects/Chapter2_RoseBay/Data_Analysis/Data/16S/Data")
write_csv(asv_rarefy30k_long,'asv_rarefy30k_long.csv')
```
```{r Rarefied30K NMDS Data preparation}
library(tidyverse)
library(vegan)
library(dplyr)
library(reshape2)
setwd("~/Dropbox/UTS/PhD/Projects/Chapter2_RoseBay/Data_Analysis/Data/16S/Data")
sample_data <- read_csv('SMD.rarefied.csv')
asv_rarefy30k_long <- read_csv("asv_rarefy30k_long.csv")
Rarefied30K_subset <- asv_rarefy30k_long %>% dplyr::select(ASV_name, Abundance_Vegan_Rarefied, SampleID)
Rarefied30K_subset <- Rarefied30K_subset %>% filter(!Abundance_Vegan_Rarefied ==0)
Rarefied30K.asvs.wide.OFG <- reshape2::dcast(Rarefied30K_subset , SampleID~ASV_name, value.var = "Abundance_Vegan_Rarefied", fill=0)
rownames(Rarefied30K.asvs.wide.OFG) <- Rarefied30K.asvs.wide.OFG$SampleID
Rarefied30K_nmds_df <- Rarefied30K.asvs.wide.OFG [,-c(1)]
```
```{r 30KRarefied NMDS}
Rarefied30K.nMDS <- metaMDS(Rarefied30K_nmds_df, distance = "bray", try =99, trymax=100, autotransform = F)
#stressplot(Rarefied30K.nMDS)
#plot(Rarefied30K.nMDS)
names(Rarefied30K.nMDS)
Rarefied30K.nMDS.points <- cbind(Rarefied30K.asvs.wide.OFG[,c(1)], as.data.frame(Rarefied30K.nMDS$points))
colnames(Rarefied30K.nMDS.points)[1] <- "SampleID"
Rarefied30K.nMDS.points <- Rarefied30K.nMDS.points %>% mutate(code=as.character(SampleID)) %>% left_join(sample_data)
Rarefied30K.nMDS.points$Transect <- as.character(Rarefied30K.nMDS.points$Transect)
```
```{r 30KRarefied MDS}
library(tidyverse)
MDS.Rarefied30K <- ggplot(Rarefied30K.nMDS.points) +
geom_point(aes(x=MDS1, y=MDS2, color = `Distance_Offshore`, size=20)) +
NathanTheme
#MDS.Rarefied30K <- MDS.Rarefied30K + scale_colour_manual(values=c("0"="#E66101", "1"="#4DAC26" , "2"="#0571B0", "3"="#cc1616", "4"="#b625e3"))
MDS.Rarefied30K
```
```{r NMDS Data preparation}
library(tidyverse)
library(vegan)
library(dplyr)
library(reshape2)
Abundance.subset <- css_df %>% dplyr::select(ASV_name, Abundance, SampleID)
Abundance.subset <- Abundance.subset %>% filter(!Abundance ==0)
abundance.asvs.wide.OFG <- reshape2::dcast(Abundance.subset, SampleID~ASV_name, value.var = "Abundance", fill=0)
rownames(abundance.asvs.wide.OFG ) <- abundance.asvs.wide.OFG $SampleID
abundance_nmds_df <- abundance.asvs.wide.OFG [,-c(1)]
```
```{r NMDS}
Abundance.nMDS <- metaMDS(abundance_nmds_df, distance = "bray", try =99, trymax=100, autotransform = F)
stressplot(Abundance.nMDS)
plot(Abundance.nMDS)
names(Abundance.nMDS)
Abundance.nMDS.points<-cbind(asvs.wide.OFG[,c(1)], as.data.frame(Abundance.nMDS$points))
colnames(Abundance.nMDS.points)[1] <- "SampleID"
Abundance.nMDS.points<-Abundance.nMDS.points %>% mutate(code=as.character(SampleID)) %>% left_join(sample_data)
Abundance.nMDS.points$Transect <- as.character(Abundance.nMDS.points$Transect)
```
```{r MDS}
library(tidyverse)
MDS.Abundance <- ggplot(Abundance.nMDS.points, aes(x=MDS1, y=MDS2, color = `Transect`)) +
geom_point(aes(size=0.4, alpha=0.5)) +
theme_bw()
MDS.Abundance <- MDS.Abundance + scale_colour_manual(values=c("0"="#E66101", "1"="#4DAC26" , "2"="#0571B0", "3"="#cc1616", "4"="#b625e3"))
MDS.Abundance
```
```{r MDS comparison}
MDS.Abundance
MDS.CSS
```