pulsatrix 1.1.0
Loading...
Searching...
No Matches
protein_contacts.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstdint>
9#include <functional>
10#include <string>
11#include <string_view>
12#include <vector>
13
17
18namespace pulsatrix {
19
22 int64_t layer = 0;
23 int64_t head = 0;
24};
25
32 int64_t layers = 0;
33 int64_t heads = 0;
34 std::vector<float> weight;
35 float bias = 0.0f;
36};
37
44[[nodiscard]] EsmContactHead LoadEsmContactHead(const std::string& directory, const EncoderLMConfig& config);
45
53[[nodiscard]] ContactMap ContactFeatures(const float* attention, int64_t tokens);
54
58 int64_t max_pass_bytes = int64_t{4} << 30;
59};
60
68public:
70 ContactPredictor(EncoderLM& model, const TextTokenizer& tokenizer, DeviceBackend* backend, ContactOptions options = {});
71
78 [[nodiscard]] ContactMap predict(std::string_view sequence, const EsmContactHead& head);
79
85 [[nodiscard]] ContactMap average_heads(std::string_view sequence, const std::vector<AttentionHead>& heads);
86
88 void for_each_head(std::string_view sequence, const std::function<void(AttentionHead, const ContactMap&)>& visit);
89
90private:
93 void Run(std::string_view sequence, const std::function<void(int64_t, const std::vector<float>&, int64_t)>& on_layer);
94
95 EncoderLM& model_;
96 const TextTokenizer& tokenizer_;
97 DeviceBackend* backend_;
98 ContactOptions options_;
99};
100
104 int64_t min = 6;
105 int64_t max = 0;
106};
108inline constexpr SeparationRange kShortRange{6, 12};
109inline constexpr SeparationRange kMediumRange{12, 24};
110inline constexpr SeparationRange kLongRange{24, 0};
111
122[[nodiscard]] double ContactPrecision(const ContactMap& predicted, const ContactMap& truth, SeparationRange range, int64_t top);
123
126 double at_l = 0;
127 double at_l2 = 0;
128 double at_l5 = 0;
129};
130[[nodiscard]] ContactPrecisions PrecisionAtL(const ContactMap& predicted, const ContactMap& truth, SeparationRange range);
131
138[[nodiscard]] ContactEvaluation EvaluateContacts(const ContactMap& predicted, const ContactMap& truth);
139
145
150
158[[nodiscard]] std::vector<HeadPrecision> RankContactHeads(ContactPredictor& predictor, const std::vector<LabeledProtein>& proteins,
160
161} // namespace pulsatrix
Contacts from a masked LM's attention, the way ESM predicts them: one forward pass over <cls> + the s...
Definition protein_contacts.hpp:67
ContactMap predict(std::string_view sequence, const EsmContactHead &head)
ESM's contact head: contact probabilities, L x L, symmetric. Matches transformers' EsmForMaskedLM....
void for_each_head(std::string_view sequence, const std::function< void(AttentionHead, const ContactMap &)> &visit)
Calls visit with every head's ContactFeatures, layer by layer, from one pass.
ContactMap average_heads(std::string_view sequence, const std::vector< AttentionHead > &heads)
The mean of heads' ContactFeatures: an unsupervised predictor with no regression. Scores rank pairs; ...
ContactPredictor(EncoderLM &model, const TextTokenizer &tokenizer, DeviceBackend *backend, ContactOptions options={})
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
A masked language model: token embeddings (with ESM's token-dropout scaling, when the config has it),...
Definition encoder_lm.hpp:109
A tokenizer: added tokens are split out of the raw text first (longest match, earliest first); each r...
Definition text_tokenizer.hpp:166
Masked-language-model encoders: ESM-2 protein language models (PLM-2), Hugging Face EsmForMaskedLM ch...
Definition acquisition_functions.hpp:16
ContactEvaluation EvaluateContacts(const ContactMap &predicted, const ContactMap &truth)
ContactPrecisions PrecisionAtL(const ContactMap &predicted, const ContactMap &truth, SeparationRange range)
constexpr SeparationRange kLongRange
Definition protein_contacts.hpp:110
constexpr SeparationRange kMediumRange
Definition protein_contacts.hpp:109
EsmContactHead LoadEsmContactHead(const std::string &directory, const EncoderLMConfig &config)
Reads the contact head from an ESM-2 checkpoint directory: esm.contact_head.regression....
ContactMap ContactFeatures(const float *attention, int64_t tokens)
One head's contact features from its attention map over <cls>, L residues and <eos> (tokens x tokens,...
std::vector< HeadPrecision > RankContactHeads(ContactPredictor &predictor, const std::vector< LabeledProtein > &proteins, SeparationRange range=kLongRange)
Every head, best first, by its features' mean precision at L in range over proteins....
double ContactPrecision(const ContactMap &predicted, const ContactMap &truth, SeparationRange range, int64_t top)
The fraction of the top highest-scoring pairs in range that are true contacts.
constexpr SeparationRange kShortRange
CASP's and ESM's ranges: short 6–11, medium 12–23, long 24 and up.
Definition protein_contacts.hpp:108
Protein structures (PLM-4): PDB and mmCIF coordinates, residue distances and true contacts,...
One attention head: layer from 0, head within it from 0.
Definition protein_contacts.hpp:21
int64_t layer
Definition protein_contacts.hpp:22
int64_t head
Definition protein_contacts.hpp:23
PrecisionAtL in each of the short, medium and long ranges.
Definition protein_contacts.hpp:133
ContactPrecisions long_range
Definition protein_contacts.hpp:136
ContactPrecisions short_range
Definition protein_contacts.hpp:134
ContactPrecisions medium_range
Definition protein_contacts.hpp:135
A square matrix over a sequence's residue pairs, row-major: predicted contact probabilities,...
Definition protein_structure.hpp:85
Definition protein_contacts.hpp:55
int64_t max_pass_bytes
The memory the forward pass may use, by ScoringPassBytes. A longer sequence is refused rather than ri...
Definition protein_contacts.hpp:58
Precision of the top L, L/2 and L/5 pairs, L the sequence length (rounded down).
Definition protein_contacts.hpp:125
double at_l2
Definition protein_contacts.hpp:127
double at_l5
Definition protein_contacts.hpp:128
double at_l
Definition protein_contacts.hpp:126
The settings of a masked LM, from its Hugging Face config.json.
Definition encoder_lm.hpp:35
ESM's contact head (EsmContactPredictionHead): a logistic regression over every head's corrected atte...
Definition protein_contacts.hpp:31
std::vector< float > weight
layers * heads values
Definition protein_contacts.hpp:34
float bias
Definition protein_contacts.hpp:35
int64_t heads
Definition protein_contacts.hpp:33
int64_t layers
Definition protein_contacts.hpp:32
Definition protein_contacts.hpp:146
double precision
the mean over the proteins
Definition protein_contacts.hpp:148
AttentionHead head
Definition protein_contacts.hpp:147
A protein with known contacts, to choose heads with.
Definition protein_contacts.hpp:141
ContactMap contacts
TrueContacts, L x L for the sequence's L residues.
Definition protein_contacts.hpp:143
std::string sequence
Definition protein_contacts.hpp:142
Pairs (i, j) counted by separation j - i: at least min, and under max unless max is 0.
Definition protein_contacts.hpp:103
int64_t min
Definition protein_contacts.hpp:104
int64_t max
Definition protein_contacts.hpp:105
The tokenizer interface (TOK-1): a pipeline of normalizer, pre-tokenizer, model, post-processor and d...