#Figure 1 Table
setwd('//vfs01/users01/butlerr/Linux/Clinotator/')
library(dplyr)
library(ggplot2)

# import & slice tbls
big_tbl <- read.csv('20180601_2str.clinvar_ge_2str.tsv', header=TRUE, sep='\t', na.strings='.')
US_tbl <- filter(big_tbl, grepl('Uncertain significance', CVCS) & CVNA >= 2 & CVSZ == 2)
LP_tbl <- filter(big_tbl, grepl('^Likely pathogenic', CVCS) & CVNA >= 2 & CVSZ == 2)
PLP_tbl <- filter(big_tbl, grepl('^Pathogenic/', CVCS) & CVNA >= 2 & CVSZ == 2)
BLB_tbl <- filter(big_tbl, grepl('^Benign/', CVCS) & CVNA >= 2 & CVSZ == 2)
LB_tbl <- filter(big_tbl, grepl('^Likely benign', CVCS) & CVNA >= 2 & CVSZ == 2)
P_tbl <- filter(big_tbl, grepl('Pathogenic($|,)', CVCS) & CVNA >= 2 & CVSZ == 2)
B_tbl <- filter(big_tbl, grepl('Benign($|,)', CVCS) & CVNA >= 2 & CVSZ == 2)

# distributions by clinsig sorting
B_dist <- sort(B_tbl$CTRS)
BLB_dist <- sort(BLB_tbl$CTRS)
LB_dist <- sort(LB_tbl$CTRS)
US_dist <- sort(US_tbl$CTRS)
LP_dist <- sort(LP_tbl$CTRS)
PLP_dist <- sort(PLP_tbl$CTRS)
P_dist <- sort(P_tbl$CTRS)

# plotting clinsig data prep
B_frame <- data.frame('dist'=B_dist)
BLB_frame <- data.frame('dist'=BLB_dist)
LB_frame <- data.frame('dist'=LB_dist)
US_frame <- data.frame('dist'=US_dist)
LP_frame <- data.frame('dist'=LP_dist)
PLP_frame <- data.frame('dist'=PLP_dist)
P_frame <- data.frame('dist'=P_dist)
B_frame$type <- "B"
BLB_frame$type <- "B/LB"
LB_frame$type <- "LB"
US_frame$type <- "US"
LP_frame$type <- "LP"
PLP_frame$type <- "P/LP"
P_frame$type <- "P"
distFrame <- rbind(B_frame, BLB_frame, LB_frame, US_frame, LP_frame, PLP_frame, P_frame)

#clinsig histogram plot combo
clinsig_breaks = c(1, 100, 400, 900, 1600, 2500, 3600, 13000)
histo_breaks = c(seq(-60.9, -25.2, 2.1), -24, seq(-23.1, 0, 2.1), seq(2.09999, 14.69999, 2.1), 14.99999, seq(16.79999, 60.89999, 2.1))

clinsig_plot <- ggplot(distFrame, aes(x=dist, fill=type)) +
  geom_histogram(breaks = histo_breaks, alpha=0.3, position="identity") +
  scale_x_continuous(breaks = seq(-60, 60, 20), minor_breaks=NULL, limits = c(-60, 60)) +
  theme_bw() + scale_y_sqrt(minor_breaks=NULL, breaks=clinsig_breaks)
clinsig_plot

# get plot data
clinsig_dat <- ggplot_build(clinsig_plot)
cs_dat <- clinsig_dat$data[[1]]

#density plot combo
# plot <- ggplot(distFrame, aes(x=dist, color=type)) + geom_density() +
#   scale_x_continuous(breaks = seq(-60, 60, 20), minor_breaks=NULL, limits = c(-60, 60)) +
#   theme_bw() + scale_y_sqrt(minor_breaks=NULL, breaks=clinsig_breaks)
# plot
