Skip to content

Commit 9b4ab45

Browse files
authored
Merge pull request #38 from calpolyccg/cli
Create CLI
2 parents ae47305 + 3e90d13 commit 9b4ab45

16 files changed

+250
-139
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# generated files
2+
psi.*.clean
3+
out.csv
24
/timer.dat
35
/resid_fixed.pdb
46
/resid.pdb
@@ -111,3 +113,7 @@ ENV/
111113
# profraw files from LLVM? Unclear exactly what triggers this
112114
# There are reports this comes from LLVM profiling, but also Xcode 9.
113115
*profraw
116+
117+
# MDSAPT-generated files
118+
timer.dat
119+
input.yaml

docs/index.rst

-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ For the poster we presented at ACS Spring 2022, `click here <./_static/mdsapt_po
2424
optimizer
2525
reader
2626
viewer
27-
scripts

docs/install.rst

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
Installation
22
============
33

4-
MD-SAPT can be installed by cloning the GitHub repository.
4+
MD-SAPT can be installed from the psi4 Conda repo like so:
5+
6+
.. code-block:: bash
7+
8+
conda install -c psi4/label/dev -c conda-forge mdsapt
9+
10+
Alternatively, it can be installed by cloning the GitHub repository.
511

612
.. code-block:: bash
713
814
git clone https://github.com/calpolyccg/MDSAPT.git
915
pip install ./MDSAPT
1016
11-
MD-SAPT can also be installed from conda.
17+
To ensure it's been installed correctly, run `mdsapt` or `python3 -m mdsapt`.
1218

13-
.. code-block:: bash
19+
.. code-block::
20+
Warning: importing 'simtk.openmm' is deprecated. Import 'openmm' instead.
21+
2022-03-30 09:32:50,071 mdsapt INFO MDSAPT 1.2.0 starting
22+
2022-03-30 09:32:50,071 mdsapt INFO Copyright (c) 2021 Alia Lescoulie, Astrid Yu, and Ashley Ringer McDonald
23+
2022-03-30 09:32:50,071 mdsapt INFO Released under GPLv3 License
24+
Usage: python -m mdsapt [OPTIONS] COMMAND [ARGS]...
25+
26+
MDSAPT - Molecular Dynamics Symmetry-Adapted Perturbation Theory
27+
28+
This command-line interface lets you easily do common MDSAPT-related tasks.
1429
15-
conda install -c psi4 MDSAPT
30+
Options:
31+
--help Show this message and exit.
1632
33+
Commands:
34+
generate Generate a template input file at filename.
35+
run Run a SAPT calculation using the configuration in in_file.

docs/quick.rst

+78-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
Quickstart Guide
22
================
33

4-
MD-SAPT simplifies for the calculation of SAPT interaction energies from MD
5-
trajectories. Running it just requires MD simulation files.
4+
MD-SAPT simplifies the calculation of SAPT interaction energies between selected residue pairs in MD trajectories. Running it just requires MD simulation files.
65

7-
Basic work flow
8-
===============
6+
Prerequisites
7+
_____________
98

10-
MD-SAPT can be run as a script or in a notebook. It gives the SAPT energy
11-
between the specified residue pairs.
9+
Ensure that you have the following things set up:
1210

13-
Setup Process
14-
_____________
11+
- You have existing MD trajectory and topology files in any form that `MDAnalysis <https://mdanalysis.readthedocs.io/en/latest/`_ supports
12+
- You have already installed MDSAPT, following the :installation guide:`install`
13+
14+
.. note:
15+
If your `PATH` environment variable is not set up to point to installed Python modules, then invoking `mdsapt` directly, as shown in this guide, may not work. In that case, try running `python3 -m mdsapt` instead.
1516
16-
The following steps describe how to set up the input yaml file
17+
Generating an input file
18+
________________________
1719

18-
1. Use the `mdsapt_get_runinput.py` script to generate a blank input file
20+
The following steps describe how to set up the input YAML file.
21+
22+
1. Run `mdsapt generate [filename]` to generate a blank Trajectory SAPT input file at the given filename. Don't worry, it will not overwrite any files unless you explicitly provide the `-f` flag.
1923

