This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filter bam, EXCLUDING specific tagged reads

I am trying to use samtools view to filter a bam file. I would ideally like to take a sorted bam with the DT flags set by picard markduplicates (e.g. DT:Z:SQ for optical/sequencing and DT:Z:LB for PCR/library) and exclude reads with either DT:SQ only OR DT:SQ & DT:LB. As far as I can tell, samtools only supports filtering by including specific tag:value pairs with the -d parameter. Is there any workaround here? Or is the only option to write another intermediate file?

samtools

1 answer

using samjdk http://lindenb.github.io/jvarkit/SamJdk.html

(not tested)

 java -jar dist/samjdk.jar -e 'Object att = record.getAttribute("DT"); return att==null || !(att.equals("SQ") || att.equals("LB"));' input.bam

This is great -- thanks!!!!!! One quick question, where am I going wrong here?

 java -jar samjdk.jar -e 'Object att = record.getAttribute("DT"); return att.equals("SQ");' input.bam

I get the following error:

[SEVERE][SamJdk]null
java.lang.NullPointerException
    at SamJdkCustom1168468679.apply(SamJdkCustom1168468679.java:14)
    at com.github.lindenb.jvarkit.tools.samjs.SamJdk.doWork(SamJdk.java:722)
    at com.github.lindenb.jvarkit.util.jcommander.Launcher.instanceMain(Launcher.java:777)
    at com.github.lindenb.jvarkit.util.jcommander.Launcher.instanceMainWithExit(Launcher.java:940)
    at com.github.lindenb.jvarkit.tools.samjs.SamJdk.main(SamJdk.java:806)
[INFO][Launcher]samjdk Exited with failure (-1)

you have some reads that don't have the attribute DT. record.getAttribute("DT") returns a null value...

Log in to answer this question.