# config.yaml # ========== 版本控制 ========== versions: snakemake: "9.1.10" bwa: "25.3.1" gatk: "1.9.5" fastp: "0.20.1" samtools: "1.134" # ========== 样本自动发现 ========== sample_discovery: raw_data_dir: "data" r1_pattern: "*_1.fq.gz" # 匹配R1文件的通配符 sample_regex: "(.*)_L/d+_/d+_/d+.fq.gz" # 从文件名提取样本ID sample_discovery: raw_data_dir: "data" r1_pattern: "*_2.fq.gz" # 匹配R2文件的通配符 sample_regex: "(.*)_L/d+_/d+_/d+.fq.gz" # 从文件名提取样本ID # ========== 参考基因组 ========== reference: url: "https://api.ncbi.nlm.nih.gov/datasets/v2/genome/accession/GCF_000001405.25/download" local_dir: "resources/reference" # ========== 分析参数 ========== params: fastp: adapter: "AGATCGGAAGAGC" quality_cutoff: 20 bwa: seed_length: 32配置文件生成一个snakefile定位文件
时间: 2025-06-05 08:44:33 浏览: 14
### 根据 Config.yaml 配置文件生成 Snakemake Snakefile 实现样本数据和参考基因组路径定位
以下是一个完整的 Snakemake 文件模板,用于读取 `config.yaml` 配置文件中的样本列表和参考基因组路径,并实现正确的路径定位。
#### 前提条件
确保已经按照引用描述完成了基础软件的安装[^1]。此外,配置文件应包含样本信息、原始数据路径、处理后的数据存储路径以及参考基因组的位置。
---
### 示例 `config.yaml` 配置文件
```yaml
samples:
- sampleA
- sampleB
paths:
raw_data: data/raw/
processed_data: results/processed/
reference_genome: /path/to/reference/genome.fa
```
---
### 示例 Snakemake Snakefile
```python
# 加载配置文件
configfile: "config.yaml"
# 定义全局变量
REFERENCE_GENOME = config["reference_genome"]
# 规则入口
rule all:
input:
expand("{processed_dir}{sample}_aligned.bam",
sample=config["samples"],
processed_dir=config["paths"]["processed_data"])
# FastQC 质量评估
rule fastqc:
input:
"{raw_dir}{sample}.fastq.gz"
output:
"{processed_dir}{sample}_fastqc.zip",
"{processed_dir}{sample}_fastqc.html"
log:
"{processed_dir}{sample}_fastqc.log"
params:
raw_dir=config["paths"]["raw_data"],
processed_dir=config["paths"]["processed_data"]
shell:
"fastqc {input} "
"--outdir={params.processed_dir} "
">{log} 2>&1"
# 数据修剪 (Trim Galore)
rule trim_galore:
input:
rules.fastqc.output[0]
output:
"{processed_dir}{sample}_trimmed.fq.gz"
log:
"{processed_dir}{sample}_trim_galore.log"
params:
processed_dir=config["paths"]["processed_data"]
shell:
"trim_galore --gzip --fastqc_args '--noextract' {input} "
"-o {params.processed_dir} "
">{log} 2>&1"
# BWA 比对
rule bwa_mem:
input:
trimmed="{processed_dir}{sample}_trimmed.fq.gz",
reference=REFERENCE_GENOME
output:
temp("{processed_dir}{sample}_unsorted.sam")
log:
"{processed_dir}{sample}_bwa.log"
params:
processed_dir=config["paths"]["processed_data"]
shell:
"bwa mem -M -t 8 {input.reference} {input.trimmed} "
"> {output} "
"2> {log}"
# SAM 转 BAM 并排序
rule sam_to_bam:
input:
"{processed_dir}{sample}_unsorted.sam"
output:
"{processed_dir}{sample}_aligned.bam"
log:
"{processed_dir}{sample}_samtools.log"
params:
processed_dir=config["paths"]["processed_data"]
shell:
"samtools view -@ 8 -bhS {input} | "
"samtools sort -@ 8 -T {wildcards.sample} -O bam - > {output} "
"2> {log}"
```
---
### 关键点解析
1. **Config File Integration**
- 使用 `configfile: "config.yaml"` 将外部 YAML 文件引入到 Snakefile 中。
- 所有路径和样本信息均从配置文件中提取,从而增强灵活性和可维护性[^1]。
2. **Sample Expansion**
- 利用 `expand()` 函数动态生成目标文件路径,支持批量处理多个样本的数据[^1]。
3. **Log Management**
- 每个规则的日志输出被重定向至特定目录下的 `.log` 文件,方便后续调试和监控进展[^2]。
4. **Reference Genome Handling**
- 参考基因组路径由配置文件提供 (`REFERENCE_GENOME`),并传递给需要它的规则(如 BWA 比对阶段)。
5. **Intermediate Files Cleanup**
- 对于临时文件(如未排序的 SAM 文件),标记为 `temp()` 类型以允许 Snakemake 自动清理它们[^1]。
---
###
阅读全文
相关推荐


















