#Library initiation
library("readxl")
library("xlsx")
library("Rmpfr")

#Load the main excel files - stble1 (master table), stable 2(track reference) and stable 3 (ENIGMA+ClinVar reference variants)
stable1 = as.data.frame(read_excel("Sup_tables_GIM_AUG_2020 REV FINAL.xlsx", sheet = "STable 1", col_names = TRUE, na="", guess_max = 15000))
stable2 = as.data.frame(read_excel("Sup_tables_GIM_AUG_2020 REV FINAL.xlsx", sheet = "STable 2", col_names = TRUE, na=""))
stable3 = as.data.frame(read_excel("Sup_tables_GIM_AUG_2020 REV FINAL.xlsx", sheet = "STable 3", col_names = TRUE, na=""))

#Table with sum of numbers from each variant in master table (row by row)
stable1_counts <- data.frame()[1:nrow(stable1),]
stable1_counts$"no_functional_impact" <- rowSums(stable1[,10:ncol(stable1)] == 0, na.rm= TRUE)
stable1_counts$"functional_impact" <- rowSums(stable1[,10:ncol(stable1)] == 1, na.rm= TRUE)
stable1_counts$"number of tests" <- stable1_counts$"no_functional_impact"  + stable1_counts$"functional_impact"

#Preparing reference only panel table [E+C]
reference_panel_E_C <- stable1
reference_panel_E_C$"T7"[!is.na(reference_panel_E_C$"T7")] <- NA
for (i in 1:nrow(reference_panel_E_C)){
  var_aa_st1 <- reference_panel_E_C[i,5]
  for (z in 1:nrow(stable3)-8){
    var_aa_st3 <- stable3[z,3]
    if (var_aa_st1 == toString(var_aa_st3)){
      reference_panel_E_C[i,7] <- stable3[z,8]
    }
  }
}
reference_panel_E_C <- as.data.frame(reference_panel_E_C[!is.na(reference_panel_E_C$"T7"), ])

#Table with sum of variants from each track in reference panel table (column by column)
count_reference_panel <- function (reference_panel, master_table){
  x <- matrix(c(colSums(reference_panel[,10:length(reference_panel)] == 0, na.rm= TRUE),
                colSums(reference_panel[,10:length(reference_panel)] == 1, na.rm= TRUE),
                colSums(master_table[,10:length(master_table)] <= 1, na.rm= TRUE),
                colSums(reference_panel[,10:length(reference_panel)] <= 1, na.rm= TRUE)), ncol=4)
  rownames(x) <- c("T10",	"T11",	"T12",	"T13",	"T14",	"T15",	"T16",	"T17",	"T18",
                                "T19",	"T20",	"T21",	"T22",	"T23",	"T24",	"T25",	"T26",	"T27",
                                "T28",	"T29",	"T30",	"T31",	"T32",	"T33",	"T34",	"T35",	"T36",
                                "T37",	"T38",	"T39",	"T40",	"T41",	"T42",	"T43",	"T44",	"T45",
                                "T46",	"T47",	"T48",	"T49",	"T50",	"T51",	"T52",	"T53",	"T54",
                                "T55",	"T56",	"T57",	"T58",	"T59",	"T60",	"T61",	"T62",	"T63",
                                "T64",	"T65",	"T66",	"T67",	"T68",	"T69",	"T70",	"T71",	"T72",
                                "T73",	"T74",	"T75",	"T76",	"T77",	"T78",	"T79",	"T80",	"T81",
                                "T82",	"T83",	"T84",	"T85",	"T86",	"T87",	"T88",	"T89",	"T90",
                                "T91",	"T92",	"T93",	"T94",	"T95",	"T96",	"T97",	"T98",	"T99",
                                "T100",	"T101",	"T102",	"T103",	"T104",	"T105",	"T106",	"T107",	"T108",
                                "T109",	"T110",	"T111",	"T112",	"T113",	"T114",	"T115",	"T116",	"T117",
                                "T118",	"T119",	"T120",	"T121",	"T122",	"T123",	"T124",	"T125",	"T126",
                                "T127",	"T128",	"T129",	"T130",	"T131",	"T132",	"T133",	"T134",	"T135",
                                "T136",	"T137",	"T138",	"T139",	"T140")
  colnames(x) <- c("Assay_no_functional_impact", "Assay_functional_impact","#_of_missense_variants_tested", "Total_classified_variants_tested")

  return(x)
}

