
##################################
#create output#
#####################


pdf(paste(filenameprefix,added,".pdf",sep=""), paper = "a4")

layout (matrix(1))
par(cex=0.6, cex.axis=1.2, las=1)
Arrlength <- 0

oldpar = par()
############### control same number on each side: graph
# 
data$scorerelative = (data$X8_test2 -data$meanbefore)/10000
data$scorenotrelative = (data$X8_test2 )/10000

 plot(data$side_punished ~ data$genotype)
#  
# 
# 
# 
# 
# data$colorwhennegative=2-(as.numeric(data$colorwhenpositive)-1)
# data$colorwhennegative2 = levels(data$colorwhenpositive)[data$colorwhennegative]
# data$colorwhenpositive2 = levels(data$colorwhenpositive)[data$colorwhenpositive]
# 
# 
# data$colorpunished= data$colorwhenpositive2
# data$colorpunished= ifelse(data$side_punished =="positive-left",data$colorwhenpositive2,data$colorwhennegative2)
# 
# data$colorpunished=as.factor(data$colorpunished)
# 
# 
#  plot(data$colorpunished ~ data$genotype)

plot (data$genotype, data$meanbefore/10000, main = "preference before training", ylim = c(-1,1))
points (data$genotype, data$meanbefore/10000)
abline(h=0, col =2)





###############
#for each genotype
########################################################################################
for (j in c(1:length(levels(data$genotype)))){
thesubset = levels (data$genotype)[j]
data_ord = subset (data_ordtot, data_ordtot$genotype == thesubset)
datanumber= as.matrix (summary (data$genotype) [j])



par(xpd= TRUE, mar=c(5.1,4,4,8))

plot(data_ord$ind,data_ord$values, type ="l",  axes = FALSE, main =paste(thesubset, "n=",datanumber ),xlim=c(0,10))
axis(2)
axis(1, at=seq(1,7,by=1), 
labels=c("pretest","training","training","test","training","training","test"), las = 2) 
abline(h=0, col =2)

s <- seq(length(data_ord$ind)-1)# one shorter than data
segments (as.numeric(data_ord$ind[s]),data_ord$values[s],as.numeric(data_ord$ind[s+datanumber]),data_ord$values[s+datanumber], col= 1:datanumber)

for (i in c(1:datanumber)){
  k=length(data_ord$ind)-datanumber +i
  #print(k)
  text (9, data_ord$values[k], paste(ifelse(is.na(data_ord$remarks [k]),as.character(data_ord$remarksnotimportant [k]),data_ord$remarks [k]) , row.names(data_ord)[i]),col=i)
}
par(oldpar)



violinplot(data_ord$values~ data_ord$ind, ylim = c(-2,2))
abline(h=0, col =2)
title (thesubset)

}

#################
# genotypes together
########

layout(matrix(c(1,1,1,1), 1, 1, byrow = TRUE))

variables = list (as.factor (data_ordtot$genotype), data_ordtot$ind)
LI_mean <- tapply(data_ordtot$values, variables, mean, na.rm= TRUE)
LI_sem <- tapply(data_ordtot$values, variables, std.error)
samplesize <-as.matrix(summary (data$genotype))

bar <- barplot(LI_mean, beside=T, ylim = c(-0.6,1),  main=("nocolorlearning"),
               axisnames =F, legend.text=(   paste(levels(data$genotype), "(",samplesize,")"  )      ), args.legend = list(x = "bottomright") ) # store location of bars in bar, and
                          # plot the barplot. ylim is used to
                           # make room for the error bar later

arrows(bar, LI_mean + LI_sem, bar, LI_mean - LI_sem,
       length = Arrlength, # width of the arrowhead
       angle = 90, # angle of the arrowhead 
       code = 3 # arrowhead in both ends
       )


axis(1, at=seq(bar [2,1], bar [2,7],by= (length(levels(data_ordtot$genotype))+1)), tick =T,
labels=c("pretest","training","training","test","training","training","test"), las = 2) 

abline(h=seq(0,1,.2),col=2)


plot(data$X8_test2/10000~data$genotype, las=2, ylab="learning score", xlab="", main="score absolute during final test")
abline(h=0, col=2)

violinplot(data$X8_test2/10000~data$genotype, las=2, ylab="learning score", xlab="", main="score absolute during final test")
abline(h=0, col=2)

plot(data$scorerelative~data$genotype, las=2, ylab="learning score", xlab="", main="score final test - pretest")
abline(h=0, col=2)
violinplot(data$scorerelative~data$genotype, las=2, ylab="learning score", xlab="", main="score final test - pretest")
abline(h=0, col=2)

data$scorenotrel_asin = asin(sqrt((data$scorenotrelative+1)/2))
#data$color_asin = asin(sqrt((data$X5_test1/10000 +1)/2))
#change=ddata$scorenotrel -data$Xb_test3/10000
plot(data$scorenotrel_asin~data$genotype,  main="score absolute Arcsin transformed during final test", ylab="",ylim=c (asin(sqrt((0))),asin(sqrt((1)))),las=2)

abline(h=asin(sqrt((0.5))), col=2)
abline(h=asin(sqrt((0))))
abline(h=asin(sqrt((1))))
#plot(data$scorerelative~data$genotype)
# abline(h=0)
aov = aov(data$scorenotrel_asin~data$genotype)
 kruskal.test(data$scorenotrelative~data$genotype)

#aov2 = aov(data$color_asin~data$genotype)
summary(aov)
TukeyHSD(aov)
#########Textplots
##########################
L=length(levels(data$genotype))
layout (matrix(1:L,L,1))

