offtarget
Classes¶
BwaAlnInteractive ¶
Bases:
Wrapper around a novel mode of 'bwa aln' that allows for "interactive" use of bwa to keep the process running and be able to send it chunks of reads periodically and get alignments back without waiting for a full batch of reads to be sent.
See:
- https://github.com/fulcrumgenomics/bwa-aln-interactive
- https://bioconda.github.io/recipes/bwa-aln-interactive/README.html
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the maximum number of hits to report - if more than this number of seed hits are found, report only the count and not each hit. |
|
|
reverse complement each query sequence before alignment. |
|
|
if True include hits to references with names ending in _alt, otherwise do not include them. |
|
the SAM alignment header. |
Source code in prymer/offtarget/bwa.py
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 | |
Functions¶
__init__ ¶
__init__(
ref: Path ,
max_hits: int ,
executable: str | Path = BWA_EXECUTABLE_NAME,
max_mismatches: int = 3,
max_mismatches_in_seed: int = 3,
max_gap_opens: int = 0,
max_gap_extensions: int = -1,
min_indel_to_end_distance: int = 3,
seed_length: int = 20,
reverse_complement: bool = False,
include_alt_hits: bool = False,
threads: Optional [int ] = None,
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref |
|
the path to the reference FASTA, which must be indexed with bwa. |
required |
max_hits |
|
the maximum number of hits to report - if more than this number of seed hits are found, report only the count and not each hit. |
required |
executable |
|
string or Path representation of the |
|
max_mismatches |
|
the maximum number of mismatches allowed in the full query sequence |
3
|
max_mismatches_in_seed |
|
the maximum number of mismatches allowed in the seed region |
3
|
max_gap_opens |
|
the maximum number of gap opens allowed in the full query sequence |
0
|
max_gap_extensions |
|
the maximum number of gap extensions allowed in the full query sequence |
-1
|
min_indel_to_end_distance |
|
do not place an indel within this many bp of the ends of the query sequence |
3
|
seed_length |
|
the length of the seed region |
20
|
reverse_complement |
|
reverse complement each query sequence before alignment |
False
|
include_alt_hits |
|
if true include hits to references with names ending in _alt, otherwise do not include them. |
False
|
threads |
|
the number of threads to use. If |
None
|
Source code in prymer/offtarget/bwa.py
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 | |
__signal_bwa ¶
Signals BWA to process the queries.
Source code in prymer/offtarget/bwa.py
close ¶
Gracefully terminates the underlying subprocess if it is still running.
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/offtarget/bwa.py
map_all ¶
Maps multiple queries and returns the results. This is more efficient than using
map_one on each query one-by-one as it batches reads to BWA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries |
|
the queries to map with BWA |
required |
Returns:
| Type | Description |
|---|---|
|
one |
Source code in prymer/offtarget/bwa.py
map_one ¶
Maps a single query to the genome and returns the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
|
the query to map with BWA |
required |
Returns:
| Type | Description |
|---|---|
|
a |
Source code in prymer/offtarget/bwa.py
to_hits ¶
Extracts the hits from the given alignment. Beyond the current alignment additional alignments are parsed from the XA SAM tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rec |
|
the given alignment |
required |
Source code in prymer/offtarget/bwa.py
BwaHit
dataclass
¶
Represents a single hit or alignment of a sequence to a location in the genome.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the reference name of the hit |
|
|
the start position of the hit (1-based inclusive) |
|
|
whether the hit is on the negative strand |
|
|
the cigar string returned by BWA |
|
|
the number of edits between the read and the reference |
Source code in prymer/offtarget/bwa.py
Attributes¶
Functions¶
__str__ ¶
Returns the string representation in bwa's XA tag format.
Source code in prymer/offtarget/bwa.py
build
staticmethod
¶
build(
refname: str ,
start: int ,
negative: bool ,
cigar: str ,
edits: int ,
revcomp: bool = False,
) -> BwaHit
Generates a hit object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refname |
|
the reference name of the hit |
required |
start |
|
the start position of the hit (1-based inclusive) |
required |
negative |
|
whether the hit is on the negative strand |
required |
cigar |
|
the cigar string returned by BWA |
required |
edits |
|
the number of edits between the read and the reference |
required |
revcomp |
|
whether the reverse-complement of the query sequence was fed to BWA, in which case various fields should be negated/reversed in the Hit |
False
|
Returns:
| Type | Description |
|---|---|
|
A Hit that represents the mapping of the original query sequence that was supplied |
Source code in prymer/offtarget/bwa.py
BwaResult
dataclass
¶
Represents zero or more hits or alignments found by BWA for the given query.
The number of hits found may be more than the number of hits listed in the hits attribute.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the query (as given, no RC'ing) |
|
|
the total number of hits found by bwa (this may be more than len(hits)) |
|
|
the subset of hits that were reported by bwa |
Source code in prymer/offtarget/bwa.py
OffTargetDetector ¶
Bases:
A class for detecting off-target mappings of primers and primer pairs that uses a custom version of "bwa aln" named "bwa-aln-interactive".
OffTargetDetector uses a custom, interactive
version of bwa aln to perform
off-target detection. This approach is faster and more sensitive than traditional isPCR and in
addition can correctly detect primers that are repetitive and contain many thousands or millions
of mappings to the genome.
Note that while this class invokes BWA with multiple threads, it is not itself thread-safe. Only one thread at a time should invoke methods on this class without external synchronization.
Off-target detection may be performed for individual primers and/or primer pairs.
To detect off-target hits for individual primers, use the OffTargetDetector.filters() method,
which will remove any primers that have more than the specified number of maximum hits against
the reference.
To detect off-target amplification of primer pairs, use the OffTargetDetector.check_one() or
OffTargetDetector.check_all() methods. These methods screen the individual primers in each
pair for off-target hits, and verify that the possible amplicons inferred by the primers'
alignments does not exceed the specified maximum number of primer pair hits.
Source code in prymer/offtarget/offtarget_detector.py
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 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 | |
Functions¶
__exit__ ¶
__exit__(
exc_type: Optional [type [BaseException ]],
exc_value: Optional [BaseException ],
traceback: Optional [TracebackType ],
) -> None
Gracefully terminates any running subprocesses.
Source code in prymer/offtarget/offtarget_detector.py
__init__ ¶
__init__(
ref: Path ,
max_primer_hits: int ,
max_primer_pair_hits: int ,
three_prime_region_length: int ,
max_mismatches_in_three_prime_region: int ,
max_mismatches: int ,
max_gap_opens: int = 0,
max_gap_extends: int = -1,
max_amplicon_size: int = 1000,
min_primer_pair_hits: int = 1,
cache_results: bool = True,
threads: Optional [int ] = None,
keep_spans: bool = True,
keep_primer_spans: bool = True,
executable: str | Path = BWA_EXECUTABLE_NAME,
) -> None
Initialize an [[OffTargetDetector]].
This class accepts a large number of parameters to control the behavior of the off-target detection. The parameters are used in the various aspects of off-target detection as follows:
- Alignment (off-target hit detection):
ref,executable,threads,three_prime_region_length,max_mismatches_in_three_prime_region,max_mismatches, andmax_primer_hits. - Filtering of individual primers:
max_primer_hits. - Checking of primer pairs:
max_primer_hits,min_primer_pair_hits,max_primer_pair_hits, andmax_amplicon_size.
The three_prime_region_length parameter is used as the seed length for bwa aln.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref |
|
the reference genome fasta file (must be indexed with BWA) |
required |
max_primer_hits |
|
the maximum number of hits an individual primer can have in the genome before it is considered an invalid primer, and all primer pairs containing the primer failed. Must be greater than or equal to 0. |
required |
max_primer_pair_hits |
|
the maximum number of amplicons a primer pair can make and be considered passing. Must be greater than or equal to 0. |
required |
min_primer_pair_hits |
|
The minimum number of amplicons a primer pair can make and be considered passing. (In most cases, this is the number of amplicons a primer pair is expected to generate.) The default is 1, which is appropriate when the primer pair is being evaluated for off-target hits against the same reference genome from which the primers were generated. If the primer pair was generated from a different reference sequence, it may be appropriate to set this value to 0. Must be greater than or equal to 0. |
1
|
three_prime_region_length |
|
the number of bases at the 3' end of the primer in which the
parameter |
required |
max_mismatches_in_three_prime_region |
|
the maximum number of mismatches that are
tolerated in the three prime region of each primer defined by
|
required |
max_mismatches |
|
the maximum number of mismatches allowed in the full length primer (including any in the three prime region). Must be greater than or equal to 0. |
required |
max_gap_opens |
|
the maximum number of gaps (insertions or deletions) allowable in an alignment of a oligo to the reference. Must be greater than or equal to 0. |
0
|
max_gap_extends |
|
the maximum number of gap extensions allowed; extending a gap beyond a single base costs 1 gap extension. Can be set to -1 to allow unlimited extensions up to max diffs (aka max mismatches), while disallowing "long gaps". Must be greater than or equal to -1. |
-1
|
max_amplicon_size |
|
the maximum amplicon size to consider amplifiable. Must be greater than 0. |
1000
|
cache_results |
|
if True, cache results for faster re-querying |
True
|
threads |
|
the number of threads to use when invoking bwa |
None
|
keep_spans |
|
if True, [[OffTargetResult]] objects will be reported with amplicon spans populated, otherwise not |
True
|
keep_primer_spans |
|
if True, [[OffTargetResult]] objects will be reported with left and right primer spans |
True
|
executable |
|
string or Path representation of the |
|
Raises:
| Type | Description |
|---|---|
|
If |
|
If any of |
|
If |
|
If |
|
If |
|
If |
|
If |
Source code in prymer/offtarget/offtarget_detector.py
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 | |
check_all ¶
Check a collection of primer pairs for off-target amplification.
This method maps each primer to the specified reference with bwa aln to search for
off-target hits. Possible amplicons are identified from the pairwise combinations of these
alignments, up to the specified max_amplicon_size.
Primer pairs are marked as passing if both of the following are true:
1. Each primer has no more than max_primer_hits alignments to the genome.
2. The size of the set of possible amplicons does not exceed max_primer_pair_hits, and is
at least min_primer_pair_hits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primer_pairs |
|
The primer pairs to check. |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary mapping each checked primer pair to its off-target detection results. |
|
See |
Source code in prymer/offtarget/offtarget_detector.py
check_one ¶
Check an individual primer pair for off-target amplification.
See OffTargetDetector.check_all() for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primer_pair |
|
The primer pair to check. |
required |
Returns:
| Type | Description |
|---|---|
|
The results of the off-target detection. |
|
See |
Source code in prymer/offtarget/offtarget_detector.py
filter ¶
Remove primers that have more than max_primer_hits mappings to the genome.
This method maps each primer to the specified reference with bwa aln to search for
off-target hits. Note that when the reference includes the sequence from which the primers
were designed, the on-target hit will be included in the hit count. max_primer_hits should
be set to at least 1 in this case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primers |
|
A list of primers to filter. |
required |
Returns:
| Type | Description |
|---|---|
|
The input primers, filtered to remove any primers with more than |
|
mappings to the genome. |
Source code in prymer/offtarget/offtarget_detector.py
mappings_of ¶
Map primers to the reference genome using bwa aln.
Alignment results may be optionally cached for efficiency. Set cache_results to True
when instantiating the [[OffTargetDetector]] to enable caching.
Any primers without cached results, or all primers when cache_results is False, will be
mapped to the reference genome using bwa aln and the specified alignment parameters.
Note: The reverse complement of each primer sequence is used for mapping. The query
sequence in each BwaResult will match the input primer sequence, as will the sequences
used as keys in the output dictionary. However, the coordinates reported in each BwaHit
associated with a result will correspond to complementary sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primers |
|
A list of primers to map. |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary mapping each primer's sequence to its alignment results. |
|
See |
Source code in prymer/offtarget/offtarget_detector.py
OffTargetResult
dataclass
¶
Information obtained by running a single primer pair through the off-target detector.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the primer pair submitted |
|
|
True if neither primer exceeds the specified maximum number of hits, and the primer
pair would generate an acceptable number of amplicons in the reference genome (i.e.
|
|
|
True if this result is part of a cache, False otherwise. This is useful for testing |
|
|
The set of mappings of the primer pair to the genome (i.e., the region spanned by the
inferred amplicon). This list will be empty if the generating [[OffTargetDetector]] was
constructed with |
|
|
the list of mappings for the left primer, independent of the pair mappings, or an empty list |
|
|
the list of mappings for the right primer, independent of the pair mappings, or an empty list |
Source code in prymer/offtarget/offtarget_detector.py
Query
dataclass
¶
Represents a single query sequence for mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the identifier for the query (e.g. read name) |
|
|
the query bases |
Source code in prymer/offtarget/bwa.py
Functions¶
to_fastq ¶
Returns the multi-line FASTQ string representation of this query
Source code in prymer/offtarget/bwa.py
Modules¶
bwa ¶
Methods and Classes to run and interact with BWA¶
The BwaAlnInteractive class is used to map
a list of queries to the reference genome. A single query may be mapped using
map_one(), while multiple queries may
be mapped using map_all(). The latter
is more efficient than mapping queries one-by-one.
The queries are provided to these methods using the Query
class, which contains the unique identifier for the query and the bases to query. These methods
return a BwaResult, which represents zero or more hits
(or alignments) found by BWA for the given query. Each hit is represented by a
BwaHit object. In some cases, BWA will write fewer (or no)
hits in the "XA" tag than the total number hits reported in the "HN". This occurs when BWA finds more
hits than max_hits (see bwt aln -X).
Use of this module requires installation of a custom version of BWA named bwa-aln-interactive.
See:
- https://github.com/fulcrumgenomics/bwa-aln-interactive
- https://bioconda.github.io/recipes/bwa-aln-interactive/README.html
Example¶
>>> from pathlib import Path
>>> ref_fasta = Path("./tests/offtarget/data/miniref.fa")
>>> query = Query(bases="TCTACTAAAAATACAAAAAATTAGCTGGGCATGATGGCATGCACCTGTAATCCCGCTACT", id="NA")
>>> bwa = BwaAlnInteractive(ref=ref_fasta, max_hits=1)
>>> result = bwa.map_one(query=query.bases, id=query.id)
>>> result.hit_count
1
>>> len(result.hits)
1
>>> result.hits[0]
BwaHit(refname='chr1', start=61, negative=False, cigar=Cigar(elements=(CigarElement(length=60, operator=<CigarOp.M: (0, 'M', True, True)>),)), edits=0)
>>> query = Query(bases="AAAAAA", id="NA")
>>> bwa.map_all(queries=[query])
[BwaResult(query=Query(id='NA', bases='AAAAAA'), hit_count=3968, hits=[])]
>>> bwa.close()
True
Attributes¶
BWA_AUX_EXTENSIONS
module-attribute
¶
The file extensions for BWA index files
BWA_EXECUTABLE_NAME
module-attribute
¶
The executable name for the interactive build of bwa aln.
MAX_GAP_EXTENSIONS
module-attribute
¶
The default maximum number of gap extensions allowed in the full query sequence
MAX_GAP_OPENS
module-attribute
¶
The default maximum number of gap opens allowed in the full query sequence
MAX_MISMATCHES
module-attribute
¶
The default maximum number of mismatches allowed in the full query sequence
MAX_MISMATCHES_IN_SEED
module-attribute
¶
The default maximum number of mismatches allowed in the seed region
Classes¶
BwaAlnInteractive ¶
Bases:
Wrapper around a novel mode of 'bwa aln' that allows for "interactive" use of bwa to keep the process running and be able to send it chunks of reads periodically and get alignments back without waiting for a full batch of reads to be sent.
See:
- https://github.com/fulcrumgenomics/bwa-aln-interactive
- https://bioconda.github.io/recipes/bwa-aln-interactive/README.html
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the maximum number of hits to report - if more than this number of seed hits are found, report only the count and not each hit. |
|
|
reverse complement each query sequence before alignment. |
|
|
if True include hits to references with names ending in _alt, otherwise do not include them. |
|
the SAM alignment header. |
Source code in prymer/offtarget/bwa.py
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 | |
Functions¶
__init__(
ref: Path ,
max_hits: int ,
executable: str | Path = BWA_EXECUTABLE_NAME,
max_mismatches: int = 3,
max_mismatches_in_seed: int = 3,
max_gap_opens: int = 0,
max_gap_extensions: int = -1,
min_indel_to_end_distance: int = 3,
seed_length: int = 20,
reverse_complement: bool = False,
include_alt_hits: bool = False,
threads: Optional [int ] = None,
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref |
|
the path to the reference FASTA, which must be indexed with bwa. |
required |
max_hits |
|
the maximum number of hits to report - if more than this number of seed hits are found, report only the count and not each hit. |
required |
executable |
|
string or Path representation of the |
|
max_mismatches |
|
the maximum number of mismatches allowed in the full query sequence |
3
|
max_mismatches_in_seed |
|
the maximum number of mismatches allowed in the seed region |
3
|
max_gap_opens |
|
the maximum number of gap opens allowed in the full query sequence |
0
|
max_gap_extensions |
|
the maximum number of gap extensions allowed in the full query sequence |
-1
|
min_indel_to_end_distance |
|
do not place an indel within this many bp of the ends of the query sequence |
3
|
seed_length |
|
the length of the seed region |
20
|
reverse_complement |
|
reverse complement each query sequence before alignment |
False
|
include_alt_hits |
|
if true include hits to references with names ending in _alt, otherwise do not include them. |
False
|
threads |
|
the number of threads to use. If |
None
|
Source code in prymer/offtarget/bwa.py
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 | |
Signals BWA to process the queries.
Source code in prymer/offtarget/bwa.py
Gracefully terminates the underlying subprocess if it is still running.
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/offtarget/bwa.py
Maps multiple queries and returns the results. This is more efficient than using
map_one on each query one-by-one as it batches reads to BWA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries |
|
the queries to map with BWA |
required |
Returns:
| Type | Description |
|---|---|
|
one |
Source code in prymer/offtarget/bwa.py
Maps a single query to the genome and returns the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
|
the query to map with BWA |
required |
Returns:
| Type | Description |
|---|---|
|
a |
Source code in prymer/offtarget/bwa.py
Extracts the hits from the given alignment. Beyond the current alignment additional alignments are parsed from the XA SAM tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rec |
|
the given alignment |
required |
Source code in prymer/offtarget/bwa.py
BwaHit
dataclass
¶
Represents a single hit or alignment of a sequence to a location in the genome.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the reference name of the hit |
|
|
the start position of the hit (1-based inclusive) |
|
|
whether the hit is on the negative strand |
|
|
the cigar string returned by BWA |
|
|
the number of edits between the read and the reference |
Source code in prymer/offtarget/bwa.py
Attributes¶
Functions¶
Returns the string representation in bwa's XA tag format.
Source code in prymer/offtarget/bwa.py
staticmethod
¶build(
refname: str ,
start: int ,
negative: bool ,
cigar: str ,
edits: int ,
revcomp: bool = False,
) -> BwaHit
Generates a hit object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refname |
|
the reference name of the hit |
required |
start |
|
the start position of the hit (1-based inclusive) |
required |
negative |
|
whether the hit is on the negative strand |
required |
cigar |
|
the cigar string returned by BWA |
required |
edits |
|
the number of edits between the read and the reference |
required |
revcomp |
|
whether the reverse-complement of the query sequence was fed to BWA, in which case various fields should be negated/reversed in the Hit |
False
|
Returns:
| Type | Description |
|---|---|
|
A Hit that represents the mapping of the original query sequence that was supplied |
Source code in prymer/offtarget/bwa.py
BwaResult
dataclass
¶
Represents zero or more hits or alignments found by BWA for the given query.
The number of hits found may be more than the number of hits listed in the hits attribute.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the query (as given, no RC'ing) |
|
|
the total number of hits found by bwa (this may be more than len(hits)) |
|
|
the subset of hits that were reported by bwa |
Source code in prymer/offtarget/bwa.py
Query
dataclass
¶
Represents a single query sequence for mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the identifier for the query (e.g. read name) |
|
|
the query bases |
Source code in prymer/offtarget/bwa.py
Functions¶
Returns the multi-line FASTQ string representation of this query
Source code in prymer/offtarget/bwa.py
Modules¶
offtarget_detector ¶
Methods and classes to detect off-target mappings for primers and primer pairs¶
The OffTargetDetector uses
BwaAlnInteractive to search for off targets for
one or more primers or primer pairs.
The filter() method
filters an iterable of Primers to return only those that have less than a given maximum number of off-target hits
to the genome.
>>> from pathlib import Path
>>> from prymer.api.span import Strand
>>> ref_fasta = Path("./tests/offtarget/data/miniref.fa")
>>> left_primer = Oligo(bases="AAAAA", tm=37, penalty=0, span=Span("chr1", start=67, end=71))
>>> right_primer = Oligo(bases="TTTTT", tm=37, penalty=0, span=Span("chr1", start=75, end=79, strand=Strand.NEGATIVE))
>>> detector = OffTargetDetector(ref=ref_fasta, max_primer_hits=204, max_primer_pair_hits=1, three_prime_region_length=20, max_mismatches_in_three_prime_region=0, max_mismatches=0, max_amplicon_size=250)
>>> len(detector.filter(primers=[left_primer, right_primer])) # keep all
2
>>> detector.close()
>>> detector = OffTargetDetector(ref=ref_fasta, max_primer_hits=203, max_primer_pair_hits=1, three_prime_region_length=20, max_mismatches_in_three_prime_region=0, max_mismatches=0, max_amplicon_size=250)
>>> len(detector.filter(primers=[left_primer, right_primer])) # keep none
0
>>> detector.close()
The check_one() and
the check_all()
methods check one or multiple primer pairs respectively for off-target mappings, returning an
OffTargetResult, or mapping from
each PrimerPair to its corresponding OffTargetResult. This result contains information about
the off-target mappings.
>>> detector = OffTargetDetector(ref=ref_fasta, max_primer_hits=1, max_primer_pair_hits=1, three_prime_region_length=20, max_mismatches_in_three_prime_region=0, max_mismatches=0, max_amplicon_size=250)
>>> primer_pair = PrimerPair(left_primer=left_primer, right_primer=right_primer, amplicon_tm=37, penalty=0.0)
>>> detector.check_one(primer_pair=primer_pair)
OffTargetResult(primer_pair=..., passes=False, cached=False, spans=[], left_primer_spans=[], right_primer_spans=[])
>>> list(detector.check_all(primer_pairs=[primer_pair]).values())
[OffTargetResult(primer_pair=..., passes=False, cached=True, spans=[], left_primer_spans=[], right_primer_spans=[])]
>>> detector = OffTargetDetector(ref=ref_fasta, max_primer_hits=204, max_primer_pair_hits=856, three_prime_region_length=20, max_mismatches_in_three_prime_region=0, max_mismatches=0, max_amplicon_size=250)
>>> result = detector.check_one(primer_pair=primer_pair)
>>> len(result.spans)
856
>>> len(result.left_primer_spans)
204
>>> len(result.right_primer_spans)
204
Finally, the mappings_of()
method maps individual primers (Primers).
>>> p1: Oligo = Oligo(tm=37, penalty=0, span=Span(refname="chr1", start=1, end=30), bases="CAGGTGGATCATGAGGTCAGGAGTTCAAGA")
>>> p2: Oligo = Oligo(tm=37, penalty=0, span=Span(refname="chr1", start=61, end=93, strand=Strand.NEGATIVE), bases="CATGCCCAGCTAATTTTTTGTATTTTTAGTAGA")
>>> results_dict: dict[str, BwaResult] = detector.mappings_of(primers=[p1, p2])
>>> list(results_dict.keys())
['CAGGTGGATCATGAGGTCAGGAGTTCAAGA', 'CATGCCCAGCTAATTTTTTGTATTTTTAGTAGA']
>>> results = list(results_dict.values())
>>> results[0]
BwaResult(query=Query(id='CAGGTGGATCATGAGGTCAGGAGTTCAAGA', bases='CAGGTGGATCATGAGGTCAGGAGTTCAAGA'), hit_count=1, hits=[...])
>>> results[0].hits[0]
BwaHit(refname='chr1', start=1, negative=False, cigar=Cigar(elements=(CigarElement(length=30, operator=<CigarOp.M: (0, 'M', True, True)>),)), edits=0)
>>> results[1]
BwaResult(query=Query(id='CATGCCCAGCTAATTTTTTGTATTTTTAGTAGA', bases='CATGCCCAGCTAATTTTTTGTATTTTTAGTAGA'), hit_count=1, hits=[...])
>>> results[1].hits[0]
BwaHit(refname='chr1', start=61, negative=True, cigar=Cigar(elements=(CigarElement(length=33, operator=<CigarOp.M: (0, 'M', True, True)>),)), edits=0)
Attributes¶
MINIMUM_THREE_PRIME_REGION_LENGTH
module-attribute
¶
Minimum allowable seed length for the 3' region.
ReferenceName
module-attribute
¶
Alias for a reference sequence name.
Classes¶
OffTargetDetector ¶
Bases:
A class for detecting off-target mappings of primers and primer pairs that uses a custom version of "bwa aln" named "bwa-aln-interactive".
OffTargetDetector uses a custom, interactive
version of bwa aln to perform
off-target detection. This approach is faster and more sensitive than traditional isPCR and in
addition can correctly detect primers that are repetitive and contain many thousands or millions
of mappings to the genome.
Note that while this class invokes BWA with multiple threads, it is not itself thread-safe. Only one thread at a time should invoke methods on this class without external synchronization.
Off-target detection may be performed for individual primers and/or primer pairs.
To detect off-target hits for individual primers, use the OffTargetDetector.filters() method,
which will remove any primers that have more than the specified number of maximum hits against
the reference.
To detect off-target amplification of primer pairs, use the OffTargetDetector.check_one() or
OffTargetDetector.check_all() methods. These methods screen the individual primers in each
pair for off-target hits, and verify that the possible amplicons inferred by the primers'
alignments does not exceed the specified maximum number of primer pair hits.
Source code in prymer/offtarget/offtarget_detector.py
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 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 | |
Functions¶
__exit__(
exc_type: Optional [type [BaseException ]],
exc_value: Optional [BaseException ],
traceback: Optional [TracebackType ],
) -> None
Gracefully terminates any running subprocesses.
Source code in prymer/offtarget/offtarget_detector.py
__init__(
ref: Path ,
max_primer_hits: int ,
max_primer_pair_hits: int ,
three_prime_region_length: int ,
max_mismatches_in_three_prime_region: int ,
max_mismatches: int ,
max_gap_opens: int = 0,
max_gap_extends: int = -1,
max_amplicon_size: int = 1000,
min_primer_pair_hits: int = 1,
cache_results: bool = True,
threads: Optional [int ] = None,
keep_spans: bool = True,
keep_primer_spans: bool = True,
executable: str | Path = BWA_EXECUTABLE_NAME,
) -> None
Initialize an [[OffTargetDetector]].
This class accepts a large number of parameters to control the behavior of the off-target detection. The parameters are used in the various aspects of off-target detection as follows:
- Alignment (off-target hit detection):
ref,executable,threads,three_prime_region_length,max_mismatches_in_three_prime_region,max_mismatches, andmax_primer_hits. - Filtering of individual primers:
max_primer_hits. - Checking of primer pairs:
max_primer_hits,min_primer_pair_hits,max_primer_pair_hits, andmax_amplicon_size.
The three_prime_region_length parameter is used as the seed length for bwa aln.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref |
|
the reference genome fasta file (must be indexed with BWA) |
required |
max_primer_hits |
|
the maximum number of hits an individual primer can have in the genome before it is considered an invalid primer, and all primer pairs containing the primer failed. Must be greater than or equal to 0. |
required |
max_primer_pair_hits |
|
the maximum number of amplicons a primer pair can make and be considered passing. Must be greater than or equal to 0. |
required |
min_primer_pair_hits |
|
The minimum number of amplicons a primer pair can make and be considered passing. (In most cases, this is the number of amplicons a primer pair is expected to generate.) The default is 1, which is appropriate when the primer pair is being evaluated for off-target hits against the same reference genome from which the primers were generated. If the primer pair was generated from a different reference sequence, it may be appropriate to set this value to 0. Must be greater than or equal to 0. |
1
|
three_prime_region_length |
|
the number of bases at the 3' end of the primer in which the
parameter |
required |
max_mismatches_in_three_prime_region |
|
the maximum number of mismatches that are
tolerated in the three prime region of each primer defined by
|
required |
max_mismatches |
|
the maximum number of mismatches allowed in the full length primer (including any in the three prime region). Must be greater than or equal to 0. |
required |
max_gap_opens |
|
the maximum number of gaps (insertions or deletions) allowable in an alignment of a oligo to the reference. Must be greater than or equal to 0. |
0
|
max_gap_extends |
|
the maximum number of gap extensions allowed; extending a gap beyond a single base costs 1 gap extension. Can be set to -1 to allow unlimited extensions up to max diffs (aka max mismatches), while disallowing "long gaps". Must be greater than or equal to -1. |
-1
|
max_amplicon_size |
|
the maximum amplicon size to consider amplifiable. Must be greater than 0. |
1000
|
cache_results |
|
if True, cache results for faster re-querying |
True
|
threads |
|
the number of threads to use when invoking bwa |
None
|
keep_spans |
|
if True, [[OffTargetResult]] objects will be reported with amplicon spans populated, otherwise not |
True
|
keep_primer_spans |
|
if True, [[OffTargetResult]] objects will be reported with left and right primer spans |
True
|
executable |
|
string or Path representation of the |
|
Raises:
| Type | Description |
|---|---|
|
If |
|
If any of |
|
If |
|
If |
|
If |
|
If |
|
If |
Source code in prymer/offtarget/offtarget_detector.py
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 | |
Check a collection of primer pairs for off-target amplification.
This method maps each primer to the specified reference with bwa aln to search for
off-target hits. Possible amplicons are identified from the pairwise combinations of these
alignments, up to the specified max_amplicon_size.
Primer pairs are marked as passing if both of the following are true:
1. Each primer has no more than max_primer_hits alignments to the genome.
2. The size of the set of possible amplicons does not exceed max_primer_pair_hits, and is
at least min_primer_pair_hits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primer_pairs |
|
The primer pairs to check. |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary mapping each checked primer pair to its off-target detection results. |
|
See |
Source code in prymer/offtarget/offtarget_detector.py
Check an individual primer pair for off-target amplification.
See OffTargetDetector.check_all() for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primer_pair |
|
The primer pair to check. |
required |
Returns:
| Type | Description |
|---|---|
|
The results of the off-target detection. |
|
See |
Source code in prymer/offtarget/offtarget_detector.py
Remove primers that have more than max_primer_hits mappings to the genome.
This method maps each primer to the specified reference with bwa aln to search for
off-target hits. Note that when the reference includes the sequence from which the primers
were designed, the on-target hit will be included in the hit count. max_primer_hits should
be set to at least 1 in this case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primers |
|
A list of primers to filter. |
required |
Returns:
| Type | Description |
|---|---|
|
The input primers, filtered to remove any primers with more than |
|
mappings to the genome. |
Source code in prymer/offtarget/offtarget_detector.py
Map primers to the reference genome using bwa aln.
Alignment results may be optionally cached for efficiency. Set cache_results to True
when instantiating the [[OffTargetDetector]] to enable caching.
Any primers without cached results, or all primers when cache_results is False, will be
mapped to the reference genome using bwa aln and the specified alignment parameters.
Note: The reverse complement of each primer sequence is used for mapping. The query
sequence in each BwaResult will match the input primer sequence, as will the sequences
used as keys in the output dictionary. However, the coordinates reported in each BwaHit
associated with a result will correspond to complementary sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primers |
|
A list of primers to map. |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary mapping each primer's sequence to its alignment results. |
|
See |
Source code in prymer/offtarget/offtarget_detector.py
OffTargetResult
dataclass
¶
Information obtained by running a single primer pair through the off-target detector.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
the primer pair submitted |
|
|
True if neither primer exceeds the specified maximum number of hits, and the primer
pair would generate an acceptable number of amplicons in the reference genome (i.e.
|
|
|
True if this result is part of a cache, False otherwise. This is useful for testing |
|
|
The set of mappings of the primer pair to the genome (i.e., the region spanned by the
inferred amplicon). This list will be empty if the generating [[OffTargetDetector]] was
constructed with |
|
|
the list of mappings for the left primer, independent of the pair mappings, or an empty list |
|
|
the list of mappings for the right primer, independent of the pair mappings, or an empty list |