Skip to content

Commit

Permalink
Add support for octal and comma separated samtools exclude flags (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colelyman authored Dec 12, 2024
1 parent 0232b06 commit 223aba5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion CRISPResso2/CRISPRessoPooledCORE.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ def calculate_aligned_samtools_exclude_flags(samtools_exclude_flags):
by filtering 0x900 (not primary alignment and supplementary alignment)
and also including any other user specified filters.
"""
samtools_exclude_flags = int(samtools_exclude_flags, base=16)
if ',' in samtools_exclude_flags:
return f'{samtools_exclude_flags},{hex(0x900)}'
elif samtools_exclude_flags.startswith('0x'):
samtools_exclude_flags = int(samtools_exclude_flags, base=16)
elif samtools_exclude_flags.startswith('0'):
samtools_exclude_flags = int(samtools_exclude_flags, base=8)
else:
samtools_exclude_flags = int(samtools_exclude_flags)
return hex(0x900 | samtools_exclude_flags)


def get_n_aligned_bam(bam_filename, samtools_exclude_flags):
p = sb.Popen(f"samtools view -F {calculate_aligned_samtools_exclude_flags(samtools_exclude_flags)} -c {bam_filename}", shell=True, stdout=sb.PIPE)
return int(p.communicate()[0])
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_tests/test_CRISPRessoPooledCORE.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ def test_calculate_aligned_samtools_exclude_flags_9():

def test_calculate_aligned_samtools_exclude_flags_0x100():
assert CRISPRessoPooledCORE.calculate_aligned_samtools_exclude_flags('0x100') == hex(0x900)


def test_calculate_aligned_samtools_exclude_flags_010():
"""This tests for proper handling of octal numbers."""
assert CRISPRessoPooledCORE.calculate_aligned_samtools_exclude_flags('010') == hex(0x908)


def test_calculate_aligned_samtools_exclude_flags_comma():
"""This tests for proper handling of commas."""
assert CRISPRessoPooledCORE.calculate_aligned_samtools_exclude_flags('0,4') == f'0,4,{hex(0x900)}'

0 comments on commit 223aba5

Please sign in to comment.