This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Tool: Quick Command to Filter Bam Files

Hi,

Was looking for a while on how to edit bed files to specific chromosome regions which made sense to me. Finally made this quick AWK command that made the outcome simple:

awk '{if ($2>=START_COORDINATES && $3=<END_COORDINATES && $1=="CHROMOSOME") print $1, $2, $3}' file.bed > file_mod.bed

An example with the command would be:

awk '{if ($2>200 && $3 <300 && $1=="chr10") print $1, $2, $3}' file.bed > file_mod.bed

Cheers.

P.S. also - check out Pierre's suggestion below!

bed awk
awk -F '\t' '($1=="chr10" && !($2 >300 || $3 <200))' file.bed > file_mode.bed

Your example requires full containment, in that both start and end are within the interval, whereas the solution by Pierre Lindenbaum solution accepts any overlap.

Though I think there might be a subtle bug there, since in BED format one coordinate is inclusive while the other not, it feels that one check should have >=

I'd rather use bedtools intersect (or bedops intersect/element-of) than mess around with awk. More performant and better documented, easier to read.

Great suggestions!

0 answers

No answers yet.

Log in to answer this question.