|
| 1 | +source("constants.R") |
| 2 | + |
| 3 | +# Calculate a single global Bayesian FDR (BFDR) threshold across the pairwise coloc |
| 4 | +# results of every LD block, and store it in the pipeline_metadata directory so the |
| 5 | +# downstream clustering step can apply one consistent H4 threshold everywhere. |
| 6 | + |
| 7 | +parser <- argparser::arg_parser("Calculate the global Bayesian FDR threshold across all LD blocks") |
| 8 | +parser <- argparser::add_argument( |
| 9 | + parser, |
| 10 | + "--output_file", |
| 11 | + help = "File to write the global BFDR threshold and metadata to", |
| 12 | + type = "character", |
| 13 | + default = NA |
| 14 | +) |
| 15 | +parser <- argparser::add_argument( |
| 16 | + parser, |
| 17 | + "--target_bfdr", |
| 18 | + help = "Target Bayesian FDR", |
| 19 | + type = "numeric", |
| 20 | + default = target_global_bfdr |
| 21 | +) |
| 22 | +parser <- argparser::add_argument( |
| 23 | + parser, |
| 24 | + "--block_list", |
| 25 | + help = "CSV of studies to exclude from the global BFDR calculation (columns: id_pattern, cis_trans)", |
| 26 | + type = "character", |
| 27 | + default = NA |
| 28 | +) |
| 29 | +parser <- argparser::add_argument( |
| 30 | + parser, |
| 31 | + "--completed_output_file", |
| 32 | + help = "Sentinel file to write once the global BFDR threshold has been calculated", |
| 33 | + type = "character", |
| 34 | + default = NA |
| 35 | +) |
| 36 | + |
| 37 | +args <- argparser::parse_args(parser) |
| 38 | + |
| 39 | +main <- function() { |
| 40 | + start_time <- Sys.time() |
| 41 | + |
| 42 | + output_file <- args$output_file |
| 43 | + if (is.null(output_file) || is.na(output_file)) { |
| 44 | + output_file <- global_bfdr_file_path(args$block_list) |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + pairwise_files <- Sys.glob( |
| 49 | + glue::glue("{ld_block_data_dir}*/*/*/{ld_block_file_basenames()$coloc_pairwise}") |
| 50 | + ) |
| 51 | + message(glue::glue("Found {length(pairwise_files)} LD blocks with pairwise coloc results")) |
| 52 | + |
| 53 | + block_list <- NULL |
| 54 | + if (!is.null(args$block_list) && !is.na(args$block_list) && file.exists(args$block_list)) { |
| 55 | + block_list <- vroom::vroom(args$block_list, show_col_types = FALSE) |
| 56 | + } |
| 57 | + |
| 58 | + h4_values <- unlist(lapply(pairwise_files, read_block_h4, block_list = block_list), use.names = FALSE) |
| 59 | + message(glue::glue( |
| 60 | + "Collected {length(h4_values)} pairwise H4 values in {diff_time_taken(start_time)}" |
| 61 | + )) |
| 62 | + |
| 63 | + |
| 64 | + bfdr <- calculate_global_bfdr_threshold( |
| 65 | + posterior_probs = h4_values, |
| 66 | + target_bfdr = args$target_bfdr, |
| 67 | + minimum_threshold = posterior_prob_h4_threshold |
| 68 | + ) |
| 69 | + |
| 70 | + global_bfdr <- data.frame( |
| 71 | + threshold = bfdr$threshold, |
| 72 | + estimated_bfdr = bfdr$estimated_bfdr, |
| 73 | + n_discoveries = bfdr$n_discoveries, |
| 74 | + target_bfdr = args$target_bfdr, |
| 75 | + minimum_threshold = posterior_prob_h4_threshold, |
| 76 | + n_pairs = length(h4_values), |
| 77 | + n_blocks = length(pairwise_files), |
| 78 | + default_threshold = posterior_prob_h4_threshold |
| 79 | + ) |
| 80 | + |
| 81 | + message(glue::glue( |
| 82 | + "Global BFDR threshold: {signif(bfdr$threshold, 3)} ", |
| 83 | + "(default {signif(posterior_prob_h4_threshold, 3)}, target BFDR={args$target_bfdr}, ", |
| 84 | + "discoveries={bfdr$n_discoveries}, estimated BFDR={signif(bfdr$estimated_bfdr, 3)})" |
| 85 | + )) |
| 86 | + |
| 87 | + dir.create(dirname(output_file), showWarnings = FALSE, recursive = TRUE) |
| 88 | + vroom::vroom_write(global_bfdr, output_file) |
| 89 | + message(glue::glue("Wrote global BFDR threshold to {output_file}")) |
| 90 | + |
| 91 | + completed_output_file <- args$completed_output_file |
| 92 | + if (!is.null(completed_output_file) && !is.na(completed_output_file)) { |
| 93 | + dir.create(dirname(completed_output_file), showWarnings = FALSE, recursive = TRUE) |
| 94 | + vroom::vroom_write(data.frame(), completed_output_file) |
| 95 | + message(glue::glue("Wrote completion sentinel to {completed_output_file}")) |
| 96 | + } |
| 97 | + |
| 98 | + return() |
| 99 | +} |
| 100 | + |
| 101 | +calculate_global_bfdr_threshold <- function(posterior_probs, target_bfdr = 0.05, minimum_threshold = 0.8) { |
| 102 | + valid_probs <- posterior_probs[is.finite(posterior_probs)] |
| 103 | + valid_probs <- valid_probs[!is.na(valid_probs)] |
| 104 | + if (length(valid_probs) == 0) { |
| 105 | + return(list( |
| 106 | + threshold = 1, |
| 107 | + estimated_bfdr = NA_real_, |
| 108 | + n_discoveries = 0 |
| 109 | + )) |
| 110 | + } |
| 111 | + |
| 112 | + ordered_probs <- sort(valid_probs, decreasing = TRUE) |
| 113 | + cumulative_bfdr <- cumsum(1 - ordered_probs) / seq_along(ordered_probs) |
| 114 | + passing_indices <- which(cumulative_bfdr <= target_bfdr) |
| 115 | + |
| 116 | + if (length(passing_indices) == 0) { |
| 117 | + return(list( |
| 118 | + threshold = 1, |
| 119 | + estimated_bfdr = NA_real_, |
| 120 | + n_discoveries = 0 |
| 121 | + )) |
| 122 | + } |
| 123 | + |
| 124 | + threshold <- ordered_probs[max(passing_indices)] |
| 125 | + threshold <- max(threshold, minimum_threshold) |
| 126 | + discoveries <- valid_probs[valid_probs >= threshold] |
| 127 | + estimated_bfdr <- if (length(discoveries) == 0) NA_real_ else sum(1 - discoveries) / length(discoveries) |
| 128 | + |
| 129 | + return(list( |
| 130 | + threshold = threshold, |
| 131 | + estimated_bfdr = estimated_bfdr, |
| 132 | + n_discoveries = length(discoveries) |
| 133 | + )) |
| 134 | +} |
| 135 | + |
| 136 | +#' Read the eligible H4 values from a block's pairwise coloc results. |
| 137 | +#' Self-pairs (study_a == study_b) and ignored pairs are excluded from the global pool, |
| 138 | +#' since self-colocs are ~1 by construction and would bias the FDR threshold downward. |
| 139 | +read_block_h4 <- function(pairwise_file, block_list = NULL) { |
| 140 | + if (!file.exists(pairwise_file) || file.size(pairwise_file) == 0) { |
| 141 | + return(numeric(0)) |
| 142 | + } |
| 143 | + |
| 144 | + header <- tryCatch( |
| 145 | + names(data.table::fread(pairwise_file, nrows = 0, showProgress = FALSE)), |
| 146 | + error = function(e) character(0) |
| 147 | + ) |
| 148 | + h4_column <- if ("h4" %in% header) "h4" else if ("PP.H4.abf" %in% header) "PP.H4.abf" else NA_character_ |
| 149 | + if (is.na(h4_column)) { |
| 150 | + return(numeric(0)) |
| 151 | + } |
| 152 | + |
| 153 | + select_columns <- intersect(c(h4_column, "study_a", "study_b", "ignore"), header) |
| 154 | + pairs <- tryCatch( |
| 155 | + data.table::fread( |
| 156 | + pairwise_file, |
| 157 | + select = select_columns, |
| 158 | + showProgress = FALSE, |
| 159 | + verbose = FALSE |
| 160 | + ), |
| 161 | + error = function(e) { |
| 162 | + message(glue::glue("Could not read {pairwise_file}: {e$message}")) |
| 163 | + return(NULL) |
| 164 | + } |
| 165 | + ) |
| 166 | + if (is.null(pairs) || nrow(pairs) == 0) { |
| 167 | + return(numeric(0)) |
| 168 | + } |
| 169 | + |
| 170 | + keep <- rep(TRUE, nrow(pairs)) |
| 171 | + if (all(c("study_a", "study_b") %in% names(pairs))) { |
| 172 | + keep <- keep & !(!is.na(pairs$study_a) & !is.na(pairs$study_b) & pairs$study_a == pairs$study_b) |
| 173 | + } |
| 174 | + if ("ignore" %in% names(pairs)) { |
| 175 | + keep <- keep & !(pairs$ignore %in% TRUE) |
| 176 | + } |
| 177 | + |
| 178 | + if (!is.null(block_list) && nrow(block_list) > 0) { |
| 179 | + finemapped_file <- file.path( |
| 180 | + dirname(pairwise_file), |
| 181 | + ld_block_file_basenames()$finemapped_studies |
| 182 | + ) |
| 183 | + finemapped_studies <- if (file.exists(finemapped_file)) { |
| 184 | + vroom::vroom(finemapped_file, show_col_types = FALSE) |
| 185 | + } else { |
| 186 | + data.frame() |
| 187 | + } |
| 188 | + cis_trans_a <- finemapped_studies$cis_trans[match(pairs$study_a, finemapped_studies$study)] |
| 189 | + cis_trans_b <- finemapped_studies$cis_trans[match(pairs$study_b, finemapped_studies$study)] |
| 190 | + blocked_pair <- is_study_blocked(block_list, pairs$study_a, cis_trans_a) | |
| 191 | + is_study_blocked(block_list, pairs$study_b, cis_trans_b) |
| 192 | + keep <- keep & !blocked_pair |
| 193 | + } |
| 194 | + |
| 195 | + return(as.numeric(pairs[[h4_column]][keep])) |
| 196 | +} |
| 197 | + |
| 198 | +invisible(main()) |
0 commit comments