lingzhi227 commited on
Commit
5e7f7bf
·
verified ·
1 Parent(s): 19c0107

Upload tasks/multisample-variants/run_script.sh with huggingface_hub

Browse files
tasks/multisample-variants/run_script.sh ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ # =============================================================================
5
+ # Task 24: Multi-sample Variant Calling and Comparison
6
+ #
7
+ # DAG (depth 6, per-sample parallel then merge):
8
+ #
9
+ # L0: reads_A + reads_B + reference
10
+ # L1: fastp A fastp B
11
+ # L2: bwa A bwa B
12
+ # L3: freebayes A freebayes B
13
+ # L4: bcftools filter A bcftools filter B
14
+ # └──────────┬───────────┘
15
+ # L5: bcftools isec (shared vs unique variants)
16
+ # └── variant annotation + summary
17
+ # L6: MERGE
18
+ # =============================================================================
19
+
20
+ THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
21
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
22
+ DATA="${SCRIPT_DIR}/data"
23
+ REF="${SCRIPT_DIR}/reference"
24
+ OUT="${SCRIPT_DIR}/outputs"
25
+ RES="${SCRIPT_DIR}/results"
26
+
27
+ REFERENCE="${REF}/reference.fna"
28
+ SAMPLES=("sampleA" "sampleB")
29
+
30
+ log_step() {
31
+ echo "=================================================================="
32
+ echo "STEP: $1"
33
+ echo "$(date)"
34
+ echo "=================================================================="
35
+ }
36
+
37
+ mkdir -p "${OUT}"/{trimmed,aligned,variants,filtered,comparison} "${RES}"
38
+
39
+ # Per-sample processing (L1-L4)
40
+ for SAMPLE in "${SAMPLES[@]}"; do
41
+ # L1: Trim
42
+ log_step "L1: fastp ${SAMPLE}"
43
+ if [ ! -f "${OUT}/trimmed/${SAMPLE}_R1.fastq.gz" ]; then
44
+ fastp --in1 "${DATA}/${SAMPLE}_R1.fastq.gz" --in2 "${DATA}/${SAMPLE}_R2.fastq.gz" \
45
+ --out1 "${OUT}/trimmed/${SAMPLE}_R1.fastq.gz" --out2 "${OUT}/trimmed/${SAMPLE}_R2.fastq.gz" \
46
+ --detect_adapter_for_pe --thread ${THREADS} \
47
+ --json "${OUT}/trimmed/${SAMPLE}_fastp.json"
48
+ else echo "Skipping (exists)"; fi
49
+
50
+ # L2: Align
51
+ log_step "L2: bwa ${SAMPLE}"
52
+ if [ ! -f "${OUT}/aligned/${SAMPLE}.bam" ]; then
53
+ [ ! -f "${REFERENCE}.bwt" ] && bwa index "${REFERENCE}" 2>/dev/null
54
+ bwa mem -t ${THREADS} -R "@RG\tID:${SAMPLE}\tSM:${SAMPLE}\tPL:ILLUMINA" \
55
+ "${REFERENCE}" "${OUT}/trimmed/${SAMPLE}_R1.fastq.gz" "${OUT}/trimmed/${SAMPLE}_R2.fastq.gz" \
56
+ | samtools sort -@ ${THREADS} -o "${OUT}/aligned/${SAMPLE}.bam"
57
+ samtools index "${OUT}/aligned/${SAMPLE}.bam"
58
+ else echo "Skipping (exists)"; fi
59
+
60
+ # L3: Call variants
61
+ log_step "L3: freebayes ${SAMPLE}"
62
+ if [ ! -f "${OUT}/variants/${SAMPLE}.vcf" ]; then
63
+ freebayes -f "${REFERENCE}" "${OUT}/aligned/${SAMPLE}.bam" \
64
+ --min-mapping-quality 20 --min-base-quality 20 \
65
+ > "${OUT}/variants/${SAMPLE}.vcf"
66
+ else echo "Skipping (exists)"; fi
67
+
68
+ # L4: Filter
69
+ log_step "L4: bcftools filter ${SAMPLE}"
70
+ if [ ! -f "${OUT}/filtered/${SAMPLE}.vcf.gz" ]; then
71
+ bcftools filter -i 'QUAL>20 && INFO/DP>5' "${OUT}/variants/${SAMPLE}.vcf" \
72
+ | bcftools view -Oz -o "${OUT}/filtered/${SAMPLE}.vcf.gz"
73
+ bcftools index "${OUT}/filtered/${SAMPLE}.vcf.gz"
74
+ else echo "Skipping (exists)"; fi
75
+ done
76
+
77
+ # L5: Compare variants between samples
78
+ log_step "L5: bcftools isec (shared vs unique)"
79
+ if [ ! -d "${OUT}/comparison/isec" ]; then
80
+ bcftools isec -p "${OUT}/comparison/isec" \
81
+ "${OUT}/filtered/sampleA.vcf.gz" \
82
+ "${OUT}/filtered/sampleB.vcf.gz"
83
+ else echo "Skipping (exists)"; fi
84
+
85
+ # Parse comparison results
86
+ UNIQUE_A=$(grep -cv "^#" "${OUT}/comparison/isec/0000.vcf" 2>/dev/null || echo "0")
87
+ UNIQUE_B=$(grep -cv "^#" "${OUT}/comparison/isec/0001.vcf" 2>/dev/null || echo "0")
88
+ SHARED=$(grep -cv "^#" "${OUT}/comparison/isec/0002.vcf" 2>/dev/null || echo "0")
89
+
90
+ # Per-sample stats
91
+ VARS_A=$(bcftools stats "${OUT}/filtered/sampleA.vcf.gz" | grep "^SN" | grep "number of records" | cut -f4)
92
+ VARS_B=$(bcftools stats "${OUT}/filtered/sampleB.vcf.gz" | grep "^SN" | grep "number of records" | cut -f4)
93
+ SNPS_A=$(bcftools stats "${OUT}/filtered/sampleA.vcf.gz" | grep "^SN" | grep "number of SNPs" | cut -f4)
94
+ SNPS_B=$(bcftools stats "${OUT}/filtered/sampleB.vcf.gz" | grep "^SN" | grep "number of SNPs" | cut -f4)
95
+
96
+ # L6: MERGE
97
+ log_step "L6-MERGE"
98
+
99
+ cat > "${RES}/variant_comparison.csv" << CSVEOF
100
+ metric,value
101
+ total_variants_sampleA,${VARS_A}
102
+ total_variants_sampleB,${VARS_B}
103
+ snps_sampleA,${SNPS_A}
104
+ snps_sampleB,${SNPS_B}
105
+ unique_to_sampleA,${UNIQUE_A}
106
+ unique_to_sampleB,${UNIQUE_B}
107
+ shared_variants,${SHARED}
108
+ CSVEOF
109
+
110
+ echo ""
111
+ echo "=== Pipeline complete ==="
112
+ cat "${RES}/variant_comparison.csv"