#SUPPLEMENTARY TABLE 5: SUMMARY STATISTICS FOR TESTED VARIANTS (NUMBER OF ASSAYS PERFORMED FOR EACH VARIANT)
#Missense variants - Calculation for ALL FUNCTIONAL TRACKS of number of times variants were tested
count_variant_tested <- function (master_table, counts){
  x<-matrix(c("N", "Percentage ",
                            nrow(master_table),(nrow(master_table)*100)/nrow(master_table),
                            sum(counts$"number of tests" == 0),round((sum(counts$"number of tests" == 0)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 1),round((sum(counts$"number of tests" >= 1)*100)/nrow(master_table),digits =2),
                            sum(counts$"number of tests" >= 2),round((sum(counts$"number of tests" >= 2)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 10),round((sum(counts$"number of tests" >= 10)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 15),round((sum(counts$"number of tests" >= 15)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 20),round((sum(counts$"number of tests" >= 20)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 25),round((sum(counts$"number of tests" >= 25)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 30),round((sum(counts$"number of tests" >= 30)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 35),round((sum(counts$"number of tests" >= 35)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 40),round((sum(counts$"number of tests" >= 40)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 50),round((sum(counts$"number of tests" >= 50)*100)/nrow(master_table),digits = 2),
                            sum(counts$"number of tests" >= 60),round((sum(counts$"number of tests" >= 60)*100)/nrow(master_table),digits = 2)),
                          ncol = 2, byrow=TRUE)

  rownames(x)<-c("Missense Variants","Possible unique (BRCA1)","not yet tested","tested >= 1 time","tested >= 2 times","tested >= 10 times",
                               "tested >= 15 times","tested >= 20 times","tested >= 25 times","tested >= 30 times","tested >= 35 times","tested >= 40 times",
                               "tested >= 50 times","tested >= 60 times")
  colnames(x)<-c("All_Functional_Tracks", "All_Functional_Tracks(Relative")
  return(x)
}

#SUPPLEMENTARY TABLE 5: Calculation for Documented variants (BRCAExchange)
count_BRCAExchange <- function(master_table, counts, reference_panel){
  x <- matrix(c(sum(master_table$"T9" == 1),(sum(master_table$"T9" == 1)*100)/sum(master_table$"T9" == 1),
                sum((master_table$"T9" == 1) & (counts$"number of tests" >= 1)),
           round((sum((master_table$"T9" == 1) & (counts$"number of tests" >= 1))*100)/sum(master_table$"T9" == 1),2)),
           ncol=2, byrow = TRUE)
  #Percentage  of documented VUS
  documented_vus <- matrix(c("-", round((100*(x[2,1])/(x[1,1]-nrow(reference_panel))),2)))
  x <- cbind(x, documented_vus[c(1:2),])
  rownames(x)<- c("Total documented missense variants","Total documented missense variants tested")
  colnames(x)<-c("N", "Percentage", "Percentage_VUS" )
  return(x)
}

#SUPPLEMENTARY TABLE 6: ASSAY THROUGHPUT
#Number of variants tested by track
count_variant_track <- function(reference_table_counts){
  Tracks <- matrix(c(nrow(reference_table_counts),round(nrow(reference_table_counts)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] == 1), round(sum(reference_table_counts[,3] == 1)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 5),round(sum(reference_table_counts[,3] >= 5)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 10),round(sum(reference_table_counts[,3] >= 10)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 20),round(sum(reference_table_counts[,3] >= 20)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 30),round(sum(reference_table_counts[,3] >= 30)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 40),round(sum(reference_table_counts[,3] >= 40)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 50),round(sum(reference_table_counts[,3] >= 50)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 60),round(sum(reference_table_counts[,3] >= 60)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 70),round(sum(reference_table_counts[,3] >= 70)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 80),round(sum(reference_table_counts[,3] >= 80)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 90),round(sum(reference_table_counts[,3] >= 90)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 100),round(sum(reference_table_counts[,3] >= 100)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 200),round(sum(reference_table_counts[,3] >= 200)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 300),round(sum(reference_table_counts[,3] >= 300)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 400),round(sum(reference_table_counts[,3] >= 400)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 500),round(sum(reference_table_counts[,3] >= 500)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 1000),round(sum(reference_table_counts[,3] >= 1000)*100/nrow(reference_table_counts),2),
                     sum(reference_table_counts[,3] >= 1500),round(sum(reference_table_counts[,3] >= 1500)*100/nrow(reference_table_counts),2)),
                   ncol=2,byrow = TRUE)
  rownames(Tracks) <- c("Total number of functional tracks","Tracks testing 1 variant only","Tracks testing >= 5 variants",
                        "Tracks testing >= 10 variants", "Tracks testing >= 20 variants","Tracks testing >= 30 variants",
                        "Tracks testing >= 40 variants","Tracks testing >= 50 variants","Tracks testing >= 60 variants",
                        "Tracks testing >= 70 variants","Tracks testing >= 80 variants","Tracks testing >= 90 variants",
                        "Tracks testing >= 100 variants","Tracks testing >= 200 variants","Tracks testing >= 300 variants",
                        "Tracks testing >= 400 variants","Tracks testing >= 500 variants","Tracks testing >= 1000 variants",
                        "Tracks testing >= 1500 variants")
  colnames(Tracks) <- c("N","Percentage ")
  return(Tracks)
}

#SUPPLEMENTARY TABLE 9: SUMMARY STATISTICS AND SPECIFICITY AND SENSITIVITY FOR EACH ASSAY FULLFILLING BOTH CRITERIA  ( #VARIANTS TESTED > 10 AND # VARIANTS IN REFERENCE PANEL [NON-PATHOGENIC; PATHOGENIC] [3:3])***********************************************************************************
#Calculating and creating data matrix
espec_sens_calculation <- function(reference_panel){
  Espec_Sensi <- as.data.frame(matrix(c(colSums((reference_panel[,10:ncol(reference_panel)] == 0) & (reference_panel$"T7" > 3),na.rm=TRUE),
                                        colSums((reference_panel[,10:ncol(reference_panel)] == 1) & (reference_panel$"T7" > 3),na.rm=TRUE),
                                        colSums((reference_panel[,10:ncol(reference_panel)] == 0) & (reference_panel$"T7" < 3),na.rm=TRUE),
                                        colSums((reference_panel[,10:ncol(reference_panel)] == 1) & (reference_panel$"T7" < 3),na.rm=TRUE)),
                                      ncol = 4))
  rownames(Espec_Sensi) <- c("T10",	"T11",	"T12",	"T13",	"T14",	"T15",	"T16",	"T17",	"T18",
                             "T19",	"T20",	"T21",	"T22",	"T23",	"T24",	"T25",	"T26",	"T27",
                             "T28",	"T29",	"T30",	"T31",	"T32",	"T33",	"T34",	"T35",	"T36",
                             "T37",	"T38",	"T39",	"T40",	"T41",	"T42",	"T43",	"T44",	"T45",
                             "T46",	"T47",	"T48",	"T49",	"T50",	"T51",	"T52",	"T53",	"T54",
                             "T55",	"T56",	"T57",	"T58",	"T59",	"T60",	"T61",	"T62",	"T63",
                             "T64",	"T65",	"T66",	"T67",	"T68",	"T69",	"T70",	"T71",	"T72",
                             "T73",	"T74",	"T75",	"T76",	"T77",	"T78",	"T79",	"T80",	"T81",
                             "T82",	"T83",	"T84",	"T85",	"T86",	"T87",	"T88",	"T89",	"T90",
                             "T91",	"T92",	"T93",	"T94",	"T95",	"T96",	"T97",	"T98",	"T99",
                             "T100",	"T101",	"T102",	"T103",	"T104",	"T105",	"T106",	"T107",	"T108",
                             "T109",	"T110",	"T111",	"T112",	"T113",	"T114",	"T115",	"T116",	"T117",
                             "T118",	"T119",	"T120",	"T121",	"T122",	"T123",	"T124",	"T125",	"T126",
                             "T127",	"T128",	"T129",	"T130",	"T131",	"T132",	"T133",	"T134",	"T135",
                             "T136",	"T137",	"T138",	"T139",	"T140")
  colnames(Espec_Sensi) <- c("classified_false_no_impact", "classified_true_impact","classified_true_no_impact", "classified_false_impact")
  Espec_Sensi$"number of reference variants as bening" <- rowSums(Espec_Sensi[,1:2],na.rm=TRUE)
  Espec_Sensi$"number of reference variants as pathogenic" <- rowSums(Espec_Sensi[,3:4],na.rm=TRUE)
  Espec_Sensi$"total" <- rowSums(Espec_Sensi[,c(5,6)],na.rm=TRUE)

  #Sensitivity and Specificity calculation and CI bounds
  Espec_Sensi$"Sensitivity" <- round(Espec_Sensi[1:131,2] / Espec_Sensi[1:131,5], digits = 2)
  Sens_l95b <- 0
  Sens_u95b <- 0
  z <- 1*1.95996
  zsq <- z*z
  for(i in 1:nrow(Espec_Sensi)) {
    #SENSITIVITY CI
    n <- Espec_Sensi[i,5]
    p <- Espec_Sensi[i,8]
    q <- 1-p
    #lower bound
    num <- (2*n*p)+zsq-1-(z*sqrt(zsq-2-(1/n)+4*p*((n*q)+1)))
    denom <- 2*(n+zsq)
    l95b <- num/denom
    Sens_l95b[i] <- l95b
    #upper bound
    num <- (2*n*p)+zsq+1+(z*sqrt(zsq+2-(1/n)+4*p*((n*q)-1)))
    denom <- 2*(n+zsq)
    u95b <- num/denom
    Sens_u95b[i] <- u95b
  }
  Espec_Sensi <- as.data.frame(cbind(Espec_Sensi,(round(Sens_l95b, digits =2))))
  Espec_Sensi <- as.data.frame(cbind(Espec_Sensi,(round(Sens_u95b,digits=2))))

  Espec_Sensi$"specificity" <- round(Espec_Sensi[1:131,3] / Espec_Sensi[1:131,6], digits = 2)
  Espec_l95b <- 0
  Espec_u95b <- 0
  for(i in 1:nrow(Espec_Sensi)) {
    #SPECIFICITY CI
    n <- Espec_Sensi[i,6]
    p <- Espec_Sensi[i,11]
    q <- 1-p
    #lower bound
    num <- (2*n*p)+zsq-1-(z*sqrt(zsq-2-(1/n)+4*p*((n*q)+1)))
    denom <- 2*(n+zsq)
    l95b <- num/denom
    Espec_l95b[i] <- l95b
    #upper bound
    num <- (2*n*p)+zsq+1+(z*sqrt(zsq+2-(1/n)+4*p*((n*q)-1)))
    denom <- 2*(n+zsq)
    u95b <- num/denom
    Espec_u95b[i] <- u95b
  }
  Espec_Sensi <- as.data.frame(cbind(Espec_Sensi,(round(Espec_l95b, digits =2))))
  Espec_Sensi <- as.data.frame(cbind(Espec_Sensi,(round(Espec_u95b,digits=2))))

  #Changing columns names
  colnames(Espec_Sensi)[9] <- "95Percentage  CI lower"
  colnames(Espec_Sensi)[10] <- "95Percentage  CI upper"
  colnames(Espec_Sensi)[12] <- "95Percentage  CI lower"
  colnames(Espec_Sensi)[13] <- "95Percentage  CI upper"

  #Prior, P1 calculation
  Espec_Sensi$"Proportion pathogenic (Prior, P1)" <- round(Espec_Sensi[,5]/Espec_Sensi[,7], digits=2)
  #"+1 proportions" (Posterior, P2)
  Espec_Sensi$"+1 proportions (Posterior, P2) - Pathogenic" <- round((Espec_Sensi[,2]+Espec_Sensi[,4])/((Espec_Sensi[,2]+Espec_Sensi[,4])+1), digits=3)
  Espec_Sensi$"+1 proportions (Posterior, P2) - Benign" <- round(1/((Espec_Sensi[,1]+Espec_Sensi[,3])+1), digits=3)
  #OddsPath calculation
  Espec_Sensi$"Odds path - Pathogenic" <- round((Espec_Sensi[,15]*(1-Espec_Sensi[,14]))/((1-Espec_Sensi[,15])*Espec_Sensi[,14]), digits =3)
  Espec_Sensi$"Odds path - Benign" <- round((Espec_Sensi[,16]*(1-Espec_Sensi[,14]))/((1-Espec_Sensi[,16])*Espec_Sensi[,14]), digits =3)

  #Evidence strenght equivalence
  Espec_Sensi$"Evidence_Path" <- ifelse(Espec_Sensi$"Odds path - Pathogenic" > 350 & Espec_Sensi$"Odds path - Pathogenic" < 800, "PS3_very_strong",
                                 ifelse(Espec_Sensi$"Odds path - Pathogenic" > 18.7 & Espec_Sensi$"Odds path - Pathogenic" <= 350, "PS3",
                                 ifelse(Espec_Sensi$"Odds path - Pathogenic" > 4.3 & Espec_Sensi$"Odds path - Pathogenic" <= 18.7,"PS3_moderate",
                                 ifelse(Espec_Sensi$"Odds path - Pathogenic" > 2.1 & Espec_Sensi$"Odds path - Pathogenic" <= 4.3,"PS3_supporting", "indeterminate"))))

  Espec_Sensi$"Evidence_Benign" <- ifelse(Espec_Sensi$"Odds path - Benign" < 0.053 & Espec_Sensi$"Odds path - Benign" > 0, "BS3",
                                   ifelse(Espec_Sensi$"Odds path - Benign" < 0.23 & Espec_Sensi$"Odds path - Benign" >=0.053, "BS3_moderate",
                                   ifelse(Espec_Sensi$"Odds path - Benign" < 0.48 & Espec_Sensi$"Odds path - Benign" >=0.23,"BS3_supporting","Indeterminate")))
 return(Espec_Sensi)
}

#SUPPLEMENTARY TABLE 6: CRITERIAS
#Tracks with number of variants in reference panel non-pathogenic; pathogenic  X:Y
ratio_criteria <- function(reference_panel_counts, reference_panel){
  Ratios <- matrix(c(sum((reference_panel_counts[,5] >= 3) & (reference_panel_counts[,6] >= 3)),round(sum((reference_panel_counts[,5] >= 3) & (reference_panel_counts[,6] >= 3))*100/nrow(reference_panel),2),
                     sum((reference_panel_counts[,5] >= 4) & (reference_panel_counts[,6] >= 4)),round(sum((reference_panel_counts[,5] >= 4) & (reference_panel_counts[,6] >= 4))*100/nrow(reference_panel),2),
                     sum((reference_panel_counts[,5] >= 5) & (reference_panel_counts[,6] >= 5)),round(sum((reference_panel_counts[,5] >= 5) & (reference_panel_counts[,6] >= 5))*100/nrow(reference_panel),2),
                     sum((reference_panel_counts[,5] >= 10) & (reference_panel_counts[,6] >= 10)),round(sum((reference_panel_counts[,5] >= 10) & (reference_panel_counts[,6] >= 10))*100/nrow(reference_panel),2)),
                   ncol=2, byrow = TRUE)
  rownames(Ratios) <- c("Tracks with # variants in ENIGMA reference panel [non-pathogenic; pathogenic] >= [3:3]",
                        "Tracks with # variants in ENIGMA reference panel [non-pathogenic; pathogenic] >= [4:4]",
                        "Tracks with # variants in ENIGMA reference panel [non-pathogenic; pathogenic] >= [5:5]",
                        "Tracks with # variants in ENIGMA reference panel [non-pathogenic; pathogenic] >= [10:10]")
  colnames(Ratios) <- c("N","Percentage")
  return(Ratios)
}
#Tracks meeting the two criteria ( number of variants tested >= 10 and number of variants in reference panel equal or greater non-pathogenic; pathogenic x:y)
ratio_criteria_2 <- function(reference_panel_counts, reference_panel){
  Criteria <- matrix(c(sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 3) & (reference_panel_counts[,6] >= 3)),
                       round(sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 3) & (reference_panel_counts[,6] >= 3))*100/nrow(reference_panel),2),
                       sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 4) & (reference_panel_counts[,6] >= 4)),
                       round(sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 4) & (reference_panel_counts[,6] >= 4))*100/nrow(reference_panel),2),
                       sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 5) & (reference_panel_counts[,6] >= 5)),
                       round(sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 5) & (reference_panel_counts[,6] >= 5))*100/nrow(reference_panel),2),
                       sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 10) & (reference_panel_counts[,6] >= 10)),
                       round(sum((reference_panel[,3] >= 10) & (reference_panel_counts[,5] >= 10) & (reference_panel_counts[,6] >= 10))*100/nrow(reference_panel),2)),
                     ncol=2, byrow=TRUE)
  rownames(Criteria) <- c("Tracks meeting the two criteria ( #variants tested >= 10 and # variants in ENIGMA reference panel >= [non-pathogenic; pathogenic] [3:3])",
                          "Tracks meeting the two criteria ( #variants tested >= 10 and # variants in ENIGMA reference panel >= [non-pathogenic; pathogenic] [4:4])",
                          "Tracks meeting the two criteria ( #variants tested >= 10 and # variants in ENIGMA reference panel >= [non-pathogenic; pathogenic] [5:5])",
                          "Tracks meeting the two criteria ( #variants tested >= 10 and # variants in ENIGMA reference panel >= [non-pathogenic; pathogenic] [10:10])")
  colnames(Criteria) <- c("N","Percentage")
  return(Criteria)
}

