lingzhi227 commited on
Commit
e2a9685
·
verified ·
1 Parent(s): 0c75bf0

Upload tasks/mapping-qc/run_script.sh with huggingface_hub

Browse files
Files changed (1) hide show
  1. tasks/mapping-qc/run_script.sh +143 -0
tasks/mapping-qc/run_script.sh ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ # =============================================================================
5
+ # Task 23: Genome Coverage and Mapping Quality Analysis
6
+ #
7
+ # DAG (depth 5):
8
+ #
9
+ # L0: PE reads + reference
10
+ # L1: fastp (trim)
11
+ # L2: bwa mem (align)
12
+ # ├──────────────────────────────────────┐
13
+ # L3: samtools stats/flagstat mosdepth (coverage)
14
+ # │ │
15
+ # L4: samtools idxstats ├── per-base coverage
16
+ # (per-contig mapping) └── per-window coverage
17
+ # │ │
18
+ # └── bedtools genomecov deeptools bamCoverage
19
+ # (coverage histogram) (normalized signal)
20
+ # L5: MERGE
21
+ # =============================================================================
22
+
23
+ THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
24
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
25
+ DATA="${SCRIPT_DIR}/data"
26
+ REF="${SCRIPT_DIR}/reference"
27
+ OUT="${SCRIPT_DIR}/outputs"
28
+ RES="${SCRIPT_DIR}/results"
29
+
30
+ REFERENCE="${REF}/reference.fna"
31
+
32
+ log_step() {
33
+ echo "=================================================================="
34
+ echo "STEP: $1"
35
+ echo "$(date)"
36
+ echo "=================================================================="
37
+ }
38
+
39
+ mkdir -p "${OUT}"/{trimmed,aligned,stats,coverage} "${RES}"
40
+
41
+ # ===========================================================================
42
+ # L1: Trimming
43
+ # ===========================================================================
44
+ log_step "L1: fastp"
45
+ if [ ! -f "${OUT}/trimmed/R1.fastq" ]; then
46
+ fastp --in1 "${DATA}/reads_R1.fastq" --in2 "${DATA}/reads_R2.fastq" \
47
+ --out1 "${OUT}/trimmed/R1.fastq" --out2 "${OUT}/trimmed/R2.fastq" \
48
+ --detect_adapter_for_pe --thread ${THREADS} \
49
+ --json "${OUT}/trimmed/fastp.json"
50
+ else echo "Skipping (exists)"; fi
51
+
52
+ # ===========================================================================
53
+ # L2: Alignment
54
+ # ===========================================================================
55
+ log_step "L2: bwa mem"
56
+ if [ ! -f "${OUT}/aligned/reads.sorted.bam" ]; then
57
+ bwa index "${REFERENCE}" 2>/dev/null
58
+ bwa mem -t ${THREADS} -R "@RG\tID:sample\tSM:sample\tPL:ILLUMINA" \
59
+ "${REFERENCE}" "${OUT}/trimmed/R1.fastq" "${OUT}/trimmed/R2.fastq" \
60
+ | samtools sort -@ ${THREADS} -o "${OUT}/aligned/reads.sorted.bam"
61
+ samtools index "${OUT}/aligned/reads.sorted.bam"
62
+ else echo "Skipping (exists)"; fi
63
+ BAM="${OUT}/aligned/reads.sorted.bam"
64
+
65
+ # ===========================================================================
66
+ # L3-A: samtools stats + flagstat
67
+ # ===========================================================================
68
+ log_step "L3-A: samtools stats"
69
+ if [ ! -f "${OUT}/stats/samtools_stats.txt" ]; then
70
+ samtools stats "${BAM}" > "${OUT}/stats/samtools_stats.txt"
71
+ samtools flagstat "${BAM}" > "${OUT}/stats/flagstat.txt"
72
+ samtools idxstats "${BAM}" > "${OUT}/stats/idxstats.txt"
73
+ else echo "Skipping (exists)"; fi
74
+
75
+ # ===========================================================================
76
+ # L3-B: mosdepth coverage analysis
77
+ # ===========================================================================
78
+ log_step "L3-B: mosdepth"
79
+ if [ ! -f "${OUT}/coverage/sample.mosdepth.summary.txt" ]; then
80
+ mosdepth -t ${THREADS} --by 1000 "${OUT}/coverage/sample" "${BAM}"
81
+ else echo "Skipping (exists)"; fi
82
+
83
+ # ===========================================================================
84
+ # L4-A: bedtools genomecov (coverage histogram)
85
+ # ===========================================================================
86
+ log_step "L4-A: bedtools genomecov"
87
+ if [ ! -f "${OUT}/coverage/genomecov.txt" ]; then
88
+ bedtools genomecov -ibam "${BAM}" > "${OUT}/coverage/genomecov.txt"
89
+ else echo "Skipping (exists)"; fi
90
+
91
+ # ===========================================================================
92
+ # L4-B: deeptools bamCoverage (normalized signal)
93
+ # ===========================================================================
94
+ log_step "L4-B: deeptools bamCoverage"
95
+ if [ ! -f "${OUT}/coverage/sample.bw" ]; then
96
+ bamCoverage --bam "${BAM}" --outFileName "${OUT}/coverage/sample.bw" \
97
+ --binSize 100 --normalizeUsing RPGC \
98
+ --effectiveGenomeSize 2900000 \
99
+ --numberOfProcessors ${THREADS} 2>/dev/null || true
100
+ else echo "Skipping (exists)"; fi
101
+
102
+ # ===========================================================================
103
+ # L5: MERGE
104
+ # ===========================================================================
105
+ log_step "L5-MERGE"
106
+
107
+ # Parse samtools stats
108
+ TOTAL_READS=$(grep "^SN" "${OUT}/stats/samtools_stats.txt" | grep "raw total sequences" | cut -f3)
109
+ MAPPED_READS=$(grep "^SN" "${OUT}/stats/samtools_stats.txt" | grep "reads mapped:" | cut -f3)
110
+ MAPPED_PCT=$(grep "mapped (" "${OUT}/stats/flagstat.txt" | head -1 | grep -oP '[\d.]+%' | tr -d '%')
111
+ PROPERLY_PAIRED=$(grep "properly paired" "${OUT}/stats/flagstat.txt" | grep -oP '[\d.]+%' | tr -d '%')
112
+ AVG_QUALITY=$(grep "^SN" "${OUT}/stats/samtools_stats.txt" | grep "average quality" | cut -f3)
113
+ AVG_INSERT=$(grep "^SN" "${OUT}/stats/samtools_stats.txt" | grep "insert size average" | cut -f3)
114
+ ERROR_RATE=$(grep "^SN" "${OUT}/stats/samtools_stats.txt" | grep "error rate" | cut -f3)
115
+
116
+ # Parse mosdepth
117
+ MEAN_COV=$(awk '$1=="total" {print $4}' "${OUT}/coverage/sample.mosdepth.summary.txt")
118
+ MIN_COV=$(awk '$1=="total" {print $5}' "${OUT}/coverage/sample.mosdepth.summary.txt")
119
+ MAX_COV=$(awk '$1=="total" {print $6}' "${OUT}/coverage/sample.mosdepth.summary.txt")
120
+
121
+ # Coverage breadth from genomecov (% bases with >=1x coverage)
122
+ BREADTH=$(awk '$1=="genome" && $2==0 {print 100-$5*100}' "${OUT}/coverage/genomecov.txt" | head -1)
123
+
124
+ cat > "${RES}/mapping_qc_report.csv" << CSVEOF
125
+ metric,value
126
+ total_reads,${TOTAL_READS}
127
+ mapped_reads,${MAPPED_READS}
128
+ mapping_rate,${MAPPED_PCT}
129
+ properly_paired_rate,${PROPERLY_PAIRED}
130
+ average_base_quality,${AVG_QUALITY}
131
+ average_insert_size,${AVG_INSERT}
132
+ error_rate,${ERROR_RATE}
133
+ mean_coverage,${MEAN_COV}
134
+ min_coverage,${MIN_COV}
135
+ max_coverage,${MAX_COV}
136
+ coverage_breadth_pct,${BREADTH}
137
+ CSVEOF
138
+
139
+ echo ""
140
+ echo "=== Pipeline complete ==="
141
+ cat "${RES}/mapping_qc_report.csv"
142
+ echo ""
143
+ ls -lh "${RES}/"