Becksteinlab / GromacsWrapper

GromacsWrapper wraps system calls to GROMACS tools into thin Python classes (GROMACS 4.6.5 - 2024 supported).
https://gromacswrapper.readthedocs.org
GNU General Public License v3.0
169 stars 53 forks source link

Can't access xvg metadata #13

Closed bk322 closed 2 years ago

bk322 commented 11 years ago

Hi Oliver, today I tried to plot xvg data in matplotlib. I read it with GromacsWrapper -- but can't access xvg metadata:

@    title "Gromacs Energies"
@    xaxis  label "Time (ps)"
@    yaxis  label "(kJ/mol)"
@TYPE xy
@ view 0.15, 0.15, 0.75, 0.85
@ legend on
@ legend box on
@ legend loctype view
@ legend 0.78, 0.8
@ legend length 2
@ s0 legend "Potential"

I need xaxis, yaxis and legend in particular:

>>> from gromacs import fileformats
>>> Ener = fileformats.xvg.XVG('energy.xvg')
>>> Ener.metadata
{}
>>> Ener.names
[]

I'm using bleeding edge GromacsWrapper, I get a moderate issue on startup:

/home/boris/.local/lib/python2.7/site-packages/GromacsWrapper-0.3.1-py2.7.egg/gromacs/__init__.py:324: GromacsImportWarning: Some Gromacs commands were NOT found; maybe source GMXRC first? The following are missing:
['a_gridcalc', 'a_ri3dc', 'g_count', 'g_flux', 'g_pme_error', 'g_ri3dc']

I don't think it is relevant.

bk322 commented 11 years ago

I'm using this:

Legend = []
fp = open(args.xvg)
for i, line in enumerate(fp):
    if line.startswith('#') or line.startswith('@'):
        if line.startswith('@'):
            if re.search('xaxis', line):
                xaxis = re.sub('.*"(.+)".*', r'\1', line.strip())
            elif re.search('yaxis', line):
                yaxis = re.sub('.*"(.+)".*', r'\1', line.strip())
            elif re.match(r'@ s', line):
                Legend.append(re.sub(r'.*"(.+)".*', r'\1', line.strip()))
    else:
        break
fp.close()
orbeckst commented 11 years ago

The issue on startup can be ignored; it just means that you don't have a number of gromacs tools installed that I use (and which I put in the default config file). You could remove them from ~/.gromacswrapper.cfg without harm.

The XVG class does not parse metadata at the moment. We could use code similar to what you posted. If you want to fork GromacsWrapper and implement code like the above I would consider merging your changes into the main development line.

ostueker commented 2 years ago

It seems the XVG reader has finally learned to read metadata, names and axis-labels from Gromacs-Generated XVG files. :tada:

However the behaviour is somewhat inconsistent. See for example:

In [1]: import gromacs.formats
   ...: print(gromacs.__version__)
0.8.2

In [2]: xvg = gromacs.formats.XVG(filename='examples/gmx_energy_temp.xvg')

In [3]: print(xvg.names)
   ...: print(xvg.metadata)
[]
{}

In [4]: print(xvg.array)
[[  0.         1.         2.         3.         4.         5.      ]
 [300.405334 301.206299 300.53598  301.529816 298.039734 300.182587]]

In [5]: print(xvg.names)
   ...: print(xvg.metadata)
['Temperature']
{'legend': ['on', 'box on', 'loctype view', '0.78, 0.8', 'length 2']}

In [6]: 

The first time I access the names and metadata attributes, they are empty, but once I have accessed the array once, they are suddenly populated. :thinking:

Cheers, ostueker (Oliver S.)

orbeckst commented 2 years ago

The XVGReader does lazy-loading, it only actually reads the file when the array is created.

That's admittedly a bit confusing, especially when it comes to the metadata.

Btw, I probably should have closed this issue when PR #18 was merged, which added some metadata parsing (although not all).

orbeckst commented 2 years ago

At least some of the metadata are read (added in PR #18). If anything else is missing that you'd really want then please open a new issue. Thank you!