-
Notifications
You must be signed in to change notification settings - Fork 4
/
common_structures.py
952 lines (857 loc) · 34.4 KB
/
common_structures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
"""
This file contains common starting structures which can be mixed into datasets, results and factories.
"""
import copy
import getpass
import re
from datetime import date, datetime
from enum import Enum
from typing import (
TYPE_CHECKING,
AbstractSet,
Any,
ClassVar,
Dict,
List,
Mapping,
Optional,
Set,
Tuple,
Union,
)
import numpy as np
from openff.toolkit.topology import Molecule
from qcelemental import constants
from qcelemental.models.common_models import Model
from qcelemental.models.results import WavefunctionProtocolEnum
from qcportal.singlepoint import SinglepointDriver
from openff.qcsubmit.exceptions import (
DatasetInputError,
PCMSettingError,
QCSpecificationError,
)
from openff.qcsubmit.utils.smirnoff import split_openff_molecule
try:
from pydantic.v1 import (
BaseModel,
Field,
HttpUrl,
PositiveInt,
StrictBool,
StrictFloat,
StrictInt,
StrictStr,
constr,
validator,
)
except ImportError:
from pydantic import (
BaseModel,
Field,
HttpUrl,
PositiveInt,
StrictBool,
StrictFloat,
StrictInt,
StrictStr,
constr,
validator,
)
if TYPE_CHECKING:
DictStrAny = Dict[str, Any]
IntStr = Union[int, str]
AbstractSetIntStr = AbstractSet[IntStr]
DictIntStrAny = Dict[IntStr, Any]
MappingIntStrAny = Mapping[IntStr, Any]
if TYPE_CHECKING:
from pydantic import AbstractSetIntStr
class DatasetConfig(BaseModel):
"""
The basic configurations for all datasets.
"""
class Config:
arbitrary_types_allowed: bool = True
allow_mutation: bool = True
validate_assignment: bool = True
json_encoders: Dict[str, Any] = {
np.ndarray: lambda v: v.flatten().tolist(),
Enum: lambda v: v.value,
}
extra: True
class ResultsConfig(BaseModel):
"""
A basic config class for results structures.
"""
class Config:
arbitrary_types_allowed: bool = True
allow_mutation: bool = False
json_encoders: Dict[str, Any] = {
np.ndarray: lambda v: v.flatten().tolist(),
Enum: lambda v: v.value,
}
class SCFProperties(str, Enum):
"""
The type of SCF property that should be extracted from a single point calculation.
"""
Dipole = "dipole"
Quadrupole = "quadrupole"
MullikenCharges = "mulliken_charges"
LowdinCharges = "lowdin_charges"
WibergLowdinIndices = "wiberg_lowdin_indices"
MayerIndices = "mayer_indices"
MBISCharges = "mbis_charges"
@classmethod
def _missing_(cls, value):
"""
overwrite the missing method to handle properties with incorrect capitalization.
"""
for member in cls.__members__.values():
if member._value_ == value.lower():
return member
raise QCSpecificationError(
f"{value} is not a valid {cls.__name__} please chose from {cls.__members__.values()}"
)
DefaultProperties = (
SCFProperties.Dipole,
SCFProperties.Quadrupole,
SCFProperties.WibergLowdinIndices,
SCFProperties.MayerIndices,
)
class ComponentProperties(BaseModel):
"""
The workflow properties class which controls if the component can be used in multiprocessing or if the component
produces duplicates.
"""
process_parallel: bool = Field(
...,
description="If the component can safely be ran in parallel `True` or not `False`.",
)
produces_duplicates: bool = Field(
...,
description="If the component is expected to produce duplicate molecules `True` or not `False`.",
)
class Config:
allow_mutation: bool = False
extra = "forbid"
class TDSettings(DatasetConfig):
"""
A replacement of the TDKeywords class in the QCFractal which drops the dihedrals field as this is moved up the model.
The settings here overwrite the global dataset and allow the user to have control over the individual scans.
"""
grid_spacing: Optional[List[int]] = Field(
None, description="List of grid spacings for the dihedral scan in degrees."
)
dihedral_ranges: Optional[List[Tuple[int, int]]] = Field(
None,
description="A list of the dihedral scan limits of the form (lower, upper)",
)
energy_decrease_thresh: Optional[float] = Field(
None,
description="The threshold of the smallest energy decrease amount to trigger activating optimizations from "
"grid point.",
)
energy_upper_limit: Optional[float] = Field(
None,
description="The threshold if the energy of a grid point that is higher than the current global minimum, to "
"start new optimizations, in unit of a.u. I.e. if energy_upper_limit = 0.05, current global "
"minimum energy is -9.9 , then a new task starting with energy -9.8 will be skipped.",
)
additional_keywords: Dict[str, Any] = Field(
{},
description="Additional keywords to add to the torsiondrive's optimization runs",
)
class PCMSettings(ResultsConfig):
"""
A class to handle PCM settings which can be used with PSi4.
"""
units: str = Field(
...,
description="The units used in the input options atomic units are used by default.",
)
codata: int = Field(
2010,
description="The set of fundamental physical constants to be used in the module.",
)
cavity_Type: str = Field(
"GePol",
description="Completely specifies type of molecular surface and its discretization.",
)
cavity_Area: float = Field(
0.3,
description="Average area (weight) of the surface partition for the GePol cavity in the specified units. By default this is in AU.",
)
cavity_Scaling: bool = Field(
True,
description="If true, the radii for the spheres will be scaled by 1.2. For finer control on the scaling factor for each sphere, select explicit creation mode.",
)
cavity_RadiiSet: str = Field(
"Bondi",
description="Select set of atomic radii to be used. Currently Bondi-Mantina Bondi, UFF and Allinger’s MM3 sets available. Radii in Allinger’s MM3 set are obtained by dividing the value in the original paper by 1.2, as done in the ADF COSMO implementation We advise to turn off scaling of the radii by 1.2 when using this set.",
)
cavity_MinRadius: float = Field(
100,
description="Minimal radius for additional spheres not centered on atoms. An arbitrarily big value is equivalent to switching off the use of added spheres, which is the default in AU.",
)
cavity_Mode: str = Field(
"Implicit",
description="How to create the list of spheres for the generation of the molecular surface.",
)
medium_SolverType: str = Field(
"IEFPCM",
description="Type of solver to be used. All solvers are based on the Integral Equation Formulation of the Polarizable Continuum Model.",
)
medium_Nonequilibrium: bool = Field(
False,
description="Initializes an additional solver using the dynamic permittivity. To be used in response calculations.",
)
medium_Solvent: str = Field(
...,
description="Specification of the dielectric medium outside the cavity. Note this will always be converted to the molecular formula to aid parsing via PCM.",
)
medium_MatrixSymm: bool = Field(
True,
description="If True, the PCM matrix obtained by the IEFPCM collocation solver is symmetrized.",
)
medium_Correction: float = Field(
0.0,
description="Correction, k for the apparent surface charge scaling factor in the CPCM solver.",
ge=0,
)
medium_DiagonalScaling: float = Field(
1.07,
description="Scaling factor for diagonal of collocation matrices, values commonly used in the literature are 1.07 and 1.0694.",
ge=0,
)
medium_ProbeRadius: float = Field(
1.0,
description="Radius of the spherical probe approximating a solvent molecule. Used for generating the solvent-excluded surface (SES) or an approximation of it. Overridden by the built-in value for the chosen solvent. Default in AU.",
)
_solvents: ClassVar[Dict[str, str]] = {
"water": "H2O",
"dimethylsulfoxide": "DMSO",
"nitromethane": "CH3NO2",
"acetonitrile": "CH3CN",
"methanol": "CH3OH",
"ethanol": "CH3CH2OH",
"1,2-dichloroethane": "C2H4CL2",
"methylenechloride": "CH2CL2",
"tetrahydrofurane": "THF",
"aniline": "C6H5NH2",
"chlorobenzene": "C6H5CL",
"chloroform": "CHCL3",
"toluene": "C6H5CH3",
"1,4-dioxane": "C4H8O2",
"carbon tetrachloride": "CCL4",
"cyclohexane": "C6H12",
"n-heptane": "C7H16",
}
@validator("units")
def _check_units(cls, unit: str) -> str:
"""
Make sure the units are a valid option.
"""
units = ["au", "angstrom"]
if unit.lower() not in units:
raise PCMSettingError(f"{unit} is not valid only {units} are supported.")
return unit
@validator("codata")
def _check_codata(cls, codata: int) -> int:
"""
Make sure the codata is a valid option in PCM.
"""
datasets = [2010, 2006, 2002, 1998]
if codata not in datasets:
raise PCMSettingError(
f"{codata} is not valid only {datasets} are supported."
)
return codata
@validator("cavity_Type")
def _check_cavity_type(cls, cavity: str) -> str:
"""
Make sure the cavity type is GePol as this is the only kind supported.
"""
if cavity.lower() != "gepol":
raise PCMSettingError(
f"{cavity} is not a supported type only GePol is available."
)
return "GePol"
@validator("cavity_RadiiSet")
def _check_radii_set(cls, radiiset: str) -> str:
"""
Make sure a valid radii set is passed.
"""
radiisets = ["bondi", "uff", "allinger"]
if radiiset.lower() not in radiisets:
raise PCMSettingError(
f"{radiiset} is not a supported set please chose from {radiisets}"
)
return radiiset
@validator("cavity_Mode")
def _check_cavity_mode(cls, cavity: str) -> str:
"""
Make sure that a valid cavity mode is passed.
"""
if cavity.lower() != "implicit":
raise PCMSettingError(
f"{cavity} is not supported via QCSubmit only implicit can be used for collection based calculations."
)
return "Implicit"
@validator("medium_SolverType")
def _check_solver(cls, solver: str) -> str:
"""
Make sure valid solver is passed.
"""
solvers = ["IEFPCM", "CPCM"]
if solver.upper() not in solvers:
raise PCMSettingError(f"{solver} not supported please chose from {solvers}")
return solver.upper()
@validator("medium_Solvent")
def _check_solvent(cls, solvent: str) -> str:
"""
Make sure that a valid solvent from the list of supported values is passed.
"""
solvent_formula = cls._solvents.get(solvent.lower(), solvent.upper())
if solvent_formula not in cls._solvents.values():
raise PCMSettingError(
f"The solvent {solvent} is not supported please chose from the following solvents or formulas {cls._solvents.items()}"
)
return solvent_formula
def __init__(self, **kwargs):
"""
Fully validate the model making sure options are compatible and convert any defaults to the give unit system.
"""
# convert all inputs to the correct units
units = kwargs.get("units", None)
if units is not None and units.lower() == "angstrom":
# we need to convert the default values only which have length scales
if "medium_ProbeRadius" not in kwargs:
medium_ProbeRadius = (
self.__fields__["medium_ProbeRadius"].default
* constants.bohr2angstroms
)
kwargs["medium_ProbeRadius"] = medium_ProbeRadius
if "cavity_MinRadius" not in kwargs:
cavity_MinRadius = (
self.__fields__["cavity_MinRadius"].default
* constants.bohr2angstroms
)
kwargs["cavity_MinRadius"] = cavity_MinRadius
if "cavity_Area" not in kwargs:
cavity_Area = (
self.__fields__["cavity_Area"].default * constants.bohr2angstroms**2
)
kwargs["cavity_Area"] = cavity_Area
super(PCMSettings, self).__init__(**kwargs)
def to_string(self) -> str:
"""
Generate the formated PCM settings string which can be ingested by psi4 via the qcschema interface.
"""
# format the medium keywords
medium_str, cavity_str = "", ""
for prop in self.__fields__.keys():
if "medium" in prop:
medium_str += f"\n {prop[7:]} = {getattr(self, prop)}"
elif "cavity" in prop:
cavity_str += f"\n {prop[7:]} = {getattr(self, prop)}"
# format the cavity keywords
pcm_string = f"""
Units = {self.units}
CODATA = {self.codata}
Medium {{{medium_str
}}}
Cavity {{{cavity_str}}}"""
return pcm_string
class QCSpec(ResultsConfig):
method: constr(strip_whitespace=True) = Field(
"B3LYP-D3BJ",
description="The name of the computational model used to execute the calculation. This could be the QC method or the forcefield name.",
)
basis: Optional[constr(strip_whitespace=True)] = Field(
"DZVP",
description="The name of the basis that should be used with the given method, outside of QC this can be the parameterization ie antechamber or None.",
)
program: constr(strip_whitespace=True) = Field(
"psi4",
description="The name of the program that will be used to perform the calculation.",
)
spec_name: constr(strip_whitespace=True) = Field(
"default",
description="The name the specification will be stored under in QCArchive.",
)
spec_description: str = Field(
"Standard OpenFF optimization quantum chemistry specification.",
description="The description of the specification which will be stored in QCArchive.",
)
store_wavefunction: WavefunctionProtocolEnum = Field(
WavefunctionProtocolEnum.none,
description="The level of wavefunction detail that should be saved in QCArchive. Note that this is done for every calculation and should not be used with optimizations.",
)
implicit_solvent: Optional[PCMSettings] = Field(
None,
description="If PCM is to be used with psi4 this is the full description of the settings that should be used.",
)
maxiter: PositiveInt = Field(
200,
description="The maximum number of SCF iterations in QM calculations this will be ignored by programs where this does not make sense.",
)
scf_properties: Optional[List[SCFProperties]] = Field(
[
SCFProperties.Dipole,
SCFProperties.Quadrupole,
SCFProperties.WibergLowdinIndices,
SCFProperties.MayerIndices,
],
description="The SCF properties which should be extracted after every single point calculation.",
)
keywords: Dict[
str, Union[StrictStr, StrictInt, StrictFloat, StrictBool, List[StrictFloat]]
] = Field(
{},
# None,
description="An optional set of program specific computational keywords that "
"should be passed to the program. These may include, for example, DFT grid "
"settings.",
)
def __init__(
self,
method: constr(strip_whitespace=True) = "B3LYP-D3BJ",
basis: Optional[constr(strip_whitespace=True)] = "DZVP",
program: constr(strip_whitespace=True) = "psi4",
spec_name: constr(strip_whitespace=True) = "default",
spec_description: str = "Standard OpenFF optimization quantum chemistry specification.",
store_wavefunction: WavefunctionProtocolEnum = WavefunctionProtocolEnum.none,
implicit_solvent: Optional[PCMSettings] = None,
maxiter: PositiveInt = 200,
scf_properties: List[SCFProperties] = DefaultProperties,
keywords: Optional[
Dict[
str,
Union[StrictStr, StrictInt, StrictFloat, StrictBool, List[StrictFloat]],
]
] = None,
):
"""
Validate the combination of method, basis and program.
"""
from openff.toolkit.typing.engines.smirnoff import get_available_force_fields
try:
from openmmforcefields.generators.template_generators import (
GAFFTemplateGenerator,
)
gaff_forcefields = GAFFTemplateGenerator.INSTALLED_FORCEFIELDS
except ModuleNotFoundError:
gaff_forcefields = [
"gaff-1.4",
"gaff-1.8",
"gaff-1.81",
"gaff-2.1",
"gaff-2.11",
]
# set up the valid method basis and program combinations
ani_methods = {"ani1x", "ani1ccx", "ani2x"}
openff_forcefields = list(
ff.split(".offxml")[0].lower() for ff in get_available_force_fields()
)
openmm_forcefields = {
"smirnoff": openff_forcefields,
"antechamber": gaff_forcefields,
}
xtb_methods = {
"gfn0-xtb",
"gfn0xtb",
"gfn1-xtb",
"gfn1xtb",
"gfn2-xtb",
"gfn2xtb",
"gfn-ff",
"gfnff",
}
rdkit_methods = {"uff", "mmff94", "mmff94s"}
settings = {
"openmm": openmm_forcefields,
"torchani": {None: ani_methods},
"xtb": {None: xtb_methods},
"rdkit": {None: rdkit_methods},
}
if program.lower() in settings:
# make sure PCM is not set
if implicit_solvent is not None:
raise QCSpecificationError(
"PCM can only be used with PSI4 please set implicit solvent to None."
)
# we need to make sure it is valid in the above list
program_settings = settings.get(program.lower(), None)
if program_settings is None:
raise QCSpecificationError(
f"The program {program.lower()} is not supported please use one of the following {settings.keys()}"
)
allowed_methods = program_settings.get(basis, None)
if allowed_methods is None:
raise QCSpecificationError(
f"The Basis {basis} is not supported for the program {program}, chose from {program_settings.keys()}"
)
# now we need to check the methods
# strip the offxml if present
method = method.split(".offxml")[0].lower()
if method not in allowed_methods:
raise QCSpecificationError(
f"The method {method} is not supported for the program {program} with basis {basis}, please chose from {allowed_methods}"
)
if keywords is None:
keywords = {}
super().__init__(
method=method,
basis=basis,
program=program.lower(),
spec_name=spec_name,
spec_description=spec_description,
store_wavefunction=store_wavefunction,
implicit_solvent=implicit_solvent,
maxiter=maxiter,
scf_properties=scf_properties,
keywords=keywords,
)
def dict(
self,
*,
include: Union["AbstractSetIntStr", "MappingIntStrAny"] = None,
exclude: Union["AbstractSetIntStr", "MappingIntStrAny"] = None,
by_alias: bool = False,
skip_defaults: bool = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> "DictStrAny":
data = super().dict(
include=include,
exclude=exclude,
by_alias=by_alias,
skip_defaults=skip_defaults,
exclude_defaults=exclude_defaults,
exclude_unset=exclude_unset,
exclude_none=exclude_none,
)
if "store_wavefunction" in data:
data["store_wavefunction"] = self.store_wavefunction.value
if "scf_properties" in data:
data["scf_properties"] = [prop.value for prop in self.scf_properties]
return data
@property
def qc_model(self) -> Model:
"""Return the qcelmental schema for this method and basis."""
return Model(method=self.method, basis=self.basis)
@property
def qc_keywords(self) -> Dict[str, Any]:
"""
Return the formatted keywords for this calculation.
"""
data = self.dict(include={"maxiter", "scf_properties"})
if self.keywords is not None:
data.update(self.keywords)
if self.implicit_solvent is not None:
if self.program.lower() != "psi4":
raise QCSpecificationError(
"PCM can only be used with PSI4 please set implicit solvent to None."
)
data["pcm"] = True
data["pcm__input"] = self.implicit_solvent.to_string()
return data
class QCSpecificationHandler(BaseModel):
"""
A mixin class for handling the QCSpecification
"""
qc_specifications: Dict[str, QCSpec] = Field(
{"default": QCSpec()},
description="The QCSpecifications which will be computed for this dataset.",
)
def clear_qcspecs(self) -> None:
"""
Clear out any current QCSpecs.
"""
self.qc_specifications = {}
def remove_qcspec(self, spec_name: str) -> None:
"""
Remove a QCSpec from the dataset.
Parameters:
spec_name: The name of the spec that should be removed.
Note:
The QCSpec settings are not mutable and so they must be removed and a new one added to ensure they are fully validated.
"""
if spec_name in self.qc_specifications.keys():
del self.qc_specifications[spec_name]
@property
def n_qc_specs(self) -> int:
"""
Return the number of QCSpecs on this dataset.
"""
return len(self.qc_specifications)
def _check_qc_specs(self) -> None:
if self.n_qc_specs == 0:
raise QCSpecificationError(
"There are no QCSpecifications for this dataset please add some using `add_qc_spec`"
)
def add_qc_spec(
self,
method: str,
basis: Optional[str],
program: str,
spec_name: str,
spec_description: str,
store_wavefunction: str = "none",
overwrite: bool = False,
implicit_solvent: Optional[PCMSettings] = None,
maxiter: PositiveInt = 200,
scf_properties: Optional[List[SCFProperties]] = None,
keywords: Optional[
Dict[
str,
Union[StrictStr, StrictInt, StrictFloat, StrictBool, List[StrictFloat]],
]
] = None,
) -> None:
"""
Add a new qcspecification to the factory which will be applied to the dataset.
Parameters:
method: The name of the method to use eg B3LYP-D3BJ
basis: The name of the basis to use can also be `None`
program: The name of the program to execute the computation
spec_name: The name the spec should be stored under
spec_description: The description of the spec
store_wavefunction: what parts of the wavefunction that should be saved
overwrite: If there is a spec under this name already overwrite it
implicit_solvent: The implicit solvent settings if it is to be used.
maxiter: The maximum number of SCF iterations that should be done.
scf_properties: The list of SCF properties that should be extracted from the calculation.
keywords: Program specific computational keywords that should be passed to
the program
"""
spec = QCSpec(
method=method,
basis=basis,
program=program,
spec_name=spec_name,
spec_description=spec_description,
store_wavefunction=store_wavefunction,
maxiter=maxiter,
scf_properties=scf_properties or DefaultProperties,
implicit_solvent=implicit_solvent,
keywords=keywords,
)
if spec_name not in self.qc_specifications:
self.qc_specifications[spec.spec_name] = spec
elif overwrite:
self.qc_specifications[spec.spec_name] = spec
else:
raise QCSpecificationError(
f"A specification is already stored under {spec_name} to replace it set `overwrite=True`."
)
class IndexCleaner:
"""
This class offers the ability to clean a molecule index that already has a numeric tag useful for datasets and
results.
"""
@staticmethod
def _clean_index(index: str) -> Tuple[str, int]:
"""
Take an index and clean it by checking if it already has an enumerator in it.
Return the core index and any numeric tags.
If no tag is found the tag is set to 0.
Parameters:
index: The index for the entry which should be checked, if no numeric tag can be found return 0.
Returns:
A tuple of the core index and the numeric tag it starts from.
Note:
This function allows the dataset to add more conformers to a molecule set so long as the index the molecule
is stored under is a new index not in the database for example if 3 conformers for ethane exist then the
new index should start from 'CC-3'.
"""
# tags take the form '-no'
match = re.search("-[0-9]+$", index)
if match is not None:
core = index[: match.span()[0]]
# drop the -
tag = int(match.group()[1:])
else:
core = index
tag = 0
return core, tag
class Metadata(DatasetConfig):
"""
A general metadata class which is required to be filled in before submitting a dataset to the qcarchive.
"""
submitter: str = Field(
getpass.getuser(),
description="The name of the submitter/creator of the dataset, this is automatically generated but can be changed.",
)
creation_date: date = Field(
datetime.today().date(),
description="The date the dataset was created on, this is automatically generated.",
)
collection_type: Optional[str] = Field(
None,
description="The type of collection that will be created in QCArchive this is automatically updated when attached to a dataset.",
)
dataset_name: Optional[str] = Field(
None,
description="The name that will be given to the collection once it is put into QCArchive, this is updated when attached to a dataset.",
)
short_description: Optional[constr(min_length=8, regex="[a-zA-Z]")] = Field( # noqa
None, description="A short informative description of the dataset."
)
long_description_url: Optional[HttpUrl] = Field(
None,
description="The url which links to more information about the submission normally a github repo with scripts showing how the dataset was created.",
)
long_description: Optional[constr(min_length=8, regex="[a-zA-Z]")] = Field( # noqa
None,
description="A long description of the purpose of the dataset and the molecules within.",
)
elements: Set[str] = Field(
set(), description="The unique set of elements present in the dataset"
)
def validate_metadata(self, raise_errors: bool = False) -> Optional[List[str]]:
"""
Before submitting this function should be called to highlight any incomplete fields.
"""
empty_fields = []
for field in self.__fields__:
if field == "long_description_url":
# The 'long_description_url' is made optional to more easily facilitate
# local or private dataset submissions.
continue
attr = getattr(self, field)
if attr is None:
empty_fields.append(field)
if empty_fields and raise_errors:
raise DatasetInputError(
f"The metadata has the following incomplete fields {empty_fields}"
)
else:
return empty_fields
class MoleculeAttributes(DatasetConfig):
"""
A class to hold and validate the molecule attributes associated with a QCArchive entry, All attributes are required
to be entered into a dataset.
Note:
The attributes here are not exhaustive but are based on those given by cmiles and can all be obtain through the openforcefield toolkit Molecule class.
"""
class Config:
extra = "allow"
canonical_smiles: str
canonical_isomeric_smiles: str
canonical_explicit_hydrogen_smiles: str
canonical_isomeric_explicit_hydrogen_smiles: str
canonical_isomeric_explicit_hydrogen_mapped_smiles: str = Field(
...,
description="The fully mapped smiles where every atom should have a numerical tag so that the molecule can be rebuilt to match the order of the coordinates.",
)
molecular_formula: str = Field(
...,
description="The hill formula of the molecule as given by the openfftoolkit.",
)
standard_inchi: str = Field(
...,
description="The standard inchi given by the inchi program ie not fixed hydrogen layer.",
)
inchi_key: str = Field(
..., description="The standard inchi key given by the inchi program."
)
fixed_hydrogen_inchi: Optional[str] = Field(
None,
description="The non-standard inchi with a fixed hydrogen layer to distinguish tautomers.",
)
fixed_hydrogen_inchi_key: Optional[str] = Field(
None, description="The non-standard inchikey with a fixed hydrogen layer."
)
unique_fixed_hydrogen_inchi_keys: Optional[Set[str]] = Field(
None,
description="The list of unique non-standard inchikey with a fixed hydrogen layer.",
)
@classmethod
def from_openff_molecule(cls, molecule: Molecule) -> "MoleculeAttributes":
"""Create the Cmiles metadata for an OpenFF molecule object.
Parameters:
molecule: The molecule for which the cmiles data will be generated.
Returns:
The Cmiles identifiers generated for the input molecule.
Note:
The Cmiles identifiers currently include:
- `canonical_smiles`
- `canonical_isomeric_smiles`
- `canonical_explicit_hydrogen_smiles`
- `canonical_isomeric_explicit_hydrogen_smiles`
- `canonical_isomeric_explicit_hydrogen_mapped_smiles`
- `molecular_formula`
- `standard_inchi`
- `inchi_key`
- `fixed_hydrogen_inchi`
- `fixed_hydrogen_inchi_key`
- `unique_fixed_hydrogen_inchi_keys`
"""
molecules = split_openff_molecule(molecule=molecule)
unique_fixed_hydrogen_inchi_keys = {
mol.to_inchikey(fixed_hydrogens=True) for mol in molecules
}
off_mol = copy.deepcopy(molecule)
if "atom_map" in off_mol.properties:
del off_mol.properties["atom_map"]
cmiles = {
"canonical_smiles": off_mol.to_smiles(
isomeric=False, explicit_hydrogens=False, mapped=False
),
"canonical_isomeric_smiles": off_mol.to_smiles(
isomeric=True, explicit_hydrogens=False, mapped=False
),
"canonical_explicit_hydrogen_smiles": off_mol.to_smiles(
isomeric=False, explicit_hydrogens=True, mapped=False
),
"canonical_isomeric_explicit_hydrogen_smiles": off_mol.to_smiles(
isomeric=True, explicit_hydrogens=True, mapped=False
),
"canonical_isomeric_explicit_hydrogen_mapped_smiles": off_mol.to_smiles(
isomeric=True, explicit_hydrogens=True, mapped=True
),
"molecular_formula": off_mol.hill_formula,
"standard_inchi": off_mol.to_inchi(fixed_hydrogens=False),
"inchi_key": off_mol.to_inchikey(fixed_hydrogens=False),
"fixed_hydrogen_inchi": off_mol.to_inchi(fixed_hydrogens=True),
"fixed_hydrogen_inchi_key": off_mol.to_inchikey(fixed_hydrogens=True),
"unique_fixed_hydrogen_inchi_keys": unique_fixed_hydrogen_inchi_keys,
}
return MoleculeAttributes(**cmiles)
def to_openff_molecule(self) -> Molecule:
"""
Create an openff molecule from the CMILES information.
"""
return Molecule.from_mapped_smiles(
mapped_smiles=self.canonical_isomeric_explicit_hydrogen_mapped_smiles,
allow_undefined_stereo=True,
)
class CommonBase(DatasetConfig, IndexCleaner, QCSpecificationHandler):
"""
A common base structure which the dataset and factory classes derive from.
"""
driver: SinglepointDriver = Field(
SinglepointDriver.energy,
description="The type of single point calculations which will be computed. Note some services require certain calculations for example optimizations require graident calculations.",
)
priority: str = Field(
"normal",
description="The priority the dataset should be computed at compared to other datasets currently running.",
)
dataset_tags: List[str] = Field(
["openff"], description="The dataset tags which help identify the dataset."
)
compute_tag: str = Field(
"openff",
description="The tag the computes tasks will be assigned to, managers wishing to execute these tasks should use this compute tag.",
)
def dict(self, *args, **kwargs):
"""
Overwrite the dict method to handle any enums when saving to yaml/json via a dict call.
"""
data = super(CommonBase, self).dict(*args, **kwargs)
# now add the enum values
if "driver" in data:
data["driver"] = self.driver.value
return data