Snakemake: remove output file
<p>I don't see how to use a Snakemake rule to remove a Snakemake output file that has become useless.</p>

<p>In concrete terms, I have a rule <code>bwa_mem_sam</code> that creates a file named <code>{sample}.sam</code>.
I have this other rule, <code>bwa_mem_bam</code> that creates a file named <code>{sample.bam}</code>.</p>

<p>Has the two files contain the same information in different formats, I'd like to remove the first one cannot succeed doing this.</p>

<p>Any help would be very much appreciated.
Ben.</p>

<pre><code>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} &gt; {output} 2&gt; {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} &gt; {output}"
</code></pre>