This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Reading not properly paired reads from a BAM file

I have following BAM file

NB551232:115:H2VJHBGXF:2:22303:20719:3554:CT:GT 97  chr3    17443   54  70M =   17458   72  GTCATATAGATGATGACTGATTTACCCTTTTACTCTGCATGCAGCTTCTCCTGATGGAGTATCGTTTGTG  EEE6/EEAEEAAEEAAEA/E/AEE/<EEEE/EE/EEEAAE/EEEEEE/E<A</EEEAAAEA<<E//EE/<  NM:i:0MD:Z:70   AS:i:70 XS:i:52
NB551232:115:H2VJHBGXF:2:22303:20719:3554:CT:GT 145 chr3    17458   46  57M =   17443   -72 ACTGATTTACCCTTTTACTCTGCATGCAGCTTCTCCTGATGGAGTATCGTTTGTGTG   EEEE6EEE/E/EE/EEEEEEAEEEE//AEEEE/AA/EEE/</EEEEE///EE/E/EA   NM:i:0  MD:Z:57 AS:i:57 XS:i:43

As the flags indicated 97 and 145 these reads are not properly paired reads. How can I read these reads with java hstjdk

Here's what I'm trying

SamReader reader = SamReaderFactory.makeDefault().
               validationStringency(ValidationStringency.LENIENT).
               open(new File(BamPath));
for(SAMRecord record : reader) {
    // do something
}

But this loop doesn't work and does not read the BAM file. Is there some limitations on SAMReader on reading reads with specific flags? How can I read such BAM file with flags like 97, 145, 161 or 81?

bam java samreader

1 answer

Not tested, but the code should look like

(...)
try(CloseableIterator<SAMRecord> iter = reader.iterator()) {
   while(iter.hasNext()) {
   final SAMRecord rec = iter.next();
   if(rec.getReadUnmappedFlag()) continue;
   //etc, etc... see the javadoc for SAMRecord  https://samtools.github.io/htsjdk/javadoc/htsjdk/htsjdk/samtools/SAMRecord.html
   }
}

Log in to answer this question.