1000 Genomes Full Pipeline Example¶
This example demonstrates Stage 3 of the GRiD workflow: dynamically generating config.yaml and running the complete GRiD pipeline on the 1000 Genomes Project dataset for the LPA KIV-2 locus (GRCh38).
The full workflow comprises three sequential modular scripts:
Data Preparation Example: Downloads inputs, streams CRAM slices, and writes locus coordinates.
IBS/IBD Neighbor Computation Example: (Optional) Computes IBS neighbors for haploid copy number estimation.
GRiD Execution: Generates configuration and runs GRiD steps (indexing, read counting, depth normalization, and genotype calling).
Stage 3 Execution Script¶
03-run-grid.sh checks for the presence of data/ibs_neighbors_chr6.tsv.gz from Stage 2. If present, haploid estimation (compute_haploid_genotypes.run) is set to True. Otherwise, it falls back to diploid-only mode.
1#!/bin/bash
2#SBATCH --job-name=grid_run
3#SBATCH --output=slurm/%x.out
4#SBATCH --error=slurm/%x.err
5#SBATCH --time=4-00:00:00
6#SBATCH --cpus-per-task=8
7#SBATCH --mem=200G
8
9# Stage 3 of 3 — generate config.yaml and run the GRiD pipeline.
10# Submit after 01_prep_data.sh (and, if you want haploid CN, 02_run_ibs.sh)
11# have both completed.
12#
13# Usage: sbatch 03_generate_config_and_run.sh (from the same working
14# directory as stages 1 and 2)
15
16set -euo pipefail
17
18module load anaconda mosdepth samtools
19
20WORK_DIR="$(pwd)"
21CRAM_DIR="$WORK_DIR/crams"
22LOG_DIR="$WORK_DIR/logs"
23DATA_DIR="$WORK_DIR/data"
24OUTPUT_DIR="$WORK_DIR/output"
25MOSDEPTH_WORK="$WORK_DIR/mosdepth_work"
26mkdir -p "$OUTPUT_DIR" "$MOSDEPTH_WORK" "$LOG_DIR" "$WORK_DIR/slurm"
27
28THREADS="${SLURM_CPUS_PER_TASK:-$(nproc)}"
29
30timestamp() { date '+%Y-%m-%d %H:%M:%S'; }
31log() { echo "[$(timestamp)] $*" | tee -a "$LOG_DIR/pipeline.log"; }
32
33# --- Load locus coordinates written by 01_prep_data.sh ---
34VARS_FILE="$DATA_DIR/pipeline_vars.sh"
35if [[ ! -f "$VARS_FILE" ]]; then
36 echo "ERROR: $VARS_FILE not found — run 01_prep_data.sh first."
37 exit 1
38fi
39source "$VARS_FILE"
40
41REF_FA="$DATA_DIR/GRCh38_no_alt.fa"
42REPEAT_MASK="$DATA_DIR/repeat_mask.bed"
43GRID_SAMPLES_FILE="$DATA_DIR/grid_samples.txt"
44
45for f in "$REF_FA" "$GRID_SAMPLES_FILE"; do
46 if [[ ! -f "$f" ]]; then
47 echo "ERROR: expected input $f not found — run 01_prep_data.sh first."
48 exit 1
49 fi
50done
51
52# --- Activate GRiD environment ---
53if ! conda env list | grep -qE '^\s*grid-test\s'; then
54 echo "ERROR: conda environment 'grid-test' not found."
55 echo "Create it first, e.g.: conda create -n grid-test python=3.11 -y"
56 exit 1
57fi
58source "$(conda info --base)/etc/profile.d/conda.sh"
59conda activate grid-test
60
61if ! command -v grid &>/dev/null; then
62 echo "ERROR: 'grid' command not found after activating grid-test."
63 echo "Check that 'pip install -e .' completed successfully in that env."
64 exit 1
65fi
66
67# --- Check for IBS output from stage 2 (optional — diploid-only if absent) ---
68IBS_OUTPUT="$DATA_DIR/ibs_neighbors_chr${CHR}.tsv.gz"
69HAPLOID_RUN="False"
70if [[ -f "$IBS_OUTPUT" ]]; then
71 log "IBS neighbors file found: $IBS_OUTPUT"
72 log "Haploid CN estimation will run."
73 HAPLOID_RUN="True"
74else
75 log "IBS neighbors file not found at $IBS_OUTPUT."
76 log "Run 02_run_ibs.sh first if you want haploid CN estimation."
77 log "Proceeding with diploid-only for this run."
78fi
79
80# Empty mask: the downloaded repeat_mask.bed spans nearly the entire LPA
81# window (KIV-2 is itself a massive tandem repeat), which zeroes out every
82# normalization bin if applied here. See prior discussion — using an empty
83# mask for this locus is the correct choice, not a workaround being skipped.
84EMPTY_MASK="$DATA_DIR/repeat_mask_empty.bed"
85touch "$EMPTY_MASK"
86
87# --- Generate config ---
88CONFIG="$WORK_DIR/config.yaml"
89
90cat > "$CONFIG" << YAML
91# Auto-generated GRiD config — 1000 Genomes LPA KIV-2 example
92samples_file: "$GRID_SAMPLES_FILE"
93directory_loc: "$CRAM_DIR"
94reference_genome: "$REF_FA"
95output_dir: "$OUTPUT_DIR"
96threads: $THREADS
97file_type: "cram"
98chrom: "chr${CHR}"
99start_bp: $START
100end_bp: $END
101output_file_type: "tsv"
102
103index:
104 run: True
105 output_file_prefix: "index_file_results"
106
107count_reads:
108 run: True
109 output_file_prefix: "read_counts"
110 min_mapq: 1
111 flags:
112 - 83 # proper pair, read reverse strand
113 - 147 # proper pair, mate reverse strand
114 - 81 # read reverse strand
115 - 145 # mate reverse strand
116
117mosdepth:
118 run: True
119 output_file_prefix: "mosdepth_results"
120 bin_size: 1000
121 mode: "fast"
122 region_name: "LPA"
123 work_dir: "$MOSDEPTH_WORK"
124 remove_intermediate: True
125
126 normalize:
127 run: True
128 min_depth: 1
129 max_depth: 30
130 top_frac: 0.9
131 output_file_prefix: "mosdepth_normalized"
132 repeat_mask_file: "$EMPTY_MASK"
133
134 neighbors:
135 run: True
136 output_file_prefix: "neighbors"
137 num_neighbors: 5
138 zmax: 2.0
139 sigma2_max: 1000
140
141compute_diploid_genotypes:
142 run: True
143 output_file_prefix: "diploid_genotypes"
144
145compute_haploid_genotypes:
146 run: $HAPLOID_RUN
147 output_file_prefix: "haploid_genotypes"
148 ibs_output: "$IBS_OUTPUT"
149 min_neighbors: 1
150 max_neighbors: 10
151 n_iters: 100
152 method: "ibs"
153YAML
154
155log "Config written to: $CONFIG"
156log " compute_haploid_genotypes.run: $HAPLOID_RUN"
157
158# --- Run GRiD ---
159log "Running GRiD pipeline..."
160grid wgs "$CONFIG"
161
162log "Done. Results in: $OUTPUT_DIR"
Full Pipeline Usage¶
Run all three stages sequentially from your working directory:
# Step 1: Prepare data and stream CRAMs
sbatch examples/01-prep-data.sh
# Step 2: (Optional) Compute IBS neighbors for haploid CN
sbatch examples/02-run-ibs.sh
# Step 3: Generate config and run GRiD
sbatch examples/03-run-grid.sh