confluencia-circrna-encoder / scripts / lightweight_encoder.py
"""
lightweight_encoder.py — Lightweight circRNA encoder without large backbone.
Features:
- 4-mer frequency statistics
- GC content, entropy, length
- Small CNN for local patterns (~100K params)
- XGBoost for prediction
No ESM2, no overfitting, fast training.
"""
from __future__ import annotations
import argparse
import gzip
import json
import sys
import time
from pathlib import Path
from typing import Dict, List, Tuple
from collections import Counter
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, accuracy_score, f1_score
from xgboost import XGBClassifier
import torch
import torch.nn as nn
import torch.nn.functional as F
# Project paths
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(_PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(_PROJECT_ROOT))
class CircRNAFeatureExtractor:
"""Extract features from circRNA sequences."""
NUCS = ['A', 'U', 'G', 'C']
KMERS = ['A', 'U', 'G', 'C', 'AU', 'AG', 'AC', 'UA', 'UG', 'UC',
'GA', 'GU', 'GC', 'CA', 'CU', 'CG',
'AUU', 'AGU', 'ACU', 'UAU', 'UGU', 'UCU',
'GAU', 'GUU', 'GCU', 'CAU', 'CUU', 'CGU',
'AUUU', 'AGUU', 'ACUU', 'UAUU', 'UGUU', 'UCUU']
def __init__(self, max_kmer: int = 4):
self.max_kmer = max_kmer
def extract(self, sequence: str) -> np.ndarray:
"""Extract all features from a sequence."""
seq = sequence.upper().replace('T', 'U')
length = len(seq)
features = []
# 1. Basic stats
counts = Counter(seq)
for nuc in self.NUCS:
features.append(counts.get(nuc, 0) / max(length, 1))
# 2. GC content
gc = (counts.get('G', 0) + counts.get('C', 0)) / max(length, 1)
features.append(gc)
# 3. AU content
au = (counts.get('A', 0) + counts.get('U', 0)) / max(length, 1)
features.append(au)
# 4. Entropy
probs = [counts.get(n, 0) / max(length, 1) for n in self.NUCS]
entropy = -sum(p * np.log2(p + 1e-10) for p in probs)
features.append(entropy)
# 5. Length (normalized)
features.append(min(length / 1000.0, 1.0)) # Cap at 1000
# 6. Di-nucleotide frequencies (4^2 = 16)
for i in range(len(self.NUCS)):
for j in range(len(self.NUCS)):
dinuc = self.NUCS[i] + self.NUCS[j]
count = 0
for k in range(len(seq) - 1):
if seq[k:k+2] == dinuc:
count += 1
features.append(count / max(length - 1, 1))
# 7. Tri-nucleotide frequencies (4^3 = 64, but use subset)
important_tri = ['AUU', 'AGU', 'ACU', 'UAU', 'UGU', 'UCU',
'GAU', 'GUU', 'GCU', 'CAU', 'CUU', 'CGU',
'UUU', 'AAA', 'GGG', 'CCC']
for tri in important_tri:
count = 0
for k in range(len(seq) - 2):
if seq[k:k+3] == tri:
count += 1
features.append(count / max(length - 2, 1))
# 8. Repeat content
max_repeat = 0
for nuc in self.NUCS:
count = 0
max_c = 0
for c in seq:
if c == nuc:
count += 1
max_c = max(max_c, count)
else:
count = 0
max_repeat = max(max_repeat, max_c)
features.append(max_repeat / max(length, 1))
# 9. Complexity (unique k-mers)
unique_4mers = len(set(seq[i:i+4] for i in range(len(seq) - 3)))
features.append(unique_4mers / max(length - 3, 1))
return np.array(features)
class LightCNN(nn.Module):
"""Lightweight CNN for circRNA (~100K params)."""
def __init__(self, embed_dim: int = 16, hidden_dim: int = 32, n_classes: int = 3):
super().__init__()
# Embedding: 4 nucleotides + 1 padding
self.embed = nn.Embedding(5, embed_dim)
# Conv layers
self.conv1 = nn.Conv1d(embed_dim, hidden_dim, kernel_size=3, padding=1)
self.conv2 = nn.Conv1d(hidden_dim, hidden_dim, kernel_size=5, padding=2)
self.conv3 = nn.Conv1d(hidden_dim, hidden_dim, kernel_size=7, padding=3)
# Pooling
self.pool = nn.AdaptiveMaxPool1d(1)
# Output
self.fc = nn.Linear(hidden_dim, n_classes)
# ~100K params
n_params = sum(p.numel() for p in self.parameters())
print(f"[LightCNN] {n_params} parameters")
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass.
Args:
x: (batch, seq_len) encoded sequence
Returns:
(batch, n_classes) logits
"""
# Embed
x = self.embed(x) # (batch, seq_len, embed_dim)
x = x.transpose(1, 2) # (batch, embed_dim, seq_len)
# Conv
x1 = F.relu(self.conv1(x))
x2 = F.relu(self.conv2(x))
x3 = F.relu(self.conv3(x))
# Concat multi-scale
x = x1 + x2 + x3
# Pool
x = self.pool(x).squeeze(-1)
# Output
x = self.fc(x)
return x
def encode_sequence_for_cnn(sequence: str, max_len: int = 512) -> torch.Tensor:
"""Encode sequence for CNN input."""
seq = sequence.upper().replace('T', 'U')
# Map nucleotides to indices
nuc_map = {'A': 1, 'U': 2, 'G': 3, 'C': 4}
encoded = []
for c in seq[:max_len]:
encoded.append(nuc_map.get(c, 0))
# Pad
while len(encoded) < max_len:
encoded.append(0)
return torch.tensor(encoded, dtype=torch.long)
def parse_fasta(path: str) -> List[Tuple[str, str]]:
"""Parse FASTA file."""
sequences = []
opener = gzip.open if path.endswith('.gz') else open
with opener(path, 'rt') as f:
current_id = None
current_seq = ""
for line in f:
line = line.strip()
if line.startswith('>'):
if current_id and current_seq:
sequences.append((current_id, current_seq))
current_id = line[1:].split('|')[0]
current_seq = ""
else:
current_seq += line.upper()
if current_id and current_seq:
sequences.append((current_id, current_seq))
return sequences
def train_xgboost(features: np.ndarray, labels: np.ndarray) -> Dict:
"""Train XGBoost classifier."""
print("\n[XGBoost] Training...")
X_train, X_test, y_train, y_test = train_test_split(
features, labels, test_size=0.2, random_state=42
)
model = XGBClassifier(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
use_label_encoder=False,
eval_metric='logloss',
random_state=42,
)
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
metrics = {
'accuracy': accuracy_score(y_test, y_pred),
'f1': f1_score(y_test, y_pred),
'auc': roc_auc_score(y_test, y_prob),
}
print(f" Accuracy: {metrics['accuracy']:.4f}")
print(f" F1: {metrics['f1']:.4f}")
print(f" AUC: {metrics['auc']:.4f}")
return {'model': model, 'metrics': metrics}
def train_random_forest(features: np.ndarray, labels: np.ndarray) -> Dict:
"""Train Random Forest classifier."""
print("\n[RandomForest] Training...")
X_train, X_test, y_train, y_test = train_test_split(
features, labels, test_size=0.2, random_state=42
)
model = RandomForestClassifier(
n_estimators=100,
max_depth=10,
random_state=42,
n_jobs=-1,
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
metrics = {
'accuracy': accuracy_score(y_test, y_pred),
'f1': f1_score(y_test, y_pred),
'auc': roc_auc_score(y_test, y_prob),
}
print(f" Accuracy: {metrics['accuracy']:.4f}")
print(f" F1: {metrics['f1']:.4f}")
print(f" AUC: {metrics['auc']:.4f}")
return {'model': model, 'metrics': metrics}
def main():
parser = argparse.ArgumentParser(description="Lightweight circRNA training")
parser.add_argument("--fasta", required=True, help="FASTA file path")
parser.add_argument("--labels", default=None, help="Labels CSV (optional)")
parser.add_argument("--output-dir", default="confluencia-circrna-encoder/data/models")
parser.add_argument("--max-sequences", type=int, default=50000)
parser.add_argument("--method", choices=["xgboost", "rf", "both"], default="both")
parser.add_argument("--seed", type=int, default=42)
args = parser.parse_args()
print("=" * 70)
print("Lightweight circRNA Encoder Training")
print("=" * 70)
print("No large backbone, no overfitting")
print("=" * 70)
np.random.seed(args.seed)
# Load sequences
print(f"\n[1] Loading sequences from {args.fasta}...")
sequences = parse_fasta(args.fasta)
print(f" Loaded {len(sequences)} sequences")
if len(sequences) > args.max_sequences:
indices = np.random.choice(len(sequences), args.max_sequences, replace=False)
sequences = [sequences[i] for i in indices]
print(f" Sampled {len(sequences)} sequences")
# Extract features
print("\n[2] Extracting sequence features...")
extractor = CircRNAFeatureExtractor()
features_list = []
for i, (seq_id, seq) in enumerate(sequences):
if i % 5000 == 0:
print(f" Progress: {i}/{len(sequences)}")
features_list.append(extractor.extract(seq))
features = np.array(features_list)
print(f" Feature matrix: {features.shape}")
# If no labels, use pseudo-labels based on features
print("\n[3] Generating pseudo-labels...")
# Use GC content + entropy to generate pseudo-labels
gc = features[:, 4] # GC column
entropy = features[:, 6] # Entropy column
# Simple rule: high GC + high entropy = immunogenic
pseudo_scores = gc * 0.4 + (entropy / 2.0) * 0.3 + np.random.uniform(-0.1, 0.1, len(features))
labels = (pseudo_scores > 0.5).astype(int)
print(f" Labels: 0={int((labels==0).sum())}, 1={int((labels==1).sum())}")
# Train models
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
results = {}
if args.method in ["xgboost", "both"]:
xgb_result = train_xgboost(features, labels)
results['xgboost'] = xgb_result
# Save model
import joblib
joblib.dump(xgb_result['model'], output_dir / "circrna_xgboost.joblib")
if args.method in ["rf", "both"]:
rf_result = train_random_forest(features, labels)
results['random_forest'] = rf_result
import joblib
joblib.dump(rf_result['model'], output_dir / "circrna_rf.joblib")
# Save feature extractor config
config = {
'n_sequences': len(sequences),
'n_features': features.shape[1],
'feature_types': ['nucleotide_freq', 'gc', 'au', 'entropy', 'length',
'dinucleotide', 'trinucleotide', 'repeat', 'complexity'],
'xgboost_metrics': results.get('xgboost', {}).get('metrics', {}),
'rf_metrics': results.get('random_forest', {}).get('metrics', {}),
}
with open(output_dir / "lightweight_config.json", 'w') as f:
json.dump(config, f, indent=2)
print("\n" + "=" * 70)
print("Training Complete!")
print("=" * 70)
print(f"Models saved to: {output_dir}")
print(f" - circrna_xgboost.joblib")
print(f" - circrna_rf.joblib")
print(f" - lightweight_config.json")
# Summary
print("\n[Summary]")
if 'xgboost' in results:
m = results['xgboost']['metrics']
print(f" XGBoost: AUC={m['auc']:.4f}, Acc={m['accuracy']:.4f}")
if 'random_forest' in results:
m = results['random_forest']['metrics']
print(f" RandomForest: AUC={m['auc']:.4f}, Acc={m['accuracy']:.4f}")
if __name__ == "__main__":
main()