|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2015 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
|
|
""" |
|
7
|
|
|
## skchem.forcefields.base |
|
8
|
|
|
|
|
9
|
|
|
Module specifying base class for forcefields. |
|
10
|
|
|
""" |
|
11
|
|
|
import warnings |
|
12
|
|
|
import pandas as pd |
|
|
|
|
|
|
13
|
|
|
import progressbar |
|
|
|
|
|
|
14
|
|
|
from rdkit.Chem.rdDistGeom import EmbedMolecule |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
from .. import core |
|
17
|
|
|
|
|
18
|
|
|
class ForceField(object): |
|
|
|
|
|
|
19
|
|
|
def __init__(self, embed=True, warn_on_fail=True, error_on_fail=False, drop_failed=True, add_hs=True): |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
self.add_hs = add_hs |
|
22
|
|
|
self.drop_failed = drop_failed |
|
23
|
|
|
self.warn_on_fail = warn_on_fail |
|
24
|
|
|
self.error_on_fail = error_on_fail |
|
25
|
|
|
self.preembed = embed |
|
26
|
|
|
|
|
27
|
|
|
def embed(self, mol): |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
success = EmbedMolecule(mol) |
|
30
|
|
|
if success == -1: |
|
31
|
|
|
msg = 'Failed to Embed Molecule {}'.format(mol.name) |
|
32
|
|
|
if self.error_on_fail: |
|
33
|
|
|
raise RuntimeError(msg) |
|
34
|
|
|
elif self.warn_on_fail: |
|
35
|
|
|
warnings.warn(msg) |
|
36
|
|
|
return None |
|
37
|
|
|
else: |
|
38
|
|
|
pass |
|
39
|
|
|
|
|
40
|
|
|
if self.add_hs: |
|
41
|
|
|
mol = mol.add_hs(add_coords=True) |
|
42
|
|
|
|
|
43
|
|
|
return mol |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def optimize(self, mol): |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
# TODO: likely need to handle which conformer here |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
if self.preembed: |
|
51
|
|
|
mol = self.embed(mol) |
|
52
|
|
|
|
|
53
|
|
|
if mol is None: |
|
54
|
|
|
return None |
|
55
|
|
|
|
|
56
|
|
|
res = self._optimize(mol) |
|
57
|
|
|
|
|
58
|
|
|
if res == -1: |
|
59
|
|
|
msg = 'Failed to optimize molecule \'{}\' using {}'.format(mol.name, self.__class__) |
|
60
|
|
|
if self.error_on_fail: |
|
61
|
|
|
raise RuntimeError(msg) |
|
62
|
|
|
elif self.warn_on_fail: |
|
63
|
|
|
warnings.warn(msg) |
|
64
|
|
|
return None |
|
65
|
|
|
|
|
66
|
|
|
return mol |
|
67
|
|
|
|
|
68
|
|
|
def _optimize(self, mol): |
|
69
|
|
|
raise NotImplementedError |
|
70
|
|
|
|
|
71
|
|
|
def transform(self, obj): |
|
|
|
|
|
|
72
|
|
|
if isinstance(obj, core.Mol): |
|
73
|
|
|
self.optimize(obj) |
|
74
|
|
|
return obj |
|
75
|
|
|
elif isinstance(obj, pd.Series): |
|
76
|
|
|
bar = progressbar.ProgressBar() |
|
|
|
|
|
|
77
|
|
|
for i, mol in enumerate(bar(obj)): |
|
78
|
|
|
res = self.optimize(mol) |
|
79
|
|
|
if res is None and self.drop_failed: |
|
80
|
|
|
obj = obj.drop(obj.index[i]) |
|
81
|
|
|
return obj |
|
82
|
|
|
elif isinstance(obj, pd.DataFrame): |
|
83
|
|
|
res = self.transform(obj.structure) |
|
84
|
|
|
return obj.ix[res.index] |
|
85
|
|
|
elif isinstance(obj, (tuple, list)): |
|
86
|
|
|
return self.transform(pd.Series(obj, [mol.name for mol in obj])) |
|
87
|
|
|
else: |
|
88
|
|
|
raise NotImplementedError |
|
89
|
|
|
|
|
90
|
|
|
class RoughEmbedding(ForceField): |
|
|
|
|
|
|
91
|
|
|
def _optimize(self, mol): |
|
92
|
|
|
return mol |
|
93
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.