def sam_flag_explainer(flag:int):
"""
Intepret sam flag and make a flag-explained-dictionary.
"""
try:
# Convert sam flags to binary format, like 0101001
# flag is a integer.
flag_binary = "{0:b}".format(flag)
except Exception as err:
if int(flag) == flag:
flag = int(flag)
flag_binary = "{0:b}".format(flag)
else:
print(f"{flag=}")
raise ValueError from err
flag_template = {
"read_paired":False,
"read_mapped_in_proper_pair":False,
"read_unmapped":False,
"mate_unmapped":False,
"read_reverse_strand":False,
"mate_reverse_strand":False,
"first_in_pair":False,
"second_in_pair":False,
"not_primary_alignment":False,
"read_fails_platform/vendor_quality_checks":False,
"read_is_PCR_or_optical_duplicate":False,
"supplementary_alignment":False,
}
for k, f in zip(flag_template.keys(), flag_binary[::-1]): # should intepret the flag from back.
if int(f) == 1:
flag_template[k] = True
return flag_template
The code is the above.
Schematically, this function does:
Convert integer into binary number -> Read it from the back -> Modify the value of the key based on binary number element.
This function is called a lot of times in my program.
And this function is slow and it accounts for a large percentage of the overall execution time.
How could I make this faster?
int('0b(what your binary int is)', 2)
to convert back. \$\endgroup\$