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|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
# =============================================================================
|
| 5 |
+
# Clinical WGS Interpretation Pipeline
|
| 6 |
+
# =============================================================================
|
| 7 |
+
#
|
| 8 |
+
# DAG Structure (depth=12, convergence=5):
|
| 9 |
+
#
|
| 10 |
+
# sample_R1.fq.gz sample_R2.fq.gz
|
| 11 |
+
# | |
|
| 12 |
+
# [fastp QC] ------ [fastp QC] Level 1
|
| 13 |
+
# +--------+---------+
|
| 14 |
+
# |
|
| 15 |
+
# [bwa-mem2 align] Level 2
|
| 16 |
+
# [samtools sort] Level 3
|
| 17 |
+
# [picard MarkDuplicates] Level 4
|
| 18 |
+
# [gatk BQSR (BaseRecal + Apply)] Level 5
|
| 19 |
+
# |
|
| 20 |
+
# +-------+-------------------+------------------+
|
| 21 |
+
# | | | |
|
| 22 |
+
# [bcftools [Delly [mosdepth [samtools Level 6
|
| 23 |
+
# mpileup (SV call)] (coverage + flagstat]
|
| 24 |
+
# + call] per-gene)]
|
| 25 |
+
# | | | |
|
| 26 |
+
# [bcftools [bcftools | |
|
| 27 |
+
# norm] view SV VCF] | | Level 7
|
| 28 |
+
# | | | |
|
| 29 |
+
# +----+----+ | | |
|
| 30 |
+
# | | | | |
|
| 31 |
+
# [Select [Select | | Level 8
|
| 32 |
+
# SNP] Indel] | |
|
| 33 |
+
# | | | |
|
| 34 |
+
# [Filter [Filter | | Level 9
|
| 35 |
+
# SNP] Indel] | |
|
| 36 |
+
# +----+----+ | |
|
| 37 |
+
# | | |
|
| 38 |
+
# [bcftools concat] | |
|
| 39 |
+
# | +-------------------+ |
|
| 40 |
+
# | | |
|
| 41 |
+
# CONVERGENCE 1 (SNV/Indel + coverage) Level 9
|
| 42 |
+
# | |
|
| 43 |
+
# | CONVERGENCE 2 (Delly SVs) Level 10
|
| 44 |
+
# +-------+
|
| 45 |
+
# |
|
| 46 |
+
# CONVERGENCE 3 (all variants merged)
|
| 47 |
+
# |
|
| 48 |
+
# +-------+---------------+--------+
|
| 49 |
+
# | | | |
|
| 50 |
+
# [SnpSift [bcftools [python | Level 11
|
| 51 |
+
# annotate annotate score] |
|
| 52 |
+
# ClinVar] (dbSNP freq)] |
|
| 53 |
+
# +-------+---------------+ |
|
| 54 |
+
# | |
|
| 55 |
+
# CONVERGENCE 4 (all annotations) |
|
| 56 |
+
# | |
|
| 57 |
+
# +-------+-------+ |
|
| 58 |
+
# | | | | Level 12
|
| 59 |
+
# [bcftools [python [python |
|
| 60 |
+
# filter gene clinical |
|
| 61 |
+
# PASS] panel] report] |
|
| 62 |
+
# +-------+-------+ |
|
| 63 |
+
# | |
|
| 64 |
+
# CONVERGENCE 5 (coverage + QC + final)
|
| 65 |
+
# [final clinical report]
|
| 66 |
+
#
|
| 67 |
+
# =============================================================================
|
| 68 |
+
|
| 69 |
+
THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
|
| 70 |
+
WORKDIR="$(cd "$(dirname "$0")" && pwd)"
|
| 71 |
+
DATA="${WORKDIR}/data"
|
| 72 |
+
REF="${WORKDIR}/reference"
|
| 73 |
+
OUT="${WORKDIR}/outputs"
|
| 74 |
+
RESULTS="${WORKDIR}/results"
|
| 75 |
+
|
| 76 |
+
mkdir -p "${OUT}"/{qc,aligned,processed,snv,sv,filtered,annotation,stats,coverage} "${RESULTS}"
|
| 77 |
+
|
| 78 |
+
# ---- Build indexes if needed ----
|
| 79 |
+
[ ! -f "${REF}/genome.fa.fai" ] && samtools faidx "${REF}/genome.fa"
|
| 80 |
+
[ ! -f "${REF}/genome.dict" ] && picard CreateSequenceDictionary R="${REF}/genome.fa" O="${REF}/genome.dict" 2>&1
|
| 81 |
+
[ ! -f "${REF}/genome.fa.bwt.2bit.64" ] && bwa-mem2 index "${REF}/genome.fa"
|
| 82 |
+
[ ! -f "${REF}/dbsnp.vcf.gz.tbi" ] && tabix -p vcf "${REF}/dbsnp.vcf.gz"
|
| 83 |
+
[ ! -f "${REF}/clinvar.vcf.gz.tbi" ] && tabix -p vcf "${REF}/clinvar.vcf.gz"
|
| 84 |
+
|
| 85 |
+
# ---- Level 1: fastp QC ----
|
| 86 |
+
if [ ! -f "${OUT}/qc/trimmed_R1.fastq.gz" ]; then
|
| 87 |
+
echo ">>> Level 1: fastp QC"
|
| 88 |
+
fastp -i "${DATA}/sample_R1.fastq.gz" -I "${DATA}/sample_R2.fastq.gz" \
|
| 89 |
+
-o "${OUT}/qc/trimmed_R1.fastq.gz" -O "${OUT}/qc/trimmed_R2.fastq.gz" \
|
| 90 |
+
--json "${OUT}/qc/fastp.json" --html "${OUT}/qc/fastp.html" \
|
| 91 |
+
--thread ${THREADS} --detect_adapter_for_pe --cut_front --cut_tail
|
| 92 |
+
fi
|
| 93 |
+
|
| 94 |
+
# ---- Level 2: bwa-mem2 align ----
|
| 95 |
+
if [ ! -f "${OUT}/aligned/raw.bam" ]; then
|
| 96 |
+
echo ">>> Level 2: bwa-mem2 alignment"
|
| 97 |
+
bwa-mem2 mem -t ${THREADS} \
|
| 98 |
+
-R "@RG\tID:clinical\tSM:patient1\tPL:ILLUMINA\tLB:WGS\tPU:unit1" \
|
| 99 |
+
"${REF}/genome.fa" \
|
| 100 |
+
"${OUT}/qc/trimmed_R1.fastq.gz" "${OUT}/qc/trimmed_R2.fastq.gz" | \
|
| 101 |
+
samtools view -bS - > "${OUT}/aligned/raw.bam"
|
| 102 |
+
fi
|
| 103 |
+
|
| 104 |
+
# ---- Level 3: samtools sort ----
|
| 105 |
+
if [ ! -f "${OUT}/aligned/sorted.bam" ]; then
|
| 106 |
+
echo ">>> Level 3: samtools sort"
|
| 107 |
+
samtools sort -@ ${THREADS} "${OUT}/aligned/raw.bam" -o "${OUT}/aligned/sorted.bam"
|
| 108 |
+
samtools index "${OUT}/aligned/sorted.bam"
|
| 109 |
+
fi
|
| 110 |
+
|
| 111 |
+
# ---- Level 4: picard MarkDuplicates ----
|
| 112 |
+
if [ ! -f "${OUT}/processed/markdup.bam" ]; then
|
| 113 |
+
echo ">>> Level 4: picard MarkDuplicates"
|
| 114 |
+
picard MarkDuplicates \
|
| 115 |
+
I="${OUT}/aligned/sorted.bam" O="${OUT}/processed/markdup.bam" \
|
| 116 |
+
M="${OUT}/processed/markdup_metrics.txt" \
|
| 117 |
+
REMOVE_DUPLICATES=false CREATE_INDEX=true
|
| 118 |
+
fi
|
| 119 |
+
|
| 120 |
+
# ---- Level 5: GATK BQSR ----
|
| 121 |
+
if [ ! -f "${OUT}/processed/recal.bam" ]; then
|
| 122 |
+
echo ">>> Level 5: GATK BQSR"
|
| 123 |
+
gatk BaseRecalibrator -R "${REF}/genome.fa" -I "${OUT}/processed/markdup.bam" \
|
| 124 |
+
--known-sites "${REF}/dbsnp.vcf.gz" -O "${OUT}/processed/recal_table.txt" 2>&1
|
| 125 |
+
gatk ApplyBQSR -R "${REF}/genome.fa" -I "${OUT}/processed/markdup.bam" \
|
| 126 |
+
--bqsr-recal-file "${OUT}/processed/recal_table.txt" \
|
| 127 |
+
-O "${OUT}/processed/recal.bam" 2>&1
|
| 128 |
+
fi
|
| 129 |
+
|
| 130 |
+
# ============================
|
| 131 |
+
# Level 6: Three parallel tracks from recal BAM
|
| 132 |
+
# ============================
|
| 133 |
+
|
| 134 |
+
# ---- Track 1: SNV/Indel calling (bcftools) ----
|
| 135 |
+
if [ ! -f "${OUT}/snv/bcf_raw.vcf.gz" ]; then
|
| 136 |
+
echo ">>> Level 6a: bcftools SNV/Indel calling"
|
| 137 |
+
bcftools mpileup -f "${REF}/genome.fa" -q 20 -Q 20 --max-depth 1000 \
|
| 138 |
+
"${OUT}/processed/recal.bam" 2>/dev/null | \
|
| 139 |
+
bcftools call -mv --ploidy GRCh38 -Oz -o "${OUT}/snv/bcf_raw.vcf.gz" 2>/dev/null
|
| 140 |
+
bcftools index "${OUT}/snv/bcf_raw.vcf.gz"
|
| 141 |
+
fi
|
| 142 |
+
|
| 143 |
+
# Level 7: Normalize
|
| 144 |
+
if [ ! -f "${OUT}/snv/bcf_norm.vcf.gz" ]; then
|
| 145 |
+
echo ">>> Level 7: Normalize variants"
|
| 146 |
+
bcftools norm -f "${REF}/genome.fa" "${OUT}/snv/bcf_raw.vcf.gz" -Oz \
|
| 147 |
+
-o "${OUT}/snv/bcf_norm.vcf.gz" 2>/dev/null
|
| 148 |
+
bcftools index "${OUT}/snv/bcf_norm.vcf.gz"
|
| 149 |
+
fi
|
| 150 |
+
|
| 151 |
+
# Level 8-9: Select + Filter SNPs and Indels
|
| 152 |
+
if [ ! -f "${OUT}/filtered/snps_filtered.vcf.gz" ]; then
|
| 153 |
+
echo ">>> Level 8-9: SNP filtering"
|
| 154 |
+
bcftools view -v snps "${OUT}/snv/bcf_norm.vcf.gz" -Oz -o "${OUT}/filtered/snps_raw.vcf.gz" 2>/dev/null
|
| 155 |
+
bcftools index "${OUT}/filtered/snps_raw.vcf.gz"
|
| 156 |
+
bcftools filter -i 'QUAL>=20 && INFO/DP>=2' "${OUT}/filtered/snps_raw.vcf.gz" -Oz \
|
| 157 |
+
-o "${OUT}/filtered/snps_filtered.vcf.gz" 2>/dev/null
|
| 158 |
+
bcftools index "${OUT}/filtered/snps_filtered.vcf.gz"
|
| 159 |
+
fi
|
| 160 |
+
|
| 161 |
+
if [ ! -f "${OUT}/filtered/indels_filtered.vcf.gz" ]; then
|
| 162 |
+
echo ">>> Level 8-9: Indel filtering"
|
| 163 |
+
bcftools view -v indels "${OUT}/snv/bcf_norm.vcf.gz" -Oz -o "${OUT}/filtered/indels_raw.vcf.gz" 2>/dev/null
|
| 164 |
+
bcftools index "${OUT}/filtered/indels_raw.vcf.gz"
|
| 165 |
+
bcftools filter -i 'QUAL>=20 && INFO/DP>=2' "${OUT}/filtered/indels_raw.vcf.gz" -Oz \
|
| 166 |
+
-o "${OUT}/filtered/indels_filtered.vcf.gz" 2>/dev/null
|
| 167 |
+
bcftools index "${OUT}/filtered/indels_filtered.vcf.gz"
|
| 168 |
+
fi
|
| 169 |
+
|
| 170 |
+
# Merge filtered SNPs + Indels
|
| 171 |
+
if [ ! -f "${OUT}/filtered/snv_merged.vcf.gz" ]; then
|
| 172 |
+
bcftools concat -a "${OUT}/filtered/snps_filtered.vcf.gz" "${OUT}/filtered/indels_filtered.vcf.gz" -Oz \
|
| 173 |
+
-o "${OUT}/filtered/snv_merged_unsorted.vcf.gz" 2>/dev/null
|
| 174 |
+
bcftools sort "${OUT}/filtered/snv_merged_unsorted.vcf.gz" -Oz \
|
| 175 |
+
-o "${OUT}/filtered/snv_merged.vcf.gz" 2>/dev/null
|
| 176 |
+
bcftools index "${OUT}/filtered/snv_merged.vcf.gz"
|
| 177 |
+
rm -f "${OUT}/filtered/snv_merged_unsorted.vcf.gz"
|
| 178 |
+
fi
|
| 179 |
+
|
| 180 |
+
# ---- Track 2: SV detection (Delly) ----
|
| 181 |
+
if [ ! -f "${OUT}/sv/delly.vcf.gz" ]; then
|
| 182 |
+
echo ">>> Level 6b: Delly SV calling"
|
| 183 |
+
delly call -g "${REF}/genome.fa" "${OUT}/processed/recal.bam" \
|
| 184 |
+
-o "${OUT}/sv/delly.bcf" 2>&1 || true
|
| 185 |
+
if [ -f "${OUT}/sv/delly.bcf" ]; then
|
| 186 |
+
bcftools view "${OUT}/sv/delly.bcf" -Oz -o "${OUT}/sv/delly.vcf.gz" 2>/dev/null || true
|
| 187 |
+
bcftools index "${OUT}/sv/delly.vcf.gz" 2>/dev/null || true
|
| 188 |
+
fi
|
| 189 |
+
fi
|
| 190 |
+
|
| 191 |
+
# ---- Track 3: Coverage analysis (mosdepth) ----
|
| 192 |
+
if [ ! -f "${OUT}/coverage/coverage.mosdepth.summary.txt" ]; then
|
| 193 |
+
echo ">>> Level 6c: mosdepth coverage"
|
| 194 |
+
mosdepth --threads ${THREADS} "${OUT}/coverage/coverage" "${OUT}/processed/recal.bam"
|
| 195 |
+
fi
|
| 196 |
+
|
| 197 |
+
# ---- Flagstat ----
|
| 198 |
+
if [ ! -f "${OUT}/stats/flagstat.txt" ]; then
|
| 199 |
+
echo ">>> Level 6d: samtools flagstat"
|
| 200 |
+
samtools flagstat "${OUT}/processed/recal.bam" > "${OUT}/stats/flagstat.txt"
|
| 201 |
+
fi
|
| 202 |
+
|
| 203 |
+
# ============================
|
| 204 |
+
# CONVERGENCE 1-3: Merge all variant tracks
|
| 205 |
+
# ============================
|
| 206 |
+
echo ">>> CONVERGENCE 1-3: Merging variant tracks"
|
| 207 |
+
|
| 208 |
+
SNP_COUNT=$(bcftools view -H "${OUT}/filtered/snps_filtered.vcf.gz" 2>/dev/null | wc -l || true)
|
| 209 |
+
INDEL_COUNT=$(bcftools view -H "${OUT}/filtered/indels_filtered.vcf.gz" 2>/dev/null | wc -l || true)
|
| 210 |
+
SNV_TOTAL=$((${SNP_COUNT:-0} + ${INDEL_COUNT:-0}))
|
| 211 |
+
|
| 212 |
+
SV_COUNT=0
|
| 213 |
+
if [ -s "${OUT}/sv/delly.vcf.gz" ]; then
|
| 214 |
+
SV_COUNT=$(bcftools view -H "${OUT}/sv/delly.vcf.gz" 2>/dev/null | wc -l || true)
|
| 215 |
+
fi
|
| 216 |
+
|
| 217 |
+
echo "SNVs: ${SNV_TOTAL} (SNPs: ${SNP_COUNT}, Indels: ${INDEL_COUNT}), SVs: ${SV_COUNT}"
|
| 218 |
+
|
| 219 |
+
# ============================
|
| 220 |
+
# Level 11: Triple annotation branch
|
| 221 |
+
# ============================
|
| 222 |
+
|
| 223 |
+
# ClinVar annotation
|
| 224 |
+
if [ ! -f "${OUT}/annotation/clinvar_annotated.vcf" ]; then
|
| 225 |
+
echo ">>> Level 11a: ClinVar annotation"
|
| 226 |
+
SnpSift annotate "${REF}/clinvar.vcf.gz" "${OUT}/filtered/snv_merged.vcf.gz" \
|
| 227 |
+
> "${OUT}/annotation/clinvar_annotated.vcf" 2>/dev/null || true
|
| 228 |
+
fi
|
| 229 |
+
|
| 230 |
+
# bcftools stats
|
| 231 |
+
if [ ! -f "${OUT}/stats/vcf_stats.txt" ]; then
|
| 232 |
+
echo ">>> Level 11b: bcftools stats"
|
| 233 |
+
bcftools stats "${OUT}/filtered/snv_merged.vcf.gz" > "${OUT}/stats/vcf_stats.txt" 2>/dev/null
|
| 234 |
+
fi
|
| 235 |
+
|
| 236 |
+
# Variant scoring (Ti/Tv, het/hom)
|
| 237 |
+
echo ">>> Level 11c: Variant scoring"
|
| 238 |
+
TITV=$(grep "^TSTV" "${OUT}/stats/vcf_stats.txt" | head -1 | cut -f5 || true)
|
| 239 |
+
TITV=${TITV:-0}
|
| 240 |
+
|
| 241 |
+
HET_COUNT=$(bcftools view -H "${OUT}/filtered/snv_merged.vcf.gz" 2>/dev/null | \
|
| 242 |
+
awk -F'\t' '{split($10,a,":"); if(a[1]=="0/1" || a[1]=="0|1") c++} END{print c+0}' || true)
|
| 243 |
+
HOM_COUNT=$(bcftools view -H "${OUT}/filtered/snv_merged.vcf.gz" 2>/dev/null | \
|
| 244 |
+
awk -F'\t' '{split($10,a,":"); if(a[1]=="1/1" || a[1]=="1|1") c++} END{print c+0}' || true)
|
| 245 |
+
|
| 246 |
+
CLINVAR_HITS=$(grep -c "CLNSIG=" "${OUT}/annotation/clinvar_annotated.vcf" 2>/dev/null || true)
|
| 247 |
+
CLINVAR_HITS=${CLINVAR_HITS:-0}
|
| 248 |
+
|
| 249 |
+
# ============================
|
| 250 |
+
# Level 12: Gene panel + clinical report
|
| 251 |
+
# ============================
|
| 252 |
+
echo ">>> Level 12: Gene panel filtering + report"
|
| 253 |
+
|
| 254 |
+
# Gene panel intersection
|
| 255 |
+
PANEL_VARIANTS=0
|
| 256 |
+
if [ -s "${REF}/gene_panel.bed" ]; then
|
| 257 |
+
bcftools view -H "${OUT}/filtered/snv_merged.vcf.gz" 2>/dev/null | \
|
| 258 |
+
awk -F'\t' '{print $1"\t"$2-1"\t"$2"\t"$4">"$5}' \
|
| 259 |
+
> "${OUT}/annotation/variant_positions.bed" || true
|
| 260 |
+
if [ -s "${OUT}/annotation/variant_positions.bed" ]; then
|
| 261 |
+
PANEL_VARIANTS=$(bedtools intersect -a "${OUT}/annotation/variant_positions.bed" \
|
| 262 |
+
-b "${REF}/gene_panel.bed" -u | wc -l || true)
|
| 263 |
+
fi
|
| 264 |
+
fi
|
| 265 |
+
|
| 266 |
+
# Coverage stats
|
| 267 |
+
MEAN_COV=$(awk 'NR==2 {print $4}' "${OUT}/coverage/coverage.mosdepth.summary.txt" 2>/dev/null || echo "0")
|
| 268 |
+
|
| 269 |
+
# Low coverage regions (< 5x)
|
| 270 |
+
LOW_COV_REGIONS=0
|
| 271 |
+
if [ -f "${OUT}/coverage/coverage.per-base.bed.gz" ]; then
|
| 272 |
+
LOW_COV_REGIONS=$(zcat "${OUT}/coverage/coverage.per-base.bed.gz" | \
|
| 273 |
+
awk '$4 < 5 && $4 > 0 {c++} END{print c+0}' || true)
|
| 274 |
+
fi
|
| 275 |
+
|
| 276 |
+
# Alignment stats
|
| 277 |
+
MAPPED=$(grep "mapped (" "${OUT}/stats/flagstat.txt" | head -1 | awk '{print $1}')
|
| 278 |
+
TOTAL_ALN=$(grep "in total" "${OUT}/stats/flagstat.txt" | awk '{print $1}')
|
| 279 |
+
MAP_RATE=$(python3 -c "print(round(${MAPPED}/${TOTAL_ALN}*100,2)) if ${TOTAL_ALN} > 0 else print(0)")
|
| 280 |
+
|
| 281 |
+
# QC stats
|
| 282 |
+
RAW_READS=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(d['summary']['before_filtering']['total_reads'])")
|
| 283 |
+
CLEAN_READS=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(d['summary']['after_filtering']['total_reads'])")
|
| 284 |
+
Q30=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(round(d['summary']['after_filtering']['q30_rate']*100,2))")
|
| 285 |
+
|
| 286 |
+
DUP_RATE=$(grep -A1 "^LIBRARY" "${OUT}/processed/markdup_metrics.txt" | tail -1 | awk -F'\t' '{printf "%.2f", $9*100}') || DUP_RATE=0
|
| 287 |
+
|
| 288 |
+
HET_HOM_RATIO=0
|
| 289 |
+
if [ "${HOM_COUNT}" -gt 0 ] 2>/dev/null; then
|
| 290 |
+
HET_HOM_RATIO=$(python3 -c "print(round(${HET_COUNT}/${HOM_COUNT},2))")
|
| 291 |
+
fi
|
| 292 |
+
|
| 293 |
+
# MultiQC
|
| 294 |
+
multiqc "${OUT}" -o "${OUT}/multiqc" --force --quiet 2>/dev/null || true
|
| 295 |
+
|
| 296 |
+
# Write report
|
| 297 |
+
cat > "${RESULTS}/report.csv" << CSVEOF
|
| 298 |
+
metric,value
|
| 299 |
+
raw_reads,${RAW_READS}
|
| 300 |
+
clean_reads,${CLEAN_READS}
|
| 301 |
+
q30_rate,${Q30}
|
| 302 |
+
mapped_reads,${MAPPED}
|
| 303 |
+
mapping_rate,${MAP_RATE}
|
| 304 |
+
duplication_rate,${DUP_RATE}
|
| 305 |
+
mean_coverage,${MEAN_COV}
|
| 306 |
+
snp_count,${SNP_COUNT}
|
| 307 |
+
indel_count,${INDEL_COUNT}
|
| 308 |
+
total_snv,${SNV_TOTAL}
|
| 309 |
+
sv_count,${SV_COUNT}
|
| 310 |
+
ti_tv_ratio,${TITV}
|
| 311 |
+
het_count,${HET_COUNT}
|
| 312 |
+
hom_count,${HOM_COUNT}
|
| 313 |
+
het_hom_ratio,${HET_HOM_RATIO}
|
| 314 |
+
clinvar_annotated,${CLINVAR_HITS}
|
| 315 |
+
gene_panel_variants,${PANEL_VARIANTS}
|
| 316 |
+
low_coverage_regions,${LOW_COV_REGIONS}
|
| 317 |
+
CSVEOF
|
| 318 |
+
|
| 319 |
+
echo "=== Final Report ==="
|
| 320 |
+
cat "${RESULTS}/report.csv"
|
| 321 |
+
echo ""
|
| 322 |
+
echo "Pipeline complete!"
|