如何在Snakemake中为配置参数设置多组取值?
实现多组参数批量运行的方案
核心思路
将多组参数打包成字符串传入Snakefile,解析为参数组合列表后,通过expand遍历所有组合生成任务,规则通过wildcards捕获文件名中的参数值,自动适配每组参数运行。
修改后的Snakefile代码
import os grouphome = os.environ['GROUPHOME'] ISOLATES = [i for i in open(grouphome+'/isolates.txt').read().split('\n') if len(i) > 0] # 解析命令行传入的多组参数 param_sets = config.get("param_sets", "") # 拆分参数组并转换为整数元组,过滤空字符串 PARAM_COMBOS = [tuple(map(int, set_str.split(','))) for set_str in param_sets.split(';') if set_str.strip()] # 校验每组参数数量是否正确 assert all(len(combo) == 3 for combo in PARAM_COMBOS), "每组参数必须包含window_size、slide_size、mean_filter_size三个值" rule all: input: # 生成所有参数组合+样本的预处理文件 expand( grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/{isolate}.csv", isolate=ISOLATES, ws=[c[0] for c in PARAM_COMBOS], ss=[c[1] for c in PARAM_COMBOS], mfs=[c[2] for c in PARAM_COMBOS], allow_missing=False ), # 生成所有参数组合+样本的ptr文件 expand( grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/ptr/{isolate}/ptr.csv", isolate=ISOLATES, ws=[c[0] for c in PARAM_COMBOS], ss=[c[1] for c in PARAM_COMBOS], mfs=[c[2] for c in PARAM_COMBOS], allow_missing=False ), # 生成所有参数组合+样本的残差拟合值文件 expand( grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/ptr/{isolate}/residuals_fitted_values.csv", isolate=ISOLATES, ws=[c[0] for c in PARAM_COMBOS], ss=[c[1] for c in PARAM_COMBOS], mfs=[c[2] for c in PARAM_COMBOS], allow_missing=False ) rule coverage_preprocessing: input: input1= grouphome+"/data/{isolate}.fasta", input2= grouphome+"/max_coverage/{isolate}_maximum_coverage.csv" output: grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/{isolate}.csv" shell: """./preprocess_coverage.py -i {input.input2} -g {input.input1} -sw {wildcards.ss} -ws {wildcards.ws} -m mean -bm mean -bs {wildcards.mfs} -o {output}""" rule calc_bptr: input: input1= grouphome+"/data/{isolate}.fasta", input2= grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/{isolate}.csv" output: output1= grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/ptr/{isolate}/ptr.csv", output2= grouphome+"/preprocessed_coverage_{ws}_{ss}_{mfs}/ptr/{isolate}/residuals_fitted_values.csv", shell: """./calculate_bptr.py -i {input.input2} -b mean -s {wildcards.mfs} -g {input.input1} -f {output.output2} -o {output.output1}"""
命令行运行方式
直接通过param_sets参数传递多组参数,每组用分号分隔,组内三个参数用逗号分隔:
snakemake --config param_sets="1000000,10000,99;1000000,100,66" --cores=9
关键修改点说明
- 参数解析:将命令行传入的字符串拆分为多个参数组合元组,确保每组包含三个参数
- rule all扩展:使用
expand遍历所有参数组合和样本,生成所有需要的输出文件路径 - 规则适配:移除原规则中直接读取config单值的逻辑,改用
wildcards从输出文件名中捕获对应参数值,自动适配每组参数运行
内容的提问来源于stack exchange,提问作者Mary
相关产品推荐
相关产品推荐

