Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,40 @@ Studies are configured in `pipeline_steps/data/study_list.csv`. Each row defines
| `id_pattern` | Glob pattern to match study IDs (e.g. `ebi-a-*`, `*` for all in directory) |
| `data_type` | One of: `phenotype`, `gene_expression`, `splice_variant`, `transcript`, `protein`, `methylation`, `metabolome`, `cell_trait`, `plasma_protein` |
| `source` | Must exist in `pipeline_steps/data/study_sources.csv` |
| `data_format` | `opengwas`, `besd`, or `tsv` |
| `data_format` | `opengwas`, `besd`, `tsv`, or `summary_stats` |
| `bespoke_parsing` | Parsing variant (e.g. `none`, `gtex_sqtl`, `godmc`) |
| `data_location` | Path to the data on disk |
| `ancestry` | e.g. `EUR` |
| `reference_build` | `GRCh37` or `GRCh38` |
| `p_value_threshold` | e.g. `1.5e-4` |
| `variant_type` | `common` or `rare_exome` |
| `metadata_type` | `json` or `tsv` |
| `metadata_type` | `json`, `tsv`, or `csv` (`csv` required for `summary_stats`) |
| `coverage` | `dense` or `sparse` |

The pipeline discovers studies by matching `data_location` + `id_pattern` against the filesystem. Study IDs are derived from folder and file names.

### summary_stats format

For delimited GWAS uploads (csv/tsv), use `data_format=summary_stats` and `metadata_type=csv`. Point `data_location` at a directory containing `metadata.csv`, where each row describes one summary-stat file (same fields as the worker upload JSON):

| Column | Description |
|--------|-------------|
| `study_name` | Unique study ID (underscores become dashes) |
| `file_location` | Path to the summary-stat file (absolute, or relative to `data_location`) |
| `trait` | Trait display name |
| `sample_size` | Sample size |
| `category` | `continuous` or `categorical` |
| `file_type` | `csv` (delimited; VCF not yet supported) |
| `source` | Unique source slug for this file/paper (used in the DB `study_sources` table) |
| `name` | Display name for the source (e.g. paper or consortium name) |
| `url` | Source URL (may be empty) |
| `doi` | DOI URL or identifier (may be empty) |
| `SNP`, `RSID`, `CHR`, `BP`, `EA`, `OA`, `P`, `BETA`, `OR`, `SE`, `EAF`, `N` | Optional column map: value is the source column name in the file |

Ancestry, reference build, and p-value threshold come from the `study_list.csv` row. Extraction reuses `extract_regions_from_summary_stats.R` (same script as the worker path).

For `study_list.csv`, use `source=manual` (or another catch-all already in `study_sources.csv`). Each study’s `source` slug is taken from `metadata.csv` and stored on the study as usual. Citation fields (`name` / `url` / `doi`) stay in `metadata.csv` only and are merged into the DB `study_sources` table when databases are created — they are not written to `studies_processed`. Use a unique `source` slug per paper/dataset.

To exclude studies, add them to `pipeline_steps/data/ignore_studies.tsv` or `pipeline_steps/data/ignore_studies_rare.tsv`.

