File size: 7,967 Bytes
53ebf66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate 5' UTR candidates and rank them with FramePool and MTtrans."""

import argparse
import json
import os
import sys
from pathlib import Path


PROJECT_ROOT = Path(__file__).resolve().parents[1]
MODEL_ROOT = PROJECT_ROOT / "model"
MODULE_ROOT = MODEL_ROOT / "src" / "mrl_te_optimization"


def parse_args():
    parser = argparse.ArgumentParser(
        description="Generate UTRGAN candidates and rank by MRL and TE."
    )
    parser.add_argument("--num-candidates", type=int, default=1024)
    parser.add_argument("--batch-size", type=int, default=128)
    parser.add_argument("--seed", type=int, default=33)
    parser.add_argument("--device", choices=("dcu", "cpu"), default="dcu")
    parser.add_argument("--device-id", default="0")
    parser.add_argument(
        "--output-dir",
        default=str(PROJECT_ROOT / "outputs" / "pretrained_batch_ranking"),
    )
    return parser.parse_args()


def configure_runtime(args):
    os.environ.setdefault("TF_USE_LEGACY_KERAS", "1")
    os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", "2")
    if args.device == "cpu":
        os.environ["HIP_VISIBLE_DEVICES"] = "-1"
        os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
    else:
        os.environ["HIP_VISIBLE_DEVICES"] = args.device_id
        os.environ["CUDA_VISIBLE_DEVICES"] = args.device_id
    for import_root in (MODEL_ROOT, MODULE_ROOT):
        if str(import_root) not in sys.path:
            sys.path.insert(0, str(import_root))


