-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kegg_enrichment.R
377 lines (247 loc) · 12.1 KB
/
Kegg_enrichment.R
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
## Author: Viviana Freire-Zapata
## Date: 12/12/21
## Topic: KEGG enrichment analysis
## Required libraries
library(tidyverse)
library(clusterProfiler)
library(RColorBrewer)
library(here)
## This function will set up your working directory
here()
## Loading data
metaG <- read_csv( here('input', 'metaG-raw-copy-numbers.csv'))
metaT_DEG <- read_csv(here('input', 'pre-drought-vs-drought.csv')) %>%
drop_na(padj)
metaB <- read_csv(here('input' , 'KEGG-table-corrected.csv'))
metadata <- read_csv(here('input', 'metadata-metaG-metaT-metaB.csv'))
metadata_metaB <- read_csv(here('input', 'metadata_metaB.csv'))
## Creating vector with Human pathways to be deleted
delete <- c("Pathways of neurodegeneration - multiple diseases","Amyotrophic lateral sclerosis",
"Alzheimer disease", "Huntington disease", "Parkinson disease", "Pathways of neurodegeneration - multiple diseases",
"Amyotrophic lateral sclerosis", "Alzheimer disease", "Huntington disease",
"Pathways in cancer", "Prion disease", "Thermogenesis", "Central carbon metabolism in cancer",
"Shigellosis", "Human papillomavirus infection", "Diabetic cardiomyopathy", "Coronavirus disease - COVID-19",
"Chemical carcinogenesis - reactive oxygen species", "Taurine and hypotaurine metabolism")
## KEGG ENRICHMENT ANALYSIS
###### METAGENOME ######
## Organizing data for analysis
## Transforming data to presence/absence form
for(i in 3:36){
metaG[metaG[,i] > 0, i] <- 1
}
## Treatment: Drought and time
## Calculating number of samples per treatment in which KO's were present
metadata_G_drought <- metadata %>%
filter(Dataset == "metaG") %>%
filter(Condition == "Drought")
metaG_drought <- metaG %>%
select(Feature, all_of(metadata_G_drought$SampleID)) %>%
mutate(Time0 = select(., contains("time0")) %>% rowSums(na.rm = TRUE)) %>%
mutate(Time6h = select(., contains("time6")) %>% rowSums(na.rm = TRUE)) %>%
mutate(Time48h = select(., contains("drought_48")) %>% rowSums(na.rm = TRUE))
## Creating tables per time
## Filtering KO's that were present in all samples per time
metaG_drought_0 <- metaG_drought %>%
select(c(Feature, Time0)) %>%
filter(Time0 == 6) %>%
mutate(Time = "t0", Condition = "drought") %>%
select(-Time0)
metaG_drought_6 <- metaG_drought %>%
select(c(Feature, Time6h)) %>%
filter(Time6h == 7) %>%
mutate(Time = "t6", Condition = "drought") %>%
select(-Time6h)
metaG_drought_48 <- metaG_drought %>%
select(c(Feature, Time48h)) %>%
filter(Time48h == 5) %>%
mutate(Time = "t48", Condition = "drought") %>%
select(-Time48h)
##Joining tables
drought_ko <- rbind(metaG_drought_0, metaG_drought_6, metaG_drought_48)
## Treatment: Pre_Drought and time
## Calculating number of samples per treatment in which KO's were present
metadata_G_pre <- metadata %>%
filter(Dataset == "metaG") %>%
filter(Condition == "PreDrought")
metaG_pre <- metaG %>%
select(Feature, all_of(metadata_G_pre$SampleID)) %>%
mutate(Time0 = select(., contains("time0")) %>% rowSums(na.rm = TRUE)) %>%
mutate(Time6h = select(., contains("time6")) %>% rowSums(na.rm = TRUE)) %>%
mutate(Time48h = select(., contains("drought_48")) %>% rowSums(na.rm = TRUE))
## Creating tables per time
## Filtering KO's that were present in all samples per time
metaG_pre_0 <- metaG_pre %>%
select(c(Feature, Time0)) %>%
filter(Time0 == 5) %>%
mutate(Time = "t0", Condition = "predrought") %>%
select(-Time0)
metaG_pre_6 <- metaG_pre %>%
select(c(Feature, Time6h)) %>%
filter(Time6h == 5) %>%
mutate(Time = "t6", Condition = "predrought") %>%
select(-Time6h)
metaG_pre_48 <- metaG_pre %>%
select(c(Feature, Time48h)) %>%
filter(Time48h == 6) %>%
mutate(Time = "t48", Condition = "predrought") %>%
select(-Time48h)
##Joining tables
pre_drought_ko <- rbind(metaG_pre_0, metaG_pre_6, metaG_pre_48)
## Joining drought and predrought ko tables
metaG_ko <- rbind(drought_ko, pre_drought_ko)
metaG_ko$Feature <- str_remove(metaG_ko$Feature, "KO:")
## ENRICHMENT ANALYSIS
metagenome_enrich <- compareCluster(Feature~Condition+Time, data = metaG_ko, fun = enrichKEGG,
organism = "ko", minGSSize = 10)
metagenome_enrich@compareClusterResult$Cluster <- factor(metagenome_enrich@compareClusterResult$Cluster,
levels = c("drought.t0", "drought.t6",
"drought.t48", "predrought.t0",
"predrought.t6","predrought.t48"))
## Plotting results
metaG_plot <- dotplot(metagenome_enrich, showCategory = 20,
title = "Metagenome",
font.size = 12) +
facet_wrap(~Condition, scales = 'free_x')
metaG_plot
ggsave(filename = 'output/KEGG_enrichment_metagenome_per_time.png', metaG_plot, height = 10, width = 10)
## Saving enrichment table results
genome_table_results <- metagenome_enrich@compareClusterResult
genome_table_results$GeneRatio <- paste0(' ', genome_table_results$GeneRatio)
genome_table_results$BgRatio <- paste0(' ', genome_table_results$GeneRatio)
write_csv(genome_table_results, 'output/metagenome_enrichment_result_table.csv')
## Treatment: Drought
## Calculating number of samples per treatment in which KO's were present
metaG_drought_only <- metaG %>%
select(Feature, all_of(metadata_G_drought$SampleID)) %>%
mutate(Drought = select(., contains("_drought_")) %>% rowSums(na.rm = TRUE))
## Creating table
## Filtering KO's that were present in all samples per condition
metaG_drought_ko <- metaG_drought_only %>%
select(c(Feature, Drought)) %>%
filter(Drought == 18) %>% # you can change this parameter to be less restrictive
mutate(Condition = "drought") %>%
select(-Drought)
## Treatment: Pre_Drought
## Calculating number of samples per treatment in which KO's were present
metaG_pre_only <- metaG %>%
select(Feature, all_of(metadata_G_pre$SampleID)) %>%
mutate(Predrought = select(., contains("_pre_")) %>% rowSums(na.rm = TRUE))
## Creating table
## Filtering KO's that were present in all samples per condition
metaG_pre_ko <- metaG_pre_only %>%
select(c(Feature, Predrought)) %>%
filter(Predrought == 16) %>%
mutate(Condition = "predrought") %>%
select(-Predrought)
## Joining drought and predrought ko tables
metaG_ko_condition <- rbind(metaG_drought_ko, metaG_pre_ko)
metaG_ko_condition$Feature <- str_remove(metaG_ko_condition$Feature, "KO:")
## ENRICHMENT ANALYSIS
metagenome_enrich_condition <- compareCluster(Feature~Condition, data = metaG_ko_condition, fun = enrichKEGG,
organism = "ko", minGSSize = 5)
## Plotting results
metaG_plot_condition <- dotplot(metagenome_enrich_condition, showCategory = 20,
title = "Metagenome_condition",
font.size = 12) +
facet_wrap(~Condition, scales = 'free_x')
metaG_plot_condition
ggsave(filename = 'output/KEGG_enrichment_metagenome_per_condition.png', metaG_plot, height = 10, width = 10)
## Saving enrichment table results
condition_results <- metagenome_enrich_condition@compareClusterResult
condition_results$GeneRatio <- paste0(' ', condition_results$GeneRatio)
condition_results$BgRatio <- paste0(' ', condition_results$GeneRatio)
write_csv(genome_table_results, 'output/metagenome_enrichment_result_table_condition.csv')
###### METATRASNCRIPTOME ######
## Drought vs Pre_drought
DEG_significant <- metaT_DEG %>%
filter(padj < 0.05) %>% ## setting a padj < 0.05 for significant DEG, you can change this if you want
mutate(DE = ifelse(log2FoldChange > 0, "upregulated", "downregulated")) %>%
rename(Feature = ...1) %>%
select(Feature, DE)
DEG_significant$Feature <- str_remove(DEG_significant$Feature, "KO:")
## ENRICHMENT ANALYSIS
metaT_enrich <- compareCluster(Feature~DE, data = DEG_significant, fun = enrichKEGG,
organism = "ko", minGSSize = 10)
## Filtering out Human KEGG pathways
metaT_enrich@compareClusterResult <- metaT_enrich@compareClusterResult %>%
filter(!(Description %in% delete))
metaT_plot <- dotplot(metaT_enrich, showCategory = 20,
title = "Metatranscriptome",
font.size = 12)+
facet_wrap(~DE, scales = 'free_x')
metaT_plot
ggsave(filename = 'output/KEGG_enrichment_metatranscriptome.png', metaT_plot, height = 12, width = 10)
trans_table_results <- metaT_enrich@compareClusterResult
trans_table_results$GeneRatio <- paste0(' ', trans_table_results$GeneRatio)
trans_table_results$BgRatio <- paste0(' ', trans_table_results$GeneRatio)
write_csv(trans_table_results, 'output/metatranscriptome_enrichment_result_table.csv')
###### METABOLOME ######
## Organizing data
df <- read_csv(here('input', 'Report_processed_MolecFormulas.csv')) %>%
pivot_longer(all_of(metadata_metaB$SampleID), names_to = 'SampleID', values_to = 'NormIntensity') %>%
filter(NormIntensity != 0)
df_kegg <- left_join(df, metaB, by = c('Mass', 'MolecularFormula'))
df_kegg_meta <- left_join(df_kegg, metadata_metaB, by = 'SampleID')
df_kegg_meta_l <- df_kegg_meta %>%
pivot_wider(names_from = Condition, values_from = Condition, names_prefix = 'GRP_') %>%
select(Mass, contains('KEGG_'), contains('GRP_')) %>%
group_by(Mass) %>%
fill(contains('GRP_'), .direction = 'downup') %>%
distinct() %>%
unite(Presence, contains('GRP_'), sep = ', ', na.rm = TRUE)
## Creating KEGG universe for metabolite analysis
## Loading .csv file
compound_df <- read.csv("KEGG_compound_db.csv")
path2id <- compound_df %>%
select(KEGG_pathway, KEGG_id) %>%
separate_rows(KEGG_pathway, sep = ';') %>%
filter(!is.na(KEGG_pathway))
## Metabolite vector of interest
## Drought
metabolite_drought <- df_kegg_meta_l %>%
ungroup() %>%
filter(Presence == "Drought") %>% #selecting KEGG compounds unique in drought
select(KEGG_id) %>%
filter(!is.na(KEGG_id)) %>%
mutate(Condition = "drought")
## Pre_Drought
metabolite_pre <- df_kegg_meta_l %>%
ungroup() %>%
filter(Presence == "PreDrought") %>%#selecting KEGG compounds unique in predrought
select(KEGG_id) %>%
filter(!is.na(KEGG_id)) %>%
mutate(Condition = "predrought")
# Joining tables
metabolite_kegg <- rbind(metabolite_drought, metabolite_pre)
## Enrichment analysis
metabolite_enrich <- compareCluster(KEGG_id~Condition, data = metabolite_kegg, fun = enricher,
TERM2GENE = path2id, minGSSize = 5)
metaB_plot <- dotplot(metabolite_enrich, showCategory = 20,
title = "Metabolite",
font.size = 12)
metaB_plot
ggsave(filename = 'output/KEGG_metabolite_erichment.png', metaB_plot)
metabolite_table_result <- metabolite_enrich@compareClusterResult
metabolite_table_result$GeneRatio <- paste0(' ', metabolite_table_result$GeneRatio)
metabolite_table_result$BgRatio <- paste0(' ', metabolite_table_result$BgRatio)
write_csv(metabolite_table_result, 'output/metabolite_enrichment_result.csv')
## JOINING metaG and metaB results in one graph
## Analysis by condition
gene <- metagenome_enrich_condition@compareClusterResult %>%
mutate(dataset = "metaG")
metabo <- metabolite_enrich@compareClusterResult %>%
mutate(dataset = "metaB")
join <- rbind(gene, metabo) %>%
separate(GeneRatio, c("numerator", "denominator"), sep = "/") %>%
filter(p.adjust < 0.05) %>%
mutate(numerator = as.numeric(numerator),
denominator = as.numeric(denominator),
GeneRatio = numerator/ denominator) %>%
group_by(dataset) %>%
slice_max(order_by = c(Count, GeneRatio), n = 30)
join_plot <- ggplot(join, aes( x= GeneRatio, y = Description, color = dataset, size = Count))+
geom_point()+
theme_bw()+
facet_wrap(~Condition)
join_plot
ggsave(filename = 'output/KEGG_join_plot.png', join_plot, width = 10, height = 6)