Snakemake-无法根据输出文件确定输入文件中的通配符文件、通配符、根据、Snakemake

2023-09-04 01:40:26 作者:我要向前进

我对Snakemaker非常陌生,也不太会说Python(抱歉,这可能是一个非常基本的愚蠢问题):

我目前正在构建一个管道来使用atlas分析一组bamfile。这些bamfile位于不同的文件夹中,不应移动到公共文件夹中。因此,我决定提供一个示例列表,如下所示(这只是一个示例,实际上示例可能位于完全不同的驱动器上):

Sample     Path
Sample1    /some/path/to/my/sample/
Sample2    /some/different/path/
怎么删除win8更新失败

并使用:

将其加载到我的config.yaml中
sample_file: /path/to/samplelist/samplslist.txt

现在转到我的Snakefile:

import pandas as pd

#define configfile with paths etc.
configfile: "config.yaml"

#read-in dataframe and define Sample and Path
SAMPLES = pd.read_table(config["sample_file"])
BAMFILE = SAMPLES["Sample"]
PATH = SAMPLES["Path"]

rule all:
    input:
        expand("{path}{sample}.summary.txt", zip, path=PATH, sample=BAMFILE)

#this works like a charm as long as I give the zip-function in the rules 'all' and 'summary':

rule indexBam:
    input: 
        "{path}{sample}.bam"
    output:
        "{path}{sample}.bam.bai"
    shell:
        "samtools index {input}"

#this following command works as long as I give the specific folder for a sample instead of {path}.
rule bamdiagnostics:
    input:
        bam="{path}{sample}.bam",
        bai=expand("{path}{sample}.bam.bai", zip, path=PATH, sample=BAMFILE)
    params:
        prefix="analysis/BAMDiagnostics/{sample}"   
    output:
        "analysis/BAMDiagnostics/{sample}_approximateDepth.txt",
        "analysis/BAMDiagnostics/{sample}_fragmentStats.txt",
        "analysis/BAMDiagnostics/{sample}_MQ.txt",
        "analysis/BAMDiagnostics/{sample}_readLength.txt",
        "analysis/BAMDiagnostics/{sample}_BamDiagnostics.log"
    message:
        "running BamDiagnostics...{wildcards.sample}"
    shell:
        "{config[atlas]} task=BAMDiagnostics bam={input.bam} out={params.prefix} logFile={params.prefix}_BamDiagnostics.log verbose"

rule summary:
    input:
        index=expand("{path}{sample}.bam.bai", zip, path=PATH, sample=BAMFILE),
        bamd=expand("analysis/BAMDiagnostics/{sample}_approximateDepth.txt", sample=BAMFILE)
    output:
        "{path}{sample}.summary.txt"
    shell:
        "echo -e '{input.index} {input.bamd}"

我收到错误

路径/to/my/Snakefile第28行的WildcardError: 无法从输出文件确定输入文件中的通配符: ‘路径’

有人能帮我吗? -我试图用join或创建输入函数来解决这个问题,但我认为我只是不够熟练,无法看到我的错误... -我猜问题是,我的摘要规则不包含带有bam诊断-输出{path}的Tuplet(因为输出在其他地方),并且无法连接到输入文件或更多... -扩展我对bamtugstics的输入-规则使代码正常工作,但当然,将每个样本输入到每个样本输出,并造成巨大的混乱: In this case, both bamfiles are used for the creation of each outputfile. This is wrong as the samples AND the output are to be treated independently.

推荐答案

根据图集文档,您似乎需要为每个样本分别运行每个规则,这里的复杂之处在于每个样本位于不同的路径中。

我修改了您的脚本以适用于上述情况(请参阅DAG)。修改了脚本开头的变量以使其更有意义。出于演示目的删除了config,并使用了pathlib库(而不是os.path.join)。pathlib不是必需的,但它帮助我保持理智。已修改外壳命令以避免config

import pandas as pd
from pathlib import Path

df = pd.read_csv('sample.tsv', sep='	', index_col='Sample')
SAMPLES = df.index
BAM_PATH = df["Path"]
# print (BAM_PATH['sample1'])

rule all:
    input:
        expand("{path}{sample}.summary.txt", zip, path=BAM_PATH, sample=SAMPLES)


rule indexBam:
    input:
        str( Path("{path}") / "{sample}.bam")
    output:
        str( Path("{path}") / "{sample}.bam.bai")
    shell:
        "samtools index {input}"

#this following command works as long as I give the specific folder for a sample instead of {path}.
rule bamdiagnostics:
    input:
        bam = lambda wildcards: str( Path(BAM_PATH[wildcards.sample]) / f"{wildcards.sample}.bam"),
        bai = lambda wildcards: str( Path(BAM_PATH[wildcards.sample]) / f"{wildcards.sample}.bam.bai"),
    params:
        prefix="analysis/BAMDiagnostics/{sample}"
    output:
        "analysis/BAMDiagnostics/{sample}_approximateDepth.txt",
        "analysis/BAMDiagnostics/{sample}_fragmentStats.txt",
        "analysis/BAMDiagnostics/{sample}_MQ.txt",
        "analysis/BAMDiagnostics/{sample}_readLength.txt",
        "analysis/BAMDiagnostics/{sample}_BamDiagnostics.log"
    message:
        "running BamDiagnostics...{wildcards.sample}"
    shell:
        ".atlas task=BAMDiagnostics bam={input.bam} out={params.prefix} logFile={params.prefix}_BamDiagnostics.log verbose"

rule summary:
    input:
        bamd = "analysis/BAMDiagnostics/{sample}_approximateDepth.txt",
        index = lambda wildcards: str( Path(BAM_PATH[wildcards.sample]) / f"{wildcards.sample}.bam.bai"),
    output:
        str( Path("{path}") / "{sample}.summary.txt")
    shell:
        "echo -e '{input.index} {input.bamd}"