primer3
Primer3 Class and Methods¶
This module contains the Primer3 class, a class to
facilitate exchange of input and output data with Primer3, a command line tool.
Similar to the NtThermoAlign and
BwaAlnInteractive classes in the prymer
library, the Primer3 class extends the
ExecutableRunner base class to
initiate an underlying subprocess, read and write input and output data, and gracefully terminate
any remaining subprocesses.
Examples¶
The genome FASTA must be provided to the Primer3 constructor, such that design and target
nucleotide sequences can be retrieved. The full path to the primer3 executable can provided,
otherwise it is assumed to be on the PATH. Furthermore, optionally a
VariantLookup may be provided to
hard-mask the design and target regions as to avoid design primers over polymorphic sites.
>>> from pathlib import Path
>>> from prymer.api.variant_lookup import VariantLookup, VariantOverlapDetector
>>> genome_fasta = Path("./tests/primer3/data/miniref.fa")
>>> genome_vcf = Path("./tests/primer3/data/miniref.variants.vcf.gz")
>>> variant_lookup: VariantLookup = VariantOverlapDetector(vcf_paths=[genome_vcf], min_maf=0.01, include_missing_mafs=False)
>>> designer = Primer3(genome_fasta=genome_fasta, variant_lookup=variant_lookup)
The get_design_sequences() method on Primer3 is used to retrieve the soft and hard masked
sequences for a given region. The hard-masked sequence replaces bases with Ns that overlap
polymorphic sites found in the VariantLookup provided in the constructor.
>>> design_region = Span(refname="chr2", start=9095, end=9120)
>>> soft_masked, hard_masked = designer.get_design_sequences(region=design_region)
>>> soft_masked
'AGTTACATTACAAAAGGCAGATTTCA'
>>> hard_masked
'AGTTANNNTACAAAAGGCAGATTTCA'
The design() method on Primer3 is used to design the primers given a
Primer3Input. The latter includes all the
parameters and target region.
>>> from prymer.primer3.primer3_parameters import PrimerAndAmpliconParameters
>>> from prymer.api import MinOptMax
>>> target = Span(refname="chr1", start=201, end=250, strand=Strand.POSITIVE)
>>> params = PrimerAndAmpliconParameters( amplicon_sizes=MinOptMax(min=100, max=250, opt=200), amplicon_tms=MinOptMax(min=55.0, max=100.0, opt=70.0), primer_sizes=MinOptMax(min=29, max=31, opt=30), primer_tms=MinOptMax(min=63.0, max=67.0, opt=65.0), primer_gcs=MinOptMax(min=30.0, max=65.0, opt=45.0), )
>>> design_input = Primer3Input( target=target, primer_and_amplicon_params=params, task=DesignLeftPrimersTask(), )
>>> left_result = designer.design(design_input=design_input)
The left_result returns the Primer3Result
container class. It contains two attributes:
1. designs: filtered and ordered (by objective function score) list of primer pairs or
single primers that were returned by Primer3.
2. failures: ordered list of Primer3Failures
detailing design failure reasons and corresponding count.
In this case, there are two failures reasons:
>>> for failure in left_result.failures: print(failure)
Primer3Failure(reason=<Primer3FailureReason.HIGH_TM: 'high tm'>, count=406)
Primer3Failure(reason=<Primer3FailureReason.GC_CONTENT: 'GC content failed'>, count=91)
While the designs attribute on Primer3Result may be used to access the list of primers or
primer pairs, it is more convenient to use the primers() and primer_pairs() methods
to return the designed primers or primer pairs (use the method corresponding to the input task) so
that the proper type is returned (i.e. Primer or
PrimerPair).
>>> for primer in left_result.primers(): print(primer)
TCTGAACAGGACGAACTGGATTTCCTCAT 65.686 1.953897 chr1:163-191:+
CTCTGAACAGGACGAACTGGATTTCCTCAT 66.152 2.293213 chr1:162-191:+
TCTGAACAGGACGAACTGGATTTCCTCATG 66.33 2.514048 chr1:163-192:+
AACAGGACGAACTGGATTTCCTCATGGAA 66.099 2.524986 chr1:167-195:+
CTGAACAGGACGAACTGGATTTCCTCATG 65.47 2.556859 chr1:164-192:+
Finally, the designer should be closed to terminate the sub-process:
Primer3 is also context a manager, and so can be used with a with clause:
Attributes¶
OligoLikeType
module-attribute
¶
Type variable for a Primer3Result, which must implement OligoLike
Classes¶
Primer3 ¶
Bases:
Enables interaction with command line tool, primer3.
Attributes:
| Name | Type | Description |
|---|---|---|
|
file handle to the open reference genome file |
|
|
|
the sequence dictionary that corresponds to the provided reference genome file |
Source code in prymer/primer3/primer3.py
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 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | |
Functions¶
__init__ ¶
__init__(
genome_fasta: Path ,
executable: Optional [str ] = None,
variant_lookup: Optional [VariantLookup ] = None,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genome_fasta |
|
Path to reference genome .fasta file |
required |
executable |
|
string representation of the path to primer3_core |
None
|
variant_lookup |
|
VariantLookup object to facilitate hard-masking variants |
None
|
Assumes the sequence dictionary is located adjacent to the .fasta file and has the same base name with a .dict suffix.
Source code in prymer/primer3/primer3.py
close ¶
Closes fasta file regardless of underlying subprocess status. Logs an error if the underlying subprocess is not successfully closed.
Returns:
| Name | Type | Description |
|---|---|---|
True |
|
if the subprocess was terminated successfully |
False |
|
if the subprocess failed to terminate or was not already running |
Source code in prymer/primer3/primer3.py
design ¶
Designs primers, primer pairs, and/or internal probes given a target region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
design_input |
|
encapsulates the target region, design task, specifications, and scoring penalties |
required |
Returns:
| Type | Description |
|---|---|
|
Primer3Result containing both the valid and failed designs emitted by Primer3 |
Raises:
| Type | Description |
|---|---|
|
if underlying subprocess is not alive |
|
if Primer3 returns errors or does not return output |
|
if Primer3 output is malformed |
|
if an unknown design task is given |
Source code in prymer/primer3/primer3.py
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 | |
get_design_sequences ¶
Extracts the reference sequence that corresponds to the design region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region |
|
the region of the genome to be extracted |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple of two sequences: the sequence for the region, and the sequence for the region |
|
with variants hard-masked as Ns |
Source code in prymer/primer3/primer3.py
Primer3Failure
dataclass
¶
Bases:
Encapsulates how many designs failed for a given reason.
Extends the fgpyo.util.metric.Metric class, which will facilitate writing out results for
primer design QC etc.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the reason the design failed |
|
|
how many designs failed |
Source code in prymer/primer3/primer3.py
Primer3Result
dataclass
¶
Bases:
Encapsulates Primer3 design results (both valid designs and failures).
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
filtered for out-of-spec characteristics and ordered (by objective function score) list of primer pairs or single oligos that were returned by Primer3 |
|
|
ordered list of Primer3Failures detailing design failure reasons and corresponding count |
Source code in prymer/primer3/primer3.py
Functions¶
as_primer_pair_result ¶
Returns this Primer3Result assuming the design results are of type PrimerPair.
Source code in prymer/primer3/primer3.py
as_primer_result ¶
Returns this Primer3Result assuming the design results are of type Primer.
Source code in prymer/primer3/primer3.py
primer_pairs ¶
Returns the design results as a list PrimerPairs
Source code in prymer/primer3/primer3.py
primers ¶
Returns the design results as a list Primers