This is a test version of Biostars. For the public version, visit https://www.biostars.org.
bedtools intersect multiple files using for loop

I am trying to do a bedtools intersect. Anyone see what is wrong with my bash code here?

#!/bin/bash

dir=$(pwd)
query=$dir/ga4k_beds
pgs=$dir/pgs_beds

for file in $query/*; do 
   bedtools intersect -wa -wb -a $file -b $pgs/* -c -C -sorted -filenames
done > $(basename $file .ext).txt

My "query" directory contains many files which need to each be iteratively queried against many files in "pgs" directory using the package "bedtools" and command "intersect", which allows an asterisk for file -b to cycle through multiple files. I am not sure if the loop is causing issues with the command or if bedtools intersect with "*" usage is not clear enough -- couldn't find examples.

bash returns:

command not found 7: /*. Exiting.e to open file /Users/..
bedtools bash

You should use 2 for loops one for each directory. Additionally, maybe you need to use $(ls $query/*.bed) same for other directory.

Thanks, your insight helped tremendously!

I was able to get everything working with:

#!/bin/bash -x
dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds
for f in $(ls $query/*.bed); do 
   for g in $(ls $pgs/*); do
      bedtools intersect -wa -wb -a $f -b $g* -C -filenames
   done 
done > $(basename $f .ext).txt

Now I just need to figure out the output. It's concatenating the first file only, no matter what loop I pipe the output from (inner or outer).

Because, in each loop you are override it with > use >> instead to concat.

0 answers

No answers yet.

Log in to answer this question.