File size: 2,953 Bytes
8760a65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
"""
BBB feature encoder — shared by training and the agent.
Produces the exact 682-dim feature vector the saved model expects:
  240 one-hot (12 x 20) + 12 hydro + 12 charge + 18 global + 400 bigram
"""
import numpy as np

AA = list('ACDEFGHIKLMNPQRSTVWY')
AA_IDX = {a: i for i, a in enumerate(AA)}

HYDRO = {'A':1.8,'R':-4.5,'N':-3.5,'D':-3.5,'C':2.5,'Q':-3.5,'E':-3.5,'G':-0.4,
         'H':-3.2,'I':4.5,'L':3.8,'K':-3.9,'M':1.9,'F':2.8,'P':-1.6,'S':-0.8,
         'T':-0.7,'W':-0.9,'Y':-1.3,'V':4.2}
CHARGE = {'R':1,'K':1,'H':0.1,'D':-1,'E':-1,'A':0,'C':0,'F':0,'G':0,'I':0,'L':0,
          'M':0,'N':0,'P':0,'Q':0,'S':0,'T':0,'V':0,'W':0,'Y':0}
MW = {'A':89,'R':174,'N':132,'D':133,'C':121,'Q':146,'E':147,'G':75,'H':155,'I':131,
      'L':131,'K':146,'M':149,'F':165,'P':115,'S':105,'T':119,'W':204,'Y':181,'V':117}

BIGRAMS = [a + b for a in AA for b in AA]
BG_IDX = {bg: i for i, bg in enumerate(BIGRAMS)}


def encode(seq: str) -> np.ndarray:
    """Encode a peptide sequence into the 682-dim feature vector."""
    seq = seq.upper().strip()
    L = len(seq)
    # One-hot (assumes fixed length; pads/truncates implicitly via position index)
    oh = np.zeros(L * 20)
    for p, a in enumerate(seq):
        if a in AA_IDX:
            oh[p * 20 + AA_IDX[a]] = 1
    h = np.array([HYDRO.get(a, 0) for a in seq])
    c = np.array([CHARGE.get(a, 0) for a in seq])
    ang = np.arange(L) * 100 * np.pi / 180
    hm = np.sqrt(np.sum(h * np.cos(ang))**2 + np.sum(h * np.sin(ang))**2) / L
    freq = np.array([seq.count(a) for a in AA], dtype=float) / L
    ent = -np.sum(freq[freq > 0] * np.log2(freq[freq > 0]))
    mw = (sum(MW.get(a, 110) for a in seq) - (L - 1) * 18) / 1000
    g = np.array([
        c.sum(), h.mean(), h.std(), h.max(), h.min(),
        (c > 0).sum(), (c < 0).sum(), (h > 1.5).sum(),
        sum(1 for a in seq if a in 'FWY'),
        sum(1 for a in seq if a in 'NQSTHY'),
        seq.count('P'), seq.count('C'), mw, ent, hm,
        c[:L // 2].sum() - c[L // 2:].sum(), c.sum() / L, (h > 1.5).sum() / L
    ])
    bg = np.zeros(400)
    for i in range(L - 1):
        d = seq[i:i + 2]
        if d in BG_IDX:
            bg[BG_IDX[d]] += 1
    return np.concatenate([oh, h, c, g, bg])


def properties(seq: str) -> dict:
    """Human-readable physicochemical properties for the agent to explain."""
    seq = seq.upper().strip()
    L = len(seq)
    h = [HYDRO.get(a, 0) for a in seq]
    c = [CHARGE.get(a, 0) for a in seq]
    mw = sum(MW.get(a, 110) for a in seq) - (L - 1) * 18
    return {
        'sequence': seq,
        'length': L,
        'molecular_weight_da': round(mw, 1),
        'mean_hydrophobicity': round(float(np.mean(h)), 3),
        'net_charge': round(sum(c), 2),
        'n_hydrophobic': int(sum(1 for x in h if x > 1.5)),
        'n_aromatic': int(sum(1 for a in seq if a in 'FWY')),
        'n_positive': int(sum(1 for x in c if x > 0)),
        'n_negative': int(sum(1 for x in c if x < 0)),
    }