Skip to content

Commit

Permalink
fix: move bounds check south
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Sep 19, 2024
1 parent 9ccae99 commit d286e32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
27 changes: 15 additions & 12 deletions prymer/primer3/primer3.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def design_primers(self, design_input: Primer3Input) -> Primer3Result: # noqa:
)

design_region: Span = self._create_design_region(
target=design_input.target,
target_region=design_input.target,
max_amplicon_length=design_input.params.max_amplicon_length,
min_primer_length=design_input.params.min_primer_length,
)
Expand Down Expand Up @@ -713,7 +713,7 @@ def _build_failures(

def _create_design_region(
self,
target: Span,
target_region: Span,
max_amplicon_length: int,
min_primer_length: int,
) -> Span:
Expand All @@ -730,19 +730,22 @@ def _create_design_region(
ValueError: If the target region is too large to be padded.
"""
padding: int = max_amplicon_length - target.length
contig_length: int = self._dict[target.refname].length
padding: int = max_amplicon_length - target_region.length
contig_length: int = self._dict[target_region.refname].length

if padding < min_primer_length:
design_region: Span = replace(
target_region,
start=max(1, target_region.start - padding),
end=min(target_region.end + padding, contig_length),
)

left_design_window: int = target_region.start - design_region.start
right_design_window: int = design_region.end - target_region.end

if left_design_window < min_primer_length or right_design_window < min_primer_length:
raise ValueError(

Check warning on line 746 in prymer/primer3/primer3.py

View check run for this annotation

Codecov / codecov/patch

prymer/primer3/primer3.py#L746

Added line #L746 was not covered by tests
f"Target region {target} is too large to design an amplicon of maximum "
f"Target region {target_region} is too large to design an amplicon of maximum "
f"length {max_amplicon_length}."
)

design_region: Span = replace(
target,
start=max(1, target.start - padding),
end=min(target.end + padding, contig_length),
)

return design_region
10 changes: 5 additions & 5 deletions tests/primer3/test_primer3.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,24 +560,24 @@ def test_primer3_result_as_primer_pair_result_exception(
@pytest.mark.parametrize("max_amplicon_length", [100, 101])
def test_pad_target_region(max_amplicon_length: int, genome_ref: Path) -> None:
"""If the target region is shorter than the max amplicon length, it should be padded to fit."""
target = Span(refname="chr1", start=201, end=250, strand=Strand.POSITIVE)
target_region = Span(refname="chr1", start=201, end=250, strand=Strand.POSITIVE)

with Primer3(genome_fasta=genome_ref) as designer:
design_region: Span = designer._create_design_region(
target=target,
target_region=target_region,
max_amplicon_length=max_amplicon_length,
min_primer_length=10,
)

assert design_region.length == 2 * max_amplicon_length - target.length
assert design_region.length == 2 * max_amplicon_length - target_region.length


def test_pad_target_region_doesnt_pad(genome_ref: Path) -> None:
"""If the target region is larger than the max amplicon length, no padding should occur."""
target = Span(refname="chr1", start=201, end=250, strand=Strand.POSITIVE)
target_region = Span(refname="chr1", start=201, end=250, strand=Strand.POSITIVE)

with Primer3(genome_fasta=genome_ref) as designer:
with pytest.raises(ValueError):
designer._create_design_region(
target=target, max_amplicon_length=10, min_primer_length=10
target_region=target_region, max_amplicon_length=10, min_primer_length=10
)

0 comments on commit d286e32

Please sign in to comment.