Snakemake 是一个用于工作流管理的工具,特别适合生物信息学领域。它基于 Python,通过定义规则(rules)来描述任务及其依赖关系,使复杂的数据分析流程更易管理和自动化。
snakemake的主要特点
1)使用python语法,规则清晰易读。
2)自动化处理任务以来,确保任务正确顺序执行
3)支持集群和云环境等大规模计算
4)结果可重复,规则可复用,可以用来构建复杂的流程。
snakemake的安装
最直接简单的方式是用pip安装
python -m venv snakemake_env #创建环境
source snakemake_env/bin/activate #激活环境
pip install snakemake #安装snakemake
snakemake --version #如果此时报错AttributeError: module 'pulp' has no attribute 'list_solvers'是由于pulp版本不匹配,运行pip install pulp==2.6.0然后就好了
snakemake --version #这样就是下载好了
7.32.4
snakemake的基本概念
1. 规则(Rule):规则是 Snakemake 的核心概念,用于定义任务(task)。每个规则描述了一个任务所需的输入文件、输出文件以及执行任务的命令。
rule rule_name: #定义一个名称为rule的规则
input: #指定任务的输入文件(单个或多个文件)
"input_file.txt"
output: #指定任务的输出文件(单个或多个文件)
"output_file.txt"
shell: #定义生成输出文件的命令。可以使用 {input} 和 {output} 占位符来引用输入和输出文件。
"command_to_generate_output {input} {output}"
2. 工作流(Workflow):工作流由多个规则组成,描述了任务之间的依赖关系。Snakemake 会根据输出文件的需求自动解析规则之间的依赖关系,并按照正确的顺序执行任务。
工作流的特点:1)Snakemake 自动解析依赖关系,无需手动指定任务顺序(自动化)。2)每个规则可以独立定义,便于复用和维护(模块化)。3)可以通过添加新规则来扩展工作流(可扩展)。
rule all: #默认规则,指定了工作流的最终目标文件results/final_report.txt
input:
"results/final_report.txt"
rule align:
input:
"data/{sample}.fastq"
output:
"results/{sample}.bam"
shell:
"bwa mem reference.fa {input} > {output}"
rule report:
input:
"results/{sample}.bam"
output:
"results/final_report.txt"
shell:
"samtools stats {input} > {output}" ##Snakemake 会自动解析依赖关系,先执行 align 规则生成 .bam 文件,再执行 report 规则生成最终报告。
3. 通配符(Wildcards):通配符用于动态生成文件名或路径,使规则更加灵活和通用。
rule process:
input:
"data/{sample}.fastq"
output:
"results/{sample}.bam"
shell:
"bwa mem reference.fa {input} > {output}"
#{sample} 是一个通配符,可以匹配不同的文件名(如 sample1.fastq、sample2.fastq)。
#Snakemake 会根据目标文件自动解析通配符的值。
4. 配置文件(Config File):配置文件用于参数化工作流,使工作流更加灵活和可配置。通过配置文件,可以将硬编码的参数(如文件路径、参数值)提取出来,便于修改和复用。ps:配置文件通常是YAML或者JSON文件,在snakemake中可以通过config对象访问配置文件中的参数。
samples: ##yaml文件
- sample1
- sample2
reference: "reference.fa"
configfile: "config.yaml" #制定配置文件的路径
rule align:
input:
"data/{sample}.fastq"
output:
"results/{sample}.bam"
shell:
"bwa mem {config[reference]} {input} > {output}"
rule all:
input:
expand("results/{sample}.bam", sample=config["samples"])
#通过 config 对象访问配置文件中的参数
#expand:根据配置文件中的 samples 列表动态生成目标文件。
一个完整的snakemake流程通常包括哪些
snakemake流程的核心文件
1.snakefile:用于定义规则(rules和任务依赖关系),示例如下:
rule all:
input: "results/final_output.txt"
rule process:
input: "data/{sample}.txt"
output: "results/{sample}.out"
shell: "command {input} > {output}"
2. 配置文件(config.yaml或config.json),用于存储参数(如文件路径、样本列表等),示例如下
samples: [sample1, sample2]
reference: "genome.fa"
3. 输入数据文件:通常存放在data里。
4. 环境文件(env.yaml 或environment.yml),定义conda的依赖环境,示例如下:
channels: [bioconda, conda-forge]
dependencies: [bwa, samtools, snakemake]
5. 输出目录:存储输出结果文件
snakemake流程的非必选文件
1. 日志文件(log);
2. 集群配置文件(cluster.yaml):定义集群作用提交参数(比如内存、cpu等);
3. 脚本文件(scripts/):复杂任务的代码(python或者R脚本)
4. README文件:提供工作流的使用说明
一些微生物和单细胞领域的snakemake流程参考
-
一个用于单细胞 RNA 测序数据分析的 Snakemake 流程。支持从原始数据到细胞聚类、差异表达分析。使用 Seurat 和 Scanpy 等流行工具。Snakemake Single-Cell RNA-Seq
https://github.com/snakemake-workflows/single-cell-rna-seq
-
一个模块化的 Snakemake 流程集合,支持多种单细胞和 bulk 测序数据分析。支持单细胞 RNA-Seq、ATAC-Seq、ChIP-Seq 等。提供预配置的分析流程。SnakePipes
https://github.com/maxplanck-ie/snakepipes
-
一个简单的单细胞 RNA-Seq 分析流程,适合初学者学习。使用 Scanpy 进行数据分析和可视化。提供详细的注释和示例数据。Single-Cell Snakemake
https://github.com/snakemake-workflows/single-cell-snakemake
-
一个专门针对宏基因组分析的 Snakemake 工作流集合。Snakemake Metagenomics
https://github.com/snakemake-workflows/metagenomics
-
一个用于宏基因组分析的 Snakemake 流程,支持从原始数据到分箱和注释。ATLAS
https://github.com/metagenome-atlas/atlas
-
基于nextflow和snakemake的宏基因组流程,包括组装、分箱和注释:nf-core/mag
https://github.com/nf-core/mag