-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
143 lines (111 loc) · 4.86 KB
/
predict.py
File metadata and controls
143 lines (111 loc) · 4.86 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
#!/usr/bin/env python
"""Inference script for fluorescent protein emission wavelength prediction.
CLI usage:
python predict.py artifacts/esm2/ensemble.json --sequence "MVSKGEE..."
python predict.py artifacts/esm2/ensemble.json --fasta candidates.fasta
Library usage:
from predict import EmissionPredictor
predictor = EmissionPredictor("artifacts/esm2/ensemble.json")
wavelength = predictor.predict("MVSKGEELFTGVVPILVELDGDVNGHKFSVSGEGEGDATYGKL")
wavelengths = predictor.predict_batch(["MVSK...", "MEEL...", ...])
"""
import argparse
import sys
from pathlib import Path
import numpy as np
import embeddings
import models
class EmissionPredictor:
"""Self-contained predictor that loads an artifact and handles embedding + inference."""
def __init__(self, artifact_path: str | Path):
self.artifact_path = Path(artifact_path)
self.artifact = models.load_artifact(self.artifact_path)
self._is_cross = (
self.artifact["model_type"] == "ensemble"
and "cross" in str(self.artifact.get("embedding", ""))
)
def predict(self, sequence: str) -> float:
"""Predict emission wavelength (nm) for a single amino acid sequence."""
return float(self.predict_batch([sequence])[0])
def _embed_sequences(self, sequences: list[str], model_name: str) -> tuple[np.ndarray, np.ndarray]:
"""Embed sequences with the given model. Returns (mean, augmented)."""
all_mean, all_aug = [], []
for seq in sequences:
m, a = embeddings.embed_single(seq, model_name)
all_mean.append(m)
all_aug.append(a)
return np.concatenate(all_mean, axis=0), np.concatenate(all_aug, axis=0)
def predict_batch(self, sequences: list[str]) -> np.ndarray:
"""Predict emission wavelengths (nm) for multiple sequences."""
if self._is_cross:
return self._predict_cross(sequences)
# Single-embedding model
emb_name = self.artifact["embedding"]
mean_emb, aug_emb = self._embed_sequences(sequences, emb_name)
if self.artifact["model_type"] == "ensemble":
X = {"mean": mean_emb, "augmented": aug_emb}
else:
pooling = self.artifact.get("pooling", "mean")
X = mean_emb if pooling == "mean" else aug_emb
return models.predict(self.artifact, X)
def _predict_cross(self, sequences: list[str]) -> np.ndarray:
"""Handle cross-embedding ensemble: each component uses its own embedding model."""
components = self.artifact["components"]
weights = self.artifact["weights"]
preds = []
for comp, w in zip(components, weights):
emb_name = comp["embedding"]
pooling = comp["pooling"]
mean_emb, aug_emb = self._embed_sequences(sequences, emb_name)
X = mean_emb if pooling == "mean" else aug_emb
preds.append(models.predict(comp, X) * w)
return sum(preds)
def _read_fasta(path: str) -> list[tuple[str, str]]:
"""Read sequences from a FASTA file. Returns list of (name, sequence)."""
entries = []
name = ""
seq_parts = []
with open(path) as f:
for line in f:
line = line.strip()
if line.startswith(">"):
if name and seq_parts:
entries.append((name, "".join(seq_parts)))
name = line[1:].split()[0]
seq_parts = []
elif line:
seq_parts.append(line)
if name and seq_parts:
entries.append((name, "".join(seq_parts)))
return entries
def main():
parser = argparse.ArgumentParser(
description="Predict fluorescent protein emission wavelength"
)
parser.add_argument("artifact", help="Path to saved model artifact")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--sequence", "-s", help="Amino acid sequence to predict")
group.add_argument("--fasta", "-f", help="Path to FASTA file with sequences")
args = parser.parse_args()
artifact_path = Path(args.artifact)
if not artifact_path.exists():
print(f"Artifact not found: {artifact_path}")
sys.exit(1)
predictor = EmissionPredictor(artifact_path)
if args.sequence:
wavelength = predictor.predict(args.sequence)
print(f"Predicted emission: {wavelength:.1f} nm")
else:
entries = _read_fasta(args.fasta)
if not entries:
print(f"No sequences found in {args.fasta}")
sys.exit(1)
print(f"Predicting {len(entries)} sequences...\n")
sequences = [seq for _, seq in entries]
wavelengths = predictor.predict_batch(sequences)
print(f"{'Name':<30s} {'Predicted (nm)':>14s}")
print("-" * 46)
for (name, _), wl in zip(entries, wavelengths):
print(f"{name:<30s} {wl:>14.1f}")
if __name__ == "__main__":
main()