This is a test version of Biostars. For the public version, visit https://www.biostars.org.
getting a list of alignment with a particular bam tag from a bam file

I have a list of values for a bam tag called XC (say needed.txt). I would like to extract the alignments which have these values from a bam file (say bamfile.bam). I would like to use pysam to do this, but could not figure out from the documentation how to modify the samfile.fetch command for this. Could you help me the modification needed to read in the required list from needed.txt and extract the alignments. Thanks

alignment

2 answers

You don't need to modify samfile.fetch(), instead you parse the resulting reads and then use the .get_tag() accessor to check the value of the XC tag.

not pysam.

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

 java -jar dist/samjdk.jar --body \
     -e 'Set<String> xc = null;  public Object apply( SAMRecord R) { if(xc==null) try {xc=new HashSet<>(IOUtil.slurpLines(new java.io.File("needed.txt")));} catch(Exception e) {throw new RuntimeIOException(e);} String att= R.getStringAttribute("XC"); return att!=null && xc.contains(att);}' \
input.bam

Log in to answer this question.