首页 > 解决方案 > 单独调用规则的变量并为特定规则添加独立环境

问题描述

我需要在集群中运行一个snakemake规则,因此对于某些规则,我需要一些工具和库来加载,而这些工具是独立/专有于其他规则的。在这种情况下,我如何在我的蛇形规则中指定这些。例如,由于rule score我需要module load r/3.5.1并且export R_lib =/user/tools/software目前,我在运行 snakemake 之前在命令行中分别运行这些行。但是,如果有办法在规则范围内做到这一点,那就太好了env

  1. 问题,

我有如下规则,

rule score:
    input:
        count=os.path.join(config['general']['paths']['outdir'], 'count_expression', '{sample}.tsv'),
        libsize=os.path.join(config['general']['paths']['outdir'], 'count_expression', '{sample}.size_tsv')
    params:
        result_dir=os.path.join(config['general']['paths']['outdir'], 'score'),
        cancertype=config['general']['paths']['cancertype'],
        sample_id=expand('{sample}',sample=samples['sample'].unique())
    output:
        files=os.path.join(config['general']['paths']['outdir'], 'score', '{sample}_bg_scores.tsv', '{sample}_tp_scores.tsv')
    shell:
        'mkdir -p {params.result_dir};Rscript {config[general][paths][tool]} {params.result_dir} {params.cancertype} {params.sample_id} {input.count} {input.libsize}'

我对上述代码片段的实际行为是:

shell:
        mkdir -p /cluster/user/snakemake_test/results_april30/score;Rscript /cluster/home/user/Projects/R_scripts/scoretool.R /cluster/user/snakemake_test/results_april30/score DMC GNMS4 MRT5T /cluster/projects/test/results/exp/MRT5T.tsv /cluster/projects/test/results/Exp/MRT5T.size.tsv

而预期的行为是:

shell:
        mkdir -p /cluster/user/snakemake_test/results_april30/score;Rscript /cluster/home/user/Projects/R_scripts/scoretool.R /cluster/user/snakemake_test/results_april30/score DMC MRT5T /cluster/projects/test/results/exp/MRT5T.tsv /cluster/projects/test/results/Exp/MRT5T.size.tsv

对于第二个样本,

shell:
        mkdir -p /cluster/user/snakemake_test/results_april30/score;Rscript /cluster/home/user/Projects/R_scripts/scoretool.R /cluster/user/snakemake_test/results_april30/score DMC GNMS4 /cluster/projects/test/results/exp/GNMS4.tsv /cluster/projects/test/results/Exp/GNMS4.ize.tsv

我需要变量 sample_d ['GNMS4', 'MRT5T']应该单独使用,而不是在一个 shell 命令行中一起使用。

标签: pythonfunctionwildcardexpandsnakemake

解决方案


关于您的第一个问题:您可以在规则部分中放置您喜欢的任何内容module loadexport命令shell

关于你的第二个问题,你可能不应该expandparams你的规则部分使用。实际上expand('{sample}',sample=samples['sample'].unique()),您并没有使用sample通配符的值,而是在sample['sample']. 您可能只需要wildcards.sample在 shell 命令的定义中使用而不是使用params元素。

如果要score根据 的可能值运行规则的多个实例,则sample需要使用另一个希望将 的输出score作为其输入的规则来“驱动”它。

请注意,为了提高可读性,您可以使用 python 的多行字符串(三引号)。

总而言之,你可以尝试这样的事情:

rule all:
    input:
        expand(
            os.path.join(
                config['general']['paths']['outdir'],
                'score',
                '{sample}_bg_scores.tsv',
                '{sample}_tp_scores.tsv'),
            sample=samples['sample'].unique())

rule score:
    input:
        count = os.path.join(
             config['general']['paths']['outdir'],
             'count_expression', '{sample}.tsv'),
        libsize = os.path.join(
             config['general']['paths']['outdir'],
             'count_expression', '{sample}.size_tsv')
    params:
        result_dir = os.path.join(config['general']['paths']['outdir'], 'score'),
        cancertype = config['general']['paths']['cancertype'],
    output:
        files = os.path.join(
            config['general']['paths']['outdir'],
            'score', '{sample}_bg_scores.tsv', '{sample}_tp_scores.tsv')
    shell:
        """
        module load r/3.5.1
        export R_lib =/user/tools/software
        mkdir -p {params.result_dir}
        Rscript {config[general][paths][tool]} {params.result_dir} {params.cancertype} {wildcards.sample} {input.count} {input.libsize}
        """

推荐阅读