-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenzyme_kinetics_fitter.py
More file actions
260 lines (218 loc) · 7.32 KB
/
Copy pathenzyme_kinetics_fitter.py
File metadata and controls
260 lines (218 loc) · 7.32 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
"""
enzyme_kinetics_fitter.py
Fit Michaelis–Menten enzyme kinetics to experimental data.
Takes a CSV file with substrate concentration [S] and initial velocity v,
fits Vmax and Km using non-linear least squares, and generates plots.
Usage examples:
python enzyme_kinetics_fitter.py --input data.csv
python enzyme_kinetics_fitter.py \
--input data.csv \
--substrate-col Substrate_mM \
--velocity-col Rate_uM_s \
--output-plot kinetics_fit.png
"""
import argparse
import json
import sys
from dataclasses import dataclass
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def michaelis_menten(S, Vmax, Km):
"""Michaelis–Menten equation: v = (Vmax * [S]) / (Km + [S])"""
return (Vmax * S) / (Km + S)
@dataclass
class FitResult:
Vmax: float
Km: float
Vmax_std: float | None
Km_std: float | None
r_squared: float | None
def fit_michaelis_menten(S: np.ndarray, v: np.ndarray) -> FitResult:
"""Fit Michaelis–Menten parameters to data."""
# Clean data: remove NaN and negative values
mask = (~np.isnan(S)) & (~np.isnan(v)) & (S > 0) & (v > 0)
S_clean = S[mask]
v_clean = v[mask]
if len(S_clean) < 3:
raise ValueError("Not enough valid data points after cleaning (need ≥ 3).")
# Initial guesses
Vmax_guess = np.max(v_clean)
Km_guess = np.median(S_clean)
try:
popt, pcov = curve_fit(
michaelis_menten,
S_clean,
v_clean,
p0=[Vmax_guess, Km_guess],
maxfev=10000,
)
Vmax_fit, Km_fit = popt
# Standard deviations from covariance matrix
perr = np.sqrt(np.diag(pcov))
Vmax_std, Km_std = perr
# R^2 calculation
v_pred = michaelis_menten(S_clean, Vmax_fit, Km_fit)
ss_res = np.sum((v_clean - v_pred) ** 2)
ss_tot = np.sum((v_clean - np.mean(v_clean)) ** 2)
r_squared = 1 - ss_res / ss_tot if ss_tot != 0 else None
return FitResult(
Vmax=Vmax_fit,
Km=Km_fit,
Vmax_std=Vmax_std,
Km_std=Km_std,
r_squared=r_squared,
)
except Exception as e:
raise RuntimeError(f"Curve fitting failed: {e}") from e
def plot_fit(S: np.ndarray,
v: np.ndarray,
fit: FitResult,
output_path: str | None = None,
title: str | None = None):
"""Generate plot with data + fitted curve + residuals."""
# Clean data same way as fitting
mask = (~np.isnan(S)) & (~np.isnan(v)) & (S > 0) & (v > 0)
S_clean = S[mask]
v_clean = v[mask]
S_range = np.linspace(0, S_clean.max() * 1.1, 200)
v_fit_curve = michaelis_menten(S_range, fit.Vmax, fit.Km)
v_pred = michaelis_menten(S_clean, fit.Vmax, fit.Km)
residuals = v_clean - v_pred
fig = plt.figure(figsize=(8, 8))
# Top: data + fit
ax1 = fig.add_subplot(2, 1, 1)
ax1.scatter(S_clean, v_clean, label="Data")
ax1.plot(S_range, v_fit_curve, label="Fit (Michaelis–Menten)")
ax1.set_xlabel("[S]")
ax1.set_ylabel("v")
ax1.set_title(title or "Enzyme Kinetics Fit (Michaelis–Menten)")
ax1.legend()
ax1.grid(True, linestyle="--", alpha=0.4)
# Bottom: residuals
ax2 = fig.add_subplot(2, 1, 2)
ax2.axhline(0, linestyle="--")
ax2.scatter(S_clean, residuals)
ax2.set_xlabel("[S]")
ax2.set_ylabel("Residuals (v_obs - v_fit)")
ax2.set_title("Residuals")
ax2.grid(True, linestyle="--", alpha=0.4)
plt.tight_layout()
if output_path:
plt.savefig(output_path, dpi=300, bbox_inches="tight")
print(f"[INFO] Plot saved to: {output_path}")
else:
plt.show()
plt.close(fig)
def parse_args(argv=None):
parser = argparse.ArgumentParser(
description="Fit Michaelis–Menten kinetics (Vmax & Km) from CSV data."
)
parser.add_argument(
"--input",
"-i",
required=True,
help="Path to CSV file containing substrate and velocity data.",
)
parser.add_argument(
"--substrate-col",
default=None,
help="Name of the substrate concentration column (default: first numeric column).",
)
parser.add_argument(
"--velocity-col",
default=None,
help="Name of the velocity column (default: second numeric column).",
)
parser.add_argument(
"--output-plot",
"-o",
default="kinetics_fit.png",
help="Output path for the plot PNG file (default: kinetics_fit.png).",
)
parser.add_argument(
"--output-json",
default=None,
help="Optional: output fit parameters as JSON to this file.",
)
parser.add_argument(
"--no-plot",
action="store_true",
help="If set, do not generate a plot.",
)
parser.add_argument(
"--title",
default=None,
help="Optional title for the plot.",
)
return parser.parse_args(argv)
def guess_numeric_columns(df: pd.DataFrame):
"""Return names of first two numeric columns, or raise error."""
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
if len(numeric_cols) < 2:
raise ValueError("CSV must contain at least two numeric columns.")
return numeric_cols[0], numeric_cols[1]
def main(argv=None):
args = parse_args(argv)
# Load CSV
try:
df = pd.read_csv(args.input)
except Exception as e:
print(f"[ERROR] Failed to read CSV: {e}")
sys.exit(1)
# Detect columns
if args.substrate_col is None or args.velocity_col is None:
s_col, v_col = guess_numeric_columns(df)
print(f"[INFO] Using numeric columns: [S] = '{s_col}', v = '{v_col}'")
else:
s_col, v_col = args.substrate_col, args.velocity_col
if s_col not in df.columns or v_col not in df.columns:
print(f"[ERROR] Columns not found in CSV. Available columns: {list(df.columns)}")
sys.exit(1)
S = df[s_col].to_numpy(dtype=float)
v = df[v_col].to_numpy(dtype=float)
# Fit
try:
fit = fit_michaelis_menten(S, v)
except Exception as e:
print(f"[ERROR] {e}")
sys.exit(1)
# Print results
print("\n=== Michaelis–Menten Fit Results ===")
print(f"Vmax = {fit.Vmax:.4g}")
if fit.Vmax_std is not None:
print(f"Vmax_std = {fit.Vmax_std:.4g}")
print(f"Km = {fit.Km:.4g}")
if fit.Km_std is not None:
print(f"Km_std = {fit.Km_std:.4g}")
if fit.r_squared is not None:
print(f"R² = {fit.r_squared:.4f}")
print("====================================\n")
# Save JSON if requested
if args.output_json:
payload = {
"Vmax": fit.Vmax,
"Km": fit.Km,
"Vmax_std": fit.Vmax_std,
"Km_std": fit.Km_std,
"r_squared": fit.r_squared,
"input_csv": args.input,
"substrate_col": s_col,
"velocity_col": v_col,
}
with open(args.output_json, "w", encoding="utf-8") as f:
json.dump(payload, f, indent=2)
print(f"[INFO] Fit parameters saved to JSON: {args.output_json}")
# Plot
if not args.no_plot:
plot_fit(
S,
v,
fit,
output_path=args.output_plot,
title=args.title,
)
if __name__ == "__main__":
main()