1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
## skchem.forcefields.base |
8
|
|
|
|
9
|
|
|
Module specifying base class for forcefields. |
10
|
|
|
""" |
11
|
1 |
|
import warnings |
12
|
1 |
|
from abc import ABCMeta, abstractmethod |
13
|
|
|
|
14
|
1 |
|
import pandas as pd |
|
|
|
|
15
|
1 |
|
from rdkit.Chem.rdDistGeom import EmbedMolecule |
|
|
|
|
16
|
|
|
|
17
|
1 |
|
from ..utils import Suppressor |
18
|
1 |
|
from ..base import Transformer |
19
|
1 |
|
from ..filters.base import TransformFilter |
20
|
|
|
|
21
|
|
|
|
22
|
1 |
|
class ForceField(Transformer, TransformFilter): |
23
|
|
|
# TODO: Multiple conformer generation handling. |
|
|
|
|
24
|
|
|
|
25
|
|
|
""" Base forcefield class. |
26
|
|
|
|
27
|
|
|
Filter drops those that fail to be optimized. |
28
|
|
|
|
29
|
|
|
""" |
30
|
|
|
|
31
|
1 |
|
__metaclass__ = ABCMeta |
32
|
|
|
|
33
|
1 |
|
def __init__(self, preembed=True, warn_on_fail=True, error_on_fail=False, |
|
|
|
|
34
|
|
|
add_hs=True, n_jobs=1, verbose=True): |
35
|
|
|
|
36
|
|
|
self.add_hs = add_hs |
37
|
|
|
self.warn_on_fail = warn_on_fail |
38
|
|
|
self.error_on_fail = error_on_fail |
39
|
|
|
self.preembed = preembed |
40
|
|
|
super(ForceField, self).__init__(verbose=verbose, n_jobs=n_jobs) |
41
|
|
|
|
42
|
1 |
|
@property |
43
|
|
|
def columns(self): |
44
|
|
|
return pd.Index(['structure']) |
45
|
|
|
|
46
|
1 |
|
def embed(self, mol): |
|
|
|
|
47
|
|
|
|
48
|
|
|
success = EmbedMolecule(mol) |
49
|
|
|
if success == -1: |
50
|
|
|
msg = 'Failed to Embed Molecule {}'.format(mol.name) |
51
|
|
|
if self.error_on_fail: |
52
|
|
|
raise RuntimeError(msg) |
53
|
|
|
elif self.warn_on_fail: |
54
|
|
|
warnings.warn(msg) |
55
|
|
|
return None |
56
|
|
|
|
57
|
|
|
if self.add_hs: |
58
|
|
|
return mol.add_hs(add_coords=True) |
59
|
|
|
else: |
60
|
|
|
return mol |
61
|
|
|
|
62
|
1 |
|
def _transform_mol(self, mol): |
63
|
|
|
|
64
|
|
|
mol = mol.copy() |
65
|
|
|
|
66
|
|
|
with Suppressor(): |
67
|
|
|
if self.preembed: |
68
|
|
|
mol = self.embed(mol) |
69
|
|
|
|
70
|
|
|
if mol is None: # embedding failed |
71
|
|
|
return None |
72
|
|
|
|
73
|
|
|
res = self._optimize(mol) |
74
|
|
|
|
75
|
|
|
if res == -1: |
76
|
|
|
msg = 'Failed to optimize molecule \'{}\' using {}'.format( |
77
|
|
|
mol.name, self.__class__) |
78
|
|
|
if self.error_on_fail: |
79
|
|
|
raise RuntimeError(msg) |
80
|
|
|
elif self.warn_on_fail: |
81
|
|
|
warnings.warn(msg) |
82
|
|
|
return None |
83
|
|
|
|
84
|
|
|
return mol |
85
|
|
|
|
86
|
1 |
|
@abstractmethod |
87
|
|
|
def _optimize(self, mol): |
88
|
|
|
pass |
89
|
|
|
|
90
|
|
|
|
91
|
1 |
|
class RoughEmbedding(ForceField): |
|
|
|
|
92
|
1 |
|
def _optimize(self, mol): |
93
|
|
|
return mol |
94
|
|
|
|
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__.py
files in your module folders. Make sure that you place one file in each sub-folder.