-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProjet-1.Rmd
485 lines (280 loc) · 11.6 KB
/
Projet-1.Rmd
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
---
title: "Projet 1"
author: "Ruben"
date: "22/02/2022"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Librairies
```{r,message=F,warning=F}
library(data.table)
library(phyloseq)
library(tidyverse)
library(dplyr)
library(ggpubr)
library(rstatix)
library(ggplot2)
library(ggsignif)
library(devtools)
library(ggthemes)
library('ggforce')
source("functions.R")
```
## Import des données
```{r,message=F,warning=F}
load("curated_v3_otu_tax.rda")
otu <- data.frame(OTU)
taxo <- data.frame(TAX)
```
## On garde les données
```{r,message=F,warning=F}
otus <- otu
tax <- taxo
map <- sampleMetadata
## on inverse non_westernized
map <- map %>%
mutate(., westernized =
case_when(non_westernized == 'yes' ~ "no",
non_westernized == 'no' ~ "yes"))
```
## On garde les données
```{r,message=F,warning=F}
bb <- map %>%
filter(westernized == "no" & age_category=="newborn")
```
## Regrouper les maladies
```{r,message=F,warning=F}
# On va regrouper les facteurs dans la colonne "disease car il y a beaucoup de facteurs"
map <- transform_disease(map)
```
## Unicité échantillons et patients
```{r,message=F,warning=F}
isUnique <- function(vector){
return(!any(duplicated(vector)))
}
isUnique(map$sample_id)
isUnique(map$subject_id)
nrow(map)
ncol(otus)
```
Les échantillons et les patients ne sont pas uniques
On a 20 000 lignes environ
```{r,message=F,warning=F}
## on enlève les échantillons dans tax qui contiennent des NA
tax <- tax[ , colSums(is.na(tax)) == 0]
ncol(otus)
nrow(map)
```
```{r,message=F,warning=F}
#index <- which(map$body_site == 'stool')
#map <- map[index,]
# on sélectionne les échantillons uniques, par leur ID
map <- map[!duplicated(map$sample_id), ]
# si un patient a plusieurs échantillons, on prend celui qui aura le nombre de reads le plus élevé
map <- unique(setDT(map)[order(subject_id, -number_reads)], by = "subject_id")
isUnique(map$sample_id)
isUnique(map$subject_id)
nrow(map)
```
Les échantillons et les patients sont uniques
On a 12661 échantillons = patients uniques
# on sélectionne les microbiotes intestinaux seulement
```{r,message=F,warning=F}
index <- which(map$body_site == 'stool')
map <- map[index,]
nrow(map)
```
## Contrôle Qualité des données
```{r,message=F,warning=F}
# on prend les échantillons en commun entre les otus et metamap
common.ids <- intersect(map$sample_id, t(colnames(otus)))
# on prend les échantillons en commun seulement
otus <- otus[,common.ids]
rownames(map) <- map$sample_id
map <- merge(x = map, y = data.frame(common.ids), by.x = "sample_id", by.y="common.ids")
# d'abord on enlève les souches qui sont présentes dans aucun échantillon
map <- map[apply(map, 1, function(x) !all(x==0)),]
# d'abord on drop les colonnes qui sont redondantes (study_condition = disease)
map$study_condition <- NULL
map$study_name <- NULL
map$infant_age <- NULL
# on réordonne les 2 tableau
nrow(map)
```
Le dataset a 9515 avec, et 127 variables
## Preparation des données
```{r,message=F,warning=F}
# est-ce que les id entre map et otus sont égaux ?
setequal(colnames(otus),t(map$sample_id))
# est-ce que les id entre otus et taxonomie sont égaux ?
setequal(rownames(otus),rownames(tax))
nrow(map)
save(otus, tax, map, file="study_after_QC.rda")
```
## Descriptif des données
```{r,message=F,warning=F}
library(gridExtra)
plot <- function(category, title, fill){
map_sub <- map %>%
select(category) %>%
table(., useNA = "always") %>%
as.data.frame(.) %>%
filter(Freq!=0)
p <- ggplot(map_sub, aes(fill=., y=Freq, x=.), binaxis='y', stackdir='center') +
geom_bar(stat="identity",position="stack")+
theme_minimal() +
geom_text(aes(label=Freq,
vjust=0.3,
hjust=ifelse(Freq>1200, 1.3,-0.3),
angle = 90),
size=5,
color="black"
)+
labs(x = NULL, y = NULL, title = "", fill= fill)
## si la catégorie est age on change les labels x qui sont mal ordonnés
if(category == 'age_category'){
p <- p + scale_x_discrete(limits=c("newborn","child","schoolage","adult","senior"))
}
return(p + guides(fill = FALSE))
}
p_age_category <- plot('age_category','Patients Counts by Age Category', 'Age Category')
#p_gender <- plot('gender','Patients Counts by Gender', 'Gender')
p_antibiotics_current_use <- plot('antibiotics_current_use','Patients Counts by Antibiotics Current Use', 'Antibiotics Current Use')
p_disease <- plot('disease','Patients Counts by Health Condition', 'Health Condition')
p_westernized <- plot('westernized','Patients Counts by Westernized', 'Westernized')
figure <- ggarrange(p_disease,
ggarrange(p_age_category, p_westernized, ncol = 2,
labels = c("age_category","westernized")),
nrow = 2,
labels = "disease")
figure
```
```{r,message=F,warning=F}
# On s'intéresse au genre bifidobactérium d'abord donc on les sélectionne
index <- which(tax[,"Genus"] =='Bifidobacterium')
bifid_especes <- rownames(tax)[index]
# Calcul des abondances bifid seulement
somme_bifid <- otus %>%
as.data.frame() %>%
dplyr::filter(rownames(.) %in% bifid_especes) %>%
colSums(., na.rm=TRUE)
# Calcul des abondances totales
somme_total <- otus %>%
colSums(., na.rm=TRUE) %>%
data.frame(.)
bifid_normalized <- somme_bifid/somme_total
# on merge
bifid <- data.frame(cbind(sample_id = rownames(somme_total), reads_total = somme_total, reads_bifid = bifid_normalized))
colnames(bifid) <- c('sample_id', 'reads_total', 'reads_bifid')
map <- merge(map,bifid, by = "sample_id")
```
## Analyse descriptive
Boxplot antibiotiques
```{r,message=F,warning=F}
### on garde que les adultes et on enlève les NA
map_antibio <- map %>%
filter((antibiotics_current_use=='yes' | antibiotics_current_use=='no') & age_category=='adult')
p <- ggboxplot(map_antibio, x="antibiotics_current_use", y="reads_bifid", fill="antibiotics_current_use",outlier.shape = NA )
p <- p + scale_fill_brewer(palette="Dark2",labels = c("No (4921 adults)", "Yes (52 adults)")) +
theme_minimal() +
labs(x = NULL, y = NULL, title = "Relative Abundance of Bifidobacterium",fill= "Takes Antibiotics") +
coord_cartesian(ylim=c(0,0.3))
p + stat_compare_means(label.y = 0.28)
map_antibio <- map %>%
filter(age_category=='adult' & (is.na(antibiotics_current_use) | antibiotics_current_use=='yes'))
p <- ggboxplot(map_antibio, x="antibiotics_current_use", y="reads_bifid", fill="antibiotics_current_use",outlier.shape = NA )
p <- p + scale_fill_brewer(palette="Dark2",labels = c("No (4921 adults)", "NA (3571 adults)")) +
theme_minimal() +
labs(x = NULL, y = NULL, title = "Relative Abundance of Bifidobacterium",fill= "Takes Antibiotics") +
coord_cartesian(ylim=c(0,0.3))
p + stat_compare_means(label.y = 0.28)
map_save <- map
```
Boxplot western/westernized
```{r,message=F,warning=F}
map <- map_save
title <- "westernized"
#title <- "Relative Abundance of Bifidobacterium"
# on prend les échantillons en commun entre les otus et metamap
common.ids <- intersect(map$sample_id, t(colnames(otus)))
# on prend les échantillons en commun seulement
otus <- otus[,common.ids]
rownames(map) <- map$sample_id
map <- merge(x = map, y = data.frame(common.ids), by.x = "sample_id", by.y="common.ids")
# transformation facteur de la col non_westernized
map$westernized = factor(map$westernized, levels=c("no", "yes"))
# on garde que ceux qui ne consomment pas d'antibio
map_adult_no_antibio <- map[which(map$antibiotics_current_use=="no" & map$age_category=="adult"),]
p<-ggplot(map_adult_no_antibio, aes(x=westernized, y=reads_bifid, fill=westernized)) + geom_violin(outlier.shape = NA )
p <- p +
scale_fill_brewer(palette="Dark2", labels = c("No (4774 adults)", "Yes (147 adults)")) +
theme_minimal() +
labs(x = NULL, y = NULL, title = title,fill= "Westernized") +
coord_cartesian(ylim=c(0,0.84)) +
stat_compare_means(label = "p.signif", method = "wilcox", ref.group = "no", label.y = 0.8)
p_wetsernized <- p + theme(legend.position = "none")
p_wetsernized
```
Boxplot healthy/disease
```{r,message=F,warning=F, fig.width =6.5}
title <- "disease"
# on enlève les arthrithis car il y en a que 3
p<-ggplot(map_adult_no_antibio, aes(x=disease, y=reads_bifid, fill=disease))+ geom_violin(outlier.shape = NA) + labs(fill= "Disease (adults count)")
p <- p + scale_fill_brewer(palette="Dark2", labels = c("Control (4348)","Adenoma (29)", "Colorectal (74)", "Metabolic (320)", "Bowel (45)","Arthritis (85)", "BD (20)")) +
theme_minimal() + labs(x = NULL, y = NULL, title = title)+coord_cartesian(ylim=c(0,0.9)) + stat_compare_means(label.y = 0.78, label.x ="Adenoma") +
stat_compare_means(label = "p.signif", method = "wilcox", ref.group = "Control", label.y = 0.60)
p_disease <- p + theme(legend.position = "none")
p_disease
```
Distribution of Bifidobactérium en fonction de l'âge (seulement contrôles)
```{r,message=F,warning=F}
title <- "age category"
# on sélectionne que les contrôles (sujets sains) pour éviter des biais
map_age <- map %>%
filter(antibiotics_current_use =="no")
map_age$age_category <- factor(map_age$age_category, levels=c("newborn","child","schoolage","adult","senior"))
p<-ggplot(map_age, aes(x = age_category,
y = reads_bifid, fill=age_category )) +
geom_violin(outlier.shape = NA) + labs(fill= "Age category")
p <- p + scale_fill_brewer(palette="Dark2", labels = c("newborn (137)","child (160)","schoolage (88)","Adult (4921)", "Senior (422)")) +
theme_minimal() + labs(x = NULL, y = NULL, title = title) + labs(color= "Age Category") +
coord_cartesian(ylim=c(0,1)) + stat_compare_means(label.y = 0.90, label.x ="child") +
scale_x_discrete(limits=c("newborn","child","schoolage","adult","senior")) +
stat_compare_means(label = "p.signif", method = "wilcox", ref.group = "newborn", label.y = 0.78)
p
p_age <- p + theme(legend.position = "none")
p_age
## export the 3 plots
#ggplot(map,
# aes(x = age_category,
# y = reads_bifid, fill=age_category )) +
# geom_boxplot(outlier.shape = NA) +
# labs(x = NULL, y = NULL, title = "Relative Abundance of Bifidobacterium, no Antibiotics Takers") + labs(color= "Age #Category") +
# scale_color_brewer(palette = "Set2", labels = c("newborn (137)","child (160)","schoolage (88)","Adult (4921)", "Senior #(422)")) +
# scale_x_discrete(limits=c("newborn","child","schoolage","adult","senior")) +
# theme_minimal() +
# coord_cartesian(ylim=c(0,1)) +
# stat_compare_means(label.y = 0.50,label.x = "child")
```
Plot which groups the 3 last ones
```{r,message=F,warning=F, fig.height = 9, fig.width = 5}
figure <- ggarrange(p_wetsernized, p_age, p_disease,
labels = c("A","B","C"),
ncol =1,
nrow = 3)
annotate_figure(figure, top = text_grob("Relative abundance of Bifidobacterium", size = 18))
#figure
```
```{r,message=F,warning=F}
# on enlève les na pour gender
map_gender <- map[!is.na(map$gender),]
map_gender <- map_gender[which(map_gender$age_category=="adult"),]
# on sélectionne que les adultes (encore une fois)
p<-ggplot(map_gender, aes(x=gender, y=reads_bifid, fill=gender))+ geom_boxplot(outlier.shape = NA) + labs(fill= "Gender")
p <- p + scale_fill_brewer(palette="Dark2") + theme_minimal() + labs(x = NULL, y = NULL, title = "Relative Abundance of Bifidobacterium among Adults, no Antibiotics Takers")+coord_cartesian(ylim=c(0,0.29)) + stat_compare_means(label.y = 0.25)
p
```