setwd('//vfs01/users01/butlerr/Linux/Clinotator/')
library(dplyr)
library(ggplot2)
library(ggExtra)
library(gridExtra)
library(RColorBrewer)

# import & clean big tbl
big_tbl <- read.csv('20180601_2str.clinvar_ge_2str.tsv', header=TRUE, sep='\t', na.strings='.', stringsAsFactors = FALSE)
filter_tbl <- filter(big_tbl, !grepl('^drug', CVCS) & CVSZ >=2 & CVNA >= 2)
filter_tbl$CVCS[grepl('Uncertain significance', filter_tbl$CVCS)] <- "US"
filter_tbl$CVCS[grepl('^Likely pathogenic', filter_tbl$CVCS)] <- "LP"
filter_tbl$CVCS[grepl('^Pathogenic/', filter_tbl$CVCS)] <- "PLP"
filter_tbl$CVCS[grepl('^Benign/', filter_tbl$CVCS)] <- "BLB"
filter_tbl$CVCS[grepl('^Likely benign', filter_tbl$CVCS)] <- "LB"
filter_tbl$CVCS[grepl('Pathogenic($|,)', filter_tbl$CVCS)] <- "P"
filter_tbl$CVCS[grepl('Benign($|,)', filter_tbl$CVCS)] <- "B"
filter_tbl$CTPS[grepl('Uncertain significance', filter_tbl$CTPS)] <- "US"
filter_tbl$CTPS[grepl('^Likely pathogenic', filter_tbl$CTPS)] <- "LP"
filter_tbl$CTPS[grepl('^Pathogenic/', filter_tbl$CTPS)] <- "PLP"
filter_tbl$CTPS[grepl('^Benign/', filter_tbl$CTPS)] <- "BLB"
filter_tbl$CTPS[grepl('^Likely benign', filter_tbl$CTPS)] <- "LB"
filter_tbl$CTPS[grepl('Pathogenic($|,)', filter_tbl$CTPS)] <- "P"
filter_tbl$CTPS[grepl('Benign($|,)', filter_tbl$CTPS)] <- "B"
filter_tbl$CVLE <- as.Date(filter_tbl$CVLE, "%Y-%m-%d")

# import & clean conflict tbl
con_tbl <- read.csv('20180601_con.20180227_clinvar_conflict.tsv', header=TRUE, sep='\t', na.strings='.', stringsAsFactors = FALSE)
confilt_tbl <- filter(con_tbl, grepl('^Conflicting i', CVCS) & CVSZ == 1 & CVNA >= 2)
confilt_tbl$CVCS <- "Conflict"
confilt_tbl$CTPS[grepl('Uncertain significance', confilt_tbl$CTPS)] <- "US"
confilt_tbl$CTPS[grepl('^Likely pathogenic', confilt_tbl$CTPS)] <- "LP"
confilt_tbl$CTPS[grepl('^Pathogenic/', confilt_tbl$CTPS)] <- "PLP"
confilt_tbl$CTPS[grepl('^Benign/', confilt_tbl$CTPS)] <- "BLB"
confilt_tbl$CTPS[grepl('^Likely benign', confilt_tbl$CTPS)] <- "LB"
confilt_tbl$CTPS[grepl('Pathogenic($|,)', confilt_tbl$CTPS)] <- "P"
confilt_tbl$CTPS[grepl('Benign($|,)', confilt_tbl$CTPS)] <- "B"
confilt_tbl$CVLE <- as.Date(confilt_tbl$CVLE, "%Y-%m-%d")

# slice into sets
US3_tbl <- filter(filter_tbl, CVCS == 'US' & CVSZ == 3)
two_str_tbl <- filter(filter_tbl, CVSZ == 2)

# 1D scatter and right histogram for US3 variants
ctrr_values <- c('0' = '#2c7bb6', '1' = '#abd9e9', '2' = '#fdae61', '3' = '#d7191c')
US3_ctrr <- ggplot(US3_tbl, aes(x=CTRS, y=CVNA, color=factor(CTRR))) + 
  geom_point() + 
  guides(color=FALSE) + 
  theme_bw() + 
  scale_color_manual(values=ctrr_values) +
  scale_x_continuous(limits = c(-80, 80)) +
  scale_y_continuous(limits = c(1, 19))
# US3_hist <- ggplot(US3_tbl, aes(x=CVNA, color=factor(CTRR))) + 
#   theme_bw() +
#   geom_density() + 
#   coord_flip() + 
#   guides(color=FALSE) +
#   scale_color_manual(values=ctrr_values) +
#   # scale_y_continuous(limits = c(0, 3)) +
#   scale_x_continuous(limits = c(1, 19))
# 1D scatter and right histogram for confilt variants
con_ctrr <- ggplot(confilt_tbl, aes(x=CTRS, y=CVNA, color=factor(CTRR))) + 
  geom_point() + 
  guides(color=FALSE) + 
  theme_bw() + 
  scale_color_manual(values=ctrr_values) +
  scale_x_continuous(limits = c(-80, 80)) +
  scale_y_continuous(limits = c(1, 19))
