This shiny app generate results from elipots lectures.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
4.1 KiB

4 years ago
  1. ---
  2. title: "Elispots"
  3. output: html_document
  4. params:
  5. file: "../Elispot-validation-spots.xlsx"
  6. positive: F
  7. showstats: F
  8. test: "Ttest"
  9. umbral_pos: 10
  10. ---
  11. ```{r, echo=F, warning=F, message=F}
  12. dades<-read_xlsx(params$file)
  13. ctrl<-"Ctrl+"
  14. mock<-"Mock"
  15. table<-dades[,colnames(dades) != "Groups"]
  16. t_mean<-dcast(melt(table, id="Mice"),Mice~variable, mean, na.rm=T)
  17. t_substr<-data.frame("Mice"=t_mean[,1],
  18. as.data.frame(t(apply(t_mean[,2:ncol(table)], 1, function(x) x-x[mock])))
  19. )
  20. t_mean_group<-merge(t_mean, unique(dades[c("Mice","Groups")]), id="Mice")
  21. mock_mean<-dcast(t_mean_group, Groups~., value.var=mock, mean)
  22. mock_mean[,2]<-mock_mean[,2]*2
  23. mock_mean[mock_mean$. < params$umbral_pos,2]<-params$umbral_pos
  24. t_substr<-merge(t_substr, unique(dades[c("Mice","Groups")]), id="Mice")
  25. t_substr<-t_substr[,c(1, ncol(t_substr), 2:(ncol(t_substr)-1))]
  26. t_substr<-t_substr[,c("Mice", "Groups", colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups")])]
  27. t_substr[,3:ncol(t_substr)]<-apply(t_substr[,3:ncol(t_substr)],2, function(x) replace(x, which(x < 0),0))
  28. colnames(t_substr)<-c("Mice", "Groups", colnames(t_mean)[2:ncol(t_mean)])
  29. t<-melt(t_substr[,!colnames(t_substr) %in% c(ctrl, mock)])
  30. if (params$test == "T-test (adj Holm)"){
  31. t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "ttest")
  32. }
  33. if (params$test == "Wilcoxon (adj Holm)"){
  34. t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "wilcox")
  35. }
  36. t_stats<-t_stats %>% filter(p.signif != "ns")
  37. t_maps<-generate_labstats(t_stats, t, "value", "variable", "Groups")
  38. if (params$showstats == F){
  39. t_stats<-as.data.frame(matrix(nrow=0, ncol=6))
  40. colnames(t_stats)<-c("variable", "group1", "group2", "p.adj", "p.signif", "Method")
  41. t_maps<-list()
  42. t_maps[["label"]]<-as.data.frame(matrix(nrow = 0, ncol=2))
  43. colnames(t_maps$label)<-c("x", "y")
  44. t_maps[["brackets"]]<-as.data.frame(matrix(nrow = 0, ncol=4))
  45. colnames(t_maps$brackets)<-c("y1", "y2", "x1", "x2")
  46. }
  47. set.seed(123)
  48. if (params$positive == T){
  49. ggplot(melt(t_substr, id=c("Mice", ctrl, "Groups")), aes(variable, value))+
  50. labs(x="", y="Spots/2.5*10^5 cells")+
  51. geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
  52. geom_hline(data=mock_mean, aes(color=Groups, yintercept = `.`))+
  53. geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
  54. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups))+
  55. # geom_quasirandom(position = position_quasirandom(), shape=21)+
  56. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
  57. geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  58. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
  59. theme_bw()+
  60. theme(axis.text.x=element_text(angle=45, hjust=1))
  61. }else{
  62. ggplot(melt(t_substr, id=c("Mice", ctrl, "Groups")), aes(variable, value))+
  63. labs(x="", y="Spots/2.5*10^5 cells")+
  64. geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
  65. geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
  66. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups))+
  67. # geom_quasirandom(width=0.2, position=position_dodge(), shape=21)+
  68. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
  69. geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  70. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
  71. theme_bw()+
  72. theme(axis.text.x=element_text(angle=45, hjust=1))
  73. }
  74. ```
  75. ```{r, echo=F, warning=F, message=F}
  76. t_stats %>%
  77. flextable() %>%
  78. theme_vanilla() %>%
  79. fontsize(size=14, part="all") %>%
  80. padding(padding=10, part="all") %>%
  81. color(~ p.adj < 0.05, color = "red")%>%
  82. autofit()
  83. ```