Skip to content

fix: use buffered and script based get-reads implementation #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion workflow/envs/pysam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ channels:
dependencies:
- python =3.9
- pysam =0.18
- pandas =1.3
- pandas =1.3
- dnaio =1.2
7 changes: 0 additions & 7 deletions workflow/rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,6 @@ def get_liftover_statement(wildcards, input, output):
return f"> {output}"


def get_read_limit_param(wildcards, input):
if config.get("limit-reads"):
return "| head -n 110000" # a bit more than 100000 reads because we also have the header
else:
return ""


def get_benchmark(benchmark):
try:
return benchmarks[benchmark]
Expand Down
15 changes: 4 additions & 11 deletions workflow/rules/download.smk
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ rule get_reads:
r1="resources/reads/{benchmark}.1.fq",
r2="resources/reads/{benchmark}.2.fq",
params:
limit=get_read_limit_param,
limit=branch(lookup("limit-reads", within=config), then=100000, otherwise=None),
bam_url=get_benchmark_bam_url,
log:
"logs/download-reads/{benchmark}.log",
conda:
"../envs/tools.yaml"
resources:
sort_threads=lambda _, threads: max(threads - 2, 1),
threads: 32
"../envs/pysam.yaml"
retries: 3
shell:
"(set +o pipefail; samtools view -f3 -h"
" {params.bam_url}"
" {params.limit} |"
" samtools sort -n -O BAM --threads {resources.sort_threads} | "
" samtools fastq -1 {output.r1} -2 {output.r2} -s /dev/null -0 /dev/null -) 2> {log}"
script:
"../scripts/get-reads.py"


rule get_archive:
Expand Down
47 changes: 47 additions & 0 deletions workflow/scripts/get-reads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import sys

sys.stderr = open(snakemake.log[0], "w")

import pysam
import dnaio


def aln_to_fq(qname, aln):
return dnaio.SequenceRecord(
name=qname,
sequence=aln.get_forward_sequence(),
qualities="".join(
map(lambda qual: chr(qual + 33), aln.get_forward_qualities())
),
)


limit = snakemake.params.limit
bam = pysam.AlignmentFile(snakemake.params.bam_url)

buffer = {}
n_written = 0
with dnaio.open(snakemake.output[0], snakemake.output[1], mode="w") as fqwriter:
for aln in bam:
if limit is not None and n_written >= limit:
break
if aln.is_secondary or aln.is_supplementary:
continue

# Some aligners (e.g. minimap2) add /1 and /2 to the read name.
# We remove them here to get the same name for both reads of a pair.
qname = aln.query_name.removesuffix("/1").removesuffix("/2")

mate_aln = buffer.get(qname)
if mate_aln is None:
buffer[qname] = aln
else:
if aln.is_read2:
aln, mate_aln = mate_aln, aln
del buffer[qname]

fqwriter.write(aln_to_fq(qname, aln), aln_to_fq(qname, mate_aln))
n_written += 1

if buffer:
print(f"Warning: {len(buffer)} reads had no mate pairs and were skipped", file=sys.stderr)