文章目录
-
- 引言
- 准备工作
-
- [1. 软件依赖](#1. 软件依赖)
- [2. 数据与文件](#2. 数据与文件)
-
- [a. 样本信息文件 `samples.tsv`](#a. 样本信息文件
samples.tsv
) - [b. 配置文件 `config.yaml`](#b. 配置文件
config.yaml
)
- [a. 样本信息文件 `samples.tsv`](#a. 样本信息文件
- Snakemake流程详解
-
- [`rule all`: 终极目标](#
rule all
: 终极目标) - [`rule reference_index`: 创建基因组索引](#
rule reference_index
: 创建基因组索引) - [`rule extract_kinetics` (可选但推荐)](#
rule extract_kinetics
(可选但推荐)) - [`rule align_to_reference`: 序列比对](#
rule align_to_reference
: 序列比对) - [`rule detect_modifications`: 检测DNA修饰](#
rule detect_modifications
: 检测DNA修饰) - [`rule find_motifs` & `reprocess_motifs`: 寻找并重处理修饰基序](#
rule find_motifs
&reprocess_motifs
: 寻找并重处理修饰基序) - 覆盖度分析系列规则
- [`rule collect_results`: 结果汇总](#
rule collect_results
: 结果汇总)
- [`rule all`: 终极目标](#
- 如何运行流程
- 结果解读
- 总结与展望
引言
随着三代测序技术,特别是PacBio SMRT测序的成熟,我们不再仅仅满足于读取DNA的ATCG序列。PacBio测序能够直接检测DNA分子上的化学修饰,如甲基化,为表观遗传学研究打开了新的大门。DNA甲基化在基因表达调控、基因组印记、胚胎发育等众多生命活动中扮演着至关重要的角色。
然而,一个完整的甲基化分析项目涉及多个步骤、多种软件和大量数据,手动操作不仅繁琐、耗时,而且容易出错,难以保证结果的可重复性。为了解决这一痛点,我们引入了强大的工作流管理工具------Snakemake。
本文将详细介绍如何利用您提供的一个功能完善的Snakefile
,构建一个从PacBio原始数据到最终甲基化报告的全自动化、可重复的分析流程。
准备工作
在开始之前,请确保已经准备好以下软件和数据。
1. 软件依赖
本流程依赖于一系列生物信息学软件。推荐使用conda
来管理这些软件环境,以避免版本冲突。
- Snakemake : 工作流管理系统。 (
conda install -c bioconda snakemake
) - PacBio SMRT Tools : PacBio官方分析工具套件,核心工具包括
pbmm2
(比对),ipdSummary
(检测修饰),motifMaker
(寻找基序)。 - Samtools : 处理SAM/BAM文件的常用工具。(
conda install -c bioconda samtools
) - Bedtools : 处理基因组区间文件的工具集。(
conda install -c bioconda bedtools
) - R: 用于数据处理和可视化,特别是绘制覆盖度图。
2. 数据与文件
您需要准备以下文件,并按照指定的目录结构放置:
.
├── raw_data/
│ ├── sample1.subreads.bam # 您的PacBio原始BAM文件
│ └── sample2.subreads.bam
├── reference/
│ └── hg38.fasta # 参考基因组FASTA文件
└── workflow/
├── Snakefile # 我们即将详细解析的主角
├── config.yaml # 流程配置文件
├── samples.tsv # 样本信息文件
└── scripts/
└── reads_depth.R # 绘制覆盖度的R脚本
a. 样本信息文件 samples.tsv
这是一个制表符分隔的文件,用于定义样本ID和对应的原始BAM文件路径。
tsv
sample_id bam_path
sample1 /path/to/raw_data/sample1.subreads.bam
sample2 /path/to/raw_data/sample2.subreads.bam
b. 配置文件 config.yaml
这是流程的"控制中心",您可以在此调整所有关键参数,而无需修改Snakefile
本身。
yaml
# 工作目录
workdir: "/path/to/your/project"
# 参考基因组
reference:
genome: "/path/to/reference/hg38.fasta"
# 软件路径 (如果不在环境变量中)
software:
pbmm2: "pbmm2"
smrtlink: "/path/to/smrtlink/smrtcmds/bin" # SMRT Link工具目录
# 线程数
threads: 16
# 甲基化分析参数
methylation:
pvalue: 0.001
modifications: "m6A,m4C" # m5C需要特定模型
chemistry: "S/P2-C2/5.0"
motif_min_score: 30
significant_coverage: 10
significant_fraction: 0.3
# 覆盖度分析参数
coverage:
bin_size: 100000
max_depth: 300
Snakemake流程详解
下面,我们将逐一解析Snakefile
中的每一个rule
,揭示其在整个分析流程中的作用。
rule all
: 终极目标
这是流程的入口点,它定义了我们希望最终生成的全部文件。当您运行Snakemake时,它会反向推导,执行所有必要的步骤来创建这些目标文件。
rule reference_index
: 创建基因组索引
目的: 为参考基因组创建索引,这是后续比对步骤所必需的。
samtools faidx
: 创建.fai
索引,用于快速访问基因组序列。pbmm2 index
: 为pbmm2
比对工具创建专门的.mmi
索引。
python
# rule reference_index in Snakefile
rule reference_index:
input:
ref = config["reference"]["genome"]
output:
fai = config["reference"]["genome"] + ".fai",
mmi = config["reference"]["genome"] + ".mmi"
threads: config["threads"]
shell:
"""
samtools faidx {input.ref}
{config[software][pbmm2]} index --preset SUBREAD {input.ref} {output.mmi}
"""
rule extract_kinetics
(可选但推荐)
目的: 从原始BAM文件中提取并处理动力学信息(kinetics)。这是识别碱基修饰的基础。
ccs-kinetics-bystrandify
: 一个用于处理动力学信息的工具,确保信息与链对应。pbindex
: 为生成的BAM文件创建.pbi
索引。
python
# rule extract_kinetics in Snakefile
rule extract_kinetics:
input:
bam = lambda wildcards: samples.loc[wildcards.sample, "bam_path"]
output:
bam = "{sample}.kinetics.bam",
bai = "{sample}.kinetics.bam.pbi"
threads: config["threads"]
shell:
"""
ccs-kinetics-bystrandify -j {threads} {input.bam} {output.bam}
pbindex {output.bam}
"""
rule align_to_reference
: 序列比对
目的: 将包含了动力学信息的PacBio reads比对到参考基因组。
pbmm2 align
: 使用--preset SUBREAD
模式进行比对,并进行排序。samtools view | awk | samtools view
: 这是一个巧妙的处理,用于在BAM头文件中添加PL:PACBIO
等信息。某些下游SMRT tools需要这些信息来识别数据来源。
python
# rule align_to_reference in Snakefile
rule align_to_reference:
input:
bam = "{sample}.kinetics.bam",
mmi = config["reference"]["genome"] + ".mmi"
output:
bam = "{sample}.kinetics.align.bam",
bai = "{sample}.kinetics.align.bam.pbi"
threads: config["threads"]
shell:
"""
{config[software][pbmm2]} align --preset SUBREAD --sort --sample {wildcards.sample} \
-j {threads} -J {threads} {input.mmi} {input.bam} {output.bam}
pbindex {output.bam}
# 确保BAM文件包含化学反应信息
samtools view -h {output.bam} | \
awk '{{if($0 ~ /^@/) {{if($0 !~ /^@RG/) print $0; else print $0"\\tPL:PACBIO\\tPM:SEQUEL\\tCM:S/P2-C2/5.0"}} else print $0}}' | \
samtools view -bS - > {wildcards.sample}.temp.bam
mv {wildcards.sample}.temp.bam {output.bam}
pbindex {output.bam}
"""
rule detect_modifications
: 检测DNA修饰
目的: 这是流程的核心。基于比对后BAM文件中的IPD(脉冲间期)信息,识别出被修饰的碱基位点。
ipdSummary
: SMRT Link中的工具,用于统计IPD信息,并通过统计检验(p-value)识别m6A, m4C等修饰。- 输出 :
.gff
: 记录每个修饰位点的详细信息。.csv
: CSV格式的修饰位点列表。.bigwig
: 可在基因组浏览器(如IGV)中可视化修饰信号。
python
# rule detect_modifications in Snakefile
rule detect_modifications:
# ... (inputs, outputs, threads, params)
shell:
"""
mkdir -p 02.align/methylome/{wildcards.sample}
{config[software][smrtlink]}/ipdSummary \
{input.bam} --reference {input.ref} --gff {output.gff} --csv {output.csv} \
--bigwig {output.bigwig} --pvalue {params.pvalue} --numWorkers {threads} \
--identify {params.mods} --methylFraction --identify-only \
--chemistry {params.chemistry}
"""
rule find_motifs
& reprocess_motifs
: 寻找并重处理修饰基序
目的: 甲基化修饰通常发生在特定的序列模式(motif)中。这两步旨在找出这些motif。
motifMaker find
: 从basemods.gff
文件中发现与修饰相关的motif。motifMaker reprocess
: 利用发现的motif信息,重新注释GFF文件,使其包含motif信息。
python
# rule find_motifs & reprocess_motifs in Snakefile
rule find_motifs:
# ...
shell:
"""
{config[software][smrtlink]}/motifMaker find \
-f {input.ref} -g {input.gff} -o {output.csv} \
-j {threads} --minScore {params.min_score}
"""
rule reprocess_motifs:
# ...
shell:
"""
{config[software][smrtlink]}/motifMaker reprocess \
-f {input.ref} -g {input.gff} -m {input.csv} -o {output.gff}
"""
覆盖度分析系列规则
目的: 评估测序深度在全基因组的分布情况。
prepare_coverage_regions
: 使用bedtools makewindows
将基因组切分为固定大小的窗口(bins)。calculate_coverage
: 使用samtools bedcov
计算每个窗口内的read覆盖数。plot_coverage
: 调用R脚本,将覆盖度数据绘制成直观的PDF和PNG图表。
rule collect_results
: 结果汇总
目的 : 将散落在中间目录的、最重要的结果文件,统一收集到一个result
目录中,方便查看和交付。
- 使用Python的
shutil
和os
模块进行文件复制和软链接创建。 - 对齐的BAM文件由于体积较大,采用创建软链接(
os.symlink
)的方式,节约磁盘空间。
如何运行流程
一切准备就绪后,只需在workflow
目录下执行一条命令:
bash
# 进入工作流目录
cd workflow
# 预演,检查流程是否能正确构建DAG(有向无环图)
snakemake -np
# 在本地执行,使用8个核心
snakemake --cores 8
# 如果您有集群环境,还可以提交到集群
# snakemake --cluster "qsub -q xxx" --jobs 100
Snakemake会自动分析依赖关系,从头开始或从中断的地方继续执行所有必要的步骤。
结果解读
流程运行结束后,所有精华都汇集在result
目录中:
result/02.Modification/{sample}/
:*.significant.bed.xls
: 核心结果。高可信度的甲基化位点,包含染色体、位置、深度、链、修饰类型(m6A/m4C)和甲基化比例。motifs.csv
: 核心结果。鉴定出的甲基化修饰基序。*.basemods.gff
: 完整的GFF格式结果,可导入IGV进行可视化。*.kinetics.align.bam
: 最终比对文件,可与GFF一同在IGV中查看,验证单个read的甲基化状态。
result/02.Modification/{sample}/mapping_stat/
:*.png
/.pdf
: 全基因组测序深度分布图。
总结与展望
通过本文介绍的Snakemake流程,您可以实现PacBio甲基化数据分析的"一键化"运行。其优势在于:
- 自动化: 无需手动执行每一步。
- 可重复性: 相同的输入和配置保证得到相同的结果。
- 可扩展性: 轻松增减样本,或在现有流程基础上添加新的分析步骤(如差异甲基化分析)。
- 清晰的参数管理 : 所有参数集中在
config.yaml
中,一目了然。
希望这篇详细的指南能帮助您在表观遗传学的研究道路上走得更远。