Skip to content

Instantly share code, notes, and snippets.

@alexcraven
Last active January 3, 2023 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcraven/3db2c09f14ec489a31df81dc7b5a0f9c to your computer and use it in GitHub Desktop.
Save alexcraven/3db2c09f14ec489a31df81dc7b5a0f9c to your computer and use it in GitHub Desktop.
Read LCM Coords
def read_lcm_values(fn): # {{{
series_type = None
series_data = {}
with open(fn) as f:
vals = []
for line in f:
prev_series_type = series_type
if re.match(".*[0-9]+ points on ppm-axis = NY.*", line):
series_type = "ppm"
elif re.match(".*NY phased data points follow.*", line):
series_type = "data"
elif re.match(".*NY points of the fit to the data follow.*", line):
series_type = "completeFit"
# completeFit implies baseline+fit
elif re.match(".*NY background values follow.*", line):
series_type = "baseline"
elif re.match(".*lines in following.*", line):
series_type = None
elif re.match("[ ]+[a-zA-Z0-9]+[ ]+Conc. = [-+.E0-9]+$", line):
series_type = None
if prev_series_type != series_type: # start/end of chunk...
if len(vals) > 0:
series_data[prev_series_type] = np.array(vals)
vals = []
else:
if series_type:
for x in re.finditer(r"([-+.E0-9]+)[ \t]*", line):
v = x.group(1)
try:
v = float(v)
vals.append(v)
except ValueError:
print("Error parsing line: %s" % (line,))
print(v)
return series_data
# }}}

Basic Python function to read LCModel COORD data : return is a dict containing:

  • ppm : points on the ppm scale
  • data : data to be fit, on the ppm scale
  • completeFit : the model fit, including baseline
  • baseline : the modelled baseline (not present if NOBASE is set, which is the default for mega-press and certain other modes)

In response to: https://forum.mrshub.org/t/questions-about-lcmodel-coordinate-files/1141

Note that this does not extract other parameters from the COORD file, and does not attempt to read individual metabolite fits (where NEACH is specified)

For MATLAB, consider:

I believe versions of the above may also be incorporated into Osprey https://github.com/schorschinho/osprey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment