<p>While I have chosen ATpoint as the official answer, another possible syntax to use is</p>
<pre><code>bedtools intersect -a stdin -b blacklist.bed -v &lt; `(map to bam)` &gt; align_black.bam
</code></pre>
<p>The main issue with this syntax is that it obfuscates the direction of flow for the pipeline, piping step 2 after step 1 is better in the official answer.</p>

CC BY-SA 4.0
2021-04-19T14:04:09.990Z
2021-05-09T05:03:14.517Z
2021-05-09T05:03:14.517Z
Jeff
<p>Here's a way to skip conversion to BAM and pipe between tools, which performs the same set operations:</p>
<pre><code>$ bam mem ref fq1.fastq fq2.fastq | sam2bed - | bedops -e 1 - blacklist.bed &gt; blacklisted_reads.bed
</code></pre>
<p>With this toolkit, the hyphen (<code>-</code>) follows a Unix convention of serving as a placeholder for stdin.</p>

CC BY-SA 4.0
2021-04-16T21:48:40.363Z
2021-04-16T21:48:40.363Z
Alex Reynolds
<p>From the bedtools intersect man (for version 2.30.0):</p>
<pre><code>-a BAM/BED/GFF/VCF file “A”. Each feature in A is compared to B in search of overlaps. 
   Use “stdin” if passing A with a UNIX pipe.
</code></pre>
<p>The stdin part, however, does not apply to version 2.27.1. The help on this version gives no advice for piping.</p>
<pre><code>bam mem ref fq1.fastq fq2.fastq | samtools view -b | bedtools intersect -a stdin -b blacklist.bed -v &gt; align_black.sam
Error: unable to open file or unable to determine types for file stdin

- Please ensure that your file is TAB delimited (e.g., cat -t FILE).
- Also ensure that your file has integer chromosome coordinates in the
  expected columns (e.g., cols 2 and 3 for BED).
</code></pre>
<p>I also tried:</p>
<pre><code>(map to bam) | bedtools intersect -a &quot;stdin&quot; -b blacklist.bed -v &gt; align_black.sam
(map to bam) | bedtools intersect -a -b blacklist.bed -v &gt; align_black.sam
(map to bam) | bedtools intersect -b blacklist.bed -v &gt; align_black.sam
</code></pre>
<p>But these don't work either, either for the same or a different reason. Here is a site that tells me to use the above syntax: <a href="https://bedtools.readthedocs.io/en/latest/content/example-usage.html" rel="nofollow noreferrer">https://bedtools.readthedocs.io/en/latest/content/example-usage.html</a></p>
<p>What is the solution to this if I want to pipe everything together for my users?</p>

<bedtools>
Unexpected error using "stdin" in bedtools intersect as a piped command
# Unexpected error using "stdin" in bedtools intersect as a piped command

(Asked by: username_0 on 2021-04-16)

From the bedtools intersect man (for version 2.30.0):
-a BAM/BED/GFF/VCF file “A”. Each feature in A is compared to B in search of overlaps. 
   Use “stdin” if passing A with a UNIX pipe.

The stdin part, however, does not apply to version 2.27.1. The help on this version gives no advice for piping.
bam mem ref fq1.fastq fq2.fastq | samtools view -b | bedtools intersect -a stdin -b blacklist.bed -v > align_black.sam
Error: unable to open file or unable to determine types for file stdin

- Please ensure that your file is TAB delimited (e.g., cat -t FILE).
- Also ensure that your file has integer chromosome coordinates in the
  expected columns (e.g., cols 2 and 3 for BED).

I also tried:
(map to bam) | bedtools intersect -a "stdin" -b blacklist.bed -v > align_black.sam
(map to bam) | bedtools intersect -a -b blacklist.bed -v > align_black.sam
(map to bam) | bedtools intersect -b blacklist.bed -v > align_black.sam

But these don't work either, either for the same or a different reason. Here is a site that tells me to use the above syntax: https://bedtools.readthedocs.io/en/latest/content/example-usage.html
What is the solution to this if I want to pipe everything together for my users?

**Tags:** bedtools

---

## 2 answers

### Answer by username_0 on 2021-04-19. Score: 1

While I have chosen ATpoint as the official answer, another possible syntax to use is
bedtools intersect -a stdin -b blacklist.bed -v < `(map to bam)` > align_black.bam

The main issue with this syntax is that it obfuscates the direction of flow for the pipeline, piping step 2 after step 1 is better in the official answer.


---

### Answer by username_1 on 2021-04-16. Score: 1

Here's a way to skip conversion to BAM and pipe between tools, which performs the same set operations:
$ bam mem ref fq1.fastq fq2.fastq | sam2bed - | bedops -e 1 - blacklist.bed > blacklisted_reads.bed

With this toolkit, the hyphen (-) follows a Unix convention of serving as a placeholder for stdin.


---

