如何在snakemake run指令下传递一个函数

时间:2017-07-21 20:09:12

标签: snakemake

我正在使用snakemake构建工作流程,并希望将其中一个规则回收到两个不同的输入源。输入源可以是source1或source1 + source2,根据输入,输出目录也会有所不同。由于在同一规则中这很复杂,我不想创建完整规则的副本,我想创建两个具有不同输入/输出的规则,但运行相同的命令。

是否有可能使这项工作?我正确地解决了DAG问题,但作业没有通过群集(ERROR : bamcov_cmd not defined)。 下面的示例(两个规则最后都使用相同的命令):

这是命令

def bamcov_cmd():
return( (deepTools_path+"bamCoverage " +
            "-b {input.bam} " +
            "-o {output} " +
            "--binSize {params.bw_binsize} " +
            "-p {threads} " +
            "--normalizeTo1x {params.genome_size} " +
            "{params.read_extension} " +
            "&> {log}") )

这是规则

rule bamCoverage:
input:
    bam = file1+"/{sample}.bam",
    bai = file1+"/{sample}.bam.bai"
output:
    "bamCoverage/{sample}.filter.bw"
params:
    bw_binsize = bw_binsize,
    genome_size = int(genome_size),
    read_extension = "--extendReads"
log:
    "bamCoverage/logs/bamCoverage.{sample}.log"
benchmark:
    "bamCoverage/.benchmark/bamCoverage.{sample}.benchmark"
threads: 16
run:
    bamcov_cmd()

这是可选的rule2

rule bamCoverage2:
input:
    bam = file2+"/{sample}.filter.bam",
    bai = file2+"/{sample}.filter.bam.bai"
output:
    "bamCoverage/{sample}.filter.bw"
params:
    bw_binsize = bw_binsize,
    genome_size = int(genome_size),
    read_extension = "--extendReads"
log:
    "bamCoverage/logs/bamCoverage.{sample}.log"
benchmark:
    "bamCoverage/.benchmark/bamCoverage.{sample}.benchmark"
threads: 16
run:
    bamcov_cmd()

1 个答案:

答案 0 :(得分:0)

你在python中提到了什么。 这取决于你在文件中有JUST python代码,还是python和Snakemake。 我将首先回答这个问题,然后我会有一个跟进回复,因为我希望你以不同的方式进行设置,所以你不必这样做。

只是Python:

 from fileContainingMyBamCovCmdFunction import bamcov_cmd

 rule bamCoverage:
 ...
     run:
         bamcov_cmd()

在视觉上,看看我是如何在此文件中执行此操作,以引用对buildHeader和buildSample的访问。这些文件由Snakefile调用。它应该对你有用。 https://github.com/LCR-BCCRC/workflow_exploration/blob/master/Snakemake/modules/py_buildFile/buildFile.py

编辑2017-07-23 - 更新以下代码段以反映用户评论

Snakemake和Python:

 include: "fileContainingMyBamCovCmdFunction.suffix"

 rule bamCoverage:
 ...
     run:
         shell(bamcov_cmd())

编辑结束

如果该功能真的特定于bamCoverage调用,如果您愿意,可以将其放回规则中。这意味着它不会在其他地方被调用,这可能是真的。 使用'。'注释文件时要小心。符号,我使用'_',因为我发现通过这种方式防止创建循环依赖更容易。 此外,如果您最终单独留下这两个规则,最终可能会出现歧义错误。 http://snakemake.readthedocs.io/en/latest/snakefiles/rules.html?highlight=ruleorder#handling-ambiguous-rules 在可能的情况下,最佳做法是使规则生成唯一的输出。

至于替代方案,请考虑设置这样的代码吗?

from subprocess import call


rule all:
    input:
          "path/to/file/mySample.bw"
          #OR
          #"path/to/file/mySample_filtered.bw"


bamCoverage:
input:
    bam = file1+"/{sample}.bam",
    bai = file1+"/{sample}.bam.bai"
output:
    "bamCoverage/{sample}.bw"
params:
    bw_binsize = bw_binsize,
    genome_size = int(genome_size),
    read_extension = "--extendReads"
log:
    "bamCoverage/logs/bamCoverage.{sample}.log"
benchmark:
    "bamCoverage/.benchmark/bamCoverage.{sample}.benchmark"
threads: 16
run:
    callString= deepTools_path + "bamCoverage " \
            + "-b " + wilcards.input.bam \
            + "-o " + wilcards.output \
            + "--binSize " str(params.bw_binsize) \
            + "-p " + str({threads}) \
            + "--normalizeTo1x " + str(params.genome_size) \
            + " " + str(params.read_extension) \
            + "&> " + str(log)
    call(callString, shell=True)


rule filterBam:
input:
    "{pathFB}/{sample}.bam"
output:
    "{pathFB}/{sample}_filtered.bam"
run:
    callString="samtools view -bh -F 512 " + wildcards.input \
    + ' > ' + wildcards.output

    call(callString, shell=True)

思想?