-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_v3_physics.py
More file actions
71 lines (54 loc) · 2.21 KB
/
update_v3_physics.py
File metadata and controls
71 lines (54 loc) · 2.21 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
import re
# Read the physics detector
with open('physics_region_detector.py', 'r') as f:
physics_code = f.read()
# Read v3
with open('quantify_all_metabolites_v3_physics.py', 'r') as f:
v3_code = f.read()
# Add import and physics functions after the existing imports
import_section_end = v3_code.find("def read_and_process")
physics_functions = '''
# Import physics-based region detection
import sys
sys.path.insert(0, '.')
from physics_region_detector import (
group_expected_peaks,
detect_actual_region,
get_physics_based_regions,
METABOLITE_PHYSICS
)
'''
v3_code = v3_code[:import_section_end] + physics_functions + v3_code[import_section_end:]
# Replace the metabolite info function
old_func_start = v3_code.find("def get_metabolite_info_v3()")
old_func_end = v3_code.find("def fit_region", old_func_start)
new_func = '''def get_metabolite_info_v3():
"""
Return metabolite information with physics-based expected peak positions.
Actual regions are detected automatically from spectra using physics principles:
- PURE-SHIFT NMR: Each distinct group → 1 singlet
- DIASTEREOTOPIC: CH2 next to chiral center → 2 peaks
- REGION GROUPING: Peaks within 0.3 ppm grouped together
- BOUNDARIES: Auto-detected at 10% peak height
"""
return METABOLITE_PHYSICS
def get_detected_regions_for_metabolite(met_name, ppm, intensity):
"""Get physics-based detected regions for a specific metabolite."""
if met_name not in METABOLITE_PHYSICS:
return None
expected = METABOLITE_PHYSICS[met_name]
regions = get_physics_based_regions(met_name, ppm, intensity, expected)
# Convert to format expected by fitting code
result = {
'regions': [r['bounds'] for r in regions],
'region_peaks': [r['peaks'] for r in regions],
'region_protons': [r['protons'] for r in regions],
'region_names': [r['name'] for r in regions],
}
return result, regions
'''
v3_code = v3_code[:old_func_start] + new_func + v3_code[old_func_end:]
# Write updated file
with open('quantify_all_metabolites_v3_physics.py', 'w') as f:
f.write(v3_code)
print("Updated quantify_all_metabolites_v3_physics.py with physics-based region detection")