#Creating the HI-SET Table
hi_set_table <- function(Espec_Sensi, reference_panel){
  hi_set_table <- data.frame()[1:11009,]
  for (i in 1:nrow(Espec_Sensi)) {
    if ((Espec_Sensi[i,5])>= 4 & (Espec_Sensi[i,6])>= 4 & (reference_panel[i,3]) >= 10 & (Espec_Sensi[i,8]) >= 0.8 & (Espec_Sensi[i,11]) >= 0.8){
      hi_set_table <- as.data.frame(rbind(hi_set_table, Espec_Sensi[i,]))
    }
  }
  return(hi_set_table)
}

#Creating the HI-SET Tracks
hi_set_tracks <- function(hi_set_table, master_table){
  hi_set_tracks <- data.frame()[1:nrow(master_table),]
  tracks <- list()
  for (i in rownames(hi_set_table)){
    for (x in colnames(master_table)){
      if (i == x){
        hi_set_tracks <- as.data.frame(cbind(hi_set_tracks, master_table[,x]))
        q <- colnames(master_table[x])
        tracks <- c(tracks, q)
      }
    }
  }
  colnames(hi_set_tracks) <- tracks
  row.names(hi_set_tracks) <- 1:11009
  hi_set_tracks <- data.frame(cbind(master_table[,c(5,7,9)],hi_set_tracks))
  return(hi_set_tracks)
}

#SUPPLEMENTARY TABLE 7: ASSAY CLASSES
assay_classes <- function(assay_classes, df_variants_reference_panel, Espec_Sensi){
  assay_classe <- matrix(c(sum(assay_classes[,5] == 2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==2)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==2))/sum(assay_classes[,5] == 2))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==2) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==2) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 2))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==2) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==2)))*100,2),

                           sum(assay_classes[,5] == 3),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==3)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==3))/sum(assay_classes[,5] == 3))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==3) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==3) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 3))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==3) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==3)))*100,2),

                           sum(assay_classes[,5] == 4),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==4)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==4))/sum(assay_classes[,5] == 4))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==4) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==4) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 4))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==4) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==4)))*100,2),

                           sum(assay_classes[,5] == 5),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==5)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==5))/sum(assay_classes[,5] == 5))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==5) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==5) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 5))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==5) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==5)))*100,2),

                           sum(assay_classes[,5] == 6),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==6)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==6))/sum(assay_classes[,5] == 6))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==6) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==6) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 6))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==6) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==6)))*100,2),

                           sum(assay_classes[,5] == 7),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==7)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==7))/sum(assay_classes[,5] == 7))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==7) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==7) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 7))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==7) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==7)))*100,2),

                           sum(assay_classes[,5] == 8),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==8))/sum(assay_classes[,5] == 8))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==8) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==8) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 8))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==8) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==8)))*100,2),

                           sum(assay_classes[,5] == 9),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==9)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==9))/sum(assay_classes[,5] == 9))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==9) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==9) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 9))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==9) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==9)))*100,2),

                           sum(assay_classes[,5] == 10),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==10)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==10))/sum(assay_classes[,5] == 10))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==10) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==10) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 10))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==10) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==10)))*100,2),

                           sum(assay_classes[,5] == 11),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==11)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==11))/sum(assay_classes[,5] == 11))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==11) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==11) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 11))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==11) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==11)))*100,2),

                           sum(assay_classes[,5] == 12),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==12)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==12))/sum(assay_classes[,5] == 12))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==12) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==12) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 12))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==12) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==12)))*100,2),

                           sum(assay_classes[,5] == 13),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==13)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==13))/sum(assay_classes[,5] == 13))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==13) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==13) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] == 13))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==13) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] ==13)))*100,2),

                           nrow(df_variants_reference_panel),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5]>=1)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5]>=1))/nrow(df_variants_reference_panel))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] >=1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] >=1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,5] >= 1))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] >=1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,5] >=1)))*100,2)),
                         ncol = 6, byrow = TRUE)
  rownames(assay_classe) <- c("Protein expression, stability and folding",	"Binding",	"Recombination",
                              "Chromosome/Mitotic apparatus", "Proliferation/Growth",	"Sensitivity",
                              "Localization",	"Transcriptional activation",	"Focus formation",
                              "Catalytic activity",	"Cell viability", 	"Cell cycle checkpoint", "Total")
  colnames(assay_classe) <- c("N", "Met_criteria","Percentage_met_criteria", "Validated", "Percentage_validated", "Percentage_of_all_meeting_criteria_that_validated")
  return(assay_classe)
}
#By host species
host_species <- function(assay_classes, df_variants_reference_panel, Espec_Sensi){
  host_species <- matrix(c(sum(assay_classes[,9] == 1),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==1)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==1))/sum(assay_classes[,9] == 1))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] == 1))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==1)))*100,2),

                           sum(assay_classes[,9] == 2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==2)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==2))/sum(assay_classes[,9] == 2))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==2) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==2) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] == 2))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==2) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==2)))*100,2),

                           sum(assay_classes[,9] == 3),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==3)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==3))/sum(assay_classes[,9] == 3))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==3) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==3) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] == 3))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==3) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==3)))*100,2),

                           sum(assay_classes[,9] == 4),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==4)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==4))/sum(assay_classes[,9] == 4))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==4) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==4) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] == 4))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==4) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==4)))*100,2),

                           sum(assay_classes[,9] == 5),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==5)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==5))/sum(assay_classes[,9] == 5))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==5) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==5) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] == 5))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==5) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==5)))*100,2),

                           sum(assay_classes[,9] == 6),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==6)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==6))/sum(assay_classes[,9] == 6))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==6) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==6) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] == 6))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==6) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] ==6)))*100,2),

                           nrow(df_variants_reference_panel),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] >=1)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] >=1))/sum(assay_classes[,9] >= 1))*100,2),
                           sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] >=1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8)),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] >=1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum(assay_classes[,9] >= 1))*100,2),
                           round((sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] >=1) & (Espec_Sensi[,8]>= 0.8) & (Espec_Sensi[,11]>=0.8))/sum((df_variants_reference_panel[,3] >= 10) & (Espec_Sensi[,5] >= 4) & (Espec_Sensi[,6] >= 4) & (assay_classes[,9] >=1)))*100,2)),
                         ncol = 6, byrow = TRUE)
  rownames(host_species) <- c("Homo sapiens",	"Saccharomyces cerevisiae",	"Mus musculus",
                              "Escherichia coli", "Oryctolagus cuniculus",	"Mesocricetus auratus", "Total")
  colnames(host_species) <- c("N", "Met_criteria","Percentage_met_criteria", "Validated", "Percentage_validated", "Percentage_of_all_meeting_criteria_that_validated")
  return(host_species)
}