2024
2. Specify the MD file locations in the input file
2125

@@ -25,21 +29,80 @@ The following steps describe how to set up the input yaml file
2529

2630
5. Specify trajectory, optimization, and SAPT settings
2731

32+
Here is an example of a filled-out YAML file:
33+
34+
.. code-block:: yaml
35+
topology_path: 'testtop.psf'
36+
trajectory_paths:
37+
- 'testtraj.dcd'
38+
selection_resid_num:
39+
- 11
40+
- 199
41+
int_pairs:
42+
# Place pair of selections defined above in a list of lists
43+
- [11, 199]
44+
trajectory_settings:
45+
start: 0
46+
stop: 98
47+
step: 1
48+
system_settings:
49+
ncpus: '12'
50+
memory: '12GB'
51+
time: '24:00:00'
52+
opt_settings:
53+
pH: 7
54+
sapt_settings:
55+
method: 'sapt0'
56+
basis: 'jun-cc-pvdz'
57+
settings:
58+
reference: 'rhf'
59+
save_psi4_output: true
60+
2861
Running SAPT
2962
____________
3063

31-
With the input done MD-SAPT is ready to be run. The settings are read using :class:`mdsapt.reader.InputReader` which is then passed into :class:`mdsapt.reader.Optimizer` which handles preparing residues. Finally :class:`mdsapt.reader.TrajectorySAPT` is used to run SAPT over the MD data. The results are stored in a :class:`Pandas.DataFrame`.
64+
Using the `mdsapt` CLI
65+
^^^^^^^^^^^^^^^^^^^^^^
66+
67+
This mode is suited for:
68+
69+
- running on an HPC cluster
70+
- running a simple input file
71+
72+
With the input done MD-SAPT is ready to be run. Simply execute
73+
74+
.. code-block:: bash
75+
mdsapt run [filename] [output]
76+
77+
and it will run SAPT on your trajectory using the parameters specified in your input file.
78+
79+
Using the `mdsapt` Python library
80+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
81+
82+
This mode is suited for:
83+
84+
- using MD-SAPT in a notebook
85+
- using MD-SAPT in your own library or applications
86+
87+
The classes involved are as follows:
88+
89+
- The settings are read using :class:`mdsapt.reader.InputReader`
90+
- The `InputReader` is then passed into :class:`mdsapt.reader.Optimizer` which handles preparing residues.
91+
- Finally, :class:`mdsapt.reader.TrajectorySAPT` is used to run SAPT over the MD data.
92+
- The results are stored in a :class:`Pandas.DataFrame` which can be accessed under the `TrajectorySAPT.results` property.
93+
94+
Here is some code demonstrating it:
3295

3396
.. code-block:: Python
3497
3598
import mdsapt
3699
37-
Settings = mdsapt.InputReader('runinput.yaml')
38100
101+
Settings = mdsapt.InputReader('runinput.yaml')
39102
Opt = mdsapt.Optimizer(Settings)
40-
41103
SAPT_run = mdsapt.TrajectorySAPT(Settings, Opt)
42-
43104
SAPT_run.run(Settings.start, Settings.stop, Settings.step)
105+
SAPT_run.results.to_csv('results.csv')
106+
107+
See also `the Binder demo <https://mybinder.org/v2/gh/calpolyccg/MDSAPT_demo/master?labpath=MD-SAPT_demo.ipynb>`_ for a bigger example.
44108

45-
SAPT_run.results.to_csv('results.csv')

docs/scripts.rst

-23
This file was deleted.

environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
- psi4>=1.6.1,<1.7
1111

1212
- mdanalysis>=2.2.0,<2.3
13+
- click
1314
- numpy
1415
- openmm
1516
- pandas

mdsapt/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from .config import Config, load_from_yaml_file
99
from .sapt import TrajectorySAPT, DockingSAPT
1010

11+
from .cli import cli
12+
1113
# Handle versioneer
1214
from ._version import get_versions
1315
versions = get_versions()

