-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_primer_pair.py
577 lines (521 loc) · 18.8 KB
/
test_primer_pair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
from dataclasses import dataclass
from dataclasses import replace
from typing import Optional
import pytest
from fgpyo.fasta.sequence_dictionary import SequenceDictionary
from fgpyo.sequence import reverse_complement
from prymer.api.oligo import Oligo
from prymer.api.primer_pair import PrimerPair
from prymer.api.span import Span
from prymer.api.span import Strand
@dataclass(init=True, frozen=True)
class PrimerPairTestCase:
"""Test case for a `PrimerPair`.
Attributes:
primer_pair: the PrimerPair to test
bed_12_fields: the fields, that when tab-delimited, are the expected string
for the `PrimerPair.bed_12` method
gc_pct: the expected value for the `PrimerPair.percent_gc_content` property
inner: the expected value for the `PrimerPair.inner` method
length: the expected value for the `PrimerPair.length` property
str_fields: the fields, that when tab-delimited, are the expected string for the
`Primer.__str__` method
"""
primer_pair: PrimerPair
bed_12_fields: list[str]
gc_pct: float
inner: Span
length: int
str_fields: list[str]
@staticmethod
def primer_pair_from_left_primer(left: Oligo, right_offset: int = 50) -> PrimerPair:
"""
Generates a PrimerPair for use in unit tests. Will first generate
a right primer using a standard formula, and then calculates the
PrimerPair fields based on the left & right primers.
"""
right: Oligo = PrimerPairTestCase.right_primer_from_left_primer(
left=left, right_offset=right_offset
)
amplicon = replace(left.span, end=right.span.end)
amplicon_sequence = PrimerPairTestCase._to_amplicon_sequence(
left.bases,
right.bases,
amplicon.length - 1,
)
amplicon_tm = left.tm + right.tm
penalty = left.penalty + right.penalty
return PrimerPair(
left_primer=left,
right_primer=right,
amplicon_sequence=amplicon_sequence,
amplicon_tm=amplicon_tm,
penalty=penalty,
)
@staticmethod
def right_primer_from_left_primer(left: Oligo, right_offset: int) -> Oligo:
"""
Provides a standard conversion for a left primer to a right primer for use in
tests of PrimerPair.
"""
return replace(
left,
bases=reverse_complement(left.bases) if left.bases is not None else None,
tm=left.tm + 10,
penalty=left.penalty + 10,
span=replace(
left.span,
start=left.span.start + right_offset,
end=left.span.end + right_offset,
strand=Strand.NEGATIVE,
),
)
@staticmethod
def _to_amplicon_sequence(
left: Optional[str], right: Optional[str], length: int
) -> Optional[str]:
"""
Provides a consistent mechanism to generate a valid amplicon sequence.
There's no inherent meaning to the algorithm.
"""
if left is None or right is None:
return None
# Combine the bases from left and right primers
bases = "".join(
f"{left_base}{right_base}" for left_base, right_base in zip(left, right, strict=True)
)
# Repeat the combined bases enough times to cover the required length
times = (length // len(bases)) + 1
extended_bases = (bases * times)[: length + 1]
return extended_bases
def build_primer_pair_test_cases() -> list[PrimerPairTestCase]:
"""
Builds a set of test cases for `PrimerPair` methods using a
supplied left primer.
"""
return [
PrimerPairTestCase(
primer_pair=PrimerPairTestCase.primer_pair_from_left_primer(
left=Oligo(
bases="GATTACA",
tm=12.34,
penalty=56.78,
span=Span(refname="chr1", start=1, end=7, strand=Strand.POSITIVE),
)
),
bed_12_fields=[
"chr1",
"0",
"57",
"chr1_1_57_F",
"500",
"+",
"0",
"57",
"100,100,100",
"3",
"7,43,7",
"0,7,50",
],
gc_pct=29.825,
inner=Span(refname="chr1", start=8, end=50),
length=57,
str_fields=[
"GATTACA",
"12.34",
"56.78",
"chr1:1-7:+",
"TGTAATC",
"22.34",
"66.78",
"chr1:51-57:-",
"GTAGTTTAAACTACGTAGTTTAAACTACGTAGTTTAAACTACGTAGTTTAAACTACG",
"34.68",
"123.56",
],
),
PrimerPairTestCase(
primer_pair=PrimerPairTestCase.primer_pair_from_left_primer(
left=Oligo(
bases="TGTAATC",
tm=87.65,
penalty=43.21,
span=Span(refname="chr22", start=100, end=106, strand=Strand.POSITIVE),
)
),
bed_12_fields=[
"chr22",
"99",
"156",
"chr22_100_156_F",
"500",
"+",
"99",
"156",
"100,100,100",
"3",
"7,43,7",
"0,7,50",
],
gc_pct=28.07,
inner=Span(refname="chr22", start=107, end=149),
length=57,
str_fields=[
"TGTAATC",
"87.65",
"43.21",
"chr22:100-106:+",
"GATTACA",
"97.65",
"53.21",
"chr22:150-156:-",
"TGGATTATAATCCATGGATTATAATCCATGGATTATAATCCATGGATTATAATCCAT",
"185.3",
"96.42",
],
),
PrimerPairTestCase(
primer_pair=PrimerPairTestCase.primer_pair_from_left_primer(
left=Oligo(
bases=None,
tm=12.34,
penalty=56.78,
span=Span(refname="chr1", start=1, end=40, strand=Strand.POSITIVE),
)
),
bed_12_fields=[
"chr1",
"0",
"90",
"chr1_1_90_F",
"500",
"+",
"0",
"90",
"100,100,100",
"3",
"40,10,40",
"0,40,50",
],
gc_pct=0.0,
inner=Span(refname="chr1", start=41, end=50),
length=90,
str_fields=[
"*",
"12.34",
"56.78",
"chr1:1-40:+",
"*",
"22.34",
"66.78",
"chr1:51-90:-",
"*",
"34.68",
"123.56",
],
),
PrimerPairTestCase(
primer_pair=PrimerPairTestCase.primer_pair_from_left_primer(
left=Oligo(
bases="GGGGGGG",
tm=12.34,
penalty=56.78,
span=Span(refname="chr1", start=1, end=7, strand=Strand.POSITIVE),
)
),
bed_12_fields=[
"chr1",
"0",
"57",
"chr1_1_57_F",
"500",
"+",
"0",
"57",
"100,100,100",
"3",
"7,43,7",
"0,7,50",
],
gc_pct=100.0,
inner=Span(refname="chr1", start=8, end=50),
length=57,
str_fields=[
"GGGGGGG",
"12.34",
"56.78",
"chr1:1-7:+",
"CCCCCCC",
"22.34",
"66.78",
"chr1:51-57:-",
"GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCG",
"34.68",
"123.56",
],
),
# This one is written out long form in order to override the automated
# generation of the right primer. We want to handle a case where the primers
# overlap
PrimerPairTestCase(
primer_pair=PrimerPair(
left_primer=Oligo(
bases="GATTACA",
tm=12.34,
penalty=56.78,
span=Span(refname="chr1", start=1, end=7, strand=Strand.POSITIVE),
),
right_primer=Oligo(
bases="TGTAATC",
tm=87.65,
penalty=43.21,
span=Span(refname="chr1", start=5, end=11, strand=Strand.NEGATIVE),
),
amplicon_tm=25,
penalty=99.99,
),
bed_12_fields=[
"chr1",
"0",
"11",
"chr1_1_11_F",
"500",
"+",
"0",
"11",
"100,100,100",
"3",
"7,1,7",
"0,5,4",
],
gc_pct=0,
inner=Span(refname="chr1", start=6, end=6),
length=11,
str_fields=[
"GATTACA",
"12.34",
"56.78",
"chr1:1-7:+",
"TGTAATC",
"87.65",
"43.21",
"chr1:5-11:-",
"*",
"25",
"99.99",
],
),
]
PRIMER_PAIR_TEST_CASES = build_primer_pair_test_cases()
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_inner(test_case: PrimerPairTestCase) -> None:
"""Test the `PrimerPair.inner` property."""
assert test_case.primer_pair.inner == test_case.inner
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_gc_content(test_case: PrimerPairTestCase) -> None:
"""Test the `PrimerPair.percent_gc_content` property."""
assert test_case.primer_pair.percent_gc_content == test_case.gc_pct
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_str(test_case: PrimerPairTestCase) -> None:
"""Test the `PrimerPair.__str__` method."""
assert str(test_case.primer_pair) == "\t".join(test_case.str_fields)
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_length(test_case: PrimerPairTestCase) -> None:
"""Test the `PrimerPair.length` property"""
assert test_case.primer_pair.length == test_case.length
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_to_bed12_row(test_case: PrimerPairTestCase) -> None:
"""Test the `PrimerPair.to_bed12_row` method."""
assert test_case.primer_pair.to_bed12_row() == "\t".join(test_case.bed_12_fields)
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_span(test_case: PrimerPairTestCase) -> None:
"""
Test that the `PrimerPair.span property returns the amplicon field
"""
assert test_case.primer_pair.span == test_case.primer_pair.amplicon
@pytest.mark.parametrize("test_case", PRIMER_PAIR_TEST_CASES)
def test_primer_pair_bases(test_case: PrimerPairTestCase) -> None:
"""
Test that the `PrimerPair.bases property returns the amplicon_sequence field
"""
assert test_case.primer_pair.bases == test_case.primer_pair.amplicon_sequence
@pytest.mark.parametrize(
"left_tail,right_tail",
[
("AAA", "GGG"),
("GGG", "AAAAA"),
],
)
def test_with_tails(left_tail: str, right_tail: str) -> None:
"""
Test that the with_tails method adds the correct tail(s) to
the PrimerPair and both Primers
"""
pp = PRIMER_PAIR_TEST_CASES[0].primer_pair
assert pp.left_primer.tail is None
assert pp.right_primer.tail is None
pp_with_tails = pp.with_tails(left_tail, right_tail)
assert pp_with_tails.left_primer.tail == left_tail
assert pp_with_tails.right_primer.tail == right_tail
pp_empty_str = pp_with_tails.with_tails("", "")
assert pp_empty_str.left_primer.tail == ""
assert pp_empty_str.right_primer.tail == ""
def test_with_names() -> None:
"""Test that the with_names method adds names to the PrimerPair and both Primers"""
# create a primer pair with no names
pp = PRIMER_PAIR_TEST_CASES[0].primer_pair
assert pp.name is None
assert pp.left_primer.name is None
assert pp.right_primer.name is None
# add names to all of them
pp_with_names = pp.with_names("pp_name", "lp_name", "rp_name")
assert pp_with_names.name == "pp_name"
assert pp_with_names.left_primer.name == "lp_name"
assert pp_with_names.right_primer.name == "rp_name"
# set them all to _empty_ names
pp_reset = pp_with_names.with_names("", "", "")
assert pp_reset.name == ""
assert pp_reset.left_primer.name == ""
assert pp_reset.right_primer.name == ""
def test_reference_mismatch() -> None:
"""
Test that a PrimerPair and both Primers all have a Span with
the same reference sequence
"""
pp = PRIMER_PAIR_TEST_CASES[0].primer_pair
with pytest.raises(ValueError, match="different references"):
replace(
pp,
left_primer=replace(
pp.left_primer,
span=replace(pp.left_primer.span, refname="no-name"),
),
)
with pytest.raises(ValueError, match="different references"):
replace(
pp,
right_primer=replace(
pp.right_primer,
span=replace(pp.right_primer.span, refname="no-name"),
),
)
def test_right_primer_before_left_primer() -> None:
"""Test that an exception is raised if the left primer starts after the right primer ends"""
pp = PRIMER_PAIR_TEST_CASES[0].primer_pair
with pytest.raises(ValueError, match="Left primer does not start before the right primer"):
replace(
pp,
left_primer=pp.right_primer,
right_primer=pp.left_primer,
)
def test_iter() -> None:
"""Test that the iter dunder method returns the left and right primers"""
pp = PRIMER_PAIR_TEST_CASES[0].primer_pair
assert list(iter(pp)) == [pp.left_primer, pp.right_primer]
@pytest.mark.parametrize(
"this, that, expected_by_amplicon_true, expected_by_amplicon_false",
[
# same primer
(
PrimerPairTestCase.primer_pair_from_left_primer(
Oligo(
bases="GATTACA",
tm=1.0,
penalty=2.0,
span=Span(refname="chr1", start=100, end=106, strand=Strand.POSITIVE),
)
),
PrimerPairTestCase.primer_pair_from_left_primer(
Oligo(
bases="GATTACA",
tm=1.0,
penalty=2.0,
span=Span(refname="chr1", start=100, end=106, strand=Strand.POSITIVE),
)
),
0,
0,
),
# different primer (chromosome)
(
PrimerPairTestCase.primer_pair_from_left_primer(
Oligo(
bases="GATTACA",
tm=1.0,
penalty=2.0,
span=Span(refname="chr1", start=100, end=106, strand=Strand.POSITIVE),
)
),
PrimerPairTestCase.primer_pair_from_left_primer(
Oligo(
bases="GATTACA",
tm=1.0,
penalty=2.0,
span=Span(refname="chr2", start=100, end=106, strand=Strand.POSITIVE),
)
),
-1,
-1,
),
# same primer when by amplicon, but different by primer
(
PrimerPairTestCase.primer_pair_from_left_primer(
Oligo(
bases="GATTAC",
tm=1.0,
penalty=2.0,
span=Span(refname="chr1", start=100, end=105, strand=Strand.POSITIVE),
),
right_offset=51,
),
PrimerPairTestCase.primer_pair_from_left_primer(
Oligo(
bases="GATTACA",
tm=1.0,
penalty=2.0,
span=Span(refname="chr1", start=100, end=106, strand=Strand.POSITIVE),
),
right_offset=50,
),
0,
-1,
),
],
)
def test_primer_pair_compare(
this: PrimerPair,
that: PrimerPair,
expected_by_amplicon_true: int,
expected_by_amplicon_false: int,
seq_dict: SequenceDictionary,
) -> None:
# expected_by_amplicon_true
assert expected_by_amplicon_true == PrimerPair.compare(
this=this, that=that, seq_dict=seq_dict, by_amplicon=True
)
assert -expected_by_amplicon_true == PrimerPair.compare(
this=that, that=this, seq_dict=seq_dict, by_amplicon=True
)
# expected_by_amplicon_false
assert expected_by_amplicon_false == PrimerPair.compare(
this=this, that=that, seq_dict=seq_dict, by_amplicon=False
)
assert -expected_by_amplicon_false == PrimerPair.compare(
this=that, that=this, seq_dict=seq_dict, by_amplicon=False
)
def test_calculate_amplicon_span() -> None:
left = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr1", 50, 59))
right = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr1", 150, 159))
assert PrimerPair.calculate_amplicon_span(left, right) == Span("chr1", 50, 159)
left = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr2", 50, 59))
right = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr3", 150, 159))
with pytest.raises(ValueError, match="different references"):
PrimerPair.calculate_amplicon_span(left, right)
left = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr1", 150, 159))
right = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr1", 50, 59))
with pytest.raises(ValueError, match="Left primer does not start before the right primer"):
PrimerPair.calculate_amplicon_span(left, right)
left = Oligo(name="l", bases="AACCGGTTAAACGTT", tm=60, penalty=1, span=Span("chr1", 150, 164))
right = Oligo(name="l", bases="AACCGGTTAA", tm=60, penalty=1, span=Span("chr1", 150, 159))
with pytest.raises(ValueError, match="Right primer ends before left primer ends"):
PrimerPair.calculate_amplicon_span(left, right)