library(ggplot2)
library(scales)
library(grid)

pl=list();
ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/metascape_result M2 down FDR less than 0.1b.csv", header = T, sep = "\t")
#df1=subset(ts, grepl("Summary", GroupID))
df1=ts[2:7,]
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[, c("Term", "Description","LogP","Count")]
#ts2$Group=1:dim(ts2)[1]
ts2$Group=rep(5,6)

ts2$InvertedLog10Pval=(-(ts2$LogP))
ts2$Description <- factor(ts2$Description, levels = ts2$Description[order(ts2$Group)])
ts2$Description  # notice the changed order of factor levels
ts2$Group=as.factor(ts2$Group)
pl[[2]]=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count, col=Group),alpha=.6) +
  scale_size(range = c(2,12)) +
  theme_bw()+
  guides(col=FALSE)+
#  xlim(1.9,5.9)+
  xlim(3.65,12.5)+
  xlab("-log10(P-value)")+
  ylab("")+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=9))
ggsave("Down in poly_seq_metascape_result_M2_bubbleplot_TopCateg_Members_v2.png", dpi = 600, width=5.9,height=5.4,units="in");


df1=ts[c(1,8,14,32,45,59,71,91,99,103),]
#,118,125,141,151,177,191,210,245,248,257),]
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[, c("Term", "Description","LogP","Count")]
#ts2$Group=1:dim(ts2)[1]
ts2$Group=c(5,rep(10,9))

ts2$InvertedLog10Pval=(-(ts2$LogP))
ts2$Description <- factor(ts2$Description, levels = ts2$Description[order(ts2$Group)])
ts2$Description  # notice the changed order of factor levels
ts2$Group=as.factor(ts2$Group)
pl[[1]]=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count, col=Group),alpha=.6) +
  scale_size(range = c(2,12)) +
#  scale_size(range = c(4,15)) +  
  theme_bw()+
#  guides(col = guide_colourbar(order = 1),
#         size = guide_legend(order = 2))
  guides(col=FALSE)+
#  guides(size=FALSE)+
#  xlim(3.08,12.33)+
  xlim(5.3,11.71)+
  xlab("-log10(P-value)")+
  ylab("")+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=9))
#p + ggtitle("Plot of length \n by dose") +  xlab("Dose (mg)") + ylab("Teeth length")
#ggarrange(p1, p2, widths = c(1.5,2))
#ggarrange(p1, p2)
#ggsave("Down in poly_seq_metascape_result_M2_bubbleplot_v2.png", dpi = 600);x1
#     grid.arrange(rectGrob(), rectGrob())
#    ml <- marrangeGrob(pl, nrow=1, ncol=2)
#    ml <- arrangeGrob(pl[[1]],pl[[2]], nrow=1, ncol=2)
ggsave("Down in poly_seq_metascape_result_M2_bubbleplot_Summaries_v2.png", dpi = 600, width=6.5,height=6,units="in");



ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/metascape_result M2 up FDR less than 0.1b.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
#df1=ts[2:10,]
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[1:10, c("Term", "Description","LogP","Count")]
#ts2$Group=1:dim(ts2)[1]
ts2$Group=rep(5,10)
ts2$InvertedLog10Pval=(-(ts2$LogP))
ts2$Description <- factor(ts2$Description, levels = ts2$Description[order(ts2$Group)])
ts2$Description  # notice the changed order of factor levels
ts2$Group=as.factor(ts2$Group)
pl[[2]]=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count),alpha=.6) +
  scale_size(range = c(2,12)) +
  theme_bw()+
  guides(col=FALSE)+
  xlim(2.5,5.7)+
#  xlim(5,6.4)+
  xlab("-log10(P-value)")+
  ylab("")+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=9))
ggsave("Up in poly_seq_metascape_result_M2_bubbleplot_Summaries_v2.png", dpi = 600, width=5.9,height=6.0,units="in");


ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/metascape_result_M0_up_b.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
#df1=ts[2:10,]
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[1:10, c("Term", "Description","LogP","Count")]
#ts2$Group=1:dim(ts2)[1]
ts2$Group=rep(5,10)
ts2$InvertedLog10Pval=(-(ts2$LogP))
ts2$Description <- factor(ts2$Description, levels = ts2$Description[order(ts2$Group)])
ts2$Description  # notice the changed order of factor levels
ts2$Group=as.factor(ts2$Group)
pl[[2]]=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count),alpha=.6) +
  scale_size(range = c(2,12)) +
  theme_bw()+
  guides(col=FALSE)+
  xlim(2.99,6.5)+
#  xlim(1.65,6.4)+
  xlab("-log10(P-value)")+
  ylab("")+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=9))
ggsave("Up in poly_seq_metascape_result_M0_bubbleplot_Summaries_v2.png", dpi = 600, width=5.9,height=6.0,units="in");


ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/metascape_result_M0_down.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
#df1=ts[2:10,]
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[1:10, c("Term", "Description","LogP","Count")]
#ts2$Group=1:dim(ts2)[1]
ts2$Group=rep(5,10)
ts2$InvertedLog10Pval=(-(ts2$LogP))
ts2$Description <- factor(ts2$Description, levels = ts2$Description[order(ts2$Group)])
ts2$Description  # notice the changed order of factor levels
ts2$Group=as.factor(ts2$Group)
pl[[2]]=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count),alpha=.6) +
  scale_size(range = c(2,12)) +
  theme_bw()+
  guides(col=FALSE)+
  xlim(2.99,6.5)+
#  xlim(1.65,6.4)+
  xlab("-log10(P-value)")+
  ylab("")+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=9))
ggsave("Down in poly_seq_metascape_result_M0_bubbleplot_Summaries_v2.png", dpi = 600, width=6.9,height=6.0,units="in");


ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/metascape_result M1 down FDR less than 0.1.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
#df1=ts[2:10,]
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[, c("Term", "Description","LogP","Count")]
#ts2$Group=1:dim(ts2)[1]
ts2$Group=rep(5,3)
ts2$InvertedLog10Pval=(-(ts2$LogP))
ts2$Description <- factor(ts2$Description, levels = ts2$Description[order(ts2$Group)])
ts2$Description  # notice the changed order of factor levels
ts2$Group=as.factor(ts2$Group)
pl[[2]]=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count),alpha=.6) +
  scale_size(range = c(2,12)) +
  theme_bw()+
  guides(col=FALSE)+
  xlim(1.99,3.6)+
#  xlim(1.65,6.4)+
  xlab("-log10(P-value)")+
  ylab("")+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=9))
ggsave("Down in poly_seq_metascape_result_M1_bubbleplot_Summaries_v2.png", dpi = 600, width=5.3,height=6.0,units="in");

