-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysis.R
More file actions
285 lines (235 loc) · 6.47 KB
/
Copy pathAnalysis.R
File metadata and controls
285 lines (235 loc) · 6.47 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
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
rm(list = ls())
options(digits = 9)
#Set working directory
setwd("C:/Users/prata/Desktop/Elucidata/LCMS analysis/LCMS-analysis/")
getwd()
metadata <- read.csv("sample_metadata.csv", header = TRUE)
maven_data <- read.csv("Maven_processed.csv", header = TRUE)
library(dplyr)
library(ggplot2)
library(ggbiplot)
library(tidyr)
library(reshape2)
# To find pool total
pool_total <-
maven_data %>% select( #remove unwanted columns
-c(
label,
metaGroupId,
groupId,
goodPeakCount,
medMz,
medRt,
maxQuality,
expectedRtDiff,
ppmDiff,
parent,
note,
formula,
compoundId
)
) %>%
group_by(compound) %>% summarize_all(funs(sum)) #gourp_by compound name and add all rowns in resulting columns
# Correction for R reading of numerical as factors
metadata$Sample <- paste0("X", metadata$Sample)
#to calculate franction enrichment
df2 <-
maven_data %>% select( #remove unwanted columns
-c(
label,
metaGroupId,
groupId,
goodPeakCount,
medMz,
medRt,
maxQuality,
expectedRtDiff,
ppmDiff,
parent,
compoundId,
formula
)
) %>%
group_by(compound, note)
#convert to ling format
df3 <- melt(df2, id = c("compound", "note"))
names(df3)[names(df3) == "variable"] <- "Sample"
#assign phenotype and time values
df4 <- full_join(df3, metadata)
df5 <- pool_total %>% ungroup() %>%
group_by(compound)
#long form for pool total data
df6 <- melt(df5, id = c("compound"))
names(df6)[names(df6) == "variable"] <- "Sample"
names(df6)[names(df6) == "value"] <- "Total"
#assign phenotype and time values to pool total data
df7 <- full_join(df6, metadata)
# join bith raw data and pool total dataframes
fraction_enrichment <- full_join(df4, df7)
fraction_enrichment <-
fraction_enrichment %>% mutate("fraction_enrich" = (value / Total) * 100) #calculate fraction enrichment
write.csv(fraction_enrichment,
file = "Data with Pool and fractions.csv")
# PCA on non transformed raw data
# see how the data looks
for (i in 2:ncol(pool_total)) {
p <- ggplot(pool_total,
aes(x = unlist(pool_total[, i], use.names = FALSE))) +
geom_histogram(bins = 10)
print(p)
}
pca <- prcomp(pool_total[, c(2:ncol(pool_total))])
summary(pca)
ggbiplot(pca)
# PCA normalized data
pool_total_normalized <- pool_total
for (i in 2:ncol(pool_total)) {
pool_total_normalized[, i] <-
scale(pool_total[, i], center = TRUE) %>% as.vector
}
pca.normalized <-
prcomp(pool_total_normalized[, c(2:ncol(pool_total))])
summary(pca.normalized)
ggbiplot(pca.normalized)
# PCA log transformed data
pool_total_log <- pool_total
for (i in 2:ncol(pool_total)) {
pool_total_log[, i] <- log10(pool_total[, i] + 1) %>% as.vector
}
# see how the data looks
for (i in 4:ncol(pool_total)) {
p <- ggplot(pool_total_log,
aes(x = unlist(pool_total_log[, i], use.names = FALSE))) +
geom_histogram(bins = 10)
print(p)
}
pca.labels <- pool_total_log$compound
pca.log <- prcomp(pool_total_log[, c(2:ncol(pool_total))])
pca.log$sdev
screeplot(pca.log, type = "lines", col = 3)
pca.log$rotation
summary(pca.log)
ggbiplot(pca.log, ellipse = TRUE, labels = pca.labels)
dev.copy(pdf,
file = "PCAlogPC1PC2.pdf",
height = 8,
width = 12)
dev.off()
ggbiplot(pca.log, choices = c(2, 3), labels = pca.labels)
dev.copy(pdf,
file = "PCAlogPC2PC3.pdf",
height = 8,
width = 12)
dev.off()
ggbiplot(pca.log, choices = c(3, 4), labels = pca.labels)
dev.copy(pdf,
file = "PCAlogPC3PC4.pdf",
height = 8,
width = 12)
dev.off()
#make data frame to check for PCA of compounds
pool_total_T <- add_rownames(pool_total) %>%
gather(var, value,-rowname) %>%
spread(rowname, value)
names(pool_total_T)[names(pool_total_T) == "var"] <- "Sample"
colnames(pool_total_T) <- unlist(pool_total_T[1, ], use.names = FALSE)
pool_total_T <- pool_total_T[-1, ]
pool_total_T[, 2:ncol(pool_total_T)] <-
sapply(pool_total_T[, 2:ncol(pool_total_T)], as.double)
str(pool_total_T)
# see how the data looks
for (i in 2:ncol(pool_total_T)) {
p <- ggplot(pool_total_T,
aes(x = unlist(pool_total_T[, i], use.names = FALSE))) +
geom_histogram(bins = 10)
print(p)
}
pool_total_T_log <- pool_total_T
for (i in 2:ncol(pool_total_T_log)) {
pool_total_T_log[, i] <- log10(pool_total_T[, i] + 1) %>% as.vector
}
for (i in 2:ncol(pool_total_T_log)) {
p <- ggplot(pool_total_T_log,
aes(x = unlist(pool_total_T_log[, i], use.names = FALSE))) +
geom_histogram(bins = 10)
print(p)
}
#calculate pca for compounds
pca.T <- prcomp(pool_total_T[])
pca.labels_T <- pool_total_T_log$compound
pca.log_T <- prcomp(pool_total_T_log[, c(2:ncol(pool_total_T_log))])
pca.log_T$sdev
screeplot(pca.log_T, type = "lines", col = 3)
pca.log_T$rotation
summary(pca.log_T)
#plot pca for compounds
ggbiplot(pca.log_T, ellipse = TRUE, labels = pca.labels_T)
dev.copy(pdf,
file = "PCAlogTPC1PC2.pdf",
height = 8,
width = 12)
dev.off()
#plot graphs for pool total vs time
totalgraph <- ggplot(
fraction_enrichment,
aes(
x = Time..mins.,
y = Total,
group = interaction(compound, Phenotype),
color = Phenotype
)
) +
geom_line() +
labs(title = "Pool Total",
x = "Time Points (in mins)",
y = "Metabolite Levels") +
facet_wrap(. ~ compound, scales = "free_y")
print(totalgraph)
dev.copy(pdf,
file = "totalgraph.pdf",
width = 25,
height = 25)
dev.off ()
#plot graphs for Fraction enrichment vs time
fractiongraph <- ggplot(
fraction_enrichment,
aes(
x = Time..mins.,
y = fraction_enrich,
group = interaction(note, Phenotype),
color = note
)
) +
geom_line() +
labs(title = "Fraction Enrichment",
x = "Time Points (in mins)",
y = "Fraction Enrichment") +
theme(legend.position = "bottom") +
facet_wrap(. ~ compound + Phenotype, scales = "free_y")
print(fractiongraph)
dev.copy(pdf,
file = "fractiongraph.pdf",
width = 25,
height = 25)
dev.off ()
# natural abundance correction
library(accucor)
corrected_maven_data <- natural_abundance_correction(
path = "Maven_processed_2.csv",
resolution = 100000,
output_filetype = 'csv',
columns_to_skip = c(
'label',
'metaGroupId',
'groupId',
'goodPeakCount',
'medMz',
'medRt',
'maxQuality',
'compoundId',
'expectedRtDiff',
'ppmDiff',
'parent'
),
report_pool_size_before_df = TRUE
)