File size: 11,446 Bytes
4e881f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
Utilities for the Transcription Factor Binding (TFB) demo notebook.

This module centralizes reusable logic so the notebook stays concise:
- DeepSEA-style dataset wrapper around OmniDataset
- Tokenizer and model loader for multi-label classification
- Dataset and DataLoader builders
- Training, evaluation, and simple inference helpers
"""
import zipfile
from typing import Optional, List, Dict, Any

import os
import json
import requests
import torch
import torch.nn as nn
import findfile

from transformers import AutoTokenizer, AutoModel

from omnigenbench import (
    OmniDataset,
    ClassificationMetric,
    AccelerateTrainer,
    ModelHub,
    OmniModelForMultiLabelSequenceClassification,
    OmniModel,
    # Import integrated baselines from the package
    OmniCNNBaseline,
    OmniRNNBaseline,
    OmniBasenjiBaseline,
    OmniBPNetBaseline,
    OmniDeepSTARRBaseline,
)


# ---------------- New helpers for 1D-conv baselines ----------------
class _TokenIdsToOneHot4(nn.Module):
    """Map tokenizer input_ids [B,L] to one-hot channels [B,4,L] for A,C,G,T.

    Unknown tokens (including N and pads) map to all-zero columns.
    """
    def __init__(self, tokenizer):
        super().__init__()
        vocab = tokenizer.get_vocab() if hasattr(tokenizer, "get_vocab") else {}
        # Build mapping id->channel for A,C,G,T (0..3), else -1
        token_to_channel = {"A": 0, "C": 1, "G": 2, "T": 3, "a": 0, "c": 1, "g": 2, "t": 3}
        vocab_size = len(vocab) if vocab else getattr(tokenizer, "vocab_size", 0) or 0
        mapping = torch.full((max(vocab_size, 1),), fill_value=-1, dtype=torch.long)
        if vocab:
            for tok, idx in vocab.items():
                if tok in token_to_channel:
                    mapping[idx] = token_to_channel[tok]
        else:
            # Fallback: try direct ids for single-letter tokens
            for ch, ch_idx in token_to_channel.items():
                try:
                    idx = tokenizer.convert_tokens_to_ids(ch)
                    if isinstance(idx, int) and idx >= 0:
                        if idx >= mapping.numel():
                            # expand mapping tensor if tokenizer reports larger id
                            new_map = torch.full((idx + 1,), fill_value=-1, dtype=torch.long)
                            new_map[: mapping.numel()] = mapping
                            mapping = new_map
                        mapping[idx] = ch_idx
                except Exception:
                    pass
        self.register_buffer("id2chan", mapping)

    def forward(self, input_ids: torch.Tensor) -> torch.Tensor:
        # input_ids: [B,L]
        if input_ids.dtype != torch.long:
            input_ids = input_ids.long()
        vocab_to_channel = self.id2chan
        max_id = int(input_ids.max().item()) if input_ids.numel() > 0 else -1
        if max_id >= vocab_to_channel.numel():
            # Expand mapping lazily with -1 for unknown new ids
            new_map = torch.full((max_id + 1,), fill_value=-1, dtype=torch.long, device=vocab_to_channel.device)
            new_map[: vocab_to_channel.numel()] = vocab_to_channel
            self.id2chan = new_map  # type: ignore[attr-defined]
            vocab_to_channel = self.id2chan
        idx = vocab_to_channel[input_ids]
        # Map unknown (-1) to 4 then one-hot num_classes=5 and slice first 4
        idx_clamped = idx.clamp(min=-1)
        idx_clamped = torch.where(idx_clamped < 0, torch.full_like(idx_clamped, 4), idx_clamped)
        one_hot5 = torch.nn.functional.one_hot(idx_clamped, num_classes=5).to(torch.float32)
        x = one_hot5[..., :4].permute(0, 2, 1).contiguous()  # [B,4,L]
        return x


def download_deepsea_dataset(local_dir):
    if not findfile.find_cwd_dir(local_dir, disable_alert=True):
        os.makedirs(local_dir, exist_ok=True)
    # else:
    #     return
    url_to_download = "https://huggingface.co/datasets/yangheng/deepsea_tfb_prediction/resolve/main/deepsea_tfb_prediction.zip"
    zip_path = os.path.join(local_dir, "deepsea_tfb_prediction.zip")
    if not os.path.exists(zip_path):
        print(f"Downloading deepsea_tfb_prediction.zip from {url_to_download}...")
        response = requests.get(url_to_download, stream=True)
        response.raise_for_status()

        with open(zip_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
        print(f"Downloaded {zip_path}")

    # Unzip the dataset if the zip file exists
    ZIP_DATASET = findfile.find_cwd_file("deepsea_tfb_prediction.zip")
    if ZIP_DATASET:
        with zipfile.ZipFile(ZIP_DATASET, 'r') as zip_ref:
            zip_ref.extractall(local_dir)
        print(f"Extracted deepsea_tfb_prediction.zip into {local_dir}")
        os.remove(ZIP_DATASET)
    else:
        print("deepsea_tfb_prediction.zip not found. Skipping extraction.")


class DeepSEADataset(OmniDataset):
    """
    A dataset for the DeepSEA task that converts DNA sequences to tokenized inputs.
    Accepts JSONL where each line contains a `sequence` field and `label(s)`.
    """

    def __init__(
        self,
        data_source: str,
        tokenizer,
        max_length: Optional[int] = None,
        **kwargs,
    ) -> None:
        super().__init__(data_source, tokenizer, max_length, **kwargs)
        self.label_indices = None
        for key, value in kwargs.items():
            self.metadata[key] = value

    def prepare_input(self, instance: Dict[str, Any], **kwargs) -> Dict[str, torch.Tensor]:
        def truncate(seq: str, windowsize: Optional[int]) -> str:
            if windowsize is None:
                return seq
            if len(seq) == windowsize:
                return seq
            if len(seq) > windowsize:
                left = (len(seq) - windowsize) // 2
                return seq[left:left + windowsize]
            return seq + ("N" * (windowsize - len(seq)))

        sequence = instance.get('sequence') or instance.get('seq')
        labels = instance.get('label', None) if 'label' in instance else instance.get('labels', None)

        tokenized_inputs = self.tokenizer(
            truncate(sequence, self.max_length),
            padding="do_not_pad",
            truncation=True,
            max_length=self.max_length,
            return_tensors="pt",
            **kwargs,
        )

        labels_tensor = torch.tensor(labels, dtype=torch.float32) if labels is not None else None
        if labels_tensor is not None and hasattr(self, 'label_indices') and self.label_indices is not None:
            labels_tensor = labels_tensor[torch.tensor(self.label_indices, dtype=torch.long)]
        tokenized_inputs["labels"] = labels_tensor

        for col in tokenized_inputs:
            if isinstance(tokenized_inputs[col], torch.Tensor) and tokenized_inputs[col].ndim > 1:
                tokenized_inputs[col] = tokenized_inputs[col].squeeze(0)

        return tokenized_inputs


def load_tokenizer_and_model(
    model_name_or_path: str,
    num_labels: int,
    threshold: float = 0.5,
    device: Optional[torch.device] = None,
):
    """
    Load tokenizer and OmniGenome-based multi-label classification model.
    """
    tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
    base_model = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True)

    model = OmniModelForMultiLabelSequenceClassification(
        base_model,
        tokenizer,
        num_labels=num_labels,
        threshold=threshold,
    )
    if device is not None:
        model = model.to(device).to(torch.float32)

    return tokenizer, model


def build_datasets(
    tokenizer,
    train_file: str,
    test_file: str,
    valid_file: Optional[str],
    max_length: int,
    max_examples: Optional[int],
    label_indices: Optional[List[int]] = None,
):
    """
    Create DeepSEADataset instances for train/valid/test.
    """
    def make_ds(path: str) -> DeepSEADataset:
        ds = DeepSEADataset(
            data_source=path,
            tokenizer=tokenizer,
            max_length=max_length,
            max_examples=max_examples,
            force_padding=False,
        )
        # attach label indices for internal selection
        ds.label_indices = label_indices
        return ds

    train_set = make_ds(train_file)
    test_set = make_ds(test_file)
    valid_set = make_ds(valid_file) if (valid_file and os.path.exists(valid_file)) else None
    return train_set, valid_set, test_set


def create_dataloaders(
    train_set: torch.utils.data.Dataset,
    valid_set: Optional[torch.utils.data.Dataset],
    test_set: Optional[torch.utils.data.Dataset],
    batch_size: int,
):
    train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True)
    test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size) if test_set is not None else None
    valid_loader = torch.utils.data.DataLoader(valid_set, batch_size=batch_size) if valid_set is not None else None
    return train_loader, valid_loader, test_loader


def run_finetuning(
    model: torch.nn.Module,
    train_loader,
    valid_loader,
    test_loader,
    epochs: int,
    learning_rate: float,
    weight_decay: float,
    patience: int,
    device: torch.device,
    save_dir: str = "tfb_model",
    seed: Optional[int] = 2024,
):
    """
    Train the model with AccelerateTrainer and save to `save_dir`.
    Returns the trainer and the best metrics.
    """
    metric_fn = [ClassificationMetric(ignore_y=-100).roc_auc_score]
    optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)

    trainer = AccelerateTrainer(
        model=model,
        train_loader=train_loader,
        eval_loader=valid_loader,
        test_loader=test_loader,
        optimizer=optimizer,
        epochs=epochs,
        compute_metrics=metric_fn,
        patience=patience,
        device=device,
        seed=seed,
    )

    metrics_best = None
    if not os.path.exists(save_dir):
        metrics_best = trainer.train()
        trainer.save_model(save_dir)
    else:
        metrics_best = {"info": f"Found existing '{save_dir}'. Skipped training."}

    return trainer, metrics_best



def run_inference(
    model_dir: str,
    tokenizer,
    sample_sequence: str,
    max_length: int,
    device: torch.device,
):
    """
    Load a saved model via ModelHub and run a single-sequence inference.
    Returns a dict containing predictions and probabilities when available.
    """
    model = ModelHub.load(model_dir)
    model.to(device)

    inputs = tokenizer(sample_sequence, return_tensors="pt", max_length=max_length, truncation=True)
    inputs = inputs.to(torch.float32)
    with torch.no_grad():
        outputs = model.inference(inputs, device=device)
    return outputs


class _MaskedGlobalMaxPool1d(nn.Module):
    def forward(self, x: torch.Tensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor:
        if attention_mask is None:
            return x.max(dim=1).values
        masked_x = x.masked_fill(attention_mask.unsqueeze(-1).eq(0), float("-inf"))
        return masked_x.max(dim=1).values


__all__ = [
    "DeepSEADataset",
    "load_tokenizer_and_model",
    "build_datasets",
    "create_dataloaders",
    "run_finetuning",
    "run_inference",
    "OmniCNNBaseline",
    "OmniRNNBaseline",
    "OmniBPNetBaseline",
    "OmniBasenjiBaseline",
    "OmniDeepSTARRBaseline",
]