-
Notifications
You must be signed in to change notification settings - Fork 1
Add interface contact probability scores #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8b1f54
Add interface contact probability scores
DimaMolod 85b129d
Address contact probability review feedback
DimaMolod c6b50a0
Trim contact probability score outputs
DimaMolod 6fce50a
Use Humphreys AF2 contact cutoff
DimaMolod 67e4841
Move benchmark helpers to manual tests
DimaMolod f50189a
Guard AF2 contact probability parsing
DimaMolod 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
|
|
||
| AF2_DISTOGRAM_CONTACT_CUTOFF = 12.0 | ||
|
|
||
|
|
||
| def contact_probs_from_distogram( | ||
| logits: np.ndarray, | ||
| bin_edges: np.ndarray, | ||
| contact_cutoff: float = AF2_DISTOGRAM_CONTACT_CUTOFF, | ||
| ) -> np.ndarray: | ||
| """ | ||
| Convert AF2 distogram logits to P(distance < contact_cutoff). | ||
|
|
||
| AF2 distogram ``bin_edges`` are finite upper bin boundaries. For scoring, | ||
| use the lower-bound convention used by AF2 contact-probability analyses: | ||
| append a 0 A lower bound and include bins whose lower bound is below the | ||
| cutoff. This follows the Humphreys et al. AF2 contact-probability | ||
| convention and includes the bin that straddles 12 A in the standard AF2 | ||
| distogram, instead of dropping it because its upper edge is slightly above | ||
| 12 A. | ||
| """ | ||
| logits_arr = np.asarray(logits) | ||
| if logits_arr.ndim != 3: | ||
| raise ValueError(f"distogram logits must be 3D, got shape {logits_arr.shape}") | ||
|
|
||
| edges = np.asarray(bin_edges, dtype=float).ravel() | ||
| if edges.size != logits_arr.shape[-1] - 1: | ||
| raise ValueError( | ||
| f"distogram bin_edges length {edges.size} does not match logits bins " | ||
| f"{logits_arr.shape[-1]}" | ||
| ) | ||
|
|
||
| work = ( | ||
| logits_arr | ||
| if np.issubdtype(logits_arr.dtype, np.floating) | ||
| else logits_arr.astype(np.float32) | ||
| ) | ||
|
|
||
| lower_bounds = np.concatenate([[0.0], edges]) | ||
| clipped_cutoff = float(np.clip(float(contact_cutoff), 3.0, 20.0)) | ||
| n_contact_bins = int(np.count_nonzero(lower_bounds < clipped_cutoff)) | ||
| n_contact_bins = max(1, min(n_contact_bins, work.shape[-1])) | ||
|
|
||
| max_logit = np.max(work, axis=-1) | ||
| dtype = np.result_type(work.dtype, np.float32) | ||
| numerator = np.zeros(max_logit.shape, dtype=dtype) | ||
| denominator = np.zeros(max_logit.shape, dtype=dtype) | ||
| for bin_idx in range(work.shape[-1]): | ||
| delta = (work[..., bin_idx] - max_logit).astype(dtype, copy=False) | ||
| bin_mass = np.exp(delta) | ||
| denominator += bin_mass | ||
| if bin_idx < n_contact_bins: | ||
| numerator += bin_mass | ||
| return numerator / denominator | ||
|
|
||
|
|
||
| def summarize_contact_prob_block( | ||
| matrix: np.ndarray | None, | ||
| idx1: np.ndarray, | ||
| idx2: np.ndarray, | ||
| top_n: int = 10, | ||
| ) -> tuple[float, float]: | ||
| """Summarize one inter-chain block as max and top-N mean.""" | ||
| if matrix is None or idx1.size == 0 or idx2.size == 0: | ||
| nan = float("nan") | ||
| return nan, nan | ||
|
|
||
| m = np.asarray(matrix, dtype=float) | ||
| if m.ndim != 2: | ||
| nan = float("nan") | ||
| return nan, nan | ||
| if idx1.max(initial=-1) >= m.shape[0] or idx2.max(initial=-1) >= m.shape[1]: | ||
| nan = float("nan") | ||
| return nan, nan | ||
|
|
||
| block = m[np.ix_(idx1, idx2)].ravel() | ||
| if ( | ||
| m.shape[0] == m.shape[1] | ||
| and idx2.max(initial=-1) < m.shape[0] | ||
| and idx1.max(initial=-1) < m.shape[1] | ||
| ): | ||
| reverse = m[np.ix_(idx2, idx1)].T.ravel() | ||
| both = np.isfinite(block) & np.isfinite(reverse) | ||
| only_reverse = ~np.isfinite(block) & np.isfinite(reverse) | ||
| if np.any(both): | ||
| block[both] = 0.5 * (block[both] + reverse[both]) | ||
| if np.any(only_reverse): | ||
| block[only_reverse] = reverse[only_reverse] | ||
|
|
||
| finite = block[np.isfinite(block)] | ||
| if finite.size == 0: | ||
| nan = float("nan") | ||
| return nan, nan | ||
|
|
||
| n = max(1, min(int(top_n), finite.size)) | ||
| top = np.partition(finite, finite.size - n)[-n:] | ||
| return float(np.max(finite)), float(np.mean(top)) | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.