#Hi-Set Counts
hi_set_counts <- function(hi_set_tracks){
  hi_set_counts <- data.frame()[1:nrow(hi_set_tracks),]
  #Adds two columns to the end of the table with numbers of "no functional impact" and "functional impact", respectively, for each missense variant
  hi_set_counts <- cbind(hi_set_counts, rowSums(hi_set_tracks[,4:ncol(hi_set_tracks)] == 0, na.rm= TRUE))
  hi_set_counts <- cbind(hi_set_counts, rowSums(hi_set_tracks[,4:ncol(hi_set_tracks)] == 1, na.rm= TRUE))
  #Adds one column to the end of the table after sum of the columns "no functional impact" and "functional impact"
  hi_set_counts <- cbind(hi_set_counts, rowSums(hi_set_tracks[,4:ncol(hi_set_tracks)] <= 1, na.rm= TRUE))
  colnames(hi_set_counts) <- c("no_functional_impact_Hi_set", "functional_impact_Hi_set", "number_of_tests_Hi_set")
  rownames(hi_set_counts) <- 1:nrow(hi_set_tracks)
  return(hi_set_counts)
}

#Numbers cited in the paper
hi_set_numbers <- function (hi_set_track){
  hi_set_track$"number_of_tests_Hi_set" <- rowSums(hi_set_track[,4:ncol(hi_set_track)] <= 1, na.rm= TRUE)
  y <- sum(hi_set_track$"number_of_tests_Hi_set" >= 1)
  x <- sum((hi_set_track$"T7" > 0)  & (hi_set_track$"number_of_tests_Hi_set" >= 1), na.rm= TRUE)
  z <- y - x
  hi_set_numbers <- matrix(c(y,
                             z,
                             x,
                             sum(hi_set_track$"number_of_tests_Hi_set" == 1)))
  rownames(hi_set_numbers) <- c("Number of variants tested in Hi Set", "Number of VUS tested in Hi Set",
                                "Number of variants tested present in reference panel", "Number of variants tested 1 time only")
  colnames(hi_set_numbers)<-c("Final numbers")
  return(hi_set_numbers)
}

#SUPPLEMENTARY TABLE 5C: VUS only (excluding reference variants)
vus_tested <- function(master_table, hi_set_tracks, reference_panel){
  master_table$"T7"[is.na(master_table$"T7")] <- "NA"
  master_table$"number of tests" <- rowSums(master_table[,10:ncol(master_table)] <= 1, na.rm= TRUE)
  hi_set_tracks$"number_of_tests_Hi_set" <- rowSums(hi_set_tracks[,4:ncol(hi_set_tracks)] <= 1, na.rm= TRUE)
  hi_set_tracks$"T7"[is.na(hi_set_tracks$"T7")] <- "NA"

  VUS_tested<-matrix(c(sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 1)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 1))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 1))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 1)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 1))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 1))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 2)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 2))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 2))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 2)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 2))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 2))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 10)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 10))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 10))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 10)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 10))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 10))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 15)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 15))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 15))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 15)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 15))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 15))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 20)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 20))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 20))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 20)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 20))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 20))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 25)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 25))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 25))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 25)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 25))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 25))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 30)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 30))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 30))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 30)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 30))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 30))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 50)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 50))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 50))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 50)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 50))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 50))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 60)),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 60))*100)/nrow(master_table),2),
                       round((sum((master_table$"T7" == "NA") & (master_table$"number of tests" >= 60))*100)/(nrow(master_table)- nrow(reference_panel)),2),

                       sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 60)),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 60))*100)/nrow(master_table),2),
                       round((sum((hi_set_tracks$"T7" == "NA") & (hi_set_tracks$"number_of_tests_Hi_set" >= 60))*100)/(nrow(master_table)- nrow(reference_panel)),2)),
                     ncol = 6, byrow=TRUE)
  rownames(VUS_tested) <- c("VUS Tested >= one time","VUS tested >= two times",
                            "VUS tested >= 10 times", "VUS tested >= 15 times", "VUS tested >= 20 times","VUS tested >= 25 times",
                            "VUS tested >= 30 times","VUS tested >= 50 times","VUS tested >= 60 times")
  colnames(VUS_tested)<-c("N_All_Functional_tracks","Percentage", "Percentage_VUS", "N_Hi_Set_E", "Percentage", "Percentage_VUS")
  return(VUS_tested)
}

#Generating the Stable9 ordered
stable8_form <- function(df_variants_reference_panel,my_data, assay_classes, Espec_Sensi, stable2){
  #dataframe with all missense variants tested (including the indeterminated/uncertain)
  whole_variants <- matrix(c(23,	4,	25,	32,	2,	35,	15,	2,	6,	6,	8,	25,	32,	35,	25,	4,	41,	117,	117,	9,	5,	5,
                             15,	16,	25,	80,	32,	5,	6,	2,	70,	13,	3,	2,	65,	9,	11,	15,	4,	5,	8,	13,	77,	36,
                             76,	1,	8,	11,	4,	3,	36,	5,	3,	2,	11,	8,	5,	3,	13,	13,	8,	2,	8,	1,	8,	8,	4,
                             9,	35,	12,	65,	77,	15,	22,	575,	5,	13,	1,	12,	12,	9,	36,	28,	8,	2,	2,	77,	4,	5,	5,
                             18,	18,	333,	3,	2,	3,	12,	28,	7,	11,	25,	3,	2,	35,	11,	117,	2,	2,	2,	10,	25,	7,
                             10,	10,	3,	5,	4,	25,	10,	25,	7,	2086,	6,	1677,	1677,	5,	3,	2,	2,	2,	2))
  #% of tested missense variants classified
  variants_classified <- matrix(c(round((df_variants_reference_panel[,3]/whole_variants)*100,2)),byrow = TRUE)
  #% of all possible missense (n = 11,009) tested
  possible_missense <- matrix(c(round((whole_variants/11009)*100,1)),byrow = TRUE)
  #Number of all documented missense variants classified
  documented_classified <- matrix(c(colSums((my_data[,10:140] <= 1) & (my_data$"T9" == 1),na.rm=TRUE)),byrow=TRUE)
  #% of all documented missense variants classified
  documented_classified <- cbind(documented_classified,matrix(c(round(documented_classified/df_variants_reference_panel[,3]*100,2)),byrow=TRUE))
  #gathering all data into a new table
  stable8 <- data.frame()[0:131,]
  stable8 <- cbind(stable8, stable2[,1:2],whole_variants, df_variants_reference_panel[,3],
                   variants_classified,possible_missense,documented_classified, Espec_Sensi[,c(7,5,6,8:14)],
                   df_variants_reference_panel[,c(2,1)],Espec_Sensi[,15:20])
  colnames(stable8)[c(2:9)] <- c("assay", "number of missense variants tested", "number of missense variants classified",
                                 "percentage of tested missense variants classified",
                                 "percentage of all possible missense (n = 11,009) tested",
                                 "number of all documented missense variants classified",
                                 "percentage of all documented missense variants classified",
                                 "number total of Classified")
  return(stable8)
}

#Remove variants not tested in Hi-Set
stable9_form <- function(hi_set_tracks, hi_set_table){
  n_col <- ncol(hi_set_tracks)
  set_numb <- ncol(hi_set_tracks)+5
  functional_col <- ncol(hi_set_tracks)+2
  no_functional_col <- ncol(hi_set_tracks)+1

  hi_set_tracks$"no_functional_impact_Hi_set" <- rowSums(hi_set_tracks[,4:n_col] == 0, na.rm = TRUE)
  hi_set_tracks$"functional_impact_Hi_set" <- rowSums(hi_set_tracks[,4:n_col] == 1, na.rm = TRUE)
  hi_set_tracks$"number_of_tests_Hi_set" <- rowSums(hi_set_tracks[,4:n_col] <= 1, na.rm = TRUE)
  clean_hi_set <- data.frame()[1:11009,]
  for (i in 1:nrow(hi_set_tracks)){
    select <- hi_set_tracks[i,ncol(hi_set_tracks)]
    if (select > 0){
      clean_hi_set <- as.data.frame(rbind(clean_hi_set, hi_set_tracks[i,]))
    }
  }

  clean_hi_set$"Call" <- ifelse(clean_hi_set$"no_functional_impact_Hi_set" >0 & clean_hi_set$"functional_impact_Hi_set" > 0, "Discordant", "")
  clean_hi_set[is.na(clean_hi_set)] <- "NA"
  for (x in colnames(clean_hi_set)){
    for (y in rownames(hi_set_table)){
      if (x == y){
        evidence_path <- hi_set_table[y,19]
        evidence_benign <- hi_set_table[y,20]
        for (z in 1:nrow(clean_hi_set)){

          if (clean_hi_set[z,x] == 1){
            if (clean_hi_set[z,ncol(clean_hi_set)-1]== 1){
              clean_hi_set[z,ncol(clean_hi_set)] <- evidence_path
            }
          }
          if (clean_hi_set[z,x] == 0){
            if (clean_hi_set[z,ncol(clean_hi_set)-1]== 1){
              clean_hi_set[z,ncol(clean_hi_set)] <- evidence_benign
            }
          }
        }
      }
    }
  }

  for (z in 1:nrow(clean_hi_set)){
    p <- set_numb
    if (clean_hi_set[z,no_functional_col] > 1 & clean_hi_set[z,functional_col] == 0){
      for (q in 4:n_col){
        if (clean_hi_set[z,q]==0){
          y <- colnames(clean_hi_set)[q]
          for (x in rownames(hi_set_table)){
            if (x==y){
              clean_hi_set[z,p] <- hi_set_table [y,20]
              p <- p+1
            }
          }
        }
      }
    }
    if (clean_hi_set[z,functional_col] > 1 & clean_hi_set[z,no_functional_col] == 0){
      for (q in 4:n_col){
        if (clean_hi_set[z,q]==1){
          y <- colnames(clean_hi_set)[q]
          for (x in rownames(hi_set_table)){
            if (x==y){
              clean_hi_set[z,p] <- hi_set_table [y,19]
              p <- p+1
            }
          }
        }
      }
    }
  }
  return(clean_hi_set)
}

