Skip to content

Commit

Permalink
Added static getData method and wrapper function. [ref #193]
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Apr 27, 2021
1 parent e7ce9a3 commit 5edaa64
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
2 changes: 2 additions & 0 deletions python/BioSimSpace/FreeEnergy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
:toctree: generated/
analyse
getData
"""

from ._free_energy import analyse
from ._free_energy import getData
from ._binding import *
from ._solvation import *
61 changes: 52 additions & 9 deletions python/BioSimSpace/FreeEnergy/_free_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
__author__ = "Lester Hedges"
__email__ = "[email protected]"

__all__ = ["FreeEnergy", "analyse"]
__all__ = ["FreeEnergy", "analyse", "getData"]

from collections import OrderedDict as _OrderedDict
from glob import glob as _glob
Expand Down Expand Up @@ -181,7 +181,8 @@ def run(self, serial=True):

self._runner.startAll(serial=serial)

def getData(self, name="data", file_link=False):
@classmethod
def getData(cls, name="data", file_link=False, work_dir=None):
"""Return a link to a zip file containing the data files required for
post-simulation analysis.
Expand All @@ -194,13 +195,26 @@ def getData(self, name="data", file_link=False):
file_link : bool
Whether to return a FileLink when working in Jupyter.
work_dir : str
The working directory for the simulation.
Returns
-------
ouput : str, IPython.display.FileLink
A path, or file link, to an archive of the process input.
"""

if work_dir is None and cls._work_dir is None:
raise ValueError("'work_dir' must be set!")
elif work_dir is None:
work_dir = cls._work_dir
else:
if type(work_dir) is not str:
raise TypeError("'work_dir' must be of type 'str'.")
if not _os.path.isdir(work_dir):
raise ValueError("'work_dir' doesn't exist!")

if type(name) is not str:
raise TypeError("'name' must be of type 'str'")

Expand All @@ -211,15 +225,20 @@ def getData(self, name="data", file_link=False):
cwd = _os.getcwd()

# Change into the working directory.
with _cd(self._work_dir):
# Glob all of the analysis files for the engine.
if self._engine == "SOMD":
files = glob("*/*/gradients.dat")
elif self._engine == "GROMACS":
files = glob("*/*/gromacs.xvg")
with _cd(work_dir):
# Glob all of the analysis files.

# First try SOMD data.
files = _glob("*/*/gradients.dat")

if len(files) == 0:
files = _glob("*/*/gromacs.xvg")

if len(files) == 0:
raise ValueError(f"Couldn't find any analysis files in '{work_dir}'")

# Write to the zip file.
with zipfile.ZipFile(cwd + f"/{zipname}", "w") as zip:
with _zipfile.ZipFile(cwd + f"/{zipname}", "w") as zip:
for file in files:
zip.write(file)

Expand Down Expand Up @@ -846,3 +865,27 @@ def analyse(work_dir):
"""

return FreeEnergy.analyse(work_dir)

def getData(name="data", file_link=False, work_dir=None):
"""Return a link to a zip file containing the data files required for
post-simulation analysis.
Parameters
----------
name : str
The name of the zip file.
file_link : bool
Whether to return a FileLink when working in Jupyter.
work_dir : str
The working directory for the simulation.
Returns
-------
ouput : str, IPython.display.FileLink
A path, or file link, to an archive of the process input.
"""
return FreeEnergy.getData(name=name, file_link=file_link, work_dir=work_dir)

0 comments on commit 5edaa64

Please sign in to comment.