-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy_maze_data_processing_macro_v3.R
325 lines (283 loc) · 9.92 KB
/
y_maze_data_processing_macro_v3.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
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
insertRow <- function(existingDF, newrow, r) {
existingDF[seq(r+1,nrow(existingDF)+1),] <- existingDF[seq(r,nrow(existingDF)),]
existingDF[r,] <- newrow
existingDF
}
options(warn=1)
# setwd(file.location)
args = commandArgs(trailingOnly=TRUE)
output.file<-args[2] #paste0("MNBF_Y-MAZE_SponAlt"," ","2020Jul1_SponAlt_Val_ZB",".csv")
ptm <- proc.time()
if (length(args)==0) {
stop("At least one argument must be supplied (input file).n", call.=FALSE)
} else {
files = unlist(strsplit(args[1],","))
}
library(readxl)
# files = list.files(pattern="*.xlsx")
Ymaze<-NULL
aa<-as.numeric(length(files))
#v3 change need to be as numeric for loop
for(a in 1:aa){
num.sheet<-as.numeric(length(excel_sheets(files[a])))
#v3 change need to be as numeric for loop
sheet.name<-excel_sheets(files[a])
for(b in 1:num.sheet){
############################################################
#meta data information
trialnum<-suppressMessages(read_excel(files[a],sheet=b,range = cell_rows(1:1),col_names = FALSE, na="-"))
num.row<-as.numeric(as.character(trialnum[1,2]))-1
meta.data<-suppressMessages(read_excel(files[a],sheet=b,range = cell_rows(1:num.row),col_names = FALSE, na="-"))
meta.data<-data.frame(meta.data)
###new version of R changes no name to ...1
####so when searching for char need to search metadata$...1 not
### as old version used metadata$X__1
trial.row<-match("Trial name", as.character(meta.data$...1))
arena.row<-match("Arena name", as.character(meta.data$...1))
date.row<-match("Start time", as.character(meta.data$...1))
subject.row<-match("mouse id", tolower(as.character(meta.data$...1)))
Trial.Number<-as.character(meta.data[trial.row,2])
Arena<-as.character(meta.data[arena.row,2])
MNBF.ID<-as.numeric(as.character(meta.data[subject.row,2]))
Date<-unlist(strsplit(as.character(meta.data[date.row,2])," "))[1]
Time<-unlist(strsplit(unlist(strsplit(as.character(meta.data[date.row,2])," "))[2],"[.]"))[1]
#read data and refind column names
dat<-read_excel(files[a],sheet=b,skip = num.row-1, na="-")
#changed to num.row -1 to get only the data V3
dat<-data.frame(dat)
dat<- dat[-c(1),]
#removed the unit line so column headers match V3
#colnames(dat)[2]<-"s"
coldat<-colnames(data.frame(read_excel(files[a],sheet=b,skip = num.row-1,na="-")))
if(sheet.name[b]=="Track-Arena 1-Subject 1"){
# if(is.na(match("In.zone.A1...Center.point.",coldat))){
# colnames(dat)[match("In.zone.1a...Center.point.",coldat)]<-"A"
#colnames(dat)[match("In.zone.1b...Center.point.",coldat)]<-"B"
#colnames(dat)[match("In.zone.1c...Center.point.",coldat)]<-"C"
#}else{
colnames(dat)[match("In.zone.A1...Center.point.",coldat)]<-"A"
colnames(dat)[match("In.zone.B1...Center.point.",coldat)]<-"B"
colnames(dat)[match("In.zone.C1...Center.point.",coldat)]<-"C"
#upper case Center.point for V3
# }
}else{
# if(is.na(match("In.zone.A2...center.point.",coldat))){
# colnames(dat)[match("In.zone.2a...Center.point.",coldat)]<-"A"
# colnames(dat)[match("In.zone.2b...Center.point.",coldat)]<-"B"
# colnames(dat)[match("In.zone.2c...Center.point.",coldat)]<-"C"
#}else{
colnames(dat)[match("In.zone.A2...Center.point.",coldat)]<-"A"
colnames(dat)[match("In.zone.B2...Center.point.",coldat)]<-"B"
colnames(dat)[match("In.zone.C2...Center.point.",coldat)]<-"C"
# }
}
#Convert misdetected entries to 0
dat[which(is.na(dat$A)),]$A<-rep(0,dim(dat[which(is.na(dat$A)),])[1])
dat[which(is.na(dat$B)),]$B<-rep(0,dim(dat[which(is.na(dat$B)),])[1])
dat[which(is.na(dat$C)),]$C<-rep(0,dim(dat[which(is.na(dat$C)),])[1])
dat[which(dat$A=="-"),]$A<-rep(0,dim(dat[which(dat$A=="-"),])[1])
dat[which(dat$B=="-"),]$B<-rep(0,dim(dat[which(dat$B=="-"),])[1])
dat[which(dat$C=="-"),]$C<-rep(0,dim(dat[which(dat$C=="-"),])[1])
dat$A<-as.numeric(as.character(dat$A))
dat$B<-as.numeric(as.character(dat$B))
dat$C<-as.numeric(as.character(dat$C))
#Deal with non-A arm starters
cnum1<-match("A",colnames(dat))
cnum2<-match("B",colnames(dat))
cnum3<-match("C",colnames(dat))
if(sum(dat[1,c(cnum1,cnum2,cnum3)])==0){
dat<-insertRow(dat,dat[1,],1)
dat$A[1]<-1
}
if(dat$A[1]==0){
dat<-insertRow(dat,dat[1,],1)
dat[1,c(cnum1,cnum2,cnum3)]<-c(1,0,0)
}
#frame jump
insert<-NULL
for(fj in 1:(dim(dat)[1]-1)){
se1<-match(1, dat[fj,c(cnum1,cnum2,cnum3)])
se2<-match(1, dat[fj+1,c(cnum1,cnum2,cnum3)])
se<-c(se1,se2)
if(!TRUE%in%is.na(se) & se1!=se2){
insert<-c(insert,fj)
}
}
if(length(insert)>0){
for(ir in 1:length(insert)){
newrow<-dat[insert[ir]+ir,]
newrow[,c(cnum1,cnum2,cnum3)]<-rep(0,3)
dat<-insertRow(dat,newrow,insert[ir]+ir)
}
}
code<-apply(dat[,c(cnum1,cnum2,cnum3)],1,sum)
dat<-cbind(code,dat)
row<-match(0,dat$code)
colnames(dat)[2]<-"s"
if(!is.na(row)){
t1<-dat$s[1]
t2<-dat$s[row]
if(1%in%dat$A[1:(row-1)]){
t3<-c("Start","End")
}else{
t3<-c(NA,NA)
}
if(1%in%dat$B[1:(row-1)]){
t4<-c("Start","End")
}else{
t4<-c(NA,NA)
}
if(1%in%dat$C[1:(row-1)]){
t5<-c("Start","End")
}else{
t5<-c(NA,NA)
}
temp<-cbind(c(t1,t2),t3,t4,t5)
dat1<-temp
while(row<dim(dat)[1] & !is.na(match(1,dat[-c(1:row),]$code))){
num1<-match(1,dat[-c(1:row),]$code)
num2<-row+num1
num3<-match(0,dat[-c(1:num2),]$code)
if(!is.na(num3)){
row<-num2+num3
t1<-dat$s[num2]
t2<-dat$s[row]
if(1%in%dat$A[num2:(row-1)]){
t3<-c("Start","End")
}else{
t3<-c(NA,NA)
}
if(1%in%dat$B[num2:(row-1)]){
t4<-c("Start","End")
}else{
t4<-c(NA,NA)
}
if(1%in%dat$C[num2:(row-1)]){
t5<-c("Start","End")
}else{
t5<-c(NA,NA)
}
temp<-cbind(c(t1,t2),t3,t4,t5)
dat1<-rbind(dat1,temp)
}else{
row<-dim(dat)[1]
t1<-dat$s[num2]
t2<-dat$s[row]
if(1%in%dat$A[num2:row]){
t3<-c("Start","End")
}else{
t3<-c(NA,NA)
}
if(1%in%dat$B[num2:row]){
t4<-c("Start","End")
}else{
t4<-c(NA,NA)
}
if(1%in%dat$C[num2:row]){
t5<-c("Start","End")
}else{
t5<-c(NA,NA)
}
temp<-cbind(c(t1,t2),t3,t4,t5)
dat1<-rbind(dat1,temp)
}
}
}else{
t1<-dat$s[1]
t2<-dat$s[dim(dat)[1]]
if(1%in%dat$A){
t3<-c("Start","End")
}else{
t3<-c(NA,NA)
}
if(1%in%dat$B){
t4<-c("Start","End")
}else{
t4<-c(NA,NA)
}
if(1%in%dat$C){
t5<-c("Start","End")
}else{
t5<-c(NA,NA)
}
temp<-cbind(c(t1,t2),t3,t4,t5)
dat1<-temp
}
dat1<-data.frame(dat1)
colnames(dat1)<-c("Time","A","B","C")
dat1$Time<-as.numeric(as.character(dat1$Time))
#time in each arm
time1<-time2<-time3<-0
j<-1
while (j<=(dim(dat1)[1]-1)) {
if(!is.na(dat1$A[j])){
time1<-time1+dat1$Time[j+1]-dat1$Time[j]
}
if(!is.na(dat1$B[j])){
time2<-time2+dat1$Time[j+1]-dat1$Time[j]
}
if(!is.na(dat1$C[j])){
time3<-time3+dat1$Time[j+1]-dat1$Time[j]
}
j<-j+2
}
Sequence<-NULL
TrialTime<-NULL
First<-c("A","B","C")[match("2",as.character(dat1[1,-1]))]
Sequence<-c(Sequence,First)
TrialTime<-c(TrialTime,dat1$Time[1])
i<-3
while(i<dim(dat1)[1]){
entry<-c("A","B","C")[match("2",as.character(dat1[i,-1]))]
if(entry==Sequence[length(Sequence)]){
lag<-dat1$Time[i]-dat1$Time[i-1]
if(lag>1){
Sequence<-c(Sequence,entry)
TrialTime<-c(TrialTime,dat1$Time[i])
}
}else{
Sequence<-c(Sequence,entry)
TrialTime<-c(TrialTime,dat1$Time[i])
}
i<-i+2
}
Triad<-c("ABC","ACB","BAC","BCA","CAB","CBA")
alt<-0
for(i in 1:(length(Sequence)-2)){
trial<-paste0(Sequence[i],Sequence[i+1],Sequence[i+2])
if(trial%in%Triad){
alt<-alt+1
}
}
#parameters
Latency.to.Leave.Start.Arm<-TrialTime[2]-TrialTime[1]
Total.Arm.Entries<-length(Sequence)
Number.of.Triplets<-Total.Arm.Entries-2
Number.of.Spontaneous.Alternations<-alt
Alternation.Ratio<-100*alt/(length(Sequence)-2)
Entry.Sequence<-paste(Sequence,collapse = "")
A.Arm.Entry<-table(Sequence)[1]
B.Arm.Entry<-table(Sequence)[2]
C.Arm.Entry<-table(Sequence)[3]
A.Arm.Time<-time1
B.Arm.Time<-time2
C.Arm.Time<-time3
Animal<-cbind(Trial.Number,
Arena,
MNBF.ID,
Entry.Sequence,
Total.Arm.Entries,
Number.of.Triplets,
Number.of.Spontaneous.Alternations,
Alternation.Ratio,
Latency.to.Leave.Start.Arm,
A.Arm.Entry,B.Arm.Entry,C.Arm.Entry,
A.Arm.Time,B.Arm.Time,C.Arm.Time,
Date,
Time)
Ymaze<-rbind(Ymaze,Animal)
cat(Date, " ", Trial.Number," ", Arena," ", "is now finished", "\n")
}
}
write.csv(Ymaze,file=output.file,row.names = FALSE)
proc.time() - ptm