# setwd( "PLOTTING" )

library( ggplot2 )
library( SuppDists )
library( MASS )
library( Haplin )
## Registered S3 methods overwritten by 'ffbase':
##   method   from
##   [.ff     ff  
##   [.ffdf   ff  
##   [<-.ff   ff  
##   [<-.ffdf ff
source( "../common_variables.R" )
load( "../GxM_PoOxM_new_betas_mult_CLO.RData" )

# promoter x NOG1
gene <- "NOG1"

cur.plot.obj <- haptable.poo.prom.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/16,10 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/16, 1/8, 1/4, 1/2, 1, 2.5, 5, 10 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLO_mult_betas_promoter-NOG1.png", width = 15, height = 12, units = "cm" )

# gene x ABCA4
gene <- "ABCA4"

cur.plot.obj <- haptable.poo.gene.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/8,20 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/8, 1/4, 1/2, 1, 2.5, 5, 10, 20 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLO_mult_betas_gene-ABCA4.png", width = 15, height = 12, units = "cm" )

# promoter x 8q21.3
gene <- "8q21.3"

cur.plot.obj <- haptable.poo.prom.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/8,75 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/8, 1/4, 1/2, 1, 2.5, 5, 10, 20, 40, 70 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLO_mult_betas_promoter-8q21-3.png", width = 15, height = 12, units = "cm" )

# enhancer x SPRY2
gene <- "SPRY2"

cur.plot.obj <- haptable.poo.enh.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/32,4 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/32, 1/16, 1/8, 1/4, 1/2, 1, 2, 4 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLO_mult_betas_enhancer-SPRY2.png", width = 15, height = 12, units = "cm" )
load( "../GxM_PoOxM_new_betas_mult_CLP.RData" )

# promoter x 8q24
gene <- "8q24"

cur.plot.obj <- haptable.poo.prom.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/64,15 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/64, 1/32, 1/16, 1/8, 1/4, 1/2, 1, 2.5, 5, 10, 15 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLP_mult_betas_promoter-8q24.png", width = 15, height = 12, units = "cm" )

# promoter x NOG1
gene <- "NOG1"

cur.plot.obj <- haptable.poo.prom.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/8,8 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/8, 1/4, 1/2, 1, 2, 4, 8 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLP_mult_betas_promoter-NOG1.png", width = 15, height = 12, units = "cm" )


# gene x IRF6
gene <- "IRF6"

cur.plot.obj <- haptable.poo.gene.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/16,8 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/16, 1/8, 1/4, 1/2, 1, 2, 4, 8 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLP_mult_betas_gene-IRF6.png", width = 15, height = 12, units = "cm" )

# enhancer x SPRY2
gene <- "SPRY2"

cur.plot.obj <- haptable.poo.enh.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/8,8 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/8, 1/4, 1/2, 2, 4, 8 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLP_mult_betas_enhancer-SPRY2.png", width = 15, height = 12, units = "cm" )
load( "../GxM_PoOxM_new_betas_mult_CLorP.RData" )

# promoter x NOG1
gene <- "NOG1"

cur.plot.obj <- haptable.poo.prom.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/8,8 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/8, 1/4, 1/2, 1, 2, 4, 8 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLorP_mult_betas_promoter-NOG1.png", width = 15, height = 12, units = "cm" )


# gene x IRF6
gene <- "IRF6"

cur.plot.obj <- haptable.poo.gene.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/16,8 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/16, 1/8, 1/4, 1/2, 1, 2, 4, 8 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLorP_mult_betas_gene-IRF6.png", width = 15, height = 12, units = "cm" )

# gene x FOXE1
gene <- "FOXE1"

cur.plot.obj <- haptable.poo.gene.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/8,4 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/8, 1/4, 1/2, 1, 2, 3, 4 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_CLorP_mult_betas_gene-FOXE1.png", width = 15, height = 12, units = "cm" )
load( "../GxM_PoOxM_new_betas_mult_cntrl.RData" )

# promoter x NOG1
gene <- "NOG1"

cur.plot.obj <- haptable.poo.prom.res.all[[ gene ]]
cur.plot.obj.list <- lapply( unique( cur.plot.obj$stratum ), function(x){
  return( dplyr::filter( cur.plot.obj, stratum == x ) )
} )
names( cur.plot.obj.list ) <- c( "all", "1", "2", "3" )

cur.signif.plot <- plot.haplinSlide( cur.plot.obj.list, title = "", y.lim = c( 1/4,4 ) )

intercept.hline <- ( as.numeric( cur.signif.plot$data$haplos[ 3 ] ) -
    as.numeric( cur.signif.plot$data$haplos[ 2 ] ) ) / 2

y.labels <- c( 1/4, 1/2, 1, 2, 4 )

cur.signif.plot +
  geom_vline( xintercept = 1.5, linetype = 2 ) +
    scale_y_continuous( breaks = y.labels, labels = as.character( y.labels ) ) +
  xlab( "stratum" ) +
    theme( axis.text.x = element_text( angle = 0, size = 12 ),
            axis.text.y = element_text( size = 12 ),
            axis.line.y = element_line( color = "black" ),
            axis.title = element_text( size = 14 ),
            panel.background = element_rect( fill = "white", colour = "grey50" ),
            panel.grid.major.y = element_line( colour = "grey70", linetype = 2 ) )

ggsave( "PoOxM_cntrl_mult_betas_promoter-NOG1.png", width = 15, height = 12, units = "cm" )