#textplot (data, cmar=0,  halign ="left")
table=c("anova", summary(aov))
for(i in 1: length (levels(data$genotype))){
Genotypetested= levels(data$genotype)[i]

EX=with(data, wilcox.test(data$scorerelative[data$genotype == Genotypetested],alternative = "greater", mu = 0))
#textplot(summary (subset (data,data$genotype == Genotypetested)))
#textplot(paste (Genotypetested, "scorerelative wilcox.test, pvalue diff from 0:",EX$p.value,"by",EX$method,EX$alternative))
table= rbind(table,c("wilcox",paste (Genotypetested, "scorerelative wilcox.test, pvalue diff from 0:",EX$p.value,"by",EX$method,EX$alternative)))

EX=with(data, wilcox.test(data$scorenotrelative[data$genotype == Genotypetested],alternative = "greater", mu = 0))
#textplot(summary (subset (data,data$genotype == Genotypetested)))
#textplot(paste (Genotypetested, "scorerelative wilcox.test, pvalue diff from 0:",EX$p.value,"by",EX$method,EX$alternative))
table= rbind(table,c("wilcox",paste (Genotypetested, "scorenotrelative wilcox.test, pvalue diff from 0:",EX$p.value,"by",EX$method,EX$alternative)))
}
textplot(table)
layout (matrix(1:1,1,1))
######################if 3 genotypes only
# 
# ##############################
# # enlever preference inne##########
# ##########
# 
# 
# 
# Testvar =data$scorenotrelative
# 
# if (length(levels(data$genotype))==2){
# stat= wilcox.test(Testvar ~data$genotype2 )
# STATLAB=paste("difference: wilcox.test, p=",stat$p.value,",", stat$alternative)
# } else{STATLAB= "no statistique more than 3 groups"}
# 
# Title= "score normal"
# 
# LI_mean <- tapply(Testvar, as.factor (data$genotype2), mean, na.rm= TRUE)
# LI_sem <- tapply(Testvar, as.factor (data$genotype2), std.error, na.rm= TRUE)
# samplesize <-as.matrix(summary (data$genotype2))
# 
# bar <- barplot(LI_mean, beside=T, ylim = c(-0.5,1),  main=(Title), axisnames =T, xlab=STATLAB,legend.text=(   paste(levels(data$genotype2), "(",samplesize,")"  )      ) ) # store location of bars in bar, and
#                           # plot the barplot. ylim is used to
#                            # make room for the error bar later
# 
# arrows(bar, LI_mean + LI_sem, bar, LI_mean - LI_sem,
#        length = Arrlength, # width of the arrowhead
#        angle = 90, # angle of the arrowhead 
#        code = 3 # arrowhead in both ends
#        )
# 
# Testvar =data$scorerelative
# if (length(levels(data$genotype))==2){
# stat= wilcox.test(Testvar ~data$genotype2 )
# STATLAB=paste("difference: wilcox.test, p=",stat$p.value,",", stat$alternative)
# } else{STATLAB= "no statistique more than 3 groups"}
# 
# Title= "score relative to pretest score(substraction)"
# 
# LI_mean <- tapply(Testvar, as.factor (data$genotype2), mean, na.rm= TRUE)
# LI_sem <- tapply(Testvar, as.factor (data$genotype2), std.error)
# samplesize <-as.matrix(summary (data$genotype2))
# 
# bar <- barplot(LI_mean, beside=T, ylim = c(-0.5,1),  main=(Title), axisnames =T, xlab=STATLAB,legend.text=(   paste(levels(data$genotype2), "(",samplesize,")"  )      ) ) # store location of bars in bar, and
#                           # plot the barplot. ylim is used to
#                            # make room for the error bar later
# 
# arrows(bar, LI_mean + LI_sem, bar, LI_mean - LI_sem,
#        length = Arrlength, # width of the arrowhead
#        angle = 90, # angle of the arrowhead 
#        code = 3 # arrowhead in both ends
#        )
# 
# 
# 
# plot (data$genotype2, data$scorerelative, main = "self learning score while naive preference taken out")
# points (data$genotype2, data$scorerelative)
# abline(h=0, col =2)
# 
# 
# 
# plot (data$genotype2, data$scorenotrelative, main = "self learning score while naive preference not taken out", las=2)
# points (data$genotype2, data$scorenotrelative)
# abline(h= 0, col=2)
# 
# 
# 
# #table= rbind(table,as.matrix(summary(aov))
# nparcomp(data$scorenotrelative~data$genotype, data=data, type ="Tukey")
# a =nparcomp(data$scorenotrelative~data$genotype, data=data, type ="Sequen")
# plot(a$Analysis.of.relative.effects$p.value.adjusted~a$Analysis.of.relative.effects$Comparison, ylim=c(0,1), main ="significance of the effects,Nonparametric relative contrast effects ")
# abline(h=0.05, col=2)
# 
# 
# 
# data2= subset (data, data$genotype != "Gal4 controls")
# data2=drop.levels(data2)
#  wilcox.test(data2$scorerelative~data2$genotype, data=data2)
# #plot(data2$scorenotrelative~data2$genotype)
# 
# data2= subset (data, data$genotype != "UAS controls")
# data2=drop.levels(data2)
#  wilcox.test(data2$scorerelative~data2$genotype, data=data2)
# #plot(data2$scorenotrelative~data2$genotype)

dev.off()




violinplot(data$X8_test2/10000~data$genotype, las=2, ylab="learning score", xlab="", main="score absolute during final test")
abline(h=0, col=2)