|
| 1 | +from paths_cli.wizard.engines import engines |
| 2 | +from paths_cli.parsing.tools import custom_eval |
| 3 | +from paths_cli.wizard.load_from_ops import load_from_ops |
| 4 | +from paths_cli.wizard.load_from_ops import LABEL as _load_label |
| 5 | +from functools import partial |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +try: |
| 9 | + import mdtraj as md |
| 10 | +except ImportError: |
| 11 | + HAS_MDTRAJ = False |
| 12 | +else: |
| 13 | + HAS_MDTRAJ = True |
| 14 | + |
| 15 | +def mdtraj_atom_helper(wizard, user_input, n_atoms): |
| 16 | + wizard.say("You should specify atom indices enclosed in double " |
| 17 | + "brackets, e.g, [" + str(list(range(n_atoms))) + "]") |
| 18 | + # TODO: implement the following: |
| 19 | + # wizard.say("You can specify atoms either as atom indices (which count " |
| 20 | + # "from zero) or as atom labels of the format " |
| 21 | + # "CHAIN:RESIDUE-ATOM, e.g., '0:ALA1-CA' for the alpha carbon " |
| 22 | + # "of alanine 1 (this time counting from one, as in the PDB) " |
| 23 | + # "of the 0th chain in the topology. You can also use letters " |
| 24 | + # "for chain IDs, but note that A corresponds to the first " |
| 25 | + # "chain in your topology, even if its name in the PDB file " |
| 26 | + # "is B.") |
| 27 | + |
| 28 | +def _get_topology(wizard): |
| 29 | + from paths_cli.wizard.engines import engines |
| 30 | + topology = None |
| 31 | + while topology is None: |
| 32 | + if len(wizard.engines) == 0: |
| 33 | + # SHOULD NEVER GET HERE |
| 34 | + wizard.say("Hey, you need to define an MD engine before you " |
| 35 | + "create CVs that refer to it. Let's do that now!") |
| 36 | + engine = engines(wizard) |
| 37 | + wizard.register(engine, 'engine', 'engines') |
| 38 | + wizard.say("Now let's get back to defining your CV") |
| 39 | + elif len(wizard.engines) == 1: |
| 40 | + topology = list(wizard.engines.values())[0].topology |
| 41 | + else: |
| 42 | + engines = list(wizard.engines.keys) |
| 43 | + name = wizard.ask("You have defined multiple engines. Which one " |
| 44 | + "should I use to define your CV?", |
| 45 | + options=engines) |
| 46 | + topology = wizard.engines[name].topology |
| 47 | + |
| 48 | + return topology |
| 49 | + |
| 50 | +def _get_atom_indices(wizard, topology, cv_type): |
| 51 | + # TODO: move the logic here to parsing.tools |
| 52 | + n_atoms = {'distance': 2, |
| 53 | + 'angle': 3, |
| 54 | + 'dihedral': 4}[cv_type] |
| 55 | + arr = None |
| 56 | + helper = partial(mdtraj_atom_helper, n_atoms=n_atoms) |
| 57 | + while arr is None: |
| 58 | + atoms_str = wizard.ask("Which atoms do you want to measure the " |
| 59 | + "{cv_type} between?", helper=helper) |
| 60 | + try: |
| 61 | + indices = custom_eval(atoms_str) |
| 62 | + except Exception as e: |
| 63 | + wizard.exception(f"Sorry, I did't understand '{atoms_str}'", e) |
| 64 | + mdtraj_atom_helper(wizard, '?', n_atoms) |
| 65 | + continue |
| 66 | + |
| 67 | + try: |
| 68 | + arr = np.array(indices) |
| 69 | + if arr.dtype != int: |
| 70 | + raise TypeError("Input is not integers") |
| 71 | + if arr.shape != (1, n_atoms): |
| 72 | + # try to clean it up |
| 73 | + if len(arr.shape) == 1 and arr.shape[0] == n_atoms: |
| 74 | + arr.shape = (1, n_atoms) |
| 75 | + else: |
| 76 | + raise TypeError(f"Invalid input. Requires {n_atoms} " |
| 77 | + "atoms.") |
| 78 | + except Exception as e: |
| 79 | + wizard.exception(f"Sorry, I did't understand '{atoms_str}'", e) |
| 80 | + mdtraj_atom_helper(wizard, '?', n_atoms) |
| 81 | + arr = None |
| 82 | + continue |
| 83 | + |
| 84 | + if arr is not None: |
| 85 | + atom_names = [str(topology.mdtraj.atom(i)) for i in arr[0]] |
| 86 | + wizard.say("Using atoms: " + " ".join(atom_names)) |
| 87 | + return arr |
| 88 | + |
| 89 | +def _mdtraj_function_cv(wizard, cv_does_str, cv_user_prompt, func, |
| 90 | + kwarg_name, n_atoms): |
| 91 | + from openpathsampling.experimental.storage.collective_variables import \ |
| 92 | + MDTrajFunctionCV |
| 93 | + wizard.say("We'll make a CV that measures the distance between two " |
| 94 | + "atoms.") |
| 95 | + topology = _get_topology(wizard) |
| 96 | + indices = _get_atom_indices(wizard, topology, n_atoms=n_atoms) |
| 97 | + kwargs = {kwarg_name: indices} |
| 98 | + |
| 99 | + return MDTrajFunctionCV(func, topology, **kwargs) |
| 100 | + |
| 101 | +def distance(wizard): |
| 102 | + return _mdtraj_function_cv( |
| 103 | + wizard=wizard, |
| 104 | + cv_does_str="distance between two atoms", |
| 105 | + cv_user_prompt="measure the distance between", |
| 106 | + func=md.compute_distances, |
| 107 | + kwarg_name='atom_pairs', |
| 108 | + n_atoms=2 |
| 109 | + ) |
| 110 | + |
| 111 | +def angle(wizard): |
| 112 | + return _mdtraj_function_cv( |
| 113 | + wizard=wizard, |
| 114 | + cv_does_str="angle made by three atoms", |
| 115 | + cv_user_prompt="use to define the angle", |
| 116 | + func=md.compute_angles, |
| 117 | + kwarg_name='angle_indices', |
| 118 | + n_atoms=3 |
| 119 | + ) |
| 120 | + |
| 121 | +def dihedral(wizard): |
| 122 | + return _mdtraj_function_cv( |
| 123 | + wizard=wizard, |
| 124 | + cv_does_str="dihedral made by four atoms", |
| 125 | + cv_user_prompt="use to define the dihedral angle", |
| 126 | + func=md.compute_dihedrals, |
| 127 | + kwarg_name='indices', |
| 128 | + n_atoms=4 |
| 129 | + ) |
| 130 | + |
| 131 | +def rmsd(wizard): |
| 132 | + raise NotImplementedError("RMSD has not yet been implemented") |
| 133 | + |
| 134 | +def coordinate(wizard): |
| 135 | + atom_index = coord = None |
| 136 | + while atom_index is None: |
| 137 | + idx = wizard.ask("For which atom do you want to get the " |
| 138 | + "coordinate? (counting from zero)") |
| 139 | + try: |
| 140 | + atom_index = int(idx) |
| 141 | + except Exception as e: |
| 142 | + wizard.exception("Sorry, I can't make an atom index from " |
| 143 | + f"'{idx}'", e) |
| 144 | + |
| 145 | + while coord is None: |
| 146 | + xyz = wizard.ask("Which coordinate (x, y, or z) do you want for " |
| 147 | + f"atom {atom_index}?") |
| 148 | + try: |
| 149 | + coord = {'x': 0, 'y': 1, 'z': 2}[xyz] |
| 150 | + except KeyError as e: |
| 151 | + wizard.bad_input("Please select one of 'x', 'y', or 'z'") |
| 152 | + |
| 153 | +SUPPORTED_CVS = {} |
| 154 | + |
| 155 | +if HAS_MDTRAJ: |
| 156 | + SUPPORTED_CVS.update({ |
| 157 | + 'Distance': distance, |
| 158 | + 'Angle': angle, |
| 159 | + 'Dihedral': dihedral, |
| 160 | + 'RMSD': rmsd, |
| 161 | + }) |
| 162 | + |
| 163 | +SUPPORTED_CVS.update({ |
| 164 | + 'Coordinate': coordinate, |
| 165 | + 'Python script': ..., |
| 166 | + _load_label: partial(load_from_ops, |
| 167 | + store_name='cvs', |
| 168 | + obj_name='CV'), |
| 169 | +}) |
| 170 | + |
| 171 | +def cvs(wizard): |
| 172 | + wizard.say("You'll need to describe your system in terms of " |
| 173 | + "collective variables (CVs). We'll use these to define " |
| 174 | + "things like stable states.") |
| 175 | + cv_names = list(SUPPORTED_CVS.keys()) |
| 176 | + cv = None |
| 177 | + while cv is None: |
| 178 | + cv_type = wizard.ask_enumerate("What kind of CV do you want to " |
| 179 | + "define?", options=cv_names) |
| 180 | + cv = SUPPORTED_CVS[cv_type](wizard) |
| 181 | + return cv |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + from paths_cli.wizard.wizard import Wizard |
| 185 | + wiz = Wizard({}) |
| 186 | + cvs(wiz) |
0 commit comments