<p>Something like the following would work:</p>
<pre><code>rule bcftools_filter:
    input:
        bcf = &quot;/dir/{sample}_aligned_call.bcf&quot;,
    output:
        &quot;/dir/{sample}_aligned_call_filter.bcf&quot;
    params:
        regions=&quot;chr1,chr2&quot;
    threads: 20
    shell:
        &quot;bcftools view -Ob --threads {threads} -m2 -M2 --regions {params.regions} -v snps {input.bcf} -o {output}&quot;
</code></pre>
<p>You could also include the regions as part of the output file name, rather than specifying it as a param.</p>

CC BY-SA 4.0
2021-08-18T22:12:38.480Z
Would a simple `--regions \"chr1,chr2\"` in the shell command not work?
Ram RS
CC BY-SA 4.0
2021-08-18T22:13:57.737Z
You don't need to even quote it. However having it as a param allows you to set it elsewhere (e.g., the command line).
Devon Ryan
CC BY-SA 4.0
2021-08-18T19:06:59.737Z
2021-08-18T19:06:59.737Z
Devon Ryan
<p>I am creating a bioinformatics pipeline using Snakemake doing everying from adapter trimming of raw sequencing data in .fq to mapping, SNP calling and SNP filtering.</p>
<p>The issue i am having is when i try to filter specific chromosomes of the bcf files, using the --regions. The rule i have created below works and does create a filtered bcf file.</p>
<pre><code>rule bcftools_filter:
    input:
        bcf = &quot;/dir/{sample}_aligned_call.bcf&quot;,
    output:
        &quot;/dir/{sample}_aligned_call_filter.bcf&quot;
    threads: 20
    shell:
        &quot;bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} -o {output}&quot;
</code></pre>
<p>However i would like to add the --regions parameter to the bcftools command. I have tried the shell part of the rule to several things</p>
<pre><code>bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions &lt;chr1,chr2&gt; -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions {chr1,chr2} -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions {{chr1,chr2}} -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions {{chr1} {chr2}} 
 {{chr2}} -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions ['chr1','chr2'] -o {output}
</code></pre>
<p>But it doesn't work, and i am having trouble finding any suggestions to this specific issue.</p>
<p>I know it works if i just run bcftools filter in my command line, so using --regions should work.</p>
<p>Could anyone have any suggestions how to add --regions to the snakemake rule?</p>

CC BY-SA 4.0
2021-08-18T14:58:14.540Z
Have you tried `expand()` function? https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html?highlight=expand#the-expand-function
zorbax
CC BY-SA 4.0
2021-08-18T16:29:23.337Z
No i havent tried that, i didn't realize you could potentially use the expand() function for the shell part. But i will look into it
RAHenriksen
<snp><snakemake><bcftools>
Snakemake rule issue with bcftools view filtering
# Snakemake rule issue with bcftools view filtering

(Asked by: username_0 on 2021-08-18)

I am creating a bioinformatics pipeline using Snakemake doing everying from adapter trimming of raw sequencing data in .fq to mapping, SNP calling and SNP filtering.
The issue i am having is when i try to filter specific chromosomes of the bcf files, using the --regions. The rule i have created below works and does create a filtered bcf file.
rule bcftools_filter:
    input:
        bcf = "/dir/{sample}_aligned_call.bcf",
    output:
        "/dir/{sample}_aligned_call_filter.bcf"
    threads: 20
    shell:
        "bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} -o {output}"

However i would like to add the --regions parameter to the bcftools command. I have tried the shell part of the rule to several things
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions  -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions {chr1,chr2} -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions {{chr1,chr2}} -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions {{chr1} {chr2}} 
 {{chr2}} -o {output}
bcftools view -Ob --threads {threads} -m2 -M2 -v snps {input.bcf} --regions ['chr1','chr2'] -o {output}

But it doesn't work, and i am having trouble finding any suggestions to this specific issue.
I know it works if i just run bcftools filter in my command line, so using --regions should work.
Could anyone have any suggestions how to add --regions to the snakemake rule?

**Tags:** snp, snakemake, bcftools
> **Comments:**
> **username_1**: Have you tried `expand()` function? https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html?highlight=expand#the-expand-function  
> **username_0**: No i havent tried that, i didn't realize you could potentially use the expand() function for the shell part. But i will look into it  

---

## 1 answers

### Answer by username_2 on 2021-08-18. Score: 1

Something like the following would work:
rule bcftools_filter:
    input:
        bcf = "/dir/{sample}_aligned_call.bcf",
    output:
        "/dir/{sample}_aligned_call_filter.bcf"
    params:
        regions="chr1,chr2"
    threads: 20
    shell:
        "bcftools view -Ob --threads {threads} -m2 -M2 --regions {params.regions} -v snps {input.bcf} -o {output}"

You could also include the regions as part of the output file name, rather than specifying it as a param.

> **Comments:**
> **username_3**: Would a simple `--regions \"chr1,chr2\"` in the shell command not work?  
> **username_2**: You don't need to even quote it. However having it as a param allows you to set it elsewhere (e.g., the command line).  

---

