Coverage for src / molecular_simulations / build / build_calvados.py: 100%
75 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-13 01:26 -0600
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-13 01:26 -0600
1import os
2from calvados.cfg import Config, Job, Components
3import numpy as np
4import pip._vendor.tomli as tomllib # for 3.10
5import yaml
6from pathlib import Path
7from typing import Any, Union, Type, TypeVar
9_T = TypeVar('_T')
10OptPath = Union[Path, str, None]
11PathLike = Union[Path, str]
13class CGBuilder:
14 """
15 Build CALVADOS system from pdb.
16 Arguments:
17 path (PathLike): Path to directory to contain run.
18 input_pdb (PathLike): Path to input structure for simulation.
19 residues_file (PathLike): Path to CALVADOS residues forcefield file.
20 domains_file (PathLike): Path to file containing domain definitions.
21 box_dim (list[float]): Dimensions (x,y,z) for orthonormal PBC.
22 temp (float): Defaults to 310.0. Simulation temperature [K].
23 ion_conc (float): Defaults to 0.15. Ion concentration [M].
24 pH (float): Defaults to 7.4. pH.
25 topol (str): Defaults to center. Varies initial placement of protein chains.
26 dcd_freq (int): Defaults to 2000. Write frequency for output dcd.
27 n_steps (int): Defaults to 10_000_000. Total number of steps to integrate
28 during simulation (timestep of 10 fs).
29 platform (str): Defaults to CUDA. OpenMM platform for running simulations.
30 restart (str): Defaults to checkpoint. Style of OpenMM restart.
31 frestart (str): Defaults to restart.chk. Name of output restart files.
32 verbose (bool): Defaults to True. Verbosity.
33 molecule_type (str): Defaults to protein. Molecule type.
34 nmol (int): Defaults to 1. Total number of molecules.
35 restraint (bool): Defaults to True. Whether to use secondary structure
36 restraints.
37 charge_termini (str): Defaults to end-capped. Terminus patching. Allowed
38 options are N, C, both, or the default.
39 restraint_type (str): Defaults to harmonic. Type of restraint, either
40 harmonic or go.
41 use_com (bool): Defaults to True. Where to apply restraintsi (com or CA).
42 colabfold (int): Defaults to 0. Predicted beta-column confidence style.
43 Use 0 for AF and 1&2 for Colabfold.
44 k_harmonic (float) Defaults to 700.0. Harmonic spring constant to use for
45 restraints [kJ/mol].
47 Usage:
48 m = CGBuilder(*args)
49 m.build()
50 """
51 def __init__(self,
52 path: PathLike,
53 input_pdb: PathLike,
54 residues_file: PathLike,
55 domains_file: PathLike,
56 box_dim: list[float],
57 temp: float = 310.,
58 ion_conc: float = 0.15,
59 pH: float = 7.4,
60 topol : str = 'center',
61 dcd_freq: int = 2000,
62 n_steps: int = 10_000_000,
63 platform: str = 'CUDA',
64 restart: str = 'checkpoint',
65 frestart: str = 'restart.chk',
66 verbose: bool = True,
67 molecule_type: str = 'protein',
68 nmol: int = 1,
69 restraint: bool = True, # secondary structure restraints
70 charge_termini: str = 'end-capped',
71 restraint_type: str = 'harmonic', # harmonic or go
72 use_com: bool = True, # apply to COMs or CAs
73 colabfold: int = 0, # (EBI AF=0, Colabfold=1&2)
74 k_harmonic: float = 700.):
75 self.path = Path(path)
76 self.input_pdb = Path(input_pdb)
77 self.residues_file = Path(residues_file)
78 self.domains_file = Path(domains_file)
79 self.box_dim = box_dim
80 self.temp = temp
81 self.ion_conc = ion_conc
82 self.pH = pH
83 self.topol = topol
84 self.dcd_freq = dcd_freq
85 self.n_steps = n_steps
86 self.platform = platform
87 self.restart = restart
88 self.frestart = frestart
89 self.verbose = verbose
90 self.molecule_type = molecule_type
91 self.nmol = nmol
92 self.restraint = restraint
93 self.charge_termini = charge_termini
94 self.restraint_type = restraint_type
95 self.use_com = use_com
96 self.colabfold = colabfold
97 self.k_harmonic = k_harmonic
99 @classmethod
100 def from_dict(cls: Type[_T], cg_params: dict) -> _T:
101 """
102 Classmethod for creating CG simulation from toml config file.
103 Recommended method for instantiating CGBuilder.
104 """
105 conf_args = cg_params['config']
106 comp_args = cg_params['components']
108 path = Path(conf_args['path'])
109 input_pdb = conf_args['input_pdb']
110 residues_file = comp_args['residues_file']
111 domains_file = comp_args['domains_file']
112 box_dim = conf_args['box_dim']
113 temp = conf_args['temp']
114 ion_conc = conf_args['ion_conc']
115 pH = conf_args['pH']
116 topol = conf_args['topol']
117 dcd_freq = conf_args['dcd_freq']
118 n_steps = conf_args['n_steps']
119 platform = conf_args['platform']
120 restart = conf_args['restart']
121 frestart = conf_args['frestart']
122 verbose = conf_args['verbose']
124 molecule_type = comp_args['molecule_type']
125 nmol = comp_args['nmol']
126 restraint = comp_args['restraint']
127 charge_termini = comp_args['charge_termini']
128 restraint_type = comp_args['restraint_type']
129 use_com = comp_args['use_com']
130 colabfold = comp_args['colabfold']
131 k_harmonic = comp_args['k_harmonic']
133 return cls(path,
134 input_pdb,
135 residues_file,
136 domains_file,
137 box_dim = box_dim,
138 temp = temp,
139 ion_conc = ion_conc,
140 pH = pH,
141 topol = topol,
142 dcd_freq = dcd_freq,
143 n_steps = n_steps,
144 platform = platform,
145 restart = restart,
146 frestart = frestart,
147 verbose = verbose,
148 molecule_type = molecule_type,
149 nmol = nmol,
150 restraint = restraint,
151 charge_termini = charge_termini,
152 restraint_type = restraint_type,
153 use_com = use_com,
154 colabfold = colabfold,
155 k_harmonic = k_harmonic)
157 def build(self) -> None:
158 """
159 Prepares system for CALVADOS simulation.
160 Writes config and components yaml files.
161 """
162 self.write_config()
163 self.write_components()
165 def write_config(self) -> None:
166 """
167 Write CALVADOS config yaml.
168 """
169 config = Config(sysname = self.input_pdb.stem,
170 box = self.box_dim,
171 temp = self.temp,
172 ionic = self.ion_conc,
173 pH = self.pH,
174 topol = self.topol,
175 wfreq = self.dcd_freq,
176 steps = self.n_steps,
177 platform = self.platform,
178 restart = self.restart,
179 frestart = self.frestart,
180 verbose = self.verbose)
182 with open(f'{self.path}/config.yaml','w') as f:
183 yaml.dump(config.config, f)
185 def write_components(self) -> None:
186 """
187 Write CALVADOS components yaml.
188 """
189 components = Components(molecule_type = self.molecule_type,
190 nmol = self.nmol,
191 restraint = self.restraint,
192 charge_termini = self.charge_termini,
193 fresidues = str(self.residues_file),
194 fdomains = str(self.domains_file),
195 pdb_folder = str(self.input_pdb.parent),
196 restraint_type = self.restraint_type,
197 use_com = self.use_com,
198 colabfold = self.colabfold,
199 k_harmonic = self.k_harmonic)
200 components.add(name = self.input_pdb.stem)
202 with open(f'{self.path}/components.yaml','w') as f:
203 yaml.dump(components.components, f)