-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea_bias_correction.py
More file actions
553 lines (450 loc) · 18.5 KB
/
area_bias_correction.py
File metadata and controls
553 lines (450 loc) · 18.5 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
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
import os
import pickle
import sys
import pandas as pd
import numpy as np
import argparse
from scipy.interpolate import interp1d
from concurrent.futures import ProcessPoolExecutor
from methods import str2bool
os.environ["PYTHONUNBUFFERED"] = "1"
os.environ["PYTHONIOENCODING"] = "utf-8"
os.environ["PYTHONUTF8"] = "1"
try:
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
except Exception:
pass
def load_models(model_path: str) -> dict:
with open(model_path, 'rb') as f:
return pickle.load(f)
def is_minutes(rt_unit: str) -> bool:
s = str(rt_unit).strip().lower()
return s in {"m", "min", "minute", "minutes"}
def create_inverse_model(model, x_range=(0.0, 45.0), num_points=500000):
x = np.linspace(float(x_range[0]), float(x_range[1]), int(num_points))
y = model([[xi] for xi in x])
y = np.array(y, dtype=float).flatten()
# Remove non-finite
m = np.isfinite(x) & np.isfinite(y)
x = x[m]
y = y[m]
# Sort by y for invertibility
order = np.argsort(y)
y_sorted = y[order]
x_sorted = x[order]
y_unique, idx = np.unique(y_sorted, return_index=True)
x_unique = x_sorted[idx]
if y_unique.size < 2:
raise ValueError("Failed to build inverse model: not enough unique points in y.")
return interp1d(y_unique, x_unique, bounds_error=False, fill_value="extrapolate")
def apply_inverse_model(inverse_model, corrected_rt_values, rt_unit: str):
arr = np.asarray(corrected_rt_values, dtype=float)
if is_minutes(rt_unit):
rt_minutes = arr
else:
rt_minutes = arr / 60.0
original_minutes = inverse_model(rt_minutes)
return original_minutes if is_minutes(rt_unit) else original_minutes * 60.0
def reverse_area_from_center(
inverse_model,
rt_center_corr: float,
area_corr: float,
widths,
rt_unit: str,
rt_min: float,
rt_max: float
):
rc_in = float(rt_center_corr)
rc_min = rc_in if is_minutes(rt_unit) else rc_in / 60.0
rc_min = float(np.clip(rc_min, rt_min, rt_max))
scales = []
for w in widths:
w = float(w)
half = w / 2.0
max_half_left = rc_min - rt_min
max_half_right = rt_max - rc_min
half_eff = min(half, max_half_left, max_half_right)
if half_eff <= 0:
continue
left_corr_min = rc_min - half_eff
right_corr_min = rc_min + half_eff
left_ori_min = apply_inverse_model(inverse_model, left_corr_min, "min")
right_ori_min = apply_inverse_model(inverse_model, right_corr_min, "min")
den = (right_corr_min - left_corr_min)
if den <= 0:
continue
scale = (right_ori_min - left_ori_min) / den
scales.append(scale)
if len(scales) == 0:
return float(area_corr)
return float(area_corr) * float(np.mean(scales))
def _normalize_name(s: str) -> str:
s = str(s).strip().lower()
s = s.replace(" ", "").replace("_", "").replace("-", "")
return s
def _strip_suffixes(s: str, suffix) -> str:
s0 = str(s)
if not suffix:
return s0
suf = str(suffix)
if s0.lower().endswith(suf.lower()):
return s0[:-len(suf)]
return s0
def _build_model_lookup(model_dict, model_suffixes):
lookup = {}
for k in model_dict.keys():
k2 = _strip_suffixes(k, model_suffixes)
nk = _normalize_name(k2)
if nk and nk not in lookup:
lookup[nk] = k
return lookup
def _to_token(name: str, suffixes=None) -> str:
s = _strip_suffixes(name, suffixes)
return _normalize_name(s)
def match_model_key(query_name: str, model_dict: dict, model_suffixes, query_suffixes=None, model_lookup=None):
if model_lookup is None:
model_lookup = _build_model_lookup(model_dict, model_suffixes)
q = _to_token(query_name, query_suffixes)
return model_lookup.get(q)
def process_aligned_file(
file_path,
model_dict,
sep,
output_dir,
rt_center_col,
RTCenterWidths,
rt_unit,
rt_min,
rt_max,
inverse_points,
model_suffixes
):
df = pd.read_csv(file_path, sep=sep)
if rt_center_col not in df.columns:
print(f"[Error] RT center column '{rt_center_col}' not found: {file_path}")
return
rt_center = pd.to_numeric(df[rt_center_col], errors="coerce").to_numpy(dtype=float)
model_lookup = _build_model_lookup(model_dict, model_suffixes)
sample_cols = []
col_to_modelkey = {}
used_model_keys = set()
for col in df.columns:
if col == rt_center_col:
continue
mk = match_model_key(
query_name=col,
model_dict=model_dict,
model_suffixes=model_suffixes,
query_suffixes=model_suffixes,
model_lookup=model_lookup
)
if mk is not None:
sample_cols.append(col)
col_to_modelkey[col] = mk
used_model_keys.add(_normalize_name(_strip_suffixes(mk, model_suffixes)))
unused_model_keys = set(model_lookup.keys()) - used_model_keys
if unused_model_keys:
print(f"Area column absent ({len(unused_model_keys)}):")
for k in sorted(unused_model_keys):
print(" -", k)
if not sample_cols:
print(f"No sample columns matched in aligned feature list: {file_path}")
return
inv_models = {}
for col in sample_cols:
mk = col_to_modelkey[col]
inv_models[col] = create_inverse_model(
model_dict[mk],
x_range=(rt_min, rt_max),
num_points=inverse_points
)
for col in sample_cols:
inv = inv_models[col]
area_vals = pd.to_numeric(df[col], errors="coerce").to_numpy(dtype=float)
out = np.empty_like(area_vals, dtype=float)
for i in range(len(area_vals)):
a = area_vals[i]
rc = rt_center[i]
if not np.isfinite(a) or not np.isfinite(rc):
out[i] = a
continue
out[i] = reverse_area_from_center(
inv,
rc,
a,
RTCenterWidths,
rt_unit,
rt_min=rt_min,
rt_max=rt_max
)
df[col] = out
os.makedirs(output_dir, exist_ok=True)
out_path = os.path.join(output_dir, os.path.basename(file_path))
df.to_csv(out_path, index=False, sep=sep)
print(f"Bias corrected feature list saved: {out_path}")
def process_corrected_file(
file_path, model_bytes, sep, output_dir,
rt_left_col, rt_right_col, area_col,
rt_unit,
rt_max=45.0,
rt_min=0.0,
RTCenterOnly=False,
RTCenterWidths=(0.2, 0.5, 1.0),
rt_center_col=None,
keep_original_values=True,
rt_rev_suffix="_rev",
area_rev_suffix="_rev",
inverse_points=500000
):
model = pickle.loads(model_bytes)
df = pd.read_csv(file_path, sep=sep)
cols = df.columns.tolist()
def resolve_col(c):
return cols[c] if isinstance(c, int) else c
area_colname = resolve_col(area_col)
if RTCenterOnly:
if rt_center_col is None:
raise ValueError("rt_center_col must be provided when RTCenterOnly=True")
rt_center_colname = resolve_col(rt_center_col)
else:
if rt_left_col is None or rt_right_col is None:
raise ValueError("rt edge columns must be provided when RTCenterOnly=True")
rt_left_colname = resolve_col(rt_left_col)
rt_right_colname = resolve_col(rt_right_col)
need_cols = [area_colname]
if RTCenterOnly:
need_cols.append(rt_center_colname)
else:
need_cols.extend([rt_left_colname, rt_right_colname])
missing = [c for c in need_cols if c not in df.columns]
if missing:
print(f"Missing columns {missing} in {file_path}")
return
inv = create_inverse_model(model, x_range=(rt_min, rt_max), num_points=inverse_points)
if RTCenterOnly:
rt_center_corr = pd.to_numeric(df[rt_center_colname], errors='coerce').to_numpy(dtype=float)
area_corr = pd.to_numeric(df[area_colname], errors='coerce').to_numpy(dtype=float)
rt_center_corr_min = rt_center_corr if is_minutes(rt_unit) else (rt_center_corr / 60.0)
rt_center_corr_min_clamped = np.clip(rt_center_corr_min, rt_min, rt_max)
rt_center_ori_min = apply_inverse_model(inv, rt_center_corr_min_clamped, "min")
area_ori = np.empty_like(area_corr, dtype=float)
for i in range(len(area_corr)):
if not np.isfinite(rt_center_corr_min[i]) or not np.isfinite(area_corr[i]):
area_ori[i] = area_corr[i]
continue
area_ori[i] = reverse_area_from_center(
inv,
rt_center_corr_min[i],
area_corr[i],
RTCenterWidths,
"min",
rt_min=rt_min,
rt_max=rt_max
)
rt_center_ori = rt_center_ori_min if is_minutes(rt_unit) else (rt_center_ori_min * 60.0)
if keep_original_values:
df[f"{rt_center_colname}{rt_rev_suffix}"] = rt_center_ori
df[f"{area_colname}{area_rev_suffix}"] = area_ori
else:
df[rt_center_colname] = rt_center_ori
df[area_colname] = area_ori
else:
left_corr = pd.to_numeric(df[rt_left_colname], errors='coerce').to_numpy(dtype=float)
right_corr = pd.to_numeric(df[rt_right_colname], errors='coerce').to_numpy(dtype=float)
area_corr = pd.to_numeric(df[area_colname], errors='coerce').to_numpy(dtype=float)
left_corr_min = left_corr if is_minutes(rt_unit) else (left_corr / 60.0)
right_corr_min = right_corr if is_minutes(rt_unit) else (right_corr / 60.0)
left_corr_min = np.clip(left_corr_min, rt_min, rt_max)
right_corr_min = np.clip(right_corr_min, rt_min, rt_max)
left_ori_min = apply_inverse_model(inv, left_corr_min, "min")
right_ori_min = apply_inverse_model(inv, right_corr_min, "min")
denom = (right_corr_min - left_corr_min)
denom_safe = np.where(denom == 0, np.nan, denom)
area_ori = area_corr * (right_ori_min - left_ori_min) / denom_safe
area_ori = np.where(np.isnan(area_ori), area_corr, area_ori)
left_ori = left_ori_min if is_minutes(rt_unit) else (left_ori_min * 60.0)
right_ori = right_ori_min if is_minutes(rt_unit) else (right_ori_min * 60.0)
if keep_original_values:
df[f"{rt_left_colname}{rt_rev_suffix}"] = left_ori
df[f"{rt_right_colname}{rt_rev_suffix}"] = right_ori
df[f"{area_colname}{area_rev_suffix}"] = area_ori
else:
df[rt_left_colname] = left_ori
df[rt_right_colname] = right_ori
df[area_colname] = area_ori
filename = os.path.basename(file_path)
output_path = os.path.join(output_dir, filename)
df.to_csv(output_path, index=False, sep=sep)
print(f"Bias corrected feature list saved: {output_path}")
def batch_reverse_feature_lists(
folder_path, model_dict, sep, output_dir,
input_suffixes, model_suffixes,
rt_left_col, rt_right_col, area_col,
rt_unit, n_workers=4,
rt_max=45.0,
rt_min=0.0,
inverse_points=500000,
RTCenterOnly=False,
RTCenterWidths=(0.2, 0.5, 1.0),
rt_center_col=None,
keep_ori=True,
rt_rev_suffix="_rev",
area_rev_suffix="_rev",
aligned_mode=False,
aligned_rt_center_col=None
):
os.makedirs(output_dir, exist_ok=True)
if aligned_mode:
if aligned_rt_center_col is None:
raise ValueError("aligned_rt_center_col is required")
if os.path.isfile(folder_path):
files = [folder_path]
else:
files = [
os.path.join(folder_path, fn)
for fn in os.listdir(folder_path)
if fn.lower().endswith(str(input_suffixes).lower())
]
if not files:
print("input files not found")
return
for fp in files:
process_aligned_file(
fp,
model_dict,
sep,
output_dir,
rt_center_col=aligned_rt_center_col,
RTCenterWidths=RTCenterWidths,
rt_unit=rt_unit,
rt_min=rt_min,
rt_max=rt_max,
inverse_points=inverse_points,
model_suffixes=model_suffixes
)
return
model_lookup = _build_model_lookup(model_dict, model_suffixes)
tasks = []
for file_name in os.listdir(folder_path):
if not file_name.lower().endswith(str(input_suffixes).lower()):
continue
file_path = os.path.join(folder_path, file_name)
mk = match_model_key(
query_name=file_name,
model_dict=model_dict,
model_suffixes=model_suffixes,
query_suffixes=input_suffixes
)
if mk is None:
print(f"No model for {file_name}")
continue
matched_model = model_dict[mk]
model_bytes = pickle.dumps(matched_model)
tasks.append((file_path, model_bytes))
if not tasks:
print("Model matching failed")
print("Model key expamle:" + list(model_dict.keys())[0])
return
with ProcessPoolExecutor(max_workers=n_workers) as executor:
futures = []
for file_path, model_bytes in tasks:
futures.append(executor.submit(
process_corrected_file,
file_path, model_bytes, sep, output_dir,
rt_left_col, rt_right_col, area_col,
rt_unit,
rt_max=rt_max,
rt_min=rt_min,
inverse_points=inverse_points,
RTCenterOnly=RTCenterOnly,
RTCenterWidths=RTCenterWidths,
rt_center_col=rt_center_col,
keep_original_values=keep_ori,
rt_rev_suffix=rt_rev_suffix,
area_rev_suffix=area_rev_suffix
))
for f in futures:
f.result()
def main():
parser = argparse.ArgumentParser(
description="Area bias correction for individual/aligned feature lists"
)
# load files
parser.add_argument("--model_path", type=str,
default=r"E:\Halo_lipidomic_zhang\rtcorrection\rt_correction_models.pkl")
parser.add_argument("--input", type=str,
default=r"E:\Halo_lipidomic_zhang\Feature_list_compare\Area_3_2026_02_06_14_18_18.txt",
help="Folder (feature lists) or file path (aligned feature list)")
parser.add_argument("--output_dir", type=str,
default=r"E:\Halo_lipidomic_zhang\Feature_list_compare\Correction\reversed")
parser.add_argument("--rt_max", type=float, default=45,help="Maximum retention time of the dataset (min) (default: 45)")
parser.add_argument("--n_workers", type=int, default=max(1, (os.cpu_count() or 2) - 1),help="Number of CPU processors (default: cpu_count-1)")
parser.add_argument("--input_suffix", type=str, default=".txt",help="Suffix of feature list files")
parser.add_argument("--model_suffix", type=str, default=".txt",help="Suffix used in model training file")
# columns
parser.add_argument("--rt_left_col", type=str, default="RT left(min)",help='Name of RT left boundary column (used when "rt_center_only=false")')
parser.add_argument("--rt_right_col", type=str, default="RT right (min)",help='Name of RT right boundary column (used when "rt_center_only=false")')
parser.add_argument("--rt_center_col", type=str, default="Average Rt(min)", help='Name of RT center column (used when "rt_center_only=true")')
parser.add_argument("--area_col", type=str, default="Area")
# modes
parser.add_argument("--rt_center_only", type=str2bool, default="true",
help='"true": correct bias using RT center (recommended); "false": correct bias using RT left and right edge (default: true)')
parser.add_argument("--aligned_mode", type=str2bool, default="true",
help='"true": input is one aligned feature list file; "false": inputs are individual feature lists (default: true)')
parser.add_argument("--rt_unit", type=str, default="min",
help='RT unit in input files "min" or "sec" (default: min)')
parser.add_argument("--keep_ori", type=str2bool, default="false",help="Keep original RT and area info (default: false)")
args = parser.parse_args()
models = load_models(args.model_path)
if "csv" in args.input_suffix:
sep = ","
else:
sep = "\t"
batch_reverse_feature_lists(
args.input,
models,
sep=sep,
output_dir=args.output_dir,
input_suffixes=args.input_suffix,
model_suffixes=args.model_suffix,
rt_left_col=args.rt_left_col,
rt_right_col=args.rt_right_col,
area_col=args.area_col,
rt_unit=args.rt_unit,
n_workers=args.n_workers,
rt_min=0,
rt_max=args.rt_max,
inverse_points=10000,
RTCenterOnly=args.rt_center_only,
RTCenterWidths=(0.2, 0.5, 1.0),
rt_center_col=args.rt_center_col,
keep_ori=args.keep_ori,
rt_rev_suffix="_rev",
area_rev_suffix="_rev",
aligned_mode=args.aligned_mode,
aligned_rt_center_col=args.rt_center_col
)
def entrypoint():
try:
main()
except SystemExit as e:
if e.code == 0:
return
print("\n[ERROR] Argument parsing caused exit.")
print(f"SystemExit code: {e}")
except Exception as e:
import traceback
print("\n[ERROR] Program failed, but it will NOT exit abruptly.")
print(f"Exception type: {type(e).__name__}")
print(f"Exception message: {e}\n")
tb = traceback.extract_tb(e.__traceback__)
print("========== TRACEBACK (most recent call last) ==========")
for frame in tb:
print(
f'File "{frame.filename}", line {frame.lineno}, in {frame.name}\n'
f' -> {frame.line}'
)
print("======================================================")
if __name__ == '__main__':
entrypoint()