This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract the mate pair information with htsjdk

I have a BAM file sorted by position. I need to process a group of reads from the BAM file at a time. To process that, I need to access the mate pair of each of these read at the same time. Is there a way using htsjdk to access the mate pair information using the information of the other pair.

I can get the Mate Pair record name with getMateReferenceName() and alignment start position by getMateAlignmentStart(). But I need to access the read sequence and the base quality string and the flag of the mate pair. How can I do that? Thanks.

bam samtools htsjdk

1 answer

SAMReader$queryMate : https://samtools.github.io/htsjdk/javadoc/htsjdk/htsjdk/samtools/SamReader.html#queryMate-htsjdk.samtools.SAMRecord-

Fetch the mate for the given read. Only valid to call this if hasIndex() == true. This will work whether the mate has a coordinate or not, so long as the given read has correct mate information. This method iterates over the SAM file, so there may not be an unclosed iterator on the SAM file when this method is called.

Note that it is not possible to call queryMate when iterating over the SamReader, because queryMate requires its own iteration, and there cannot be two simultaneous iterations on the same SamReader. The work-around is to open a second SamReader on the same input file, and call queryMate on the second reader.

Log in to answer this question.