bedtools intersect multiple files using for loop
0
0
Entering edit mode
24 months ago
clemaster • 0

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 • 1.4k views
ADD COMMENT
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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).

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1501 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6