library(ggplot2)
library(scales)

ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/v1/metascape_result_M2_all_enr.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[, c("Term", "Description","LogP","Count")]
ts2$Group=1:dim(ts2)[1]

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)
p=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count, col=Group),alpha=.6) +
  scale_size(range = c(2,15)) +
  theme_bw()+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=7))
ggsave("m2_poly_bubbleplot_v1.png", dpi = 600);
write.table(ts2, file="m2_poly_bubbleplot_v1.csv",quote=FALSE,row.names=FALSE,sep="\t")

ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/v1/metascape_result_M2_down.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[, c("Term", "Description","LogP","Count")]
ts2$Group=1:dim(ts2)[1]

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)
p=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count, col=Group),alpha=.6) +
  scale_size(range = c(2,15)) +
  theme_bw()+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=7))
ggsave("m2_poly_down_bubbleplot_v1.png", dpi = 600);
write.table(ts2, file="m2_poly_down_bubbleplot_v1.csv",quote=FALSE,row.names=FALSE,sep="\t")


ts=read.table("~/bak/doc/fleming/kafasla/HuRpaper/v1/metascape_result_M0_all_enr.csv", header = T, sep = "\t")
df1=subset(ts, grepl("Summary", GroupID))
c=lengths(gregexpr(",", df1$Symbols)) + 1
df1$Count=c
ts2=df1[, c("Term", "Description","LogP","Count")]
ts2$Group=1:dim(ts2)[1]

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)
p=ggplot(ts2, aes(x = InvertedLog10Pval, y = Description)) +
  geom_point(aes(size = Count, col=Group),alpha=.6) +
  scale_size(range = c(2,15)) +
  theme_bw()+
  theme(axis.text.x   = element_text(face="bold", size=11),axis.text.y =    element_text(face="bold", size=7))
ggsave("m0_poly_bubbleplot_v1.png", dpi = 600);
write.table(ts2, file="m0_poly_bubbleplot_v1.csv",quote=FALSE,row.names=FALSE,sep="\t")

