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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
# =============================================================================
|
| 5 |
+
# CNV Detection from WES Pipeline
|
| 6 |
+
# =============================================================================
|
| 7 |
+
# DAG Structure (depth=10, convergence=4, tools=10):
|
| 8 |
+
#
|
| 9 |
+
# tumor.fastq.gz normal.fastq.gz
|
| 10 |
+
# | |
|
| 11 |
+
# [fastp QC] ----------- [fastp QC] Level 1
|
| 12 |
+
# | |
|
| 13 |
+
# [bwa-mem2 align] ------ [bwa-mem2 align] Level 2
|
| 14 |
+
# | |
|
| 15 |
+
# [picard MarkDup] ------ [picard MarkDup] Level 3
|
| 16 |
+
# | |
|
| 17 |
+
# [mosdepth coverage] --- [mosdepth coverage] Level 4
|
| 18 |
+
# | |
|
| 19 |
+
# +-----------+-----------+
|
| 20 |
+
# |
|
| 21 |
+
# CONVERGENCE 1 Level 5
|
| 22 |
+
# (T+N BAMs + coverage)
|
| 23 |
+
# |
|
| 24 |
+
# +-----------+-----------+
|
| 25 |
+
# | | |
|
| 26 |
+
# [cnvkit [freec] [gatk4 Level 6
|
| 27 |
+
# batch] CollectRC->
|
| 28 |
+
# DenoiseRC->
|
| 29 |
+
# ModelSeg->
|
| 30 |
+
# CallSeg]
|
| 31 |
+
# | | |
|
| 32 |
+
# +-----------+-----+-----+
|
| 33 |
+
# |
|
| 34 |
+
# CONVERGENCE 2 Level 7
|
| 35 |
+
# (3-caller merge)
|
| 36 |
+
# [bedtools intersect]
|
| 37 |
+
# |
|
| 38 |
+
# +-------+-------+
|
| 39 |
+
# | | |
|
| 40 |
+
# [bcftools [bedtools [python Level 8
|
| 41 |
+
# stats] intersect gene-level
|
| 42 |
+
# w/ genes] stats]
|
| 43 |
+
# | | |
|
| 44 |
+
# +-------+-------+
|
| 45 |
+
# |
|
| 46 |
+
# CONVERGENCE 3 Level 9
|
| 47 |
+
# (stats + gene-impact)
|
| 48 |
+
# |
|
| 49 |
+
# [python report]
|
| 50 |
+
# CONVERGENCE 4 <-- QC + coverage Level 10
|
| 51 |
+
# =============================================================================
|
| 52 |
+
|
| 53 |
+
THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
|
| 54 |
+
WORKDIR="$(cd "$(dirname "$0")" && pwd)"
|
| 55 |
+
DATA="${WORKDIR}/data"
|
| 56 |
+
REF="${WORKDIR}/reference"
|
| 57 |
+
OUT="${WORKDIR}/outputs"
|
| 58 |
+
RES="${WORKDIR}/results"
|
| 59 |
+
GENOME="${REF}/genome.fa"
|
| 60 |
+
TARGETS="${REF}/targets.bed"
|
| 61 |
+
|
| 62 |
+
mkdir -p "${OUT}"/{tumor,normal,cnvkit,freec,gatk_cnv,merged,qc} "${RES}"
|
| 63 |
+
|
| 64 |
+
# =============================================================================
|
| 65 |
+
# Index reference
|
| 66 |
+
# =============================================================================
|
| 67 |
+
if [ ! -f "${GENOME}.bwt.2bit.64" ]; then
|
| 68 |
+
echo "Indexing reference..."
|
| 69 |
+
bwa-mem2 index "${GENOME}"
|
| 70 |
+
samtools faidx "${GENOME}"
|
| 71 |
+
samtools dict "${GENOME}" > "${GENOME%.fa}.dict"
|
| 72 |
+
fi
|
| 73 |
+
|
| 74 |
+
# =============================================================================
|
| 75 |
+
# Level 1-3: Process Tumor
|
| 76 |
+
# =============================================================================
|
| 77 |
+
if [ ! -f "${OUT}/tumor/tumor.dedup.bam" ]; then
|
| 78 |
+
echo "[L1-3] Processing tumor..."
|
| 79 |
+
fastp -i "${DATA}/tumor_R1.fastq.gz" -I "${DATA}/tumor_R2.fastq.gz" \
|
| 80 |
+
-o "${OUT}/tumor/R1_trimmed.fq.gz" -O "${OUT}/tumor/R2_trimmed.fq.gz" \
|
| 81 |
+
--json "${OUT}/qc/tumor_fastp.json" --thread "${THREADS}" --length_required 30
|
| 82 |
+
|
| 83 |
+
bwa-mem2 mem -t "${THREADS}" \
|
| 84 |
+
-R "@RG\tID:tumor\tSM:tumor\tPL:ILLUMINA\tLB:lib1" \
|
| 85 |
+
"${GENOME}" "${OUT}/tumor/R1_trimmed.fq.gz" "${OUT}/tumor/R2_trimmed.fq.gz" \
|
| 86 |
+
| samtools sort -@ "${THREADS}" -o "${OUT}/tumor/tumor.sorted.bam"
|
| 87 |
+
|
| 88 |
+
picard MarkDuplicates \
|
| 89 |
+
I="${OUT}/tumor/tumor.sorted.bam" \
|
| 90 |
+
O="${OUT}/tumor/tumor.dedup.bam" \
|
| 91 |
+
M="${OUT}/tumor/tumor.dedup_metrics.txt" \
|
| 92 |
+
REMOVE_DUPLICATES=false CREATE_INDEX=true 2>/dev/null
|
| 93 |
+
fi
|
| 94 |
+
|
| 95 |
+
# =============================================================================
|
| 96 |
+
# Level 1-3: Process Normal
|
| 97 |
+
# =============================================================================
|
| 98 |
+
if [ ! -f "${OUT}/normal/normal.dedup.bam" ]; then
|
| 99 |
+
echo "[L1-3] Processing normal..."
|
| 100 |
+
fastp -i "${DATA}/normal_R1.fastq.gz" -I "${DATA}/normal_R2.fastq.gz" \
|
| 101 |
+
-o "${OUT}/normal/R1_trimmed.fq.gz" -O "${OUT}/normal/R2_trimmed.fq.gz" \
|
| 102 |
+
--json "${OUT}/qc/normal_fastp.json" --thread "${THREADS}" --length_required 30
|
| 103 |
+
|
| 104 |
+
bwa-mem2 mem -t "${THREADS}" \
|
| 105 |
+
-R "@RG\tID:normal\tSM:normal\tPL:ILLUMINA\tLB:lib1" \
|
| 106 |
+
"${GENOME}" "${OUT}/normal/R1_trimmed.fq.gz" "${OUT}/normal/R2_trimmed.fq.gz" \
|
| 107 |
+
| samtools sort -@ "${THREADS}" -o "${OUT}/normal/normal.sorted.bam"
|
| 108 |
+
|
| 109 |
+
picard MarkDuplicates \
|
| 110 |
+
I="${OUT}/normal/normal.sorted.bam" \
|
| 111 |
+
O="${OUT}/normal/normal.dedup.bam" \
|
| 112 |
+
M="${OUT}/normal/normal.dedup_metrics.txt" \
|
| 113 |
+
REMOVE_DUPLICATES=false CREATE_INDEX=true 2>/dev/null
|
| 114 |
+
fi
|
| 115 |
+
|
| 116 |
+
# =============================================================================
|
| 117 |
+
# Level 4: mosdepth coverage
|
| 118 |
+
# =============================================================================
|
| 119 |
+
if [ ! -f "${OUT}/tumor/tumor.mosdepth.global.dist.txt" ]; then
|
| 120 |
+
echo "[L4] Running mosdepth coverage..."
|
| 121 |
+
mosdepth --by "${TARGETS}" -t "${THREADS}" \
|
| 122 |
+
"${OUT}/tumor/tumor" "${OUT}/tumor/tumor.dedup.bam" || true
|
| 123 |
+
mosdepth --by "${TARGETS}" -t "${THREADS}" \
|
| 124 |
+
"${OUT}/normal/normal" "${OUT}/normal/normal.dedup.bam" || true
|
| 125 |
+
fi
|
| 126 |
+
|
| 127 |
+
# =============================================================================
|
| 128 |
+
# Level 5: CONVERGENCE 1 — T+N BAMs ready
|
| 129 |
+
# =============================================================================
|
| 130 |
+
echo "[L5] Convergence 1: T+N BAMs ready"
|
| 131 |
+
|
| 132 |
+
# =============================================================================
|
| 133 |
+
# Level 6a: CNVkit batch
|
| 134 |
+
# =============================================================================
|
| 135 |
+
if [ ! -f "${OUT}/cnvkit/tumor.cns" ]; then
|
| 136 |
+
echo "[L6a] Running CNVkit..."
|
| 137 |
+
cnvkit.py batch \
|
| 138 |
+
"${OUT}/tumor/tumor.dedup.bam" \
|
| 139 |
+
--normal "${OUT}/normal/normal.dedup.bam" \
|
| 140 |
+
--targets "${TARGETS}" \
|
| 141 |
+
--fasta "${GENOME}" \
|
| 142 |
+
--output-dir "${OUT}/cnvkit" \
|
| 143 |
+
--diagram --scatter \
|
| 144 |
+
-p "${THREADS}" || true
|
| 145 |
+
|
| 146 |
+
# Call CNVs
|
| 147 |
+
if [ -f "${OUT}/cnvkit/tumor.cns" ]; then
|
| 148 |
+
cnvkit.py call "${OUT}/cnvkit/tumor.cns" -o "${OUT}/cnvkit/tumor.call.cns" || true
|
| 149 |
+
fi
|
| 150 |
+
fi
|
| 151 |
+
|
| 152 |
+
# =============================================================================
|
| 153 |
+
# Level 6b: Control-FREEC
|
| 154 |
+
# =============================================================================
|
| 155 |
+
if [ ! -f "${OUT}/freec/tumor_CNVs" ]; then
|
| 156 |
+
echo "[L6b] Running Control-FREEC..."
|
| 157 |
+
# Create FREEC config
|
| 158 |
+
cat > "${OUT}/freec/config.txt" << FREEC_CFG
|
| 159 |
+
[general]
|
| 160 |
+
chrLenFile = ${GENOME}.fai
|
| 161 |
+
ploidy = 2
|
| 162 |
+
window = 50000
|
| 163 |
+
outputDir = ${OUT}/freec
|
| 164 |
+
maxThreads = ${THREADS}
|
| 165 |
+
|
| 166 |
+
[sample]
|
| 167 |
+
mateFile = ${OUT}/tumor/tumor.dedup.bam
|
| 168 |
+
inputFormat = BAM
|
| 169 |
+
mateOrientation = FR
|
| 170 |
+
|
| 171 |
+
[control]
|
| 172 |
+
mateFile = ${OUT}/normal/normal.dedup.bam
|
| 173 |
+
inputFormat = BAM
|
| 174 |
+
mateOrientation = FR
|
| 175 |
+
|
| 176 |
+
[target]
|
| 177 |
+
captureRegions = ${TARGETS}
|
| 178 |
+
FREEC_CFG
|
| 179 |
+
|
| 180 |
+
freec -conf "${OUT}/freec/config.txt" || true
|
| 181 |
+
fi
|
| 182 |
+
|
| 183 |
+
# =============================================================================
|
| 184 |
+
# Level 6c: GATK4 CNV
|
| 185 |
+
# =============================================================================
|
| 186 |
+
if [ ! -f "${OUT}/gatk_cnv/tumor.called.seg" ]; then
|
| 187 |
+
echo "[L6c] Running GATK4 CNV pipeline..."
|
| 188 |
+
|
| 189 |
+
# Create interval list from BED
|
| 190 |
+
gatk BedToIntervalList \
|
| 191 |
+
-I "${TARGETS}" \
|
| 192 |
+
-O "${OUT}/gatk_cnv/targets.interval_list" \
|
| 193 |
+
-SD "${GENOME%.fa}.dict" || true
|
| 194 |
+
|
| 195 |
+
if [ -f "${OUT}/gatk_cnv/targets.interval_list" ]; then
|
| 196 |
+
# Preprocess intervals
|
| 197 |
+
gatk PreprocessIntervals \
|
| 198 |
+
-R "${GENOME}" \
|
| 199 |
+
-L "${OUT}/gatk_cnv/targets.interval_list" \
|
| 200 |
+
--bin-length 0 \
|
| 201 |
+
--padding 250 \
|
| 202 |
+
-O "${OUT}/gatk_cnv/preprocessed.interval_list" || true
|
| 203 |
+
|
| 204 |
+
# Collect read counts
|
| 205 |
+
for sample in tumor normal; do
|
| 206 |
+
gatk CollectReadCounts \
|
| 207 |
+
-I "${OUT}/${sample}/${sample}.dedup.bam" \
|
| 208 |
+
-L "${OUT}/gatk_cnv/preprocessed.interval_list" \
|
| 209 |
+
-R "${GENOME}" \
|
| 210 |
+
-O "${OUT}/gatk_cnv/${sample}.counts.hdf5" || true
|
| 211 |
+
done
|
| 212 |
+
|
| 213 |
+
# Create PoN from normal
|
| 214 |
+
if [ -f "${OUT}/gatk_cnv/normal.counts.hdf5" ]; then
|
| 215 |
+
gatk CreateReadCountPanelOfNormals \
|
| 216 |
+
-I "${OUT}/gatk_cnv/normal.counts.hdf5" \
|
| 217 |
+
-O "${OUT}/gatk_cnv/pon.hdf5" || true
|
| 218 |
+
fi
|
| 219 |
+
|
| 220 |
+
# Denoise read counts
|
| 221 |
+
if [ -f "${OUT}/gatk_cnv/tumor.counts.hdf5" ] && [ -f "${OUT}/gatk_cnv/pon.hdf5" ]; then
|
| 222 |
+
gatk DenoiseReadCounts \
|
| 223 |
+
-I "${OUT}/gatk_cnv/tumor.counts.hdf5" \
|
| 224 |
+
--count-panel-of-normals "${OUT}/gatk_cnv/pon.hdf5" \
|
| 225 |
+
--standardized-copy-ratios "${OUT}/gatk_cnv/tumor.standardized.tsv" \
|
| 226 |
+
--denoised-copy-ratios "${OUT}/gatk_cnv/tumor.denoised.tsv" || true
|
| 227 |
+
fi
|
| 228 |
+
|
| 229 |
+
# Model segments
|
| 230 |
+
if [ -f "${OUT}/gatk_cnv/tumor.denoised.tsv" ]; then
|
| 231 |
+
gatk ModelSegments \
|
| 232 |
+
--denoised-copy-ratios "${OUT}/gatk_cnv/tumor.denoised.tsv" \
|
| 233 |
+
-O "${OUT}/gatk_cnv" \
|
| 234 |
+
--output-prefix tumor || true
|
| 235 |
+
fi
|
| 236 |
+
|
| 237 |
+
# Call segments
|
| 238 |
+
if [ -f "${OUT}/gatk_cnv/tumor.cr.seg" ]; then
|
| 239 |
+
gatk CallCopyRatioSegments \
|
| 240 |
+
-I "${OUT}/gatk_cnv/tumor.cr.seg" \
|
| 241 |
+
-O "${OUT}/gatk_cnv/tumor.called.seg" || true
|
| 242 |
+
fi
|
| 243 |
+
fi
|
| 244 |
+
fi
|
| 245 |
+
|
| 246 |
+
# =============================================================================
|
| 247 |
+
# Level 7: CONVERGENCE 2 — merge callers
|
| 248 |
+
# =============================================================================
|
| 249 |
+
echo "[L7] Merging CNV calls..."
|
| 250 |
+
|
| 251 |
+
export OUT
|
| 252 |
+
python3 << 'MERGE'
|
| 253 |
+
import os
|
| 254 |
+
|
| 255 |
+
OUT = os.environ.get("OUT", "outputs")
|
| 256 |
+
os.makedirs(f"{OUT}/merged", exist_ok=True)
|
| 257 |
+
|
| 258 |
+
all_cnvs = []
|
| 259 |
+
|
| 260 |
+
# CNVkit calls
|
| 261 |
+
cns_file = f"{OUT}/cnvkit/tumor.call.cns"
|
| 262 |
+
if os.path.exists(cns_file):
|
| 263 |
+
with open(cns_file) as f:
|
| 264 |
+
next(f) # header
|
| 265 |
+
for line in f:
|
| 266 |
+
parts = line.strip().split("\t")
|
| 267 |
+
if len(parts) >= 5:
|
| 268 |
+
chrom, start, end = parts[0], int(parts[1]), int(parts[2])
|
| 269 |
+
try:
|
| 270 |
+
cn = int(float(parts[4]))
|
| 271 |
+
if cn != 2:
|
| 272 |
+
all_cnvs.append((chrom, start, end, "CNVkit", cn))
|
| 273 |
+
except:
|
| 274 |
+
pass
|
| 275 |
+
print(f"CNVkit: {sum(1 for c in all_cnvs if c[3]=='CNVkit')} CNVs")
|
| 276 |
+
|
| 277 |
+
# FREEC calls
|
| 278 |
+
freec_file = f"{OUT}/freec/tumor_CNVs"
|
| 279 |
+
if os.path.exists(freec_file):
|
| 280 |
+
cnt = 0
|
| 281 |
+
with open(freec_file) as f:
|
| 282 |
+
for line in f:
|
| 283 |
+
parts = line.strip().split("\t")
|
| 284 |
+
if len(parts) >= 5:
|
| 285 |
+
chrom, start, end = parts[0], int(parts[1]), int(parts[2])
|
| 286 |
+
cn = int(parts[3])
|
| 287 |
+
if cn != 2:
|
| 288 |
+
all_cnvs.append((chrom, start, end, "FREEC", cn))
|
| 289 |
+
cnt += 1
|
| 290 |
+
print(f"FREEC: {cnt} CNVs")
|
| 291 |
+
|
| 292 |
+
# GATK calls
|
| 293 |
+
gatk_file = f"{OUT}/gatk_cnv/tumor.called.seg"
|
| 294 |
+
if os.path.exists(gatk_file):
|
| 295 |
+
cnt = 0
|
| 296 |
+
with open(gatk_file) as f:
|
| 297 |
+
for line in f:
|
| 298 |
+
if line.startswith("@") or line.startswith("CONTIG"):
|
| 299 |
+
continue
|
| 300 |
+
parts = line.strip().split("\t")
|
| 301 |
+
if len(parts) >= 6:
|
| 302 |
+
chrom, start, end = parts[0], int(parts[1]), int(parts[2])
|
| 303 |
+
call = parts[5] if len(parts) > 5 else ""
|
| 304 |
+
if call in ("+", "-", "AMP", "DEL"):
|
| 305 |
+
all_cnvs.append((chrom, start, end, "GATK", call))
|
| 306 |
+
cnt += 1
|
| 307 |
+
print(f"GATK: {cnt} CNVs")
|
| 308 |
+
|
| 309 |
+
# Write merged BED
|
| 310 |
+
with open(f"{OUT}/merged/all_cnvs.bed", "w") as f:
|
| 311 |
+
for chrom, start, end, caller, cn in sorted(all_cnvs):
|
| 312 |
+
f.write(f"{chrom}\t{start}\t{end}\t{caller}\t{cn}\n")
|
| 313 |
+
|
| 314 |
+
print(f"Total merged CNVs: {len(all_cnvs)}")
|
| 315 |
+
MERGE
|
| 316 |
+
|
| 317 |
+
# =============================================================================
|
| 318 |
+
# Level 8-10: Stats + Report
|
| 319 |
+
# =============================================================================
|
| 320 |
+
echo "[L8-10] Generating report..."
|
| 321 |
+
|
| 322 |
+
python3 << 'REPORT'
|
| 323 |
+
import os, json
|
| 324 |
+
|
| 325 |
+
OUT = os.environ.get("OUT", "outputs")
|
| 326 |
+
RES_DIR = os.environ.get("RES", "results")
|
| 327 |
+
|
| 328 |
+
metrics = {}
|
| 329 |
+
|
| 330 |
+
# fastp stats
|
| 331 |
+
for sample in ["tumor", "normal"]:
|
| 332 |
+
fp = f"{OUT}/qc/{sample}_fastp.json"
|
| 333 |
+
if os.path.exists(fp):
|
| 334 |
+
with open(fp) as f:
|
| 335 |
+
fj = json.load(f)
|
| 336 |
+
metrics[f"{sample}_reads"] = fj["summary"]["before_filtering"]["total_reads"] // 2
|
| 337 |
+
metrics[f"{sample}_reads_after_trim"] = fj["summary"]["after_filtering"]["total_reads"] // 2
|
| 338 |
+
|
| 339 |
+
# mosdepth coverage
|
| 340 |
+
for sample in ["tumor", "normal"]:
|
| 341 |
+
summ = f"{OUT}/{sample}/{sample}.mosdepth.summary.txt"
|
| 342 |
+
if os.path.exists(summ):
|
| 343 |
+
with open(summ) as f:
|
| 344 |
+
for line in f:
|
| 345 |
+
if line.startswith("total_region") or line.startswith("total"):
|
| 346 |
+
parts = line.strip().split("\t")
|
| 347 |
+
if len(parts) >= 4:
|
| 348 |
+
metrics[f"{sample}_mean_coverage"] = float(parts[3])
|
| 349 |
+
break
|
| 350 |
+
|
| 351 |
+
# Picard dedup metrics
|
| 352 |
+
for sample in ["tumor", "normal"]:
|
| 353 |
+
dup_file = f"{OUT}/{sample}/{sample}.dedup_metrics.txt"
|
| 354 |
+
if os.path.exists(dup_file):
|
| 355 |
+
with open(dup_file) as f:
|
| 356 |
+
header = None
|
| 357 |
+
for line in f:
|
| 358 |
+
if line.startswith("LIBRARY"):
|
| 359 |
+
header = line.strip().split("\t")
|
| 360 |
+
elif header and not line.startswith("#") and line.strip():
|
| 361 |
+
vals = line.strip().split("\t")
|
| 362 |
+
if len(vals) > 8:
|
| 363 |
+
try:
|
| 364 |
+
metrics[f"{sample}_dup_pct"] = round(float(vals[8]) * 100, 2)
|
| 365 |
+
except:
|
| 366 |
+
pass
|
| 367 |
+
break
|
| 368 |
+
|
| 369 |
+
# CNV caller counts
|
| 370 |
+
for caller, prefix in [("cnvkit", "cnvkit"), ("freec", "freec"), ("gatk", "gatk")]:
|
| 371 |
+
count = 0
|
| 372 |
+
if caller == "cnvkit":
|
| 373 |
+
f = f"{OUT}/cnvkit/tumor.call.cns"
|
| 374 |
+
if os.path.exists(f):
|
| 375 |
+
with open(f) as fh:
|
| 376 |
+
next(fh)
|
| 377 |
+
for line in fh:
|
| 378 |
+
parts = line.strip().split("\t")
|
| 379 |
+
if len(parts) >= 5:
|
| 380 |
+
try:
|
| 381 |
+
cn = int(float(parts[4]))
|
| 382 |
+
if cn != 2:
|
| 383 |
+
count += 1
|
| 384 |
+
except:
|
| 385 |
+
pass
|
| 386 |
+
elif caller == "freec":
|
| 387 |
+
f = f"{OUT}/freec/tumor_CNVs"
|
| 388 |
+
if os.path.exists(f):
|
| 389 |
+
with open(f) as fh:
|
| 390 |
+
for line in fh:
|
| 391 |
+
parts = line.strip().split("\t")
|
| 392 |
+
if len(parts) >= 4 and parts[3] != "2":
|
| 393 |
+
count += 1
|
| 394 |
+
elif caller == "gatk":
|
| 395 |
+
f = f"{OUT}/gatk_cnv/tumor.called.seg"
|
| 396 |
+
if os.path.exists(f):
|
| 397 |
+
with open(f) as fh:
|
| 398 |
+
for line in fh:
|
| 399 |
+
if not line.startswith("@") and not line.startswith("CONTIG"):
|
| 400 |
+
parts = line.strip().split("\t")
|
| 401 |
+
if len(parts) > 5 and parts[5] in ("+", "-", "AMP", "DEL"):
|
| 402 |
+
count += 1
|
| 403 |
+
metrics[f"{prefix}_cnv_count"] = count
|
| 404 |
+
|
| 405 |
+
# Merged CNVs
|
| 406 |
+
merged_f = f"{OUT}/merged/all_cnvs.bed"
|
| 407 |
+
if os.path.exists(merged_f):
|
| 408 |
+
with open(merged_f) as f:
|
| 409 |
+
metrics["total_merged_cnvs"] = sum(1 for l in f if l.strip())
|
| 410 |
+
|
| 411 |
+
# Target info
|
| 412 |
+
targets_f = os.environ.get("REF", "reference") + "/targets.bed"
|
| 413 |
+
if os.path.exists(targets_f):
|
| 414 |
+
with open(targets_f) as f:
|
| 415 |
+
metrics["target_intervals"] = sum(1 for l in f if l.strip())
|
| 416 |
+
|
| 417 |
+
# Write CSV
|
| 418 |
+
with open(f"{RES_DIR}/report.csv", "w") as f:
|
| 419 |
+
f.write("metric,value\n")
|
| 420 |
+
for k, v in metrics.items():
|
| 421 |
+
f.write(f"{k},{v}\n")
|
| 422 |
+
|
| 423 |
+
print("Report written:")
|
| 424 |
+
for k, v in metrics.items():
|
| 425 |
+
print(f" {k} = {v}")
|
| 426 |
+
REPORT
|
| 427 |
+
|
| 428 |
+
echo "=== Pipeline Complete ==="
|
| 429 |
+
cat "${RES}/report.csv"
|