stable10_form <- function(stable9_E_C, hi_set_table){
  set_numb <- 33
  clean_discordant <- data.frame()[1:11009,]
  for (i in 1:nrow(stable9_E_C)){
    select <- stable9_E_C[i,29]
    if (select == "Discordant"){
      clean_discordant <- as.data.frame(rbind(clean_discordant, stable9_E_C[i,1:29]))
    }
  }
  clean_discordant$"Ratio Benign/Path" <- (-1*((clean_discordant[,27]-clean_discordant[,28])/clean_discordant[,27]))
  clean_discordant$"Log2ratio" <- log2(clean_discordant$"Ratio Benign/Path")

  clean_discordant$Call <- NA
  clean_discordant[is.na(clean_discordant)] <- "NA"
  for (x in colnames(clean_discordant)){
    for (y in rownames(Espec_Sens_table_E_C)){
      if (x == y){
        evidence_path <- hi_set_table[y,19]
        evidence_benign <- hi_set_table[y,20]

        for (z in 1:nrow(clean_discordant)){ #Now moving through the rows to find the variant tested in the track

          if (clean_discordant[z,x] == 1){ #Find the functional impact mark
            if (clean_discordant[z,30]== 1){ #Check if the variant was tested only once
              clean_discordant[z,32] <- "Discordant"
            }
          }
          if (clean_discordant[z,x] == 0){
            if (clean_discordant[z,30]== 2){
              clean_discordant[z,32] <- "Discordant"
            }
          }
        }
      }
    }
  }
  return (clean_discordant)
}

stable11_form <- function(stable1, hi_set_table){
  #Setting important numbers before adding columns to the table
  n_col <- ncol(stable1)
  set_numb <- 147
  #Getting a new table with only tested variants from table 1
  stable1_modified <- stable1
  stable1_modified$"no_functional_impact_Hi_set" <- rowSums(stable1_modified[,10:140] == 0, na.rm = TRUE)
  stable1_modified$"functional_impact_Hi_set" <- rowSums(stable1_modified[,10:140] == 1, na.rm = TRUE)
  stable1_modified$"number_of_tests_Hi_set" <- rowSums(stable1_modified[,10:140] <= 1, na.rm = TRUE)
  clean_stable1 <- data.frame()[1:11009,]
  for (i in 1:nrow(stable1_modified)){
    select <- stable1_modified[i,ncol(stable1_modified)]
    if (select > 0){
      clean_stable1 <- as.data.frame(rbind(clean_stable1, stable1_modified[i,]))
    }
  }

  clean_stable1$"Ratio Benign/Path" <- (-1*((clean_stable1[,142]-clean_stable1[,143])/clean_stable1[,142]))
  clean_stable1$"Log2ratio" <- log2(clean_stable1$"Ratio Benign/Path")

  #Catching the evidence path for each track (column) - one by one
  clean_stable1$Call <- NA
  clean_stable1[is.na(clean_stable1)] <- "NA"
  for (x in colnames(clean_stable1)){
    for (y in rownames(Espec_Sens_table_E_C)){
      if (x == y){
        evidence_path <- hi_set_table[y,19]
        evidence_benign <- hi_set_table[y,20]

        for (z in 1:nrow(clean_stable1)){ #Now moving through the rows to find the variant tested in the track

          if (clean_stable1[z,x] == 1){ #Find the functional impact mark
            if (clean_stable1[z,143]== 1){ #Check if the variant was tested only once
              clean_stable1[z,146] <- evidence_path
            }
          }
          if (clean_stable1[z,x] == 0){
            if (clean_stable1[z,143]== 1){
              clean_stable1[z,146] <- evidence_benign
            }
          }
        }
      }
    }
  }

  for (z in 1:2701){
    p <- set_numb
    if (clean_stable1[z,141] > 1 & clean_stable1[z,142] == 0){
      for (q in 10:140){
        if (clean_stable1[z,q]==0){
          y <- colnames(clean_stable1)[q]
          for (x in rownames(hi_set_table)){
            if (x==y){
              clean_stable1[z,p] <- hi_set_table [y,20]
              p <- p+1
            }
          }
        }
      }
    }
    if (clean_stable1[z,142] > 1 & clean_stable1[z,141] == 0){
      for (q in 10:140){
        if (clean_stable1[z,q]==1){
          y <- colnames(clean_stable1)[q]
          for (x in rownames(hi_set_table)){
            if (x==y){
              clean_stable1[z,p] <- hi_set_table [y,19]
              p <- p+1
            }
          }
        }
      }
    }
    if (clean_stable1[z,145] > 0){
      for (q in 10:140){
        if (clean_stable1[z,q]==0){
          y <- colnames(clean_stable1)[q]
          for (x in rownames(hi_set_table)){
            if (x==y){
              clean_stable1[z,p] <- hi_set_table [y,20]
              p <- p+1
            }
          }
        }
      }
    }
    if (clean_stable1[z,145] < 0){
      for (q in 10:140){
        if (clean_stable1[z,q]==1){
          y <- colnames(clean_stable1)[q]
          for (x in rownames(hi_set_table)){
            if (x==y){
              clean_stable1[z,p] <- hi_set_table [y,19]
              p <- p+1
            }
          }
        }
      }
    }
  }

  return(clean_stable1)
}

#OUTPUTS
#Espec_sens_Calculation
Espec_Sens_table_E_C <- espec_sens_calculation(reference_panel_E_C)
#Counts from Reference panel Enigma and Reference panel Enigma + ClinVar
reference_panel_E_C_counts <- count_reference_panel(reference_panel_E_C, stable1)
#Hi-Set Table
hi_set_table_E_C <- hi_set_table(Espec_Sens_table_E_C, reference_panel_E_C_counts)
#Hi-set tracks
hi_set_tracks_E_C <- hi_set_tracks(hi_set_table_E_C, stable1)
#Hi-set Counts
hi_set_counts_E_C <- hi_set_counts(hi_set_tracks_E_C)
#Hi-set numbers cited on the paper
hi_set_numbers_E_C <- hi_set_numbers(hi_set_tracks_E_C)

#######################################################|||| TABLES ||||#######################################################

#STABL5_A_Number of variants tested per
variants_tested <- count_variant_tested(stable1,stable1_counts)
#STABLE5_B_Documented variants tested
BRCAExchange_E_C_counts <- count_BRCAExchange(stable1,stable1_counts,reference_panel_E_C)
#STABLE5_C_VUS_ONLY(Excluding reference panel)
VUS_only_E_C <- vus_tested(stable1, hi_set_tracks_E_C, reference_panel_E_C)

#STABLE6_A_Assay throughput
tested_variants_E_C <- count_variant_track(reference_panel_E_C_counts)
#STABLE6_B_Number of tracks with number in reference panel [non-pathogenic; pathogenic]>= [x:y]
ratio_criteria_1_E_C <- ratio_criteria(Espec_Sens_table_E_C,reference_panel_E_C_counts)
#STABLE6_C_Number of tracks with number in reference panel tested variants >= 10 and [non-pathogenic; pathogenic]>= [x:y]
ratio_criteria_2_E_C <- ratio_criteria_2(Espec_Sens_table_E_C, reference_panel_E_C_counts)

#STABLE7_A_Assay_classes
assay_class_table_E_C <- assay_classes(stable2, reference_panel_E_C_counts, Espec_Sens_table_E_C)
#STABLE7_B_Host_Species_classes
host_species_table_E_C <- host_species(stable2, reference_panel_E_C_counts, Espec_Sens_table_E_C)

#STABLE8
stable8_E_C <- stable8_form (reference_panel_E_C_counts, stable1, assay_class_table_E_C, Espec_Sens_table_E_C, stable2)

#STABLE9
stable9_E_C <- stable9_form (hi_set_tracks_E_C, hi_set_table_E_C)

#STABLE10
stable10_E_C <- stable10_form (stable9_E_C)

#STABLE11
stable11_E_C <- stable11_form (stable1, Espec_Sens_table_E_C)

#######################################################|||| OUTPUT ||||#######################################################

#Output excel file for reference panel [ENIGMA + CLIN VAR]
write.xlsx(variants_tested,file="output_E_C.xlsx", sheetName="STable6_Missense_Variants_tested")
write.xlsx(BRCAExchange_E_C_counts,file="output_E_C.xlsx", sheetName="STable6_Documented_Variants(BRCAExchange)", append=TRUE)
write.xlsx(VUS_only_E_C,file="output_E_C.xlsx", sheetName="STable6_VUS_only_(excluding reference variants)", append=TRUE)
write.xlsx(tested_variants_E_C,file="output_E_C.xlsx", sheetName="STable7_Functional_track_throughput", append=TRUE)
write.xlsx(ratio_criteria_1_E_C,file="output_E_C.xlsx", sheetName="STable7_#_variants_classified_by_track", append=TRUE)
write.xlsx(ratio_criteria_2_E_C,file="output_E_C.xlsx", sheetName="STable7_#_tracks_meeting_criteria", append=TRUE)
write.xlsx(assay_class_table_E_C,file="output_E_C.xlsx", sheetName="STable8_Assay_classes", append=TRUE)
write.xlsx(host_species_table_E_C,file="output_E_C.xlsx", sheetName="STable8_host_classes", append=TRUE)
write.xlsx(stable8_E_C,file="output_E_C.xlsx", sheetName="STable9_Sensitivity_specificity", append=TRUE, row.names=FALSE)
write.xlsx(stable9_E_C,file="output_E_C.xlsx", sheetName="STable 10_Hi_Set_Approach", append=TRUE, row.names=FALSE)
write.xlsx(stable11_E_C,file="output_E_C.xlsx", sheetName="STable 11_Majority_Voting_approach", append=TRUE)
#Extra numbers
write.xlsx(hi_set_numbers_E_C,file="output_E_C.xlsx", sheetName="Numbers_from_Hi_Set_Pick", append=TRUE)


