-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneration.py
More file actions
244 lines (207 loc) · 7.98 KB
/
Copy pathgeneration.py
File metadata and controls
244 lines (207 loc) · 7.98 KB
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
#!/usr/bin/env python
import os
import json
import argparse
from src.generation.generate import generate_rna
from src.generation.configs import RTGenerationConfig
from src.generation.utils import save_to_fasta, read_fasta_sequences
def parse_args():
parser = argparse.ArgumentParser(
description="Generate RNA sequences for protein / DNA / RNA targets using RNA-X."
)
parser.add_argument(
"--targets-fasta",
required=True,
help="Path to FASTA file with target sequences (protein/DNA/RNA).",
)
parser.add_argument(
"--output-dir",
default="./outputs",
help="Directory where FASTA and JSON outputs will be written (default: outputs).",
)
parser.add_argument(
"--n-per-target",
type=int,
default=20,
help="Number of RNA sequences to generate per target (default: 20).",
)
parser.add_argument(
"--score-threshold",
type=float,
default=0.6,
help="Minimum score for accepting a generated RNA (default: 0.6).",
)
parser.add_argument(
"--rna-length",
type=int,
default=50,
help="Length of generated RNA sequences (default: 50).",
)
parser.add_argument(
"--target-type",
choices=["protein", "dna", "rna"],
default="protein",
help="Type of target sequences provided in the FASTA file (default: protein).",
)
parser.add_argument(
"--model-path",
default="weights/checkpoint",
help="Path to the main RNA-X model checkpoint (default: weights/checkpoint).",
)
parser.add_argument(
"--prediction-model-dir",
default="weights/prediction",
help="Directory containing prediction models, e.g. weights/prediction/protein.pth.",
)
parser.add_argument(
"--log-file",
default=None,
help="Optional path to a log file. If not set, logs are written to OUTPUT_DIR/log.",
)
parser.add_argument(
"--no-mcts",
action="store_true",
help="Disable MCTS during generation (apply_mcts=False).",
)
parser.add_argument(
"--no-scoring",
action="store_true",
help="Disable scoring model (use_scoring=False).",
)
return parser.parse_args()
def main():
args = parse_args()
os.makedirs(args.output_dir, exist_ok=True)
out_fasta = os.path.join(args.output_dir, "generated_rnas.fasta")
out_json = os.path.join(args.output_dir, "generated_rnas.json")
gen_work_dir = args.output_dir
log_file = (
args.log_file
if args.log_file is not None
else os.path.join(gen_work_dir, "log")
)
targets = read_fasta_sequences(args.targets_fasta)
if not targets:
raise ValueError(f"No targets found in FASTA file: {args.targets_fasta}")
all_sequences = []
all_headers = []
all_meta = []
total_attempts = 0
prediction_model_path = os.path.join(
args.prediction_model_dir, f"{args.target_type}.pth"
)
if not os.path.exists(args.model_path):
raise FileNotFoundError(
f"Model checkpoint not found at: {args.model_path}"
)
if not os.path.exists(prediction_model_path) and not args.no_scoring:
raise FileNotFoundError(
f"Prediction model not found at: {prediction_model_path}"
)
print(f"[info] Loaded {len(targets)} targets from {args.targets_fasta}")
print(f"[info] Output directory: {args.output_dir}")
print(f"[info] Generating {args.n_per_target} RNAs per target")
print(f"[info] Score threshold: {args.score_threshold}")
print(f"[info] RNA length: {args.rna_length}")
print(f"[info] Target type: {args.target_type}")
print(f"[info] Model path: {args.model_path}")
print(f"[info] Prediction model path: {prediction_model_path}")
print(f"[info] Log file: {log_file}")
print()
for target_idx, (target_name, target_seq) in enumerate(targets, start=1):
print(
f"[target {target_idx}/{len(targets)}] {target_name} "
f"(sequence length = {len(target_seq)})"
)
# Prepare the target string expected by generate_rna
if args.target_type == "protein":
target_for_model = "AA:" + target_seq
elif args.target_type == "dna":
target_for_model = "DNA:" + target_seq
elif args.target_type == "rna":
target_for_model = "RNA:" + target_seq
else:
raise ValueError(f"Unsupported target type: {args.target_type}")
accepted_for_this_target = 0
# Keep generating until we have n_per_target for THIS target
while accepted_for_this_target < args.n_per_target:
total_attempts += 1
cfg = RTGenerationConfig()
cfg.rna_length = args.rna_length
result = generate_rna(
protein_seq=target_for_model,
config=cfg,
log_file=log_file,
model_path=args.model_path,
prediction_model_path=prediction_model_path,
output_dir=None,
fasta_name=None,
use_scoring=not args.no_scoring,
apply_mcts=not args.no_mcts,
)
score = getattr(result, "best_value", None)
seq = getattr(result, "best_sequence", None)
scores_all = getattr(result, "best_scores", None)
if seq is None or score is None:
print(
f"[warn] Attempt {total_attempts}: generation failed; "
f"retrying this target."
)
continue
# Strip "RNA:" prefix if present
if seq.startswith("RNA:"):
seq = seq[4:]
if score >= args.score_threshold:
global_idx = len(all_sequences) + 1
header = (
f"RNA-X_{global_idx}|target={target_name}|"
f"len={len(seq)}|score={score:.4f}"
)
all_sequences.append(seq)
all_headers.append(header)
all_meta.append(
{
"global_index": global_idx,
"target_index": target_idx,
"target_name": target_name,
"header": header,
"score": float(score),
"scores": scores_all,
"length": len(seq),
"target_type": args.target_type,
}
)
accepted_for_this_target += 1
print(
f"[ok] Accepted RNA_{global_idx} for target "
f"{target_name} (len={len(seq)}, score={score:.3f}) "
f"[{accepted_for_this_target}/{args.n_per_target}]"
)
else:
print(
f"[low] Attempt {total_attempts}: score={score:.3f} "
f"< threshold {args.score_threshold}; retrying."
)
print(f"[target {target_idx}] Completed {args.n_per_target} sequences.\n")
# Save results once at the end
save_to_fasta(all_sequences, filename=out_fasta, headers=all_headers)
summary = {
"targets_fasta": os.path.abspath(args.targets_fasta),
"output_fasta": os.path.abspath(out_fasta),
"n_targets": len(targets),
"n_generated_total": len(all_sequences),
"n_per_target": args.n_per_target,
"rna_length": args.rna_length,
"score_threshold": args.score_threshold,
"target_type": args.target_type,
"model_path": os.path.abspath(args.model_path),
"prediction_model_path": os.path.abspath(prediction_model_path),
"attempts": total_attempts,
"details": all_meta,
}
with open(out_json, "w") as f:
json.dump(summary, f, indent=2)
print(f"[done] Wrote {len(all_sequences)} RNAs to {out_fasta}")
print(f"[done] Wrote metadata to {out_json}")
if __name__ == "__main__":
main()