-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
65 lines (50 loc) · 1.66 KB
/
Copy pathexample.py
File metadata and controls
65 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Minimal example: embed an RNA sequence with RCLM.
Usage:
python example.py [cpu|cuda]
"""
import os
import sys
import numpy as np
import torch
from rclm import Model
device = sys.argv[1] if len(sys.argv) > 1 else "cpu"
expdir = os.path.dirname(os.path.abspath(__file__))
# Same alphabet convention used in DRfold2/cfg_95/EvoMSA2XYZ.py:rnalm_alphas
NT2IDX = {"A": 0, "G": 1, "C": 2, "U": 3,
"a": 0, "g": 1, "c": 2, "u": 3,
"T": 3, "t": 3, "-": 4}
lmcfg = {
"s_in_dim": 5,
"z_in_dim": 2,
"s_dim": 512,
"z_dim": 128,
"N_elayers": 18,
}
def load_model(device="cpu"):
model = Model.RNA2nd(lmcfg)
weight_path = os.path.join(expdir, "weight", "epoch_67000")
model.load_state_dict(torch.load(weight_path, map_location="cpu"), strict=False)
model.to(device)
model.eval()
return model
def embed(model, seq, device="cpu"):
L = len(seq)
aa = np.eye(5)[[NT2IDX.get(c, NT2IDX["-"]) for c in seq]]
# seq_idx is 1-based, matching DRfold2/cfg_95/test_modeldir.py:data_collect
seq_idx = np.arange(L) + 1
in_dict = {
"aa": torch.FloatTensor(aa).to(device),
"idx": torch.LongTensor(seq_idx).to(device),
"mask": torch.zeros(L).to(device),
}
with torch.no_grad():
s, z = model.embedding(in_dict)
return s, z
if __name__ == "__main__":
seq = "GGGCUAUUAGCUCAGUUGGUAGAGCCCUGGAUUGUGAUUCCAGUUGUCGUGGGUUCGAAUCCCAUUAGCCCCA"
rnalm = load_model(device)
s, z = embed(rnalm, seq, device)
print(f"sequence length: {len(seq)}")
print(f"s (per-residue) shape: {tuple(s.shape)}") # L x 512
print(f"z (pairwise) shape: {tuple(z.shape)}") # L x L x 128