-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 18.4 KB
/
.Rhistory
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
sum(
(control - samples[[i]][[j]])^2 / length(control)
)
)
WSD <- sqrt(
sum(
((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
)
)
if (!exists("WSD_output")) {
WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
} else {
WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
WSD_output <- rbind(WSD_output, WSD_output_new)
}
}
}
}
colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD", "ID")
return(WSD_output) # return output for samples from all different proteins
}
WSD(data = alpha,
control_temp = 75,
control_pH = 7,
min_wavelength = 200,
max_wavelength = 250)
WSDoutput <- WSD(data = all_WSD,
control_temp = 75,
control_pH = 7,
min_wavelength = 200,
max_wavelength = 250)
WSD_plot(WSDoutput)
WSD_plot <- function(data){
#able to plot WSD outputs using ggplot2
ggplot2::ggplot(data = data, aes(x=Temperature, y=WSD, group= interaction(pH, ID), color=as.factor(pH), shape=ID)) + #convert temp to factor so not continuous
geom_abline(intercept = 0, size = 1) + #horizontal line
geom_point(size = 2.5) +
geom_line(size = 1) +
theme_bw() +
labs(color="pH", title = "Spectral Difference values of Proteins") +
xlab("Temperature (°C)")
}
WSD <- function(data, control_temp, control_pH, units=NULL, min_wavelength, max_wavelength) {
ID <- data$ID
data <- data
for (protein in unique(ID)) {
#select control data
control <- dplyr::filter(data, ID == protein)
control <- dplyr::filter(control, Temperature == control_temp & pH == control_pH)
control <- dplyr::filter(control, WL >= min_wavelength, WL <= max_wavelength)
control <- dplyr::arrange(control, WL)
control <- dplyr::pull(control, MolarElipticity)
# select sample data (including control)
samples <- dplyr::filter(data, ID == protein)
samples <- dplyr::filter(samples, WL >= min_wavelength, WL <= max_wavelength)
samples <- tidyr::spread(samples, Temperature, MolarElipticity)
samples <- dplyr::arrange(samples, pH, WL)
samples <- dplyr::group_by(samples, pH)
samples <- dplyr::group_split(samples)
#find weighted squared difference between y_Ai and y_Bi
if (!exists("WSD_output")) {
WSD_output <- data.frame()
}
for (i in 1:length(samples)) {
num_conditions <- ncol(samples[[i]])
for (j in 4:num_conditions) { #note we start at 3 because col 1 is wavelength and col2 is pH, col 3 is protein
SD <- sqrt(
sum(
(control - samples[[i]][[j]])^2 / length(control)
)
)
WSD <- sqrt(
sum(
((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
)
)
if (!exists("WSD_output")) {
WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
} else {
WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
WSD_output <- rbind(WSD_output, WSD_output_new)
}
}
}
}
colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD", "ID")
return(WSD_output) # return output for samples from all different proteins
}
WSD_plot <- function(data){
#able to plot WSD outputs using ggplot2
ggplot2::ggplot(data = data, aes(x=Temperature, y=WSD, group= interaction(pH, ID), color=as.factor(pH), shape=ID)) + #convert temp to factor so not continuous
geom_abline(intercept = 0, size = 1) + #horizontal line
geom_point(size = 2.5) +
geom_line(size = 1) +
theme_bw() +
labs(color="pH", title = "Spectral Difference values of Proteins") +
xlab("Temperature (°C)")
}
WSDoutput <- WSD(data = all_WSD,
control_temp = 75,
control_pH = 7,
min_wavelength = 200,
max_wavelength = 250)
WSD_plot(WSDoutput)
WSD_plot(WSDoutput) +
geom_smooth()
WSD_plot(WSDoutput) +
ylab("Weighted Spectral Difference")
WSD_dataprep <- function(..., pH, Temp=NULL, ID) {
if(length(list(...)) == 1L) {
# check if single input is list (maybe list of dataframes)
if (is.list(...) == TRUE) {
x <- list(...)[[1]] #have to list (...) to be able to use it, but already was list, so get 1st list back out with [[1]]
#purpose of this was to get x <- ... which doesn't work in functions. this was best solution I found.
} else if (unique(summary(list(...))[,2]) %in% c("data.frame", "tibble", "data.table")) {
x <- list(...)
} else {
stop("data variable input is not of type dataframe or list of dataframes")
}
} else {
x <- list(...)
}
if (unique(summary(x)[,2]) %in% c("data.frame", "tibble", "data.table")) {
df_count <- length(x)
} else {
stop("not all unnamed input to function are dataframes of same length")
}
ID <- rep(ID, times=df_count, each = max(length(pH), length(Temp)))
ID <- head(ID, df_count)
pH <- rep(pH, times=df_count)
pH <- head(pH, df_count)
if (df_count == length(pH)) {
df_tidy <- data.frame() #initialize dataframe to store final output
for (i in 1:df_count) {
df <- x[i][[1]]
colnames(df)[1] <- "WL"
df_tidy_i <- tidyr::gather(df, Temperature, MolarElipticity, -WL)
df_tidy_i$pH <- pH[i]
df_tidy_i$Temperature <- as.numeric(gsub("\\D+", "", df_tidy_i$Temperature))
df_tidy_i$ID <- ID[i]
df_tidy <- rbind(df_tidy, df_tidy_i)
}
df_tidy_WSD <- df_tidy
return(df_tidy_WSD)
} else if (df_count == length(Temp)) {
df_tidy <- data.frame() #initialize dataframe to store final output
for (i in 1:df_count) {
df <- x[i][[1]]
colnames(df)[1] <- "WL"
df_tidy_i <- tidyr::gather(df, pH, MolarElipticity, -WL)
df_tidy_i$Temperature <- Temp[i]
df_tidy_i$pH <- as.numeric(gsub("\\D+", "", df_tidy_i$pH))
df_tidy_i$ID <- ID[i]
df_tidy <- rbind(df_tidy, df_tidy_i)
}
df_tidy_WSD <- df_tidy
return(df_tidy_WSD)
# } else if (pH == NULL && Temp == NULL) {
# stop("provide etiher pH or Temperature vector corresponding to each dataframe uploaded")
} else {
stop("unknown error, unfamiliar conditions loaded in")
}
}
WSD <- function(data, control_temp, control_pH, units=NULL, min_wavelength, max_wavelength) {
ID <- data$ID
data <- data
for (protein in unique(ID)) {
#select control data
control <- dplyr::filter(data, ID == protein)
control <- dplyr::filter(control, Temperature == control_temp & pH == control_pH)
control <- dplyr::filter(control, WL >= min_wavelength, WL <= max_wavelength)
control <- dplyr::arrange(control, WL)
control <- dplyr::pull(control, MolarElipticity)
# select sample data (including control)
samples <- dplyr::filter(data, ID == protein)
samples <- dplyr::filter(samples, WL >= min_wavelength, WL <= max_wavelength)
samples <- tidyr::spread(samples, Temperature, MolarElipticity)
samples <- dplyr::arrange(samples, pH, WL)
samples <- dplyr::group_by(samples, pH)
samples <- dplyr::group_split(samples)
#find weighted squared difference between y_Ai and y_Bi
if (!exists("WSD_output")) {
WSD_output <- data.frame()
}
for (i in 1:length(samples)) {
num_conditions <- ncol(samples[[i]])
for (j in 4:num_conditions) { #note we start at 3 because col 1 is wavelength and col2 is pH, col 3 is protein
SD <- sqrt(
sum(
(control - samples[[i]][[j]])^2 / length(control)
)
)
WSD <- sqrt(
sum(
((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
)
)
if (!exists("WSD_output")) {
WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
} else {
WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
WSD_output <- rbind(WSD_output, WSD_output_new)
}
}
}
}
colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD", "ID")
return(WSD_output) # return output for samples from all different proteins
}
#
#
#
#
# WSD <- function(control, samples, ControlTemp, ControlpH, units, ID, MinWavelength, MaxWavelength) {
# #find squared difference between y_Ai and y_Bi
# WSD_output <- data.frame()
# for (i in 1:length(samples)) {
# for (j in 3:ncol(samples[[i]])) { #note we start at 3 because col 1 is wavelength and col2 is pH
# SD <- sqrt(
# sum(
# (control - samples[[i]][[j]])^2 / length(control)
# )
# )
# WSD <- sqrt(
# sum(
# ((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
# )
# )
# if (!exists("WSD_output")) {
# WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD)
# } else {
# WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD)
# WSD_output <- rbind(WSD_output, WSD_output_new)
# }
# }
# }
# colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD")
# return(WSD_output)
#
#
#
#
# }
#
#
#
# WSD_manual <- function(control, samples, ControlTemp, ControlpH, units, ID, MinWavelength, MaxWavelength) {
# #find squared difference between y_Ai and y_Bi
# WSD_output <- data.frame()
# for (i in 1:length(samples)) {
# for (j in 3:ncol(samples[[i]])) { #note we start at 3 because col 1 is wavelength and col2 is pH
# SD <- sqrt(
# sum(
# (control - samples[[i]][[j]])^2 / length(control)
# )
# )
# WSD <- sqrt(
# sum(
# ((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
# )
# )
# if (!exists("WSD_output")) {
# WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD)
# } else {
# WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD)
# WSD_output <- rbind(WSD_output, WSD_output_new)
# }
# }
# }
# colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD")
# return(WSD_output)
# }
#TODO
# check if control is either a vector or a dataframe/datatable/matrix with only 1 dimension
# attempt to convert control input to a vector
# error if ctl cannot be made into a vector
# determine type of samples input
# provide error if it is not either a dataframe/datatable/tibble/matrix variable with at least 2 columns
# attempt to convert samples to dataframe (if not already)
# NOTE: samples must have first column be wavelength values corresponding to elipticity measurements.
# give error if samples cannot be converted
# give error if length ctl != nrow(samples)
# provide possible pre-built options for units of input data
# default the unit to be MolarElipticity?
# give error if units is not recognized type
# Allow optional ID column for labelling all results from output. for example, name of protein
#allows for combination of results from multiple
# allow ControlTemp and ControlpH be either numeric values or vectors same length as
# new function to convert from raw data to Molar Elipticity units?
#
#
# WSD_preprocessed <- function(control, samples, ControlTemp, ControlpH, units, ID, MinWavelength, MaxWavelength) {
# #this function is appropriate to be used on data that is already tidy formatted with col 1 re
# ####NEED TO FINISH THIS DESCRIPTION######
#
# #find squared difference between y_Ai and y_Bi
# WSD_output <- data.frame()
# for (i in 1:length(samples)) {
# for (j in 3:ncol(samples[[i]])) { #note we start at 3 because col 1 is wavelength and col2 is pH
# SD <- sqrt(
# sum(
# (control - samples[[i]][[j]])^2 / length(control)
# )
# )
# WSD <- sqrt(
# sum(
# ((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
# )
# )
# if (!exists("WSD_output")) {
# WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD)
# } else {
# WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD)
# WSD_output <- rbind(WSD_output, WSD_output_new)
# }
# }
# }
# colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD")
# return(WSD_output)
# }
# WSD_Combine <- function() {
# #used to combine multiple WSD calculations from different trials (useful for comparisons, statistical significance, plotting combined, etc)
# }
WSD_plot <- function(data){
#able to plot WSD outputs using ggplot2
ggplot2::ggplot(data = data, aes(x=Temperature, y=WSD, group= interaction(pH, ID), color=as.factor(pH), shape=ID)) + #convert temp to factor so not continuous
geom_abline(intercept = 0, size = 1) + #horizontal line
geom_point(size = 2.5) +
geom_line(size = 1) +
theme_bw() +
labs(color="pH", title = "Spectral Difference values of Proteins") +
xlab("Temperature (°C)")
}
alphaCD_pH7 <- read.csv("~/PersonalProjects/cd_data/alphaCD_pH7.csv")
alphaCD_pH4 <- read.csv("~/PersonalProjects/cd_data/alphaCD_pH4.csv")
alphaCD_pH2 <- read.csv("~/PersonalProjects/cd_data/alphaCD_pH2.csv")
betaCD_pH7 <- read.csv("~/PersonalProjects/cd_data/betaCD_pH7.csv")
betaCD_pH4 <- read.csv("~/PersonalProjects/cd_data/betaCD_pH4.csv")
betaCD_pH2 <- read.csv("~/PersonalProjects/cd_data/betaCD_pH2.csv")
betacohCD_pH7 <- read.csv("~/PersonalProjects/cd_data/betacohCD_pH7.csv")
betacohCD_pH4 <- read.csv("~/PersonalProjects/cd_data/betacohCD_pH4.csv")
betacohCD_pH2 <- read.csv("~/PersonalProjects/cd_data/betacohCD_pH2.csv")
WSD_dataprep <- function(..., pH, Temp=NULL, ID) {
if(length(list(...)) == 1L) {
# check if single input is list (maybe list of dataframes)
if (is.list(...) == TRUE) {
x <- list(...)[[1]] #have to list (...) to be able to use it, but already was list, so get 1st list back out with [[1]]
#purpose of this was to get x <- ... which doesn't work in functions. this was best solution I found.
} else if (unique(summary(list(...))[,2]) %in% c("data.frame", "tibble", "data.table")) {
x <- list(...)
} else {
stop("data variable input is not of type dataframe or list of dataframes")
}
} else {
x <- list(...)
}
if (unique(summary(x)[,2]) %in% c("data.frame", "tibble", "data.table")) {
df_count <- length(x)
} else {
stop("not all unnamed input to function are dataframes of same length")
}
ID <- rep(ID, times=df_count, each = max(length(pH), length(Temp)))
ID <- head(ID, df_count)
pH <- rep(pH, times=df_count)
pH <- head(pH, df_count)
if (df_count == length(pH)) {
df_tidy <- data.frame() #initialize dataframe to store final output
for (i in 1:df_count) {
df <- x[i][[1]]
colnames(df)[1] <- "WL"
df_tidy_i <- tidyr::gather(df, Temperature, MolarElipticity, -WL)
df_tidy_i$pH <- pH[i]
df_tidy_i$Temperature <- as.numeric(gsub("\\D+", "", df_tidy_i$Temperature))
df_tidy_i$ID <- ID[i]
df_tidy <- rbind(df_tidy, df_tidy_i)
}
df_tidy_WSD <- df_tidy
return(df_tidy_WSD)
} else if (df_count == length(Temp)) {
df_tidy <- data.frame() #initialize dataframe to store final output
for (i in 1:df_count) {
df <- x[i][[1]]
colnames(df)[1] <- "WL"
df_tidy_i <- tidyr::gather(df, pH, MolarElipticity, -WL)
df_tidy_i$Temperature <- Temp[i]
df_tidy_i$pH <- as.numeric(gsub("\\D+", "", df_tidy_i$pH))
df_tidy_i$ID <- ID[i]
df_tidy <- rbind(df_tidy, df_tidy_i)
}
df_tidy_WSD <- df_tidy
return(df_tidy_WSD)
# } else if (pH == NULL && Temp == NULL) {
# stop("provide etiher pH or Temperature vector corresponding to each dataframe uploaded")
} else {
stop("unknown error, unfamiliar conditions loaded in")
}
}
alpha <- WSD_dataprep(alphaCD_pH2, alphaCD_pH4, alphaCD_pH7, pH = c(2,4,7), ID = "HSPalpha")
all_WSD <- WSD_dataprep(alphaCD_pH2, alphaCD_pH4, alphaCD_pH7,
betaCD_pH2, betaCD_pH4, betaCD_pH7,
betacohCD_pH2, betacohCD_pH4, betacohCD_pH7,
pH = c(2,4,7),
ID = c("HSPalpha",# "HSPalpha", "HSPalpha",
"HSPbeta",#"HSPbeta","HSPbeta",
#"HSPbetacoh","HSPbetacoh",
"HSPbetacoh"))
all_raw <- list(alphaCD_pH2, alphaCD_pH4, alphaCD_pH7,
betaCD_pH2, betaCD_pH4, betaCD_pH7,
betacohCD_pH2, betacohCD_pH4, betacohCD_pH7)
all_WSD <- WSD_dataprep(all_raw,
pH = c(2,4,7),
ID = c("HSPalpha", "HSPbeta", "HSPbetacoh"))
# WSD function
# WSD <- function(data, control_temp, control_pH, units=NULL, min_wavelength, max_wavelength) {
#
# ID <- data$ID
# data <- data
# for (protein in unique(ID)) {
# #select control data
# control <- dplyr::filter(data, ID == protein)
# control <- dplyr::filter(control, Temperature == control_temp & pH == control_pH)
# control <- dplyr::filter(control, WL >= min_wavelength, WL <= max_wavelength)
# control <- dplyr::arrange(control, WL)
# control <- dplyr::pull(control, MolarElipticity)
#
# # select sample data (including control)
# samples <- dplyr::filter(data, ID == protein)
# samples <- dplyr::filter(samples, WL >= min_wavelength, WL <= max_wavelength)
# samples <- tidyr::spread(samples, Temperature, MolarElipticity)
# samples <- dplyr::arrange(samples, pH, WL)
# samples <- dplyr::group_by(samples, pH)
# samples <- dplyr::group_split(samples)
#
# #find weighted squared difference between y_Ai and y_Bi
# if (!exists("WSD_output")) {
# WSD_output <- data.frame()
# }
#
# for (i in 1:length(samples)) {
# num_conditions <- ncol(samples[[i]])
# for (j in 4:num_conditions) { #note we start at 3 because col 1 is wavelength and col2 is pH, col 3 is protein
# SD <- sqrt(
# sum(
# (control - samples[[i]][[j]])^2 / length(control)
# )
# )
# WSD <- sqrt(
# sum(
# ((abs(control)) / sum(abs(control))) * (control - samples[[i]][[j]])^2
# )
# )
# if (!exists("WSD_output")) {
# WSD_output <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
# } else {
# WSD_output_new <- data.frame(colnames(samples[[i]][j]), samples[[i]][[2]][1], WSD, SD, protein)
# WSD_output <- rbind(WSD_output, WSD_output_new)
#
# }
# }
#
# }
#
# }
# colnames(WSD_output) <- c("Temperature", "pH", "WSD", "SD", "ID")
# return(WSD_output) # return output for samples from all different proteins
# }
WSD(data = alpha,
control_temp = 75,
control_pH = 7,
min_wavelength = 200,
max_wavelength = 250)
WSDoutput <- WSD(data = all_WSD,
control_temp = 75,
control_pH = 7,
min_wavelength = 200,
max_wavelength = 250)
#define "control" data
ctl_temp <- 75
ctl_pH <- 7
min_wavelength <- 200
max_wavelength <- 250
WSD_plot(WSDoutput) +
ylab("Weighted Spectral Difference")
View(alpha)
View(all_WSD)
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
library(WeightedSpectralDifference)
# load in desired data to be analyzed
alphaCD_pH7 <- read.csv("~/PersonalProjects/cd_data/alphaCD_pH7.csv")
# load package for analysis
library(WeightedSpectralDifference)
source('~/.active-rstudio-document', encoding = 'UTF-8', echo=TRUE)
source('~/.active-rstudio-document', encoding = 'UTF-8', echo=TRUE)
source('~/.active-rstudio-document', encoding = 'UTF-8', echo=TRUE)
# plot of the WSD values:
ggplot(data = WSD_all, aes(x=Temperature, y=WSD, group= interaction(pH, ID), color=as.factor(pH), shape=ID)) + #convert temp to factor so not continuous
geom_abline(intercept = 0, size = 1) + #horizontal line
geom_point(size = 2.5) +
geom_line(size = 1) +
theme_bw() +
labs(color="pH", title = "Weighted Spectral Difference values of Proteins") +
xlab("Temperature (°C)") +
ylab("Weighted Spectral Difference")
getwd()
getwd()