【问题标题】:Is it possible to add a conditional statement in snakemake's rule all?是否可以在snakemake的全部规则中添加条件语句?
【发布时间】:2020-11-21 23:22:49
【问题描述】:

我想使用奇异性运行多个名为 qc.smk 、dada2.smk、picrust2.smk的蛇文件。然后有一个名为longitudinal.smk 的蛇文件我想有条件地运行。例如,如果纵向数据是 正在使用。

# set vars

LONGITUDINAL = config['perform_longitudinal']

rule all:
  input:
    # fastqc output before trimming
    raw_html = expand("{scratch}/fastqc/{sample}_{num}_fastqc.html", scratch = SCRATCH, sample=SAMPLE_SET, num=SET_NUMS),
    raw_zip = expand("{scratch}/fastqc/{sample}_{num}_fastqc.zip", scratch = SCRATCH, sample=SAMPLE_SET, num=SET_NUMS),
    raw_multi_html = SCRATCH + "/fastqc/raw_multiqc.html",
    raw_multi_stats = SCRATCH + "/fastqc/raw_multiqc_general_stats.txt"

# there are many more files in rule all

##### setup singularity #####

singularity: "docker://continuumio/miniconda3"

##### load rules #####

include: "rules/qc.smk"
include: "rules/dada2.smk"
include: "rules/phylogeny.smk"
include: "rules/picrust2.smk"

if LONGITUDINAL == 'yes':
    include: 'rules/longitudinal.smk'
    print("Will perform a longitudinal analysis")
else:
    print("no longitudinal analysis")

上面的代码仅在我运行纵向数据集时才有效。但是,当我不运行纵向分析时,snakemake 会失败并说:

MissingInputException in line 70 of /mnt/c/Users/noahs/projects/tagseq-qiime2-snakemake-1/Snakefile:
Missing input files for rule all:

我想如果我能够添加一个类似的条件语句,就像我为我的外部蛇文件提供的那个,snakemake 不会因为不包括纵向蛇文件而吓坏我。

【问题讨论】:

    标签: python snakemake qiime


    【解决方案1】:

    您可以在rule all 之外定义您想要的输出列表(或字典),并将其提供给输入,类似这样的工作:

    myoutput = list()
    
    if condition_1 == True:
        myoutput.append("file_1.txt")
    if condition_2 == True:
        myoutput.append("file_2.txt")
    
    rule all:
        input:
            myoutput
    

    编辑:

    将myoutput作为规则all的输入中的第一个:

    rule all:
        input:
            myoutput,
            raw_html = "raw_html_path",
            raw_zip = "raw_zip_path"
    

    或将其命名,并将其放置在任何地方:

    rule all:
        input:
            raw_html = "raw_html_path",
            myoutput = myoutput,
            raw_zip = "raw_zip_path"
    

    在 Python(和 snakemake)中,命名位置参数总是在命名参数之前。

    【讨论】:

    • 感谢您的回复。我不确定我是否犯了错误,因为这种结构不适合我。我的所有输出都必须附加列表才能正常工作吗?我目前的所有输出文件都在我的规则中正常列出,并在我的其余输入文件下方添加了myoutput,并得到了这个错误:SyntaxError in line 178 of /mnt/c/Users/noahs/projects/tagseq-qiime2-snakemake-1/Snakefile: positional argument follows keyword argument 178 是我在规则中添加myoutput
    • 将myoutput列表移动到all: input:中的第一个,或者像all: input: optional = myoutput一样做某事。
    • 这仍然不适合我。 append 函数可能不喜欢我的文件的命名方式,尽管我的命名模式在snakemake 中工作正常。这是我尝试按照您的建议通过 append 函数的文件名示例。 myout.append("pw_diff = OUTPUTDIR + '/qiime2/asv/longitudinal/' + PROJ + ANALYSIS + 'pairwise-differences.qzv'")
    • 非常感谢!你是救生员。我能够通过首先在规则之外定义位置参数来使其工作。因为我需要将多个输入的一个列表添加到另一个输入列表中,所以我还必须使用扩展而不是附加。但在你的帮助下,我无法弄清楚。
    【解决方案2】:

    合并列表形式展开语句的解决方案:

    我使用配置文件将语句传递给 Snakefile

    ## Config.yml ##
    # longitudinal analysis
    perform_longitudinal: 'yes' # yes for longitudinal analysis 
    

    当在配置中输入“yes”时,Snakemake 将在 rule all 中包含附加变量并运行附加 Snakefile 以生成这些文件。最终有多个 Snakefile,所以我使用奇异性让 Snakemake 知道规则所有输入文件都适用于所有 6 个 Snakefile。

    ## Snakefile ##
    
    configfile: "config.yaml"
    
    LONGITUDINAL = config['perform_longitudinal']
    
     # rule all input files
     raw_html=file.txt, 
     raw_zip=file.txt,
     raw_multi_htmt=file.txt,
     raw_multi_stats=file.txt,
     Longitudinal_analaysis_files=file.txt
    
    # rule all files excluding longitudinal analysis
    rule_all_input_list=['raw_html','raw_zip','raw_multi_htmt','raw_multi_stats']
    
    #longitudinal analysis files
    rule_all_longitudinal_input=['Longitudinal_analaysis_files']
    
    if LONGITUDINAL == 'yes':
    
        rule_all_input_list.extend(rule_all_longitudinal_input)
    
    # conditionally add Snakefile to workflow
        include: 'rules/longitudinal.smk'
    
        print("Will perform a longitudinal analysis")
    
    else:
        print("no longitudinal analysis")
    
    
    rule all:
        input:
            data = rule_all_input_list
    
    ##### setup singularity #####
    
    # this container defines the underlying OS for each job when using the workflow
    # with --use-conda --use-singularity
    singularity: "docker://continuumio/miniconda3"
    
    ##### load rules #####
    
    include: "rules/qc.smk"
    include: "rules/dada2.smk"
    include: "rules/phylogeny.smk"
    include: "rules/picrust2.smk"
    include: "rules/differential.smk"
    

    我在 GitHub https://github.com/nasiegel88/tagseq-qiime2-snakemake-1 上有一个不太简化的版本

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2011-11-21
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2020-07-23
      相关资源
      最近更新 更多