Good idea, but actually, on my computer, bamtools is about 3 times slower than grep:
$ time (bamtools filter -in example.bam -tag NM:1 > /dev/null ) real 0m36.750s user 0m36.512s sys 0m0.216s $ time (samtools view -h example.bam | grep '^@|NM:i:0' > /dev/null) real 0m12.197s user 0m14.432s sys 0m0.788s
(I ran the commands multiple times, so the BAM file is already in the cache memories of whichever component of the computer its operating system.)
I am using the bamtools binary from Debian 8 (Jessie).
$ bamtools --version bamtools 2.3.0 Part of BamTools API and toolkit Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg (c) 2009-2012 Marth Lab, Biology Dept., Boston College $ dpkg -l bamtools Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-=========================-=================-=================-======================================================== ii bamtools 2.3.0+dfsg-2 amd64 toolkit for manipulating BAM (genome alignment) files
Grep is pretty darn efficient. I can't think of a faster way to look at each read than to look at each read. Fundamentally any alternative would do the same.
Would grep really be as efficient as using samtools view if that was an option?
I meant to run
samtools viewand pipe throughgrep. If samtools has a filter flag for your tag of interest that would be a tiny bit faster. If your tag of interest is obscure then you've got to grep it. grep has a non-regex fixed mode that is very fast, but nothing is going to read a BAM faster thansamtools view.let's assume my aligner writes the sam output to standard output. Is there a one-liner that would pipe and grep and this directly into a bam file (including the header)?
For example, if to pipe my alignment output to bam I use:
How can I change it to pipe only alignments with NH = 1?
This should work, but there are probably faster ways...
Thanks thackl. It is indeed pretty slow. If anyone is following and knows a samtools command which is faster for achieving this that'll be great.
If you want bam2bam the following grep is about 10x faster than my previous idea. Roughly 1 minute / GB bam on my system. Depending on what type of alignments are in your bam, the samtools filter flags (no unmapped, only primary, no supplement - these cannot be unique mappings anyway) might speed up things as well.
Pardon my ignorance in linux. Is the full command you're suggesting this: