Skip to content

Refactor constraint pipeline to per-base architecture with v4.1.1 support - #51

Open
jkgoodrich wants to merge 40 commits into
mainfrom
jg/switch_constraint_to_per_base
Open

Refactor constraint pipeline to per-base architecture with v4.1.1 support#51
jkgoodrich wants to merge 40 commits into
mainfrom
jg/switch_constraint_to_per_base

Conversation

@jkgoodrich

@jkgoodrich jkgoodrich commented Mar 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

Major refactor of the gnomAD constraint pipeline from a grouped/aggregated approach to a per-base (per-variant) architecture. Adds version 4.1.1 support, introduces new pipeline steps, extracts constants into a dedicated module, and adds release preparation for the mutation rate table.

Architecture changes

  • Per-variant expected counts: The old single apply_models() step is replaced by a three-step per-variant path:
    1. --apply-models-per-variant — compute expected counts per variant
    2. --aggregate-per-variant-expected — sum per-variant counts by transcript/consequence
    3. --aggregate-by-constraint-groups — group into constraint categories (lof, mis, syn)
  • Aggregated alternative: --apply-models-aggregated chains all three steps without writing per-variant intermediates, for faster iteration
  • --use-aggregated-expected flag allows --aggregate-by-constraint-groups to read from the aggregated path output instead of per-variant

New pipeline steps

Step Description
--create-training-set Count observed + possible variants at synonymous sites for model training
--apply-models-per-variant Per-variant model application (plateau + coverage correction)
--aggregate-per-variant-expected Aggregate per-variant expected counts by transcript/consequence
--aggregate-by-constraint-groups Group aggregated counts into constraint groups
--apply-models-aggregated Single-step alternative to the above three steps
--compute-gene-quality-metrics Per-transcript quality metrics (coverage, MQ, segdup, LCR overlap)
--prepare-release-mutation-rate Prepare mutation rate table for public release (HT + TSV)
--export-release-downsampling-tsv Export downsampling constraint metrics to TSV

constraint.py utility refactor

  • Replaced functions: add_vep_context_annotations -> prepare_context_ht; annotate_context_ht -> preprocess_data; create_observed_and_possible_ht -> create_training_set; apply_models (local) -> uses gnomad.utils.constraint.apply_models + new create_per_variant_expected_ht
  • New functions: create_per_variant_expected_ht, aggregate_per_variant_expected_ht, create_aggregated_expected_ht, aggregate_by_constraint_groups, compute_gene_quality_metrics, prepare_release_mutation_ht, prepare_release_ht, plus helpers
  • Updated gnomad_methods imports: Uses new APIs from gnomad_methods (assemble_constraint_context_ht, build_constraint_consequence_groups, aggregate_constraint_metrics_expr, calibration_model_group_expr, variant_observed_and_possible_expr, count_observed_and_possible_by_group, compute_percentile_thresholds, rank_array_element_metrics, etc.)
  • Terminology update: "population" -> "genetic ancestry group" throughout

resource_utils.py refactor

  • Moved constants to new constants.py module (VERSIONS, DATA_TYPES, MODEL_TYPES, POPS, COVERAGE_CUTOFF, MU_GROUPING, etc.)
  • New resource getters: get_per_variant_apply_ht, get_aggregated_expected_ht, get_constraint_group_ht, get_gene_quality_metrics_ht, get_release_mutation_ht, get_release_mutation_tsv_path, get_loeuf_thresholds_path
  • New helpers: filter_for_test, get_adj_r_ht, get_syn_adj_r_ht, get_all_sites_an
  • Added SITES_VERSION_MAP to handle v4.1.1 using v4.1 sites release
  • Added PCSK9 regions to keep_regions for test filtering
  • PipelineResourceCollection and PipelineStepResourceCollection imports moved from constraint_pipeline.py to resource_utils.py

constants.py

Centralizes all pipeline constants including:

  • Pipeline config: VERSIONS, CURRENT_VERSION (4.1.1), SITES_VERSION_MAP, DATA_TYPES, MODEL_TYPES, GENOMIC_REGIONS
  • Grouping keys: MU_GROUPING, CALIBRATION_GROUPING, AGGREGATE_SUM_FIELDS
  • Release formatting: RELEASE_KEY_ORDER, RELEASE_CG_RENAME, RELEASE_CG_SELECT, RELEASE_PIPELINE_PARAM_GLOBALS, RELEASE_GROUP_NAMES, RELEASE_LOF_FIELDS, CONSTRAINT_GRANULARITIES, PLI_EXPECTED_VALUES
  • Annotation constants: CLASSIC_LOF_ANNOTATIONS, SFS_BIN_CUTOFFS, ADJ_FREQ_META

Other changes

  • Version bump: CURRENT_VERSION 4.1 -> 4.1.1, added to VERSIONS list
  • Documentation: New CLAUDE.md with code style, gotchas, and dataproc submission instructions; expanded README.md with full pipeline step table, resource paths, and constraint_metrics schema
  • .gitignore: Added .claude/ and CLAUDE.local.md