library(Seurat)

source("R/functions/GenePlot2.r")

dn.fig = "figures/hand_gating"
dir.create(dn.fig, showWarnings = F, recursive = T)

fn = "data/H1_day0_scranNorm_adtbatchNorm_dist_clustered_TSNE_labels.rds"
h1 = readRDS(fn)

bc = SubsetData(h1, subset.name = "K1", accept.value = "C3")

GenePlot2(bc, "CD20_PROT","CD38_PROT")
ggsave(file.path(dn.fig, "CD20_CD38_biplot_Bc.png"), w=6, h=4)

dat = h1@assay$CITE@data
df = dat %>% as.data.frame() %>%
  tibble::rownames_to_column("protein") %>%
  gather("cell","value", -protein)
df = df %>%
  left_join(h1@meta.data %>% dplyr::select("barcode_check",starts_with("K")),
            by=c("cell"="barcode_check"))
df.bc = df %>%
  dplyr::filter(K1=="C3")

prot = c("CD3_PROT", "CD56_PROT", "CD14_PROT", "CD19_PROT", "CD20_PROT", "CD38_PROT")

ggplot(df %>% dplyr::filter(protein %in% prot), aes(value)) +
  geom_density(fill = "grey80", alpha = 0.5) +
  geom_density(data = df.bc %>% dplyr::filter(protein %in% prot), fill = "lightgreen", alpha = 0.5) +
  facet_wrap(~protein) +
  scale_x_continuous(breaks = seq(-5, 15, 5), minor_breaks = seq(-5 , 16, 1)) +
  coord_cartesian(xlim = c(-3,16)) +
  theme_bw()
ggsave(file.path(dn.fig, "Density_Bc_markers.png"))

  # GenePlot2(h1, "CD3_PROT","CD56_PROT")

CBSig = function(SeuratObject, scale.norm = F,
                 cd3.th = 3, cd56.th = 3, cd14.th = 2, cd19.th = 3, cd20.th = 3.75, cd38.th = 7) {
  cat(paste(cd3.th, cd56.th, cd14.th, cd19.th, cd20.th, cd38.th,"\n"))
  gated.idx <- SeuratObject@assay$CITE@data["CD3_PROT",  ] < cd3.th &
    SeuratObject@assay$CITE@data["CD14_PROT", ] < cd14.th &
    SeuratObject@assay$CITE@data["CD56_PROT", ] < cd56.th &
    SeuratObject@assay$CITE@data["CD19_PROT", ] > cd19.th &
    SeuratObject@assay$CITE@data["CD20_PROT", ] > cd20.th &
    SeuratObject@assay$CITE@data["CD38_PROT", ] > cd38.th
  gated.cells = SeuratObject@cell.names[gated.idx]
  cat(length(gated.cells), " gated cells\n")
  gated <- SubsetData(SeuratObject, cells.use = gated.cells, do.clean = F)
  if (scale.norm == TRUE) {
    gated <- NormalizeData(gated)
    gated <- ScaleData(gated)
  }
  else  {
    return(gated)
  }
  return(gated)
}

bc38 = CBSig(h1)
bc.hg = CBSig(h1, cd20.th = -Inf, cd38.th = -Inf)
# GenePlot2(bc.hg, "CD20_PROT","CD38_PROT")
# ggsave("CD20_CD38_biplot_Bc_hg.png", w=6, h=4)

df.b = data.frame(x = -Inf, y = -Inf, label=glue::glue("{length(bc@cell.names)} cells"))
df.b.sel = data.frame(x = 4, y = 10.5, label=glue::glue("{length(bc38@cell.names)} cells"))
GenePlot2(bc.hg, "CD20_PROT","CD38_PROT") +
  geom_rect(xmin=3.75, xmax=12.1, ymin=6.5, ymax=10.6, col="red", fill=NA, size=0.5) +
  geom_text(data=df.b, aes(x, y, label=label), hjust = -0.1, vjust = -0.5, col="black", inherit.aes = F) +
  geom_text(data=df.b.sel, aes(x, y, label=label), hjust = 0, vjust = 1, col="red", inherit.aes = F) +
  xlab("CD20") + ylab("CD38")
ggsave(file.path(dn.fig, "CD20_CD38_biplot_Bc_hg_gate.png"), w=4, h=3)
ggsave(file.path(dn.fig, "CD20_CD38_biplot_Bc_hg_gate.pdf"), w=4, h=3)

df.subj = h1@meta.data %>% dplyr::mutate(response = str_remove(adjmfc.time, "d0 ")) %>%
  dplyr::select(subject=sampleid, response) %>%
  dplyr::mutate(subject = factor(subject)) %>%
  distinct() %>%
  dplyr::arrange(subject)

df.bc = bc@meta.data %>%
  dplyr::mutate(response = str_remove(adjmfc.time, "d0 ")) %>%
  dplyr::select(subject=sampleid, response)
df.bc38 = bc38@meta.data %>%
  dplyr::mutate(response = str_remove(adjmfc.time, "d0 ")) %>%
  dplyr::select(subject=sampleid, response)

bc.sum = df.bc %>%
  group_by(subject, response) %>%
  summarise(nBC = n())
cd38.sum = df.bc38 %>%
  group_by(subject, response) %>%
  summarise(nCD38 = n())
DF = left_join(bc.sum, cd38.sum, by=c("subject","response")) %>%
  dplyr::mutate(nCD38 = ifelse(is.na(nCD38), 0, nCD38)) %>%
  dplyr::mutate(CD38.freq = nCD38/nBC) %>%
  dplyr::mutate(response = factor(response, levels=c("low","high")))
fwrite(DF, file="results/CITEseq_CD38hi_cell_data.txt", sep="\t")

# Compare low and high responders ----------------------

X = DF$CD38.freq
Y = DF$response
w.pv = wilcox.test(X[Y=="high"], X[Y=="low"], exact=F, alternative="greater")$p.value

df.text = data.frame(label.w.pv=sprintf("p = %.2g",w.pv))

df.text$x = 0.25
df.text$y = 0.1

ggplot(DF, aes(x=response, y=CD38.freq, group=response)) +
  geom_boxplot(alpha=1, outlier.colour = NA) +
  geom_dotplot(binaxis = "y", stackdir = "center", aes(fill=response)) +
  scale_fill_manual(values=c("white", "black"), name="Response",
                    breaks=c(0,2), labels=c("low","high")) +
  geom_text(data = df.text, aes(label=label.w.pv), x=1.5, y=Inf,vjust=1.1, hjust=0.5, size=4, inherit.aes = F) +
  xlab("Response") + ylab("CD38++ cells (% of alive B cells)") +
  # coord_cartesian(xlim=c(0.8, 2.2)) +
  theme_bw() + theme(legend.position="none") +
  theme(panel.border = element_blank(), strip.background = element_blank(),
        strip.text.x = element_text(size=12),
        panel.spacing = unit(0,"mm"), panel.grid.major.x = element_blank(),
        axis.ticks = element_blank())

fn.fig = file.path(dn.fig, "CITEseq_CBSig_vs_response")
ggsave(paste0(fn.fig, ".png"), w=3, h=3.5)
ggsave(paste0(fn.fig, ".pdf"), w=3, h=3.5, useDingbats=F)


0x1, 0xABCD, -0x2, +0x3L


# Made for http://themilksource.com/

# Map using CDC data for Exclusive Breastfeeding at 6 months by state
# CDC data: https://www.cdc.gov/breastfeeding/pdf/2016breastfeedingreportcard.pdf

# ggmap:
# D. Kahle and H. Wickham. ggmap: Spatial Visualization with ggplot2. The R Journal, 5(1), 144-161. URL
# http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf

library(tidyverse)
library(albersusa)
library(sf)
library(sp)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(scales)

us <- usa_composite()
us_map <- fortify(us, region="name")

url <- "https://raw.githubusercontent.com/sgranitz/milksource/master/cdc_bf_data.csv"
bf_data <- as_tibble(read.csv(url, fileEncoding = "UTF-8-BOM"))

bf_data <- bf_data %>%
  mutate(grp = ifelse(exc_bf_6mth > 15, ifelse(exc_bf_6mth >= 25.5, "c. >=25.5%", "b. 15-25.5%"), "a. <15%"))
bf_data$grp <- as.factor(bf_data$grp)

bf_data <- bf_data %>%
  mutate(grp2 = ifelse(ever > 50,
                      ifelse(ever >= 60,
                             ifelse(ever >= 70,
                                    ifelse(ever >= 80,
                                           ifelse(ever >= 90,
                                                  "e. >= 90",
                                                  "d. >=80"),
                                           "c. >= 70"),
                                    "b. >= 60"),
                             "a. >=50"),
                      "<50"))
bf_data$grp2 <- as.factor(bf_data$grp2)

bf_data$name <- as.character(bf_data$state)
us2 <- as_tibble(us) %>% left_join(bf_data, bf = "name")

gg <- ggplot() +
  geom_map(data = us_map, map = us_map,
           aes(x = long, y = lat, map_id = id),
               color = "#2b2b2b", size = 0.1, fill = NA) +
  theme_map()

gg +
  geom_map(data = us@data, map = us_map,
           aes(fill = us2$grp, map_id = name),
           color = "white", size = 0.1) +
  coord_map() + labs(title = "Exclusive Breastfeeding Rates") +
  scale_fill_manual(name = "Per CDC 2016 Report: Exclusive Breastfeeding at 6 Months, %",
                    values = c("#EAB79E", "#D3DFE0", "#ECDBBC")) +
  theme(legend.position = "bottom",
        legend.key.width = unit(3, "lines"))

