This is a test version of Biostars. For the public version, visit https://www.biostars.org.
piping, for loop and bedtools

Hi,

I have a bed file with peaks merged from multiple samples ("merged.bed") and I want to use intersect bed to see where each peak intersects with the others.

This is all part of a bash script where the arguments are the name of the bed file from each sample.

In the first line brought here, I intersect the first argument with the merged file and then, using piping, I loop through the rest of the arguments and pipe them as well, where -a is stdin ("-").

bedtools intersect -a merged.bed -b $1 -c |
  for file in $@
  do 
    if [[ $file != $1 ]]
    then
      bedtools intersect -a - -b $file -c 
    fi
  done

but it seems that only the first iteration of the loop works, and I don't get as many intersection columns as there should be.

in an example result, the first 5 columns of each row are from the merged.bed file and then I only get results from the first and second arguments, even though there are 4.

chr1    3100293 3101103 H3K27ac_sertoli_REP2_peak_1     1       0       0
chr1    3131709 3132101 H3K27ac_granulosa_REP1_peak_1   1       1       0
chr1    3211080 3211681 H3K27ac_sertoli_REP2_peak_2     1       0       0
chr1    3211927 3214326 H3K27ac_sertoli_REP1_peak_1,H3K27ac_sertoli_REP2_peak_3 2       0       0
chr1    3215038 3215333 H3K27ac_sertoli_REP2_peak_4     1       0       0
chr1    3216776 3217083 H3K27ac_sertoli_REP2_peak_5     1       0       0

would love to get some help here, thanks a lot :)

bedtools bash

1 answer

When it comes to streams, it is important to understand that streams are not files.

Once a stream is seen (used) it is gone; you can't pass it to another source.

As the first command runs, it consumes the data from the standard input - nothing is left for the other files.

Log in to answer this question.