Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pysam/libcalignedsegment.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cdef int NCIGAR_CODES = 10

CIGAR2CODE = dict([y, x] for x, y in enumerate(CODE2CIGAR))
CIGAR_REGEX = re.compile("(\d+)([MIDNSHP=XB])")
HEX_VALUE_REGEX = re.compile(r"([0-9A-F][0-9A-F])*") # from SAM specification

# names for keys in dictionary representation of an AlignedSegment
KEY_NAMES = ["name", "flag", "ref_name", "ref_pos", "map_quality", "cigar",
Expand Down Expand Up @@ -2520,6 +2521,12 @@ cdef class AlignedSegment:
value_ptr = <uint8_t*><char*>value
value_size = len(value)+1
elif typecode == b'H':
# validate Hex values
if not HEX_VALUE_REGEX.fullmatch(value):
raise ValueError(
f"Invalid value {value} for tag {tag.decode()} with value_type 'H': "
f"must match the regular expression {HEX_VALUE_REGEX.pattern}"
)
# Note that hex tags are stored the very same
# way as Z string.s
value = force_bytes(value)
Expand Down