File size: 20,232 Bytes
f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 b6d1ec8 f8a4853 |
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
predict_models_dir.py
Predict for all models in models_dir on a folder of FASTA genomes.
Optionally annotate with ground truth from a TSV and compute the same metrics
as in your original script (overall + Isolate + MAG + AUC).
Inputs:
--genomes_dir Folder with FASTA files (.fna/.fa/.fasta)
--models_dir Folder with model_*.joblib + feature_columns_*.json
--outdir Output folder
--truth_tsv OPTIONAL: genomes-all_metadata_with_genetic_code_id_noNA.tsv
(must contain Genome, Genome_type, Genetic_code_ID)
Ground truth (if provided):
ALT = Genetic_code_ID != 11
STD = Genetic_code_ID == 11
Outputs:
- <outdir>/<model>__pred.csv (per model, per genome)
- <outdir>/all_models_predictions_long.csv (long: model x genome)
- <outdir>/prediction_summary.csv (ONLY if truth_tsv is provided)
- <outdir>/top_models_by_pr_auc.txt (ONLY if truth_tsv is provided)
Requires:
- aragorn in PATH (or pass --aragorn)
"""
import os
import re
import json
import time
import argparse
import subprocess
from pathlib import Path
from collections import Counter
import numpy as np
import pandas as pd
from joblib import load as joblib_load
from sklearn.metrics import (
confusion_matrix,
accuracy_score,
precision_score,
recall_score,
f1_score,
roc_auc_score,
average_precision_score,
)
from sklearn.base import BaseEstimator, ClassifierMixin, clone
# =========================
# PU class (for joblib load)
# =========================
class PUBaggingClassifier(BaseEstimator, ClassifierMixin):
def __init__(self, base_estimator, n_bags=15, u_ratio=3.0, random_state=42):
self.base_estimator = base_estimator
self.n_bags = int(n_bags)
self.u_ratio = float(u_ratio)
self.random_state = int(random_state)
self.models_ = None
self.classes_ = np.array([0, 1], dtype=int)
def fit(self, X, y, sample_weight=None):
y = np.asarray(y).astype(int)
pos_idx = np.where(y == 1)[0]
unl_idx = np.where(y == 0)[0]
if pos_idx.size == 0:
raise ValueError("PU training requires at least one positive sample (y==1).")
rng = np.random.RandomState(self.random_state)
self.models_ = []
if unl_idx.size == 0:
m = clone(self.base_estimator)
try:
if sample_weight is not None:
m.fit(X, y, sample_weight=np.asarray(sample_weight))
else:
m.fit(X, y)
except TypeError:
m.fit(X, y)
self.models_.append(m)
return self
k_u = int(min(unl_idx.size, max(1, round(self.u_ratio * pos_idx.size))))
for _ in range(self.n_bags):
u_b = rng.choice(unl_idx, size=k_u, replace=(k_u > unl_idx.size))
idx_b = np.concatenate([pos_idx, u_b])
X_b = X.iloc[idx_b] if hasattr(X, "iloc") else X[idx_b]
y_b = y[idx_b]
sw_b = None
if sample_weight is not None:
sw_b = np.asarray(sample_weight)[idx_b]
m = clone(self.base_estimator)
try:
if sw_b is not None:
m.fit(X_b, y_b, sample_weight=sw_b)
else:
m.fit(X_b, y_b)
except TypeError:
m.fit(X_b, y_b)
self.models_.append(m)
return self
def predict_proba(self, X):
if not self.models_:
raise RuntimeError("PUBaggingClassifier not fitted")
probs = [m.predict_proba(X) for m in self.models_]
return np.mean(np.stack(probs, axis=0), axis=0)
def predict(self, X):
return (self.predict_proba(X)[:, 1] >= 0.5).astype(int)
# =========================
# Feature extraction
# =========================
CODON_RE = re.compile(r"\(([ACGTUacgtu]{3})\)")
def set_single_thread_env():
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
def list_fasta_files(genomes_dir: str):
exts = (".fna", ".fa", ".fasta")
paths = []
for fn in os.listdir(genomes_dir):
p = os.path.join(genomes_dir, fn)
if not os.path.isfile(p):
continue
if fn.endswith(exts):
paths.append(p)
return sorted(paths)
def calc_gc_and_tetra(fasta_path):
bases = ["A", "C", "G", "T"]
all_kmers = ["".join([a, b, c, d]) for a in bases for b in bases for c in bases for d in bases]
tetra_counts = {k: 0 for k in all_kmers}
A = C = G = T = 0
tail = ""
with open(fasta_path, "r") as fh:
for line in fh:
if line.startswith(">"):
continue
s = line.strip().upper().replace("U", "T")
s = re.sub(r"[^ACGT]", "N", s)
if not s:
continue
seq = tail + s
for ch in s:
if ch == "A": A += 1
elif ch == "C": C += 1
elif ch == "G": G += 1
elif ch == "T": T += 1
for i in range(len(seq) - 3):
k = seq[i:i+4]
if "N" in k:
continue
tetra_counts[k] += 1
tail = seq[-3:] if len(seq) >= 3 else seq
total_acgt = A + C + G + T
gc_percent = (float(G + C) / float(total_acgt) * 100.0) if total_acgt > 0 else 0.0
windows_total = sum(tetra_counts.values())
denom = float(windows_total) if windows_total > 0 else 1.0
tetra_freq = {f"tetra_{k}": float(v) / denom for k, v in tetra_counts.items()}
features = {
"gc_percent": float(gc_percent),
"genome_length": float(total_acgt),
}
features.update(tetra_freq)
return features
def run_aragorn(aragorn_bin, fasta_path, out_txt):
cmd = [aragorn_bin, "-t", "-l", "-gc1", "-w", "-o", out_txt, fasta_path]
with open(os.devnull, "w") as devnull:
subprocess.run(cmd, stdout=devnull, stderr=devnull, check=False)
def parse_anticodons_from_aragorn(aragorn_txt):
counts = Counter()
if not os.path.isfile(aragorn_txt):
return counts
with open(aragorn_txt, "r") as fh:
for line in fh:
for m in CODON_RE.finditer(line):
cod = m.group(1).upper().replace("U", "T")
if re.fullmatch(r"[ACGT]{3}", cod):
counts[cod] += 1
return counts
def build_ac_features(anticodon_counts):
bases = ["A", "C", "G", "T"]
feats = {}
for a in bases:
for b in bases:
for c in bases:
cod = f"{a}{b}{c}"
feats[f"ac_{cod}"] = float(anticodon_counts.get(cod, 0))
return feats
def build_plr_features(ac_features, needed_plr_cols, eps=0.5):
plr_feats = {}
for col in needed_plr_cols:
core = col[len("plr_"):]
left, right = core.split("__")
a = ac_features.get(f"ac_{left}", 0.0)
b = ac_features.get(f"ac_{right}", 0.0)
plr_feats[col] = float(np.log((a + eps) / (b + eps)))
return plr_feats
def build_features_for_genome(fasta_path, aragorn_bin, feature_columns, reuse_aragorn=True):
acc = os.path.splitext(os.path.basename(fasta_path))[0]
feat_gc_tetra = calc_gc_and_tetra(fasta_path)
tmp_aragorn = fasta_path + ".aragorn.txt"
if reuse_aragorn and os.path.isfile(tmp_aragorn):
try:
if os.path.getmtime(tmp_aragorn) < os.path.getmtime(fasta_path):
run_aragorn(aragorn_bin, fasta_path, tmp_aragorn)
except Exception:
run_aragorn(aragorn_bin, fasta_path, tmp_aragorn)
else:
run_aragorn(aragorn_bin, fasta_path, tmp_aragorn)
anticodon_counts = parse_anticodons_from_aragorn(tmp_aragorn)
ac_feats = build_ac_features(anticodon_counts)
plr_cols = [c for c in feature_columns if c.startswith("plr_")]
plr_feats = build_plr_features(ac_feats, plr_cols) if plr_cols else {}
all_feats = {}
all_feats.update(ac_feats)
all_feats.update(plr_feats)
all_feats.update(feat_gc_tetra)
row = {col: float(all_feats.get(col, 0.0)) for col in feature_columns}
return acc, row
# =========================
# Ground truth from TSV (optional)
# =========================
def load_truth_tsv(tsv_path: str) -> pd.DataFrame:
df = pd.read_csv(tsv_path, sep="\t", dtype=str)
for col in ["Genome", "Genome_type", "Genetic_code_ID"]:
if col not in df.columns:
raise ValueError(f"TSV missing column '{col}'. Columns: {list(df.columns)}")
df["Genome"] = df["Genome"].astype(str)
df["Genome_type"] = df["Genome_type"].astype(str)
df["Genetic_code_ID"] = pd.to_numeric(df["Genetic_code_ID"], errors="coerce").astype("Int64")
# ALT ground truth: != 11
df["y_true_alt"] = df["Genetic_code_ID"].apply(lambda x: (pd.notna(x) and int(x) != 11)).astype(int)
df["true_label"] = df["y_true_alt"].map({0: "STD", 1: "ALT"})
return df[["Genome", "Genome_type", "Genetic_code_ID", "y_true_alt", "true_label"]]
# =========================
# Metrics (only if truth exists)
# =========================
def safe_confusion(y_true, y_pred):
cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
tn, fp, fn, tp = int(cm[0,0]), int(cm[0,1]), int(cm[1,0]), int(cm[1,1])
return tn, fp, fn, tp
def compute_metrics_block(y_true, y_pred, y_score=None):
y_true = np.asarray(y_true, dtype=int)
y_pred = np.asarray(y_pred, dtype=int)
tn, fp, fn, tp = safe_confusion(y_true, y_pred)
n = int(len(y_true))
pos = int(np.sum(y_true == 1))
out = {
"n": n,
"positives": pos,
"tn": tn, "fp": fp, "fn": fn, "tp": tp,
"accuracy": float(accuracy_score(y_true, y_pred)) if n else np.nan,
"precision": float(precision_score(y_true, y_pred, zero_division=0)) if n else np.nan,
"recall": float(recall_score(y_true, y_pred, zero_division=0)) if n else np.nan,
"f1": float(f1_score(y_true, y_pred, zero_division=0)) if n else np.nan,
"specificity": float(tn / (tn + fp)) if (tn + fp) > 0 else np.nan,
"fn_rate": float(fn / (fn + tp)) if (fn + tp) > 0 else np.nan, # 1 - recall
"fp_rate": float(fp / (fp + tn)) if (fp + tn) > 0 else np.nan,
}
if y_score is not None:
y_score = np.asarray(y_score, dtype=float)
if n > 0 and len(np.unique(y_true)) == 2:
try:
out["roc_auc"] = float(roc_auc_score(y_true, y_score))
except Exception:
out["roc_auc"] = np.nan
try:
out["pr_auc"] = float(average_precision_score(y_true, y_score))
except Exception:
out["pr_auc"] = np.nan
else:
out["roc_auc"] = np.nan
out["pr_auc"] = np.nan
else:
out["roc_auc"] = np.nan
out["pr_auc"] = np.nan
return out
# =========================
# Model discovery
# =========================
def find_models(models_dir: Path):
return sorted(models_dir.glob("model_*.joblib"))
def pick_feature_cols(models_dir: Path, feature_cols_arg: str | None):
if feature_cols_arg:
return Path(feature_cols_arg)
p = models_dir / "feature_columns_64log_gc_tetra.json"
if p.exists():
return p
candidates = sorted(models_dir.glob("feature_columns_*.json"))
if not candidates:
raise FileNotFoundError(f"No feature_columns_*.json found in {models_dir}")
return candidates[0]
# =========================
# Main
# =========================
def main():
ap = argparse.ArgumentParser(
description="Predict for all models in a directory; optionally annotate truth from TSV and compute metrics."
)
ap.add_argument("--genomes_dir", required=True, help="Folder with FASTA genomes (.fna/.fa/.fasta).")
ap.add_argument("--models_dir", required=True, help="Folder with model_*.joblib + feature_columns_*.json.")
ap.add_argument("--outdir", required=True, help="Output folder for CSV predictions.")
ap.add_argument("--aragorn", default="aragorn", help="Path to ARAGORN binary.")
ap.add_argument("--feature_cols", default=None, help="Optional: force a specific feature_columns_*.json.")
ap.add_argument("--reuse_aragorn", action="store_true", help="Reuse *.aragorn.txt if it exists and is fresh.")
ap.add_argument(
"--truth_tsv",
default=None,
help="OPTIONAL: genomes-all_metadata_with_genetic_code_id_noNA.tsv (Genome, Genome_type, Genetic_code_ID).",
)
args = ap.parse_args()
set_single_thread_env()
genomes_dir = Path(args.genomes_dir)
models_dir = Path(args.models_dir)
outdir = Path(args.outdir)
outdir.mkdir(parents=True, exist_ok=True)
fasta_files = list_fasta_files(str(genomes_dir))
if not fasta_files:
raise SystemExit(f"No FASTA files found in {genomes_dir}")
models = find_models(models_dir)
if not models:
raise SystemExit(f"No model_*.joblib found in {models_dir}")
feat_cols_path = pick_feature_cols(models_dir, args.feature_cols)
print(f"[INFO] Genomes : {len(fasta_files)} in {genomes_dir}")
print(f"[INFO] Models : {len(models)} in {models_dir}")
print(f"[INFO] FeatCols: {feat_cols_path}")
print(f"[INFO] Truth : {args.truth_tsv if args.truth_tsv else '(none)'}")
# Load feature columns
with open(feat_cols_path, "r") as fh:
feature_columns = json.load(fh)
# Optional truth
truth = None
if args.truth_tsv:
truth = load_truth_tsv(args.truth_tsv)
# Build features once (shared across all models)
t_feat0 = time.time()
rows, accs = [], []
for i, fasta in enumerate(fasta_files, 1):
if i % 50 == 0 or i == 1 or i == len(fasta_files):
print(f"[FEAT] {i}/{len(fasta_files)} {os.path.basename(fasta)}")
acc, feats = build_features_for_genome(
fasta_path=fasta,
aragorn_bin=args.aragorn,
feature_columns=feature_columns,
reuse_aragorn=args.reuse_aragorn
)
accs.append(acc)
rows.append(feats)
X = pd.DataFrame(rows, index=accs)[feature_columns]
print(f"[FEAT] Built X={X.shape} in {(time.time()-t_feat0):.1f}s")
# Annotation base table (always exists)
ann = pd.DataFrame({"Genome": accs})
if truth is not None:
ann = ann.merge(truth, how="left", on="Genome")
n_annot = int(ann["y_true_alt"].notna().sum())
n_missing = int(len(ann) - n_annot)
print(f"[TRUTH] Annotated: {n_annot}/{len(ann)} Missing_in_TSV: {n_missing}")
else:
# Ensure columns exist for consistent outputs
ann["Genome_type"] = pd.NA
ann["Genetic_code_ID"] = pd.NA
ann["y_true_alt"] = pd.NA
ann["true_label"] = pd.NA
long_rows = []
summary_rows = []
for mi, model_path in enumerate(models, 1):
model_name = model_path.stem
print("\n" + "="*80)
print(f"[{mi}/{len(models)}] MODEL: {model_path.name}")
print("="*80)
t0 = time.time()
model = joblib_load(model_path)
# Probabilities (if possible)
proba = None
if hasattr(model, "predict_proba"):
try:
proba = model.predict_proba(X)[:, 1]
except Exception:
proba = None
# Pred class
if hasattr(model, "predict"):
try:
yhat = model.predict(X)
except Exception:
yhat = (proba >= 0.5).astype(int) if proba is not None else np.zeros(len(X), dtype=int)
else:
yhat = (proba >= 0.5).astype(int) if proba is not None else np.zeros(len(X), dtype=int)
elapsed = time.time() - t0
df_pred = ann.copy()
df_pred["model"] = model_name
df_pred["y_pred_alt"] = np.asarray(yhat).astype(int)
df_pred["pred_label"] = df_pred["y_pred_alt"].map({0: "STD", 1: "ALT"})
df_pred["proba_alt"] = np.asarray(proba, dtype=float) if proba is not None else np.nan
out_csv = outdir / f"{model_name}__pred.csv"
df_pred.to_csv(out_csv, index=False)
print(f"[WRITE] {out_csv} rows={len(df_pred)} time={(elapsed/60):.2f} min")
# Long output
keep_cols = ["model", "Genome", "Genome_type", "Genetic_code_ID", "y_true_alt", "y_pred_alt", "proba_alt"]
keep_cols = [c for c in keep_cols if c in df_pred.columns]
long_rows.extend(df_pred[keep_cols].to_dict(orient="records"))
# Metrics only if we have truth annotations
if truth is not None:
df_eval = df_pred[df_pred["y_true_alt"].notna()].copy()
if df_eval.shape[0] == 0:
print("[METRICS] No annotated genomes for this run (truth TSV did not match Genome names).")
continue
y_true = df_eval["y_true_alt"].astype(int).values
y_pred = df_eval["y_pred_alt"].astype(int).values
y_score = df_eval["proba_alt"].astype(float).values if proba is not None else None
overall = compute_metrics_block(y_true, y_pred, y_score=y_score)
def subset_metrics(gen_type: str):
sub = df_eval[df_eval["Genome_type"] == gen_type]
if sub.shape[0] == 0:
return None
yt = sub["y_true_alt"].astype(int).values
yp = sub["y_pred_alt"].astype(int).values
ys = sub["proba_alt"].astype(float).values if proba is not None else None
return compute_metrics_block(yt, yp, y_score=ys)
iso = subset_metrics("Isolate")
mag = subset_metrics("MAG")
srow = {
"model": model_name,
"model_file": str(model_path),
"feature_cols": str(feat_cols_path),
"n_genomes_total": int(len(df_pred)),
"n_annotated": int(df_eval.shape[0]),
"n_missing_truth": int(len(df_pred) - df_eval.shape[0]),
"elapsed_sec": float(elapsed),
"elapsed_min": float(elapsed/60.0),
}
for k, v in overall.items():
srow[f"overall_{k}"] = v
if iso is not None:
for k, v in iso.items():
srow[f"isolate_{k}"] = v
if mag is not None:
for k, v in mag.items():
srow[f"mag_{k}"] = v
summary_rows.append(srow)
# Write combined outputs
long_csv = outdir / "all_models_predictions_long.csv"
pd.DataFrame(long_rows).to_csv(long_csv, index=False)
print(f"\n[WRITE] {long_csv} rows={len(long_rows)}")
if truth is not None:
summary_csv = outdir / "prediction_summary.csv"
df_sum = pd.DataFrame(summary_rows)
df_sum.to_csv(summary_csv, index=False)
print(f"[WRITE] {summary_csv} rows={len(df_sum)}")
# Top models report
if not df_sum.empty and "overall_pr_auc" in df_sum.columns:
df_rank = df_sum.sort_values(["overall_pr_auc", "overall_roc_auc"], ascending=False, na_position="last")
report_path = outdir / "top_models_by_pr_auc.txt"
cols = [
"model",
"n_annotated",
"overall_positives",
"overall_precision",
"overall_recall",
"overall_f1",
"overall_pr_auc",
"overall_roc_auc",
"isolate_fn", "isolate_fp", "mag_fn", "mag_fp",
"elapsed_min",
]
cols = [c for c in cols if c in df_rank.columns]
with open(report_path, "w", encoding="utf-8") as f:
f.write("Top models by overall PR-AUC (ALT = Genetic_code_ID != 11)\n")
f.write(df_rank[cols].head(25).to_string(index=False))
f.write("\n")
print(f"[WRITE] {report_path}")
print("[DONE]")
if __name__ == "__main__":
main()
|