Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Fine-mapping result post-processing

Turns per-region fine-mapping objects into flat TSV tables, diagnostic plots, VCFs and QTL-GWAS overlap summaries.

Overview

Fine-mapping leaves you with per-region R objects, which are the wrong shape for almost everything that comes next - inspecting results, sharing them, or intersecting them with a GWAS. This module reads those objects and re-expresses them as text: susie_to_tsv writes a per-variant table (PIP, credible-set membership, posterior mean and sd), a wide per-variant by per-effect log-Bayes-factor table, and a per-credible-set effect table. The remaining workflows build on those tables - collapsing them across regions, drawing PIP landscape and UpSet plots, annotating variants, exporting VCFs, and overlapping QTL credible sets with GWAS signals.

The export step consumes a FineMappingResult object rather than a raw susieR fit, so the three tables are views onto that object: variant.tsv is its topLoci view, lbf.tsv its lbf view and effect.tsv its cs_summary view. Because the object carries its own region and trait, --region-list is accepted for CLI compatibility but is no longer used.

When to run it. After fine-mapping, on the outputs of susie_twas or fsusie, and before any downstream reporting or GWAS integration. Most of the workflows beyond susie_to_tsv additionally need GWAS summary statistics, plot-recipe files or a singularity container.

Input

  • --rds_path tests/fixtures/mnm_postprocessing/protocol_example.ENSG00000283047.fine_mapping.rds (one or more FineMappingResult RDS files written by mnm_regression; each carries its own region and trait, and the step runs once per file)

QtlFineMappingResult: S4 DataFrame, 2 rows x 7 columns
$ study: character [1:2] test_study test_study
$ context: character [1:2] context1 context2
$ trait: character [1:2] ENSG00000283047 ENSG00000283047
$ method: character [1:2] susie susie
$ entry: S4 object  <SimpleList>
 ..$ listData: List of 2
 .. ..$ : S4 object  <FineMappingEntry>
 .. .. ..$ variantIds: ...
 .. .. ..$ susieFit: ...
 .. .. ..$ topLoci: ...
 .. .. ..$ cvResult: ...
 .. ..$ : S4 object  <FineMappingEntry>
 .. .. ..$ variantIds: ...
 .. .. ..$ susieFit: ...
 .. .. ..$ topLoci: ...
 .. .. ..$ cvResult: ...
 ..$ elementType: character [1:1] ANY
 ..$ elementMetadata: name [1:1] NULL
 ..$ metadata: List of 0
... (truncated)
  • --study toy (tag used as name in output filenames and in intermediate RDS names. Required.)

  • --cwd output_postproc (working directory all outputs are written under. Defaults to output.)

  • --container (an empty string runs the step natively in the project environment instead of through a singularity image)

  • --region-list (accepted for CLI compatibility and ignored. The FineMappingResult carries its own region and trait, so no external region list is needed.)

The same directory also holds protocol_example.fsusie.fine_mapping.rds and protocol_example.mvsusie.fine_mapping.rds, for the fsusie_to_tsv and mv_susie_2 workflows.

The export_top_loci workflow consolidates results across regions and takes a different set of inputs, none of which the example data ships:

  • --file_path (directory holding the per-region fine-mapping RDS files)

  • --prefix, --suffix (the filename parts before and after the region id, so each region resolves to <file_path>/<prefix>.<region>.<suffix> - for example ROSMAP_AC_eQTL.ENSG00000012779.univariate_bvsr.rds)

  • --qtl_type cis (one of cis, trans or trans_<program>; appears in the combined output filename)

  • --region_file (BED file listing the regions to consolidate)