gg +
  geom_map(data = us@data, map = us_map,
           aes(fill = us2$grp2, map_id = name),
           color = "white", size = 0.1) +
  coord_map() + labs(title = "Ever Breastfed Rates") +
  scale_fill_manual(name = "Per CDC 2016 Report: Ever Breastfed, %",
                    values = c("#EFFAFF", "#E4F0F2", "#D3DFE0", "#C2CDCF", "#B2BCC0")) +
  theme(legend.position = "bottom",
        legend.key.width = unit(3, "lines"))


source("global.R")
all_gene <- read.csv("refGene.cleaned.txt", header=T, sep="\t")
names(all_gene) <- c("chr", "strand", "start", "end", "cdsStart", "cdsEnd", "exonCount", "exonStarts", "exonEnds", "score", "name")
temp <-with(all_gene, substr(chr, 4, 100))
temp[temp=="X"] <- 23
all_gene$chr <- as.integer(temp)

all_snp <- read.table("annotation.variant.txt", header=T, sep="\t")
all_snp <- subset(all_snp, tier2=="No")
region <- read.table("../dat/HD_region_HG19.txt", header=F)
names(region) <- c("chr", "start", "end", "region")

result <- c()
for (i in unique(all_snp$HD)){

  region_start <-region$start[i] - region_flank_5
  region_end <- region$end[i] + region_flank_5
  gene <- subset(all_gene, chr==region$chr[i]& ((start > region_start& start < region_end )|(end > region_start& end < region_end )))
  if(dim(gene)[1]>0){
    result <- rbind(result, data.frame(HD=i, region=paste(sep="", "chr", region[i,]$chr, ":", sprintf("%0.2f", region[i,]$start/1000000), "-", sprintf("%0.2f", region[i,]$end/1000000)), gene= gene$name))
  }
}

write.table(result, "../dat/HD.genelist.txt", col.names=T, row.names=T, sep="\t", quote=F)


#' Get hourly ladder counts from FPC
#'
#' Downloads hourly counts of one species in one ladder at a mainstem Columbia or Snake River dam.
#' @param sdate Start date `character` in m-d-yyyy format. e.g., `"8-1-2019"`
#' @param edate End date `character` in m-d-yyyy format
#' @param ladder_id  BON `1:2`, TDA `3:4`, JDA `5:6`, MCN `7:8`, IHR `9:10`, LMN `11:12`, LGS `13`, LGR `14`
#' @param species Species for which to get counts. e.g., `"adultchinook"` or `"jackchinook"`
#' @import dplyr
#' @importFrom lubridate ymd_h
#' @importFrom readr parse_number
#' @importFrom tidyr pivot_longer
#' @return
#' @export
#'
#' @examples
#' \dontrun{
#' fpc_hourly(sdate="8-1-2019", edate="10-1-2019", ladder_id=1, species="adultchinook")
#' fpc_hourly(sdate="8-1-2019", edate="10-1-2019", ladder_id=1, species="jackchinook")
#' }
fpc_hourly <- function(sdate, edate, ladder_id, species="adultchinook"){
  if(!(ladder_id %in% 1:14)){stop("Ladder id doesn't exist")}
  url <- "https://www.fpc.org/adults/R_adultcoequeries_hourlyladderspecies_getresults.php?"
  suppressWarnings(readr::read_csv(glue::glue("{url}ladderid={ladder_id}&sdate={sdate}&edate={edate}&species={species}"),
                                   col_types=readr::cols(CountDate=readr::col_date(format="%b %d %Y")))) %>%
    dplyr::filter(Description!="Average") %>%
    tidyr::pivot_longer(cols=dplyr::starts_with("h"),values_to="Count") %>%
    dplyr::mutate(hr=readr::parse_number(name)-1,
                  date_time=lubridate::ymd_h(paste(CountDate,hr))) %>%
    dplyr::select(-name)
}


#Expedia final project for BA with R
setwd("C:/Users/chand/Box Sync/Study/Sem 1/BA with Rattle/Final Project-Expedia/data")
getwd()

# Load library
library(randomForest)
library(rattle)
library(rpart)
library(rpart.plot)
library(rpart)
library(rpart.plot)
library(readxl)
library(rpart)
library(rpart.plot)
library(Hmisc)
library(rattle)
library(plyr)
library(caTools)
#For Imputing missing Values
library(mice)

#This command loads the expedia data set into a variable called "traindata."
traindata = read.csv("train.csv")
str(traindata)

#This command writes the expedia data set for country 2 in a new csv file called "traincnty"
traincnty = subset(traindata, prop_country_id == 2)
nrow(traincnty)
write.csv(traincnty, "C:/Users/chand/Box Sync/Study/Sem 1/BA with Rattle/Final Project-Expedia/data/traincnty.csv")
summary(traincnty)

#Creating equally distributed subset with proper weightage
grossdata = subset(traindata,gross_bookings_usd !="NULL")
str(gsRtGnull)
gsRtGnull = subset(traindata, gross_bookings_usd =="NULL")
gsRtGnull = subset(gsRtGnull, random_bool == "0")
eqdata= rbind(gsRtGnull[1:276592, ],  grossdata)
remove(grossdata,gsRtGnull)
str(eqdata)
# New dataframe with useful columns
eqdata2=eqdata[c("srch_id","date_time","site_id","visitor_location_country_id","prop_id","prop_starrating","prop_review_score","prop_brand_bool","prop_location_score1","prop_location_score2","prop_log_historical_price","position","price_usd","promotion_flag","srch_destination_id","srch_length_of_stay","srch_booking_window","srch_adults_count","srch_children_count","srch_room_count","srch_saturday_night_bool","random_bool","booking_bool","gross_bookings_usd","click_bool")]
str(eqdata2)
#remove(eqdata)

#Changing the value from NULL to NA's
eqdata2$prop_location_score2[eqdata2$prop_location_score2 == "NULL"] <- NA
eqdata2$prop_review_score[eqdata2$prop_review_score == "NULL"] <- NA
eqdata2$gross_bookings_usd[eqdata2$gross_bookings_usd == "NULL"] <- NA

#Impute NA values of these 2 columns.
set.seed(88)
simple = eqdata2[c("prop_review_score","prop_starrating","price_usd","prop_location_score1","prop_location_score2","promotion_flag","srch_room_count")]
imputed = complete(mice(simple))
summary(imputed)
eqdata2$prop_location_score2 = imputed$prop_location_score2
eqdata2$prop_review_score = imputed$prop_review_score
summary(eqdata2)

# Code to remove these NA rows
eqdata2=eqdata2[!(is.na(eqdata2$prop_location_score2) | eqdata2$prop_location_score2==""), ]
eqdata2=eqdata2[!(is.na(eqdata2$prop_review_score) | eqdata2$prop_review_score==""), ]
str(eqdata2)
summary(eqdata2$prop_location_score2)
summary(eqdata2$prop_review_score)
eqdata2$prop_review_score <- as.factor(eqdata2$prop_review_score)
eqdata2$prop_starrating <- as.factor(eqdata2$prop_starrating)
eqdata2$prop_brand_bool <- as.factor(eqdata2$prop_brand_bool)
eqdata2$prop_location_score2 <- as.numeric(eqdata2$prop_location_score2)
eqdata2$promotion_flag <- as.factor(eqdata2$promotion_flag)
eqdata2$srch_length_of_stay <- as.factor(eqdata2$srch_length_of_stay)
eqdata2$srch_booking_window <- as.factor(eqdata2$srch_booking_window)
eqdata2$srch_adults_count <- as.factor(eqdata2$srch_adults_count)
eqdata2$srch_children_count <- as.factor(eqdata2$srch_children_count)
eqdata2$srch_room_count <- as.factor(eqdata2$srch_room_count)
eqdata2$srch_saturday_night_bool <- as.factor(eqdata2$srch_saturday_night_bool)
eqdata2$random_bool <- as.factor(eqdata2$random_bool)
eqdata2$booking_bool <- as.factor(eqdata2$booking_bool)
eqdata2$click_bool <- as.factor(eqdata2$click_bool)
gross_bookings_usdSafe = eqdata2$gross_bookings_usd
# was working but suddenly factor to int conversion is having issues
eqdata2$gross_bookings_usd <- gross_bookings_usdSafe
  #as.numeric(levels(eqdata2$gross_bookings_usd))
head(gross_bookings_usd22)
str(gross_bookings_usd22)
gross_bookings_usdtest = as.numeric.factor(gross_bookings_usdSafe)
str(gross_bookings_usdtest)
eqdata2$srch_booking_window <- as.numeric(eqdata2$srch_booking_window)
str(eqdata2)
head(eqdata2$srch_booking_window)
nrow(eqdata2)

# Randomly split data for a smaller set for fast processing
set.seed(88)
split = sample.split(eqdata2, SplitRatio = 0.03)
qualityTrain = subset(eqdata2, split == TRUE)
dataTest = subset(eqdata2, split == FALSE)
nrow(qualityTrain)
str(qualityTrain)
#Find Correlations of all the 25 numeric variables
cor()

#Random forest
varNames <- names(qualityTrain)
# Exclude ID or Response variable
varNames <- varNames[!varNames %in% c("date_time","click_bool","srch_id","site_id","gross_bookings_usd","visitor_location_country_id","prop_id","srch_destination_id","srch_destination_id")]
varNames
# add + sign between exploratory variables
varNames1 <- paste(varNames, collapse = "+")
  varNames1
# Add response variable and convert to a formula object
rf.form <- as.formula(paste("gross_bookings_usd", varNames1, sep = " ~ "))
rf.form
rJungle <- randomForest(rf.form, qualityTrain, ntree=50, importance=T)
rJungle2 <- randomForest(rf.form, eqdata2, ntree=10, importance=T)
plot(rJungle)
varImpPlot(rjungle)
#testdata = read.csv("test.csv")
#str(testdata)
Prediction <- predict(rJungle, dataTest)
rmse <- mean((dataTest$gross_bookings_usd - predictions)^2)
print(rmse)

