setwd('//vfs01/users01/butlerr/Linux/Clinotator/')
library(dplyr)
library(pastecs)

# 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 <- "CI"
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)
three_str_tbl <- filter(filter_tbl, CVSZ == 3)
four_str_tbl <- filter(filter_tbl, CVSZ == 4)
calibration_tbl <- filter(filter_tbl, CVSZ == 2 & CVCS %in% c("B", "LB", "US", "LP", "P"))
B_tbl <- filter(two_str_tbl, CVCS == "B")
BLB_tbl <- filter(two_str_tbl, CVCS == "BLB")
LB_tbl <- filter(two_str_tbl, CVCS == "LB")
US_tbl <- filter(two_str_tbl, CVCS == "US")
LP_tbl <- filter(two_str_tbl, CVCS == "LP")
PLP_tbl <- filter(two_str_tbl, CVCS == "PLP")
P_tbl <- filter(two_str_tbl, CVCS == "P")
Overall_tbl <- rbind(two_str_tbl, three_str_tbl, four_str_tbl, confilt_tbl)

# average and std dev
tables <- list(two_str_tbl, three_str_tbl, four_str_tbl, confilt_tbl, calibration_tbl, Overall_tbl)
cvna_set <- sapply(tables, function(x) stat.desc(x$CVNA))
ctaa_set <- sapply(tables, function(x) stat.desc(x$CTAA))