mdsapt/__main__.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Main entrypoint for the CLI.
4+
"""
5+
6+
# Note that we import directly from the CLI instead of the full package. This is an optimization
7+
# since some CLI tasks do not need the entire package.
8+
from .cli import cli
9+
10+
11+
if __name__ == '__main__':
12+
cli()

mdsapt/cli.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import logging
2+
import os
3+
import sys
4+
5+
import click
6+
from mdsapt.config import RangeFrameSelection
7+
8+
# Note that we do not import MDSAPT. This is a speed optimization; it is imported later.
9+
10+
11+
logger = logging.getLogger(__name__)
12+
13+
_dir_path = os.path.dirname(os.path.realpath(__file__))
14+
"""Location of the mdsapt package to be used in resolving templates."""
15+
16+
17+
@click.group()
18+
def cli():
19+
"""
20+
MDSAPT - Molecular Dynamics Symmetry-Adapted Perturbation Theory, by Alia Lescoulie, Astrid Yu, and Ashley Ringer McDonald.
21+
22+
This command-line interface lets you easily do common MDSAPT-related tasks.
23+
"""
24+
25+
26+
@cli.command()
27+
@click.argument(
28+
'filename',
29+
default='input.yaml',
30+
)
31+
@click.option(
32+
'-t', '--template', 'template',
33+
help="Template to generate from. By default, trajectory.",
34+
type=click.Choice(['trajectory', 'docking'], case_sensitive=False),
35+
default='trajectory',
36+
)
37+
@click.option(
38+
'-f', '--force',
39+
help="If provided, overwrites existing files.",
40+
is_flag=True,
41+
)
42+
def generate(filename: str, template: str, force: bool):
43+
"""
44+
Generate a template input file at filename.
45+
"""
46+
ensure_safe_to_overwrite(filename, force)
47+
48+
# TODO: make a wizard for these templates
49+
template_path = os.path.join(_dir_path, 'data', f'{template}_template.yaml')
50+
51+
with open(template_path, 'r') as template:
52+
template_data = template.read()
53+
with open(filename, 'w') as new_file:
54+
new_file.write(template_data)
55+
56+
logger.info(f'Generated template input file {filename}')
57+
58+
59+
@cli.command()
60+
@click.argument(
61+
'in_file',
62+
default='input.yaml',
63+
)
64+
@click.argument(
65+
'out_file',
66+
default='out.csv',
67+
)
68+
@click.option(
69+
'-f', '--force',
70+
help="If provided, overwrites existing files.",
71+
is_flag=True,
72+
)
73+
def run(in_file: str, out_file: str, force: bool):
74+
"""
75+
Run a SAPT calculation using the configuration in in_file. Outputs will be written to
76+
out_file.
77+
"""
78+
import mdsapt
79+
80+
ensure_safe_to_overwrite(out_file, force)
81+
82+
config = mdsapt.load_from_yaml_file(in_file)
83+
if isinstance(config.analysis, mdsapt.config.TrajectoryAnalysisConfig):
84+
sapt = mdsapt.TrajectorySAPT(config)
85+
frames = config.analysis.frames
86+
sapt.run(frames.start, frames.stop, frames.step)
87+
elif isinstance(config.analysis, mdsapt.config.DockingAnalysisConfig):
88+
sapt = mdsapt.DockingSAPT(config)
89+
sapt.run()
90+
91+
logger.info('saving results to CSV')
92+
sapt.results.to_csv(out_file)
93+
94+
95+
def ensure_safe_to_overwrite(path: str, force: bool):
96+
"""
97+
Helper function to ensure that it's safe to overwrite the given file, and
98+
halts the program if not.
99+
"""
100+
if not os.path.exists(path):
101+
return
102+
103+
if force:
104+
logger.warning("will overwrite existing CSV %s", path)
105+
return
106+
107+
logger.error("Halting, file already exists: %s", path)
108+
logger.error("If you want to overwrite that file, add the -f flag")
109+
sys.exit(-1)

0 commit comments

Comments
 (0)