SMILES Methods¶
This section documents the SMILES strings manipulation and augmentation methods.
Basic Functions¶
augchem.modules.smiles.smiles_modules.atom_positions(smiles: str) -> Tuple[List[str], List[int]]
¶
Extracts individual characters from a SMILES string and identifies indices of atoms.
This function tokenizes a SMILES string into individual characters and identifies positions of actual atoms by excluding special characters like brackets, parentheses, bonds, digits, etc.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
Returns¶
Tuple[List[str]
, List[int]]
A tuple containing:
- List of individual characters from the SMILES string
- List of indices where non-special characters (atoms) are located
Examples¶
atom_positions("CC(=O)O") = (['C', 'C', '(', '=', 'O', ')', 'O'], [0, 1, 4, 6])
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
augchem.modules.smiles.smiles_modules.tokenize(smiles: str)
¶
Tokenizes a SMILES string using a regular expression pattern.
Splits a SMILES string into chemically meaningful tokens according to a predefined regex pattern. This tokenization preserves atom types, bonds, stereochemistry, and other structural features.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
Returns¶
List[str]
A list of chemical tokens extracted from the SMILES string
Examples¶
tokenize("CC(=O)O") = ['C', 'C', '(', '=', 'O', ')', 'O']
tokenize("C1=CC=CC=C1") = ['C', '1', '=', 'C', 'C', '=', 'C', 'C', '=', 'C', '1']
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
Augmentation Methods¶
augchem.modules.smiles.smiles_modules.enumerateSmiles(smiles: str) -> Optional[str]
¶
Generates a valid non-canonical SMILES representation of the input molecule.
Creates an alternative, but chemically equivalent SMILES string by randomizing the atom ordering while preserving the molecular structure. Returns None if the generation fails or produces an invalid SMILES.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
Returns¶
Optional[str]
A new, valid SMILES string with randomized atom ordering, or None if generation fails
Raises¶
ValueError If the input SMILES string is invalid
Examples¶
enumerateSmiles("CC(=O)O") = 'OC(C)=O'
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
augchem.modules.smiles.smiles_modules.mask(smiles: str, mask_ratio: float = 0.5, seed=45) -> List[str]
¶
Replaces random tokens in a SMILES string with a masking token '[M]'.
Tokenizes the SMILES string and randomly replaces a specified fraction of tokens with a mask token. Useful for creating partially obscured molecular representations for machine learning applications.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
mask_ratio
: float, default=0.5
Fraction of tokens to replace with mask tokens (0.0 to 1.0)
seed
: int or numpy.random.RandomState, default=45
Random seed or random number generator for reproducibility
Returns¶
str
SMILES string with selected tokens replaced by '[M]'
Examples¶
mask("CC(=O)O", mask_ratio=0.4, seed=42) = 'CM[M]'
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
augchem.modules.smiles.smiles_modules.delete(smiles: str, delete_ratio: float = 0.3, seed=45) -> List[str]
¶
Removes random tokens from a SMILES string.
Tokenizes the SMILES string and randomly deletes a specified fraction of tokens. This creates an incomplete representation that can be used for data augmentation or model robustness testing.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
delete_ratio
: float, default=0.3
Fraction of tokens to delete (0.0 to 1.0)
seed
: int or numpy.random.RandomState, default=45
Random seed or random number generator for reproducibility
Returns¶
str
SMILES string with selected tokens removed
Examples¶
delete("CC(=O)O", delete_ratio=0.3, seed=42) = 'C(=O)O'
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
augchem.modules.smiles.smiles_modules.swap(smiles: str, seed=45) -> List[str]
¶
Exchanges two random atom tokens within a SMILES string.
Identifies non-special character positions in the SMILES string and swaps two randomly selected atoms. This preserves the token count but alters the molecular structure.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
seed
: int or numpy.random.RandomState, default=45
Random seed or random number generator for reproducibility
Returns¶
str SMILES string with two atoms swapped
Examples¶
swap("CC(=O)O", seed=42) = 'OC(=O)C'
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
augchem.modules.smiles.smiles_modules.fusion(smiles: str, mask_ratio: float = 0.05, delete_ratio: float = 0.3, seed=45) -> List[str]
¶
Applies one randomly selected augmentation method to a SMILES string.
Randomly chooses between masking, deletion, or swapping transformations and applies it to the input SMILES. This provides a diverse set of augmentation possibilities with a single function call.
Parameters¶
smiles
: str
A valid SMILES string representation of a molecule
mask_ratio
: float, default=0.05
Fraction of tokens to mask if masking is selected (0.0 to 1.0)
delete_ratio
: float, default=0.3
Fraction of tokens to delete if deletion is selected (0.0 to 1.0)
seed
: int or numpy.random.RandomState, default=45
Random seed or random number generator for reproducibility
Returns¶
str
Augmented SMILES string
Raises¶
ValueError If input SMILES is empty or if augmentation fails
Examples¶
fusion("CC(=O)O", seed=42) = 'CC(O)='
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|
Dataset Augmentation¶
augchem.modules.smiles.smiles_modules.augment_dataset(col_to_augment: str, dataset: pd.DataFrame, augmentation_methods: List[str], mask_ratio: float = 0.1, property_col: str = None, delete_ratio: float = 0.3, augment_percentage: float = 0.2, seed: int = 42)
¶
Applies selected augmentation methods to SMILES strings in a dataset.
Generates augmented variants of molecular SMILES strings using specified methods and adds them to the dataset. Tracks relationships between original and augmented molecules using parent indices.
Parameters¶
col_to_augment
: str
Column name containing SMILES strings to augment
dataset
: pd.DataFrame
DataFrame containing molecular data with SMILES strings
augmentation_methods
: List[str]
List of methods to apply. Valid options: "mask", "delete", "swap", "fusion", "enumeration"
mask_ratio
: float, default=0.1
Fraction of tokens to mask when using mask augmentation
property_col
: str, optional
Column name containing property values to preserve in augmented data
delete_ratio
: float, default=0.3
Fraction of tokens to delete when using delete augmentation
augment_percentage
: float, default=0.2
Target size of augmented dataset as a fraction of original dataset size
seed
: int, default=42
Random seed for reproducibility
Returns¶
pd.DataFrame
Original dataset with augmented molecules appended, including a 'parent_idx'
column that references original molecule indices
Raises¶
ValueError If input data is not in SMILES format or an unknown augmentation method is specified
Notes¶
Property columns with names starting with "Property_" will be set to "-" in augmented rows.
Source code in augchem\modules\smiles\smiles_modules.py
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 |
|