-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferential_Analysis.R
More file actions
172 lines (132 loc) · 5.41 KB
/
Copy pathDifferential_Analysis.R
File metadata and controls
172 lines (132 loc) · 5.41 KB
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
# Load required libraries
library(limma)
library(dplyr)
# BiocManager::install("limma")
# B0 - baseline
# D1 - new start
# D5 - week 5
# F3 - month 3
# F6 - month 6
# F9 - month 9
# F12 - moth 12
metlData <- read.csv("~/Roselab/Metabolite/data/data_for_analysis/metabolite_data_74.csv", row.names = "ID" )
metaData <- read.csv("~/Roselab/Metabolite/data/data_for_analysis/meta_data_74.csv")
summary(unlist(metlData[9032, ]))
hist(unlist(metlData[9032, ]), breaks = 20)
hist(log2(unlist(metlData[9032, ])), breaks = 20)
#------------------------- Testing loop -------------------------------------
metlData_log2 <- log2(metlData + 1)
# Define timepoints of interest
timepoints <- c("B0", "D1", "D5", "F3", "F6", "F9", "F12")
arms <- unique(metaData$Arm) # e.g., "IF", "NIF"
# Setting parameters to test loop
a <- arms[1] #NIF
tp <- timepoints[2] #D1
md_arm <- metaData %>% filter(Arm == a)
##### In time loop
comparison_samples <- md_arm %>%
filter(Time %in% c("B0", tp)) %>%
pull(Sample_ID)
data_subset <- metlData_log2[, comparison_samples, drop = FALSE]
# SEtting up column labels
comparison_samples
md_arm$Sample_ID
matches <- match(comparison_samples, md_arm$Sample_ID)
matches
md_arm$Time[matches]
time_group <- factor(md_arm$Time[matches],
levels = timepoints)
colnames(data_subset)
metaData
#Setting up differential analysis
# For each metabolite, fit a linear model where the expression depends on which timepoint the sample was collected at.
design <- model.matrix(~ 0 + time_group)
colnames(design) <- levels(time_group)
# design
#
# for (tp in setdiff(timepoints, "B0")) {
# # Identify samples for this timepoint and baseline
# comparison_samples <- md_arm %>%
# filter(Time %in% c("B0", tp)) %>%
# pull(Sample_ID)
#
# # Subset log2-transformed data for these samples
# data_subset <- metlData_log2[, comparison_samples, drop = FALSE]
#
# # Make sure sample order in metadata matches column order in data
# time_group <- factor(md_arm$Time[match(comparison_samples, md_arm$Sample_ID)],
# levels = timepoints)
# # Build design matrix
# design <- model.matrix(~ 0 + time_group)
# colnames(design) <- levels(time_group)
#
# # Contrast: current timepoint vs B0
# contrast.matrix <- makeContrasts(contrasts = paste(tp, "- B0"), levels = design)
#
# # Fit model and compute statistics
# fit <- lmFit(data_subset, design)
# fit2 <- contrasts.fit(fit, contrast.matrix)
# fit2 <- eBayes(fit2)
#
# # Get topTable with all metabolites
# result <- topTable(fit2, number = Inf, adjust.method = "fdr", sort.by = "none")
#
# # Save result to CSV
# file_name <- paste0("Differential_", a, "_", tp, "_vs_B0.csv")
# write.csv(result, file = file_name, row.names = TRUE)
#
# # Progress message
# cat("Saved differential analysis for arm", a, "timepoint", tp, "vs B0 to", file_name, "\n")
# # }
# }
#------------------------- suggested loop ----------------------------------
# -------------------------------
# Step 1: Prepare log2-transformed data
# -------------------------------
# Safely apply log2 transformation with a small offset to avoid log(0)
# Only apply if not already log-transformed
# You can adjust the pseudo count (1) if your values are very small
metlData_log2 <- log2(metlData + 1)
# Optional: Inspect distribution before and after log2 to verify
# hist(unlist(metlData[2000, ]), main = "Original")
# hist(unlist(metlData_log2[2000, ]), main = "Log2-transformed")
# -------------------------------
# Step 2: Differential analysis
# -------------------------------
# Define timepoints of interest
timepoints <- c("B0", "D1", "D5", "F3", "F6", "F9", "F12")
arms <- unique(metaData$Arm) # e.g., "IF", "NIF"
# Loop over arms and timepoints to compare against baseline
for (a in arms) {
# Subset metadata for current arm
md_arm <- metaData %>% filter(Arm == a)
for (tp in setdiff(timepoints, "B0")) {
# Identify samples for this timepoint and baseline
comparison_samples <- md_arm %>%
filter(Time %in% c("B0", tp)) %>%
pull(Sample_ID)
# Subset log2-transformed data for these samples
data_subset <- metlData_log2[, comparison_samples, drop = FALSE]
# Make sure sample order in metadata matches column order in data
time_group <- factor(md_arm$Time[match(comparison_samples, md_arm$Sample_ID)],
levels = timepoints)
# Build design matrix
#For each metabolite, fit a linear model where the expression depends on which timepoint the sample was collected at.
design <- model.matrix(~ 0 + time_group)
colnames(design) <- levels(time_group)
# Contrast: current timepoint vs B0
contrast.matrix <- makeContrasts(contrasts = paste(tp, "- B0"), levels = design)
# Fit model and compute statistics
fit <- lmFit(data_subset, design)
fit2 <- contrasts.fit(fit, contrast.matrix)
fit2 <- eBayes(fit2)
# Get topTable with all metabolites
result <- topTable(fit2, number = Inf, adjust.method = "fdr", sort.by = "none")
# Save result to CSV
file_name <- file.path("~/Roselab/Metabolite/results/Differential_Analysis/",
paste0("Differential_", a, "_", tp, "_vs_B0.csv"))
write.csv(result, file = file_name, row.names = TRUE)
# Progress message
cat("Saved differential analysis for arm", a, "timepoint", tp, "vs B0 to", file_name, "\n")
}
}