This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Reduce Bam File With Specific Loci In Bedfile

I want to reduce my bam file, obtaining a new bam file that contain

only loci in the bed file in input like

chr1 30498890 34983248

chr2 30398890 31483248

chr3 10492890 14983248

chr4 30492890 34283248

I try with:

samtools view -L test.bed test.bam | awk '$2 != 4 {print}'

and

intersectBed -abam file.bam -b test.bed

but the source bam file isn't reduce dimension.

How can I do ?

bam

2 answers

I easily solved with BEDtools intersectBed -abam file.bam -b test.bed > reduced.bam

You can use the bedOps utiltiy's tool sam2bed and then modify the command as

sam2bed <(samtools view -L test.bed test.bam | awk '$2 != 4 {print}') output.bed

Log in to answer this question.