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.

136 lines
6.7 KiB

4 years ago
  1. library(dplyr)
  2. library(dunn.test)
  3. library(tibble)
  4. library(reshape2)
  5. library(ggplot2)
  6. corr_multi<-function(table, genes1, genes2, font_size=5, ruta="./", exportar=F)
  7. {
  8. cor.list<-list()
  9. cont<-1
  10. for (gene1 in genes1){
  11. for (gene2 in genes2){
  12. print(gene1)
  13. print(gene2)
  14. cor_temp<-cor.test(table[,gene1], table[,gene2])
  15. if (!is.na(cor_temp$p.value)){
  16. pval_plot<-paste("cor =",format(cor_temp$estimate, digits=2),if(cor_temp$p.value < 2.2e-16){" , p < 2.2e-16"}else{paste(", p=",format(cor_temp$p.value,digits=3))})
  17. }
  18. cor.list[[cont]]<-ggplotGrob(ggplot(data=table, aes(x=table[,gene1], y=table[,gene2]))+
  19. geom_point()+
  20. geom_smooth(method="lm")+
  21. geom_text(aes(x=min(table[,gene1], na.rm=T), y=(max(table[,gene2],na.rm = T)+max(table[,gene2],na.rm = T)*0.2), label=pval_plot), hjust="inward", family="serif", size=font_size)+
  22. labs(x=gene1, y=gene2)+
  23. theme_bw())
  24. if (exportar == T){
  25. png(paste0(ruta,gene1,"vs",gene2,".png"), res=900, width=3000, height=3000)
  26. plot(cor.list[[cont]])
  27. dev.off()
  28. }
  29. cont<-cont+1
  30. }
  31. }
  32. return(cor.list)
  33. }
  34. multi_stats<-function(table, value.var, x, group, stat.test, adjust="default", paired=F){
  35. ## Requires dplyr and tibble packets
  36. defaults=c("dunn"="none", "ttest"="holm", "wilcox"="holm")
  37. funs<-c("ttest"="pairwise.t.test", "wilcox"="pairwise.wilcox.test")
  38. if (adjust == "default"){adjust=defaults[stat.test]}
  39. stat.def<-as.data.frame(matrix(nrow=0, ncol=5))
  40. colnames(stat.def)<-c(x, "group1", "group2", "p.adj", "p.signif")
  41. for (point in unique(table[,x])){
  42. condition<-all(table %>% filter(table[,x] == point) %>% pull(value.var) == 0) == F
  43. len_group<-length(unique(table %>% filter(table[,x] == point) %>% pull(group)))
  44. if (condition == T & !is.na(condition) & len_group > 1){
  45. if(stat.test == "dunn"){
  46. test<-dunn.test(table %>% filter(table[,x] == point) %>% pull(value.var), table %>% filter(table[,x] == point) %>% pull(group), method=adjust)
  47. comp<-strsplit(test$comparisons, " - ")
  48. stat.temp<-data.frame(matrix(unlist(comp), nrow=length(comp), byrow=T), "p.adj"=test$P.adjusted, stringsAsFactors = F, check.names = F)
  49. colnames(stat.temp)[1:2]<-c("group1", "group2")
  50. }else if (stat.test %in% names(funs)){
  51. test<-get(funs[stat.test])(table %>% filter(table[,x] == point) %>% pull(value.var), table %>% filter(table[,x] == point) %>% pull(group), method=adjust, paired=paired)$p.value
  52. stat.temp<-melt(test)
  53. colnames(stat.temp)<-c("group1", "group2","p.adj")
  54. }
  55. stat.temp["p.signif"]<-case_when(
  56. stat.temp$p.adj >= 0.05 ~ "ns",
  57. stat.temp$p.adj < 0.0001 ~ "****",
  58. stat.temp$p.adj < 0.001 ~ "***",
  59. stat.temp$p.adj < 0.01 ~ "**",
  60. stat.temp$p.adj < 0.05 ~ "*"
  61. )
  62. stat.temp<-stat.temp %>% add_column(x=point, .before=T)
  63. colnames(stat.temp)[1]<-x
  64. stat.def<-rbind(stat.def, stat.temp)
  65. }
  66. }
  67. stat.def["Method"]<-stat.test
  68. return(stat.def)
  69. }
  70. generate_labstats<-function(table_stat, table, value.var, x, group, y="max", bracket.offset=0.05, bracket.length=0.02){
  71. table[,group]<-as.factor(table[,group])
  72. table[,x]<-as.factor(table[,x])
  73. se<-function(x, na.rm=F) sd(x, na.rm = na.rm)/sqrt(length(x))
  74. if (y == "max"){
  75. formula<-as.formula(paste0(colnames(table_stat)[1], "~."))
  76. agg<-dcast(table, formula, value.var = value.var, fun.aggregate = max, na.rm=T)
  77. }else if (y == "mean"){
  78. formula<-as.formula(paste0(colnames(table_stat)[1], "~", group))
  79. agg<-dcast(table, formula, value.var = value.var, fun.aggregate = mean, na.rm=T)
  80. agg<- data.frame(x=agg[,1], "."=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
  81. colnames(agg)[1]<-x
  82. }else if (y == "mean+sd"){
  83. formula<-as.formula(paste0(colnames(table_stat)[1], "~", group))
  84. agg<- dcast(table, formula, value.var = value.var, fun.aggregate = function(x) mean(x,na.rm=T)+sd(x,na.rm=T))
  85. agg<- data.frame(x=agg[,1], "."=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
  86. colnames(agg)[1]<-x
  87. }else if (y == "mean+se"){
  88. formula<-as.formula(paste0(colnames(table_stat)[1], "~", group))
  89. agg<- dcast(table, formula, value.var = value.var, fun.aggregate = function(x) mean(x,na.rm=T)+se(x,na.rm=T))
  90. agg<- data.frame(agg[,1], "."=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
  91. colnames(agg)[1]<-x
  92. }
  93. t<-data.frame("y1"=merge(table_stat, agg ,sort=F)[,"."]+diff(range(table[value.var], na.rm = T))*bracket.offset,
  94. "y2"=merge(table_stat, agg ,sort=F)[,"."]+diff(range(table[value.var], na.rm = T))*bracket.offset,
  95. "x1"= match(table_stat[,x], unique(table[,x]))+
  96. 0.75*((match(table_stat$group1, levels(table[,group]))-0.5)/length(levels(table[,group]))-0.5),
  97. "x2"= match(table_stat[,x], unique(table[,x]))+
  98. 0.75*((match(table_stat$group2, levels(table[,group]))-0.5)/length(levels(table[,group]))-0.5)
  99. )
  100. for (dia in unique(table_stat[,1])){
  101. t[table_stat[,x] == dia,"y1"]<-seq(t[table_stat[,x] == dia,"y1"][1],
  102. t[table_stat[,x] == dia,"y1"][1]+diff(range(table[,value.var], na.rm = T))*0.05*(nrow(table_stat[table_stat[,x] == dia,])-1),
  103. by=diff(range(table[,value.var], na.rm = T))*0.05)
  104. t[table_stat[,x] == dia,"y2"]<-t[table_stat[,x] == dia,"y1"]
  105. }
  106. t_def<-as.data.frame(matrix(ncol=4, nrow=0))
  107. for (row in 1:nrow(t)){
  108. t_def<-rbind(t_def, t[row,],
  109. c(t[row,"y1"]-diff(range(table[,value.var], na.rm = T))*bracket.length, t[row,"y1"], t[row,"x1"], t[row,"x1"]),
  110. c(t[row,"y1"]-diff(range(table[,value.var], na.rm = T))*bracket.length, t[row,"y1"], t[row,"x2"], t[row,"x2"]))
  111. }
  112. t_lab<-data.frame("x"=t$x1+(t$x2-t$x1)/2, "y"=t$y1+diff(range(table[,value.var], na.rm = T))*0.005, check.names = F)
  113. return(list("label"=t_lab, "brackets"=t_def))
  114. }
  115. secfile<-function(file){
  116. ext<-strsplit(file, ".", fixed = T)[[1]]
  117. ext<-ext[length(ext)]
  118. num<-1
  119. while(file.exists(file) == T){
  120. if(num == 1){
  121. file_tmp<-strsplit(file, ".", fixed=T)[[1]]
  122. file<-paste0(paste(file_tmp[1:(length(file_tmp)-1)], collapse = "."),"_",num,".",file_tmp[length(file_tmp)])
  123. }else{
  124. file_tmp<-paste(strsplit(file, "_", fixed=T)[[1]][-length(strsplit(file, "_", fixed=T)[[1]])], collapse = "_")
  125. file<-paste0(file_tmp, "_", num,".",ext)
  126. }
  127. num<-num+1
  128. }
  129. return(file)
  130. }