Hi,
I would like to know if is possible to filter a bam file with a bed file. I want to keep only all reads non-overlapped with my bed file. Do you know if there is a option or a way to do that with bedtools or samtools?
Thanks.
Sophie
Hi,
I would like to know if is possible to filter a bam file with a bed file. I want to keep only all reads non-overlapped with my bed file. Do you know if there is a option or a way to do that with bedtools or samtools?
Thanks.
Sophie
Although this has been already answered, just a quick note about bedtools vs samtools for reads filtering. I have always experienced slower times when using bedtools intersect
compared to samtools view -L
. I've sometimes even found samtools performing in half bedtools' time, so I try to favour the former as much as possible. if you would want the reads that overlap a bedfile I would definitely suggest using samtools instead of bedtools, but as the non-overlapping-reads are wanted it wouldn't work. in case the bedtools intersect
filtering takes a long time or if you have to perform the same filter over and over again I would suggest to create a "negative" bedfile substracting your bedfile from a bedfile containing your genome's coordinates. this subtraction (which would be really fast and only performed once) would give you a bedfile with regions you'd like the reads to be at, therefore you could use it with samtools view -L
as much as needed.
You will have to add -v
to it.
bedtools intersect -abam reads.bam -b sample.bed -v > reads.nosample.bam
Bedtools intersect works with both BED/GFF/VCF and BAM files as input.
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Oh thank you very much for these useful advices
I like your idea of creating a negative bed file and then using bedtools subtract to remove those regions from bam.