SAFE
SAFE Encoder-Decoder¶
SAFEConverter
¶
Molecule line notation conversion from SMILES to SAFE
A SAFE representation is a string based representation of a molecule decomposition into fragment components, separated by a dot ('.'). Note that each component (fragment) might not be a valid molecule by themselves, unless explicitely correct to add missing hydrogens.
Slicing algorithms
By default SAFE strings are generated using BRICS
, however, the following alternative are supported:
- Hussain-Rea (
hr
) - RECAP (
recap
) - RDKit's MMPA (
mmpa
) - Any possible attachment points (
attach
)
Furthermore, you can also provide your own slicing algorithm, which should return a pair of atoms corresponding to the bonds to break.
Source code in safe/converter.py
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 46 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 77 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 119 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 167 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 214 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 254 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 315 316 317 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 |
|
__init__(slicer='brics', require_hs=None, use_original_opener_for_attach=True, ignore_stereo=False)
¶
Constructor for the SAFE converter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slicer |
Optional[Union[str, List[str], Callable]]
|
slicer algorithm to use for encoding. Can either be one of the supported slicing algorithm (SUPPORTED_SLICERS) or a custom callable that returns the bond ids that can be sliced. |
'brics'
|
require_hs |
Optional[bool]
|
whether the slicing algorithm require the molecule to have hydrogen explictly added.
|
None
|
use_original_opener_for_attach |
bool
|
whether to use the original branch opener digit when adding back mapping number to attachment points, or use simple enumeration. |
True
|
ignore_stereo |
bool
|
RDKIT does not support some particular SAFE subset when stereochemistry is defined. |
False
|
Source code in safe/converter.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
decoder(inp, as_mol=False, canonical=False, fix=True, remove_dummies=True, remove_added_hs=True)
¶
Convert input SAFE representation to smiles
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
str
|
input SAFE representation to decode as a valid molecule or smiles |
required |
as_mol |
bool
|
whether to return a molecule object or a smiles string |
False
|
canonical |
bool
|
whether to return a canonical |
False
|
fix |
bool
|
whether to fix the SAFE representation to take into account non-connected attachment points |
True
|
remove_dummies |
bool
|
whether to remove dummy atoms from the SAFE representation. Note that removing_dummies is incompatible with |
True
|
remove_added_hs |
bool
|
whether to remove all the added hydrogen atoms after applying dummy removal for recovery |
True
|
Source code in safe/converter.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
encoder(inp, canonical=True, randomize=False, seed=None, constraints=None, allow_empty=False, rdkit_safe=True)
¶
Convert input smiles to SAFE representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
Union[str, Mol]
|
input smiles |
required |
canonical |
bool
|
whether to return canonical smiles string. Defaults to True |
True
|
randomize |
Optional[bool]
|
whether to randomize the safe string encoding. Will be ignored if canonical is provided |
False
|
seed |
Optional[int]
|
optional seed to use when allowing randomization of the SAFE encoding. Randomization happens at two steps: 1. at the original smiles representation by randomization the atoms. 2. at the SAFE conversion by randomizing fragment orders |
None
|
constraints |
Optional[List[Mol]]
|
List of molecules or pattern to preserve during the SAFE construction. Any bond slicing would happen outside of a substructure matching one of the patterns. |
None
|
allow_empty |
bool
|
whether to allow the slicing algorithm to return empty bonds |
False
|
rdkit_safe |
bool
|
whether to apply rdkit-safe digit standardization to the output SAFE string. |
True
|
Source code in safe/converter.py
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 254 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 315 316 317 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 |
|
randomize(mol, rng=None)
staticmethod
¶
Randomize the position of the atoms in a mol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
molecules to randomize |
required |
rng |
Optional[int]
|
optional seed to use |
None
|
Source code in safe/converter.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
encode(inp, canonical=True, randomize=False, seed=None, slicer=None, require_hs=None, constraints=None, ignore_stereo=False)
¶
Convert input smiles to SAFE representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
Union[str, Mol]
|
input smiles |
required |
canonical |
bool
|
whether to return canonical SAFE string. Defaults to True |
True
|
randomize |
Optional[bool]
|
whether to randomize the safe string encoding. Will be ignored if canonical is provided |
False
|
seed |
Optional[int]
|
optional seed to use when allowing randomization of the SAFE encoding. |
None
|
slicer |
Optional[Union[List[str], str, Callable]]
|
slicer algorithm to use for encoding. Defaults to "brics". |
None
|
require_hs |
Optional[bool]
|
whether the slicing algorithm require the molecule to have hydrogen explictly added. |
None
|
constraints |
Optional[List[Mol]]
|
List of molecules or pattern to preserve during the SAFE construction. |
None
|
ignore_stereo |
Optional[bool]
|
RDKIT does not support some particular SAFE subset when stereochemistry is defined. |
False
|
Source code in safe/converter.py
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 |
|
decode(safe_str, as_mol=False, canonical=False, fix=True, remove_added_hs=True, remove_dummies=True, ignore_errors=False)
¶
Convert input SAFE representation to smiles Args: safe_str: input SAFE representation to decode as a valid molecule or smiles as_mol: whether to return a molecule object or a smiles string canonical: whether to return a canonical smiles or a randomized smiles fix: whether to fix the SAFE representation to take into account non-connected attachment points remove_added_hs: whether to remove the hydrogen atoms that have been added to fix the string. remove_dummies: whether to remove dummy atoms from the SAFE representation ignore_errors: whether to ignore error and return None on decoding failure or raise an error
Source code in safe/converter.py
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 |
|
SAFE Tokenizer¶
SAFESplitter
¶
Standard Splitter for SAFE string
Source code in safe/tokenizer.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 77 78 79 80 81 82 83 84 85 86 87 |
|
detokenize(chars)
¶
Detokenize SAFE notation
Source code in safe/tokenizer.py
75 76 77 78 79 |
|
pre_tokenize(pretok)
¶
Pretokenize using an input pretokenizer object from the tokenizer library
Source code in safe/tokenizer.py
85 86 87 |
|
split(n, normalized)
¶
Perform splitting for pretokenization
Source code in safe/tokenizer.py
81 82 83 |
|
tokenize(line)
¶
Tokenize a safe string into characters.
Source code in safe/tokenizer.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
SAFETokenizer
¶
Bases: PushToHubMixin
Class to initialize and train a tokenizer for SAFE string Once trained, you can use the converted version of the tokenizer to an HuggingFace PreTrainedTokenizerFast
Source code in safe/tokenizer.py
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 119 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 167 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 214 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 254 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 315 316 317 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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
|
bos_token_id
property
¶
Get the bos token id
eos_token_id
property
¶
Get the bos token id
pad_token_id
property
¶
Get the bos token id
__getstate__()
¶
Getting state to allow pickling
Source code in safe/tokenizer.py
195 196 197 198 199 200 201 202 |
|
__len__()
¶
Gets the count of tokens in vocab along with special tokens.
Source code in safe/tokenizer.py
226 227 228 229 230 |
|
__setstate__(d)
¶
Setting state during reloading pickling
Source code in safe/tokenizer.py
204 205 206 207 208 209 210 |
|
decode(ids, skip_special_tokens=True, ignore_stops=False, stop_token_ids=None)
¶
Decodes a list of ids to molecular representation in the format in which this tokenizer was created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
list
|
list of IDs |
required |
skip_special_tokens |
bool
|
whether to skip all special tokens when encountering them |
True
|
ignore_stops |
bool
|
whether to ignore the stop tokens, thus decoding till the end |
False
|
stop_token_ids |
Optional[List[int]]
|
optional list of stop token ids to use |
None
|
Returns:
Name | Type | Description |
---|---|---|
sequence |
str
|
str representation of molecule |
Source code in safe/tokenizer.py
314 315 316 317 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 |
|
encode(sample_str, ids_only=True, **kwargs)
¶
Encodes a given molecule string once training is done
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample_str |
str
|
Sample string to encode molecule |
required |
ids_only |
bool
|
whether to return only the ids or the encoding objet |
True
|
Returns:
Name | Type | Description |
---|---|---|
object |
list
|
Returns encoded list of IDs |
Source code in safe/tokenizer.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
from_dict(data)
classmethod
¶
Load tokenizer from dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
dictionary containing the tokenizer info |
required |
Source code in safe/tokenizer.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
from_pretrained(pretrained_model_name_or_path, cache_dir=None, force_download=False, local_files_only=False, token=None, return_fast_tokenizer=False, proxies=None, **kwargs)
classmethod
¶
Instantiate a [~tokenization_utils_base.PreTrainedTokenizerBase
] (or a derived class) from a predefined
tokenizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretrained_model_name_or_path |
Union[str, PathLike]
|
Can be either:
|
required |
cache_dir |
Optional[Union[str, PathLike]]
|
Path to a directory in which a downloaded predefined tokenizer vocabulary files should be cached if the standard cache should not be used. |
None
|
force_download |
bool
|
Whether or not to force the (re-)download the vocabulary files and override the cached versions if they exist. |
False
|
proxies |
Optional[Dict[str, str]]
|
A dictionary of proxy servers to use by protocol or endpoint, e.g.,
|
None
|
token |
Optional[Union[str, bool]]
|
The token to use as HTTP bearer authorization for remote files.
If |
None
|
local_files_only |
bool
|
Whether or not to only rely on local files and not to attempt to download any files. |
False
|
return_fast_tokenizer |
Optional[bool]
|
Whether to return fast tokenizer or not. |
False
|
Examples:
# We can't instantiate directly the base class *PreTrainedTokenizerBase* so let's show our examples on a derived class: BertTokenizer
# Download vocabulary from huggingface.co and cache.
tokenizer = SAFETokenizer.from_pretrained("datamol-io/safe-gpt")
# If vocabulary files are in a directory (e.g. tokenizer was saved using *save_pretrained('./test/saved_model/')*)
tokenizer = SAFETokenizer.from_pretrained("./test/saved_model/")
# If the tokenizer uses a single vocabulary file, you can point directly to this file
tokenizer = BertTokenizer.from_pretrained("./test/saved_model/tokenizer.json")
Source code in safe/tokenizer.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
|
get_pretrained(**kwargs)
¶
Get a pretrained tokenizer from this tokenizer
Returns:
Type | Description |
---|---|
PreTrainedTokenizerFast
|
Returns pre-trained fast tokenizer for hugging face models. |
Source code in safe/tokenizer.py
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 |
|
load(file_name)
classmethod
¶
Load the current tokenizer from file
Source code in safe/tokenizer.py
304 305 306 307 308 309 310 311 312 |
|
push_to_hub(repo_id, use_temp_dir=None, commit_message=None, private=None, token=None, max_shard_size='10GB', create_pr=False, safe_serialization=False, **deprecated_kwargs)
¶
Upload the tokenizer to the 🤗 Model Hub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The name of the repository you want to push your {object} to. It should contain your organization name when pushing to a given organization. |
required |
use_temp_dir |
Optional[bool]
|
Whether or not to use a temporary directory to store the files saved before they are pushed to the Hub.
Will default to |
None
|
commit_message |
Optional[str]
|
Message to commit while pushing. Will default to |
None
|
private |
Optional[bool]
|
Whether or not the repository created should be private. |
None
|
token |
Optional[Union[bool, str]]
|
The token to use as HTTP bearer authorization for remote files. If |
None
|
max_shard_size |
Optional[Union[int, str]]
|
Only applicable for models. The maximum size for a checkpoint before being sharded. Checkpoints shard
will then be each of size lower than this size. If expressed as a string, needs to be digits followed
by a unit (like |
'10GB'
|
create_pr |
bool
|
Whether or not to create a PR with the uploaded files or directly commit. |
False
|
safe_serialization |
bool
|
Whether or not to convert the model weights in safetensors format for safer serialization. |
False
|
Source code in safe/tokenizer.py
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 485 486 487 488 489 490 491 492 493 494 495 496 497 |
|
save(file_name=None)
¶
Saves the :class:~tokenizers.Tokenizer
to the file at the given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
File where to save tokenizer |
None
|
Source code in safe/tokenizer.py
272 273 274 275 276 277 278 279 280 281 282 283 |
|
save_pretrained(*args, **kwargs)
¶
Save pretrained tokenizer
Source code in safe/tokenizer.py
268 269 270 |
|
set_special_tokens(tokenizer, bos_token=CLS_TOKEN, eos_token=SEP_TOKEN)
classmethod
¶
Set special tokens for a tokenizer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer |
Tokenizer
|
tokenizer for which special tokens will be set |
required |
bos_token |
str
|
Optional bos token to use |
CLS_TOKEN
|
eos_token |
str
|
Optional eos token to use |
SEP_TOKEN
|
Source code in safe/tokenizer.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
to_dict(**kwargs)
¶
Convert tokenizer to dict
Source code in safe/tokenizer.py
254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
train(files, **kwargs)
¶
This is to train a new tokenizer from either a list of file or some input data
Args
files (str): file in which your molecules are separated by new line
kwargs (dict): optional args for the tokenizer train
Source code in safe/tokenizer.py
183 184 185 186 187 188 189 190 191 192 193 |
|
train_from_iterator(data, **kwargs)
¶
Train the Tokenizer using the provided iterator.
You can provide anything that is a Python Iterator
* A list of sequences :obj:List[str]
* A generator that yields :obj:str
or :obj:List[str]
* A Numpy array of strings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Iterator
|
data iterator |
required |
**kwargs |
Any
|
additional keyword argument for the tokenizer |
{}
|
Source code in safe/tokenizer.py
212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
Utils¶
MolSlicer
¶
Slice a molecule into head-linker-tail
Source code in safe/utils.py
37 38 39 40 41 42 43 44 45 46 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 77 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 119 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 167 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 214 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 254 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 |
|
__call__(mol, expected_head=None)
¶
Perform slicing of the input molecule
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Union[Mol, str]
|
input molecule |
required |
expected_head |
Union[Mol, str]
|
substructure that should be part of the head. The small fragment containing this substructure would be kept as head |
None
|
Source code in safe/utils.py
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 214 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 254 255 256 257 258 259 260 |
|
__init__(shortest_linker=False, min_linker_size=0, require_ring_system=True, verbose=False)
¶
Constructor of bond slicer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shortest_linker |
bool
|
whether to consider longuest or shortest linker. Does not have any effect when expected_head group is provided during splitting |
False
|
min_linker_size |
int
|
minimum linker size |
0
|
require_ring_system |
bool
|
whether all fragment needs to have a ring system |
True
|
verbose |
bool
|
whether to allow verbosity in logging |
False
|
Source code in safe/utils.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
get_ring_system(mol)
¶
Get the list of ring system from a molecule
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
input molecule for which we are computing the ring system |
required |
Source code in safe/utils.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
link_fragments(linker, head, tail)
classmethod
¶
Link fragments together using the provided linker
Parameters:
Name | Type | Description | Default |
---|---|---|---|
linker |
Union[Mol, str]
|
linker to use |
required |
head |
Union[Mol, str]
|
head fragment |
required |
tail |
Union[Mol, str]
|
tail fragment |
required |
Source code in safe/utils.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
attr_as(obj, field, value)
¶
Temporary replace the value of an object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
object to temporary patch |
required |
field |
str
|
name of the key to change |
required |
value |
Any
|
value of key to be temporary changed |
required |
Source code in safe/utils.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
compute_side_chains(mol, core, label_by_index=False)
¶
Compute the side chain of a molecule given a core
Finding the side chains
The algorithm to find the side chains from core assumes that the core we get as input has attachment points. Those attachment points are never considered as part of the query, rather they are used to define the attachment points on the side chains. Removing the attachment points from the core is exactly the same as keeping them.
mol = "CC1=C(C(=NO1)C2=CC=CC=C2Cl)C(=O)NC3C4N(C3=O)C(C(S4)(C)C)C(=O)O"
core0 = "CC1(C)CN2C(CC2=O)S1"
core1 = "CC1(C)SC2C(-*)C(=O)N2C1-*"
core2 = "CC1N2C(SC1(C)C)C(N)C2=O"
side_chain = compute_side_chain(core=core0, mol=mol)
dm.to_image([side_chain, core0, mol])
mol
, but core2 is not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
molecule to split |
required |
core |
Mol
|
core to use for deriving the side chains |
required |
Source code in safe/utils.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
convert_to_safe(mol, canonical=False, randomize=False, seed=1, slicer='brics', split_fragment=True, fraction_hs=None, resolution=0.5)
¶
Convert a molecule to a safe representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
molecule to convert |
required |
canonical |
bool
|
whether to use canonical encoding |
False
|
randomize |
bool
|
whether to randomize the encoding |
False
|
seed |
Optional[int]
|
random seed |
1
|
slicer |
str
|
the slicer to use for fragmentation |
'brics'
|
split_fragment |
bool
|
whether to split fragments |
True
|
fraction_hs |
bool
|
proportion of random atom to which we will add explicit hydrogens |
None
|
resolution |
Optional[float]
|
resolution for the partitioning algorithm |
0.5
|
seed |
Optional[int]
|
random seed |
1
|
Source code in safe/utils.py
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 |
|
filter_by_substructure_constraints(sequences, substruct, n_jobs=-1)
¶
Check whether the input substructures are present in each of the molecule in the sequences
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequences |
List[Union[str, Mol]]
|
list of molecules to validate |
required |
substruct |
Union[str, Mol]
|
substructure to use as query |
required |
n_jobs |
int
|
number of jobs to use for parallelization |
-1
|
Source code in safe/utils.py
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
|
find_partition_edges(G, partition)
¶
Find the edges connecting the subgraphs in a given partition of a graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
The original graph. |
required |
partition |
list of list of nodes
|
The partition of the graph where each element is a list of nodes representing a subgraph. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List[Tuple]
|
A list of edges connecting the subgraphs in the partition. |
Source code in safe/utils.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
fragment_aware_spliting(mol, fraction_hs=None, **kwargs)
¶
Custom splitting algorithm for dataset building.
This slicing strategy will cut any bond including bonding with hydrogens However, only one cut per atom is allowed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
molecule to split |
required |
fraction_hs |
Optional[bool]
|
proportion of random atom to which we will add explicit hydrogens |
None
|
kwargs |
Any
|
additional arguments to pass to the partitioning algorithm |
{}
|
Source code in safe/utils.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
list_individual_attach_points(mol, depth=None)
¶
List all individual attachement points.
We do not allow multiple attachment points per substitution position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
molecule for which we need to open the attachment points |
required |
Source code in safe/utils.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
|
mol_partition(mol, query=None, seed=None, **kwargs)
¶
Partition a molecule into fragments using a bond query
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
molecule to split |
required |
query |
Optional[Mol]
|
bond query to use for splitting |
None
|
seed |
Optional[int]
|
random seed |
None
|
kwargs |
Any
|
additional arguments to pass to the partitioning algorithm |
{}
|
Source code in safe/utils.py
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 |
|
standardize_attach(inputs, standard_attach='[*]')
¶
Standardize the attachment points of a molecule
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
str
|
input molecule |
required |
standard_attach |
str
|
standard attachment point to use |
'[*]'
|
Source code in safe/utils.py
571 572 573 574 575 576 577 578 579 580 581 |
|