This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Efficiently Extracting Reads With Specific Names ('Queryname') From .Bam File

Greetings all, The problem before me is as follows:

I have a pretty large .bam file, and from that file I need to find all mapping locations of a particular read name ("queryname" according to .bam lingo). Is there any way to do so efficiently? picard offers "FilterSamReads.jar", but this method is actually even slower than converting the .bam file to a .sam file and just using grep to extract reads with specific names.

In particular, one would imagine that one could take advantage of sorting the .bam file by queryname (using samtools sort -n) to do this efficiently in a similar manner to which one can extract all mappings to a particular reference in coordinate-sorted .bam files (produced by "samtools sort" within the -n option).

So, the main purpose of writing this is to verify that no efficient method actually exists before spending the time writing a new one.

Cheers.

mapping sam bam picard samtools

1 answer

Rather than making the .sam, you can pipe

samtools view file.bam | grep queryname - > subset.sam

That will save you the time of expanding a huge .bam file.

samtools view expands the huge .bam file though. That does, however, save hard disk space.

Hi, how can I use this command for multiple reads of interest? Can I use a text file that has the read IDs and use grep on that text file?

Log in to answer this question.