-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit primer pairs in penalty order. #87
Open
tfenne
wants to merge
3
commits into
main
Choose a base branch
from
tf_emit_pairs_in_order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
.vscode/ | ||
.idea/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,11 @@ | |
|
||
""" | ||
|
||
from collections.abc import Sequence | ||
from pathlib import Path | ||
from typing import Iterator | ||
from typing import Optional | ||
from typing import Tuple | ||
|
||
from pysam import FastaFile | ||
|
||
|
@@ -79,20 +81,21 @@ def score( | |
# The penalty for the amplicon melting temperature. | ||
# The difference in melting temperature between the calculated and optimal is weighted by the | ||
# product melting temperature. | ||
tm = amplicon_tm | ||
tm_penalty: float | ||
if tm > amplicon_tms.opt: | ||
tm_penalty = (tm - amplicon_tms.opt) * weights.product_tm_gt | ||
if amplicon_tms.opt == 0.0: | ||
tm_penalty = 0.0 | ||
elif amplicon_tm > amplicon_tms.opt: | ||
tm_penalty = (amplicon_tm - amplicon_tms.opt) * weights.product_tm_gt | ||
else: | ||
tm_penalty = (amplicon_tms.opt - tm) * weights.product_tm_lt | ||
tm_penalty = (amplicon_tms.opt - amplicon_tm) * weights.product_tm_lt | ||
|
||
# Put it all together | ||
return left_primer.penalty + right_primer.penalty + size_penalty + tm_penalty | ||
|
||
|
||
def build_primer_pairs( | ||
left_primers: list[Oligo], | ||
right_primers: list[Oligo], | ||
def build_primer_pairs( # noqa: C901 | ||
left_primers: Sequence[Oligo], | ||
right_primers: Sequence[Oligo], | ||
target: Span, | ||
amplicon_sizes: MinOptMax[int], | ||
amplicon_tms: MinOptMax[float], | ||
|
@@ -136,42 +139,60 @@ def build_primer_pairs( | |
region_end = max(p.span.end for p in right_primers) | ||
bases = fasta.fetch(target.refname, region_start - 1, region_end) | ||
|
||
# Each tuple is left_idx, right_idx, penalty, tm | ||
pairings: list[Tuple[int, int, float, float]] = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion Could we make this a dataclass? A four element tuple with multiple elements of the same type seems destined to be mis-indexed. |
||
|
||
for i in range(0, len(left_primers)): | ||
for j in range(0, len(right_primers)): | ||
lp = left_primers[i] | ||
rp = right_primers[j] | ||
|
||
# Ignore pairings with amplicons too large | ||
if rp.span.end - lp.span.start + 1 > amplicon_sizes.max: | ||
continue | ||
|
||
# Ignore pairings with amplicons too small | ||
if rp.span.end - lp.span.start + 1 < amplicon_sizes.min: | ||
continue | ||
|
||
amp_mapping = Span(refname=target.refname, start=lp.span.start, end=rp.span.end) | ||
amp_bases = bases[amp_mapping.start - region_start : amp_mapping.end - region_start + 1] | ||
amp_tm = calculate_long_seq_tm(amp_bases) | ||
|
||
if amp_tm < amplicon_tms.min or amp_tm > amplicon_tms.max: | ||
continue | ||
|
||
penalty = score( | ||
left_primer=lp, | ||
right_primer=rp, | ||
amplicon=amp_mapping, | ||
amplicon_tm=amp_tm, | ||
amplicon_sizes=amplicon_sizes, | ||
amplicon_tms=amplicon_tms, | ||
weights=weights, | ||
) | ||
|
||
pairings.append((i, j, penalty, amp_tm)) | ||
|
||
pairings.sort(key=lambda tup: tup[2]) | ||
|
||
with NtThermoAlign() as ntthal: | ||
# generate all the primer pairs that don't violate hard size and Tm constraints | ||
for lp in left_primers: | ||
for rp in right_primers: | ||
if rp.span.end - lp.span.start + 1 > amplicon_sizes.max: | ||
for i, j, penalty, tm in pairings: | ||
lp = left_primers[i] | ||
rp = right_primers[j] | ||
|
||
if max_heterodimer_tm is not None: | ||
if ntthal.duplex_tm(lp.bases, rp.bases) > max_heterodimer_tm: | ||
continue | ||
|
||
amp_mapping = Span(refname=target.refname, start=lp.span.start, end=rp.span.end) | ||
amp_bases = bases[ | ||
amp_mapping.start - region_start : amp_mapping.end - region_start + 1 | ||
] | ||
amp_tm = calculate_long_seq_tm(amp_bases) | ||
amp_bases = bases[lp.span.start - region_start : rp.span.end - region_start + 1] | ||
|
||
if amp_tm < amplicon_tms.min or amp_tm > amplicon_tms.max: | ||
continue | ||
pp = PrimerPair( | ||
left_primer=lp, | ||
right_primer=rp, | ||
amplicon_sequence=amp_bases, | ||
amplicon_tm=tm, | ||
penalty=penalty, | ||
) | ||
|
||
if max_heterodimer_tm is not None: | ||
if ntthal.duplex_tm(lp.bases, rp.bases) > max_heterodimer_tm: | ||
continue | ||
|
||
penalty = score( | ||
left_primer=lp, | ||
right_primer=rp, | ||
amplicon=amp_mapping, | ||
amplicon_tm=amp_tm, | ||
amplicon_sizes=amplicon_sizes, | ||
amplicon_tms=amplicon_tms, | ||
weights=weights, | ||
) | ||
|
||
pp = PrimerPair( | ||
left_primer=lp, | ||
right_primer=rp, | ||
amplicon_sequence=amp_bases, | ||
amplicon_tm=amp_tm, | ||
penalty=penalty, | ||
) | ||
|
||
yield pp | ||
yield pp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion I think this is a good sign that now would be an appropriate time to consider implementing #85