---
title: "Elispots"
output: html_document
params:
  file: "../Elispot-validation-spots.xlsx"
  positive: F
  showstats: F
  test: "Ttest"
  umbral_pos: 10
---

```{r, echo=F, warning=F, message=F}
dades<-read_xlsx(params$file)

ctrl<-"Ctrl+"
mock<-"Mock"

table<-dades[,colnames(dades) != "Groups"]
t_mean<-dcast(melt(table, id="Mice"),Mice~variable, mean, na.rm=T)

t_substr<-data.frame("Mice"=t_mean[,1],
                     as.data.frame(t(apply(t_mean[,2:ncol(table)], 1, function(x) x-x[mock])))
)
t_mean_group<-merge(t_mean, unique(dades[c("Mice","Groups")]), id="Mice")
mock_mean<-dcast(t_mean_group, Groups~., value.var=mock, mean)
mock_mean[,2]<-mock_mean[,2]*2
mock_mean[mock_mean$. < params$umbral_pos,2]<-params$umbral_pos

t_substr<-merge(t_substr, unique(dades[c("Mice","Groups")]), id="Mice")
t_substr<-t_substr[,c(1, ncol(t_substr), 2:(ncol(t_substr)-1))]
t_substr<-t_substr[,c("Mice", "Groups", colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups")])]
t_substr[,3:ncol(t_substr)]<-apply(t_substr[,3:ncol(t_substr)],2, function(x) replace(x, which(x < 0),0))
colnames(t_substr)<-c("Mice", "Groups", colnames(t_mean)[2:ncol(t_mean)])

t<-melt(t_substr[,!colnames(t_substr) %in% c(ctrl, mock)])
if (params$test == "T-test (adj Holm)"){
    t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "ttest")
}
if (params$test == "Wilcoxon (adj Holm)"){
    t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "wilcox")
}
t_stats<-t_stats %>% filter(p.signif != "ns")

t_maps<-generate_labstats(t_stats, t, "value", "variable", "Groups")

if (params$showstats == F){
    t_stats<-as.data.frame(matrix(nrow=0, ncol=6))
    colnames(t_stats)<-c("variable", "group1", "group2", "p.adj", "p.signif", "Method")
    t_maps<-list()
    t_maps[["label"]]<-as.data.frame(matrix(nrow = 0, ncol=2))
    colnames(t_maps$label)<-c("x", "y")
    t_maps[["brackets"]]<-as.data.frame(matrix(nrow = 0, ncol=4))
    colnames(t_maps$brackets)<-c("y1", "y2", "x1", "x2")
}

set.seed(123)
if (params$positive == T){
    ggplot(melt(t_substr, id=c("Mice", ctrl, "Groups")), aes(variable, value))+
        labs(x="", y="Spots/2.5*10^5 cells")+
        geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
        geom_hline(data=mock_mean, aes(color=Groups, yintercept = `.`))+
        geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
        geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups))+
        # geom_quasirandom(position = position_quasirandom(), shape=21)+
        scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
        geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
        geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
        theme_bw()+
        theme(axis.text.x=element_text(angle=45, hjust=1))
}else{
    ggplot(melt(t_substr, id=c("Mice", ctrl, "Groups")), aes(variable, value))+
        labs(x="", y="Spots/2.5*10^5 cells")+
        geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
        geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
        geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups))+
        # geom_quasirandom(width=0.2, position=position_dodge(), shape=21)+
        scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
        geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
        geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
        theme_bw()+
        theme(axis.text.x=element_text(angle=45, hjust=1))
}

```

```{r, echo=F, warning=F, message=F}
t_stats %>%
            flextable() %>%
            theme_vanilla() %>%
            fontsize(size=14, part="all") %>%
            padding(padding=10, part="all") %>%
            color(~ p.adj < 0.05, color = "red")%>%
            autofit()
```

