Repository navigation
Expand file tree
/
Copy pathfinal.R
More file actions
1368 lines (1164 loc) · 48 KB
/
Copy pathfinal.R
File metadata and controls
1368 lines (1164 loc) · 48 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## WELCOME TO PIPLINE:
# 1. before running - use the GUI in matlab to create "matlab_to_R_GUI_data.xls"
# 2. this code needs two input files (CumulativeFeedingInExcelFormat.xlsx, DataInExcelFormat.xls)
# both has to be in the same directory of this file
# imports
library("readxl")
library("writexl")
library("stringr")
library("stringi")
library("openxlsx")
library(numbers)
library(ggplot2)
library(reshape2)
library(plyr)
library(lmerTest)
library(car)
library(emmeans)
options(scipen=999)
library(grid)
library("Hmisc")
library("dplyr")
library(svDialogs)
library(extrafont)
library(tidyverse)
library(gridExtra)
# another stuff
options(scipen=999)
setwd("C:/Users/labophir/Desktop/New folder") # NOTE - in another computers, this path need to be change to the right path
graphics.off()
par("mar")
par(mar=c(1,1,1,1))
# create pipeline directory
dir.create(file.path('pipeline'),recursive=TRUE, showWarnings = FALSE)
# ____________________________________________________
# 1. organizer: organize the data.
# need: A. input file 1 - CumulativeFeedingInExcelFormat.xlsx format, in the same directory
# B. input file 2 - DataInExcelFormat.xls format (with "Electrodes" sheet) in the same directory
# C. "matlab_to_R_GUI_data.xls" (created by matlab, no need to change location)
# create: A. "colors_hex.csv"
# B. organized_file
# C. organized_file_without_zeros
GUI_data = read_excel("matlab_to_R_GUI_data.xls")
if (GUI_data$to_do_org == TRUE) { # then get into the organizer part
# create output directory + get the data file
data_file = GUI_data$org_file
data_file = gsub("\\\\", "/", data_file)
data_file = basename(data_file)
first_file_name = tools::file_path_sans_ext(data_file)
first_path = paste0('pipeline/', first_file_name)
dir.create(file.path(first_path),recursive=TRUE, showWarnings = FALSE)
# electrodes data
nums_file = GUI_data$elec_file
nums_file = gsub("\\\\", "/", nums_file)
nums_file = basename(nums_file)
# handle case where the first sheet is empty
sheets <- excel_sheets(c(data_file))
start = 1
if (all(sheets[1] == "Sheet1")) {
actual_sheets = sheets[2:length(sheets)]
start = 2
} else {
actual_sheets = sheets[1:length(sheets)]
}
# create "colors_hex.csv"
condition = unlist(actual_sheets) # default value
is_condition = "F"
colors = c("red", "orange", "green", "cyan", "blue", "purple", "pink", "gray") # default value
if (!is.na(GUI_data$stat_colors)) {
colors = as.list(strsplit(GUI_data$stat_colors, ",")[[1]])
}
colors = colors[1:length(condition)]
df = cbind(condition = unlist(condition), color = unlist(colors))
df = as.data.frame(df)
write.csv(df, "colors_hex.csv") # in the working directory
# now creates the other files
# add time column
init_file = openxlsx::read.xlsx(data_file, sheet = 2) # add the sheet
time <- c(seq(from = 10/60, to = 10/60*ncol(init_file), by = 10/60))
all_files <- list(as.data.frame(time))
num_of_groups = length(sheets) - start + 1
groups_names = list()
# loop through all the sheets
# create the df
for (i in start:length(sheets)) {
curr_file = openxlsx::read.xlsx(data_file, sheets[i])
curr_file = as.data.frame(t(curr_file))
# find the numbers to the row of names
till <- ncol(curr_file) + 1
nums = read_excel(nums_file, sheet = "Electrodes", range = anchored("A1", dim = c(till, 1)))
nums <- t(nums)
# if there is new names of groups (entered in the GUI), update the data
if (is_condition == "T") {
nums <- paste0(as.character(condition[i-1]), "-",(nums+1)/2)
} else {
nums <- paste0(sheets[i], "-",(nums+1)/2)
}
names(curr_file) <- c(nums)
# wrap up
all_files <- cbind(all_files, curr_file)
}
# export with zeros cols and NA
name = paste0("organized_", first_file_name, ".xlsx")
write_xlsx(all_files, path = paste0(first_path, "/", name))
# export without NA
all_files <- all_files[, !grepl('NA', colnames(all_files)) > 0]
name = paste0("organized_without_zeros_", tools::file_path_sans_ext(data_file), ".xlsx")
write_xlsx(all_files, path = paste0(first_path, "/", name))
# if there is columns that contain only zero,
# they need to be removed from all of the groups
# find indexes
sums_arr <- colSums(all_files)
places = c()
for (i in 1:length(sums_arr)) {
if (sums_arr[i] == 0) {
places <- append(places, i)
}
}
# calc which cols should be removed
items_in_group = till-1
if (length(places) > 0) {
div = c(mod(places, items_in_group))
to_remove = c()
for (num in div) {
res = num - items_in_group
while (res + items_in_group <= items_in_group*num_of_groups) {
res = res + items_in_group
to_remove <- append(to_remove, res)
}
res = 0
}
# remove
all_files <- all_files[,-to_remove]
# export without zeros cols in every group
write_xlsx(all_files, path = paste0(first_path, "/", name))
}
print("ORGANIZER - DONE")
}
# ________________________________________________
# 2. Jennifer's statistic: do statistics and plots.
# combination of two programs- led or no led.
# need: A. input file organized_file or organized_file_without_zeros
# B. "colors_hex.csv"
# create: Jennifer directory with many files inside
if (GUI_data$to_do_org == TRUE) {
# first, check if input data contain led to know which code to activate
capital = str_detect(sheets, "LED")
small = str_detect(sheets, "led")
both = str_detect(sheets, "Led")
if (any(capital == "TRUE") || any(small == "TRUE") || any(both == "TRUE")) {
# there is led - go to code_logistic_regression_no_agar
# parameters:
#------------
file = paste0('pipeline/', first_file_name, '/', name) # the output of the last code
# not suppose to happened but anyway
if (is.na(file)) {
file = GUI_data$org_file
file = gsub("\\\\", "/", file)
file = basename(file)
}
# remove outliers
remove.outliers <- T
# load colors (CSV format, hexadecimals or RGB)
colors <- read.csv('colors_hex.csv')
if("X" %in% colnames(colors)) {
colors = select(colors, -X)
}
if(ncol(colors)>2) #if RGB - convert to hexadecimal values
colors$color <- rgb(colors$R,colors$G, colors$B, maxColorValue=255)
# load data
df <- as.data.frame(read_excel(file))
colnames(df) <- gsub('Naֳ¯ve', 'Naive', colnames(df))
# retrieve file name
file.name <- substr(file, 1, nchar(file)-5)
# create output directory
dir.create(file.path(paste0(first_path, '/Jennifer/', '/plots/')),recursive=TRUE, showWarnings = FALSE)
# change to long format
colnames(df)[1] <- 'time'
df2 <- melt(df, id.vars = c('time'))
df2 <- df2[!is.na(df2$value),] # remove missing values
# retrieve fly ID - save in 'rep'
tmp <- strsplit(as.character(df2$variable), '-')
df2$rep <- as.factor(sapply(tmp, function(x) x[[2]]))
# retrieve condition and agar
tmp <- strsplit(sapply(tmp, function(x) x[[1]]),'_')
df2$condition <- as.factor(sapply(tmp, function(x) x[[1]]))
df2$agar <- as.factor(sapply(tmp, function(x) x[[2]]))
df2$sample <- as.factor(paste0(df2$condition, '_', df2$rep))
# plot mean values for each group with original data and save plot
m <- ddply(df2, ~time+condition+agar, summarize, med=median(value), mean=mean(value)) # calculate mean values
p<-ggplot(data=df2, aes(x=time, y=value, group=variable)) +
geom_line() +
geom_line(data=m, aes(x=time, y=mean, group=condition), color='red', size=1.2) +
facet_grid(agar~condition)
pdf(paste0(first_path, '/Jennifer', '/plot_all_samp.pdf'), width = 7, height = 4)
print(p)
dev.off()
# perform logistic regression for each replicate (fly)
samp <- unique(df2$variable)
res <- data.frame(condition='', agar='', sample=as.character(samp), sample2=as.character(samp), Asym=0, xmid=0, scal=0, stringsAsFactors = F)
for(i in 1:length(samp)){
tmp.data <- df2[df2$variable==samp[i],]
res[i, 'condition'] <- as.character(tmp.data[1,'condition'])
res[i, 'agar'] <- as.character(tmp.data[1,'agar'])
res[i, 'sample'] <- as.character(tmp.data[1,'sample'])
tryCatch({
print(i)
# regression
model <-nls(value~SSlogis(time, Asym, xmid, scal), data=tmp.data)
# save model results
res[i, 'Asym'] <- coef(model)[1]
res[i, 'xmid'] <- coef(model)[2]
res[i, 'scal'] <- coef(model)[3]
# plot and save data and regression curves
tiff(paste0(first_path, '/Jennifer', '/plots/good_plot_', samp[i], '.tiff'), width = 700, height = 550)
plot(tmp.data$time,tmp.data$value)
lines(tmp.data$time,predict(model))
title(paste0(samp[i], ', ID: ', i))
dev.off()
}, error=function(e){
# in case model failed to converge
cat("BAD SAMPLE!!!!\n")
tiff(paste0(first_path, '/Jennifer', '/plots/bad_plot_', samp[i], '.tiff'), width = 700, height = 550)
plot(tmp.data$time,tmp.data$value)
title(paste0(samp[i], ', ID: ', i))
dev.off()
})
}
# remove bad samples (model failed to converge) and mark them as outliers
res$out <- 0
res[res$Asym==0,'out'] <- 1
res2 <- res[res$Asym!=0, ]
# remove outliers (based on IQR criterion)
### ONLY IF FLAG IS TRUE!!! ###
if(remove.outliers){
tmp <- boxplot(res2$Asym~res2$condition, plot=FALSE)$out
res2[res2$Asym%in%tmp,'out'] <- 1
tmp <- boxplot(res2$xmid~res2$condition, plot=FALSE)$out
res2[res2$xmid%in%tmp,'out'] <- 1
tmp <- boxplot(res2$scal~res2$condition, plot=FALSE)$out
res2[res2$scal%in%tmp,'out'] <- 1
}
# save parameters - with OUTLIERS
res[res$sample%in%res2[res2$out==1, 'sample'],'out'] <- 1
write.table(res,paste0(first_path, '/Jennifer', '/param_table.csv'), sep=',', row.names = F)
res2 <- res2[!res2$out,]
# remove bad samples from original data
df2 <- df2[which(df2$variable%in%res[res$out==0, 'sample2']),]
df2 <- df2[which(df2$variable%in%res2[res2$out==0, 'sample2']),]
# plot mean values
m <- ddply(df2, ~condition+agar+time, summarize, med=median(value), mean=mean(value), sd=sd(value), n=length(value) )
m$se <- m$sd/sqrt(m$n)
m$int <- as.factor(paste0(m$condition, '_', m$agar))
m$int <- factor(m$int, levels=colors$condition)
# with ribbons (SE)
p<-ggplot(data=m, aes(x=time, y=mean, color=int)) +
geom_line(size=1.5)+
scale_color_manual(values=colors$color)+
scale_fill_manual(values=colors$color)+
geom_ribbon(aes(ymin = mean-se, ymax = mean+se,fill = int),linetype=0, alpha=.3)+
theme_classic()+
scale_y_continuous(expand=c(0,0))+
scale_x_continuous(expand=c(0,0))+
theme(axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
axis.title.x = element_text(size=20),
legend.text =element_text(size=15),
legend.title = element_blank())+
ylab('Cumulative Number\nof Sips')+
xlab('Time (min)')
pdf(paste0(first_path, '/Jennifer', '/plot_mean_samp_with_SE.pdf'), width = 7, height = 4)
print(p)
dev.off()
# without ribbons (SE)
p<-ggplot(data=m, aes(x=time, y=mean, color=int)) +
geom_line(size=1.5)+
scale_color_manual(values=colors$color)+
scale_fill_manual(values=colors$color)+
theme_classic()+
scale_y_continuous(expand=c(0,0))+
scale_x_continuous(expand=c(0,0))+
theme(axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
axis.title.x = element_text(size=20),
legend.text =element_text(size=15),
legend.title = element_blank())+
ylab('Cumulative Number\nof Sips')+
xlab('Time (min)')
pdf(paste0(first_path, '/Jennifer', '/plot_mean_samp_without_SE.pdf'), width = 7, height = 4)
print(p)
dev.off()
###################################################
# plot parameter distributions
#-------------------------------------------------------
res2$int <- as.factor(paste0(res2$condition, '_', res2$agar))
res2$int <- factor(res2$int, levels=colors$condition)
# Asym
p<- ggplot(res2, aes(x=int, y=Asym, fill=int))+
geom_boxplot(width=0.3)+
xlab('')+
theme_bw()+
scale_fill_manual(values=colors$color)+
theme(axis.text.x = element_text(size=15, angle=45, hjust=1),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
legend.position = 'none')
pdf(paste0(first_path, '/Jennifer', '/plot_Asym_distibution.pdf'), width = 5, height = 4)
print(p)
dev.off()
# xmid
p<- ggplot(res2, aes(x=int, y=xmid, fill=int))+
geom_boxplot(width=0.3)+
xlab('')+
theme_bw()+
scale_fill_manual(values=colors$color)+
theme(axis.text.x = element_text(size=15, angle=45, hjust=1),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
legend.position = 'none')
pdf(paste0(first_path, '/Jennifer', '/plot_xmid_distibution.pdf'), width = 5, height = 4)
print(p)
dev.off()
# scal
p<- ggplot(res2, aes(x=int, y=scal, fill=int))+
geom_boxplot(width=0.3)+
xlab('')+
theme_bw()+
scale_fill_manual(values=colors$color)+
theme(axis.text.x = element_text(size=15, angle=45, hjust=1),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
legend.position = 'none')
pdf(paste0(first_path, '/Jennifer', '/plot_scal_distibution.pdf'), width = 5, height = 4)
print(p)
dev.off()
###################################################
# statistical tests
#-------------------------------------------------------
# Asym
# mixed linear model
model <- lmer(Asym~condition*agar+(1|sample), res2)
# 2-way ANOVA
a1<-anova(model)
a1 <- round(a1, digits=4)
a1 <- cbind(Asym=rownames(a1), a1)
write.table(a1, paste0(first_path, '/Jennifer', '/2way_anova.csv'), sep=',', row.names=F)
# post-hoc - pairwise comparisons
emm1 <- emmeans(model, ~condition*agar)
con1 <- as.data.frame(contrast(emm1, "pairwise", simple="each", combine=TRUE, adjust='fdr'))
# post hoc - test difference of differences
con1.diff <- as.data.frame(contrast(emm1, interaction = "pairwise", by = NULL))
colnames(con1.diff)[1] <- 'condition'
colnames(con1.diff)[2] <- 'agar'
con1.diff <- cbind(con1.diff[,c(2,1)], contrast='.', con1.diff[,3:ncol(con1.diff)])
all.con1 <- cbind(param='Asym', rbind(con1, con1.diff))
# xmid
# mixed linear model
model <- lmer(xmid~condition*agar+(1|sample), res2)
summary(model)
# 2-way ANOVA
a2<-anova(model)
a2 <- round(a2, digits=4)
a2 <- cbind(xmid=rownames(a2), a2)
write.table(a2, paste0(first_path, '/Jennifer', '/2way_anova.csv'), sep=',', row.names=F, append = T)
# post-hoc - pairwise comparisons
emm2 <- emmeans(model, ~condition*agar)
con2 <- as.data.frame(contrast(emm2, "pairwise", simple="each", combine=TRUE, adjust='fdr'))
# post hoc - test difference of differences
con2.diff <- as.data.frame(contrast(emm2, interaction = "pairwise", by = NULL))
colnames(con2.diff)[1] <- 'condition'
colnames(con2.diff)[2] <- 'agar'
con2.diff <- cbind(con2.diff[,c(2,1)], contrast='.', con2.diff[,3:ncol(con2.diff)])
all.con2 <- cbind(param='xmid', rbind(con2, con2.diff))
# scal
# mixed linear model
model <- lmer(scal~condition*agar+(1|sample), res2)
summary(model)
# 2-way ANOVA
a3<-anova(model)
a3 <- round(a3, digits=4)
a3 <- cbind(scal=rownames(a3), a3)
write.table(a3, paste0(first_path, '/Jennifer', '/2way_anova.csv'), sep=',', row.names=F, append = T)
# post-hoc - pairwise comparisons
emm3 <- emmeans(model, ~condition*agar)
con3 <- as.data.frame(contrast(emm3, "pairwise", simple="each", combine=TRUE, adjust='fdr'))
# post hoc - test difference of differences
con3.diff <- as.data.frame(contrast(emm3, interaction = "pairwise", by = NULL))
colnames(con3.diff)[1] <- 'condition'
colnames(con3.diff)[2] <- 'agar'
con3.diff <- cbind(con3.diff[,c(2,1)], contrast='.', con3.diff[,3:ncol(con3.diff)])
all.con3 <- cbind(param='scal', rbind(con3, con3.diff))
# bind all results
all.con <- rbind(all.con1, all.con2, all.con3)
all.con[5:ncol(all.con)] <- round(all.con[5:ncol(all.con)], digits=4)
# add significance asterisks
all.con$sig <- ''
all.con[all.con$p.value<0.05, 'sig']<- '*'
all.con[all.con$p.value<0.01, 'sig']<- '**'
all.con[all.con$p.value<0.001, 'sig']<- '***'
# save post hoc results
write.table(all.con, paste0(first_path, '/Jennifer', '/post_hoc.csv'), sep=',', row.names=F)
print("JENNIFER'S CODE DONE")
} else { # there is no led - go to code_logistic_regression
# parameters:
#------------
file = paste0('pipeline/', first_file_name, '/', name) # the output of the last code
# remove outliers
remove.outliers <- T
# load colors (CSV format, hexadecimals or RGB)
colors <- read.csv('colors_hex.csv')
if("X" %in% colnames(colors)) {
colors = select(colors, -X)
}
if(ncol(colors)>2) #if RGB - convert to hexadecimal values
colors$color <- rgb(colors$R,colors$G, colors$B, maxColorValue=255)
# load data
df <- as.data.frame(read_excel(file))
colnames(df) <- gsub('Naֳ¯ve', 'Naive', colnames(df))
# retrieve file name
file.name <- substr(file, 1, nchar(file)-5)
# create output directory
dir.create(file.path(paste0(first_path, '/Jennifer', '/plots/')),recursive=TRUE, showWarnings = FALSE)
# change to long format
colnames(df)[1] <- 'time'
df2 <- melt(df, id.vars = c('time'))
df2 <- df2[!is.na(df2$value),]
# retrieve fly ID - save in 'rep'
tmp <- strsplit(as.character(df2$variable), '-')
df2$rep <- as.factor(sapply(tmp, function(x) x[[2]]))
# retrieve conditions
tmp <- strsplit(sapply(tmp, function(x) x[[1]]),'_')
df2$condition <- as.factor(sapply(tmp, function(x) x[[1]]))
# plot mean values for each group with original data and save plot
m <- ddply(df2, ~time+condition, summarize, med=median(value), mean=mean(value)) # calculate mean values
p<-ggplot(data=df2, aes(x=time, y=value, group=variable)) +
geom_line() +
geom_line(data=m, aes(x=time, y=mean, group=condition), color='red', size=1.2) +
facet_grid(~condition)
pdf(paste0(first_path, '/Jennifer', '/plot_all_samp.pdf'), width = 7, height = 4)
print(p)
dev.off()
# perform logistic regression for each replicate (fly)
samp <- unique(df2$variable)
res <- data.frame(condition='', sample=as.character(samp), Asym=0, xmid=0, scal=0, stringsAsFactors = F)
for(i in 1:length(samp)){
tmp.data <- df2[df2$variable==samp[i],]
res[i, 'condition'] <- as.character(tmp.data[1,'condition'])
res[i, 'sample'] <- as.character(tmp.data[1,'variable'])
tryCatch({
print(i)
# regression
model <-nls(value~SSlogis(time, Asym, xmid, scal), data=tmp.data)
# save model results
res[i, 'Asym'] <- coef(model)[1]
res[i, 'xmid'] <- coef(model)[2]
res[i, 'scal'] <- coef(model)[3]
# plot and save data and regression curves
tiff(paste0(first_path, '/Jennifer', '/plots/good_plot_', samp[i], '.tiff'), width = 700, height = 550)
plot(tmp.data$time,tmp.data$value)
lines(tmp.data$time,predict(model))
title(paste0(samp[i], ', ID: ', i))
dev.off()
}, error=function(e){
# in case model failed to converge
cat("BAD SAMPLE!!!!\n")
tiff(paste0(first_path, '/Jennifer', '/plots/bad_plot_', samp[i], '.tiff'), width = 700, height = 550)
plot(tmp.data$time,tmp.data$value)
title(paste0(samp[i], ', ID: ', i))
dev.off()
})
}
# remove bad samples (model failed to converge) and mark them as outliers
res$out <- 0
res[res$Asym==0,'out'] <- 1
res2 <- res[res$Asym!=0,]
# remove outliers (based on IQR criterion)
### ONLY IF FLAG IS TRUE!!! ###
if(remove.outliers){
tmp <- boxplot(res2$Asym~res2$condition, plot=FALSE)$out
res2[res2$Asym%in%tmp,'out'] <- 1
tmp <- boxplot(res2$xmid~res2$condition, plot=FALSE)$out
res2[res2$xmid%in%tmp,'out'] <- 1
tmp <- boxplot(res2$scal~res2$condition, plot=FALSE)$out
res2[res2$scal%in%tmp,'out'] <- 1
}
# save parameters - with OUTLIERS
res[res$sample%in%res2[res2$out==1, 'sample'],'out'] <- 1
write.table(res,paste0(first_path, '/Jennifer', '/param_table.csv'), sep=',', row.names = F)
res2 <- res2[!res2$out,]
# remove bad samples from original data
df2 <- df2[which(df2$variable%in%res[res$out==0, 'sample']),]
df2 <- df2[which(df2$variable%in%res2[res2$out==0, 'sample']),]
# plot mean values
m <- ddply(df2, ~time+condition, summarize, med=median(value), mean=mean(value), sd=sd(value), n=length(value) )
m$se <- m$sd/sqrt(m$n)
m$condition <- factor(m$condition, levels=colors$condition)
# with ribbons (SE)
p<-ggplot(data=m, aes(x=time, y=mean, color=condition)) +
geom_line(size=1.5)+
scale_color_manual(values=colors$color)+
scale_fill_manual(values=colors$color)+
geom_ribbon(aes(ymin = mean-se, ymax = mean+se,fill = condition),linetype=0, alpha=.3)+
theme_classic()+
scale_y_continuous(expand=c(0,0))+
scale_x_continuous(expand=c(0,0))+
theme(axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
axis.title.x = element_text(size=20),
legend.text =element_text(size=15),
legend.title = element_blank())+
ylab('Cumulative Number\nof Sips')+
xlab('Time (min)')
pdf(paste0(first_path, '/Jennifer', '/plot_mean_samp_with_SE.pdf'), width = 7, height = 4)
print(p)
dev.off()
# without ribbons (SE)
p<-ggplot(data=m, aes(x=time, y=mean, color=condition)) +
geom_line(size=1.5)+
scale_color_manual(values=colors$color)+
scale_fill_manual(values=colors$color)+
theme_classic()+
scale_y_continuous(expand=c(0,0))+
scale_x_continuous(expand=c(0,0))+
theme(axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
axis.title.x = element_text(size=20),
legend.text =element_text(size=15),
legend.title = element_blank())+
ylab('Cumulative Number\nof Sips')+
xlab('Time (min)')
pdf(paste0(first_path, '/Jennifer', '/plot_mean_samp_without_SE.pdf'), width = 7, height = 4)
print(p)
dev.off()
###################################################
# plot parameter distributions
#-------------------------------------------------------
# Asym
p<- ggplot(res2, aes(x=condition, y=Asym, fill=condition))+
geom_boxplot(width=0.3)+
xlab('')+
theme_bw()+
scale_fill_manual(values=colors$color)+
theme(axis.text.x = element_text(size=15, angle=45, hjust=1),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
legend.position = 'none')
pdf(paste0(first_path, '/Jennifer', '/plot_Asym_distibution.pdf'), width = 5, height = 4)
print(p)
dev.off()
# xmid
p<- ggplot(res2, aes(x=condition, y=xmid, fill=condition))+
geom_boxplot(width=0.3)+
xlab('')+
theme_bw()+
scale_fill_manual(values=colors$color)+
theme(axis.text.x = element_text(size=15, angle=45, hjust=1),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
legend.position = 'none')
pdf(paste0(first_path, '/Jennifer', '/plot_xmid_distibution.pdf'), width = 5, height = 4)
print(p)
dev.off()
# scal
p<- ggplot(res2, aes(x=condition, y=scal, fill=condition))+
geom_boxplot(width=0.3)+
xlab('')+
theme_bw()+
scale_fill_manual(values=colors$color)+
theme(axis.text.x = element_text(size=15, angle=45, hjust=1),
axis.text.y = element_text(size=15),
axis.title.y = element_text(size=20),
legend.position = 'none')
pdf(paste0(first_path, '/Jennifer', '/plot_scal_distibution.pdf'), width = 5, height = 4)
print(p)
dev.off()
###################################################
# statistical tests
#-------------------------------------------------------
# Asym
# linear model
model <- lm(Asym~condition, res2)
# 1-way ANOVA
a1 <- anova(model)
a1 <- round(a1, digits=4)
a1 <- cbind(Asym=rownames(a1), a1)
write.table(a1, paste0(first_path, '/Jennifer', '/1way_anova.csv'), sep=',', row.names=F)
# post-hoc
emm1 <- emmeans(model, ~condition)
con1 <- as.data.frame(contrast(emm1, "pairwise", simple="each", combine=TRUE, adjust='fdr'))
colnames(con1)[1] <- 'condition'
all.con1 <- cbind(param='Asym',con1)
# xmid
# linear model
model <- lm(xmid~condition, res2)
summary(model)
# 1-way ANOVA
a2<-anova(model)
a2 <- round(a2, digits=4)
a2 <- cbind(xmid=rownames(a2), a2)
write.table(a2, paste0(first_path, '/Jennifer', '/1way_anova.csv'), sep=',', row.names=F, append = T)
# post-hoc
emm2 <- emmeans(model, ~condition)
con2 <- as.data.frame(contrast(emm2, "pairwise", simple="each", combine=TRUE, adjust='fdr'))
colnames(con2)[1] <- 'condition'
all.con2 <- cbind(param='xmid', con2)
# scal
# linear model
model <- lm(scal~condition, res2)
summary(model)
# 1-way ANOVA
a3 <-anova(model)
a3 <- round(a3, digits=4)
a3 <- cbind(scal=rownames(a3), a3)
write.table(a3, paste0(first_path, '/Jennifer', '/1way_anova.csv'), sep=',', row.names=F, append = T)
# post-hoc
emm3 <- emmeans(model, ~condition)
con3 <- as.data.frame(contrast(emm3, "pairwise", simple="each", combine=TRUE, adjust='fdr'))
colnames(con3)[1] <- 'condition'
all.con3 <- cbind(param='scal',con3)
# bind all results
all.con <- rbind(all.con1, all.con2, all.con3)
all.con[3:ncol(all.con)] <- round(all.con[3:ncol(all.con)], digits=4)
# add significance asterisks
all.con$sig <- ''
all.con[all.con$p.value<0.05, 'sig']<- '*'
all.con[all.con$p.value<0.01, 'sig']<- '**'
all.con[all.con$p.value<0.001, 'sig']<- '***'
# save post hoc results
write.table(all.con, paste0(first_path, '/Jennifer', '/post_hoc.csv'), sep=',', row.names=F)
print("JENNIFER'S CODE DONE")
}
}
# _______________________________________________
# 3. statistic: do another statistic tests - first sips, then PI.
# need: DataInExcelFormat.xls format (could be the same as the previous or another)
# create: A. sips_file_name.csv
# B. PI_file_name.csv
# _______________________________________________
# stat for sips
# enter name of file and sheet here
second_file = GUI_data$input_path
second_file = gsub("\\\\", "/", second_file)
second_file = basename(second_file)
GUI_data$input_path = second_file
data = read_excel(second_file, sheet = "NumberOfSips")
num_of_groups = ncol(data)/2 # note - assume there is always two from every group
titles = names(data)
# create path
second_file_name = tools::file_path_sans_ext(second_file)
second_path = paste0('pipeline/', second_file_name)
dir.create(file.path(second_path),recursive=TRUE, showWarnings = FALSE)
df = list()
is_there_not_normal = 0
data = combn(data, 2, simplify=FALSE)
# looping on the cols
# check if there even one not normal - then all not normal
for (loop_i in 1:length(data)) {
curr = data[loop_i]
unstacked = curr
curr = stack(data.frame(curr))
shapiro = shapiro.test(curr$values)
p_value = shapiro$p.value
if (shapiro$p.value >= 0.05) {
is_there_not_normal = 1
}
}
# NORMAL
if (is_there_not_normal == 0) {
for (loop_i in 1:length(data)) {
curr = data[loop_i]
unstacked = curr
curr = stack(data.frame(curr))
groups = paste(names(data[[loop_i]])[1],"-", names(data[[loop_i]])[2])
# two groups
if (num_of_groups == 2) {
# shapiro
shapiro = shapiro.test(curr$values)
p_value = shapiro$p.value
name_of_test =shapiro$method
W = shapiro$statistic[[1]]
first_test = cbind(name_of_test, p_value, W)
# t test
is_normal = "normal"
second_test = t.test(curr$values)
second_test_name = second_test$method
t = second_test[["statistic"]][[1]]
DF_parameter = second_test[["parameter"]][[1]]
p.value = second_test[["p.value"]]
conf.int.1 = second_test[["conf.int"]][1]
conf.int.2 = second_test[["conf.int"]][2]
mean_of_x = second_test[["estimate"]][[1]]
mean = second_test[["null.value"]][[1]]
stderr = second_test[["stderr"]]
second_test = cbind(second_test_name, t, DF_parameter, p.value, conf.int.1, conf.int.2, mean_of_x, mean, stderr)
# wrap up
final = data.frame(groups, first_test, is_normal, second_test)
df = rbind(df, final)
}
# 3 or more groups
else {
# anova
first_test = "anova"
anova = aov(formula = curr$values ~ curr$ind)
raw_anova = anova
anova = summary(anova)
shapiro = shapiro.test(curr$values)
p_value = shapiro$p.value
# TukeyHSD
is_normal = "normal"
second_test_name = "TukeyHSD"
second_test = TukeyHSD(raw_anova)
confidence_level = capture.output(second_test)[2]
p_adj = second_test$`curr$ind`[,"p adj"]
diff = second_test$`curr$ind`[,"diff"]
lwr = second_test$`curr$ind`[,"lwr"]
upr = second_test$`curr$ind`[,"upr"]
second_test_to_write =cbind(confidence_level, p_adj, diff, lwr, upr)
# anova
DF = anova[[1]][["Df"]][1]
Sum_Sq = anova[[1]][["Sum Sq"]][1]
F_value = anova[[1]][["Mean Sq"]][1]
Mean_Sq = anova[[1]][["F value"]][1]
Pr_largger_then_F = anova[[1]][["Pr(>F)"]][1]
Residuals_DF = anova[[1]][["Df"]][2]
Residuals_Sum_Sq = anova[[1]][["Sum Sq"]][2]
Residuals_F_value = anova[[1]][["Mean Sq"]][2]
anova_to_write = cbind(Pr_largger_then_F, F_value, Mean_Sq, Sum_Sq, DF, Residuals_F_value, Residuals_Sum_Sq, Residuals_DF)
# wrap up
final = data.frame(groups, p_value, first_test, anova_to_write, is_normal, second_test_name, second_test_to_write)
df = rbind(df, final)
}
name = paste0(second_path, "/sips_normal_", second_file_name, ".csv")
write.csv(df, name)
print("STATISTIC SIPS - DONE")
}
}
# NOT NORMAL
if (is_there_not_normal == 1) {
for (loop_i in 1:length(data)) {
curr = data[loop_i]
unstacked = curr
curr = stack(data.frame(curr))
groups = paste(names(data[[loop_i]])[1],"-", names(data[[loop_i]])[2])
# two groups
if (num_of_groups == 2) {
# shepiro
shapiro = shapiro.test(curr$values)
name_of_test =shapiro$method
p_value = shapiro$p.value
W = shapiro$statistic[[1]]
first_test = cbind(name_of_test, p_value, W)
# wilcox
is_normal = "not normal"
second_test = wilcox.test(curr$values)
second_test_name = second_test$method
V = second_test$statistic[[1]]
p.value = second_test$p.value
second_test = cbind(second_test_name, V, p.value)
# wrap up
final = data.frame(groups, first_test, is_normal, second_test)
df = rbind(df, final)
}
# 3 or more groups
else {
first_test = "anova"
anova = aov(formula = curr$values ~ curr$ind)
raw_anova = anova
anova = summary(anova)
shapiro = shapiro.test(curr$values)
p_value = shapiro$p.value
# kruskal
is_normal = "not normal"
kruskal = kruskal.test(curr$values ~ curr$ind)
second_test_name = kruskal$method
Kruskal_Wallis_chi_squared = kruskal$statistic[[1]]
Kruskal_DF = kruskal$parameter[[1]]
kruskal_p_value = kruskal$p.value
second_test_to_write =cbind(Kruskal_Wallis_chi_squared, Kruskal_DF, kruskal_p_value)
# anova
DF = anova[[1]][["Df"]][1]
Sum_Sq = anova[[1]][["Sum Sq"]][1]
F_value = anova[[1]][["Mean Sq"]][1]
Mean_Sq = anova[[1]][["F value"]][1]
Pr_largger_then_F = anova[[1]][["Pr(>F)"]][1]
Residuals_DF = anova[[1]][["Df"]][2]
Residuals_Sum_Sq = anova[[1]][["Sum Sq"]][2]
Residuals_F_value = anova[[1]][["Mean Sq"]][2]
anova_to_write = cbind(Pr_largger_then_F, F_value, Mean_Sq, Sum_Sq, DF, Residuals_F_value, Residuals_Sum_Sq, Residuals_DF)
# wrap up
final = data.frame(groups, p_value, first_test, anova_to_write, is_normal, second_test_name, second_test_to_write)
df = bind_rows(df, final)
}
name = paste0(second_path, "/sips_not_normal_", second_file_name, ".csv")
write.csv(df, name)
print("STATISTIC SIPS - DONE")
}
}
# stat for PI
data = read_excel(second_file, sheet = "NumberOfSips")
num_of_groups = ncol(data)/2 # note - assume there is always two from every group
# calculate preference
for (i in 1:num_of_groups) {
preference = (data[i] - data[i+num_of_groups])/(data[i] + data[i+num_of_groups])
if (i == 1) { # add the first
mat = data.frame(preference)
} else { # add the rest
mat <- cbind(mat, preference)
}
}
data = mat
mat <- stack(mat)
df = list()
is_there_not_normal = 0
data = combn(data, 2, simplify=FALSE)
# looping on the cols
# check if there even one not normal - then all treated as not normal
for (loop_i in 1:length(data)) {
curr = data[loop_i]
unstacked = curr
curr = stack(data.frame(curr))
shapiro = shapiro.test(curr$values)
p_value = shapiro$p.value
if (shapiro$p.value >= 0.05) {
is_there_not_normal = 1
}
}
# NORMAL
if (is_there_not_normal == 0) {
for (loop_i in 1:length(data)) {
curr = data[loop_i]
unstacked = curr
curr = stack(data.frame(curr))
groups = paste(names(data[[loop_i]])[1],"-", names(data[[loop_i]])[2])
# two groups
if (num_of_groups == 2) {
# shapiro
shapiro = shapiro.test(curr$values)
p_value = shapiro$p.value
name_of_test =shapiro$method
W = shapiro$statistic[[1]]
first_test = cbind(name_of_test, p_value, W)
# t test
is_normal = "normal"
second_test = t.test(curr$values)
second_test_name = second_test$method
t = second_test[["statistic"]][[1]]
DF_parameter = second_test[["parameter"]][[1]]
p.value = second_test[["p.value"]]
conf.int.1 = second_test[["conf.int"]][1]
conf.int.2 = second_test[["conf.int"]][2]
mean_of_x = second_test[["estimate"]][[1]]
mean = second_test[["null.value"]][[1]]