Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,299 @@
|
|
| 1 |
"""
|
| 2 |
-
N2N Precision Engine β Production API
|
| 3 |
Inventor: Manav Vanga | Patent Pending 2026
|
| 4 |
-
Brain: DNABERT-2 trained
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
-
import os, re, hashlib,
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
import numpy as np
|
|
|
|
| 10 |
from flask import Flask, request, jsonify
|
| 11 |
from flask_cors import CORS
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
CORS(app)
|
| 15 |
|
| 16 |
-
# ββ
|
| 17 |
SLIP_SCORES = {'C':0.82,'A':0.61,'T':0.34,'U':0.34,'G':0.19,'N':0.50}
|
| 18 |
POSITION_WEIGHTS = [
|
| 19 |
0.20,0.22,0.24,0.26,0.28,0.32,0.36,0.42,0.50,0.58,
|
| 20 |
0.65,0.72,0.80,0.88,0.95,1.00,1.00,1.00,1.80,
|
| 21 |
1.40,1.20,1.00,0.85,0.72,0.60,0.50,0.42,0.36,0.28
|
| 22 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
PLUS4_ROAD = {
|
| 24 |
-
'C':('Slippery','High readthrough
|
| 25 |
-
'A':('Smooth', 'Moderate readthrough'),
|
| 26 |
-
'T':('Rough', 'Low readthrough'),
|
| 27 |
-
'U':('Rough', 'Low readthrough'),
|
| 28 |
-
'G':('Sticky', 'Very low readthrough'),
|
| 29 |
}
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def encode_window(window):
|
| 43 |
import math
|
| 44 |
from collections import Counter
|
|
@@ -62,136 +317,114 @@ def encode_window(window):
|
|
| 62 |
[rfc,hex_mean,up_mean,gc,0.5,entropy(w[18:]),entropy(w[:15])],
|
| 63 |
dtype=np.float32)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def get_tier(score):
|
| 71 |
-
if score >= 55: return 'HIGH'
|
| 72 |
-
if score >= 30: return 'MEDIUM'
|
| 73 |
-
return 'LOW'
|
| 74 |
-
|
| 75 |
-
# ββ Load RFC model ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
-
rfc_model = None
|
| 77 |
-
BRAIN_TYPE = "RFC-Rule"
|
| 78 |
|
| 79 |
try:
|
| 80 |
import joblib
|
| 81 |
-
rfc_model
|
| 82 |
BRAIN_TYPE = "RFC-ML"
|
| 83 |
print("RFC-ML brain loaded")
|
| 84 |
except Exception as e:
|
| 85 |
-
print("RFC-ML not found
|
| 86 |
-
|
| 87 |
-
# ββ Try loading DNABERT-2 in background βββββββββββββββββββββββββββ
|
| 88 |
-
dnabert_model = None
|
| 89 |
-
dnabert_tokenizer = None
|
| 90 |
|
| 91 |
def load_dnabert():
|
| 92 |
-
global dnabert_model,
|
| 93 |
try:
|
| 94 |
import torch
|
| 95 |
import torch.nn as nn
|
| 96 |
from transformers import AutoTokenizer, BertModel, BertConfig
|
| 97 |
from huggingface_hub import snapshot_download
|
| 98 |
|
| 99 |
-
print("Loading DNABERT-2 brain
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
dnabert = BertModel.from_pretrained(
|
| 105 |
-
model_path, config=config, ignore_mismatched_sizes=True)
|
| 106 |
|
| 107 |
class RPScoreHead(nn.Module):
|
| 108 |
-
def __init__(self,
|
| 109 |
super().__init__()
|
| 110 |
self.net = nn.Sequential(
|
| 111 |
-
nn.Linear(
|
| 112 |
-
nn.Linear(512,256),
|
| 113 |
-
nn.Linear(256,128),
|
| 114 |
-
nn.Linear(128,32),
|
| 115 |
-
nn.Linear(32,1),
|
| 116 |
)
|
| 117 |
-
def forward(self, x):
|
| 118 |
-
return self.net(x).squeeze(-1) * 100.0
|
| 119 |
|
| 120 |
class N2NModel(nn.Module):
|
| 121 |
-
def __init__(self,
|
| 122 |
super().__init__()
|
| 123 |
-
self.encoder =
|
| 124 |
-
self.head = RPScoreHead(
|
| 125 |
-
def forward(self,
|
| 126 |
-
out = self.encoder(input_ids=
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
m.load_state_dict(
|
| 135 |
m.eval()
|
| 136 |
dnabert_model = m
|
|
|
|
| 137 |
BRAIN_TYPE = "DNABERT-2"
|
| 138 |
-
print("DNABERT-2
|
| 139 |
else:
|
| 140 |
-
print("
|
| 141 |
except Exception as e:
|
| 142 |
print("DNABERT-2 failed: " + str(e))
|
| 143 |
|
| 144 |
-
# Load in background thread so API starts immediately
|
| 145 |
-
import threading
|
| 146 |
threading.Thread(target=load_dnabert, daemon=True).start()
|
| 147 |
|
| 148 |
-
# ββ Prediction ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 149 |
def predict(window):
|
| 150 |
-
|
| 151 |
-
if dnabert_model is not None and dnabert_tokenizer is not None:
|
| 152 |
try:
|
| 153 |
import torch
|
| 154 |
-
enc
|
| 155 |
-
|
| 156 |
-
truncation=True)
|
| 157 |
with torch.no_grad():
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
return round(score, 2), "DNABERT-2"
|
| 161 |
except:
|
| 162 |
pass
|
| 163 |
-
|
| 164 |
-
# Try RFC-ML
|
| 165 |
if rfc_model is not None:
|
| 166 |
try:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
return round(max(0,min(100,score)), 2), "RFC-ML"
|
| 170 |
except:
|
| 171 |
pass
|
| 172 |
-
|
| 173 |
-
# Rule-based fallback
|
| 174 |
-
return compute_rp_score_rfc(window), "RFC-Rule"
|
| 175 |
|
| 176 |
# ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 177 |
@app.route('/', methods=['GET'])
|
| 178 |
def home():
|
| 179 |
return jsonify({
|
| 180 |
'name': 'N2N Precision Engine',
|
| 181 |
-
'version': '
|
| 182 |
'brain': BRAIN_TYPE,
|
| 183 |
'inventor': 'Manav Vanga',
|
| 184 |
'patent': 'Pending 2026',
|
| 185 |
-
'description': 'Predicts readthrough for all nonsense mutation diseases',
|
| 186 |
-
'
|
|
|
|
| 187 |
})
|
| 188 |
|
| 189 |
@app.route('/api/health', methods=['GET'])
|
| 190 |
def health():
|
| 191 |
return jsonify({
|
| 192 |
-
'status':
|
| 193 |
-
'brain':
|
| 194 |
-
'version':
|
|
|
|
|
|
|
| 195 |
})
|
| 196 |
|
| 197 |
@app.route('/api/score', methods=['GET','POST'])
|
|
@@ -200,65 +433,101 @@ def score():
|
|
| 200 |
data = request.get_json() or {}
|
| 201 |
window = data.get('window','')
|
| 202 |
gene = data.get('gene','UNKNOWN')
|
|
|
|
| 203 |
else:
|
| 204 |
window = request.args.get('window','')
|
| 205 |
gene = request.args.get('gene','UNKNOWN')
|
|
|
|
| 206 |
|
| 207 |
if not window or len(window) < 20:
|
| 208 |
-
return jsonify({'error':'window required (min 20bp)'}), 400
|
| 209 |
|
| 210 |
window = window.upper().replace('U','T')
|
| 211 |
-
|
| 212 |
-
tier = get_tier(
|
| 213 |
w = (window+'N'*30)[:30]
|
| 214 |
p4 = w[18] if len(w)>18 else 'N'
|
| 215 |
-
road, road_desc = PLUS4_ROAD.get(p4,('Unknown','Unknown'))
|
| 216 |
-
drugs =
|
| 217 |
audit = hashlib.sha256(
|
| 218 |
-
(window+str(
|
| 219 |
).encode()).hexdigest()[:16]
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
return jsonify({
|
| 222 |
-
'gene':
|
| 223 |
-
'window':
|
| 224 |
-
'rp_score':
|
| 225 |
-
'tier':
|
| 226 |
-
'plus4_base':
|
| 227 |
-
'plus4_road':
|
| 228 |
-
'plus4_road_desc':
|
| 229 |
-
'therapy':
|
| 230 |
-
'
|
| 231 |
-
'
|
| 232 |
-
'
|
| 233 |
-
'
|
| 234 |
-
'
|
| 235 |
-
'
|
| 236 |
-
'
|
| 237 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
})
|
| 239 |
|
| 240 |
@app.route('/api/demo', methods=['GET'])
|
| 241 |
def demo():
|
| 242 |
demos = [
|
| 243 |
-
('CFTR','Y122X', 'AAGAAATCGATCAGTTAACAGCTTGCAGCN','18.5%
|
| 244 |
-
('CFTR','G542X', 'AAGAAATCGATCAGTTGAGAGCTTGCAGCN','0.3%
|
| 245 |
-
('CFTR','W1282X','AAGAAATCGATCAGTTGACAGCTTGCAGCN','8.2%
|
| 246 |
-
('DMD', 'Q1922X','
|
| 247 |
-
('TP53','R213X', '
|
| 248 |
]
|
| 249 |
results = []
|
| 250 |
for gene, variant, window, expected in demos:
|
| 251 |
-
|
| 252 |
results.append({
|
| 253 |
'gene': gene,
|
| 254 |
'variant': variant,
|
| 255 |
-
'rp_score':
|
| 256 |
-
'tier': get_tier(
|
| 257 |
'expected': expected,
|
| 258 |
'brain': brain,
|
| 259 |
})
|
| 260 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
if __name__ == '__main__':
|
| 263 |
-
port = int(os.environ.get('PORT',7860))
|
| 264 |
app.run(host='0.0.0.0', port=port)
|
|
|
|
| 1 |
"""
|
| 2 |
+
N2N Precision Engine β Production API v3.0
|
| 3 |
Inventor: Manav Vanga | Patent Pending 2026
|
| 4 |
+
Brain: DNABERT-2 v2 (Pearson r=0.941, trained on 30,387 biological variants)
|
| 5 |
+
Calibrated thresholds: HIGH=0.88, MED=0.76
|
| 6 |
+
Includes: Full drug database + ClinicalTrials.gov live integration
|
| 7 |
"""
|
| 8 |
|
| 9 |
+
import os, re, hashlib, threading
|
| 10 |
from datetime import datetime, timezone
|
| 11 |
import numpy as np
|
| 12 |
+
import requests
|
| 13 |
from flask import Flask, request, jsonify
|
| 14 |
from flask_cors import CORS
|
| 15 |
|
| 16 |
app = Flask(__name__)
|
| 17 |
CORS(app)
|
| 18 |
|
| 19 |
+
# ββ Inventor constants ββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
SLIP_SCORES = {'C':0.82,'A':0.61,'T':0.34,'U':0.34,'G':0.19,'N':0.50}
|
| 21 |
POSITION_WEIGHTS = [
|
| 22 |
0.20,0.22,0.24,0.26,0.28,0.32,0.36,0.42,0.50,0.58,
|
| 23 |
0.65,0.72,0.80,0.88,0.95,1.00,1.00,1.00,1.80,
|
| 24 |
1.40,1.20,1.00,0.85,0.72,0.60,0.50,0.42,0.36,0.28
|
| 25 |
]
|
| 26 |
+
|
| 27 |
+
# ββ Calibrated thresholds (from validation on 10 known variants) ββ
|
| 28 |
+
HIGH_THRESHOLD = 0.88
|
| 29 |
+
MED_THRESHOLD = 0.76
|
| 30 |
+
|
| 31 |
PLUS4_ROAD = {
|
| 32 |
+
'C':('Slippery','High readthrough β ribosome slides through stop codon'),
|
| 33 |
+
'A':('Smooth', 'Moderate readthrough β some ribosomal slippage'),
|
| 34 |
+
'T':('Rough', 'Low readthrough β ribosome mostly terminates'),
|
| 35 |
+
'U':('Rough', 'Low readthrough β ribosome mostly terminates'),
|
| 36 |
+
'G':('Sticky', 'Very low readthrough β ribosome terminates strongly'),
|
| 37 |
}
|
| 38 |
+
|
| 39 |
+
# ββ Complete drug database ββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
+
DRUG_DATABASE = {
|
| 41 |
+
'HIGH': {
|
| 42 |
+
'therapy': 'Readthrough Therapy β Strong Candidate',
|
| 43 |
+
'mechanism': 'Promote ribosomal readthrough of premature stop codon',
|
| 44 |
+
'approved': [
|
| 45 |
+
{
|
| 46 |
+
'name': 'Ataluren (PTC124)',
|
| 47 |
+
'status': 'EMA Approved (EU) β FDA Breakthrough Therapy',
|
| 48 |
+
'diseases': ['Duchenne MD', 'Cystic Fibrosis'],
|
| 49 |
+
'dose': '10/10/20 mg/kg three times daily',
|
| 50 |
+
'note': 'First-in-class readthrough drug'
|
| 51 |
+
},
|
| 52 |
+
],
|
| 53 |
+
'phase3': [
|
| 54 |
+
{
|
| 55 |
+
'name': 'ELX-02 (Eloxx)',
|
| 56 |
+
'status': 'Phase 3 Clinical Trial',
|
| 57 |
+
'diseases': ['Cystic Fibrosis', 'Dravet Syndrome'],
|
| 58 |
+
'mechanism': 'Eukaryotic ribosome-targeting aminoglycoside',
|
| 59 |
+
'note': 'More selective than gentamicin, less nephrotoxic'
|
| 60 |
+
},
|
| 61 |
+
],
|
| 62 |
+
'phase2': [
|
| 63 |
+
{
|
| 64 |
+
'name': 'SRI-37240 + SRI-41315',
|
| 65 |
+
'status': 'Phase 2',
|
| 66 |
+
'diseases': ['Cystic Fibrosis'],
|
| 67 |
+
'mechanism': 'Novel readthrough compound class',
|
| 68 |
+
'note': 'University of Alabama Birmingham'
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
'name': 'Gentamicin (G418)',
|
| 72 |
+
'status': 'Phase 2 / Off-label',
|
| 73 |
+
'diseases': ['Multiple β aminoglycoside readthrough'],
|
| 74 |
+
'mechanism': 'Aminoglycoside-induced misreading of stop codon',
|
| 75 |
+
'note': 'Nephrotoxicity limits long-term use'
|
| 76 |
+
},
|
| 77 |
+
],
|
| 78 |
+
'preclinical': [
|
| 79 |
+
'Negamycin derivatives',
|
| 80 |
+
'NV848 (Nonsense Therapeutics)',
|
| 81 |
+
'Escin β natural readthrough compound',
|
| 82 |
+
'Tylosin β macrolide with readthrough activity',
|
| 83 |
+
],
|
| 84 |
+
'combination': [
|
| 85 |
+
'Ataluren + NMD inhibitor (amlexanox)',
|
| 86 |
+
'ELX-02 + CFTR corrector (lumacaftor)',
|
| 87 |
+
'Readthrough + proteasome inhibitor',
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
'MEDIUM': {
|
| 91 |
+
'therapy': 'Combination Approach β Moderate Candidate',
|
| 92 |
+
'mechanism': 'Combine readthrough with NMD suppression',
|
| 93 |
+
'approved': [
|
| 94 |
+
{
|
| 95 |
+
'name': 'Gentamicin',
|
| 96 |
+
'status': 'Off-label / Investigational',
|
| 97 |
+
'diseases': ['Multiple'],
|
| 98 |
+
'note': 'Short-term use, monitor kidneys'
|
| 99 |
+
}
|
| 100 |
+
],
|
| 101 |
+
'phase3': [
|
| 102 |
+
{
|
| 103 |
+
'name': 'ELX-02',
|
| 104 |
+
'status': 'Phase 3 β may benefit moderate responders',
|
| 105 |
+
'diseases': ['CF', 'Dravet'],
|
| 106 |
+
'note': 'Trial enrollment open'
|
| 107 |
+
}
|
| 108 |
+
],
|
| 109 |
+
'phase2': [
|
| 110 |
+
{
|
| 111 |
+
'name': 'Amlexanox + Readthrough',
|
| 112 |
+
'status': 'Phase 2 combination',
|
| 113 |
+
'diseases': ['Multiple NMD diseases'],
|
| 114 |
+
'mechanism': 'NMD inhibition prolongs readthrough mRNA',
|
| 115 |
+
'note': 'Increases mRNA half-life for readthrough product'
|
| 116 |
+
}
|
| 117 |
+
],
|
| 118 |
+
'preclinical': [
|
| 119 |
+
'SMG1 kinase inhibitors',
|
| 120 |
+
'NMDI-14',
|
| 121 |
+
'UPF1 inhibitors',
|
| 122 |
+
],
|
| 123 |
+
'combination': [
|
| 124 |
+
'Readthrough + NMD inhibitor',
|
| 125 |
+
'Low-dose gentamicin + antioxidant',
|
| 126 |
+
]
|
| 127 |
+
},
|
| 128 |
+
'LOW': {
|
| 129 |
+
'therapy': 'Alternative Strategy β Poor Readthrough Candidate',
|
| 130 |
+
'mechanism': 'Bypass or compensate for the nonsense mutation',
|
| 131 |
+
'approved': [
|
| 132 |
+
{
|
| 133 |
+
'name': 'Eteplirsen (Exondys 51)',
|
| 134 |
+
'status': 'FDA Approved',
|
| 135 |
+
'diseases': ['Duchenne MD β exon 51 skipping'],
|
| 136 |
+
'note': 'Exon skipping β bypasses mutation entirely'
|
| 137 |
+
},
|
| 138 |
+
{
|
| 139 |
+
'name': 'Nusinersen (Spinraza)',
|
| 140 |
+
'status': 'FDA Approved',
|
| 141 |
+
'diseases': ['Spinal Muscular Atrophy'],
|
| 142 |
+
'note': 'Antisense oligonucleotide β splicing modulation'
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
'name': 'Onasemnogene (Zolgensma)',
|
| 146 |
+
'status': 'FDA Approved',
|
| 147 |
+
'diseases': ['SMA type 1'],
|
| 148 |
+
'note': 'Gene replacement therapy'
|
| 149 |
+
},
|
| 150 |
+
],
|
| 151 |
+
'phase3': [
|
| 152 |
+
{
|
| 153 |
+
'name': 'Casimersen (Amondys 45)',
|
| 154 |
+
'status': 'FDA Approved β exon 45 skipping',
|
| 155 |
+
'diseases': ['Duchenne MD'],
|
| 156 |
+
'note': 'Exon skipping strategy'
|
| 157 |
+
}
|
| 158 |
+
],
|
| 159 |
+
'phase2': [
|
| 160 |
+
{
|
| 161 |
+
'name': 'Gene therapy vectors',
|
| 162 |
+
'status': 'Multiple Phase 1/2 trials',
|
| 163 |
+
'diseases': ['Disease-specific'],
|
| 164 |
+
'note': 'AAV-delivered corrected gene copy'
|
| 165 |
+
}
|
| 166 |
+
],
|
| 167 |
+
'preclinical': [
|
| 168 |
+
'Base editing (adenine base editor)',
|
| 169 |
+
'Prime editing',
|
| 170 |
+
'CRISPR-Cas9 correction',
|
| 171 |
+
'Codon suppressor tRNA therapy',
|
| 172 |
+
],
|
| 173 |
+
'combination': [
|
| 174 |
+
'Exon skipping + supportive care',
|
| 175 |
+
'Gene therapy + enzyme replacement',
|
| 176 |
+
]
|
| 177 |
+
}
|
| 178 |
}
|
| 179 |
|
| 180 |
+
# ββ ClinicalTrials.gov integration ββββββββββββββββββββββββββββββββ
|
| 181 |
+
READTHROUGH_DRUGS = [
|
| 182 |
+
'ataluren','ptc124','elx-02','gentamicin','eloxx',
|
| 183 |
+
'readthrough','nonsense mutation','premature stop codon'
|
| 184 |
+
]
|
| 185 |
+
|
| 186 |
+
def fetch_clinical_trials(gene=None, condition=None, max_trials=5):
|
| 187 |
+
"""
|
| 188 |
+
Fetch live clinical trials from ClinicalTrials.gov API v2
|
| 189 |
+
Free, no API key needed.
|
| 190 |
+
"""
|
| 191 |
+
try:
|
| 192 |
+
# Build search query
|
| 193 |
+
terms = []
|
| 194 |
+
if gene:
|
| 195 |
+
terms.append(gene)
|
| 196 |
+
terms.append('nonsense mutation readthrough')
|
| 197 |
+
|
| 198 |
+
query = ' '.join(terms)
|
| 199 |
+
|
| 200 |
+
url = "https://clinicaltrials.gov/api/v2/studies"
|
| 201 |
+
params = {
|
| 202 |
+
'query.term': query,
|
| 203 |
+
'filter.overallStatus': 'RECRUITING,ACTIVE_NOT_RECRUITING,ENROLLING_BY_INVITATION',
|
| 204 |
+
'pageSize': max_trials,
|
| 205 |
+
'format': 'json',
|
| 206 |
+
'fields': 'NCTId,BriefTitle,Phase,OverallStatus,Condition,InterventionName,LocationCity,LocationCountry,StartDate,PrimaryCompletionDate'
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
resp = requests.get(url, params=params, timeout=10)
|
| 210 |
+
if resp.status_code != 200:
|
| 211 |
+
return []
|
| 212 |
+
|
| 213 |
+
data = resp.json()
|
| 214 |
+
studies = data.get('studies', [])
|
| 215 |
+
trials = []
|
| 216 |
+
|
| 217 |
+
for s in studies:
|
| 218 |
+
proto = s.get('protocolSection', {})
|
| 219 |
+
ident = proto.get('identificationModule', {})
|
| 220 |
+
status = proto.get('statusModule', {})
|
| 221 |
+
desc = proto.get('conditionsModule', {})
|
| 222 |
+
interv = proto.get('armsInterventionsModule', {})
|
| 223 |
+
locs = proto.get('contactsLocationsModule', {})
|
| 224 |
+
|
| 225 |
+
interventions = []
|
| 226 |
+
for arm in interv.get('interventions', []):
|
| 227 |
+
interventions.append(arm.get('name',''))
|
| 228 |
+
|
| 229 |
+
conditions = desc.get('conditions', [])
|
| 230 |
+
|
| 231 |
+
locations = []
|
| 232 |
+
for loc in locs.get('locations', [])[:3]:
|
| 233 |
+
city = loc.get('city','')
|
| 234 |
+
country = loc.get('country','')
|
| 235 |
+
if city or country:
|
| 236 |
+
locations.append(city + ', ' + country)
|
| 237 |
+
|
| 238 |
+
trials.append({
|
| 239 |
+
'nct_id': ident.get('nctId',''),
|
| 240 |
+
'title': ident.get('briefTitle',''),
|
| 241 |
+
'phase': status.get('phase','N/A'),
|
| 242 |
+
'status': status.get('overallStatus',''),
|
| 243 |
+
'conditions': conditions[:3],
|
| 244 |
+
'interventions': interventions[:3],
|
| 245 |
+
'locations': locations[:3],
|
| 246 |
+
'url': 'https://clinicaltrials.gov/study/' + ident.get('nctId',''),
|
| 247 |
+
})
|
| 248 |
+
|
| 249 |
+
return trials
|
| 250 |
+
|
| 251 |
+
except Exception as e:
|
| 252 |
+
return []
|
| 253 |
+
|
| 254 |
+
def fetch_drug_trials(drug_name, max_trials=3):
|
| 255 |
+
"""Fetch trials for a specific drug."""
|
| 256 |
+
try:
|
| 257 |
+
url = "https://clinicaltrials.gov/api/v2/studies"
|
| 258 |
+
params = {
|
| 259 |
+
'query.term': drug_name + ' nonsense mutation',
|
| 260 |
+
'filter.overallStatus': 'RECRUITING,ACTIVE_NOT_RECRUITING',
|
| 261 |
+
'pageSize': max_trials,
|
| 262 |
+
'format': 'json',
|
| 263 |
+
'fields': 'NCTId,BriefTitle,Phase,OverallStatus,LocationCountry'
|
| 264 |
+
}
|
| 265 |
+
resp = requests.get(url, params=params, timeout=8)
|
| 266 |
+
if resp.status_code != 200:
|
| 267 |
+
return []
|
| 268 |
+
|
| 269 |
+
studies = resp.json().get('studies', [])
|
| 270 |
+
results = []
|
| 271 |
+
for s in studies:
|
| 272 |
+
proto = s.get('protocolSection', {})
|
| 273 |
+
ident = proto.get('identificationModule', {})
|
| 274 |
+
status = proto.get('statusModule', {})
|
| 275 |
+
results.append({
|
| 276 |
+
'nct_id': ident.get('nctId',''),
|
| 277 |
+
'title': ident.get('briefTitle','')[:80],
|
| 278 |
+
'phase': status.get('phase',''),
|
| 279 |
+
'status': status.get('overallStatus',''),
|
| 280 |
+
'url': 'https://clinicaltrials.gov/study/' + ident.get('nctId',''),
|
| 281 |
+
})
|
| 282 |
+
return results
|
| 283 |
+
except:
|
| 284 |
+
return []
|
| 285 |
+
|
| 286 |
+
# ββ Helper functions ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 287 |
+
def compute_rp_score_rfc(window):
|
| 288 |
+
w = (window.upper().replace('T','U')+'N'*30)[:30]
|
| 289 |
+
rfc = sum(SLIP_SCORES.get(b,0.5)*wt for b,wt in zip(w,POSITION_WEIGHTS))
|
| 290 |
+
return round(max(0.0, min(100.0, rfc/sum(POSITION_WEIGHTS)*100)), 2)
|
| 291 |
+
|
| 292 |
+
def get_tier(score):
|
| 293 |
+
if score >= HIGH_THRESHOLD: return 'HIGH'
|
| 294 |
+
if score >= MED_THRESHOLD: return 'MEDIUM'
|
| 295 |
+
return 'LOW'
|
| 296 |
+
|
| 297 |
def encode_window(window):
|
| 298 |
import math
|
| 299 |
from collections import Counter
|
|
|
|
| 317 |
[rfc,hex_mean,up_mean,gc,0.5,entropy(w[18:]),entropy(w[:15])],
|
| 318 |
dtype=np.float32)
|
| 319 |
|
| 320 |
+
# ββ Load brains βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 321 |
+
BRAIN_TYPE = "RFC-Rule"
|
| 322 |
+
rfc_model = None
|
| 323 |
+
dnabert_model = None
|
| 324 |
+
dnabert_tok = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
try:
|
| 327 |
import joblib
|
| 328 |
+
rfc_model = joblib.load("models/rfc_head_weights.pkl")
|
| 329 |
BRAIN_TYPE = "RFC-ML"
|
| 330 |
print("RFC-ML brain loaded")
|
| 331 |
except Exception as e:
|
| 332 |
+
print("RFC-ML not found: " + str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
def load_dnabert():
|
| 335 |
+
global dnabert_model, dnabert_tok, BRAIN_TYPE
|
| 336 |
try:
|
| 337 |
import torch
|
| 338 |
import torch.nn as nn
|
| 339 |
from transformers import AutoTokenizer, BertModel, BertConfig
|
| 340 |
from huggingface_hub import snapshot_download
|
| 341 |
|
| 342 |
+
print("Loading DNABERT-2 brain...")
|
| 343 |
+
mp = snapshot_download("zhihan1996/DNABERT-2-117M")
|
| 344 |
+
tok = AutoTokenizer.from_pretrained(mp, trust_remote_code=True)
|
| 345 |
+
cfg = BertConfig.from_pretrained(mp)
|
| 346 |
+
db = BertModel.from_pretrained(mp, config=cfg, ignore_mismatched_sizes=True)
|
|
|
|
|
|
|
| 347 |
|
| 348 |
class RPScoreHead(nn.Module):
|
| 349 |
+
def __init__(self, h=768):
|
| 350 |
super().__init__()
|
| 351 |
self.net = nn.Sequential(
|
| 352 |
+
nn.Linear(h,512), nn.LayerNorm(512), nn.GELU(), nn.Dropout(0.15),
|
| 353 |
+
nn.Linear(512,256), nn.LayerNorm(256), nn.GELU(), nn.Dropout(0.10),
|
| 354 |
+
nn.Linear(256,128), nn.GELU(), nn.Dropout(0.05),
|
| 355 |
+
nn.Linear(128,32), nn.GELU(),
|
| 356 |
+
nn.Linear(32,1), nn.Sigmoid()
|
| 357 |
)
|
| 358 |
+
def forward(self, x): return self.net(x).squeeze(-1) * 100.0
|
|
|
|
| 359 |
|
| 360 |
class N2NModel(nn.Module):
|
| 361 |
+
def __init__(self, db):
|
| 362 |
super().__init__()
|
| 363 |
+
self.encoder = db
|
| 364 |
+
self.head = RPScoreHead()
|
| 365 |
+
def forward(self, ids, mask):
|
| 366 |
+
out = self.encoder(input_ids=ids, attention_mask=mask)
|
| 367 |
+
return self.head(out.last_hidden_state[:,0,:])
|
| 368 |
+
|
| 369 |
+
m = N2NModel(db)
|
| 370 |
+
w = "models/n2n_dnabert2_v2.pt"
|
| 371 |
+
if os.path.exists(w):
|
| 372 |
+
import torch
|
| 373 |
+
ck = torch.load(w, map_location='cpu')
|
| 374 |
+
m.load_state_dict(ck['model_state_dict'])
|
| 375 |
m.eval()
|
| 376 |
dnabert_model = m
|
| 377 |
+
dnabert_tok = tok
|
| 378 |
BRAIN_TYPE = "DNABERT-2"
|
| 379 |
+
print("DNABERT-2 v2 loaded. Pearson r=0.941")
|
| 380 |
else:
|
| 381 |
+
print("v2 weights not found")
|
| 382 |
except Exception as e:
|
| 383 |
print("DNABERT-2 failed: " + str(e))
|
| 384 |
|
|
|
|
|
|
|
| 385 |
threading.Thread(target=load_dnabert, daemon=True).start()
|
| 386 |
|
|
|
|
| 387 |
def predict(window):
|
| 388 |
+
if dnabert_model is not None and dnabert_tok is not None:
|
|
|
|
| 389 |
try:
|
| 390 |
import torch
|
| 391 |
+
enc = dnabert_tok(window, return_tensors='pt',
|
| 392 |
+
max_length=36, padding='max_length', truncation=True)
|
|
|
|
| 393 |
with torch.no_grad():
|
| 394 |
+
s = dnabert_model(enc['input_ids'], enc['attention_mask']).item()
|
| 395 |
+
return round(s, 3), "DNABERT-2"
|
|
|
|
| 396 |
except:
|
| 397 |
pass
|
|
|
|
|
|
|
| 398 |
if rfc_model is not None:
|
| 399 |
try:
|
| 400 |
+
s = float(rfc_model.predict(encode_window(window).reshape(1,-1))[0])
|
| 401 |
+
return round(max(0,min(100,s))/100, 3), "RFC-ML"
|
|
|
|
| 402 |
except:
|
| 403 |
pass
|
| 404 |
+
return round(compute_rp_score_rfc(window)/100, 3), "RFC-Rule"
|
|
|
|
|
|
|
| 405 |
|
| 406 |
# ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 407 |
@app.route('/', methods=['GET'])
|
| 408 |
def home():
|
| 409 |
return jsonify({
|
| 410 |
'name': 'N2N Precision Engine',
|
| 411 |
+
'version': '3.0',
|
| 412 |
'brain': BRAIN_TYPE,
|
| 413 |
'inventor': 'Manav Vanga',
|
| 414 |
'patent': 'Pending 2026',
|
| 415 |
+
'description': 'Predicts readthrough therapy response for all nonsense mutation diseases',
|
| 416 |
+
'calibration': {'high_threshold': HIGH_THRESHOLD, 'med_threshold': MED_THRESHOLD},
|
| 417 |
+
'endpoints': ['/api/health', '/api/score', '/api/demo', '/api/trials'],
|
| 418 |
})
|
| 419 |
|
| 420 |
@app.route('/api/health', methods=['GET'])
|
| 421 |
def health():
|
| 422 |
return jsonify({
|
| 423 |
+
'status': 'healthy',
|
| 424 |
+
'brain': BRAIN_TYPE,
|
| 425 |
+
'version': '3.0',
|
| 426 |
+
'calibrated': True,
|
| 427 |
+
'thresholds': {'high': HIGH_THRESHOLD, 'med': MED_THRESHOLD},
|
| 428 |
})
|
| 429 |
|
| 430 |
@app.route('/api/score', methods=['GET','POST'])
|
|
|
|
| 433 |
data = request.get_json() or {}
|
| 434 |
window = data.get('window','')
|
| 435 |
gene = data.get('gene','UNKNOWN')
|
| 436 |
+
fetch_trials = data.get('trials', True)
|
| 437 |
else:
|
| 438 |
window = request.args.get('window','')
|
| 439 |
gene = request.args.get('gene','UNKNOWN')
|
| 440 |
+
fetch_trials = request.args.get('trials','true').lower() == 'true'
|
| 441 |
|
| 442 |
if not window or len(window) < 20:
|
| 443 |
+
return jsonify({'error': 'window required (min 20bp DNA sequence)'}), 400
|
| 444 |
|
| 445 |
window = window.upper().replace('U','T')
|
| 446 |
+
score, brain_used = predict(window)
|
| 447 |
+
tier = get_tier(score)
|
| 448 |
w = (window+'N'*30)[:30]
|
| 449 |
p4 = w[18] if len(w)>18 else 'N'
|
| 450 |
+
road, road_desc = PLUS4_ROAD.get(p4, ('Unknown','Unknown'))
|
| 451 |
+
drugs = DRUG_DATABASE[tier]
|
| 452 |
audit = hashlib.sha256(
|
| 453 |
+
(window+str(score)+datetime.now(timezone.utc).isoformat()
|
| 454 |
).encode()).hexdigest()[:16]
|
| 455 |
|
| 456 |
+
# Fetch live clinical trials
|
| 457 |
+
trials = []
|
| 458 |
+
if fetch_trials:
|
| 459 |
+
trials = fetch_clinical_trials(gene=gene if gene != 'UNKNOWN' else None)
|
| 460 |
+
|
| 461 |
return jsonify({
|
| 462 |
+
'gene': gene,
|
| 463 |
+
'window': window[:30],
|
| 464 |
+
'rp_score': score,
|
| 465 |
+
'tier': tier,
|
| 466 |
+
'plus4_base': p4,
|
| 467 |
+
'plus4_road': road,
|
| 468 |
+
'plus4_road_desc': road_desc,
|
| 469 |
+
'therapy': drugs['therapy'],
|
| 470 |
+
'mechanism': drugs['mechanism'],
|
| 471 |
+
'approved_drugs': drugs['approved'],
|
| 472 |
+
'phase3_drugs': drugs['phase3'],
|
| 473 |
+
'phase2_drugs': drugs['phase2'],
|
| 474 |
+
'preclinical': drugs['preclinical'],
|
| 475 |
+
'combination': drugs['combination'],
|
| 476 |
+
'clinical_trials': trials,
|
| 477 |
+
'brain': brain_used,
|
| 478 |
+
'confidence': 'HIGH' if brain_used=='DNABERT-2' else 'MEDIUM',
|
| 479 |
+
'audit_hash': audit,
|
| 480 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
| 481 |
+
'inventor': 'Manav Vanga',
|
| 482 |
+
'patent': 'Pending 2026',
|
| 483 |
+
})
|
| 484 |
+
|
| 485 |
+
@app.route('/api/trials', methods=['GET'])
|
| 486 |
+
def trials():
|
| 487 |
+
"""Live clinical trials from ClinicalTrials.gov"""
|
| 488 |
+
gene = request.args.get('gene','')
|
| 489 |
+
condition = request.args.get('condition','')
|
| 490 |
+
drug = request.args.get('drug','')
|
| 491 |
+
|
| 492 |
+
if drug:
|
| 493 |
+
results = fetch_drug_trials(drug)
|
| 494 |
+
else:
|
| 495 |
+
results = fetch_clinical_trials(gene=gene, condition=condition)
|
| 496 |
+
|
| 497 |
+
return jsonify({
|
| 498 |
+
'query': {'gene':gene, 'condition':condition, 'drug':drug},
|
| 499 |
+
'count': len(results),
|
| 500 |
+
'trials': results,
|
| 501 |
+
'source': 'ClinicalTrials.gov API v2',
|
| 502 |
+
'note': 'Live data β refreshed on every request',
|
| 503 |
})
|
| 504 |
|
| 505 |
@app.route('/api/demo', methods=['GET'])
|
| 506 |
def demo():
|
| 507 |
demos = [
|
| 508 |
+
('CFTR','Y122X', 'AAGAAATCGATCAGTTAACAGCTTGCAGCN', '18.5% paper'),
|
| 509 |
+
('CFTR','G542X', 'AAGAAATCGATCAGTTGAGAGCTTGCAGCN', '0.3% paper'),
|
| 510 |
+
('CFTR','W1282X','AAGAAATCGATCAGTTGACAGCTTGCAGCN', '8.2% paper'),
|
| 511 |
+
('DMD', 'Q1922X','GCAGCAGCAGCAGCATGACGCAGCAGCAGC', 'predicted HIGH'),
|
| 512 |
+
('TP53','R213X', 'CGCGGCGGCGGCGGTGACGCAGCAGCAGCN', 'predicted HIGH'),
|
| 513 |
]
|
| 514 |
results = []
|
| 515 |
for gene, variant, window, expected in demos:
|
| 516 |
+
s, brain = predict(window)
|
| 517 |
results.append({
|
| 518 |
'gene': gene,
|
| 519 |
'variant': variant,
|
| 520 |
+
'rp_score': s,
|
| 521 |
+
'tier': get_tier(s),
|
| 522 |
'expected': expected,
|
| 523 |
'brain': brain,
|
| 524 |
})
|
| 525 |
+
return jsonify({
|
| 526 |
+
'demo_results': results,
|
| 527 |
+
'brain': BRAIN_TYPE,
|
| 528 |
+
'calibration': {'high': HIGH_THRESHOLD, 'med': MED_THRESHOLD},
|
| 529 |
+
})
|
| 530 |
|
| 531 |
if __name__ == '__main__':
|
| 532 |
+
port = int(os.environ.get('PORT', 7860))
|
| 533 |
app.run(host='0.0.0.0', port=port)
|