-
Notifications
You must be signed in to change notification settings - Fork 0
/
UV-Laser_ablation_evaluation.R
166 lines (164 loc) · 7.52 KB
/
UV-Laser_ablation_evaluation.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
#segmented package for the piecewise linear regression
library("segmented")
library("zoo")
library("strucchange")
library("outliers")
library("MASS")
library("robustbase")
killbreaks <- function(breaks) {
if ((breaks[2]-breaks[1]) < 30) {
breaks <- breaks[-2];
breaks <- killbreaks(breaks);
}
return(breaks)
}
#user selects the parent directory for the files
dir <- choose.dir(default = "", caption = "Select folder")
#looking for all csv files from knime
name.files <- list.files(path = dir, pattern = "csv", all.files = FALSE,
full.names = FALSE, recursive = TRUE,
ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
k <- length(name.files)
#starting a loop that loads each csv file after another and starts the regression
for (i in 1:k) {
try({
#loading the data
df.cut <- read.table(paste(dir,"\\",name.files[i[1]],sep=""), sep=";", header=TRUE);
offset <- df.cut[1,1]-1
df.cut <- data.frame((df.cut$x-df.cut[1,1]+1),(df.cut$Y));
df.cut
names(df.cut) <- c("x", "Y");
df.cut$Y <- c(head(df.cut$Y, 2),rollmean(df.cut$Y,5),tail(df.cut$Y, 2))
#generate ln of x
df.Logx <- data.frame(log(df.cut[1]),df.cut[2]);
bp <- breakpoints(Y ~ x, data = df.Logx, breaks = 4);
reg <- segmented.lm(obj = lm(Y ~ x, data = df.Logx),
seg.Z = ~ x, psi = NA,
seg.control (toll = 1e-5, it.max = 100, n.boot = 100, h = 1, K = length(bp$breakpoints), display = FALSE));
#saving breakpoints to Variable
breaks <- exp(reg$psi[(length(bp$breakpoints)+1):(length(bp$breakpoints)*2)])
#breaks <- breaks[which(breaks < (145-offset) | breaks > (161-offset))]
if (breaks[1] < 25) {
breaks <- killbreaks(breaks);
}
span <- max(df.cut$Y)-min(df.cut$Y[1:100])
#regression of the whole graph
span.checks <- c()
##for (a in 1:length(breaks)) {
## if (((df.cut$Y[which(abs(df.cut$x-breaks[a])==min(abs(df.cut$x-breaks[a])))]-min(df.cut$Y[1:100])) < 0.85*span) & ((df.cut$Y[which(abs(df.cut$x-breaks[a])==min(abs(df.cut$x-breaks[a])))]-min(df.cut$Y[1:100])) > 0.15*span) ) {
## span.checks <- c(span.checks, a);
## };
## };
## if (length(span.checks) > 0) {
## breaks <- breaks[-span.checks];
##}
for (n in 1:length(breaks)) {
nam <- paste ("X",n-1, sep = "");
assign(nam, breaks[n]);
};
if (breaks[1] < 40) {
if (length(breaks) == 4) {
kinetics <- nls (Y ~ (x < X0)* (N0) +
(x >= X0 & x < X1)* (N0 + N1*(1 - exp(-kon * (x-X0)))) +
(x >= X1 & x < X2)* (N0 + N1) +
(x >= X2 & x < X3)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X2)))) +
(x >= X3)* (N2),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("TRUE","TRUE","TRUE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
};
if (length(breaks) == 2) {
kinetics <- nls (Y ~ (x < X0)* (N0) +
(x >= X0 & x < X1)* (N0 + N1*(1 - exp(-kon * (x-X0)))) +
(x >= X1 )* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X1)))),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("TRUE","FALSE","FALSE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
};
if (length(breaks) == 3) {
if ((mean(df.cut$Y[(which(abs(df.cut$x-breaks[2])==min(abs(df.cut$x-breaks[2])))):(which(abs(df.cut$x-breaks[3])==min(abs(df.cut$x-breaks[3]))))])-min(df.cut$Y[1:100])) < 0.85*span) {
kinetics <- nls (Y ~ (x < X0)* (N0) +
(x >= X0 & x < X1)* (N0 + N1*(1 - exp(-kon * (x-X0)))) +
(x >= X1 & x < X2)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X1)))) +
(x >= X2)* (N2),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("TRUE","FALSE","TRUE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
} else {
kinetics <- nls (Y ~ (x < X0)* (N0) +
(x >= X0 & x < X1)* (N0 + N1*(1 - exp(-kon * (x-X0)))) +
(x >= X1 & x < X2)* (N0 + N1) +
(x >= X2)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X2)))),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("TRUE","TRUE","FALSE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
};
};
} else {
if (length(breaks) == 3) {
kinetics <- nls (Y ~ (x < X0)* (N0 + N1*(1 - exp(-kon * (x)))) +
(x >= X0 & x < X1)* (N0 + N1) +
(x >= X1 & x < X2)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X1)))) +
(x >= X2)* (N2),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("FALSE","TRUE","TRUE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
};
if (length(breaks) == 1) {
kinetics <- nls (Y ~ (x < X0)* (N0 + N1*(1 - exp(-kon * (x)))) +
(x >= X0)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X0)))),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("FALSE","FALSE","FALSE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
};
if (length(breaks) == 2) {
if ((mean(df.cut$Y[(which(abs(df.cut$x-breaks[1])==min(abs(df.cut$x-breaks[1])))):(which(abs(df.cut$x-breaks[2])==min(abs(df.cut$x-breaks[2]))))])-min(df.cut$Y)) < 0.85*span) {
kinetics <- nls (Y ~ (x < X0)* (N0 + N1*(1 - exp(-kon * (x)))) +
(x >= X0 & x < X1)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X0)))) +
(x >= X1)* (N2),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("FALSE","FALSE","TRUE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
} else {
kinetics <- nls (Y ~ (x < X0)* (N0 + N1*(1 - exp(-kon * (x)))) +
(x >= X0 & x < X1)* (N0 + N1) +
(x >= X1)* (N2 + (N0 + N1 - N2)*(exp(-koff * (x-X1)))),
start = list (N0 = head(df.cut$Y,1), N1 = max(df.cut$Y), N2 = tail(df.cut$Y,1), kon = 0.025, koff = 0.004),
data = df.cut,
control=nls.control(minFactor=1e-5, maxiter=10000, warnOnly = TRUE));
Structure <- c("FALSE","TRUE","FALSE");
Structure <- setNames(Structure, c("Lag","Plateau","Tail"));
};
};
};
#saving the relevant parameters
names <- c()
for (n in 1:length(breaks)) {
names <- c(names,paste ("X",n-1, sep = ""));
};
names <- c(names, "R2");
breaks <- c(breaks, (1-(kinetics$m$deviance()/(sum((df.cut$Y-mean(df.cut$Y))^2)))))
out.breaks <- setNames(breaks,names);
output <- c(coefficients(kinetics), out.breaks,Structure)
write.table(output, paste(dir,"\\",name.files[i[1]],".txt",sep=""), sep="\t")
#saving a plot of the fitted curves and raw data
jpeg(paste(dir,"\\",name.files[i[1]],".jpg",sep=""));
plot(df.cut$x,kinetics$m$fitted(),xlab = "time in sec", ylab = "mean bg corrected intensity");
points(df.cut$x,df.cut$Y,col="red");
dev.off();
});
}