File size: 6,776 Bytes
2803b0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# ------------------------------------------------------------
# CancerTranscriptome-Mini-48M
# Model: Lightweight adaptation of BulkFormer
# Author: Walter Alvarado (NASA Ames Research Center)
# License: MIT
#
# References:
# (1) Boming Kang, Rui Fan, Meizheng Yi, Chunmei Cui, Qinghua Cui.
#     “A large-scale foundation model for bulk transcriptomes.”
#     bioRxiv (2025). doi:10.1101/2025.06.11.659222
#
# (2) Alvarado W. “CancerTranscriptome-Mini-48M: A compact cancer-
#     focused BulkFormer derivative.” https://github.com/alwalt/BioFM
#
# Data Source:
# ARCHS4 Human RNA-seq v2.5 (Lachmann et al., Nat Commun 2018)
# ------------------------------------------------------------

import torch
import torch.nn as nn
from torch_geometric.nn.conv import GCNConv
from performer_pytorch import Performer

# Default model hyperparameters
model_params = {
    "dim": 320,
    "bins": 10,
    "gb_repeat": 1,
    "p_repeat": 2,
    "bin_head": 8,
    "full_head": 4,
    "gene_length": 19357
}

# ------------------------------------------------------------
# Rotary Expression Embedding (REE)
# ------------------------------------------------------------

class PositionalExprEmbedding(nn.Module):
    """
    Rotary Expression Embedding (REE):
    Converts continuous gene expression values into a sinusoidal
    embedding usable by Performer/Transformer blocks. Deterministic,
    not learned. Masked positions (-10) → zero vector.
    """
    def __init__(self, dim, mask_token=-10):
        super().__init__()
        self.mask_token = mask_token
        self.inv_freq = nn.Parameter(
            1.0 / (100 ** (torch.arange(0, dim, 2).float() / dim)),
            requires_grad=False
        )

    def forward(self, x):
        mask = (x == self.mask_token).nonzero(as_tuple=False)
        x = torch.einsum("bi,j->bij", x, self.inv_freq)
        x = torch.cat([x.sin(), x.cos()], dim=-1)
        x[mask[:, 0], mask[:, 1]] = 0
        return x


# ------------------------------------------------------------
# GBFormer Block (Graph + Local Performer + Global Performer)
# ------------------------------------------------------------

class GBFormer(nn.Module):
    """
    A single GBFormer block:
      - LayerNorm
      - GCNConv (gene-gene propagation)
      - Binning by learned importance score
      - Local Performer per-bin
      - Global Performer
    """
    def __init__(self, dim, gene_length, bin_head, full_head, bins, p_repeat):
        super().__init__()

        self.dim = dim
        self.bins = bins
        self.bin_head = bin_head
        self.full_head = full_head
        self.p_repeat = p_repeat

        self.layernorm = nn.LayerNorm(dim)
        self.gcn = GCNConv(dim, dim, cached=True, add_self_loops=False)

        # Learn scoring → assign gene to bin
        self.which_bin = nn.Linear(dim, 1)

        # Local Performer per bin
        self.bin_layers = nn.ModuleList([
            Performer(
                dim=dim,
                heads=bin_head,
                depth=1,
                dim_head=dim // bin_head,
                attn_dropout=0.2,
                ff_dropout=0.2
            )
            for _ in range(bins)
        ])

        # Global Performer stack
        self.global_layers = nn.Sequential(*[
            Performer(
                dim=dim,
                heads=full_head,
                depth=1,
                dim_head=dim // full_head
            )
            for _ in range(p_repeat)
        ])

    def forward(self, x, graph):
        B, G, D = x.shape

        x = self.layernorm(x)
        x = x + self.gcn(x, graph)  # residual GCN update

        if self.bins > 0:
            scores = self.which_bin(x).squeeze(-1)       # [B, G]
            order = torch.argsort(scores, dim=1, descending=True)
            order_full = order.unsqueeze(-1).expand(-1, -1, D)

            x_sorted = x.gather(1, order_full)
            bin_size = (G - 1) // self.bins + 1
            chunks = torch.split(x_sorted, bin_size, dim=1)

            processed = [
                layer(chunk)
                for chunk, layer in zip(chunks, self.bin_layers)
            ]

            x_cat = torch.cat(processed, dim=1)
            x = torch.empty_like(x_cat).scatter_(1, order_full, x_cat)

        x = self.global_layers(x)
        return x


# ------------------------------------------------------------
# Full BulkFormer Model
# ------------------------------------------------------------

class BulkFormer(nn.Module):
    """
    CancerTranscriptome-Mini-48M:
    A compact BulkFormer-style masked-expression model.
    Combines:
      - ESM2 gene identity embeddings
      - Rotary Expression Embeddings (REE)
      - Graph Convolution (GCNConv)
      - Local/global Performer attention
      - Optional intermediate repr_layers for feature extraction
    """
    def __init__(
        self,
        dim,
        graph,
        gene_emb,
        gene_length,
        bin_head=4,
        full_head=4,
        bins=10,
        gb_repeat=1,
        p_repeat=1
    ):
        super().__init__()

        self.dim = dim
        self.graph = graph
        self.gene_length = gene_length

        # Identity embeddings from ESM2 (trainable projection)
        self.gene_emb = nn.Parameter(gene_emb)
        self.gene_proj = nn.Sequential(
            nn.Linear(gene_emb.shape[1], 4 * dim),
            nn.ReLU(),
            nn.Linear(4 * dim, dim)
        )

        # REE for expression
        self.expr_emb = PositionalExprEmbedding(dim)

        # Pre-attention mixing layer
        self.mix = nn.Sequential(
            nn.Linear(dim, 4 * dim),
            nn.ReLU(),
            nn.Linear(4 * dim, dim)
        )

        # Stacked GBFormer blocks
        self.gb_blocks = nn.ModuleList([
            GBFormer(dim, gene_length, bin_head, full_head, bins, p_repeat)
            for _ in range(gb_repeat)
        ])

        self.final_norm = nn.LayerNorm(dim)

        # Output head → scalar prediction per gene
        self.head = nn.Sequential(
            nn.Linear(dim, 4 * dim),
            nn.ReLU(),
            nn.Linear(4 * dim, 1),
            nn.ReLU()
        )

    def forward(self, x, repr_layers=None):
        B, G = x.shape
        hidden = {}

        x = (
            self.expr_emb(x) +
            self.gene_proj(self.gene_emb) +
            torch.zeros(B, 1, self.dim, device=x.device)  # no AE latent in this version
        )

        x = self.mix(x)

        for i, block in enumerate(self.gb_blocks):
            x = block(x, self.graph)
            if repr_layers and i in repr_layers:
                hidden[i] = x

        x = self.final_norm(x)
        out = self.head(x).squeeze(-1)

        if repr_layers:
            return out, hidden
        return out