minerva-dna 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. minerva_dna-0.1.0/LICENSE +202 -0
  2. minerva_dna-0.1.0/PKG-INFO +355 -0
  3. minerva_dna-0.1.0/README.md +319 -0
  4. minerva_dna-0.1.0/minerva/__init__.py +21 -0
  5. minerva_dna-0.1.0/minerva/backbones.py +147 -0
  6. minerva_dna-0.1.0/minerva/constants.py +139 -0
  7. minerva_dna-0.1.0/minerva/data.py +554 -0
  8. minerva_dna-0.1.0/minerva/example_data/TwoAYGGAY_Pseudomonas_fluorescens_SBW25.gb +9335 -0
  9. minerva_dna-0.1.0/minerva/example_data/UG27_systems.gb +1834 -0
  10. minerva_dna-0.1.0/minerva/finetuning.py +497 -0
  11. minerva_dna-0.1.0/minerva/gene_calling.py +173 -0
  12. minerva_dna-0.1.0/minerva/interaction_heads.py +408 -0
  13. minerva_dna-0.1.0/minerva/jacobian.py +849 -0
  14. minerva_dna-0.1.0/minerva/losses.py +51 -0
  15. minerva_dna-0.1.0/minerva/masking.py +51 -0
  16. minerva_dna-0.1.0/minerva/modeling_minerva.py +1648 -0
  17. minerva_dna-0.1.0/minerva/modeling_rinalmo.py +286 -0
  18. minerva_dna-0.1.0/minerva/rna_structure.py +683 -0
  19. minerva_dna-0.1.0/minerva/sequence_utils.py +815 -0
  20. minerva_dna-0.1.0/minerva/tokenization_rinalmo.py +49 -0
  21. minerva_dna-0.1.0/minerva/vendor_rinalmo/LICENSE +201 -0
  22. minerva_dna-0.1.0/minerva/vendor_rinalmo/__init__.py +13 -0
  23. minerva_dna-0.1.0/minerva/vendor_rinalmo/attention.py +237 -0
  24. minerva_dna-0.1.0/minerva/vendor_rinalmo/model.py +39 -0
  25. minerva_dna-0.1.0/minerva/vendor_rinalmo/modules.py +180 -0
  26. minerva_dna-0.1.0/minerva/vendor_rinalmo/rope.py +45 -0
  27. minerva_dna-0.1.0/minerva/visualization.py +1204 -0
  28. minerva_dna-0.1.0/minerva_dna.egg-info/PKG-INFO +355 -0
  29. minerva_dna-0.1.0/minerva_dna.egg-info/SOURCES.txt +40 -0
  30. minerva_dna-0.1.0/minerva_dna.egg-info/dependency_links.txt +1 -0
  31. minerva_dna-0.1.0/minerva_dna.egg-info/requires.txt +25 -0
  32. minerva_dna-0.1.0/minerva_dna.egg-info/top_level.txt +1 -0
  33. minerva_dna-0.1.0/pyproject.toml +55 -0
  34. minerva_dna-0.1.0/setup.cfg +4 -0
  35. minerva_dna-0.1.0/tests/test_backbones.py +167 -0
  36. minerva_dna-0.1.0/tests/test_examples_and_viewer.py +62 -0
  37. minerva_dna-0.1.0/tests/test_model_correctness.py +192 -0
  38. minerva_dna-0.1.0/tests/test_modeling_rinalmo.py +155 -0
  39. minerva_dna-0.1.0/tests/test_notebooks.py +95 -0
  40. minerva_dna-0.1.0/tests/test_rna_structure.py +328 -0
  41. minerva_dna-0.1.0/tests/test_sequence_dataset.py +101 -0
  42. minerva_dna-0.1.0/tests/test_sequence_utils.py +1062 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Garyk Brixi
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,355 @@
1
+ Metadata-Version: 2.4
2
+ Name: minerva-dna
3
+ Version: 0.1.0
4
+ Summary: Minerva: genomic language models for coevolutionary mining
5
+ Author: Gary Brixi
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/garykbrixi/minerva
8
+ Project-URL: Source, https://github.com/garykbrixi/minerva
9
+ Project-URL: Model, https://huggingface.co/gbrixi/minerva-mlm
10
+ Requires-Python: >=3.11
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: torch<3,>=2.1.0
14
+ Requires-Dist: transformers<6,>=4.41.0
15
+ Requires-Dist: numpy<3,>=1.23
16
+ Requires-Dist: biopython<2,>=1.80
17
+ Requires-Dist: pyrodigal<4,>=3.0
18
+ Requires-Dist: huggingface_hub<2,>=0.23.0
19
+ Requires-Dist: safetensors<1,>=0.4.0
20
+ Requires-Dist: matplotlib<4,>=3.7
21
+ Requires-Dist: ViennaRNA<3,>=2.6
22
+ Requires-Dist: plotly<7,>=5.0
23
+ Requires-Dist: datasets<5,>=2.16
24
+ Requires-Dist: accelerate<2,>=0.26
25
+ Requires-Dist: peft<1,>=0.10.0
26
+ Provides-Extra: flash
27
+ Requires-Dist: flash-attn>=2.5.8; extra == "flash"
28
+ Provides-Extra: viz
29
+ Requires-Dist: bokeh<4,>=3; extra == "viz"
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest<10,>=7; extra == "dev"
32
+ Requires-Dist: nbclient<1,>=0.8; extra == "dev"
33
+ Requires-Dist: ipykernel<8,>=6; extra == "dev"
34
+ Requires-Dist: ipywidgets<9,>=8; extra == "dev"
35
+ Dynamic: license-file
36
+
37
+
38
+ <h1><img src="https://raw.githubusercontent.com/garykbrixi/minerva/main/assets/minerva_owl.png" alt="" height="46" valign="middle"> Minerva</h1>
39
+
40
+ [![tests](https://github.com/garykbrixi/minerva/actions/workflows/tests.yml/badge.svg)](https://github.com/garykbrixi/minerva/actions/workflows/tests.yml)
41
+ [![python](https://img.shields.io/badge/python-3.11%2B-blue)](https://github.com/garykbrixi/minerva)
42
+ [![model](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Minerva--MLM-yellow)](https://huggingface.co/gbrixi/minerva-mlm)
43
+ [![license](https://img.shields.io/badge/license-Apache--2.0-green)](https://github.com/garykbrixi/minerva/blob/main/LICENSE)
44
+
45
+ **Coevolutionary discovery using genome language models**
46
+
47
+ Minerva predicts coevolution using genome language models. Powered by Minerva-MLM, it delivers database-scale, alignment-free, interaction-specific predictions across prokaryotic genomes. Through adaptation on homologous loci, Minerva can discover additional interactions.
48
+
49
+ [Install](#install) · [Checkpoints](#pretrained-checkpoints) · [Quick start](#quick-start) · [Preparing inputs](#preparing-inputs) · [Interaction heads](#interaction-heads) · [Jacobian fingerprinting](#jacobian-fingerprinting) · [RNA structure](#rna-secondary-structure) · [Eukaryotic RNA](#eukaryotic-rna) · [Finetuning](#finetuning) · [Examples](https://github.com/garykbrixi/minerva/tree/main/examples/) · [Citation](#citation)
50
+
51
+ ## Install
52
+
53
+ To install Minerva, use:
54
+
55
+ ```bash
56
+ pip install minerva-dna
57
+ ```
58
+
59
+ Minerva uses `flash-attn` automatically when it is installed. Otherwise it falls
60
+ back to PyTorch SDPA. Please install flash attention first for faster inference.
61
+
62
+ ## Pretrained Checkpoints
63
+
64
+ Minerva-MLM is a 650M parameter transformer trained for over 1.3 trillion tokens (~3.4 Terabases). Minerva-MLM is initialized from [gLM2 650M](https://github.com/TattaBio/gLM2) and adopts the mixed-modality tokenization, and was trained at 4096 and 8192 context lengths.
65
+
66
+ Checkpoints are hosted on Hugging Face:
67
+
68
+ | Model | Context | Hugging Face repo |
69
+ | ----------- | ------- | --------------------------------------------------- |
70
+ | Minerva-MLM | 4096 | [`gbrixi/minerva-mlm`](https://huggingface.co/gbrixi/minerva-mlm) |
71
+ | Minerva-MLM-8k | 8192 | [`gbrixi/minerva-mlm-8k`](https://huggingface.co/gbrixi/minerva-mlm-8k) |
72
+
73
+ Minerva-MLM checkpoints include three interaction heads and Jacobian fingerprint types:
74
+
75
+ - **base_pairing** — RNA base-pairing contacts
76
+ - **protein** — protein contact prediction
77
+ - **repeat** — repeat element detection
78
+
79
+ ## Quick start
80
+
81
+ ```python
82
+ from transformers import AutoTokenizer
83
+ from minerva import MinervaForMaskedLM
84
+ import torch
85
+
86
+ model = MinervaForMaskedLM.from_pretrained(
87
+ "gbrixi/minerva-mlm", torch_dtype=torch.bfloat16,
88
+ ).cuda().eval()
89
+ tokenizer = AutoTokenizer.from_pretrained("gbrixi/minerva-mlm")
90
+
91
+ tokens = tokenizer(
92
+ "<+>cgcggggtggagcagcctggtagctcgtcgggctcataacccgaagatcgtcggttcaaatccggcccccgcaacca",
93
+ return_tensors="pt",
94
+ ).to(model.device)
95
+
96
+ with torch.no_grad():
97
+ outputs = model(**tokens, output_interactions=True)
98
+
99
+ base_pairing = outputs.interactions["base_pairing"] # [batch, L, L]
100
+ protein = outputs.interactions["protein"] # [batch, L, L]
101
+ repeat = outputs.interactions["repeat"] # [batch, L, L]
102
+ ```
103
+
104
+ > Importing the class directly needs no `trust_remote_code`. Without the package
105
+ > installed, use `AutoModelForMaskedLM.from_pretrained(repo, trust_remote_code=True)`.
106
+
107
+ ## Preparing inputs
108
+
109
+ Minerva reads a **mixed protein + DNA** sequence: coding regions are upper-case
110
+ amino acids, intergenic regions are lower-case nucleotides, and `<+>` / `<->`
111
+ markers denote strand.
112
+
113
+ ```
114
+ <+>MALTKVEKRNRIKRRVRGK<+>aatttaaggaa<->MLGIDNIERVKPGGLELVDRLV
115
+ └── CDS (protein) ──┘└ intergenic ┘└──── CDS on - strand ────┘
116
+ ```
117
+
118
+ There are **three ways** to produce this format depending on what you start
119
+ with:
120
+
121
+ | You have | Use | What happens |
122
+ | --- | --- | --- |
123
+ | **Annotated GenBank** (CDS features) | `minerva.data.extract_and_tokenize_gb(path)` | CDS translated, intergenic kept as DNA, strand markers inserted. One sequence per LOCUS. |
124
+ | **Unannotated sequence** (FASTA / raw DNA) | `minerva.gene_calling.build_minerva_input(seq)` | Genes called with **Pyrodigal**, then packaged. |
125
+ | **Raw genome + external CDS calls** | `minerva.sequence_utils.build_prodigal_mixed_sequence(seq, cds)` | Your own `[{start, end, strand}]` calls packaged, with genome↔token maps. |
126
+
127
+ ### From unannotated sequence (gene calling)
128
+
129
+ If you only have a FASTA file or a raw nucleotide string, we use [Pyrodigal](https://github.com/althonos/pyrodigal) to automatically call genes:
130
+
131
+ ```python
132
+ from minerva.gene_calling import build_minerva_input, fasta_to_minerva_inputs
133
+
134
+ # From a single nucleotide string
135
+ out = build_minerva_input(sequence) # dict: token_string + coord maps
136
+ token_string = out["token_string"]
137
+
138
+ # From a FASTA file (one result per record)
139
+ inputs = fasta_to_minerva_inputs("contigs.fasta")
140
+ ```
141
+
142
+ Pyrodigal needs ≥ 20 kb to estimate gene-scoring statistics from a sequence;
143
+ shorter contigs use its pre-trained profiles, which `meta=True` forces
144
+ for metagenomic assemblies. A CDS token is one amino acid and an intergenic
145
+ token one base, so `token_to_genome` / `genome_to_token` map token index to
146
+ genome position.
147
+
148
+ ### Context length & capping
149
+
150
+ Minerva's context is 4096 (`gbrixi/minerva-mlm`) or 8192 tokens
151
+ (`gbrixi/minerva-mlm-8k`). One token is one amino acid, one nucleotide, or one
152
+ strand marker, so a typical (~88 % coding) bacterial genome packs to ~10 kb per
153
+ 4096 tokens (~20 kb for the 8k model).
154
+
155
+ Pass `max_tokens` to the builders to cap a sequence. It truncates at a gene
156
+ boundary, keeps the 5′ end, and keeps the coordinate maps consistent.
157
+
158
+ ```python
159
+ out = build_minerva_input(sequence, max_tokens=4096) # <= 4096 tokens
160
+ ```
161
+
162
+ To cover a whole genome, tile it instead with
163
+ `minerva.sequence_utils.chunk_sequence_with_stride`.
164
+
165
+ ### Translation tables
166
+
167
+ CDS translate with NCBI table 11 by default, but a GenBank feature's own
168
+ `/transl_table` takes precedence. Override with `translation_table=` on the
169
+ builders or `--translation_table` on `scripts/finetune.py`.
170
+
171
+ See [`examples/`](https://github.com/garykbrixi/minerva/tree/main/examples/) for runnable, end-to-end walkthroughs.
172
+
173
+ ## Using the model
174
+
175
+ These continue from the [quick start](#quick-start), with `model`, `tokenizer`, `tokens` and
176
+ `outputs` already defined.
177
+
178
+ ### Interaction heads
179
+
180
+ To plot the interaction-head outputs:
181
+
182
+ ```python
183
+ from minerva.visualization import plot_interactions
184
+
185
+ token_list = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0].tolist())
186
+ plot_interactions(outputs.interactions, tokens=token_list)
187
+ ```
188
+
189
+ Set `interaction_layers=6` to use the six-layer interaction heads:
190
+
191
+ ```python
192
+ outputs = model(**tokens, output_interactions=True, interaction_layers=6)
193
+ ```
194
+
195
+ Raw attention tensors follow the Hugging Face convention:
196
+
197
+ ```python
198
+ outputs = model(
199
+ **tokens,
200
+ output_interactions=True,
201
+ output_attentions=True,
202
+ attention_layers=[31, 32],
203
+ )
204
+ ```
205
+
206
+ ### Jacobian fingerprinting
207
+
208
+ Use `get_fingerprints` when you want named interaction-pattern channels from a
209
+ sequence. It computes the required full categorical Jacobian internally and returns
210
+ a `FingerprintResult`; the large raw Jacobian is not kept unless requested.
211
+
212
+ ```python
213
+ sequence = "<+>cgcggggtggagcagcctggtagctcgtcgggctcataacccgaagatcgtcggttcaaatccggcccccgcaacca"
214
+ window = (0, min(len(sequence), 96))
215
+
216
+ fp = model.get_fingerprints(
217
+ sequence,
218
+ tokenizer,
219
+ position_range=window,
220
+ max_batch_size=64,
221
+ )
222
+
223
+ fp.channel_names # ["basepairing", "repeat", "protein", "other"]
224
+ basepairing = fp["basepairing"] # [L, L]
225
+ protein = fp["protein"] # [L, L]
226
+ repeat = fp["repeat"] # [L, L]
227
+ ```
228
+
229
+ To plot the result:
230
+
231
+ ```python
232
+ from minerva.visualization import plot_fingerprints
233
+
234
+ plot_fingerprints(fp, title="Minerva multimodal fingerprint")
235
+ ```
236
+
237
+ ### RNA secondary structure
238
+
239
+ The `base_pairing` head returns a dense contact map which can be converted to an RNA structure using `minerva.rna_structure`:
240
+
241
+ ```python
242
+ from minerva.rna_structure import call_structure, call_structures
243
+
244
+ token_list = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0].tolist())
245
+ s = call_structure(outputs.interactions["base_pairing"], tokens=token_list)
246
+
247
+ s.dot_bracket # '(((((((..((((........)))).(((((.......)))))...'
248
+ s.to_vienna("t.fa") # read by RNAfold, forna, VARNA, R2R
249
+ s.to_ct("t.ct") # connect table, keeps pseudoknots
250
+ s.plot() # matplotlib Figure
251
+ ```
252
+
253
+ Each intergenic region of a mixed locus is a separate molecule, so
254
+ `call_structures` returns one structure per region:
255
+
256
+ ```python
257
+ structures = call_structures(outputs.interactions["base_pairing"], token_list)
258
+ ```
259
+
260
+ An interactive viewer is in
261
+ [`examples/notebooks/rna_structure.ipynb`](https://github.com/garykbrixi/minerva/blob/main/examples/notebooks/rna_structure.ipynb).
262
+
263
+ ## Eukaryotic RNA
264
+
265
+ For researchers studying eukaryotic RNAs, Minerva provides a RiNALMo-based checkpoint with base-pairing and repeat interaction heads. See [eukaryotic RNA support](https://github.com/garykbrixi/minerva/tree/main/examples/eukaryotic_rna/) for usage and finetuning.
266
+
267
+ ## Finetuning
268
+
269
+ `scripts/finetune.py` wraps HF `Trainer` + `accelerate` with Minerva's MLM loss.
270
+ It supports full finetuning and LoRA, and ingests GenBank files directly.
271
+
272
+ ```bash
273
+ # LoRA finetune
274
+ accelerate launch --num_processes=8 scripts/finetune.py \
275
+ --output_dir ./output \
276
+ --genbank_file genome.gb \
277
+ --tokenizer_name gbrixi/minerva-mlm \
278
+ --model_name_or_path gbrixi/minerva-mlm \
279
+ --use_lora --lora_r 1 --lora_alpha 2 \
280
+ --learning_rate 1e-4 --bf16
281
+
282
+ # Full finetune on a GenBank file across 8 GPUs
283
+ accelerate launch --num_processes=8 scripts/finetune.py \
284
+ --output_dir ./output \
285
+ --genbank_file genome.gb \
286
+ --tokenizer_name gbrixi/minerva-mlm \
287
+ --model_name_or_path gbrixi/minerva-mlm \
288
+ --per_device_train_batch_size 4 \
289
+ --bf16
290
+ ```
291
+
292
+ Minerva-MLM LoRA checkpoints loaded with PEFT:
293
+
294
+ ```python
295
+ from peft import PeftModel
296
+ from minerva import MinervaForMaskedLM
297
+
298
+ base = MinervaForMaskedLM.from_pretrained("gbrixi/minerva-mlm")
299
+ model = PeftModel.from_pretrained(base, "path/to/lora_ckpt")
300
+ ```
301
+
302
+ A LOCUS longer than `--max_seq_length` is split into non-overlapping blocks,
303
+ each its own training example, see
304
+ `minerva.finetuning.load_genbank_dataset`.
305
+
306
+ ## Repo layout
307
+
308
+ ```
309
+ minerva/
310
+ modeling_minerva.py # MinervaConfig / MinervaForMaskedLM (custom transformer + heads)
311
+ modeling_rinalmo.py # RiNALMoMinervaForMaskedLM (RNA backbone + heads)
312
+ tokenization_rinalmo.py # RiNALMo nucleotide tokenizer
313
+ backbones.py # Backbone-specific training configuration
314
+ interaction_heads.py # Shared interaction heads
315
+ data.py # GenBank parsing + tokenization
316
+ gene_calling.py # Pyrodigal gene calling: FASTA/raw DNA -> mixed tokens
317
+ sequence_utils.py # reverse-complement + external-CDS -> mixed tokens
318
+ masking.py # DataCollatorForMinervaMLM
319
+ losses.py # grouped_mlm_loss
320
+ example_data/ # sample GenBank loci (minerva.data.example_path)
321
+ scripts/
322
+ finetune.py # HF Trainer / accelerate wrapper
323
+ examples/ # end-to-end tutorials & notebooks
324
+ call_genes_from_fasta.py # FASTA/raw DNA -> Minerva input walkthrough
325
+ notebooks/ # interactive Colab-ready notebooks
326
+ tests/ # package unit + smoke tests
327
+ ```
328
+
329
+ ## Citation
330
+
331
+ If you use Minerva in your work, please cite the paper.
332
+
333
+ If you use the Jacobian fingerprints, please also cite the categorical Jacobian:
334
+
335
+ > Zhang, Z., Wayment-Steele, H.K., Brixi, G., Wang, H., Kern, D. & Ovchinnikov, S. Protein language
336
+ > models learn evolutionary statistics of interacting sequence motifs. *Proc. Natl. Acad. Sci. U.S.A.*
337
+ > **121** (45), e2406285121 (2024). https://doi.org/10.1073/pnas.2406285121
338
+
339
+ ```bibtex
340
+ @article{zhang2024categoricaljacobian,
341
+ title = {Protein language models learn evolutionary statistics of interacting sequence motifs},
342
+ author = {Zhang, Z. and Wayment-Steele, H. K. and Brixi, G. and Wang, H. and Kern, D. and Ovchinnikov, S.},
343
+ journal = {Proceedings of the National Academy of Sciences},
344
+ volume = {121},
345
+ number = {45},
346
+ pages = {e2406285121},
347
+ year = {2024},
348
+ doi = {10.1073/pnas.2406285121}
349
+ }
350
+ ```
351
+
352
+ ## License
353
+
354
+ Apache 2.0 — see [LICENSE](https://github.com/garykbrixi/minerva/blob/main/LICENSE). Minerva-MLM is initialized from
355
+ [gLM2 650M](https://github.com/TattaBio/gLM2) (Tatta Bio, Apache 2.0).