0
# Core Chemistry
1
2
Fundamental molecular representation, manipulation, and I/O operations that form the foundation of RDKit's cheminformatics capabilities. These functions handle molecule creation from various chemical formats, basic molecular property access, format conversions, and core molecular operations.
3
4
## Capabilities
5
6
### SMILES Operations
7
8
Convert between SMILES strings and molecular objects for basic chemical structure representation.
9
10
```python { .api }
11
def MolFromSmiles(smiles: str, sanitize: bool = True) -> Mol:
12
"""
13
Create a molecule from a SMILES string.
14
15
Parameters:
16
- smiles: SMILES string representation
17
- sanitize: Whether to sanitize the molecule (default True)
18
19
Returns:
20
Mol object or None if parsing fails
21
"""
22
23
def MolToSmiles(mol: Mol, canonical: bool = True, isomericSmiles: bool = True,
24
kekuleSmiles: bool = False, rootedAtAtom: int = -1,
25
allBondsExplicit: bool = False, allHsExplicit: bool = False) -> str:
26
"""
27
Convert a molecule to SMILES string.
28
29
Parameters:
30
- mol: Input molecule
31
- canonical: Generate canonical SMILES (default True)
32
- isomericSmiles: Include stereochemistry information (default True)
33
- kekuleSmiles: Generate Kekule form (default False)
34
- rootedAtAtom: Root SMILES at specific atom index (default -1)
35
- allBondsExplicit: Make all bonds explicit (default False)
36
- allHsExplicit: Make all hydrogens explicit (default False)
37
38
Returns:
39
SMILES string representation
40
"""
41
```
42
43
### MOL Format Operations
44
45
Handle MOL block format for detailed molecular structure representation including 3D coordinates.
46
47
```python { .api }
48
def MolFromMolBlock(molblock: str, sanitize: bool = True,
49
removeHs: bool = True, strictParsing: bool = True) -> Mol:
50
"""
51
Create a molecule from a MOL block string.
52
53
Parameters:
54
- molblock: MOL format string
55
- sanitize: Whether to sanitize the molecule (default True)
56
- removeHs: Remove explicit hydrogens (default True)
57
- strictParsing: Use strict parsing rules (default True)
58
59
Returns:
60
Mol object or None if parsing fails
61
"""
62
63
def MolToMolBlock(mol: Mol, includeStereo: bool = True,
64
confId: int = -1, kekulize: bool = True) -> str:
65
"""
66
Convert a molecule to MOL block format.
67
68
Parameters:
69
- mol: Input molecule
70
- includeStereo: Include stereochemistry (default True)
71
- confId: Conformer ID to use (default -1 for default conformer)
72
- kekulize: Convert to Kekule form (default True)
73
74
Returns:
75
MOL block string representation
76
"""
77
```
78
79
### SDF File Operations
80
81
Read and write Structure Data Files containing multiple molecules with associated data.
82
83
```python { .api }
84
def SDMolSupplier(fileName: str, sanitize: bool = True,
85
removeHs: bool = True, strictParsing: bool = True):
86
"""
87
Create an SDF file supplier for reading multiple molecules.
88
89
Parameters:
90
- fileName: Path to SDF file
91
- sanitize: Whether to sanitize molecules (default True)
92
- removeHs: Remove explicit hydrogens (default True)
93
- strictParsing: Use strict parsing rules (default True)
94
95
Returns:
96
SDMolSupplier iterator object
97
"""
98
99
def SDWriter(fileName: str):
100
"""
101
Create an SDF file writer for writing multiple molecules.
102
103
Parameters:
104
- fileName: Output SDF file path
105
106
Returns:
107
SDWriter object
108
"""
109
```
110
111
### Molecular Manipulation
112
113
Basic molecular editing operations including hydrogen addition/removal and sanitization.
114
115
```python { .api }
116
def AddHs(mol: Mol, explicitOnly: bool = False, addCoords: bool = False) -> Mol:
117
"""
118
Add explicit hydrogens to a molecule.
119
120
Parameters:
121
- mol: Input molecule
122
- explicitOnly: Only add hydrogens that are already explicit (default False)
123
- addCoords: Add coordinates for new hydrogens (default False)
124
125
Returns:
126
New molecule with explicit hydrogens
127
"""
128
129
def RemoveHs(mol: Mol, implicitOnly: bool = False,
130
updateExplicitCount: bool = False, sanitize: bool = True) -> Mol:
131
"""
132
Remove explicit hydrogens from a molecule.
133
134
Parameters:
135
- mol: Input molecule
136
- implicitOnly: Only remove implicit hydrogens (default False)
137
- updateExplicitCount: Update implicit hydrogen counts (default False)
138
- sanitize: Sanitize after removal (default True)
139
140
Returns:
141
New molecule with hydrogens removed
142
"""
143
144
def SanitizeMol(mol: Mol, sanitizeOps: int = None, catchErrors: bool = False) -> None:
145
"""
146
Sanitize a molecule by kekulizing, setting aromaticity, etc.
147
148
Parameters:
149
- mol: Molecule to sanitize (modified in place)
150
- sanitizeOps: Bitmask of operations to perform (default all)
151
- catchErrors: Catch and ignore errors (default False)
152
153
Returns:
154
None (modifies molecule in place)
155
"""
156
```
157
158
### Molecular Properties
159
160
Access basic molecular properties and structural information.
161
162
```python { .api }
163
def GetMolProps(mol: Mol) -> dict:
164
"""
165
Get all molecular properties as a dictionary.
166
167
Parameters:
168
- mol: Input molecule
169
170
Returns:
171
Dictionary of property name/value pairs
172
"""
173
174
def SetMolProp(mol: Mol, key: str, val: str) -> None:
175
"""
176
Set a molecular property.
177
178
Parameters:
179
- mol: Target molecule
180
- key: Property name
181
- val: Property value
182
183
Returns:
184
None (modifies molecule in place)
185
"""
186
187
def GetMolProp(mol: Mol, key: str) -> str:
188
"""
189
Get a molecular property value.
190
191
Parameters:
192
- mol: Input molecule
193
- key: Property name
194
195
Returns:
196
Property value as string
197
"""
198
```
199
200
## Usage Examples
201
202
### Basic SMILES Processing
203
204
```python
205
from rdkit import Chem
206
207
# Parse SMILES and handle errors
208
smiles = "CCO"
209
mol = Chem.MolFromSmiles(smiles)
210
if mol is not None:
211
canonical_smiles = Chem.MolToSmiles(mol)
212
print(f"Canonical SMILES: {canonical_smiles}")
213
else:
214
print("Invalid SMILES string")
215
```
216
217
### Working with SDF Files
218
219
```python
220
from rdkit import Chem
221
222
# Read molecules from SDF file
223
supplier = Chem.SDMolSupplier('compounds.sdf')
224
for mol in supplier:
225
if mol is not None:
226
smiles = Chem.MolToSmiles(mol)
227
print(f"Molecule: {smiles}")
228
229
# Write molecules to SDF file
230
writer = Chem.SDWriter('output.sdf')
231
mol1 = Chem.MolFromSmiles('CCO')
232
mol2 = Chem.MolFromSmiles('CCCO')
233
writer.write(mol1)
234
writer.write(mol2)
235
writer.close()
236
```
237
238
### Molecular Manipulation
239
240
```python
241
from rdkit import Chem
242
243
# Add and remove hydrogens
244
mol = Chem.MolFromSmiles('CCO')
245
mol_with_hs = Chem.AddHs(mol)
246
print(f"Atoms with Hs: {mol_with_hs.GetNumAtoms()}")
247
248
mol_no_hs = Chem.RemoveHs(mol_with_hs)
249
print(f"Atoms without Hs: {mol_no_hs.GetNumAtoms()}")
250
```