MobleyLab / chemper
1
"""
2
mol_toolkit is a set of factory methods which
3
allow the user to interact with ChemPer Mol, Atom, and Bond
4
objects without having to explicitly define which toolkit they
5
are using.
6

7
For example one user could be loading their molecules through openeye
8
and the others could be using RDKit to create what we would call a "user mol"
9

10
In this case they can call:
11
from chemper.mol_toolkits import mol_toolkit
12
mol = mol_toolkit.Mol(user_mol)
13
without needing to tell us what toolkit they are using or they
14
could specify
15
mol = mol_toolkit.Mol(user_mol, toolkit='openeye')
16

17
"""
18 9
import os
19 9
from chemper.chemper_utils import get_data_path
20 9
from chemper.mol_toolkits.adapters import MolAdapter, BondAdapter, AtomAdapter
21
# define default_toolkit:
22 9
try:
23 9
    from openeye import oechem
24 3
    from chemper.mol_toolkits import cp_openeye
25 3
    HAS_OE = True
26 6
except ImportError:
27 6
    HAS_OE = False
28

29 9
try:
30 9
    from rdkit import Chem
31 7
    from chemper.mol_toolkits import cp_rdk
32 7
    HAS_RDK = True
33 2
except ImportError:
34 2
    HAS_RDK = False
35

36 9
if not HAS_OE and not HAS_RDK:
37 0
    raise ImportWarning("No Cheminformatics toolkit was found."\
38
                        " currently chemper supports OpenEye and RDKit")
39

40 9
def Mol(mol):
41
    """
42

43
    Parameters
44
    ----------
45
    mol - a molecule object from any supported toolkit
46

47
    Returns
48
    -------
49
    mol - a chemper Mol
50

51
    """
52
    # if it is already a chemper molecule return as is
53 9
    if isinstance(mol, MolAdapter):
54 9
        return mol
55

56
    # check if this is an Openeye molecule
57 9
    if HAS_OE and isinstance(mol, oechem.OEMolBase):
58 0
        return cp_openeye.Mol(mol)
59

60
    # check if it is an RDK molecule
61 9
    if HAS_RDK and isinstance(mol, Chem.rdchem.Mol):
62 0
        return cp_rdk.Mol(mol)
63

64 9
    err_msg = """
65
    Your molecule has the type %s.
66
    Currently chemper only supports OpenEye and RDKit.
67
    To add support to a new toolkit submit an issue on GitHub at
68
    github.com/MobleyLab/chemper
69
    """
70 9
    raise TypeError(err_msg % type(mol))
71

72

73 9
def Atom(atom):
74
    """
75

76
    Parameters
77
    ----------
78
    atom - Atom object from any supported toolkit
79

80
    Returns
81
    -------
82
    atom - a chemper Atom object
83

84
    """
85 9
    if isinstance(atom, AtomAdapter):
86 0
        return atom
87

88 9
    if HAS_OE and isinstance(atom, oechem.OEAtomBase):
89 0
        return cp_openeye.Atom(atom)
90

91 9
    if HAS_RDK and isinstance(atom, Chem.rdchem.Atom):
92 0
        return cp_rdk.Atom(atom)
93

94 9
    err_msg = """
95
    Your atom has the type %s.
96
    Currently chemper only supports OpenEye and RDKit.
97
    To add support to a new toolkit submit an issue on GitHub at
98
    github.com/MobleyLab/chemper
99
    """
100 9
    raise TypeError(err_msg % type(atom))
101

102

103 9
def Bond(bond):
104
    """
105

106
    Parameters
107
    ----------
108
    bond - Bond object from any supported toolkit
109

110
    Returns
111
    -------
112
    bond - a chemper Bond object
113

114
    """
115 9
    if isinstance(bond, BondAdapter):
116 0
        return bond
117

118 9
    if HAS_OE and isinstance(bond, oechem.OEBondBase):
119 0
        return cp_openeye.Bond(bond)
120

121 9
    if HAS_RDK and isinstance(bond, Chem.rdchem.Bond):
122 0
        return cp_rdk.Bond(bond)
123

