|
| 1 | +#' Run GoShifter enrichment pipeline |
| 2 | +#' |
| 3 | +#' Run the \href{https://github.com/immunogenomics/goshifter}{GoShifter} |
| 4 | +#' locus-specific enrichment pipeline. This function orchestrates the full |
| 5 | +#' workflow: preparing SNP maps and LD files, querying ROADMAP annotations, |
| 6 | +#' running the GoShifter permutation test, and collecting results. |
| 7 | +#' |
| 8 | +#' @param locus_dir Path to the locus-level results directory. |
| 9 | +#' @param dat A \code{data.table} or \code{data.frame} with columns |
| 10 | +#' \code{SNP}, \code{CHR}, \code{POS}, and \code{P}. |
| 11 | +#' @param SNP_group Character string labelling this group of SNPs |
| 12 | +#' (default \code{""}). |
| 13 | +#' @param goshifter_path Path to the directory containing |
| 14 | +#' \code{goshifter.py}. If \code{NULL}, the bundled copy shipped with |
| 15 | +#' \pkg{echolocatoR} is used (see \code{\link{GOSHIFTER_find_folder}}). |
| 16 | +#' @param permutations Number of permutations for the enrichment test |
| 17 | +#' (default \code{1000}). |
| 18 | +#' @param ROADMAP_search A keyword query passed to |
| 19 | +#' \code{\link{ROADMAP_query}} to filter ROADMAP annotations |
| 20 | +#' (e.g. \code{"monocyte"}). |
| 21 | +#' @param chromatin_states Character vector of chromatin state abbreviations |
| 22 | +#' to test enrichment for (default \code{c("TssA")}). |
| 23 | +#' @param R2_filter LD r-squared threshold for GoShifter |
| 24 | +#' (default \code{0.8}). |
| 25 | +#' @param overlap_threshold Minimum number of overlapping SNPs required |
| 26 | +#' before running GoShifter on an annotation (default \code{1}). |
| 27 | +#' @param force_new_goshifter If \code{TRUE}, re-run even if results |
| 28 | +#' already exist on disk (default \code{FALSE}). |
| 29 | +#' @param remove_tmps Remove intermediate GoShifter output files after |
| 30 | +#' collecting results (default \code{TRUE}). |
| 31 | +#' @param verbose Print messages (default \code{TRUE}). |
| 32 | +#' @param save_results Write combined results to a tab-delimited file |
| 33 | +#' (default \code{TRUE}). |
| 34 | +#' |
| 35 | +#' @returns A \code{data.table} of GoShifter enrichment results across |
| 36 | +#' all requested chromatin states. |
| 37 | +#' |
| 38 | +#' @source |
| 39 | +#' \href{https://github.com/immunogenomics/goshifter}{GoShifter GitHub} |
| 40 | +#' \href{https://pubmed.ncbi.nlm.nih.gov/26140449/}{ |
| 41 | +#' Trynka et al. (2015) \emph{Am J Hum Genet}} |
| 42 | +#' |
| 43 | +#' @family GOSHIFTER |
| 44 | +#' @export |
| 45 | +#' @importFrom data.table fread rbindlist fwrite |
| 46 | +#' @examples |
| 47 | +#' \dontrun{ |
| 48 | +#' locus_dir <- echodata::locus_dir |
| 49 | +#' dat <- echodata::BST1 |
| 50 | +#' gs_out <- GOSHIFTER(locus_dir = locus_dir, dat = dat) |
| 51 | +#' } |
| 52 | +GOSHIFTER <- function(locus_dir, |
| 53 | + dat, |
| 54 | + SNP_group = "", |
| 55 | + goshifter_path = NULL, |
| 56 | + permutations = 1000, |
| 57 | + ROADMAP_search = "", |
| 58 | + chromatin_states = c("TssA"), |
| 59 | + R2_filter = 0.8, |
| 60 | + overlap_threshold = 1, |
| 61 | + force_new_goshifter = FALSE, |
| 62 | + remove_tmps = TRUE, |
| 63 | + verbose = TRUE, |
| 64 | + save_results = TRUE) { |
| 65 | + |
| 66 | + ## Resolve GoShifter install path |
| 67 | + goshifter_path <- GOSHIFTER_find_folder(goshifter = goshifter_path) |
| 68 | + ## Clean up any leftover .pyc files |
| 69 | + pyc_files <- file.path( |
| 70 | + goshifter_path, |
| 71 | + c("chromtree.pyc", "data.pyc", "docopt.pyc", "functions.pyc") |
| 72 | + ) |
| 73 | + suppressWarnings(file.remove(pyc_files)) |
| 74 | + |
| 75 | + ## Chromatin-state key |
| 76 | + chromState_key <- data.table::fread( |
| 77 | + system.file("extdata/ROADMAP", |
| 78 | + "ROADMAP_chromatinState_HMM.tsv", |
| 79 | + package = "echoannot") |
| 80 | + ) |
| 81 | + |
| 82 | + ## Create GoShifter results directory |
| 83 | + GS_results_path <- file.path(locus_dir, "GoShifter") |
| 84 | + dir.create(GS_results_path, showWarnings = FALSE, recursive = TRUE) |
| 85 | + results_file <- file.path( |
| 86 | + GS_results_path, |
| 87 | + paste0("GOSHIFTER.", |
| 88 | + paste(chromatin_states, collapse = "-"), |
| 89 | + ".", SNP_group, ".txt") |
| 90 | + ) |
| 91 | + |
| 92 | + ## Prepare SNP-map file |
| 93 | + GOSHIFTER_create_snpmap( |
| 94 | + dat = dat, |
| 95 | + GS_results = GS_results_path, |
| 96 | + verbose = verbose |
| 97 | + ) |
| 98 | + |
| 99 | + ## Prepare LD files |
| 100 | + GOSHIFTER_create_LD( |
| 101 | + locus_dir = locus_dir, |
| 102 | + dat = dat, |
| 103 | + verbose = verbose |
| 104 | + ) |
| 105 | + |
| 106 | + ## Query ROADMAP annotations |
| 107 | + grl_roadmap <- ROADMAP_query( |
| 108 | + query_dat = dat, |
| 109 | + keyword_query = ROADMAP_search, |
| 110 | + nThread = 1, |
| 111 | + verbose = verbose |
| 112 | + ) |
| 113 | + |
| 114 | + ## Use cached results if available |
| 115 | + if (file.exists(results_file) && !force_new_goshifter) { |
| 116 | + GS_RESULTS <- data.table::fread(results_file) |
| 117 | + } else { |
| 118 | + ## Run GoShifter for each chromatin state |
| 119 | + GS_RESULTS <- lapply(chromatin_states, function(cs) { |
| 120 | + tryCatch({ |
| 121 | + messager("+ GoShifter:: Running on chromatin state subset:", |
| 122 | + cs, v = verbose) |
| 123 | + GOSHIFTER_run( |
| 124 | + goshifter_path = goshifter_path, |
| 125 | + locus_dir = locus_dir, |
| 126 | + GRlist = grl_roadmap, |
| 127 | + dat = dat, |
| 128 | + chromatin_state = cs, |
| 129 | + R2_filter = R2_filter, |
| 130 | + permutations = permutations, |
| 131 | + remove_tmps = remove_tmps, |
| 132 | + verbose = verbose, |
| 133 | + overlap_threshold = overlap_threshold |
| 134 | + ) |
| 135 | + }, error = function(e) { |
| 136 | + messager("GoShifter:: Error for chromatin state ", |
| 137 | + cs, ": ", conditionMessage(e), v = verbose) |
| 138 | + NULL |
| 139 | + }) |
| 140 | + }) |> data.table::rbindlist(fill = TRUE) |
| 141 | + } |
| 142 | + |
| 143 | + ## Save results |
| 144 | + if (save_results) { |
| 145 | + data.table::fwrite(GS_RESULTS, results_file, |
| 146 | + sep = "\t", quote = FALSE) |
| 147 | + } |
| 148 | + |
| 149 | + ## Clean up intermediate output files |
| 150 | + if (remove_tmps) { |
| 151 | + tmp_suffixes <- c(".nperm1000.enrich", |
| 152 | + ".nperm1000.locusscore", |
| 153 | + ".nperm1000.snpoverlap") |
| 154 | + suppressWarnings( |
| 155 | + file.remove(file.path(GS_results_path, |
| 156 | + paste0("GS_results", tmp_suffixes))) |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + return(GS_RESULTS) |
| 161 | +} |
0 commit comments