OPSIN¶
IUPAC name to structure: OPSIN asks the online service, PYOPSIN runs
the OPSIN jar locally. See the OPSIN tutorial.
provesid.opsin
¶
OPSIN, which turns systematic chemical names into structures.
Two ways in. OPSIN calls the hosted web service at
EBI, one request per name, with pacing, retries and caching.
PYOPSIN runs the same parser locally through
py2opsin, which bundles OPSIN's jar and needs a Java runtime but no
network, and parses a list in one JVM start.
OPSIN reads names; it does not look them up. "ethanol" and
"2-acetyloxybenzoic acid" parse, while a trade name such as
"aspirin" does not.
Examples:
>>> from provesid.opsin import OPSIN, PYOPSIN
>>> PYOPSIN().get_std_inchikey("ethanol")
'LFQSCWFLJHTTHZ-UHFFFAOYSA-N'
>>> OPSIN().get_id("ethanol")["smiles"]
'C(C)O'
Attributes¶
OPSIN_MIN_INTERVAL
module-attribute
¶
Seconds between two requests to OPSIN. The service is a name parser rather
than a database lookup, and EBI publishes no per-IP figure for it, so this is
politeness: ten requests a second. Note that OPSIN and ChEBI are both served
from www.ebi.ac.uk, so they share a pacing clock and ten per second is
the budget for the two of them together --- which is the point of keying the
clock by host.
Classes¶
OPSINError
¶
Bases: ServiceError
Base exception for OPSIN web-service failures.
The methods below do not let it reach the caller: they report a failure in
the status key of the dict they return, which is the contract they have
always had.
Examples:
>>> issubclass(OPSINTimeoutError, OPSINError)
True
Source code in src/provesid/opsin.py
48 49 50 51 52 53 54 55 56 57 58 59 60 | |
OPSINNotFoundError
¶
Bases: OPSINError, NotFoundError
Raised for an absence OPSIN reports with no usable body.
Rare in practice: OPSIN explains an unparseable name in the body of its
404, and opsin_classify treats that body
as the answer rather than as absence.
Examples:
>>> issubclass(OPSINNotFoundError, NotFoundError)
True
Source code in src/provesid/opsin.py
63 64 65 66 67 68 69 70 71 72 73 74 75 | |
OPSINTimeoutError
¶
Bases: OPSINError, ServiceTimeoutError
Every attempt timed out or the connection could not be made.
Examples:
>>> issubclass(OPSINTimeoutError, ServiceTimeoutError)
True
Source code in src/provesid/opsin.py
78 79 80 81 82 83 84 85 86 | |
OPSIN
¶
A Python interface to the OPSIN name-to-structure web service.
OPSIN turns a systematic IUPAC name into a structure. This class calls the
hosted service; PYOPSIN runs the same parser
locally through py2opsin, which is faster and works offline, and is the
better choice for anything but a handful of names.
Attributes:
| Name | Type | Description |
|---|---|---|
base_url |
The OPSIN web service endpoint. |
|
use_cache |
Whether lookups are served from the cache. |
Examples:
>>> OPSIN().base_url
'https://www.ebi.ac.uk/opsin/ws/'
Source code in src/provesid/opsin.py
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 | |
Attributes¶
CACHE_SCHEMA_VERSION
class-attribute
instance-attribute
¶
Bumped whenever an entry written by an earlier version must not be
served. Version 2 is this change: the old code cached failures, so a
name that hit one momentary 503 is on disk as a permanent "FAILURE"
for a name OPSIN parses perfectly well --- and every cached failure
carries an empty message, because the old code never read the body
that explains it. Neither is fixable in place, so the old entries are
made unreachable instead.
Methods:¶
__init__(use_cache=True)
¶
Initialize the OPSIN web-service client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_cache
|
bool
|
Whether to use the cache for lookups (default True). When False, skips the cache lookup but still stores results. |
True
|
Examples:
>>> OPSIN(use_cache=False).use_cache
False
Source code in src/provesid/opsin.py
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 | |
__cache_key__()
¶
Identify this client for cache-key purposes.
Without this the key is the class name alone, which is stable across
processes --- what stable_key_part
is for --- but carries no way to retire an entry whose content is now
known to be wrong. The schema version is that way.
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of the class path, the configured base URL and
|
Examples:
>>> OPSIN().__cache_key__()
('provesid.opsin.OPSIN', 'https://www.ebi.ac.uk/opsin/ws/', 2)
Source code in src/provesid/opsin.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
clear_cache()
¶
Delete every cached OPSIN answer, in memory and on disk.
The same as provesid.clear_cache(service='opsin').
Examples:
>>> opsin = OPSIN()
>>> opsin.clear_cache()
>>> opsin.get_cache_info()['file_count']
0
Source code in src/provesid/opsin.py
258 259 260 261 262 263 264 265 266 267 268 269 270 | |
get_cache_info()
¶
Size and location of the OPSIN cache.
Returns:
| Type | Description |
|---|---|
|
The statistics
|
Examples:
>>> OPSIN().get_cache_info()['cache_directory'].endswith('opsin')
True
Source code in src/provesid/opsin.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
get_id(iupac_name, timeout=30)
¶
Return the structure OPSIN parses out of an IUPAC name.
Adapted from the IUPAC WorldFAIR book: https://iupac.github.io/WFChemCookbook/tools/opsin_api_jn.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
The systematic name to parse. |
required |
timeout
|
Request timeout in seconds. |
30
|
Returns:
| Type | Description |
|---|---|
|
A dict in the shape of |
Examples:
>>> OPSIN().get_id("ethanol")["smiles"]
'C(C)O'
>>> OPSIN().get_id("notachemical")["status"]
'FAILURE'
>>> OPSIN().get_id(" ")["message"]
'empty name'
Source code in src/provesid/opsin.py
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 | |
get_id_from_list(iupac_names, timeout=30, pause_time=0.5)
¶
Parse a list of IUPAC names, one request each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_names
|
list
|
The systematic names to parse. |
required |
timeout
|
Per-request timeout in seconds. |
30
|
|
pause_time
|
Seconds to sleep after each name, on top of the pacing the transport already applies. |
0.5
|
Returns:
| Type | Description |
|---|---|
|
One dict per input name, in order, each in the shape
|
Examples:
>>> records = OPSIN().get_id_from_list(["ethanol", "benzene"])
>>> [(r["iupac_name"], r["status"]) for r in records]
[('ethanol', 'SUCCESS'), ('benzene', 'SUCCESS')]
Source code in src/provesid/opsin.py
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 | |
PYOPSIN
¶
OPSIN run locally through py2opsin: offline, and faster for many names.
py2opsin (https://github.com/JacksonBurns/py2opsin, pip install
py2opsin) bundles OPSIN's jar and calls it through Java, so a Java
runtime must be installed. Each method starts the JVM once, about half a
second, whether it is given one name or a list; pass a list to parse many.
Every method accepts a name or a list of names and returns a string or a
list of strings to match. A name OPSIN cannot parse gives "" and a
RuntimeWarning from py2opsin; nothing is raised.
Examples:
>>> opsin = PYOPSIN()
>>> opsin.get_smiles("ethanol")
'C(C)O'
>>> opsin.get_smiles(["ethanol", "benzene"])
['C(C)O', 'C1=CC=CC=C1']
Source code in src/provesid/opsin.py
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 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | |
Methods:¶
__init__(jar_fpath='default')
¶
Initialize the local OPSIN parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jar_fpath
|
Path to an OPSIN jar, or |
'default'
|
Examples:
>>> PYOPSIN().jar_fpath
'default'
Source code in src/provesid/opsin.py
424 425 426 427 428 429 430 431 432 433 434 435 436 | |
get_smiles(iupac_name)
¶
Parse a name to SMILES, as OPSIN writes it (not canonicalised).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
A name, or a list of names. |
required |
Returns:
| Type | Description |
|---|---|
|
The SMILES, or a list of them; |
Examples:
>>> PYOPSIN().get_smiles("ethanol")
'C(C)O'
>>> PYOPSIN().get_smiles("notachemical12345")
''
Source code in src/provesid/opsin.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
get_extended_smiles(iupac_name)
¶
Parse a name to ChemAxon extended SMILES, which keeps atom labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
A name, or a list of names. |
required |
Returns:
| Type | Description |
|---|---|
|
The extended SMILES, or a list of them; |
Examples:
>>> PYOPSIN().get_extended_smiles("ethanol")
'C(C)O |$_AV:1;2;O$|'
Source code in src/provesid/opsin.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
get_inchi(iupac_name)
¶
Parse a name to a non-standard InChI (InChI=1/...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
A name, or a list of names. |
required |
Returns:
| Type | Description |
|---|---|
|
The InChI, or a list of them; |
Examples:
>>> PYOPSIN().get_inchi("ethanol")
'InChI=1/C2H6O/c1-2-3/h3H,2H2,1H3'
Source code in src/provesid/opsin.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
get_std_inchi(iupac_name)
¶
Parse a name to a standard InChI (InChI=1S/...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
A name, or a list of names. |
required |
Returns:
| Type | Description |
|---|---|
|
The standard InChI, or a list of them; |
Examples:
>>> PYOPSIN().get_std_inchi("ethanol")
'InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3'
Source code in src/provesid/opsin.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
get_std_inchikey(iupac_name)
¶
Parse a name to a standard InChIKey.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
A name, or a list of names. |
required |
Returns:
| Type | Description |
|---|---|
|
The InChIKey, or a list of them; |
Examples:
>>> PYOPSIN().get_std_inchikey("ethanol")
'LFQSCWFLJHTTHZ-UHFFFAOYSA-N'
Source code in src/provesid/opsin.py
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
get_CML(iupac_name)
¶
Parse a name to a Chemical Markup Language document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
A name, or a list of names. |
required |
Returns:
| Type | Description |
|---|---|
|
The CML as an XML string. For a list, the lines of one document
holding a |
Examples:
>>> PYOPSIN().get_CML("ethanol")[:38]
"<?xml version='1.0' encoding='UTF-8'?>"
Source code in src/provesid/opsin.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | |
get_id(iupac_name)
¶
Parse one name to every representation OPSIN writes.
Six parser runs, one per representation. For more than one name use
get_id_from_list, which
costs the same six runs for the whole list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_name
|
str
|
The systematic name. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> record = PYOPSIN().get_id("ethanol")
>>> record["status"], record["smiles"], record["stdinchikey"]
('SUCCESS', 'C(C)O', 'LFQSCWFLJHTTHZ-UHFFFAOYSA-N')
Source code in src/provesid/opsin.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 569 570 571 572 573 574 575 576 577 578 579 | |
get_id_from_list(iupac_names)
¶
Parse many names to every representation, six parser runs in all.
py2opsin takes the whole list per run, so this does not loop over
get_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iupac_names
|
list
|
The systematic names. |
required |
Returns:
| Type | Description |
|---|---|
list
|
One |
Examples:
>>> records = PYOPSIN().get_id_from_list(["ethanol", "notachemical12345"])
>>> [(r["iupac_name"], r["smiles"], r["status"]) for r in records]
[('ethanol', 'C(C)O', 'SUCCESS'), ('notachemical12345', '', 'FAILURE')]
Source code in src/provesid/opsin.py
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 | |
Functions:¶
opsin_classify(response)
¶
Decide what an OPSIN response means, treating its 404 as an answer.
OPSIN answers a name it cannot parse with HTTP 404 and a complete JSON
body: {"status": "FAILURE", "message": "<name> was uninterpretable due
to the following section of the name: ..."}. The 404 is an envelope, not
the answer --- the answer is in the body, and it is the only place the
reason exists. Verified live on 2026-09-19 against
notachemical12345.
Treating that as ABSENT would raise before
the body was read, which is what the module did by hand before: it mapped
the status to the string "FAILURE" and returned, so message was
empty for every failure the module ever reported --- including in the
WARNING that get_id_from_list logs, which has therefore always said
"Failed to get ID for x: ".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The response to classify. |
required |
Returns:
| Type | Description |
|---|---|
Outcome
|
|
Examples:
>>> class R: status_code = 404
>>> opsin_classify(R()).name
'OK'
>>> class R: status_code = 503
>>> opsin_classify(R()).name
'RETRY'
Source code in src/provesid/opsin.py
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 | |