def main():
    args = parse_args()
    if args.num_candidates < 1 or args.batch_size < 1:
        raise ValueError("--num-candidates and --batch-size must be positive")
    configure_runtime(args)

    import numpy as np
    import pandas as pd
    import tensorflow as tf
    import torch

    import framepool
    import util

    output_dir = Path(args.output_dir).expanduser().resolve()
    output_dir.mkdir(parents=True, exist_ok=True)

    generator_path = PROJECT_ROOT / "weight" / "checkpoint_3000.h5"
    framepool_path = PROJECT_ROOT / "weight" / "utr_model_combined_residual_new.h5"
    mttrans_path = (
        PROJECT_ROOT
        / "weight"
        / "mttrans"
        / "RL_hard_share_MTL"
        / "3R"
        / "schedule_MTL-model_best_cv1.pth"
    )
    for path in (generator_path, framepool_path, mttrans_path):
        if not path.is_file():
            raise FileNotFoundError(path)

    tf_device = "/GPU:0" if args.device == "dcu" else "/CPU:0"
    torch_device = torch.device("cuda:0" if args.device == "dcu" else "cpu")
    if args.device == "dcu":
        tf_gpus = tf.config.list_physical_devices("GPU")
        if not tf_gpus:
            raise RuntimeError("TensorFlow did not detect a DCU")
        if not torch.cuda.is_available():
            raise RuntimeError("PyTorch did not detect a DCU")
        for gpu in tf_gpus:
            try:
                tf.config.experimental.set_memory_growth(gpu, True)
            except RuntimeError:
                pass

    # Loading on CPU avoids device-side random-initializer kernels; inference
    # is explicitly placed on the requested device below.
    with tf.device("/CPU:0"):
        generator = tf.keras.models.load_model(generator_path, compile=False)
        mrl_model = framepool.load_framepool(str(framepool_path))
    generator.trainable = False
    mrl_model.trainable = False

    checkpoint = torch.load(
        mttrans_path, map_location="cpu", weights_only=False
    )
    te_model = checkpoint["state_dict"].to(torch_device)
    te_model.eval()

    np.random.seed(args.seed)
    tf.random.set_seed(args.seed)
    torch.manual_seed(args.seed)
    if args.device == "dcu":
        torch.cuda.manual_seed_all(args.seed)

    noise = np.random.RandomState(args.seed).normal(
        size=(args.num_candidates, 40)
    ).astype(np.float32)

    generated_batches = []
    with tf.device(tf_device):
        for start in range(0, args.num_candidates, args.batch_size):
            stop = min(start + args.batch_size, args.num_candidates)
            generated_batches.append(
                generator(tf.convert_to_tensor(noise[start:stop]), training=False).numpy()
            )
    generated = np.concatenate(generated_batches, axis=0)
    if generated.shape != (args.num_candidates, 128, 5):
        raise RuntimeError(f"Unexpected generator shape: {generated.shape}")
    if not np.isfinite(generated).all():
        raise RuntimeError("Generator output contains NaN/Inf")

    sequences = list(util.recover_seq(generated, util.rev_rna_vocab))
    mrl_scores = []
    with tf.device(tf_device):
        for start in range(0, len(sequences), args.batch_size):
            chunk = sequences[start : start + args.batch_size]
            encoded = np.asarray(
                [util.encode_seq_framepool(seq) for seq in chunk],
                dtype=np.float32,
            )
            prediction = mrl_model(tf.convert_to_tensor(encoded), training=False)
            mrl_scores.extend(tf.reshape(prediction, (-1,)).numpy().tolist())

    te_scores = []
    with torch.inference_mode():
        for start in range(0, len(sequences), args.batch_size):
            chunk = sequences[start : start + args.batch_size]
            encoded = np.asarray(util.one_hot_all_motif(chunk), dtype=np.float32)
            encoded = torch.from_numpy(encoded).transpose(1, 2).to(torch_device)
            prediction = te_model(encoded)
            te_scores.extend(prediction.reshape(-1).cpu().numpy().tolist())

    mrl_scores = np.asarray(mrl_scores, dtype=np.float32)
    te_scores = np.asarray(te_scores, dtype=np.float32)
    if not np.isfinite(mrl_scores).all() or not np.isfinite(te_scores).all():
        raise RuntimeError("MRL/TE scores contain NaN/Inf")

    table = pd.DataFrame(
        {
            "candidate_id": [
                f"UTRGAN_{index + 1:05d}" for index in range(len(sequences))
            ],
            "sequence": sequences,
            "length": [len(sequence) for sequence in sequences],
            "mrl_score": mrl_scores,
            "te_score": te_scores,
        }
    )
    table["is_duplicate"] = table.duplicated("sequence", keep="first")
    table["mrl_rank"] = table["mrl_score"].rank(
        method="first", ascending=False
    ).astype(int)
    table["te_rank"] = table["te_score"].rank(
        method="first", ascending=False
    ).astype(int)
    unique = table.drop_duplicates("sequence", keep="first").copy()

    table.to_csv(output_dir / "all_candidates_scores.csv", index=False)
    unique.sort_values("mrl_score", ascending=False).to_csv(
        output_dir / "ranked_by_mrl.csv", index=False
    )
    unique.sort_values("te_score", ascending=False).to_csv(
        output_dir / "ranked_by_te.csv", index=False
    )
    np.save(output_dir / "generator_probabilities.npy", generated)

    summary = {
        "requested_candidates": args.num_candidates,
        "generated_candidates": len(table),
        "unique_sequences": len(unique),
        "duplicate_sequences": int(table["is_duplicate"].sum()),
        "generator_shape": list(generated.shape),
        "generator_probability_max_error": float(
            np.max(np.abs(generated.sum(axis=-1) - 1.0))
        ),
        "length_min": int(table["length"].min()),
        "length_max": int(table["length"].max()),
        "mrl_min": float(mrl_scores.min()),
        "mrl_max": float(mrl_scores.max()),
        "mrl_mean": float(mrl_scores.mean()),
        "te_min": float(te_scores.min()),
        "te_max": float(te_scores.max()),
        "te_mean": float(te_scores.mean()),
        "tensorflow_version": tf.__version__,
        "torch_version": torch.__version__,
        "torch_hip": torch.version.hip,
        "device": args.device,
        "seed": args.seed,
    }
    (output_dir / "summary.json").write_text(
        json.dumps(summary, indent=2), encoding="utf-8"
    )
    print(json.dumps(summary, indent=2))
    print("UTRGAN_PRETRAINED_BATCH_MRL_TE_RANKING_PASS")


if __name__ == "__main__":
    main()