…les. Introduce gene quality metrics computation in the constraint pipeline and refactor resource utility constants.
…ic loss-of-function annotations and streamline resource collection for the constraint pipeline. Remove deprecated code and improve clarity in resource utility functions.
…unused `print_global_struct` function and replace `aggregate_expected_variants_expr` with `aggregate_constraint_metrics_expr` in the aggregation process. Introduce new utility functions for better organization and clarity.
…tion. Replace deprecated functions with updated counterparts, streamline percentile threshold calculations, and enhance the handling of observed and possible variants in the constraint pipeline.
…ring. Introduce new utility functions for filtering test data and reading adjacency data, while updating the context preparation process to streamline annotations for coverage, allele number, and site frequency spectrum. Remove deprecated functions and improve overall code organization.
…get_syn_adj_r_ht` function for reading synonymous DNM adjacency data and update context preparation to include synonymous adjacencies in annotations. Enhance overall clarity and organization of the constraint pipeline.
…iltering. Replace population references with genetic ancestry group terminology and improve documentation for downsampling logic. Streamline annotations for observed and possible variants in the constraint pipeline.
…supported versions list. Adjust current version constant accordingly.
…on. Add functionality to compute gene quality metrics and streamline aggregation processes. Implement new command-line arguments for applying models on aggregated data and using aggregated expected outputs. Refactor utility functions for clarity and organization.
…ments to the README and CLAUDE.md files. Revise project overview, structure, and pipeline steps to reflect the current version 4.1.1. Add details on new command-line arguments and resource paths, while improving clarity and organization throughout the documentation.
…. Update keep_regions for both GRCh37 and alternative chromosome formats to include specific genomic intervals for PCSK9.
…filtering. Exclude non-Python files from zip builds to reduce size. Introduce a mapping for sites versioning and refactor gene quality metrics computation to utilize updated data structures. Improve clarity in utility functions and adjust documentation accordingly.
…to include segmental duplication and low-complexity region metrics. Update related utility functions for improved clarity and efficiency. Enhance resource handling by integrating new genomic interval data for more accurate quality assessments.
…eline. Add utility function to retrieve thresholds path and update resource handling to include new TSV export functionality. Refactor related code for clarity and consistency.
…se in the constraint pipeline. Introduce new command-line argument for mutation rate preparation, update resource handling, and implement utility function for mutation rate Table preparation. Enhance logging for export processes.
@jkgoodrich jkgoodrich self-assigned this Mar 24, 2026
@jkgoodrich jkgoodrich added enhancement New feature or request Constraint labels Mar 24, 2026
ch-kr added a commit to broadinstitute/regional_missense_constraint that referenced this pull request Aug 13, 2026
`create_constraint_prep_ht` read the per-variant expected dataset at
gnomad-constraint's `CURRENT_VERSION`, which moved from 4.1 to 4.1.1 in the
per-base refactor (broadinstitute/gnomad-constraint#51). The coverage corrected
dataset only exists for 4.1, so the read resolved to a nonexistent path and
`prep-constraint` failed immediately:

    HailException: No file or directory found at gs://gnomad/v4.1.1/
    constraint_coverage_corrected/apply_models/transcript_consequences/
    gnomad.v4.1.1.per_variant_expected.coverage_corrected.ht

The version is now pinned in `CONSTRAINT_VERSION` and passed explicitly, so the
read stops tracking a default that moves underneath it. This affects any freeze,
not just freeze 3.

Assisted-by: ClaudeCode:claude-opus-5
Bins are computed as hl.int(rank * multiplier / n_rows) over 0-based
ranks, so they run from 0 to multiplier - 1. The README documented them
as 1-based.

Assisted-by: ClaudeCode:claude-opus-5[1m]
Rank and percentile bin annotations are no longer added inside
compute_constraint_metrics. They are applied by the now-public
compute_constraint_percentile_bins, which runs as a second phase over an
intermediate constraint_metrics_pre_rank Table. The ranking moved from
third to last in the orchestration; nothing downstream of it reads the
rank or bin fields, so the annotations are additive and order-independent.

This allows the ranking to be recomputed without rerunning the metrics,
needed to reissue v4.1.1 as v4.1.2 with corrected bins
(broadinstitute/gnomad_production#2202). --skip-pre-rank-metrics reuses
the existing pre-rank Table and recomputes only the ranking, and
--use-mane-select-over-canonical exposes the transcript filter that the
ranking phase now takes directly.

With --skip-pre-rank-metrics the step declares the pre-rank Table as an
input and drops its upstream pipeline inputs, since the aggregated
constraint group and gene quality metrics Tables are not read in that
mode and need not exist for the target version. Without this the input
existence check fails on a missing constraint_group.ht, which --overwrite
does not suppress.

Register 4.1.2 in VERSIONS and SITES_VERSION_MAP. CURRENT_VERSION is left
at 4.1.1, so runs pass --version 4.1.2 explicitly.

Assisted-by: ClaudeCode:claude-opus-5[1m]
@jkgoodrich
jkgoodrich force-pushed the jg/switch_constraint_to_per_base branch from 02f9658 to 0470d69 Compare August 18, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Constraint enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants