I'm using pysam to filter a BAM file based on alignment identity (which I'm calculating from the NM tag). Because the filtering is performed on individual reads, and not reads pairs, this can lead to misleading flags in the resulting BAM file.
For example, lets suppose that both reads from a pair were mapped before filtering, but one of them was removed due to low identity to the reference. Ideally, the read that was kept should have the "mate unmapped" (0x8) bitflag, but because the filtering of paired reads is performed independently, there's no way of knowing beforehand that this flag should be kept.
Is there a way to re-evaluate the SAM/BAM file after it was modified to update the bitflags?