Chemical Search API Reference¶
pycomptox.chemical.search.Chemical
¶
Bases: CachedAPIClient
A class for interacting with the CompTox Dashboard Chemical Search API.
This class provides methods to search for chemicals using various criteria including name, identifiers, formulas, and mass ranges.
Attributes:
| Name | Type | Description |
|---|---|---|
api_key |
str
|
The API key for accessing the CompTox Dashboard API. |
base_url |
str
|
The base URL for the CompTox API. |
session |
Session
|
A requests session for making API calls. |
time_delay_between_calls |
float
|
Minimum time delay (in seconds) between API calls. |
_last_call_time |
float
|
Timestamp of the last API call. |
Source code in src/pycomptox/chemical/search.py
8 9 10 11 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 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 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 | |
__init__(api_key=None, base_url='https://comptox.epa.gov/ctx-api', time_delay_between_calls=0.0, **kwargs)
¶
Initialize the Chemical search client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
The API key for accessing the CompTox Dashboard API. If not provided, the function will attempt to load it from: 1. COMPTOX_API_KEY environment variable 2. Saved configuration file (use save_api_key() to set) |
None
|
base_url
|
str
|
The base URL for the API. Defaults to "https://comptox.epa.gov/ctx-api". |
'https://comptox.epa.gov/ctx-api'
|
time_delay_between_calls
|
float
|
Minimum time delay (in seconds) between consecutive API calls. Defaults to 0.0 (no delay). Set this to a positive value if rate limiting becomes necessary. |
0.0
|
**kwargs
|
Additional arguments for CachedAPIClient (cache_manager, use_cache) |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no API key is provided and none can be loaded. |
Example
Using a provided API key¶
client = Chemical(api_key="your_api_key")
Using saved API key (after calling save_api_key())¶
client = Chemical()
With rate limiting¶
client = Chemical(time_delay_between_calls=0.5) # 0.5 second delay
Source code in src/pycomptox/chemical/search.py
search_by_exact_batch_values(values, use_cache=None)
¶
Search by exact batch of values.
POST /chemical/search/equal/ NOTE: Search batch of values (values are separated by newline and maximum 200 values are allowed).
Example
curl -X POST "https://comptox.epa.gov/ctx-api/chemical/search/equal/" \ -H 'accept: application/json'\ -H 'content-type: application/json' \ -d '"Bisphenol A\nDTXSID7020182\n80-05-7"'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
List[str]
|
A list of search values (chemical names, DTXSID, CAS numbers, etc.). Maximum 200 values are allowed per batch. |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: A list of chemical records matching the search values. Each record contains: - dtxsid (str): The DSSTox Substance ID - dtxcid (str): The DSSTox Compound ID - casrn (str): The CAS Registry Number - smiles (str): The SMILES notation - preferredName (str): The preferred chemical name - searchName (str): The name field that matched - searchValue (str): The value that was matched - rank (int): The search result ranking - hasStructureImage (int): Whether a structure image is available - isMarkush (bool): Whether the chemical is a Markush structure - searchMsgs (List[str]): Search messages - suggestions (List[str]): Alternative suggestions - isDuplicate (bool): Whether this is a duplicate result |
Raises:
| Type | Description |
|---|---|
ValueError
|
If more than 200 values are provided, or no data is found. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") values = ["Bisphenol A", "DTXSID7020182", "80-05-7"] results = client.search_by_exact_batch_values(values) for result in results: ... print(f"{result['preferredName']} - {result['dtxsid']}")
Source code in src/pycomptox/chemical/search.py
search_by_exact_formula(formula, use_cache=None)
¶
Search chemicals by exact molecular formula.
GET /chemical/search/by-exact-formula/{formula} Searches for chemicals with the exact molecular formula (including all atoms).
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/search/by-exact-formula/C15H16O2" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
The exact molecular formula (e.g., "C15H16O2"). |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of DTXSID identifiers for chemicals matching the exact formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data is found or the formula is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") dtxsids = client.search_by_exact_formula("C15H16O2") print(f"Found {len(dtxsids)} chemicals with exact formula C15H16O2")
Source code in src/pycomptox/chemical/search.py
search_by_exact_value(value, use_cache=None)
¶
Search chemicals by exact name/identifier match.
GET /chemical/search/equal/{word} NOTE: Search value needs to be URL encoded for synonyms
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/search/equal/DTXSID7020182" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The exact value to search for (e.g., chemical name, DTXSID, CAS number). |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: A list of chemical records that exactly match the search value. Each record contains the same fields as search_by_starting_value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data is found or the request is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") results = client.search_by_exact_value("Bisphenol A") print(results[0].get("dtxsid"))
Source code in src/pycomptox/chemical/search.py
search_by_msready_formula(formula, use_cache=None)
¶
Search chemicals by MS-ready molecular formula.
GET /chemical/search/by-msready-formula/{formula} MS-ready formulas exclude certain atoms/groups that are not typically observed in mass spectrometry.
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/search/by-msready-formula/C15H16O2" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
The MS-ready molecular formula (e.g., "C15H16O2"). |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of DTXSID identifiers for chemicals matching the formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data is found or the formula is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") dtxsids = client.search_by_msready_formula("C15H16O2") print(f"Found {len(dtxsids)} chemicals with MS-ready formula C15H16O2")
Source code in src/pycomptox/chemical/search.py
search_by_starting_value(value, use_cache=None)
¶
Search chemicals where name/identifier starts with the given value.
GET /chemical/search/start-with/{word} NOTE: Search value needs to be URL encoded for synonyms
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/search/start-with/DTXSID7020182" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The starting value to search for (e.g., chemical name, DTXSID, CAS number). |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: A list of chemical records matching the search criteria.
Each record contains:
- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data is found or the request is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") results = client.search_by_starting_value("DTXSID7020182") print(results[0].get("preferredName"))
Source code in src/pycomptox/chemical/search.py
search_by_substring_value(value, use_cache=None)
¶
Search chemicals where name/identifier contains the given substring.
GET /chemical/search/contain/{word} NOTE: Search value needs to be URL encoded for synonyms
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/search/contain/DTXCID505" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The substring to search for within chemical names/identifiers. |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: A list of chemical records containing the search substring. Each record contains the same fields as search_by_starting_value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data is found or the request is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") results = client.search_by_substring_value("phenol") for chem in results[:5]: ... print(chem['preferredName'])
Source code in src/pycomptox/chemical/search.py
search_chemical_count_by_exact_formula(formula, use_cache=None)
¶
Get the count of chemicals with a given exact formula.
GET /chemical/count/by-exact-formula/{formula} Returns the number of chemicals that have the specified exact molecular formula.
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/count/by-exact-formula/C15H16O2" \ -H 'accept: application/hal+json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
The exact molecular formula (e.g., "C15H16O2"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The count of chemicals with the given exact formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the formula is invalid or no data is found. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") count = client.search_chemical_count_by_exact_formula("C15H16O2") print(f"There are {count} chemicals with exact formula C15H16O2")
Source code in src/pycomptox/chemical/search.py
search_chemical_count_by_ms_ready_formula(formula, use_cache=None)
¶
Get the count of chemicals with a given MS-ready formula.
GET /chemical/count/by-msready-formula/{formula} Returns the number of chemicals that have the specified MS-ready formula.
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/count/by-msready-formula/C15H16O2" \ -H 'accept: application/hal+json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
The MS-ready molecular formula (e.g., "C15H16O2"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The count of chemicals with the given MS-ready formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the formula is invalid or no data is found. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") count = client.search_chemical_count_by_ms_ready_formula("C15H16O2") print(f"There are {count} chemicals with MS-ready formula C15H16O2")
Source code in src/pycomptox/chemical/search.py
search_ms_ready_by_dtxcid(dtxcid, use_cache=None)
¶
Search MS-ready chemicals by DTXCID.
GET /chemical/msready/search/by-dtxcid/{dtxcid} Returns MS-ready version(s) of a chemical identified by DTXCID.
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/msready/search/by-dtxcid/DTXCID30182" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtxcid
|
str
|
The DSSTox Compound ID (e.g., "DTXCID30182"). |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of MS-ready DTXSID identifiers related to the given DTXCID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the DTXCID is invalid or no data is found. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") dtxsids = client.search_ms_ready_by_dtxcid("DTXCID30182") print(f"Found {len(dtxsids)} MS-ready forms")
Source code in src/pycomptox/chemical/search.py
search_ms_ready_by_dtxcid_batch(dtxcids, use_cache=None)
¶
Search MS-ready chemicals by batch of DTXCIDs.
POST /chemical/msready/search/by-dtxcid/ Returns MS-ready version(s) for multiple chemicals identified by their DTXCIDs.
Example
curl -X POST "https://comptox.epa.gov/ctx-api/chemical/msready/search/by-dtxcid/" \ -H 'accept: application/json'\ -H 'content-type: application/json' \ -d '["DTXCID30182","DTXCID20182"]'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtxcids
|
List[str]
|
A list of DSSTox Compound IDs (e.g., ["DTXCID30182", "DTXCID40123"]). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The API response. This could be: - A dictionary mapping DTXCIDs to MS-ready forms - A list of results The exact format depends on the API response. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no DTXCIDs are provided or the request is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") dtxcids = ["DTXCID30182", "DTXCID40123", "DTXCID50456"] results = client.search_ms_ready_by_dtxcid_batch(dtxcids) print(f"Results type: {type(results)}")
Process based on actual response structure¶
Source code in src/pycomptox/chemical/search.py
search_ms_ready_by_formula(formula, use_cache=None)
¶
Search MS-ready chemicals by molecular formula.
GET /chemical/msready/search/by-formula/{formula} Specifically searches for MS-ready chemicals with the given formula.
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/msready/search/by-formula/C16H24N2O5S" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
The molecular formula (e.g., "C16H24N2O5S"). |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of DTXSID identifiers for MS-ready chemicals matching the formula. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the formula is invalid or no data is found. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") dtxsids = client.search_ms_ready_by_formula("C16H24N2O5S") print(f"Found {len(dtxsids)} MS-ready chemicals")
Source code in src/pycomptox/chemical/search.py
search_ms_ready_by_mass_range(min_mass, max_mass, use_cache=None)
¶
Search MS-ready chemicals within a mass range.
GET /chemical/msready/search/by-mass/{start}/{end} Searches for MS-ready chemicals with molecular mass between min_mass and max_mass.
Example
curl -X GET "https://comptox.epa.gov/ctx-api/chemical/msready/search/by-mass/200.9/200.95" \ -H 'accept: application/json'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_mass
|
float
|
The minimum molecular mass. |
required |
max_mass
|
float
|
The maximum molecular mass. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of DTXSID identifiers for MS-ready chemicals in the mass range. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mass range is invalid or no data is found. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") dtxsids = client.search_ms_ready_by_mass_range(200.9, 200.95) print(f"Found {len(dtxsids)} MS-ready chemicals in mass range")
Source code in src/pycomptox/chemical/search.py
search_ms_ready_by_mass_range_batch(masses, error=0.005, use_cache=None)
¶
Search MS-ready chemicals using batch of mass ranges.
POST /chemical/msready/search/by-mass/ Searches for MS-ready chemicals matching multiple masses with a given error tolerance.
Example
curl -X POST "https://comptox.epa.gov/ctx-api/chemical/msready/search/by-mass/" \ -H 'accept: application/json'\ -H 'content-type: application/json' \ -d '{"masses":[0],"error":0}'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
masses
|
List[float]
|
A list of molecular masses to search for. |
required |
error
|
float
|
The mass error tolerance. Defaults to 0.005. |
0.005
|
Returns:
| Type | Description |
|---|---|
Dict[str, List[str]]
|
Dict[str, List[str]]: A dictionary where keys are mass values (as strings) and values are lists of DTXSID identifiers matching that mass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no masses are provided or the request is invalid. |
RuntimeError
|
If the API request fails. |
Example
client = Chemical(api_key="your_api_key") masses = [200.9, 250.5, 300.2] results = client.search_ms_ready_by_mass_range_batch(masses, error=0.01) for mass, dtxsids in results.items(): ... print(f"Mass {mass}: {len(dtxsids)} chemicals found")