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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
# =============================================================================
|
| 4 |
+
# Somatic Variant Calling Pipeline (Tumor-Normal Paired)
|
| 5 |
+
# =============================================================================
|
| 6 |
+
# DAG (depth=12, convergence=4):
|
| 7 |
+
# [T: fastp→bwa→sort→markdup→BQSR] ‖ [N: fastp→bwa→sort→markdup→BQSR]
|
| 8 |
+
# CONVERGE₁(shared reference + sites)
|
| 9 |
+
# → [Caller1(somatic-paired) ‖ Caller2(joint) ‖ Caller3(pileup)]
|
| 10 |
+
# CONVERGE₂(multi-caller)
|
| 11 |
+
# → [filter ‖ coverage-stats]
|
| 12 |
+
# CONVERGE₃(filtered-variants + coverage)
|
| 13 |
+
# → annotate
|
| 14 |
+
# CONVERGE₄(annotations + variants) → report
|
| 15 |
+
# =============================================================================
|
| 16 |
+
|
| 17 |
+
THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
|
| 18 |
+
REF="reference/genome.fasta"
|
| 19 |
+
DBSNP="reference/dbsnp.vcf.gz"
|
| 20 |
+
KNOWN_INDELS="reference/known_indels.vcf.gz"
|
| 21 |
+
GNOMAD="reference/gnomad.vcf.gz"
|
| 22 |
+
INTERVALS="reference/intervals.list"
|
| 23 |
+
OUTDIR="outputs"
|
| 24 |
+
RESDIR="results"
|
| 25 |
+
|
| 26 |
+
mkdir -p "$OUTDIR"/{fastp,align,markdup,bqsr,mutect2,freebayes,bcftools_call,merge,filter,coverage,annotate} "$RESDIR"
|
| 27 |
+
|
| 28 |
+
# ─── Step 1a: Tumor QC + alignment (branch 1) ────────────────────────────────
|
| 29 |
+
echo "[Step 1a] Processing tumor sample..."
|
| 30 |
+
if [ ! -f "$OUTDIR/align/tumor.sorted.bam" ]; then
|
| 31 |
+
# Merge all tumor lane FASTQs
|
| 32 |
+
cat data/tumor/tumor_*_R1.fastq.gz > "$OUTDIR/fastp/tumor_R1.fastq.gz"
|
| 33 |
+
cat data/tumor/tumor_*_R2.fastq.gz > "$OUTDIR/fastp/tumor_R2.fastq.gz"
|
| 34 |
+
|
| 35 |
+
# QC
|
| 36 |
+
fastp -i "$OUTDIR/fastp/tumor_R1.fastq.gz" -I "$OUTDIR/fastp/tumor_R2.fastq.gz" \
|
| 37 |
+
-o "$OUTDIR/fastp/tumor_qc_R1.fastq.gz" -O "$OUTDIR/fastp/tumor_qc_R2.fastq.gz" \
|
| 38 |
+
-j "$OUTDIR/fastp/tumor_fastp.json" -w "$THREADS" --detect_adapter_for_pe 2>/dev/null
|
| 39 |
+
|
| 40 |
+
# Align with read group
|
| 41 |
+
bwa-mem2 mem -t "$THREADS" \
|
| 42 |
+
-R "@RG\tID:tumor\tSM:tumor\tPL:ILLUMINA\tLB:lib1\tPU:unit1" \
|
| 43 |
+
"$REF" "$OUTDIR/fastp/tumor_qc_R1.fastq.gz" "$OUTDIR/fastp/tumor_qc_R2.fastq.gz" 2>/dev/null \
|
| 44 |
+
| samtools sort -@ "$THREADS" -o "$OUTDIR/align/tumor.sorted.bam" -
|
| 45 |
+
samtools index "$OUTDIR/align/tumor.sorted.bam"
|
| 46 |
+
fi
|
| 47 |
+
|
| 48 |
+
# ─── Step 1b: Normal QC + alignment (branch 2) ──────────────────────────────
|
| 49 |
+
echo "[Step 1b] Processing normal sample..."
|
| 50 |
+
if [ ! -f "$OUTDIR/align/normal.sorted.bam" ]; then
|
| 51 |
+
cat data/normal/normal_*_R1.fastq.gz > "$OUTDIR/fastp/normal_R1.fastq.gz"
|
| 52 |
+
cat data/normal/normal_*_R2.fastq.gz > "$OUTDIR/fastp/normal_R2.fastq.gz"
|
| 53 |
+
|
| 54 |
+
fastp -i "$OUTDIR/fastp/normal_R1.fastq.gz" -I "$OUTDIR/fastp/normal_R2.fastq.gz" \
|
| 55 |
+
-o "$OUTDIR/fastp/normal_qc_R1.fastq.gz" -O "$OUTDIR/fastp/normal_qc_R2.fastq.gz" \
|
| 56 |
+
-j "$OUTDIR/fastp/normal_fastp.json" -w "$THREADS" --detect_adapter_for_pe 2>/dev/null
|
| 57 |
+
|
| 58 |
+
bwa-mem2 mem -t "$THREADS" \
|
| 59 |
+
-R "@RG\tID:normal\tSM:normal\tPL:ILLUMINA\tLB:lib1\tPU:unit1" \
|
| 60 |
+
"$REF" "$OUTDIR/fastp/normal_qc_R1.fastq.gz" "$OUTDIR/fastp/normal_qc_R2.fastq.gz" 2>/dev/null \
|
| 61 |
+
| samtools sort -@ "$THREADS" -o "$OUTDIR/align/normal.sorted.bam" -
|
| 62 |
+
samtools index "$OUTDIR/align/normal.sorted.bam"
|
| 63 |
+
fi
|
| 64 |
+
|
| 65 |
+
# ─── Step 2: Mark duplicates ─────────────────────────────────────────────────
|
| 66 |
+
echo "[Step 2] Marking duplicates..."
|
| 67 |
+
for SAMPLE in tumor normal; do
|
| 68 |
+
if [ ! -f "$OUTDIR/markdup/${SAMPLE}.dedup.bam" ]; then
|
| 69 |
+
gatk MarkDuplicates \
|
| 70 |
+
-I "$OUTDIR/align/${SAMPLE}.sorted.bam" \
|
| 71 |
+
-O "$OUTDIR/markdup/${SAMPLE}.dedup.bam" \
|
| 72 |
+
-M "$OUTDIR/markdup/${SAMPLE}.metrics.txt" \
|
| 73 |
+
--REMOVE_DUPLICATES false \
|
| 74 |
+
--CREATE_INDEX true 2>/dev/null
|
| 75 |
+
fi
|
| 76 |
+
done
|
| 77 |
+
|
| 78 |
+
# ─── Step 3: Base Quality Score Recalibration ────────────────────────────────
|
| 79 |
+
echo "[Step 3] BQSR..."
|
| 80 |
+
for SAMPLE in tumor normal; do
|
| 81 |
+
if [ ! -f "$OUTDIR/bqsr/${SAMPLE}.recal.bam" ]; then
|
| 82 |
+
gatk BaseRecalibrator \
|
| 83 |
+
-I "$OUTDIR/markdup/${SAMPLE}.dedup.bam" \
|
| 84 |
+
-R "$REF" \
|
| 85 |
+
--known-sites "$DBSNP" \
|
| 86 |
+
--known-sites "$KNOWN_INDELS" \
|
| 87 |
+
-O "$OUTDIR/bqsr/${SAMPLE}.recal_table" 2>/dev/null
|
| 88 |
+
|
| 89 |
+
gatk ApplyBQSR \
|
| 90 |
+
-I "$OUTDIR/markdup/${SAMPLE}.dedup.bam" \
|
| 91 |
+
-R "$REF" \
|
| 92 |
+
--bqsr-recal-file "$OUTDIR/bqsr/${SAMPLE}.recal_table" \
|
| 93 |
+
-O "$OUTDIR/bqsr/${SAMPLE}.recal.bam" 2>/dev/null
|
| 94 |
+
|
| 95 |
+
samtools index "$OUTDIR/bqsr/${SAMPLE}.recal.bam"
|
| 96 |
+
fi
|
| 97 |
+
done
|
| 98 |
+
|
| 99 |
+
# ── CONVERGENCE POINT 1: Tumor + Normal preprocessed ────────────────────────
|
| 100 |
+
|
| 101 |
+
TUMOR_BAM="$OUTDIR/bqsr/tumor.recal.bam"
|
| 102 |
+
NORMAL_BAM="$OUTDIR/bqsr/normal.recal.bam"
|
| 103 |
+
|
| 104 |
+
# ─── Step 4a: Caller 1 — Paired somatic calling (branch 1) ──────────────────
|
| 105 |
+
echo "[Step 4a] Running Caller 1 (somatic paired)..."
|
| 106 |
+
if [ ! -f "$OUTDIR/mutect2/somatic_raw.vcf.gz" ]; then
|
| 107 |
+
gatk Mutect2 \
|
| 108 |
+
-R "$REF" \
|
| 109 |
+
-I "$TUMOR_BAM" \
|
| 110 |
+
-I "$NORMAL_BAM" \
|
| 111 |
+
-normal normal \
|
| 112 |
+
--germline-resource "$GNOMAD" \
|
| 113 |
+
-L "$INTERVALS" \
|
| 114 |
+
-O "$OUTDIR/mutect2/somatic_raw.vcf.gz" \
|
| 115 |
+
--f1r2-tar-gz "$OUTDIR/mutect2/f1r2.tar.gz" 2>/dev/null
|
| 116 |
+
|
| 117 |
+
# Get pileup summaries for contamination estimation
|
| 118 |
+
gatk GetPileupSummaries \
|
| 119 |
+
-I "$TUMOR_BAM" \
|
| 120 |
+
-V "$GNOMAD" \
|
| 121 |
+
-L "$GNOMAD" \
|
| 122 |
+
-O "$OUTDIR/mutect2/tumor_pileups.table" 2>/dev/null || true
|
| 123 |
+
|
| 124 |
+
gatk GetPileupSummaries \
|
| 125 |
+
-I "$NORMAL_BAM" \
|
| 126 |
+
-V "$GNOMAD" \
|
| 127 |
+
-L "$GNOMAD" \
|
| 128 |
+
-O "$OUTDIR/mutect2/normal_pileups.table" 2>/dev/null || true
|
| 129 |
+
|
| 130 |
+
# Calculate contamination
|
| 131 |
+
if [ -f "$OUTDIR/mutect2/tumor_pileups.table" ] && [ -f "$OUTDIR/mutect2/normal_pileups.table" ]; then
|
| 132 |
+
gatk CalculateContamination \
|
| 133 |
+
-I "$OUTDIR/mutect2/tumor_pileups.table" \
|
| 134 |
+
-matched "$OUTDIR/mutect2/normal_pileups.table" \
|
| 135 |
+
-O "$OUTDIR/mutect2/contamination.table" \
|
| 136 |
+
--tumor-segmentation "$OUTDIR/mutect2/segments.table" 2>/dev/null || true
|
| 137 |
+
fi
|
| 138 |
+
|
| 139 |
+
# Filter
|
| 140 |
+
FILTER_ARGS="-R $REF -V $OUTDIR/mutect2/somatic_raw.vcf.gz -O $OUTDIR/mutect2/somatic_filtered.vcf.gz"
|
| 141 |
+
if [ -f "$OUTDIR/mutect2/contamination.table" ]; then
|
| 142 |
+
FILTER_ARGS="$FILTER_ARGS --contamination-table $OUTDIR/mutect2/contamination.table"
|
| 143 |
+
fi
|
| 144 |
+
if [ -f "$OUTDIR/mutect2/segments.table" ]; then
|
| 145 |
+
FILTER_ARGS="$FILTER_ARGS --tumor-segmentation $OUTDIR/mutect2/segments.table"
|
| 146 |
+
fi
|
| 147 |
+
gatk FilterMutectCalls $FILTER_ARGS 2>/dev/null || true
|
| 148 |
+
fi
|
| 149 |
+
|
| 150 |
+
# ─── Step 4b: Caller 2 — Joint genotype calling (branch 2) ──────────────────
|
| 151 |
+
echo "[Step 4b] Running Caller 2 (joint calling)..."
|
| 152 |
+
if [ ! -f "$OUTDIR/freebayes/joint_raw.vcf" ]; then
|
| 153 |
+
freebayes -f "$REF" \
|
| 154 |
+
--pooled-continuous \
|
| 155 |
+
--min-alternate-fraction 0.05 \
|
| 156 |
+
--min-alternate-count 2 \
|
| 157 |
+
"$TUMOR_BAM" "$NORMAL_BAM" \
|
| 158 |
+
> "$OUTDIR/freebayes/joint_raw.vcf" 2>/dev/null || true
|
| 159 |
+
fi
|
| 160 |
+
|
| 161 |
+
# ─── Step 4c: Caller 3 — Pileup-based calling (branch 3) ────────────────────
|
| 162 |
+
echo "[Step 4c] Running Caller 3 (pileup-based)..."
|
| 163 |
+
if [ ! -f "$OUTDIR/bcftools_call/pileup_raw.vcf.gz" ]; then
|
| 164 |
+
bcftools mpileup -f "$REF" \
|
| 165 |
+
--annotate FORMAT/AD,FORMAT/DP \
|
| 166 |
+
"$TUMOR_BAM" "$NORMAL_BAM" 2>/dev/null \
|
| 167 |
+
| bcftools call -mv -Oz -o "$OUTDIR/bcftools_call/pileup_raw.vcf.gz" 2>/dev/null || true
|
| 168 |
+
bcftools index "$OUTDIR/bcftools_call/pileup_raw.vcf.gz" 2>/dev/null || true
|
| 169 |
+
fi
|
| 170 |
+
|
| 171 |
+
# ── CONVERGENCE POINT 2: Multi-caller results ───────────────────────────────
|
| 172 |
+
|
| 173 |
+
# ─── Step 5a: Filter and normalize variants (branch 1) ──────────────────────
|
| 174 |
+
echo "[Step 5a] Filtering variants..."
|
| 175 |
+
# Normalize all VCFs
|
| 176 |
+
for vcf_pair in "mutect2/somatic_filtered.vcf.gz:filter/mutect2_norm.vcf.gz" \
|
| 177 |
+
"freebayes/joint_raw.vcf:filter/freebayes_norm.vcf.gz" \
|
| 178 |
+
"bcftools_call/pileup_raw.vcf.gz:filter/bcftools_norm.vcf.gz"; do
|
| 179 |
+
SRC="${vcf_pair%%:*}"
|
| 180 |
+
DST="${vcf_pair##*:}"
|
| 181 |
+
if [ -f "$OUTDIR/$SRC" ] && [ ! -f "$OUTDIR/$DST" ]; then
|
| 182 |
+
bcftools norm -f "$REF" -m -both "$OUTDIR/$SRC" 2>/dev/null \
|
| 183 |
+
| bcftools view -f PASS,. -Oz -o "$OUTDIR/$DST" 2>/dev/null || \
|
| 184 |
+
cp "$OUTDIR/$SRC" "$OUTDIR/$DST" 2>/dev/null || true
|
| 185 |
+
bcftools index "$OUTDIR/$DST" 2>/dev/null || true
|
| 186 |
+
fi
|
| 187 |
+
done
|
| 188 |
+
|
| 189 |
+
# ─── Step 5b: Coverage statistics (branch 2) ────────────────────────────────
|
| 190 |
+
echo "[Step 5b] Computing coverage..."
|
| 191 |
+
for SAMPLE in tumor normal; do
|
| 192 |
+
if [ ! -f "$OUTDIR/coverage/${SAMPLE}.mosdepth.summary.txt" ]; then
|
| 193 |
+
mosdepth --threads "$THREADS" --fast-mode \
|
| 194 |
+
"$OUTDIR/coverage/$SAMPLE" "$OUTDIR/bqsr/${SAMPLE}.recal.bam" 2>/dev/null || true
|
| 195 |
+
fi
|
| 196 |
+
done
|
| 197 |
+
|
| 198 |
+
# ── CONVERGENCE POINT 3: Filtered variants + coverage ───────────────────────
|
| 199 |
+
|
| 200 |
+
# ─── Step 6: Annotate variants ──────────────────────────────────────────────
|
| 201 |
+
echo "[Step 6] Annotating variants..."
|
| 202 |
+
# Annotate with dbSNP
|
| 203 |
+
for caller in mutect2 freebayes bcftools; do
|
| 204 |
+
NORM="$OUTDIR/filter/${caller}_norm.vcf.gz"
|
| 205 |
+
ANN="$OUTDIR/annotate/${caller}_annotated.vcf.gz"
|
| 206 |
+
if [ -f "$NORM" ] && [ ! -f "$ANN" ]; then
|
| 207 |
+
bcftools annotate -a "$DBSNP" -c ID "$NORM" -Oz -o "$ANN" 2>/dev/null || \
|
| 208 |
+
cp "$NORM" "$ANN" 2>/dev/null || true
|
| 209 |
+
bcftools index "$ANN" 2>/dev/null || true
|
| 210 |
+
fi
|
| 211 |
+
done
|
| 212 |
+
|
| 213 |
+
# ── CONVERGENCE POINT 4: Annotated variants → report ────────────────────────
|
| 214 |
+
|
| 215 |
+
# ─── Step 7: Generate report ────────────────────────────────────────────────
|
| 216 |
+
echo "[Step 7] Compiling report..."
|
| 217 |
+
python3 scripts/compile_report.py
|
| 218 |
+
|
| 219 |
+
echo "Pipeline complete. Results in results/report.csv"
|