Skip to content

Commit

Permalink
make blacklisting actually work
Browse files Browse the repository at this point in the history
Used the -U flag incorrectly originally
  • Loading branch information
Kudostoy0u authored Jan 3, 2025
1 parent b9ec456 commit 21535b7
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/finaletoolkit/utils/_filter_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,40 @@ def filter_file(
# create temp dir to store intermediate sorted file
try:
with tf.TemporaryDirectory() as temp_dir:
flag_filtered_bam = f'{temp_dir}/flag_filtered{suffix}'
temp_1 = f"{temp_dir}/output1{suffix}"
temp_2 = f"{temp_dir}/output2{suffix}"
if blacklist_file is not None:
try:
subprocess.run(
f"samtools view {input_file} -U {temp_1} -L {blacklist_file} -@ {workers} -b > /dev/null && samtools index {temp_1}",
shell=True,
check=True)
except Exception:
traceback.print_exc()
exit(1)
else:
subprocess.run(f"mv {input_file} {temp_1}")

samtools_command = (
f'samtools view {input_file} -F 3852 -f 3 -b -h -o '
f'{flag_filtered_bam} -q {quality_threshold} -@ {workers}'
f'samtools view {temp_1} -F 3852 -f 3 -b -h -o '
f'{temp_2} -q {quality_threshold} -@ {workers}'
)

if whitelist_file is not None:
samtools_command += f' -M -L {whitelist_file}'
if blacklist_file is not None:
samtools_command += f' -U {blacklist_file}'

try:
subprocess.run(samtools_command, shell=True, check=True)
except Exception:
except subprocess.CalledProcessError as e:
print(e)
traceback.print_exc()
exit(1)

# suppress index file warning
save = pysam.set_verbosity(0)

# filter for reads on different reference and length
with pysam.AlignmentFile(flag_filtered_bam, 'rb',threads=workers//3) as in_file:
with pysam.AlignmentFile(temp_2, 'rb',threads=workers//3) as in_file:
with pysam.AlignmentFile(
output_file, 'wb', template=in_file, threads=workers-workers//3) as out_file:
for read in in_file:
Expand Down

0 comments on commit 21535b7

Please sign in to comment.