@@ -77,14 +77,18 @@ training_dir <- "/workspaces/dsp_metagenomics_training/"
7777
7878``` {r}
7979# Abundance table
80- abundance <- readRDS(file = paste0(training_dir, "data/MetaphlanAbundance_Species.rds"))
80+ abundance <- readRDS(file = paste0(training_dir,
81+ "data/MetaphlanAbundance_Species.rds"))
8182rownames(abundance) <- gsub("_SRR_db1.metaphlan", "", rownames(abundance))
8283
8384# Taxonomical annotation
84- annotation <- readRDS(file = paste0(training_dir, "data/MetaphlanAnnotations_Species.rds"))
85+ annotation <- readRDS(file = paste0(training_dir,
86+ "data/MetaphlanAnnotations_Species.rds"))
8587
8688# Metadata
87- metadata <- read.table(file = paste0(training_dir, "data/metadata.tsv"), header = TRUE,
89+ metadata <- read.table(file = paste0(training_dir,
90+ "data/metadata.tsv"),
91+ header = TRUE,
8892 sep = "\t",
8993 quote = "",
9094 row.names = NULL)
@@ -95,8 +99,8 @@ rownames(metadata) <- metadata$Sample
9599## Take only samples with sequencing and metadata information
96100
97101``` {r}
98- abundance <- abundance[rownames(abundance) %in%
99- rownames(metadata),]
102+ abundance <- abundance[rownames(abundance) %in%
103+ rownames(metadata), ]
100104```
101105
102106
@@ -105,8 +109,8 @@ abundance <- abundance[rownames(abundance) %in%
105109``` {r}
106110results_dir <- paste0(training_dir, "results/report/03_Abundance-overview/")
107111
108- dir.create(results_dir,
109- recursive = TRUE,
112+ dir.create(results_dir,
113+ recursive = TRUE,
110114 showWarnings = FALSE)
111115```
112116
@@ -115,14 +119,14 @@ dir.create(results_dir,
115119``` {r}
116120taxa_total <- colSums(abundance)
117121
118- top_taxa <- names(sort(taxa_total,
122+ top_taxa <- names(sort(taxa_total,
119123 decreasing = TRUE))[1:10]
120124```
121125
122126## Filter to top 10 taxa
123127
124128``` {r}
125- abundance_top10 <- as.data.frame(abundance) %>%
129+ abundance_top10 <- as.data.frame(abundance) %>%
126130 select(all_of(top_taxa))
127131```
128132
@@ -142,13 +146,13 @@ abundance_long <- abundance_top10 %>%
142146 values_to = "Abundance")
143147
144148abundance_long$Species_name <- annotation$species[match(abundance_long$Species,
145- rownames(annotation))]
149+ rownames(annotation))]
146150
147151abundance_long$Area <- metadata$Area[match(abundance_long$Sample,
148152 metadata$Sample)]
149153
150154abundance_long$Sampling <- metadata$Sampling[match(abundance_long$Sample,
151- metadata$Sample)]
155+ metadata$Sample)]
152156```
153157
154158## Stack bar plot of the top-10 most dominant species
@@ -167,33 +171,25 @@ abundance_long$Sampling <- metadata$Sampling[match(abundance_long$Sample,
167171``` {r}
168172abundance_long <- abundance_long %>%
169173 arrange(Sampling, Sample) %>%
170- mutate(Sample = factor(Sample,
174+ mutate(Sample = factor(Sample,
171175 levels = unique(Sample)))
172176
173- pbar <- ggplot(abundance_long, aes(x = Sample,
177+ pbar <- ggplot(abundance_long, aes(x = Sample,
174178 y = Abundance,
175179 fill = Species_name)) +
176-
177180 geom_bar(stat = "identity",
178181 position = "stack",
179182 show.legend = TRUE) +
180-
181183 scale_fill_brewer(palette = "Paired") +
182-
183184 labs(title = "Top-10 Most Abundant Species",
184185 x = "Sample",
185186 y = "Relative Abundance (%)") +
186-
187187 theme_bw() +
188-
189- theme(axis.text.x = element_text(angle = 45,
188+ theme(axis.text.x = element_text(angle = 45,
190189 hjust = 1)) +
191-
192190 scale_y_continuous(expand = c(0, 0)) +
193-
194191 scale_x_discrete(expand = c(0, 0)) +
195-
196- facet_wrap(. ~ Area,
192+ facet_wrap(. ~ Area,
197193 scales = "free")
198194
199195pbar
@@ -223,32 +219,27 @@ abundance_long <- abundance_top10 %>%
223219
224220abundance_summary <- abundance_long %>%
225221 group_by(Area, Sampling, Species) %>%
226- summarise(MeanAbundance = mean(Abundance),
222+ summarise(MeanAbundance = mean(Abundance),
227223 .groups = "drop") #ungroup
228224
229- abundance_summary$Species_name <-
225+ abundance_summary$Species_name <-
230226 annotation$species[match(abundance_summary$Species,
231227 rownames(annotation))]
232228
233- pbar_sum <- ggplot(abundance_summary,
234- aes(x = interaction(Area,
235- Sampling,
229+ pbar_sum <- ggplot(abundance_summary,
230+ aes(x = interaction(Area,
231+ Sampling,
236232 sep = " | "),
237- y = MeanAbundance,
233+ y = MeanAbundance,
238234 fill = Species_name)) +
239-
240- geom_bar(stat = "identity",
241- position = "stack",
235+ geom_bar(stat = "identity",
236+ position = "stack",
242237 show.legend = TRUE) +
243-
244238 scale_fill_brewer(palette = "Paired") +
245-
246239 theme_bw() +
247-
248240 labs(title = "Mean Abundance of Top 10 Taxa by Area and Sampling",
249241 x = "Area | Sampling",
250242 y = "Mean Relative Abundance (%)") +
251-
252243 theme(axis.text.x = element_text(hjust = 0.5),
253244 strip.text = element_text(face = "bold")) +
254245 scale_y_continuous(expand = c(0, 0)) +
@@ -257,8 +248,8 @@ pbar_sum <- ggplot(abundance_summary,
257248pbar_sum
258249
259250# Save it as a png
260- ggsave(paste0(results_dir, "02_top10_species_per_area_sampling.png"),
261- width = 8, height = 6, dpi = 300)
251+ ggsave(paste0(results_dir, "02_top10_species_per_area_sampling.png"),
252+ width = 8, height = 6, dpi = 300)
262253
263254```
264255
0 commit comments