File size: 3,697 Bytes
6d1bbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""MLPFeatures: Hand-crafted feature MLP for PPI binary prediction.

Uses interpretable features rather than raw sequences:
  - AA composition (20-dim × 2 proteins)
  - Sequence length × 2
  - Network degree × 2
  - Length ratio
  - Subcellular location co-occurrence (one-hot)

Simple 3-layer MLP. Cheapest to train, most interpretable.
"""

from __future__ import annotations

import torch
import torch.nn as nn

# Standard 20 amino acids
_AA_LETTERS = "ACDEFGHIKLMNPQRSTVWY"
_AA_TO_IDX = {c: i for i, c in enumerate(_AA_LETTERS)}

# Known subcellular locations (top-10 + "other")
SUBCELLULAR_LOCATIONS = [
    "Nucleus", "Cytoplasm", "Membrane", "Cell membrane",
    "Mitochondrion", "Endoplasmic reticulum", "Golgi apparatus",
    "Secreted", "Cell junction", "Cytoskeleton", "other",
]
_LOC_TO_IDX = {loc: i for i, loc in enumerate(SUBCELLULAR_LOCATIONS)}
N_LOCATIONS = len(SUBCELLULAR_LOCATIONS)


def compute_aa_composition(seq: str) -> list[float]:
    """Compute 20-dim amino acid frequency vector."""
    if not seq:
        return [0.0] * 20
    counts = [0] * 20
    total = 0
    for c in seq:
        idx = _AA_TO_IDX.get(c)
        if idx is not None:
            counts[idx] += 1
            total += 1
    if total == 0:
        return [0.0] * 20
    return [c / total for c in counts]


def encode_subcellular(loc: str | None) -> list[float]:
    """Encode subcellular location as one-hot vector."""
    vec = [0.0] * N_LOCATIONS
    if loc is None:
        return vec
    idx = _LOC_TO_IDX.get(loc, _LOC_TO_IDX["other"])
    vec[idx] = 1.0
    return vec


def extract_features(
    seq1: str,
    seq2: str,
    degree1: float,
    degree2: float,
    loc1: str | None,
    loc2: str | None,
) -> list[float]:
    """Extract feature vector for a protein pair.

    Returns:
        Feature vector of length 20+20+2+2+1+11+11 = 67.
    """
    aa1 = compute_aa_composition(seq1)  # 20
    aa2 = compute_aa_composition(seq2)  # 20
    len1 = len(seq1) if seq1 else 0
    len2 = len(seq2) if seq2 else 0
    len_ratio = min(len1, len2) / max(len1, len2) if max(len1, len2) > 0 else 0.0
    loc1_vec = encode_subcellular(loc1)  # 11
    loc2_vec = encode_subcellular(loc2)  # 11

    return (
        aa1 + aa2                                      # 40
        + [len1 / 1000.0, len2 / 1000.0]             # 2 (normalized)
        + [degree1 / 1000.0, degree2 / 1000.0]       # 2 (normalized)
        + [len_ratio]                                  # 1
        + loc1_vec + loc2_vec                          # 22
    )


FEATURE_DIM = 67  # 20+20+2+2+1+11+11


class MLPFeatures(nn.Module):
    """MLP classifier using hand-crafted protein pair features.

    Args:
        input_dim: Feature vector dimensionality.
        hidden_dims: Sizes of hidden layers.
        dropout: Dropout rate.
    """

    def __init__(
        self,
        input_dim: int = FEATURE_DIM,
        hidden_dims: tuple[int, int, int] = (256, 128, 64),
        dropout: float = 0.3,
    ) -> None:
        super().__init__()

        layers: list[nn.Module] = [nn.BatchNorm1d(input_dim)]
        in_dim = input_dim
        for out_dim in hidden_dims:
            layers += [
                nn.Linear(in_dim, out_dim),
                nn.ReLU(),
                nn.BatchNorm1d(out_dim),
                nn.Dropout(dropout),
            ]
            in_dim = out_dim
        layers.append(nn.Linear(in_dim, 1))
        self.net = nn.Sequential(*layers)

    def forward(self, features: torch.Tensor) -> torch.Tensor:
        """
        Args:
            features: (B, FEATURE_DIM) float tensor.

        Returns:
            (B,) raw logits.
        """
        return self.net(features).squeeze(-1)