#Linear Model
PointsReg <- lm(gross_bookings_usd ~ site_id+srch_adults_count+srch_children_count, data=grossdata, rm.na = TRUE)

summary(PointsReg)
summary(qualityTrain)
# Make predictions on test set
PointsPredictions = predict(PointsReg, newdata=dataTest)
# Compute out-of-sample R^2
SSE = sum((PointsPredictions - dataTest$gross_bookings_usd)^2)
SST = sum((mean(qualityTrain$gross_bookings_usd) - dataTest$gross_bookings_usd)^2)
R2 = 1 - SSE/SST
R2
# Compute the RMSE
RMSE = sqrt(SSE/nrow(dataTest))
RMSE

#Decision Tree
bk_tree <- rpart(booking_bool ~  prop_starrating + price_usd + prop_location_score1 + prop_location_score2 + promotion_flag + srch_room_count, data = eqdata2, method = "class")
printcp(bk_tree)
plotcp(bk_tree)
#prune(titanictree, cp= titanictree$cptable[which.min(titanictree$cptable[,"xerror"]),"CP"])
fancyRpartPlot(bk_tree)

# Logistic Regression Model
QualityLog = glm(booking_bool ~ prop_starrating + price_usd + prop_location_score1 + prop_location_score2 + promotion_flag + srch_room_count, data=dataTest, family=binomial)
summary(QualityLog)
# Make predictions on training set
predictTrain = predict(QualityLog, type="response")
# Analyze predictions
summary(predictTrain)
tapply(predictTrain, qualityTrain$PoorCare, mean)

StevensForest = randomForest(Reverse ~ Circuit + Issue + Petitioner + Respondent + LowerCourt + Unconst, data = Train, ntree=200, nodesize=25) #no. of trees and bucket size

######################################Mrunmayi################
#Cp complexity parameter: large cp means small trees, best soln had cp close to 0
#Randomize data
myraw <- read.csv("without _clickbool.csv",header=TRUE)
head(myraw)
rand <- runif(nrow(myraw))

myrawrand <- myraw[order(rand), ]

#Partition data
myrawrandtrain <- myrawrand[1:4000, ]
myrawrandtest <- myrawrand[4001:8634, ]

#Build decision tree
myrawrandtraintree <- rpart(booking_bool ~ ., data = myrawrandtrain, method = "class")
myrawrandtraintree
printcp(myrawrandtraintree)
?printcp
plotcp(myrawrandtraintree)
#prune(titanictree, cp= titanictree$cptable[which.min(titanictree$cptable[,"xerror"]),"CP"])
fancyRpartPlot(myrawrandtraintree)

#Evaluate tree performance
myrawrandtrain$treepred <- predict(myrawrandtraintree, myrawrandtrain, type = "class")
table(Actual = myrawrandtrain$booking_bool, Predicted = myrawrandtrain$treepred)
tree.sse = sum((myrawrandtrain$treepred - myrawrandtrain$booking_bool)^2)
tree.sse

myrawrandtrain$treepredcorrect <- myrawrandtrain$booking_bool == myrawrandtrain$treepred
traintreecorrectcount <- length(which(myrawrandtrain$treepredcorrect))
traintreeincorrectcount <- nrow(myrawrandtrain) - traintreecorrectcount
traintreeerrorrate <- traintreeincorrectcount/nrow(myrawrandtrain)
traintreeaccuracy <- 1-traintreeerrorrate

myrawrandtest$treepred <- predict(myrawrandtraintree, myrawrandtest, type = "class")
table(Actual = myrawrandtest$booking_bool, Predicted = myrawrandtest$treepred)

myrawrandtest$treepredcorrect <- myrawrandtest$booking_bool == myrawrandtest$treepred
testtreecorrectcount <- length(which(myrawrandtest$treepredcorrect))
testtreeincorrectcount <- nrow(myrawrandtest) - testtreecorrectcount
testtreeerrorrate <- testtreeincorrectcount/nrow(myrawrandtest)
testtreeaccuracy <- 1-testtreeerrorrate

#Compare
paste("TREE TRAIN: Error Rate (", traintreeerrorrate, ") Accuracy (", traintreeaccuracy, ")")
paste("TREE TEST: Error Rate (", testtreeerrorrate, ") Accuracy (", testtreeaccuracy, ")")


PATH = "/Users/Joe/Documents/Blog/"
PATH = "~/Documents/paulinshek.github.io/"
library(dplyr)

source(paste0(PATH,"_source/2016-02-14-relationship-prediction/dataclean.r"))
source(paste0(PATH,"_source/2016-02-14-relationship-prediction/calibrationPlotModelFitting.r"))

outcome1 = as.vector(hcmst$w2_broke_up)

bup1 = rep(0,nrow(partner))
bup1[which(outcome1=="broke up")] = 1
bup1[which(outcome1=="partner passed away")] = NA
bup1[which(is.na(outcome1))]=NA

data = cbind(partner,respondent)

levels(data$income)[c(2,4)] = "more"
levels(data$income)[c(3)] = "same"

data$politics = as.vector(data$politics)
data$ppolitics = as.vector(data$ppolitics)
data$ppolitics[which(data$ppolitics%in%c("nopreference","independent","another party, please specify"))]="other"

data$samepolitics = "no"
data$samepolitics[which(data$politics=="republican"&data$ppolitics=="republican")]="samecon"
data$samepolitics[which(data$politics=="democrat"&data$ppolitics=="democrat")]="samelib"

data$samereligion = 0
data$religion=as.vector(data$religion)
data$preligion=as.vector(data$preligion)

data$samereligion[which(data$religion==data$preligion)] = 1

data$howmet = "other"
data$howmet[which(data$metschool=="yes")]="school"
data$howmet[which(data$metwork=="yes")]="work"
data$howmet[which(data$metchurch=="yes")]="church"
data$howmet[which(data$metdservice=="yes")]="dservice"
data$howmet[which(data$metvacation=="yes")]="vacation"
data$howmet[which(data$metnightout=="yes")]="nightout"
data$howmet[which(data$metsocialclub=="yes")]="socialclub"
data$howmet[which(data$metprivparty=="yes")]="privparty"

data$relstat = NA
data$relstat[which(data$married=="yes, i am married")]="married"
data$relstat[which(data$sexpartner=="yes, i have a sexual partner (boyfriend or girlfriend)")]="sexpartner"
data$relstat[which(data$sexpartner=="i have a romantic partner who is not yet a sexual partner")]="nonsexpartner"

data$livingtog=as.vector(data$livingtog)
data$livingtog[which(data$livingtog=="refused")]=NA

data$bup1=bup1
rm(bup1)

datatouse = data %>% filter(!is.na(page),!is.na(bup1))

lm1 = glm(bup1~relstat+
            I((age+page)/2)+
            I(pmin(age,page)/pmax(age,page))+
            I(age-romanceage)+
            I(log(age-romanceage+1))+
            I(log(log(age-romanceage+1)+1))+
            #I(romanceage-metage)+
            income+
            #samepolitics+
            samereligion+
            howmet+
            hhincomeg+
            I(kidsu18==0)+
            relqual+
            livingtog,
            data = datatouse,
            family = binomial(link = logit))

logit=function(x){return(exp(x)/(exp(x)+1))}
invlogit = function(x){return(log(x/(1 -x)))}

probs = logit(predict(lm1,datatouse))
CalibrationPlot(probs,datatouse$bup1,bins = 20)

summary(lm1)

lm2 = step(lm1) # bup1 ~ relstat + I(log(age - romanceage + 1)) + relqual + livingtog

levels(data$education)=c(NA,NA,rep("school",8),"hs","collnodeg",rep("degree",2),"masters","doc")
levels(data$peducation)=c(NA,rep("school",8),"hs","collnodeg",rep("degree",2),"masters","doc")

data$education = as.numeric(data$education)
data$peducation = as.numeric(data$peducation)

data$edudiff = abs(data$education-data$peducation)

levels(data$mumeducation)=c(NA,rep("school",8),"hs","collnodeg",rep("degree",2),"masters","doc")
levels(data$pmumeducation)=c(NA,rep("school",8),"hs","collnodeg",rep("degree",2),"masters","doc")

data$mumeducation = as.numeric(data$mumeducation)
data$pmumeducation = as.numeric(data$pmumeducation)

data$mumedudiff = abs(data$mumeducation-data$pmumeducation)

data$parentapprove=as.vector(data$parentapprove)
data$parentapprove[which(data$parentapprove=="refused")]=NA

datapaste = apply(data,MARGIN = 1,FUN = paste,collapse="~")

datatouse = data %>% filter(!is.na(page),
                            !is.na(bup1),
                            !is.na(edudiff),
                            !is.na(mumedudiff),
                            !is.na(relspermonth),
                            !grepl("refused", datapaste))

lm3 = glm(bup1~relstat+
            I((age+page)/2)+
            I(pmin(age,page)/pmax(age,page))+
            I(age-romanceage)+
            I(log(age-romanceage+1))+
            I(log(log(age-romanceage+1)+1))+
            #I(romanceage-metage)+
            income+
            #samepolitics+
            samereligion+
            metwork+
            metschool+
            metchurch+
            metdservice+
            metvacation+
            metnightout+
            metsocialclub+
            metprivparty+
            metother+
            hhincomeg+
            kidsu18+
            relqual+
            livingtog +
            mumedudiff +
            #parentapprove +
            edudiff+
            I(education+peducation)+
            I(log(relspermonth+1)),
            data = datatouse,
            family = binomial(link = logit))

lm4 = step(lm3)

probs = logit(predict(lm4,datatouse))
CalibrationPlot(probs,datatouse$bup1,bins = 20)