124 9
    err_msg = """
125
    Your bond has the type %s.
126
    Currently chemper only supports OpenEye and RDKit.
127
    To add support to a new toolkit submit an issue on GitHub at
128
    github.com/MobleyLab/chemper
129
    """
130 9
    raise TypeError(err_msg % type(bond))
131

132
# =======================================
133
# check user specifications
134
# =======================================
135

136 9
def check_toolkit(toolkit=None):
137
    """
138

139
    Parameters
140
    ----------
141
    toolkit - str
142
        'openeye', 'rdkit', or None
143
        if None then the toolkit will be picked automatically
144

145
    Returns
146
    -------
147
    toolkit - str
148
    returns the name of the toolkit to be used.
149
    If the package isn't available for the specified toolkit
150
    then an error is raised instead
151
    """
152
    # check for a stable
153 9
    if toolkit is None:
154 9
        if HAS_OE:
155 3
            return 'openeye'
156 6
        elif HAS_RDK:
157 6
            return 'rdkit'
158

159 9
    if toolkit.lower() == 'openeye' and HAS_OE:
160 3
        return 'openeye'
161

162 9
    if toolkit.lower() == 'rdkit' and HAS_RDK:
163 7
        return 'rdkit'
164

165 9
    if toolkit.lower() == 'openeye' or toolkit.lower() == 'rdkit':
166 8
        raise ImportError("Toolkit (%s) was not importable" % toolkit)
167

168
    else:
169 9
        raise ImportError("The provided toolkit (%s) isn't supported,"\
170
                          " ChemPer only supports 'openeye' and 'rdkit'" \
171
                          % toolkit)
172

173 9
def check_mol_file(file_name):
174
    """
175

176
    Parameters
177
    ----------
178
    file_name - str
179
        path to a molecule file
180

181
    Returns
182
    -------
183
    path - str
184
        absolute path to a molecule file
185
        raises error if file isn't available
186
    """
187
    # is it a local file?
188 9
    if os.path.exists(file_name):
189 9
        return os.path.abspath(file_name)
190

191 9
    path = get_data_path(os.path.join('molecules', file_name))
192

193 9
    if not os.path.exists(path):
194 0
        raise IOError("Molecule file (%s) was not found locally or in chemper/data/molecules" % file_name)
195

196 9
    return path
197

198

199
# ================================================================
200
# get molecule from SMILES
201
# ================================================================
202

203 9
def MolFromSmiles(smiles, toolkit=None):
204
    """
205

206
    Parameters
207
    ----------
208
    smiles - str
209
        SMILES string
210
    toolkit - str or None
211
        'openeye' or 'rdkit' or None to let chemper pick
212

213
    Returns
214
    -------
215
    mol - ChemPer Mol
216
    """
217 9
    toolkit = check_toolkit(toolkit)
218 9
    if toolkit.lower() == 'openeye':
219 3
        return cp_openeye.MolFromSmiles(smiles)
220

221 6
    return cp_rdk.MolFromSmiles(smiles)
222

223
# =======================================
224
# get molecules from files
225
# =======================================
226

227 9
def mols_from_mol2(mol2_file, toolkit=None):
228
    """
229
    Creates a list of chemper Mols from the provided mol2 file
230
    using a specified or default toolkit
231

232
    Parameters
233
    ----------
234
    mol2_file: str
235
               path to mol2 file, this can be a relative or absolute path locally
236
               or the path to a molecules file stored in chemper at chemper/data/molecules/
237
    toolkit: None or str
238
             'openeye' or 'rdkit' are the two supported toolkits
239
             if None then the first package available (in the order listed here)
240
             will be used
241

242
    Returns
243
    -------
244
    mol2s: list of chemper Mol
245
           list of molecules in the provided mol2 file
246
    """
247 9
    toolkit = check_toolkit(toolkit)
248 9
    mol2_path = check_mol_file(mol2_file)
249

250 9
    if toolkit.lower() == 'openeye':
251 3
        return cp_openeye.mols_from_mol2(mol2_path)
252

253 7
    return cp_rdk.mols_from_mol2(mol2_path)
254

255

256

Read our documentation on viewing source code .

Loading