2016-12-06 44 views
1

我不明白如何使用Snakemake規則刪除一個無用的Snakemake輸出文件。Snakemake:刪除輸出文件

具體而言,我有一個規則bwa_mem_sam,它創建一個名爲{sample}.sam的文件。 我有這個其他規則,bwa_mem_bam創建一個名爲{sample.bam}的文件。

這兩個文件包含不同格式的相同信息,我想刪除第一個不能成功做到這一點。

任何幫助將非常感激。 本。

rule bwa_mem_map: 
    input: 
     sam="{sample}.sam", 
     bam="{sample}.bam" 
    shell: 
     "rm {input.sam}" 

# Convert SAM to BAM. 
rule bwa_mem_map_bam: 
    input: 
     rules.sam_to_bam.output 


# Use bwa mem to map reads on a reference genome. 
rule bwa_mem_map_sam: 
    input: 
     reference=reference_genome(), 
     index=reference_genome_index(), 
     fastq=lambda wildcards: config["units"][SAMPLE_TO_UNIT[wildcards.sample]], 
    output: 
     "mapping/{sample}.sam" 
    threads: 12 
    log: 
     "mapping/{sample}.log" 
    shell: 
     "{BWA} mem -t {threads} {input.reference} {input.fastq} > {output} 2> {log} "\ 
     "|| (rc=$?; cat {log}; exit $rc;)" 


rule sam_to_bam: 
    input: 
     "{prefix}.sam" 
    output: 
     "{prefix}.bam" 
    threads: 8 
    shell: 
     "{SAMTOOLS} view --threads {threads} -b {input} > {output}" 

回答

1

你不需要一個規則來刪除你的SAM文件。只是標誌着「bwa_mem_map_sam」規則的輸出中的SAM文件作爲臨時:

rule bwa_mem_map_sam: 
    input: 
     reference=reference_genome(), 
     index=reference_genome_index(), 
     fastq=lambda wildcards: config["units"][SAMPLE_TO_UNIT[wildcards.sample]], 
    output: 
     temp("mapping/{sample}.sam") 
    threads: 12 
    log: 
     "mapping/{sample}.log" 
    shell: 
     "{BWA} mem -t {threads} {input.reference} {input.fastq} > {output} 2> {log} "\ 
     "|| (rc=$?; cat {log}; exit $rc;)" 

就不需要臨時文件了(即:不能用作任何其他規則輸入),它將被snakemake去除。

編輯後評論: 如果我理解正確,您的陳述「如果用戶要求sam ...」意味着sam文件被放入目標規則。如果是這種情況,那麼只要目標規則的輸入包含sam文件,文件將不會被刪除(我猜)。如果bam文件放在目標規則(而不是sam)中,則它將被刪除。

另一種方式是這樣的:

rule bwa_mem_map: 
    input: 
     sam="{sample}.sam", 
     bam="{sample}.bam" 
    output: 
     touch("{sample}_samErased.txt") 
    shell: 
     "rm {input.sam}" 

,並在目標規則索要 「{}樣品_samErased.txt」。

+0

謝謝您的回答。我想到了這一點,但我認爲在建立一個完整的圖書館的背景下它不那麼靈活。基本上我認爲每個規則都應該是最小的,應該實施自治規則來增加更多的功能。因此,在這種情況下,我認爲可能需要使用SAM輸出,這就是爲什麼我想保留能夠請求SAM的選項,並且如果用戶要求提供BAM,那麼SAM將被刪除。 – blaurent

+0

@blaurent不用客氣。如果它能解決您的問題,您可以將其標記爲已接受! –

+0

抱歉我們的信息重疊。 – blaurent

0

根據上面的註釋,你想問用戶他是否想要一個山姆或巴姆輸出。

你可以使用它作爲一個配置參數:

snakemake --config output_format=sam

然後你使用這種Snakefile:

samples = ['A','B'] 

rule all: 
    input: 
     expand('{sample}.mapped.{output_format}', sample=samples, output_format=config['output_format']) 

rule bwa: 
    input: '{sample}.fastq' 
    output: temp('{sample}.mapped.sam') 
    shell: 
     """touch {output}""" 
rule sam_to_bam: 
    input: '{sample}.mapped.sam' 
    output: '{sample}.mapped.bam' 
    shell: 
     """touch {output}"""