Snakemake规则重复申请内存求助:如何规避默认设置干扰
问题描述
我发现所有Snakemake规则都会重复申请内存,一次是低于请求值的mem_mb,另一次是实际设置的mem_gb。将规则作为localrule运行时速度更快,请问如何确保默认设置不会产生干扰?
资源信息
resources: mem_mb=100, disk_mb=8620, tmpdir=/tmp/pop071.54835, partition=h24, qos=normal, mem_gb=100, time=120:00:00
规则定义
rule bwa_mem2_mem: input: R1 = "data/results/qc/{species}.{population}.{individual}_1.fq.gz", R2 = "data/results/qc/{species}.{population}.{individual}_2.fq.gz", R1_unp = "data/results/qc/{species}.{population}.{individual}_1_unp.fq.gz", R2_unp = "data/results/qc/{species}.{population}.{individual}_2_unp.fq.gz", idx= "data/results/genome/genome", ref = "data/results/genome/genome.fa" output: bam = "data/results/mapped_reads/{species}.{population}.{individual}.bam", log: bwa ="logs/bwa_mem2/{species}.{population}.{individual}.log", sam ="logs/samtools_view/{species}.{population}.{individual}.log", benchmark: "benchmark/bwa_mem2_mem/{species}.{population}.{individual}.tsv", resources: time = parameters["bwa_mem2"]["time"], mem_gb = parameters["bwa_mem2"]["mem_gb"], params: extra = parameters["bwa_mem2"]["extra"], tag = compose_rg_tag, threads: parameters["bwa_mem2"]["threads"], shell: "bwa-mem2 mem -t {threads} -R '{params.tag}' {params.extra} {input.idx} {input.R1} {input.R2} | " "samtools sort -l 9 -o {output.bam} --reference {input.ref} --output-fmt CRAM -@ {threads} /dev/stdin 2> {log.sam}"
配置文件
cluster: mkdir -p logs/{rule} && # change the log file to logs/slurm/{rule} sbatch --partition={resources.partition} --time={resources.time} --qos={resources.qos} --cpus-per-task={threads} --mem={resources.mem_gb} --job-name=smk-{rule}-{wildcards} --output=logs/{rule}/{rule}-{wildcards}-%j.out --parsable # Required to pass job IDs to scancel default-resources: - partition=h24 - qos=normal - mem_gb=100 - time="04:00:00" restart-times: 3 max-jobs-per-second: 10 max-status-checks-per-second: 1 local-cores: 1 latency-wait: 60 jobs: 100 keep-going: True rerun-incomplete: True printshellcmds: True scheduler: greedy use-conda: True # Required to run with local conda enviroment cluster-status: status-sacct.sh # Required to monitor the status of the submitted jobs cluster-cancel: scancel # Required to cancel the jobs with Ctrl + C cluster-cancel-nargs: 50
解决方案
1. 明确重复内存资源的来源
mem_mb是Snakemake内置的默认资源(默认值100MB),主要用于本地调度器的内存限制;你自定义的mem_gb是给集群调度器(如Slurm)使用的资源参数。两者同时存在是因为Snakemake会自动注入默认资源,除非你明确覆盖。
2. 同步mem_mb与mem_gb数值
在配置文件的default-resources中添加动态规则,让mem_mb自动跟随mem_gb的数值,确保两者一致:
default-resources: - partition=h24 - qos=normal - mem_gb=100 - mem_mb=lambda wildcards, resources: resources.mem_gb * 1024 - time="04:00:00"
这样所有规则的mem_mb会自动换算为mem_gb对应的MB数,不再出现100MB的默认值。
3. 确认集群调度仅使用mem_gb
你的cluster配置已经正确使用--mem={resources.mem_gb},这一步无需修改,集群只会按照mem_gb的数值申请内存,mem_mb不会干扰集群调度逻辑。
4. 优化本地运行速度
本地运行更快的核心原因是:默认100MB的mem_mb会让本地调度器误判规则内存需求,导致资源调度受限或等待。同步mem_mb与mem_gb后,本地调度器能识别规则实际需要的内存量,不会再限制运行速度。
5. 规则级别单独设置(可选)
如果部分规则需要自定义mem_mb,可在规则的resources块中明确指定:
resources: time = parameters["bwa_mem2"]["time"], mem_gb = parameters["bwa_mem2"]["mem_gb"], mem_mb = parameters["bwa_mem2"]["mem_gb"] * 1024
内容的提问来源于stack exchange,提问作者andapo
相关产品推荐
相关产品推荐

