def _new_rec(self, name: str, chrom: str, start: int, attrs: Optional[Dict[str, Any]]) -> AlignedSegment: """Generates a new AlignedSegment. Sets the segment up with the correct header and adds the RG attribute if not contained in attrs. Args: name: the name of the read/template chrom: the chromosome to which the read is mapped start: the start position of the read on the chromosome attrs: an optional dictionary of SAM attributes with two-char keys Returns: AlignedSegment: an aligned segment with name, chrom, pos, attributes the read group, and the unmapped flag all set appropriately. """ if chrom is not sam.NO_REF_NAME and chrom not in self._seq_lookup: raise ValueError( f"{chrom} is not a valid chromosome name in this builder.") rec = AlignedSegment(header=self._samheader) rec.query_name = name rec.reference_name = chrom rec.reference_start = start rec.mapping_quality = self.mapping_quality if chrom == sam.NO_REF_NAME or start == sam.NO_REF_POS: rec.is_unmapped = True attrs = attrs if attrs else dict() if "RG" not in attrs: attrs["RG"] = self.rg_id() rec.set_tags(list(attrs.items())) return rec
def test_sam_alignment_to_padded_alignment(): alignment = AlignedSegment() alignment.reference_start = 0 alignment.query_sequence = 'AGCTTAGCTAGCTACCTATATCTTGGTCTTGGCCG' alignment.cigartuples = ((0,10), (2,1), (0,25)) ref = Reference('test', 'AGCTTAGCTAAGCTACCTATATCTTGGTCTTGGCCG') (pad_ref, pad_match, pad_query) = sam_alignment_to_padded_alignment(alignment, ref) assert pad_ref == 'AGCTTAGCTAAGCTACCTATATCTTGGTCTTGGCCG' assert pad_match == '|||||||||| |||||||||||||||||||||||||' assert pad_query == 'AGCTTAGCTA-GCTACCTATATCTTGGTCTTGGCCG'
def _make_read_unmapped(rec: AlignedSegment) -> None: """Removes mapping information from a read.""" if rec.is_reverse: quals = rec.query_qualities quals.reverse() rec.query_sequence = dnautils.reverse_complement(rec.query_sequence) rec.query_qualities = quals rec.is_reverse = False rec.reference_id = sam.NO_REF_INDEX rec.reference_start = sam.NO_REF_POS rec.cigar = None rec.mapping_quality = 0 rec.template_length = 0 rec.is_duplicate = False rec.is_secondary = False rec.is_supplementary = False rec.is_proper_pair = False rec.is_unmapped = True