Skip to content

Commit

Permalink
Add one more test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfenne committed Nov 8, 2024
1 parent f810b1d commit eb5dc15
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
7 changes: 5 additions & 2 deletions prymer/api/picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,17 @@ def build_primer_pairs(
Returns:
an iterator over all the valid primer pairs, unsorted
"""
print(f"Target={target}; lefts={left_primers}.")
print(f"Target={target}; rights={right_primers}.")

# Short circuit if we have no left primers or no right primers
if not any(left_primers) or not any(right_primers):
return

if not all(p.span.refname == target.refname for p in left_primers):
if any(p.span.refname != target.refname for p in left_primers):
raise ValueError("Left primers exist on different reference than target.")

if not all(p.span.refname == target.refname for p in right_primers):
if any(p.span.refname != target.refname for p in right_primers):
raise ValueError("Right primers exist on different reference than target.")

# Grab the sequence we'll use to fill in the amplicon sequence
Expand Down
52 changes: 50 additions & 2 deletions tests/api/test_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def fasta(tmp_path: Path) -> Path:
return path


def p(bases: str, tm: float, pos: int, pen: float = 0) -> Oligo:
def p(bases: str, tm: float, pos: int, pen: float = 0, chrom: str = "chr1") -> Oligo:
"""Generates a primer for testing."""
oligo = Oligo(
name="left",
tm=tm,
penalty=pen,
bases=bases,
span=Span("chr1", pos, pos + len(bases) - 1),
span=Span(chrom, pos, pos + len(bases) - 1),
tail=None,
)
assert oligo.span.length == len(oligo.bases)
Expand Down Expand Up @@ -354,3 +354,51 @@ def test_build_primer_pairs_amplicon_tm_filtering(
)

assert len(pairs) == (1 if max_tm > amp_tm else 0)


def test_build_primer_pairs_fails_when_primers_on_wrong_reference(
fasta: Path,
weights: PrimerAndAmpliconWeights,
) -> None:
target = Span("chr1", 240, 260)
valid_lefts = [p(REF_BASES[200:220], tm=60, pos=201, pen=1)]
invalid_lefts = [p(REF_BASES[200:220], tm=60, chrom="X", pos=201, pen=1)]
valid_rights = [p(reverse_complement(REF_BASES[280:300]), tm=61, pos=281, pen=1)]
invalid_rights = [p(reverse_complement(REF_BASES[280:300]), tm=61, chrom="X", pos=281, pen=1)]

picks = picking.build_primer_pairs(
left_primers=valid_lefts,
right_primers=valid_rights,
target=target,
amplicon_sizes=MinOptMax(0, 100, 500),
amplicon_tms=MinOptMax(0, 80, 150),
max_heterodimer_tm=None,
weights=weights,
fasta_path=fasta,
)

assert next(picks) is not None

with pytest.raises(ValueError, match="Left primers exist on different reference"):
_picks = list(picking.build_primer_pairs(
left_primers=invalid_lefts,
right_primers=valid_rights,
target=target,
amplicon_sizes=MinOptMax(0, 100, 500),
amplicon_tms=MinOptMax(0, 80, 150),
max_heterodimer_tm=None,
weights=weights,
fasta_path=fasta,
))

with pytest.raises(ValueError, match="Right primers exist on different reference"):
_picks = list(picking.build_primer_pairs(
left_primers=valid_lefts,
right_primers=invalid_rights,
target=target,
amplicon_sizes=MinOptMax(0, 100, 500),
amplicon_tms=MinOptMax(0, 80, 150),
max_heterodimer_tm=None,
weights=weights,
fasta_path=fasta,
))

0 comments on commit eb5dc15

Please sign in to comment.