# con_hist <- ggplot(confilt_tbl, aes(x=CVNA, color=factor(CTRR))) + 
#   theme_bw() +
#   geom_density() + 
#   coord_flip() + 
#   guides(color=FALSE) +
#   scale_color_manual(values=ctrr_values) +
#   # scale_y_continuous(limits = c(0, 3)) +
#   scale_x_continuous(limits = c(1, 19))
# 1D scatter and right histogram for two_str variants
twostr_ctrr <- ggplot(two_str_tbl, aes(x=CTRS, y=CVNA, color=factor(CTRR))) + 
  geom_point() + 
  guides(color=FALSE) + 
  theme_bw() + 
  scale_color_manual(values=ctrr_values) +
  scale_x_continuous(limits = c(-80, 80)) +
  scale_y_continuous(limits = c(1, 19))
# twostr_hist <- ggplot(two_str_tbl, aes(x=CVNA, color=factor(CTRR))) + 
#   theme_bw() +
#   geom_density() + 
#   coord_flip() + 
#   guides(color=FALSE) +
#   scale_color_manual(values=ctrr_values) +
#   # scale_y_continuous(limits = c(0, 3)) +
#   scale_x_continuous(limits = c(1, 19))


# Just US3 plot
# grid.arrange(US3_ctrr, US3_hist, ncol=2, widths=c(4, 1), heights=c(8, 4))
# 2D grid of both US3 and confilt
# grid.arrange(twostr_ctrr, twostr_hist, US3_ctrr, US3_hist, con_ctrr, con_hist, ncol=2, nrow=3, widths=c(5, 1), heights=c(3, 3, 3))

# 1D vertical of scatters only
grid.arrange(twostr_ctrr, US3_ctrr, con_ctrr, ncol=1, nrow=3, widths=c(5), heights=c(3, 3, 3))


# # 2 and 3 star histogram  and scatter plot components
# STZ3_tbl <- filter(filter_tbl, CVSZ == 3)
# STZ2_tbl <- filter(filter_tbl, CVSZ == 2)
#ggplot STZ comparison w/ grid
# STZ2_ctps <- ggplot(STZ2_tbl, aes(x=abs(CTRS), y=CVNA, color=factor(CTPS))) + 
#   geom_point() + ggtitle('Two star variants', subtitle = 'by CTPS') + theme_bw()
# STZ2_ctrr <- ggplot(STZ2_tbl, aes(x=abs(CTRS), y=CVNA, color=factor(CTRR))) + 
#   geom_point() + ggtitle('Two star variants', subtitle = 'by CTRR') + theme_bw()
# STZ3_ctrr <- ggplot(STZ3_tbl, aes(x=abs(CTRS), y=CVNA, color=factor(CTRR))) + 
#   geom_point() + ggtitle('Three star variants', subtitle = 'by CTRR') + 
#   theme_bw() + guides(color=FALSE)
# big_bar <- ggplot(filter_tbl, aes(x=CVSZ, fill=factor(CTRR))) + 
#   theme_bw() +
#   geom_bar(position='dodge') + 
#   scale_x_discrete(drop=FALSE) +
#   scale_fill_manual(values=ctrr_values, drop=FALSE) +
#   guides(color=FALSE) +
#   scale_y_log10() 
# empty <- ggplot() + geom_point(aes(1,1), colour) +
#   theme(axis.ticks=element_blank(),
#         panel.background=element_blank(),
#         axis.text.x=element_blank(),
#         axis.text.y=element_blank(),
#         axis.title.x=element_blank(),
#         axis.title.y=element_blank())

# #ggplot US3 horizontal and vertical marginal histograms w/ grid
# hist_top <- ggplot(US3_tbl, aes(x=abs(CTRS), color=factor(CTRR))) + 
#   geom_density()
# hist_right <- ggplot(US3_tbl, aes(x=CVNA, color=factor(CTRR))) +
#   geom_density() + 
#   coord_flip() + 
#   guides(color=FALSE)
# scatter <- ggplot(US3_tbl, aes(x=abs(CTRS), y=CVNA, color=factor(CTRR))) + 
#   geom_point() +
#   guides(color=FALSE)
# empty <- ggplot()+geom_point(aes(1,1), colour="white")+
#   theme(axis.ticks=element_blank(),
#         panel.background=element_blank(),
#         axis.text.x=element_blank(),
#         axis.text.y=element_blank(),
#         axis.title.x=element_blank(),
#         axis.title.y=element_blank())
# grid.arrange(hist_top, empty, scatter, hist_right, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))
# position=position_jitter(width=.1,height=.1)
