WGBS:只跑代码
使用配套 chr1 输入,从修剪开始生成 BAM、甲基化计数、bigWig、profile 和扇形图。 先激活环境 conda activate wgbs,并选择新的空输出目录。
返回完整教程 · 下载运行脚本 · profile Python · 扇形图 Python
两条启动命令
按实际位置修改 TUTORIAL、INPUT、WORK。第一条只检查,不计算;第二条才启动。
TUTORIAL=/home/sqc/tutil/WGBS_COMPLETE_TUTORIAL_20261126
INPUT=/home/sqc/tutil/WGBS_CHR1_CLASSROOM_INPUT_20261126
WORK="$HOME/wgbs_chr1_course"
bash "$TUTORIAL/scripts/run_classroom_pipeline.sh" --check "$INPUT" "$WORK"
bash "$TUTORIAL/scripts/run_classroom_pipeline.sh" "$INPUT" "$WORK"本入口复现已有图表的 Q20、最短 20 bp、固定 5+5 bp 设置;额外裁剪必要性尚无对照结论。 它仅使用已经筛出的 chr1 教学 FASTQ,Bismark 为本例核实后的默认 directional; 换文库请先看第九节。
完整运行代码
所有步骤按前台顺序执行,失败会停止。进度及各步日志写入新工作目录。 下列代码与可下载脚本内容相同。
#!/usr/bin/env bash
# Reproduce the SE chr1 classroom workflow in a new output directory.
set -euo pipefail
trap 'printf "Failed at line %s. Check the current step log.\n" "$LINENO" >&2' ERR
if [[ "${1:-}" == "--help" || $# == 0 ]]; then
printf 'Usage: bash %s [--check] INPUT_DIRECTORY [NEW_OUTPUT_DIRECTORY]\n' "$0"
printf 'Activate the course environment first. --check only checks inputs/tools.\n'
exit 0
fi
check_only=0
if [[ "$1" == "--check" ]]; then check_only=1; shift; fi
input=$(cd "${1:?Provide the classroom input directory}" && pwd)
work=${2:-"$HOME/wgbs_chr1_course"}
samples=(SRR534177_colWT SRR534239_met1)
ref_name=Arabidopsis_thaliana_TAIR10_chr1
for tool in trim_galore fastqc bismark_genome_preparation bowtie2 \
bismark deduplicate_bismark samtools bismark_methylation_extractor \
MethylDackel bedGraphToBigWig computeMatrix plotProfile python; do
command -v "$tool" >/dev/null || { printf 'Missing tool: %s\n' "$tool" >&2; exit 1; }
done
for file in "$input/00_reference/$ref_name.fa" \
"$input/00_reference/$ref_name.gff3" "$input/00_reference/$ref_name.genes.bed" \
"$input/scripts/gene_region_profile.py" "$input/scripts/context_pie.py"; do
[[ -s "$file" ]] || { printf 'Missing input: %s\n' "$file" >&2; exit 1; }
done
for sample in "${samples[@]}"; do
[[ -s "$input/01_rawdata/$sample.chr1.fastq.gz" ]] ||
{ printf 'Missing FASTQ: %s\n' "$sample" >&2; exit 1; }
done
python -c 'import matplotlib'
if [[ -e "$work" && ! -d "$work" ]]; then
printf 'Output is not a directory: %s\n' "$work" >&2; exit 1
fi
if [[ -d "$work" && -n "$(find "$work" -mindepth 1 -maxdepth 1 -print -quit)" ]]; then
printf 'Choose an empty output directory: %s\n' "$work" >&2; exit 1
fi
printf 'Input and tool checks passed. Output: %s\n' "$work"
if (( check_only )); then exit 0; fi
mkdir -p "$work"
cd "$work"
mkdir -p 00_reference 01_rawdata 03_trimmed 04_bismark_index \
05_alignment 06_methylation 07_igv 08_plots 10_summary scripts
exec > >(tee 10_summary/code_only.log) 2>&1
export MPLBACKEND=Agg
export OPENBLAS_NUM_THREADS=1
export OMP_NUM_THREADS=1
# 1. Copy only the supplied chr1 classroom input, not the original full FASTQ.
cp "$input/00_reference/$ref_name.fa" "$input/00_reference/$ref_name.gff3" \
"$input/00_reference/$ref_name.genes.bed" 00_reference/
cp "$input"/01_rawdata/*.chr1.fastq.gz 01_rawdata/
cp "$input/scripts/gene_region_profile.py" "$input/scripts/context_pie.py" scripts/
ref="00_reference/$ref_name.fa"
genes="00_reference/$ref_name.gff3"
genome_dir="$PWD/04_bismark_index/$ref_name"
gzip -t 01_rawdata/*.chr1.fastq.gz
samtools faidx "$ref"
# 2. The fixed 5+5 bases reproduce the published lesson figures.
# Their necessity has not been established by an unclipped M-bias comparison.
for sample in "${samples[@]}"; do
printf '\nTrimming %s\n' "$sample"
trim_galore --cores 2 --quality 20 --length 20 --fastqc \
--clip_R1 5 --three_prime_clip_R1 5 \
--output_dir 03_trimmed "01_rawdata/$sample.chr1.fastq.gz" \
> "03_trimmed/$sample.trim.log" 2>&1
done
# 3. Build the shared chr1 bisulfite reference once.
printf '\nBuilding chr1 index\n'
mkdir -p "$genome_dir"
ln -s "$PWD/$ref" "$genome_dir/$ref_name.fa"
bismark_genome_preparation --bowtie2 --parallel 4 "$genome_dir" \
> 04_bismark_index/index.log 2>&1
cut -f1,2 "$ref.fai" > "07_igv/$ref_name.chrom.sizes"
for sample in "${samples[@]}"; do
printf '\nAligning and analysing %s\n' "$sample"
stem="$sample.chr1_trimmed_bismark_bt2"
# 4. These two specific libraries use Bismark's default directional mode.
bismark --genome "$genome_dir" --bowtie2 --bam -p 2 \
-o 05_alignment "03_trimmed/${sample}.chr1_trimmed.fq.gz" \
> "05_alignment/$sample.bismark.log" 2>&1
samtools quickcheck -v "05_alignment/$stem.bam"
samtools view -c "05_alignment/$stem.bam"
# 5. Deduplicate before coordinate sorting.
(cd 05_alignment && deduplicate_bismark --bam "$stem.bam") \
> "05_alignment/$sample.dedup.log" 2>&1
bam="05_alignment/$stem.deduplicated.sorted.bam"
samtools sort -@ 2 -o "$bam" "05_alignment/$stem.deduplicated.bam"
samtools index "$bam"
samtools quickcheck -v "$bam"
samtools flagstat "$bam" > "10_summary/$sample.flagstat.txt"
samtools coverage "$bam" > "10_summary/$sample.coverage.tsv"
stem="$stem.deduplicated.sorted"
# 6. Keep Bismark calls/reports and MethylDackel 0-1 tracks.
bismark_methylation_extractor --single-end --bedGraph --counts --gzip \
--genome_folder "$genome_dir" -o 06_methylation "$bam" \
> "06_methylation/$sample.extract.log" 2>&1
MethylDackel extract --CHG --CHH --fraction \
-o "06_methylation/$sample" "$ref" "$bam" \
> "06_methylation/$sample.methyldackel.log" 2>&1
# 7. Six tracks in total; values are fractions, not percentages.
for context in CpG CHG CHH; do
awk 'BEGIN{OFS="\t"} !/^(track|browser|#)/ && NF==4 {
print $1,$2,$3,$4
}' "06_methylation/${sample}_${context}.meth.bedGraph" |
LC_ALL=C sort -k1,1 -k2,2n > "07_igv/$sample.$context.clean.bedGraph"
bedGraphToBigWig "07_igv/$sample.$context.clean.bedGraph" \
"07_igv/$ref_name.chrom.sizes" "07_igv/$sample.$context.bw"
done
# 8. Read gene coordinates directly from GFF3. No extra BED for this step.
for mode in absolute norm; do
profile_args=()
if [[ "$mode" == norm ]]; then profile_args=(--smooth-window 3 --normalize-max); fi
python scripts/gene_region_profile.py \
--genes "$genes" --cpg 06_methylation/CpG_{OB,OT}_"$stem".txt.gz \
--noncpg 06_methylation/{CHG,CHH}_{OB,OT}_"$stem".txt.gz \
--sample "$sample" --title "$sample" "${profile_args[@]}" \
--out-tsv "08_plots/$sample.official_like_profile.$mode.tsv" \
--out-plot "08_plots/$sample.official_like_profile.$mode.png"
done
# 9. deepTools uses the supplied six-column .bed and keeps all genes.
computeMatrix scale-regions \
-S "07_igv/$sample.CpG.bw" "07_igv/$sample.CHG.bw" "07_igv/$sample.CHH.bw" \
-R "00_reference/$ref_name.genes.bed" \
-b 1000 -a 1000 --regionBodyLength 1000 --binSize 100 -p 2 \
-o "08_plots/$sample.deeptools.all_genes.matrix.gz"
plotProfile -m "08_plots/$sample.deeptools.all_genes.matrix.gz" \
--perGroup --samplesLabel CG CHG CHH \
--yAxisLabel "Mean methylation signal" --plotTitle "$sample" \
-out "08_plots/$sample.deeptools_profile.all_genes.png"
# 10. Pie denominator: all methylated CG/CHG/CHH calls in this report.
python scripts/context_pie.py --report "06_methylation/${stem}_splitting_report.txt" \
--sample "$sample" --outdir 08_plots
done
printf '\nCompleted. Figures: %s/08_plots/ ; reports: %s/10_summary/\n' "$PWD" "$PWD"看结果
ls -lh "$WORK"/08_plots/*.png
ls -lh "$WORK"/07_igv/*.bw
cat "$WORK"/10_summary/*.coverage.tsv03_trimmed/ 看 FastQC;05_alignment/ 看比对与去重报告; 06_methylation/ 看 splitting report、M-bias;08_plots/ 看图; 10_summary/code_only.log 看总进度。图的含义与本服务器已有真实结果见完整教程。
本脚本组合了已实跑的课堂步骤;此次更新通过 Bash 语法和本机输入/工具预检查, 没有再次启动整套比对。IGV 的交互载入与读图在完整教程中保留。
修剪日志:03_trimmed/*.trim.log;总进度日志:10_summary/code_only.log。