The remaining workflows take these additional flags:

  • --tsv_path output_postproc/*.lbf.tsv (per-region LBF tables to concatenate; susie_tsv_collapse)

  • --annot_tibble <annotation_tibble.tsv> (annotation tibble used to label variants; susie_pip_landscape_plot and fsusie_extract_effect)

  • --trait_to_select 1 (how many traits the credible-set UpSet diagram shows; susie_upsetR_cs_plot)

  • --SNP_list <variant_list.txt> (variants to annotate; tmp_annotatation_of_snps)

  • --annotation_rds <annotation.rds> (annotation reference the variants are looked up against; tmp_annotatation_of_snps)

  • --geno_ref <genotype_reference.bim.gz> (genotype reference used to resolve variant identifiers; export_top_loci)

  • --min_corr 0.8 (minimum correlation for a variant to join a top-loci block; export_top_loci)

  • --condition_meta <gwas_meta.tsv> (GWAS meta file naming the conditions to overlap; overlap_qtl_gwas)

Output

Per-region TSV export (susie_to_tsv, sQTL_susie_to_tsv, fsusie_to_tsv), one set per input SuSiE object:

  • <region>.variant.tsv -- one row per variant: PIP, credible-set order and id, posterior mean and sd.

  • <region>.lbf.tsv -- per-variant log Bayes factors for each single-effect.

  • <region>.effect.tsv -- per-effect summary (susie_to_tsv and sQTL_susie_to_tsv only).

  • <name>.all_variants.tsv -- variants pooled across regions (fsusie_to_tsv only).

Collapsed tables (susie_tsv_collapse):

  • <prefix>.chr<chromosome>.unisusie_rss.lbf.tsv -- per-region lbf tables concatenated by chromosome.

Diagnostic plots:

  • <region>.pip_landscape_plot.pdf (susie_pip_landscape_plot)

  • <name>.UpSetR.pdf and <name>.UpSetR_cs.pdf (susie_upsetR_plot, susie_upsetR_cs_plot)

Variant annotation (tmp_annotatation_of_snps):

  • <name>.annotated.rds and <name>.annotated_rev.rds

fSuSiE effect extraction:

  • <region>.estimated_effect.tsv (fsusie_extract_effect)

  • <region>.affected_region.tsv (fsusie_affected_region)

VCF export (mv_susie_2):

  • <region>.<context>.vcf.bgz

cis and GWAS consolidation:

  • <name>_cache/<name>_<meta_info>.tsv, collapsed into <name>.cis_results_db.tsv (or <name>.block_results_db.tsv for GWAS input) -- cis_results_export

  • <name>.union_export.tsv.gz -- gwas_results_export

  • summary/<region>.top_loci.bed.gz, collapsed into summary/<name>.<qtl_type>.top_loci.bed.gz -- export_top_loci

QTL and GWAS overlap (overlap_qtl_gwas):

  • gwas_qtl/cache/<name>_gwas_batch_meta_<meta_info>.tsv

  • <name>.overlapped.gwas.tsv

No example output ships with the repository: output_postproc/ is empty and the pipeline cannot be re-run here, so no structure dumps are shown. The filenames above come from each step output: statement.

Minimal Working Example

Export fine-mapping results to per-variant, LBF and effect tables

susie_to_tsv reads each FineMappingResult object and writes its three text views.

Timing: TBD (on toy dataset)

fSuSiE and sQTL exports

fsusie_to_tsv and sQTL_susie_to_tsv perform the same export for fSuSiE and sQTL objects.

Timing: TBD (on toy dataset)

Collapse per-region tables

susie_tsv_collapse concatenates the per-region LBF tables into one table per chromosome.

Timing: TBD (on toy dataset)

Diagnostic plots

susie_pip_landscape_plot draws the PIP landscape; susie_upsetR_plot and susie_upsetR_cs_plot draw UpSet diagrams of credible-set overlap. The landscape plot needs an annotation tibble; --trait_to_select picks how many traits the credible-set diagram shows.

Timing: TBD (on toy dataset)

Variant annotation

tmp_annotatation_of_snps annotates a variant list against a reference RDS, then writes the reverse mapping.

Timing: TBD (on toy dataset)

fSuSiE effect extraction

fsusie_extract_effect writes the estimated effect curve per region; fsusie_affected_region writes the intervals a signal spans.

Timing: TBD (on toy dataset)

VCF export

mv_susie_2 writes multivariate results as bgzipped VCF, one file per region and context.

Timing: TBD (on toy dataset)

cis and GWAS consolidation

cis_results_export collects per-region results into a results database, gwas_results_export does the same for GWAS blocks, combine_export_meta merges the per-chunk caches, and export_top_loci writes the combined top-loci BED. All four resolve each region as <file_path>/<prefix>.<region>.<suffix> and need a GWAS meta file.

Timing: TBD (on toy dataset)

QTL and GWAS overlap

overlap_qtl_gwas intersects QTL credible sets with GWAS results, caching a batch meta file per chunk before writing the overlap table.

Timing: TBD (on toy dataset)

The meta file consumed by the consolidation workflows is produced by:

get_condition <- function(conditions, Author, qtl_type){
    strings <- c()
    for(condition in conditions){
        string =  paste(condition, Author, qtl_type, sep = "_")  
        string = paste(unique(unlist(strsplit(string, "_"))), collapse = "_")
        strings <- c(strings, string)
    }
    return(strings)
}

raw_name<- c("Mic","Ast","Oli","OPC","Exc","Inh","DLPFC","PCC","AC")
raw_name_kellis<- c("Mic_Kellis","Ast_Kellis","Oli_Kellis","OPC_Kellis","Exc_Kellis","Inh_Kellis","Ast.10","Mic.12","Mic.13")

dejager_name <- get_condition(raw_name, "De_Jager","eQTL")
kellis_name <- get_condition(raw_name_kellis, "Kellis","eQTL")
eQTL_meta <- data.frame(raw_name = c(raw_name, raw_name_kellis), new_name = c(dejager_name, kellis_name))
write_delim(eQTL_meta, "output/mnm_postprocessing/eQTL_meta.tsv")

Command Interface

usage: sos run pipeline/mnm_postprocessing.ipynb
               [workflow_name | -t targets] [options] [workflow_options]
  workflow_name:        Single or combined workflows defined in this script
  targets:              One or more targets to generate
  options:              Single-hyphen sos parameters (see "sos run -h" for details)
  workflow_options:     Double-hyphen workflow-specific parameters

Workflows:
  susie_to_tsv
  sQTL_susie_to_tsv
  fsusie_to_tsv
  susie_tsv_collapse
  susie_pip_landscape_plot
  susie_upsetR_plot
  susie_upsetR_cs_plot
  tmp_annotatation_of_snps
  fsusie_extract_effect
  fsusie_affected_region
  mv_susie
  cis_results_export
  gwas_results_export
  export_top_loci
  combine_export_meta
  overlap_qtl_gwas

Global Workflow Options:
  --cwd output (as path)
                        A region list file documenting the chr_pos_ref_alt of a
                        susie_object
  --study VAL (as str, required)
  --container ''
                        Path to work directory where output locates Containers
                        that contains the necessary packages
  --modular-script-dir code/script (as path)
  --entrypoint  ('micromamba run -a "" -n' + ' ' + re.sub(r'(_apptainer:latest|_docker:latest|\.sif)$', '', container.split('/')[-1])) if container else ""

  --job-size 1 (as int)
                        For cluster jobs, number commands to run per job
  --walltime 96h
                        Wall clock time expected
  --mem 6G
                        Memory expected
  --numThreads 2 (as int)
                        Number of threads
  --windows 1000000 (as int)

Sections
  susie_to_tsv_1:
    Workflow Options:
      --region-list . (as path)
                        Migrated: consumes a FineMappingResult RDS
                        (fine_mapping.R output) instead of a raw susieR object.
                        The three legacy outputs map to fine_mapping_export.R
                        views: variant.tsv = topLoci   (per-variant: pip,
                        posterior mean/sd, logBF, cs, region) lbf.tsv     = lbf
                        (wide per-variant x per-effect log Bayes factor matrix)
                        effect.tsv  = cs_summary (per credible set: size,
                        purity, V, logBF, lead variant) The FineMappingResult
                        carries its own region/trait, so `region_list` is
                        accepted for CLI compatibility but is no longer used.
      --rds-path  paths

  sQTL_susie_to_tsv_1:
    Workflow Options:
      --region-list . (as path)
                        Migrated: identical to susie_to_tsv but preserves the
                        sQTL / leafcutter2 filename sanitization ("*" -> "N";
                        ":"/"+" scrubbed from the task tag), since intron ids
                        contain shell-hostile characters. Consumes a
                        FineMappingResult RDS and writes the topLoci / lbf /
                        cs_summary views.
      --rds-path  paths

  fsusie_to_tsv_1:
    Workflow Options:
      --region-list . (as path)
                        Migrated: consumes an fSuSiE FineMappingResult RDS.
                        variant.tsv = topLoci (per-variant: pip, cs,
                        cs_95_purity) lbf.tsv     = lbf     (fSuSiE per-variant
                        x per-effect lBF matrix) (Fixes the legacy bug where
                        lbf.tsv was written the variant table, not the lbf
                        matrix.) is_dummy = cs_95_purity < 0.5; the functional
                        effect curves + peak positions are exported by
                        fsusie_extract_effect / fsusie_affected_region
                        (credible_band / affected_regions views), so they are
                        not duplicated here.
      --rds-path  paths

  *_to_tsv_2:
    Workflow Options:
      --name  f'{_input[0]:b}'.split(".")[0]

                        Reduce: concatenate the per-region variant tables (the
                        *variant.tsv outputs of the parallel _1 map step) into
                        one file, in R via data.table::rbindlist (fill=TRUE
                        aligns columns). _input carries all of _1's outputs, so
                        keep only the variant tables (not the lbf / effect
                        ones).
  susie_tsv_collapse:
    Workflow Options:
      --tsv-path  paths

                        Concatenate the per-region lbf tables (named
                        *.chr<N>_<start>_<end>...lbf.tsv) by chromosome.
                        Chromosome split is plain python (no pandas); the concat
                        is R data.table::rbindlist (headers handled by fread;
                        fill=TRUE aligns columns).
  susie_pip_landscape_plot:
    Workflow Options:
      --rds-path  paths

                        Per-region PIP landscape from a FineMappingResult
                        (fine_mapping output). Modernized from the legacy multi-
                        TSV plot: pecotmr does no plotting, so the ggplot lives
                        in fine_mapping_pip_plot.R. (The regulatory/gene
                        annotation overlay is a deferred enhancement;
                        annot_tibble is kept for CLI compat.)
  susie_upsetR_plot:
    Workflow Options:
      --rds-path  paths

                        UpSet plot of credible-set variant overlap across the
                        FineMappingResult(s)' (study, context, trait, method)
                        tuples. pecotmr does no plotting; the UpSet lives in
                        fine_mapping_upset.R.
  susie_upsetR_cs_plot:
    Workflow Options:
      --rds-path  paths

                        Credible-set overlap UpSet plot across the
                        FineMappingResult(s), via fine_mapping_upset.R (pecotmr
                        does no plotting). trait_to_select is retained for CLI
                        compatibility; the single-trait "CS shared with other
                        phenotypes" sub-view is a deferred wrapper option.
      --trait-to-select 1 (as int)
  tmp_annotatation_of_snps_1:
    Workflow Options:
      --SNP-list VAL (as path, required)
      --annotation-rds VAL (as path, required)
  tmp_annotatation_of_snps_2:
    Workflow Options:
      --SNP-list VAL (as path, required)
      --annotation-rds VAL (as path, required)
  fsusie_extract_effect:
    Workflow Options:
      --rds-path  paths

                        Fitted effect curve + credible band per fSuSiE credible
                        set, via fsusieCredibleBand (fine_mapping_export.R
                        `credible_band` view). pecotmr does the wavelet credible
                        band; it needs an UNtrimmed fSuSiE fit (fine_mapping
                        trim=FALSE). The effect-curve PDF is a deferred plot
                        wrapper (pecotmr does no plotting); annot_tibble is kept
                        for CLI compatibility.
  fsusie_affected_region:
    Workflow Options:
      --rds-path  paths

                        GRanges of intervals where the fSuSiE credible band
                        excludes 0 (with a direction mcol), via
                        fsusieAffectedRegions (fine_mapping_export.R
                        `affected_regions` view). Needs an UNtrimmed fSuSiE fit
                        (fine_mapping trim=FALSE). The region PDF is a deferred
                        plot wrapper.
  mv_susie_2:
    Workflow Options:
      --rds-path  paths

                        Write per-context fine-mapping VCFs (ES / SE / LP / AF +
                        PIP / CS / within_cs_pip in the sample column) from a
                        FineMappingResult via fine_mapping_vcf.R ->
                        pecotmr::writeSumstatsVcf, replacing the inline
                        create_vcf. writeSumstatsVcf splits a multi-context
                        collection into one .vcf.bgz per context; the runtime
                        set is tracked with output: dynamic() (so {cwd} must be
                        created up front, since a dynamic output -- unlike a
                        concrete target -- does not pre-create its dir).
  cis_results_export_1, gwas_results_export_1, export_top_loci_1: Exporting cis
                        susie_twas results
    Workflow Options:
      --per-chunk 200 (as int)
                        per chunk we process at most 200 datasets
      --region-file . (as path)
                        Region list should have last column being region name.
      --file-path ''
                        the path stored original output files
      --prefix  (as list)
                        assuming orinal files are named as prefix.id.suffix
                        prefix of the original files (before id) to identify
                        file.
      --suffix VAL (as str, required)
                        suffix of the original files (after id) to identify
                        file.
      --condition-meta . (as path)
                        if need to rename context, please load condition meta
                        file
      --pip-thres 0.05 (as float)
                        if the pip all variants in a cs < this threshold, then
                        remove this cs
      --cs-size 3 (as int)
                        only keep top cs_size variants in one cs
      --exported-file . (as path)
                        provide exported meta to filter the exported genes
      --region-list . (as path)
                        Optional: if a region list is provide the analysis will
                        be focused on provided region. The LAST column of this
                        list will contain the ID of regions to focus on
      --region-name  (as list)
                        Optional: if a region name is provided the analysis
                        would be focused on the union of provides region list
                        and region names
      --geno-ref . (as path)
                        the (aligned) geno bim file to check allele flipping.
                        Retained for CLI compatibility; allele alignment is now
                        done upstream by fine_mapping.R (matchVariants), so it
                        is unused here.
      --context-meta . (as path)
                        context meta file to map renamed context backs
      --min-corr  (as list)
                        CS purity (min.abs.corr) cutoff(s); the first value is
                        used for conditions_top_loci.
      --[no-]gwas (default to False)
                        set this parameter as `True` when exporting gwas data.
      --[no-]fsusie (default to False)
                        set this parameter as `True` when exporting fsusie data.
      --[no-]metaQTL (default to False)
                        set this parameter as `True` when exporting metaQTL
                        data.
      --[no-]mnm (default to False)
                        set this parameter as `True` when exporting mnm data.
  cis_results_export_2, gwas_results_export_2:
    Workflow Options:
      --[no-]step1-only (default to False)
                        set it as True if you only want run first step (running
                        parallel) and combine them manually after all finished
      --exported-file . (as path)
                        provide exported meta to filter the exported genes
      --[no-]gwas (default to False)
                        optional: qtl or gwas, there is slightly different in
                        qtl and gwas rds file
  cis_results_export_3:
    Workflow Options:
      --[no-]step1-only (default to False)
                        set it as True if you only want run first step (running
                        parallel) and combine them manually after all finished
  gwas_results_export_3: get union of step1 1200 blocks costed ~5mins with in
                        one for loop
    Workflow Options:
      --[no-]step1-only (default to False)
  combine_export_meta:  simply combine seperate meta files, not works for having
                        an exsiting exported file for now.
    Workflow Options:
      --cache-path VAL (as path, required)
      --output-file VAL (as str, required)
      --[no-]remove-cache (default to False)
  export_top_loci_2:
    Workflow Options:
      --export-path  cwd

                        Export the per-region top-loci BED from each
                        cis_results_db (an FMR in the modern pipeline) via the
                        pecotmr-backed fine_mapping_top_loci_bed.R wrapper.
                        getTopLoci does the PIP/CS + independent purity
                        filtering; lfsr / conditional_effect are per-(variant,
                        context) numeric columns, so the legacy filter_lfsr
                        semicolon split/rejoin is gone. `export_suffix`
                        deliberately differs from step 1's `--suffix` (input
                        rds) to avoid CLI collision.
      --export-prefix ''
      --export-suffix 'cis_results_db.rds'
      --fsusie-prefix ''
      --[no-]preset-top-loci (default to False)
      --signal-cutoff 0.025 (as float)
                        PIP cutoff for getTopLoci; optional independent CS
                        purity (min.abs.corr) cutoff (<0 => none).
      --min-purity -1.0 (as float)
      --lfsr-thres 0.01 (as float)
                        Retained for CLI compatibility; lfsr is now a per-
                        variant column (no split/rejoin).
  export_top_loci_3:
    Workflow Options:
      --qtl-type VAL (as str, required)
                        Combine per-region .top_loci.bed.gz files into one
                        study-level file. Public-facing name:
                        {name}.{qtl_type}[.{variant_tag}].top_loci.bed.gz
      --variant-tag ''
      --combine auto
                        'auto' (default): skip step 3 if region_name or
                        region_list is set; 'yes': force run; 'no': force skip.
      --region-list . (as path)
                        Re-declared so this step can detect step-1 subset
                        filters.
      --region-name  (as list)
  overlap_qtl_gwas_1:
    Workflow Options:
      --per-chunk 100 (as int)
      --gwas-meta-path . (as path)
      --qtl-meta-path . (as path)
      --gwas-file-path ''
      --qtl-file-path ''
      --region-list . (as path)
                        Optional: focus on a region list (last column = region
                        id) and/or region names.
      --region-name  (as list)
      --signal-cutoff 0.0 (as float)
                        PIP cutoff for the top-loci overlap. 0 = intersect all
                        top-loci variants (the legacy behaviour, which had its
                        CS filter commented out); the coarse block pairing that
                        feeds coloc wants breadth, not just the strongest
                        signal.
  overlap_qtl_gwas_2:
    Workflow Options:
      --[no-]step1-only (default to False)
                        set it as True if you only want run first step (running
                        parallel) and combine them manually after all finished

Workflow implementation

The global parameters and the workflow step definitions.