-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF3g,h_SF7c,e_confocal.R
121 lines (97 loc) · 3.11 KB
/
F3g,h_SF7c,e_confocal.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
# path definition
# home PC
path = "C:/path/to/folder/"
setwd(path)
# packages
wants <- c("openxlsx",
"readxl",
"stringr",
"rstatix")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
lapply(wants, require, character.only = TRUE)
# import data
sheets <- excel_sheets("microscopy_data.xlsx")
sheets_format <- which(str_detect(sheets, "_format"))
data <- vector("list")
for (sheet in sheets_format){
data[[str_remove(sheets[sheet], "_format")]] <- read.xlsx(
"microscopy_data.xlsx", sheet= sheet)
}
# functions
stars_annotation <- function(p_values){
stars <- c()
for (p in p_values){
if (p > 0.05){stars <- c(stars, " ")}
if (between(p, 0.01, 0.05)){stars <- c(stars, "*")}
if (between(p, 0.001, 0.01)){stars <- c(stars, "**")}
if (p < 0.001){stars <- c(stars, "***")}
}
return(stars)
}
paired_test <- function(data_from_list){
data_temp <- as.data.frame(data_from_list)
data_temp$condition <- as.factor(data_temp$condition)
data_temp$state <- as.factor(data_temp$state)
levels <- levels(data_temp$condition)
base_stim <- as.data.frame(levels)
row = 0
for (i in levels){
temp = subset(data_temp, condition == as.character(i))
row = row +1
temp_test <-t.test(temp$colocalisation~temp$state, paired = TRUE)
base_stim[row, 2] <- cohens_d(temp, colocalisation ~ state, paired = TRUE)$effsize
base_stim[row, 3] <- temp_test$conf.int[1]
base_stim[row, 4] <- temp_test$conf.int[2]
base_stim[row, 5] <- temp_test$statistic
base_stim[row, 6] <- temp_test$parameter
base_stim[row, 7] <- temp_test$p.value
base_stim[row, 8] <- stars_annotation(temp_test$p.value)
names(base_stim) <- c("condition", "effect_size",
"lower_95%_CI", "upper_95%_CI",
"t_statistic", "degrees_of_freedom",
"p_value", "significance")
results_test <- base_stim
}
return(base_stim)
}
exclude_NA_rows <- function(data_from_list){
data_temp <- as.data.frame(data_from_list)
if (any(is.na(data_temp)) == TRUE){
na_rows <- which(is.na(data_temp$colocalisation))
exclude_rows <- c(na_rows)
for (row in na_rows){
exclude_rows <- c(exclude_rows, which(data_temp$ID==data_temp[row,]$ID))
}
exclude_rows <- unique(exclude_rows)
return(data_temp[-exclude_rows,])
}else{
return(data_from_list)
}
}
save_results <- function(data_list){
results <- vector("list")
n = 0
for (set in data_list){
n = n +1
results[[names(data)[n]]] <- paired_test(exclude_NA_rows(set))
}
return(results)
}
results_export <- function(results, filename){
n = 0
for (set in results){
n = n +1
temp_df <- as.data.frame(set)
temp_df <- cbind(a = rep(names(results)[n], length(set[,1])), temp_df)
if (n == 1){
results_export <- as.data.frame(temp_df)
}else{
results_export <- rbind(results_export, temp_df)
}
}
write.xlsx(results_export, filename)
return(results_export)
}
results <- save_results(data)
export <- results_export(results, "Results_confocal.xlsx")