How to efficiently remove a list of reads from BAM file?
3
3
Entering edit mode
6.0 years ago
Tao ▴ 460

Hi Guys,

I have a BAM file, and a big read list. What I want to do is to remove the reads in the read list from the BAM file. I can transform Bam to Sam file and then use a Python script to remove unwanted reads. And then transform Sam to Bam again. But I am wondering if there is a more efficient way, which I mean faster, easier, and memory-efficient, to achieve this goal?

Any advice is appreciated!

Tao

RNA-Seq BAM samtools Sam • 11k views
ADD COMMENT
8
Entering edit mode
6.0 years ago

picard FilterSamReads

READ_LIST_FILE (File) Read List File containing reads that will be included or excluded from the OUTPUT SAM or BAM file. Default value: null.

ADD COMMENT
0
Entering edit mode

Exciting! That's what I'm looking for!

Many Thanks! Pierre.

ADD REPLY
2
Entering edit mode
6.0 years ago
Vivek ★ 2.5k
samtools view -h sample1.bam | grep -vf read_ids_to_remove.txt | samtools view -bS -o sample1_filter.bam -‚Äč

http://genometoolbox.blogspot.com/2013/06/remove-list-of-reads-from-bam-file.html

ADD COMMENT
3
Entering edit mode

Hi Vivek,

Thanks for your nice reply. I also found this method, but unfortunately, it's kind of some inefficient. I think may be 'grep' is always doing global search. Here, given a read ID, grep has to confirm all the bam context doesn't contain this read ID(pattern). So it's much slower when a big list of unwanted reads occur. While in my script, I only need to confirm the first column(reads ID) of the Bam file doesn't contain those unwanted reads. But, anyway, your method is a good choice to handle small read list. Thanks!

Tao

ADD REPLY
2
Entering edit mode

this can be made faster by adding the -@ parameter in samtools view. Given a multicore machine, utilize threads to make samtools (implemented in newer versions) faster.

ADD REPLY
1
Entering edit mode

That's a good idea!

ADD REPLY
2
Entering edit mode
6.0 years ago

The fastest and easiest solution is probably to use BBMap + samtools:

filterbyname.sh in=mapped.bam out=filtered.bam names=names.txt include=false

Samtools needs to be in the path. The memory usage depends on the number of names; the speed doesn't (well, not much).

ADD COMMENT

Login before adding your answer.

Traffic: 2014 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