####---- Upstream Regulator Bar Chart Function ----####

make_ura_bar_chart <- function(top_10_upstream_regulators,x_axis_label,y_axis_label,non_significant_colour,significant_colour,bar_outline_size,bar_transparency,legend_position,significant_name,non_significant_name,data_label_size, direction)
{ 
  top_10_upstream_regulators <- top_10_upstream_regulators[seq(dim(top_10_upstream_regulators)[1],1),]
  n_sig = nrow(subset(top_10_upstream_regulators,significant == "TRUE"))
  n_non_sig = nrow(subset(top_10_upstream_regulators,significant == "FALSE"))
  
  if (direction=="activated") 
  {
  limits = c(0,max(top_10_upstream_regulators$activation_zscore*1.25))
  data_label_just = -0.5
  }
  if (direction=="inhibited") 
  {
  limits = c(min(top_10_upstream_regulators$activation_zscore*1.25),0)
  data_label_just = 1.5
  }
    
  if (n_sig+n_non_sig == 0)
  {
    ggp = ggplot(data.frame()) + theme_SL2() + geom_blank() + ggtitle("There were too few gene sets sensibly plot this.")
  }
  else
  {
    ggp = ggplot(data=top_10_upstream_regulators , aes(x=upstream_regulator, y=activation_zscore, fill=significant,group=significant)) +
      geom_bar(colour="black",stat="identity", position = "dodge", alpha = bar_transparency) +
      coord_flip() +
      scale_x_discrete(limits = top_10_upstream_regulators$upstream_regulator) +
      ylab(x_axis_label) +
      xlab(y_axis_label) +
      scale_fill_manual(values=c(non_significant_colour,significant_colour),breaks=c("TRUE","FALSE"),labels=c(significant_name, non_significant_name)) +
      theme_SL2() +
      theme(axis.text.y = element_text(hjust=1),legend.position=legend_position, legend.spacing.x = unit(0.15, 'cm')) +
      scale_y_continuous(expand=c(0,0), limits=limits) +
      geom_text(aes(label=genes_activated+genes_inhibited), position=position_dodge(width=0.9), size=data_label_size, show.legend = FALSE, hjust=data_label_just)
  }
  return(ggp)
}

