-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFull Analysis Steps.R
2737 lines (2240 loc) · 125 KB
/
Full Analysis Steps.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
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##Title: Full Analysis
##Author: Emily Oosterhout
##
setwd("C:/DATA FOOD COMPONENT ANALYSIS/RP2_ChemIBDFood")
#Import libraries
library(readxl)
library(dplyr)
library(tidyverse)
library(data.table)
library(reshape2)
library(patchwork)
library(rlang)
library(writexl)
#packages for testing/visualizing data distribution
library(ggplot2)
library(ggbreak)
library(ggpubr)
library(FSA)
library(ggsignif)
library(ggrepel)
library(RColorBrewer)
library(gridExtra)
library(jtools)
library(VennDiagram)
library(GGally)
library(MetBrewer)
#packages for linear regression/statistical testing
library(stats)
library(outliers)
#packages for correlation analysis
library(Hmisc)
library(reshape2)
library(RcmdrMisc)
library(psych)
library(corrplot)
library(gdata)
library (plyr)
library(foreach)
library(ppcor)
## Functions ##
# Function to set outliers as NA within each column using Tukey's fences
remove_outliers <- function(data, multiplier = 1.5) {
cleaned_data <- data
# Loop through each column in the dataframe
for (col in names(data)) {
# Calculate the lower and upper fences
q1 <- quantile(data[[col]], 0.25, na.rm = TRUE)
q3 <- quantile(data[[col]], 0.75, na.rm = TRUE)
iqr <- q3 - q1
lower_fence <- q1 - multiplier * iqr
upper_fence <- q3 + multiplier * iqr
# Identify values outside the fences
outliers <- data[[col]] < lower_fence | data[[col]] > upper_fence
# Set outliers as NA in the dataframe
cleaned_data[[col]][outliers] <- NA
}
return(cleaned_data)
}
##=========================================== LOAD DATA, CLEANING NAMES AND SUBSETTING ===============================
data_full <- as.data.frame(read_xlsx('analysis_table.xlsx'))
row.names(data_full) <- data_full$UMCGIBDResearchIDorLLDeepID
# Subset full dataset on plausible intake (sex dependent Willet)
#Males
intake_male <- subset(data_full, data_full$sex == 'male')
intake_male_high <- subset(intake_male, intake_male["SUMOFKCAL"] > 4000)
intake_male <- subset(data_full, data_full$sex == 'male')
intake_male_low <- subset(intake_male, intake_male["SUMOFKCAL"] < 800)
#Females
intake_female <- subset(data_full, data_full$sex == 'female')
intake_female_high <- subset(intake_female, intake_female["SUMOFKCAL"] > 3500)
intake_female <- subset(data_full, data_full$sex == 'female')
intake_female_low <- subset(intake_female, intake_female["SUMOFKCAL"] < 500)
#Merge participants with implausible intake (25 participants)
implausible_intake_male <- rbind(intake_male_high, intake_male_low)
implausible_intake_female <- rbind(intake_female_high, intake_female_low)
implausible_intake <- rbind(implausible_intake_male, implausible_intake_female)
# Remove participants from original dataframe based on presence in imlausible intake
plausible_intake <- data_full[!(rownames(data_full) %in% rownames(implausible_intake)), ]
# Get metabolite data from analysis table using participant list from raw metabolite files
intake <- as.data.frame(read_xlsx('chem_raw_participant_V2.xlsx'))
fecal <- as.data.frame(read_xlsx('fecal_mtb_full.xlsx'))
serum <- as.data.frame(read_xlsx("blood_mtb.xlsx"))
#participant ID's as rownames, for matching correlations
rownames(intake) <- intake$UMCGIBDResearchIDorLLDeepID
rownames(fecal) <- fecal$UMCGIBDResearchIDorLLDeepID
rownames(serum) <- serum$UMCGIBDResearchIDorLLDeepID
#select participant IDs from raw data files
participants_intake <- as.character(intake$UMCGIBDResearchIDorLLDeepID)
participants_fecal <- as.character(fecal$UMCGIBDResearchIDorLLDeepID)
participants_serum <- as.character(serum$UMCGIBDResearchIDorLLDeepID)
#filter full analysis table on metabolite type
intake_mtb <- plausible_intake[plausible_intake$UMCGIBDResearchIDorLLDeepID %in% participants_intake,]
fecal_mtb <- plausible_intake[plausible_intake$UMCGIBDResearchIDorLLDeepID %in% participants_fecal,]
serum_mtb <- plausible_intake[plausible_intake$UMCGIBDResearchIDorLLDeepID %in% participants_serum,]
##========================================== CLEAN INTAKE METABOLITE NAMES TO WORK IN REGRESSION MODEL ===========================================##
intake <- intake[!(rownames(intake) %in% rownames(implausible_intake)), ]
#clean compound names, intake metabolites
names(intake) = gsub(pattern = ":", replacement = "_", x = names(intake))
names(intake) = gsub(pattern = " ", replacement = "_", x = names(intake))
names(intake) = gsub(pattern = "-", replacement = "_", x = names(intake))
names(intake) = gsub(pattern = ",", replacement = "", x = names(intake))
names(intake) = gsub(pattern = '"', replacement = "", x = names(intake))
names(intake) = gsub(pattern = "\\|.*", replacement = "", x = names(intake))
names(intake) = gsub(pattern = "\\(", replacement = "", x = names(intake))
names(intake) = gsub(pattern = "\\)", replacement = "", x = names(intake))
names(intake) = gsub(pattern = "\\+", replacement = "pos", x = names(intake))
names(intake) = gsub(pattern = "\\'", replacement = "", x = names(intake))
names(intake) = gsub(pattern = "\\â±", replacement = "", x = names(intake))
# Get current column names
old_colnames <- colnames(intake)
# Replace numeric column names with letters
new_colnames <- make.names(old_colnames, unique = TRUE)
# Assign new column names to the data frame
colnames(intake) <- new_colnames
row.names(intake) <- intake$UMCGIBDResearchIDorLLDeepID
##===================================== DIFFERENTIAL ABUNDANCE ANALYSIS: INTAKE_IBDvsNon-IBD (LOG TRANSFORMED AND FILTERED DATA) ====================================================
# Columns containing intake metabolites
intake_cols <- grep("^int_", names(intake_mtb), value = TRUE)
metabolites_intake <- intake_mtb[,colnames(intake_mtb) %in% intake_cols]
# Calculate the percentage of non-zero values for each variable
non_zero_pct <- apply(metabolites_intake != 0, 2, mean)
# Filter variables with at least a non-zero value in 20% of the data
filter_20pct <- metabolites_intake[,non_zero_pct >= 0.2]
#add pseudocount to all variables
pseudo <- filter_20pct + 1
# Create a new column specifying IBD (yes/no) for each sample
pseudo_diagnosis <- cbind(intake_mtb$diagnosis, pseudo)
names(pseudo_diagnosis)[1] <- 'diagnosis'
##======================= WILCOXON TEST ======================##
wilcoxon_p <- c() # Initialize empty vector for p-values
# Do "for loop" over selected column names
for (i in 2:1010) {
result <- wilcox.test(pseudo_diagnosis[, i] ~ diagnosis,
data = pseudo_diagnosis)
# Stores p-value to the vector with this column name
wilcoxon_p[[i]] <- result$p.value
}
#store metabolites with raw p-value in new dataframe
wilcoxon_p <- data.frame(metabolites = names(pseudo_diagnosis[,2:1010]),
p_raw = unlist(wilcoxon_p))
wilcoxon_p$p_adjusted <- p.adjust(wilcoxon_p$p_raw, method = "fdr") #add column with p_adj for multiple testing
# prepare a dataframe to plot p values
df <- data.frame(x = c(wilcoxon_p$p_raw, wilcoxon_p$p_adjusted),
type=rep(c("raw", "fdr"),
c(length(wilcoxon_p$p_raw),
length(wilcoxon_p$p_adjusted))))
# make a histrogram of p values and adjusted p values
wilcoxon_plot <- ggplot(df) +
geom_histogram(aes(x=x, fill=type)) +
labs(x = "p-value", y = "Frequency")
wilcoxon_plot
##============================== VOLCANO PLOT OF DIFFERENTIAL ABUNDANCE ANALYSIS ===============================##
#log2 transformation
intake_log <- log2(pseudo_diagnosis[,2:1010])
intake_log <- cbind(diagnosis = pseudo_diagnosis$diagnosis, intake_log)
#calculate the mean of each metabolite in IBD group
IBD <- filter(intake_log, intake_log$diagnosis == 'IBD')
IBD_m = apply(IBD[,2:1010], 2, mean)
#calcuate the mean of each metabolite in LLDEEP group
control <- filter(intake_log, intake_log$diagnosis == 'control')
control_m = apply(control[,2:1010], 2, mean)
#because the data is already log2 transformed, take the difference between the means.
foldchange <- control_m - IBD_m
hist(foldchange, xlab = "log2 Fold Change (IBD vs LLDEEP)")
#add foldchange to df containing p-values
wilcoxon_p$foldchange <- foldchange
# add a column of NAs
wilcoxon_p$intakedifference <- "NO"
# if log2Foldchange > 0.6 and pvalue < 0.05, set as "UP"
wilcoxon_p$intakedifference[wilcoxon_p$foldchange > 0.6 & wilcoxon_p$p_raw < 0.05] <- "YES"
# if log2Foldchange < -0.6 and pvalue < 0.05, set as "DOWN"
wilcoxon_p$intakedifference[wilcoxon_p$foldchange < -0.6 & wilcoxon_p$p_raw < 0.05] <- "DOWN"
# Create a new column "label" to df, that will contain the name of metabolites where intake is different between groups (NA in case they are not)
wilcoxon_p$label <- NA
wilcoxon_p$label[wilcoxon_p$intakedifference != "NO"] <- wilcoxon_p$metabolites[wilcoxon_p$intakedifference != "NO"]
wilcoxon_p$label[wilcoxon_p$intakedifference == "NO"] <- wilcoxon_p$metabolites[wilcoxon_p$intakedifference == "NO"]
ggplot(wilcoxon_p, aes(x=foldchange, y=-1*log10(p_raw), col=intakedifference, label=label)) +
geom_point() +
theme_minimal() +
theme(legend.position = 'bottom') +
scale_color_manual(values=c("#999999", "#009E73")) +
geom_vline(xintercept=c(-0.6, 0.6), col="red") +
geom_hline(yintercept=-log10(0.05), col="red") +
geom_text_repel(size = 2) +
scale_x_continuous(name = 'foldchange (HC - IBD)')
##================================================= SIGNIFICANT INTAKE DIFFERENCE (IBD vs NON-IBD) BASED ON FOLDCHANGE ==============================================##
# Sorts foldchange in decreasing order. Takes 6 first ones. Takes those rows that match
# with foldchange. Takes metabolites.
highest6 <- wilcoxon_p[wilcoxon_p$foldchange %in% sort(wilcoxon_p$foldchange, decreasing = TRUE)[1:6], ]$metabolites
# From intake table, takes only those metabolites that had highest foldchange
highest6_chem <- intake_log[,colnames(intake_log) %in% highest6]
# Adds colData that includes patient status information
highest6_full <- cbind(pseudo_diagnosis$diagnosis, highest6_chem)
names(highest6_full)[1] <- 'diagnosis'
highest6_full <- na.omit(highest6_full)
# Puts plots in the same picture
gridExtra::grid.arrange(
# Plot 1
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,2], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[2]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 2
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,3], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[3]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 3
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,4], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[4]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 4
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,5], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[5]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 5
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,6], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[6]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 6
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,7], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[7]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# 3 columns and 2 rows
ncol = 3,
nrow = 2
)
##===================================== DIFFERENTIAL ABUNDANCE ANALYSIS: INTAKE_calprotectin >150 vs <150 (LOG TRANSFORMED AND FILTERED DATA) ====================================================
# Columns containing intake metabolites
intake_cols <- grep("^int_", names(intake_mtb), value = TRUE)
metabolites_intake <- intake_mtb[,colnames(intake_mtb) %in% intake_cols]
# Calculate the percentage of non-zero values for each variable
non_zero_pct <- apply(metabolites_intake != 0, 2, mean)
# Filter variables with at least a non-zero value in 20% of the data
filter_20pct <- metabolites_intake[,non_zero_pct >= 0.2]
#add pseudocount to all variables
pseudo <- filter_20pct + 1
# Create a new column specifying calprotectin >150 (yes/no) for each sample
pseudo_calprotectin <- cbind(intake_mtb$calprotectin_above150, pseudo)
names(pseudo_calprotectin)[1] <- 'calprotectin_above150'
#pseudo_clean <- remove_outliers(pseudo_diagnosis, 'diagnosis')
##======================= WILCOXON TEST ======================##
wilcoxon_p <- c() # Initialize empty vector for p-values
# Do "for loop" over selected column names
for (i in 2:1010) {
result <- wilcox.test(pseudo_calprotectin[, i] ~ calprotectin_above150,
data = pseudo_calprotectin)
# Stores p-value to the vector with this column name
wilcoxon_p[[i]] <- result$p.value
}
#store metabolites with raw p-value in new dataframe
wilcoxon_p <- data.frame(metabolites = names(pseudo_calprotectin[,2:1010]),
p_raw = unlist(wilcoxon_p))
wilcoxon_p$p_adjusted <- p.adjust(wilcoxon_p$p_raw, method = "fdr") #add column with p_adj for multiple testing
# prepare a dataframe to plot p values
df <- data.frame(x = c(wilcoxon_p$p_raw, wilcoxon_p$p_adjusted),
type=rep(c("raw", "fdr"),
c(length(wilcoxon_p$p_raw),
length(wilcoxon_p$p_adjusted))))
# make a histrogram of p values and adjusted p values
wilcoxon_plot <- ggplot(df) +
geom_histogram(aes(x=x, fill=type)) +
labs(x = "p-value", y = "Frequency")
wilcoxon_plot
##============================== VOLCANO PLOT OF DIFFERENTIAL ABUNDANCE ANALYSIS ===============================##
#log2 transformation
intake_log <- log2(pseudo_calprotectin[,2:1010])
intake_log <- cbind(calprotectin_above150 = pseudo_calprotectin$calprotectin_above150, intake_log)
#calculate the mean of each metabolite in >150 group
high_calprotectin <- filter(intake_log, intake_log$calprotectin_above150 == 'yes')
high_calprotectin_m = apply(high_calprotectin[,2:1010], 2, mean)
#calcuate the mean of each metabolite in <150 group
low_calprotectin <- filter(intake_log, intake_log$calprotectin_above150 == 'no')
low_calprotectin_m = apply(low_calprotectin[,2:1010], 2, mean)
#because the data is already log2 transformed, take the difference between the means.
foldchange <- low_calprotectin_m - high_calprotectin_m
hist(foldchange, xlab = "log2 Fold Change (<150 vs >150)")
#add foldchange to df containing p-values
wilcoxon_p$foldchange <- foldchange
# add a column of NAs
wilcoxon_p$intakedifference <- "NO"
# if log2Foldchange > 0.6 and pvalue < 0.05, set as "UP"
wilcoxon_p$intakedifference[wilcoxon_p$foldchange > 0.6 & wilcoxon_p$p_raw < 0.05] <- "YES"
# if log2Foldchange < -0.6 and pvalue < 0.05, set as "DOWN"
wilcoxon_p$intakedifference[wilcoxon_p$foldchange < -0.6 & wilcoxon_p$p_raw < 0.05] <- "DOWN"
# Create a new column "label" to df, that will contain the name of metabolites where intake is different between groups (NA in case they are not)
wilcoxon_p$label <- NA
wilcoxon_p$label[wilcoxon_p$intakedifference != "NO"] <- wilcoxon_p$metabolites[wilcoxon_p$intakedifference != "NO"]
wilcoxon_p$label[wilcoxon_p$intakedifference == "NO"] <- wilcoxon_p$metabolites[wilcoxon_p$intakedifference == "NO"]
ggplot(wilcoxon_p, aes(x=foldchange, y=-1*log10(p_raw), col=intakedifference, label=label)) +
geom_point() +
theme_minimal() +
theme(legend.position = 'bottom') +
scale_color_manual(values=c("#999999", "#009E73")) +
geom_vline(xintercept=c(-0.6, 0.6), col="red") +
geom_hline(yintercept=-log10(0.05), col="red") +
geom_text_repel(size = 2) +
scale_x_continuous(name = 'foldchange (<150 - >150)')
##================================================= SIGNIFICANT INTAKE DIFFERENCE (IBD vs NON-IBD) BASED ON FOLDCHANGE ==============================================##
# Sorts foldchange in decreasing order. Takes 6 first ones. Takes those rows that match
# with foldchange. Takes metabolites.
highest6 <- wilcoxon_p[wilcoxon_p$foldchange %in% sort(wilcoxon_p$foldchange, decreasing = TRUE)[1:6], ]$metabolites
# From intake table, takes only those metabolites that had highest foldchange
highest6_chem <- intake_log[,colnames(intake_log) %in% highest6]
# Adds colData that includes patient status information
highest6_full <- cbind(pseudo_calprotectin$calprotectin_above150, highest6_chem)
names(highest6_full)[1] <- 'calprotectin_above150'
highest6_full <- na.omit(highest6_full)
# Puts plots in the same picture
gridExtra::grid.arrange(
# Plot 1
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,2], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("yes", "no")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[2]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 2
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,3], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("yes", "no")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[3]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 3
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,4], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("yes", "no")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[4]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 4
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,5], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("yes", "no")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[5]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 5
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,6], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("yes", "no")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[6]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 6
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,7], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("yes", "no")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[7]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# 3 columns and 2 rows
ncol = 3,
nrow = 2
)
##================================================ INTAKE METABOLITES BASED ON INTAKEDIFFERENCE FOUND IN DIAGNOSIS AND calprotectin ====================================================##
#Metabolite names based on intakedifference == YES (only works when NOT running calprotectin part of script)
intakedifference_diagnosis <- as.character(wilcoxon_p$metabolites[wilcoxon_p$intakedifference != "NO"])
#Metabolite names based on intakedifference == YES
intakedifference_calprotectin <- as.character(wilcoxon_p$metabolites[wilcoxon_p$intakedifference != "NO"])
#New df containing only intake metabolites that show difference in intake in
## IBD vs Non-IBD
## calprotectin <150 vs calprotectin >150
intake_mtb_1 <- intake_mtb[,colnames(intake_mtb) %in% intakedifference_diagnosis]
intake_mtb_2 <- intake_mtb_1[,colnames(intake_mtb_1) %in% intakedifference_calprotectin]
##========================================== LINEAR REGRESSION: IBD vs NON-IBD ========================================
# Only columns containing metabolite data from original intake df
metabolites_intake <- intake[,7:1114]
colnames(metabolites_intake) <- make.unique(colnames(metabolites_intake), sep = "_") #make sure that column names are all unique
# Calculate the percentage of non-zero values for each variable
non_zero_pct <- apply(metabolites_intake != 0, 2, mean)
# Filter variables with at least a non-zero value in 20% of the data
filter_20pct <- metabolites_intake[,non_zero_pct >= 0.2]
#add pseudocount to all variables
pseudo <- filter_20pct + 1
sum(is.na(pseudo)) #0 NA
# Apply the remove_outliers function to set outliers as NA within each column
pseudo_clean <- remove_outliers(pseudo)
sum(is.na(pseudo_clean)) #106632 NA
#RANK transformation
pseudo_rank <- pseudo_clean %>% mutate_all(~ (length(.) + 1) - rank(.))
## Filtering of analysis table on metabolite type is performed in == LOAD DATA, CLEANING NAMES AND SUBSETTING == #
# Create a new column specifying IBD (yes/no) for each sample
pseudo_diagnosis <- cbind(diagnosis = intake_mtb$diagnosis, pseudo_rank)
# Add covariates to df
pseudo_diagnosis <- cbind(age = intake_mtb$sex, pseudo_diagnosis)
pseudo_diagnosis <- cbind(sex = intake_mtb$age, pseudo_diagnosis)
pseudo_diagnosis <- cbind(BMI = intake_mtb$BMI, pseudo_diagnosis)
#============LINEAR REGRESSION ==============#
#dependent variable: individual diet metabolites
#predictor variables: covariates(age, sex, BMI), IBD(yes/no)
#columns with intake data + predictor variables
metabolite_names <- names(pseudo_diagnosis[,5:996])
predictor_vars <- c('age', 'sex', 'BMI', 'diagnosis')
# Create an empty dataframe to store results
results_df <- data.frame(Intake_Metabolite = character(),
Coefficient = numeric(),
Estimate = numeric(),
PValue = numeric(),
RSquared = numeric(),
stringsAsFactors = FALSE)
# Perform linear regression over intake_metabolites using a for loop
# Perform linear regression for each dependent variable
for (dep_var in metabolite_names) {
# Create formula
formula <- paste(dep_var, paste(predictor_vars, collapse = " + "), sep = " ~ ")
# Perform linear regression
regression_model <- lm(formula, data = pseudo_diagnosis)
# Extract coefficient estimates, p-values, and R-squared value
coefficients <- coef(regression_model)
p_values <- summary(regression_model)$coefficients[, "Pr(>|t|)"]
r_squared <- summary(regression_model)$r.squared
# Filter significant coefficients (p-value < 0.05)
significant_coeffs <- coefficients[p_values < 0.05]
significant_pvalues <- p_values[p_values < 0.05]
# Create a dataframe for the results
if (length(significant_coeffs) > 0) {
results <- data.frame(Intake_Metabolite = dep_var,
Coefficient = names(significant_coeffs),
Estimate = significant_coeffs,
PValue = significant_pvalues,
RSquared = r_squared,
stringsAsFactors = FALSE)
# Append results to the main dataframe
results_df <- rbind(results_df, results)
}
}
# Filter results df on significant coefficients 'diagnosis'
linreg_diagnosis <- results_df[results_df$Coefficient == 'diagnosisIBD',]
linreg_diagnosis$p_adjusted <- p.adjust(linreg_diagnosis$PValue, method = "fdr") #add column with p_adj for multiple testing
##============================== VOLCANO PLOT OF LINREG_DIAGNOSIS ===============================##
ggplot(linreg_diagnosis, aes(x=Estimate, y=-1*log10(PValue), label=Intake_Metabolite)) +
geom_point() +
theme_minimal() +
theme(legend.position = 'bottom') +
scale_color_manual(values=c("#999999", "#009E73")) +
ylim(0, NA) +
geom_text_repel(size = 2) +
scale_x_continuous(name = 'Estimate')
##================================================= SIGNIFICANT INTAKE DIFFERENCE (IBD vs NON-IBD) BASED ON FOLDCHANGE ==============================================##
# Sorts pvalue in increasing order. Takes 6 first ones. Takes those rows that match
# with foldchange. Takes metabolites.
highest6 <- linreg_diagnosis[linreg_diagnosis$RSquared %in% sort(linreg_diagnosis$RSquared, decreasing = T)[1:6], ]$Intake_Metabolite
# From intake table, takes only those metabolites that had highest foldchange
highest6_chem <- pseudo_diagnosis[,colnames(pseudo_diagnosis) %in% highest6]
# Adds colData that includes patient status information
highest6_full <- cbind(pseudo_diagnosis$diagnosis, highest6_chem)
names(highest6_full)[1] <- 'diagnosis'
highest6_full <- na.omit(highest6_full)
# Puts plots in the same picture
gridExtra::grid.arrange(
# Plot 1
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,2], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[2]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 2
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,3], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[3]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 3
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,4], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[4]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 4
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,5], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[5]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 5
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,6], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[6]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 6
ggplot(highest6_full, aes(x = diagnosis, y = highest6_full[,7], fill = diagnosis)) +
geom_boxplot() +
geom_signif(comparisons = list(c("IBD", "control")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[7]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# 3 columns and 2 rows
ncol = 3,
nrow = 2
)
##========================================== LINEAR REGRESSION: >150 CALPROTECTIN vs <150 CALPROTECTIN ========================================
# Only columns containing metabolite data from original intake df
metabolites_intake <- intake[,7:1114]
colnames(metabolites_intake) <- make.unique(colnames(metabolites_intake), sep = "_") #make sure that column names are all unique
# Calculate the percentage of non-zero values for each variable
non_zero_pct <- apply(metabolites_intake != 0, 2, mean)
# Filter variables with at least a non-zero value in 20% of the data
filter_20pct <- metabolites_intake[,non_zero_pct >= 0.2]
#add pseudocount to all variables
pseudo <- filter_20pct + 1
sum(is.na(pseudo)) #0 NA
# Apply the remove_outliers function to set outliers as NA within each column
pseudo_clean <- remove_outliers(pseudo)
sum(is.na(pseudo_clean)) #106632 NA
#RANK transformation
pseudo_rank <- pseudo_clean %>% mutate_all(~ (length(.) + 1) - rank(.))
## Filtering of analysis table on metabolite (intake, fecal, serum) type is performed in == LOAD DATA, CLEANING NAMES AND SUBSETTING == #
# Create a new column specifying high calprotectin (yes/no) for each sample
pseudo_calprotectin <- cbind(calprotectin_above150 = intake_mtb$calprotectin_above150, pseudo_rank)
# Add covariates to df
pseudo_calprotectin <- cbind(age = intake_mtb$age, pseudo_calprotectin)
pseudo_calprotectin <- cbind(sex = intake_mtb$sex, pseudo_calprotectin)
pseudo_calprotectin <- cbind(BMI = intake_mtb$BMI, pseudo_calprotectin)
#============LINEAR REGRESSION ==============#
#dependent variable: individual diet metabolites
#predictor variables: covariates(age, sex, BMI), calprotectin >150 (yes/no)
#columns with intake data + predictor variables
metabolite_names <- names(pseudo_calprotectin[,5:1013])
predictor_vars <- c('age', 'sex', 'BMI', 'calprotectin_above150')
# Create an empty dataframe to store results
results_df <- data.frame(Intake_Metabolite = character(),
Coefficient = numeric(),
Estimate = numeric(),
PValue = numeric(),
RSquared = numeric(),
stringsAsFactors = FALSE)
# Perform linear regression over intake_metabolites using a for loop
# Perform linear regression for each dependent variable
for (dep_var in metabolite_names) {
# Create formula
formula <- paste(dep_var, paste(predictor_vars, collapse = " + "), sep = " ~ ")
# Perform linear regression
regression_model <- lm(formula, data = pseudo_calprotectin)
# Extract coefficient estimates, p-values, and R-squared value
coefficients <- coef(regression_model)
p_values <- summary(regression_model)$coefficients[, "Pr(>|t|)"]
r_squared <- summary(regression_model)$r.squared
# Filter significant coefficients (p-value < 0.05)
significant_coeffs <- coefficients[p_values < 0.05]
significant_pvalues <- p_values[p_values < 0.05]
# Create a dataframe for the results
if (length(significant_coeffs) > 0) {
results <- data.frame(Intake_Metabolite = dep_var,
Coefficient = names(significant_coeffs),
Estimate = significant_coeffs,
PValue = significant_pvalues,
RSquared = r_squared,
stringsAsFactors = FALSE)
# Append results to the main dataframe
results_df <- rbind(results_df, results)
}
}
# Filter results df on significant coefficients 'calprotectin_above150'
linreg_calprotectin <- results_df[results_df$Coefficient == 'calprotectin_above150yes',]
linreg_calprotectin$p_adjusted <- p.adjust(linreg_calprotectin$PValue, method = "fdr") #add column with p_adj for multiple testing
##============================== VOLCANO PLOT OF LINREG_CALPROTECTIN ===============================##
ggplot(linreg_calprotectin, aes(x=Estimate, y=-1*log10(PValue), label=Intake_Metabolite)) +
geom_point() +
theme_minimal() +
theme(legend.position = 'bottom') +
scale_color_manual(values=c("#999999", "#009E73")) +
ylim(0, NA) +
geom_text_repel(size = 2) +
scale_x_continuous(name = 'Estimate')
##================================================= SIGNIFICANT INTAKE DIFFERENCE (CALPROTECTIN) ==============================================##
# Sorts pvalue in increasing order. Takes 6 first ones. Takes those rows that match
# with foldchange. Takes metabolites.
highest6 <- linreg_calprotectin[linreg_calprotectin$RSquared %in% sort(linreg_calprotectin$RSquared, decreasing = T)[1:6], ]$Intake_Metabolite
# From intake table, takes only those metabolites that had highest foldchange
highest6_chem <- pseudo_calprotectin[,colnames(pseudo_calprotectin) %in% highest6]
# Adds colData that includes patient status information
highest6_full <- cbind(pseudo_calprotectin$calprotectin_above150, highest6_chem)
names(highest6_full)[1] <- 'calprotectin_above150'
highest6_full <- na.omit(highest6_full)
# Puts plots in the same picture
gridExtra::grid.arrange(
# Plot 1
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,2], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("no", "yes")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[2]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 2
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,3], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("no", "yes")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[3]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 3
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,4], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("no", "yes")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[4]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 4
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,5], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("no", "yes")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[5]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 5
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,6], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("no", "yes")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[6]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# Plot 6
ggplot(highest6_full, aes(x = calprotectin_above150, y = highest6_full[,7], fill = calprotectin_above150)) +
geom_boxplot() +
geom_signif(comparisons = list(c("no", "yes")),
map_signif_level=TRUE) +
ylab("Predicted metabolite intake") + # y axis title
ggtitle(names(highest6_full)[7]) + # main title
theme_minimal() +
theme(title = element_text(size = 7),
legend.position = 'none',
axis.text = element_text(size = 7),
axis.title.x=element_blank()), # makes titles smaller, removes x axis title
# 3 columns and 2 rows
ncol = 3,
nrow = 2
)
##========================================== LINEAR REGRESSION: BEFORE A FLARE vs DURING/AFTER A FLARE ========================================
# Only columns containing metabolite data from original intake df
metabolites_intake <- intake[,7:1114]
colnames(metabolites_intake) <- make.unique(colnames(metabolites_intake), sep = "_") #make sure that column names are all unique
# Calculate the percentage of non-zero values for each variable
non_zero_pct <- apply(metabolites_intake != 0, 2, mean)
# Filter variables with at least a non-zero value in 20% of the data
filter_20pct <- metabolites_intake[,non_zero_pct >= 0.2]
#add pseudocount to all variables
pseudo <- filter_20pct + 1
sum(is.na(pseudo)) #0 NA
# Apply the remove_outliers function to set outliers as NA within each column
pseudo_clean <- remove_outliers(pseudo)
sum(is.na(pseudo_clean)) #106632 NA
#LOG transformation or RANK transformation
pseudo_rank <- pseudo_clean %>% mutate_all(~ (length(.) + 1) - rank(.))
## Filtering of analysis table on metabolite type (intake, fecal, serum) is performed in == LOAD DATA, CLEANING NAMES AND SUBSETTING == #
# Create a new column specifying IBD (yes/no) for each sample
pseudo_flare <- cbind(before_a_flare = intake_mtb$before_a_flare, pseudo_rank)
# Add covariates to df
pseudo_flare <- cbind(age = intake_mtb$age, pseudo_flare)
pseudo_flare <- cbind(sex = intake_mtb$sex, pseudo_flare)
pseudo_flare <- cbind(BMI = intake_mtb$BMI, pseudo_flare)
#============LINEAR REGRESSION ==============#
#dependent variable: individual diet metabolites
#predictor variables: covariates(age, sex, BMI), before a flare (yes/no)
#columns with intake data + predictor variables
metabolite_names <- names(pseudo_flare[,5:1013])
predictor_vars <- c('age', 'sex', 'BMI', 'before_a_flare')
# Create an empty dataframe to store results
results_df <- data.frame(Intake_Metabolite = character(),
Coefficient = numeric(),
Estimate = numeric(),
PValue = numeric(),
RSquared = numeric(),
stringsAsFactors = FALSE)
# Perform linear regression over intake_metabolites using a for loop
# Perform linear regression for each dependent variable
for (dep_var in metabolite_names) {
# Create formula
formula <- paste(dep_var, paste(predictor_vars, collapse = " + "), sep = " ~ ")
# Perform linear regression
regression_model <- lm(formula, data = pseudo_flare)
# Extract coefficient estimates, p-values, and R-squared value
coefficients <- coef(regression_model)
p_values <- summary(regression_model)$coefficients[, "Pr(>|t|)"]
r_squared <- summary(regression_model)$r.squared
# Filter significant coefficients (p-value < 0.05)
significant_coeffs <- coefficients[p_values < 0.05]
significant_pvalues <- p_values[p_values < 0.05]