####---- Number of Significant Genes Lollipop Plot Function ----####

make_number_of_significant_genes_lollipop <- function(x_axis_label, y_axis_label, comparison_labels, direction_labels, up_lollipop_colour, down_lollipop_colour, stick_colour, stick_size, lollipop_size, legend_position, data_label_size)
{

  # gets the names of the columns that indicate significance
  sig_genes_rownames = data.frame(matrix(ncol=length(comparisons), nrow=0), check.names=TRUE)
  colnames(sig_genes_rownames) = paste(comparisons,"_sig",sep="")
  sig_genes_rownames = colnames(data.frame(sig_genes_rownames, check.names=TRUE))

  # gets the names of the columns that indicate fold change
  fold_change_rownames = data.frame(matrix(ncol=length(comparisons), nrow=0), check.names=TRUE)
  colnames(fold_change_rownames) = paste(comparisons,"_log2fold",sep="")
  fold_change_rownames = colnames(data.frame(fold_change_rownames, check.names=TRUE))

  # gets the sig counts
  upregulated=c()
  downregulated=c()
  for(index in 1:length(comparisons))
  {
    upregulated[index] = nrow(subset(Mde_annotated,Mde_annotated[[sig_genes_rownames[index]]] == "True" & Mde_annotated[[fold_change_rownames[index]]] > 0))
    downregulated[index] = -nrow(subset(Mde_annotated,Mde_annotated[[sig_genes_rownames[index]]] == "True" & Mde_annotated[[fold_change_rownames[index]]] < 0))
  }

  # makes table of sig counts by comparsison
  table_data = data.frame(comparison = factor(default_comparison_labels, levels=default_comparison_labels), up = upregulated, down = downregulated)

  # ggplot friendly tables
  table_data = melt(table_data, id.vars = "comparison")
  table_data_up = subset(table_data, variable == "up")
  table_data_down = subset(table_data, variable == "down")

  # limits
  max = max(abs(table_data$value)) *1.5
  limits = c(-max,max)


  # make the plot
  ggp = ggplot(table_data, aes(x=value, y=comparison, colour = variable)) +
    geom_segment(aes(xend = 0, yend = comparison), size = stick_size, show.legend = FALSE, colour=stick_colour) +
    geom_point(size = lollipop_size) +
    geom_label(data=table_data_up, aes(label=value),label.padding = unit(0.75, "lines"), show.legend = FALSE, size=data_label_size, nudge_x = max*0.2, vjust = 0.5, colour = "black") +
    geom_label(data=table_data_down, aes(label=value),label.padding = unit(0.75, "lines"), show.legend = FALSE, size=data_label_size, nudge_x = -max*0.2, vjust = 0.5, colour = "black") +
    geom_vline(xintercept = 0, colour = "black", size = 1) +
    scale_color_manual(values=c(up_lollipop_colour,down_lollipop_colour),labels=direction_labels) +
    xlim(limits) +
    labs(y=y_axis_label, x=x_axis_label) +
    theme_SL2() +
    theme(legend.position=legend_position, legend.spacing.x = unit(0.25, 'cm'))

  return(ggp)
}

