Question: bedtools intersect with match on name column
0
gravatar for corend
2.2 years ago by
corend70
corend70 wrote:

I have files a.bed and b.bed

a.bed

chr1 10 20 geneA
chr1 30 40 geneB

b.bed

chr1 5 15 geneA
chr1 10 25 geneB

if I intersectbed those files I will obtain:

chr1 10 15 geneA
chr1 10 20 geneA

But I would like the name column to match between files so that i will obtain:

chr1 10 15 geneA

If name column does not match, I don't want to output the intersection. I can't find any option in intersectbed to "force identical name", any suggestions?

intersectbed bedtools • 1.3k views
ADD COMMENTlink modified 2.2 years ago • written 2.2 years ago by corend70
3
gravatar for ATpoint
2.2 years ago by
ATpoint46k
ATpoint46k wrote:

I would do a two-step process:

## First use the -wa -wb options to report both the entire entry of -a and of -b in case of overlap,
## and then use awk to check if the genes are the same.
## If so, keep the row, if not discard:

bedtools intersect -a a.txt -b b.txt -wa -wb | awk 'OFS="\t" {if ($4 == $8) print}' > intermediate.txt

## Then use intersect again to get the actual overlap:
bedtools intersect -a <(cut -f1-4 intermediate.txt) -b <(cut -f 5-8 intermediate.txt)
ADD COMMENTlink modified 2.2 years ago • written 2.2 years ago by ATpoint46k

Sorry, it is not working, here is the problem:

In the second step, you don't intersect line by line, so the problem is still the same. Just try it and you will see.

ADD REPLYlink written 2.2 years ago by corend70
1

Based on the data you provided it gives exactly the result you intended:

enter image description here

ADD REPLYlink written 2.2 years ago by ATpoint46k

My bad, my example was not a MWE, I will validate your answer. If you replace 10 25 by 10 35 here you will see the problem.

ADD REPLYlink written 2.2 years ago by corend70
1
gravatar for corend
2.2 years ago by
corend70
corend70 wrote:

I finaly found out how to do it:

As I want a Chromosome and name match, I just make a bedfile in the format chr+name start end name, and it works well.

ADD COMMENTlink written 2.2 years ago by corend70
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1991 users visited in the last hour
_