Thx for the reply! When you say that I can substract 2*read length, substract from what?
Let's say I have a read with POS 39312, PNEXT 39572 and TLEN 101. (CIGAR = 101M).
I can add 101 to 39312 to get it's approximate end pos and then with PNEXT, should I substract 101 from it? (Because Read-pairs go in opposite directions. I'm guessing PNEXT is the 1-based left-most position as well.) Substracting one from the other (39471-39413) would then give me a read-pair distance of 58. Is this how you meant?
What Devon said.
once you have the read ,you'll have to walk over the CIGAR string; Here is the code from HTSJDK:
There are other cigar operations, though... that code doesn't look very robust. It will return an incorrect answer for any read with an insertion, soft-clipping, etc.
One of the weaknesses of the sam format is that it does not tell you the stop position of reads. If you map with BBMap, you can use the "stoptag" flag to get this information in a custom field, though.