python - 使用 Snakemake 的分组样本每组单个输出文件

标签 python wildcard snakemake

我正在尝试编写一个 snakemake 管道来处理分成多个组的样本,每个组需要一个摘要文件。例如,样本划分如下:

Group 1:
    Sample 1
    Sample 2
Group 2: 
    Sample 3 
    Sample 4

每个样本都使用 bedtools 为每个样本生成一个输出文件。接下来,我需要对每个Group收集的样本进行分组层面的总结。一个简短的 snakemake 文件如下所示:

 rule intersect:
     input:
         bam = join('{group}','{sample}.bam'),
         reg_bed = join('{group}', 'region.bed')
     output:
         reg_intersect = join('{group}', '{sample}.intersect.bed')
    shell:
        'bedtools intersect -wa -wb -a {input.reg_bed} -b {input.bam} > {output.reg_intersect}'

rule aggregate:
    input:
        rules.interesect.output
    output:
        join('{group}','summary_stats.csv')
    shell:
        touch(join('{group}','summary_stats.csv'))
        #this would be a call to a python function that operates on all the output files to generate a single file

我收到投诉,通配符在输入和输出之间不一致(输入包含 {group} 和 {sample},而输出只有 {group}。我试过使用 expand() 但我必须包括组和值示例,并且示例是不可能的,因为它取决于组。

欢迎提出任何建议。

最佳答案

就像@Maarten-vd-Sande 所说的那样,最好的方法是使用输入函数获取聚合 规则中某个组的所有样本。

samplesInGroups = {"group1":["sample1","sample2"],"group2":["sample3","sample4"]}

def getSamplesInGroup(wildcards):
    return [join(wildcards.group,s+".intersect.bed") for s in samplesInGroups[wildcards.group]]

rule all:
     input:  expand("{group}/summary_stats.csv",group=list(samplesInGroups.keys()))

rule intersect:
     input:
         bam = join('{group}','{sample}.bam'),
         reg_bed = join('{group}', 'region.bed')
     output:
         reg_intersect = join('{group}', '{sample}.intersect.bed')
     shell:
        'bedtools intersect -wa -wb -a {input.reg_bed} -b {input.bam} > {output.reg_intersect}'

rule aggregate:
     input:
        getSamplesInGroup
     output:
        join('{group}','summary_stats.csv')
     shell:
        "python ... {input}"

关于python - 使用 Snakemake 的分组样本每组单个输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57484960/

相关文章:

python - 尝试使用 pip 为 python 下载 boto

snakemake - 如何在输入和输出中正确使用通配符

python - Snakemake 将多个命令行集成在一个规则中

python - 访问 Python 字典元素

python 的生命周期

java - 带菱形运算符的通配符

dictionary - 允许通配符查找的英语词典API

python - 如何在 snakemake 的扩展函数参数中使用通配符?

python - 如何将值分配给列表中的 python 字典

bash - 带通配符的文件循环