def set_pair_info(r1: AlignedSegment, r2: AlignedSegment, proper_pair: bool = True) -> None: """Resets mate pair information between reads in a pair. Requires that both r1 and r2 are mapped. Can be handed reads that already have pairing flags setup or independent R1 and R2 records that are currently flagged as SE reads. Args: r1: read 1 r2: read 2 with the same queryname as r1 """ assert not r1.is_unmapped, f"Cannot process unmapped mate {r1.query_name}/1" assert not r2.is_unmapped, f"Cannot process unmapped mate {r2.query_name}/2" assert r1.query_name == r2.query_name, f"Attempting to pair reads with different qnames." for r in [r1, r2]: r.is_paired = True r.is_proper_pair = proper_pair r1.is_read1 = True r1.is_read2 = False r2.is_read2 = True r2.is_read1 = False for src, dest in [(r1, r2), (r2, r1)]: dest.next_reference_id = src.reference_id dest.next_reference_start = src.reference_start dest.mate_is_reverse = src.is_reverse dest.mate_is_unmapped = False dest.set_tag("MC", src.cigarstring) insert_size = isize(r1, r2) r1.template_length = insert_size r2.template_length = -insert_size
def _set_flags(self, rec: pysam.AlignedSegment, is_r1: bool, strand: str) -> None: """Appropriately sets most flag fields on the given read. Args: rec: the read to set the flags on is_r1: True if the read is a R1, False if it is an R2 strand: Either "+" or "-" to indicate strand of the read """ rec.is_paired = True rec.is_read1 = is_r1 rec.is_read2 = not is_r1 rec.is_qcfail = False rec.is_duplicate = False rec.is_secondary = False rec.is_supplementary = False if not rec.is_unmapped: rec.is_reverse = strand != "+"