### study_sources.csv
Expand All @@ -50,7 +72,7 @@ To exclude studies, add them to `pipeline_steps/data/ignore_studies.tsv` or `pip
| `url` | Source URL |
| `doi` | DOI for citation |

When adding a new source to `study_list.csv`, add a corresponding row to `study_sources.csv`.
When adding a new source to `study_list.csv`, add a corresponding row to `study_sources.csv`. For one-off summary-stat ingestions, prefer putting citation fields in `metadata.csv` instead (see above).

---

Expand All @@ -72,7 +94,7 @@ These must exist under `$DATA_DIR` before running:

4. **Study data**
- Paths specified in `study_list.csv`; typically under `/local-scratch/data/`
- Must match the expected layout for the chosen `data_format` (opengwas, besd, tsv)
- Must match the expected layout for the chosen `data_format` (opengwas, besd, tsv, summary_stats)

---

Expand Down
6 changes: 6 additions & 0 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ rule extract_regions_from_studies:
command = f'Rscript extract_regions_from_rare_tsv.R \
--extracted_study_location {study.extracted_location} \
--extracted_output_file {output}'
elif study.data_format == 'summary_stats':
command = f'Rscript extract_regions_from_summary_stats.R \
--extracted_study_location {study.extracted_location} \
--extracted_output_file {output}'
else:
raise ValueError(f'Cant ingest unknown data format: {study.data_format}')

Expand Down Expand Up @@ -363,11 +367,13 @@ rule create_static_web_files:
threads: 1
params:
static_web_dir=STATIC_WEB_DIR,
results_dir=version_results_dir,
output: temporary(static_web_files_ready_file)
shell:
"""
Rscript create_static_web_files.R \
--studies_db_file {studies_db_file} \
--results_dir {params.results_dir} \
--static_web_dir {params.static_web_dir} \
--static_web_files_ready_file {output}
"""
Expand Down
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ restart-times: 0
max-jobs-per-second: 10
max-status-checks-per-second: 10
latency-wait: 5
cores: 150
cores: 200
keep-going: True
keep-incomplete: True
rerun-incomplete: True
Expand Down
5 changes: 4 additions & 1 deletion docker/requirements.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install.packages(c("remotes", "devtools"))

cran_install <- c(
"testthat", "susieR", "Rfast", "duckdb", "validate", "redux", "sendmailR", "igraph", "svglite", "readxl",
"janitor", "styler"
"janitor", "styler", "binom", "epitools", "scales", "ggrepel", "pheatmap", "UpSetR", "ieugwasr"
)
install.packages(cran_install)

Expand All @@ -19,6 +19,9 @@ remotes::install_version("lintr", version = "3.3.0.1")
remotes::install_version("RcppEigen", version = "0.3.3.9.3")
devtools::install_github("jrs95/hyprcoloc", upgrade = "never")
devtools::install_github("MRCIEU/gwasglue", upgrade = "never")
devtools::install_github("MRCIEU/gpmapr", upgrade = "never")
devtools::install_github("MRCIEU/TwoSampleMR", upgrade = "never")
devtools::install_github("WSpiller/MVMR", upgrade = "never")

biocmanager_install <- c("Homo.sapiens", "GenomicRanges", "biomaRt")
BiocManager::install(biocmanager_install)
74 changes: 25 additions & 49 deletions pipeline_steps/coloc_studies_in_ld_block.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ dplyr.summarise.inform <- FALSE

bp_range <- 50000

# Wallace 2020-style defaults: interpret priors at a reference locus size,
# then rescale to each harmonised locus so locus-level priors stay comparable.
wallace_reference_nsnps <- 1000
coloc_base_p1 <- 1e-4
coloc_base_p2 <- 1e-4
coloc_base_p12 <- 5e-6
coloc_p1 <- 1e-4
coloc_p2 <- 1e-4
coloc_p12 <- 5e-6

parser <- argparser::arg_parser("Colocalise studies per LD block")
parser <- argparser::add_argument(
Expand Down Expand Up @@ -230,25 +227,25 @@ run_coloc_for_study_pairs <- function(study_pairs, studies_to_colocalise, coloc_
if (is.null(result)) {
result <- data.frame(
unique_study_a = pair$unique_study_a,
study_a = NA,
study_a = NA_character_,
unique_study_b = pair$unique_study_b,
study_b = NA,
bp_distance = NA,
ignore = T,
false_positive = F,
false_negative = F,
nsnps = NA,
hit1 = NA,
hit2 = NA,
PP.H0.abf = NA,
PP.H1.abf = NA,
PP.H2.abf = NA,
PP.H3.abf = NA,
PP.H4.abf = NA,
idx1 = NA,
idx2 = NA,
h4 = NA,
ld_block = ld_block
study_b = NA_character_,
bp_distance = NA_real_,
ignore = TRUE,
nsnps = NA_real_,
hit1 = NA_character_,
hit2 = NA_character_,
PP.H0.abf = NA_real_,
PP.H1.abf = NA_real_,
PP.H2.abf = NA_real_,
PP.H3.abf = NA_real_,
PP.H4.abf = NA_real_,
idx1 = NA_real_,
idx2 = NA_real_,
h4 = NA_real_,
ld_block = ld_block,
false_positive = FALSE,
false_negative = FALSE
)
return(result)
}
Expand Down Expand Up @@ -343,21 +340,16 @@ pairwise_coloc_analysis <- function(first_gwas, second_gwas) {
return(NULL)
}

dynamic_priors <- calculate_dynamic_coloc_priors(nsnps)
result <- coloc::coloc.bf_bf(
bf1 = first_lbf,
bf2 = second_lbf,
p1 = coloc_base_p1,
p2 = coloc_base_p2,
p12 = coloc_base_p12
p1 = coloc_p1,
p2 = coloc_p2,
p12 = coloc_p12
)
result$summary$h4 <- result$summary$PP.H4.abf
result$summary$dynamic_p1 <- coloc_base_p1
result$summary$dynamic_p2 <- coloc_base_p2
result$summary$dynamic_p12 <- coloc_base_p12
result$summary$nsnps <- nsnps
coloc_results <- result$summary
return(coloc_results)
return(result$summary)
}

harmonise_gwases <- function(...) {
Expand Down Expand Up @@ -392,22 +384,6 @@ harmonise_gwases <- function(...) {
return(gwases)
}

calculate_dynamic_coloc_priors <- function(nsnps) {
nsnps <- max(1, as.numeric(nsnps))
target_h1 <- coloc_base_p1 * wallace_reference_nsnps
target_h2 <- coloc_base_p2 * wallace_reference_nsnps
target_h4 <- coloc_base_p12 * wallace_reference_nsnps

max_per_snp <- 0.99 / nsnps
p1 <- min(target_h1 / nsnps, max_per_snp)
p2 <- min(target_h2 / nsnps, max_per_snp)
p12 <- min(target_h4 / nsnps, p1, p2, 0.99 / nsnps)
p12 <- max(p12, .Machine$double.eps)

# return(list(p1 = p1, p2 = p2, p12 = p12))
return(list(p1 = coloc_base_p1, p2 = coloc_base_p2, p12 = coloc_base_p12))
}

resolve_worker_file_paths <- function(finemapped_studies) {
return(
finemapped_studies |>
Expand Down
79 changes: 63 additions & 16 deletions pipeline_steps/compile_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,58 +113,87 @@ aggregate_data_produced_by_pipeline <- function(
) {
file_names <- ld_block_file_basenames(args$block_list)

message("Aggregating extracted_studies")
extracted_studies_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$extracted_studies)
)
message(glue::glue("Reading {length(extracted_studies_files)} extracted_studies file(s)"))
extracted_studies <- lapply(extracted_studies_files, function(file) {
return(vroom::vroom(file, show_col_types = F))
}) |> dplyr::bind_rows()

message("Aggregating standardised_studies")
standardised_studies_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$standardised_studies)
)
message(glue::glue("Reading {length(standardised_studies_files)} standardised_studies file(s)"))
standardised_studies <- lapply(standardised_studies_files, function(file) {
return(vroom::vroom(file, show_col_types = F, col_types = standardised_column_types))
}) |> dplyr::bind_rows()

message("Aggregating imputed_studies")
imputed_studies_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$imputed_studies)
)
message(glue::glue("Reading {length(imputed_studies_files)} imputed_studies file(s)"))
imputed_studies <- lapply(imputed_studies_files, function(file) {
return(vroom::vroom(file, show_col_types = F, col_types = imputed_column_types))
}) |> dplyr::bind_rows()

message("Aggregating finemapped_studies")
finemapped_studies_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$finemapped_studies)
)
message(glue::glue("Reading {length(finemapped_studies_files)} finemapped_studies file(s)"))
finemapped_studies <- lapply(finemapped_studies_files, function(file) {
return(vroom::vroom(file, show_col_types = F, col_types = finemapped_column_types))
}) |> dplyr::bind_rows()

message("Aggregating coloc_pairwise")
pairwise_coloc_input_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$coloc_pairwise)
)
coloc_pairwise_results <- vroom::vroom(pairwise_coloc_input_files,
delim = "\t",
show_col_types = F,
col_select = c(
"unique_study_a", "unique_study_b", "PP.H0.abf", "PP.H1.abf",
"PP.H2.abf", "PP.H3.abf", "PP.H4.abf", "ld_block",
"false_positive", "false_negative", "ignore"
)
)
message("pairwise coloc results: ", nrow(coloc_pairwise_results))
message(glue::glue("Reading {length(pairwise_coloc_input_files)} coloc_pairwise file(s)"))
# Read/filter per LD block: multi-file vroom() is fragile at this scale, and the
# unfiltered pairwise table exceeds R's ~2.1B row limit. Keep weakly informative
# pairs only (H3 or H4 above a low floor); full pairs remain in per-block files.
coloc_pairwise_compile_pp_threshold <- 0.2
coloc_pairwise_results <- parallel::mclapply(
pairwise_coloc_input_files,
mc.cores = 1,
function(file) {
data.table::setDTthreads(1)
pairs <- data.table::fread(file, showProgress = FALSE)
pairs <- pairs[
PP.H4.abf > coloc_pairwise_compile_pp_threshold |
PP.H3.abf > coloc_pairwise_compile_pp_threshold
]
return(pairs)
}
) |>
data.table::rbindlist(use.names = TRUE, fill = TRUE) |>
tibble::as_tibble()
message(glue::glue(
"pairwise coloc results: {nrow(coloc_pairwise_results)} ",
"(kept PP.H3 or PP.H4 > {coloc_pairwise_compile_pp_threshold} per LD block)"
))

message("Aggregating coloc_clustered")
coloc_clustered_input_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$clustered)
)
coloc_clustered_results <- vroom::vroom(coloc_clustered_input_files, delim = "\t", show_col_types = F)
coloc_clustered_results <- vroom_multi(
coloc_clustered_input_files,
"coloc_clustered",
delim = "\t",
show_col_types = F
)
message("clustered coloc results: ", nrow(coloc_clustered_results))

coloc_clustered_results <- coloc_clustered_results |>
Expand All @@ -173,17 +202,28 @@ aggregate_data_produced_by_pipeline <- function(
dplyr::ungroup() |>
dplyr::arrange(coloc_group_id)

message("Aggregating compare_rare_results")
compare_rare_input_files <- Filter(
function(file) file.exists(file),
file.path(ld_info$ld_block_data, file_names$compare_rare_results)
)
if (length(compare_rare_input_files) == 0) {
message("Reading 0 compare_rare_results file(s)")
compare_rare_results <- data.frame()
} else {
compare_rare_results <- vroom::vroom(compare_rare_input_files, delim = "\t", show_col_types = F)
compare_rare_results <- vroom_multi(
compare_rare_input_files,
"compare_rare_results",
delim = "\t",
show_col_types = F
)
}

studies_to_process <- vroom::vroom(studies_to_process_file, show_col_types = F)
studies_to_process <- vroom::vroom(
studies_to_process_file,
show_col_types = F,
col_types = studies_processed_column_types
)

studies_with_extractions <- character(0)
if (nrow(extracted_studies) > 0) {
Expand All @@ -197,10 +237,17 @@ aggregate_data_produced_by_pipeline <- function(
))

if (file.exists(studies_processed_file)) {
studies_processed <- vroom::vroom(studies_processed_file, show_col_types = F)
studies_processed <- dplyr::bind_rows(studies_processed, studies_to_add) |> dplyr::distinct()
studies_processed <- vroom::vroom(
studies_processed_file,
show_col_types = F,
col_types = studies_processed_column_types
)
# Newer metadata is appended last; keep the last row per study_name so
# non-identical historical duplicates do not accumulate.
studies_processed <- dplyr::bind_rows(studies_processed, studies_to_add)
studies_processed <- studies_processed[!duplicated(studies_processed$study_name, fromLast = TRUE), ]
} else {
studies_processed <- studies_to_add
studies_processed <- studies_to_add[!duplicated(studies_to_add$study_name, fromLast = TRUE), ]
}

if (file.exists(traits_processed_file)) {
Expand Down
Loading
Loading