forked from eschultzphd/RootMeasurement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoRun_Tips_Proto.R
197 lines (169 loc) · 6.89 KB
/
AutoRun_Tips_Proto.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
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
#(C) Eric R Schultz 2017
####SET PATHS AND CORRECTION FACTORS####
library(plyr)
library(plotrix)
library(dplyr)
Pre <- function() {
message("Enter Scale Correction Factor")
CF <- scan(n = 1)
message("\n","Enter Angle B Correction Factor")
BCF <- scan(n = 1)
paste(CF, BCF, sep = " ")
}
Path <- file.choose()
#### IMPORTING ####
#Split output from RunFirst
strsplit(Pre(), " ")->Settings
t(data.frame(unlist(Settings)))->set2
as.numeric(set2[,1])->CF
as.numeric(set2[,2])->BCF
#Parse data into distinct time points
snakes <- read.delim(Path, header=FALSE, skip=11)
time <- lapply(1:(NROW(unique(snakes[,1]))-2), function(x){paste0("t-",x)}) #assign var
times <- lapply(1:(NROW(time)), function(x){snakes[snakes[,"V1"]==x,]}) #separation occurs
#Determine number of traces (snakes)
totpl<-sum(times[[1]][,2]==0)
#First/Last point identification
FLs <- lapply(1:NROW(times), function(x){suppressWarnings(
filter(
times[[x]],
times[[x]][,2]==0 |
abs((times[[x]][(1:NROW(times[[x]])),"V2"]-
times[[x]][(1:NROW(times[[x]]))+1,"V2"])>0) |
times[[x]][(1:NROW(times[[x]])),"V4"]-
times[[x]][(1:NROW(times[[x]]))-1,"V4"]>0)
)})
#### QUANTIFICATION, CALCULATION, AND CONCATINATION ####
#Calculation of Length Along Trace or Integrated Length (L; using times)
L1 <- lapply(1:NROW(times), function(x){
ifelse(
(times[[x]][1:NROW(times[[x]]),2]-times[[x]][1:NROW(times[[x]])+1,2])<0,
(sqrt(((times[[x]][1:NROW(times[[x]]),3] - times[[x]][1:NROW(times[[x]])+1,3])^2) +
(times[[x]][1:NROW(times[[x]]),4] - times[[x]][1:NROW(times[[x]])+1,4])^2)),
NA)
})
L2 <- lapply(1:NROW(times), function(x){
split(L1[[x]],ifelse(is.na(L1[[x]]),NA,cumsum(is.na(L1[[x]]))))
})
L3 <- lapply(1:NROW(times), function(x){
lapply(L2[[x]],sum)
})
LENGTH <- lapply(1:NROW(times), function(x){
unlist(L3[[x]])/CF
})
#Calculation of Length of the Cord or First Point to Last Point (Lc; using FLs)
Lc <- lapply(1:NROW(FLs), function(x){
(sqrt(
((FLs[[x]][seq(1, by=2, NROW(FLs[[x]])),3] -
FLs[[x]][seq(1, by=2, NROW(FLs[[x]]))+1,3])^2) +
((FLs[[x]][seq(1, by=2, NROW(FLs[[x]])),4] -
FLs[[x]][seq(1, by=2, NROW(FLs[[x]]))+1,4])^2)
)
/CF)
})
#Calculation of Angle of Trace (B; using FLs)
B <- lapply(1:NROW(FLs), function(x){
(atan((FLs[[x]][seq(1, by=2, NROW(FLs[[x]]))+1,3] -
FLs[[x]][seq(1, by=2, NROW(FLs[[x]])),3]) /
(FLs[[x]][seq(1, by=2, NROW(FLs[[x]]))+1,4] -
FLs[[x]][seq(1, by=2, NROW(FLs[[x]])),4])
)
* (180/pi)
+ BCF)
})
#Lc/L for straightness
STR <- relist((unlist(Lc)/unlist(LENGTH))*100, Lc)
#Lx/L for HGI
HGI <- relist(((sin(unlist(B)*(pi/180))*unlist(Lc))/unlist(LENGTH))*100, Lc)
##Counting Wave Number for Wave Density (WD; using times)
Wn1 <- lapply(1:NROW(times), function(x){
ifelse(
(
(times[[x]][1:NROW(times[[x]]),2] - times[[x]][1:NROW(times[[x]])+1,2])<0),
(times[[x]][1:NROW(times[[x]]),3] - times[[x]][1:NROW(times[[x]])+1,3]),
NA
)
}) #Count directional changes
Wn2 <- lapply(1:NROW(times), function(x){
split(Wn1[[x]][!is.na(Wn1[[x]])],cumsum(is.na(Wn1[[x]]))[!is.na(Wn1[[x]])])
}) #Split into list
Wn3 <- lapply(1:NROW(times), function(x){
mapply("-", Wn2[[x]], lapply(Wn2[[x]],mean), SIMPLIFY = FALSE)
}) #Calculate and correct using mean of trace
Wn <- lapply(1:NROW(times), function(x){
as.list(
sapply(lapply(lapply(lapply(Wn3[[x]], sign), diff), abs), sum)/2)
}) #Calculate Wave Number (Wn)
WD <- lapply(1:NROW(times), function(x){
unlist(Wn[[x]])/LENGTH[[x]]}) #Wn to WD; waves / mm
#Trace tip angle, used for gravitropic assays (i.e. Root tip curvature in response to grav)
tips <- lapply(1:NROW(times), function(x){suppressWarnings(
rbind(
filter(
times[[x]],
times[[x]][(1:NROW(times[[x]]))+5,"V2"]==0 | #5th point from end
times[[x]][(1:NROW(times[[x]]))+1,"V2"]==0), #End points
head(tail(times[[x]], n=-(NROW(times[[x]])-5)), n=1), #5th from end of end of list
tail(times[[x]], n=-(NROW(times[[x]])-1))) #End point of end of list
)})
btip <- lapply(1:NROW(tips), function(x){
(atan((tips[[x]][seq(1, by=2, NROW(tips[[x]]))+1,3] -
tips[[x]][seq(1, by=2, NROW(tips[[x]])),3]) /
(tips[[x]][seq(1, by=2, NROW(tips[[x]]))+1,4] -
tips[[x]][seq(1, by=2, NROW(tips[[x]])),4])
)
* (180/pi)
+ BCF)
})
#Apply stats and build table
L.mean <- lapply(1:NROW(times), function(x){mean(LENGTH[[x]])})
Lc.mean <- lapply(1:NROW(times), function(x){mean(Lc[[x]])})
STR.mean <- lapply(1:NROW(times), function(x){mean(STR[[x]])})
WD.mean <- lapply(1:NROW(times), function(x){mean(WD[[x]])})
B.mean <- lapply(1:NROW(times), function(x){mean(B[[x]])})
btip.mean <- lapply(1:NROW(times), function(x){mean(btip[[x]])})
HGI.mean <- lapply(1:NROW(times), function(x){mean(HGI[[x]])})
L.sd <- lapply(1:NROW(times), function(x){sd(LENGTH[[x]])})
Lc.sd <- lapply(1:NROW(times), function(x){sd(Lc[[x]])})
STR.sd <- lapply(1:NROW(times), function(x){sd(STR[[x]])})
WD.sd <- lapply(1:NROW(times), function(x){sd(WD[[x]])})
B.sd <- lapply(1:NROW(times), function(x){sd(B[[x]])})
btip.sd <- lapply(1:NROW(times), function(x){sd(btip[[x]])})
HGI.sd <- lapply(1:NROW(times), function(x){sd(HGI[[x]])})
L.combo <- lapply(1:NROW(times), function(x){
c(unlist(LENGTH[[x]]), L.mean[[x]], L.sd[[x]])})
Lc.combo <- lapply(1:NROW(times), function(x){
c(unlist(Lc[[x]]), Lc.mean[[x]], Lc.sd[[x]])})
STR.combo <- lapply(1:NROW(times), function(x){
c(unlist(STR[[x]]), STR.mean[[x]], STR.sd[[x]])})
WD.combo <- lapply(1:NROW(times), function(x){
c(unlist(WD[[x]]), WD.mean[[x]], WD.sd[[x]])})
B.combo <- lapply(1:NROW(times), function(x){
c(unlist(B[[x]]), B.mean[[x]], B.sd[[x]])})
btip.combo <- lapply(1:NROW(times), function(x){
c(unlist(btip[[x]]), btip.mean[[x]], btip.sd[[x]])})
HGI.combo <- lapply(1:NROW(times), function(x){
c(unlist(HGI[[x]]), HGI.mean[[x]],HGI.sd[[x]])})
all.dat <- as.data.frame(c(
L.combo, "",
Lc.combo, "",
STR.combo, "",
WD.combo, "",
B.combo, "",
btip.combo, "",
HGI.combo)) #Final table build
rownames(all.dat) <- c(paste("Trace", 1:totpl), "Mean", "Std. Dev")
colnames(all.dat) <- c(paste("Length - Time ", 1:NROW(times)), "",
paste("Lc - Time", 1:NROW(times)), "",
paste("STR - Time", 1:NROW(times)), "",
paste("WD - Time", 1:NROW(times)), "",
paste("B - Time", 1:NROW(times)), "",
paste("Tip Angle - Time", 1:NROW(times)), "",
paste("HGI - Time", 1:NROW(times)))
#### EXPORT AND SAVING ####
SavePath <- dirname(Path)
message("\n","Enter filename (Use _ insead of space):")
processed.filename <- scan(n = 1, what = "character") # read 1 line from console
Save.Path <- file.path(SavePath, paste0(processed.filename,".csv"))
write.csv(all.dat, Save.Path)
message("\n","Process complete - file saved")