CAS Common Chemistry¶
Online: CAS Common Chemistry, which needs an API key; see API keys and the CAS Common Chemistry tutorial.
provesid.cascommonchem
¶
CAS Common Chemistry, online: CASCommonChem.
CAS Common Chemistry (https://commonchemistry.cas.org) is CAS's free subset
of the CAS Registry: about half a million substances with their CAS number,
names, structure and some experimental properties. Its API (v2.0) needs a
free API key, sent in the X-API-KEY header; see
CASCommonChem._load_api_key for where it is looked for.
The lookups report failure in the dict they return (found False and a
status) rather than raising, and are cached only when they succeed.
Examples:
>>> from provesid.cascommonchem import CASCommonChem
>>> cas = CASCommonChem(api_key="your-cas-api-key")
>>> result = cas.cas_to_detail("7732-18-5")
>>> result["found"], result["rn"]
(True, '7732-18-5')
Attributes¶
CAS_MIN_INTERVAL
module-attribute
¶
Seconds between two requests to CAS Common Chemistry. CAS does not publish a per-second figure alongside the API key, so this is politeness: five requests a second is what the rest of the package uses for a service that has not said otherwise.
CAS_SMILES_MAX_HITS
module-attribute
¶
Most search hits smiles_to_detail
fetches a record for. Every hit costs one request the first time, because a
hit carries no formula. Water's InChI finds 30, most of them water clusters.
Classes¶
CASCommonChemError
¶
Bases: ServiceError
Base exception for CAS Common Chemistry failures.
Raised by the transport rather than by the methods below, which catch it
and report the failure in the status key of the dict they return ---
the contract those methods have always had.
Examples:
>>> issubclass(CASCommonChemNotFoundError, CASCommonChemError)
True
Source code in src/provesid/cascommonchem.py
49 50 51 52 53 54 55 56 57 58 59 60 61 | |
CASCommonChemNotFoundError
¶
Bases: CASCommonChemError, NotFoundError
CAS answered, and its answer was that there is no such substance.
Examples:
>>> issubclass(CASCommonChemNotFoundError, NotFoundError)
True
Source code in src/provesid/cascommonchem.py
64 65 66 67 68 69 70 71 72 | |
CASCommonChemTimeoutError
¶
Bases: CASCommonChemError, ServiceTimeoutError
Every attempt timed out or the connection could not be made.
Examples:
>>> issubclass(CASCommonChemTimeoutError, ServiceTimeoutError)
True
Source code in src/provesid/cascommonchem.py
75 76 77 78 79 80 81 82 83 | |
CASCommonChem
¶
calling the CAS Common Chemistry API v2.0 to get the information for a given CAS RN Requires API key authentication via X-API-KEY header
The endpoint comes from the Swagger file shipped in the package data directory. Every lookup needs the network.
Raises:
| Type | Description |
|---|---|
ValueError
|
At construction, when no API key can be found. |
Examples:
>>> cas = CASCommonChem(api_key="your-cas-api-key")
>>> cas.base_url
'https://commonchemistry.cas.org/api'
>>> cas.name_to_detail("aspirin")["rn"]
'50-78-2'
Source code in src/provesid/cascommonchem.py
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 | |
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 CAS
number that was looked up during one network outage is on disk as a
permanent {"found": False, "status": "Network Error"}. Version 1
entries are made unreachable rather than left to be served as fact.
Methods:¶
__init__(swagger_file_name='commonchemistry-swagger.json', use_cache=True, api_key=None, api_key_file=None)
¶
Initialize CAS Common Chemistry API client
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
swagger_file_name
|
Name of the swagger JSON file |
'commonchemistry-swagger.json'
|
|
use_cache
|
bool
|
Whether to use cache for lookups |
True
|
api_key
|
str
|
Direct API key string (takes precedence over api_key_file) |
None
|
api_key_file
|
str
|
Path to file containing API key |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no API key is found in any of the places
|
Source code in src/provesid/cascommonchem.py
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 | |
__cache_key__()
¶
Identify this client for cache-key purposes.
The API key is deliberately not part of it: two keys reach the same registry and get the same answer, so keying on it would only mean a new key started from an empty cache. The endpoint and the schema version are what change what a call returns.
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of the class path, the configured base URL and
|
Examples:
>>> cas = CASCommonChem.__new__(CASCommonChem)
>>> cas.base_url = "https://commonchemistry.cas.org/api"
>>> cas.__cache_key__()
('provesid.cascommonchem.CASCommonChem', 'https://commonchemistry.cas.org/api', 2)
Source code in src/provesid/cascommonchem.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
cas_to_detail(cas_rn, timeout=30)
¶
Returns a dictionary with the data for a given CAS RN using API v2.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cas_rn
|
str
|
CAS Registry Number (with or without hyphens) |
required |
timeout
|
Request timeout in seconds |
30
|
Returns:
| Type | Description |
|---|---|
|
Dictionary with compound details including:
|
Examples:
>>> cas = CASCommonChem(api_key="your-cas-api-key")
>>> result = cas.cas_to_detail("7732-18-5")
>>> result["found"], result["rn"]
(True, '7732-18-5')
>>> CASCommonChem(api_key="not-a-key").cas_to_detail("7732-18-5")["status"]
'Unauthorized - Check API Key'
Source code in src/provesid/cascommonchem.py
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 | |
name_to_detail(name, timeout=30)
¶
Returns compound details for a given name using API v2.0
CAS's search matches a name only if CAS lists it as a synonym
("acetone" is found, "propan-2-one" is not). For a structure, use
smiles_to_detail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Compound name |
required |
timeout
|
Request timeout in seconds |
30
|
Returns:
| Type | Description |
|---|---|
|
Dictionary with compound details (same as cas_to_detail), for the
first search hit when there are several (logged). |
Examples:
>>> cas = CASCommonChem(api_key="your-cas-api-key")
>>> cas.name_to_detail("aspirin")["rn"]
'50-78-2'
Source code in src/provesid/cascommonchem.py
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 | |
smiles_to_detail(smiles, timeout=30)
¶
Look a substance up by structure: search CAS by InChI, keep the exact match.
CAS's search matches a SMILES only as the exact string CAS stores
(OCC finds ethanol, CCO does not), and even then its first hit
can be an isotopologue. So the SMILES is not sent. RDKit writes its
standard InChI, which CAS matches whatever the SMILES spelling, and
which keeps isotopes apart. The hits are then narrowed down:
- The record's InChI must equal the query's. CAS gives oligomers and polymers the InChI of their repeat unit, so for ethanol it also finds "Ethanol, dimer".
- Records whose formula is RDKit's formula for the SMILES are
preferred. That drops the dimer, whose formula is
(C2H6O)2. When no record passes, all of step 1's are kept, since CAS writes some salts differently (C2H4O2.Na). - Of those left, the record with the most synonyms wins: sodium
chloride (65) over rock salt (6), both
ClNa. When more than one was left, the others are logged at WARNING.
Each hit costs one detail request the first time it is seen, up to
CAS_SMILES_MAX_HITS. The detail answers are cached, and so is the
result. A convenience layer over CAS's /search and /detail
calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles
|
str
|
SMILES string. |
required |
timeout
|
Request timeout in seconds, per request. |
30
|
Returns:
| Type | Description |
|---|---|
|
Dictionary with compound details, as
|
Examples:
>>> cas = CASCommonChem(api_key="your-cas-api-key")
>>> cas.smiles_to_detail("CCO")["rn"]
'64-17-5'
>>> cas.smiles_to_detail("C1=CC=CC=C1")["name"]
'Benzene'
>>> cas.smiles_to_detail("not-a-smiles")["status"]
'Invalid SMILES'
Source code in src/provesid/cascommonchem.py
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 | |
clear_cache()
¶
Delete every cached CAS Common Chemistry answer, in memory and on disk.
The same as provesid.clear_cache(service='cas').
Examples:
>>> cas = CASCommonChem(api_key="your-cas-api-key")
>>> cas.clear_cache()
>>> cas.get_cache_info()['file_count']
0
Source code in src/provesid/cascommonchem.py
532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
get_cache_info()
¶
Size and location of the CAS Common Chemistry cache.
Returns:
| Type | Description |
|---|---|
|
The statistics
|
Examples:
>>> CASCommonChem(api_key="your-cas-api-key").get_cache_info()['cache_directory'].endswith('cas')
True
Source code in src/provesid/cascommonchem.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |