| 1 |
(advanced)= |
| 2 |
|
| 3 |
# Advanced usage |
| 4 |
|
| 5 |
This guide covers advanced PubChemPy usage patterns, API best practices, error handling, logging, and low-level request functions. |
| 6 |
|
| 7 |
(avoiding_timeouterror)= |
| 8 |
|
| 9 |
## Avoiding TimeoutError |
| 10 |
|
| 11 |
If there are too many results for a request, you will receive a TimeoutError. There are different ways to avoid this, depending on what type of request you are doing. |
| 12 |
|
| 13 |
If retrieving full compound or substance records, instead request a list of cids or sids for your input, and then request the full records for those identifiers individually or in small groups. For example: |
| 14 |
|
| 15 |
```python |
| 16 |
sids = get_sids("Aspirin", "name") |
| 17 |
for sid in sids: |
| 18 |
s = Substance.from_sid(sid) |
| 19 |
``` |
| 20 |
|
| 21 |
When using the `formula` namespace or a `searchtype`, you can also alternatively use the `listkey_count` and `listkey_start` keyword arguments to specify pagination. The `listkey_count` value specifies the number of results per page, and the `listkey_start` value specifies which page to return. For example: |
| 22 |
|
| 23 |
```python |
| 24 |
get_compounds("CC", "smiles", searchtype="substructure", listkey_count=5) |
| 25 |
get("C10H21N", "formula", listkey_count=3, listkey_start=6) |
| 26 |
``` |
| 27 |
|
| 28 |
## Logging |
| 29 |
|
| 30 |
PubChemPy can generate logging statements if required. The logger is named 'pubchempy'. Set the desired logging level globally for all loggers: |
| 31 |
|
| 32 |
```python |
| 33 |
import logging |
| 34 |
logging.basicConfig(level=logging.DEBUG) |
| 35 |
``` |
| 36 |
|
| 37 |
Or specifically enable the PubChempy logger: |
| 38 |
|
| 39 |
```python |
| 40 |
console_handler = logging.StreamHandler() |
| 41 |
console_handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT)) |
| 42 |
pcp_logger = logging.getLogger("pubchempy") |
| 43 |
pcp_logger.setLevel(logging.DEBUG) |
| 44 |
pcp_logger.addHandler(console_handler) |
| 45 |
``` |
| 46 |
|
| 47 |
The levels, in order of increasing verbosity, are CRITICAL, ERROR, WARNING, INFO, DEBUG. |
| 48 |
|
| 49 |
There is more information on logging in the [Python logging documentation][python logging documentation]. |
| 50 |
|
| 51 |
## Using behind a proxy |
| 52 |
|
| 53 |
When using PubChemPy behind a proxy, you may receive a `URLError`: |
| 54 |
|
| 55 |
``` |
| 56 |
URLError: <urlopen error [Errno 65] No route to host> |
| 57 |
``` |
| 58 |
|
| 59 |
A simple fix is to specify the proxy information via urllib: |
| 60 |
|
| 61 |
```python |
| 62 |
import urllib |
| 63 |
proxy_support = urllib.request.ProxyHandler({ |
| 64 |
"http": "http://<proxy.address>:<port>", |
| 65 |
"https": "https://<proxy.address>:<port>" |
| 66 |
}) |
| 67 |
opener = urllib.request.build_opener(proxy_support) |
| 68 |
urllib.request.install_opener(opener) |
| 69 |
``` |
| 70 |
|
| 71 |
## Custom requests |
| 72 |
|
| 73 |
If you wish to perform more complicated requests, you can use the {func}`~pubchempy.request` function. This is an extremely simple wrapper around the REST API that allows you to construct any sort of request from a few parameters. The [PUG REST Specification] has all the information you will need to formulate your requests. |
| 74 |
|
| 75 |
The {func}`~pubchempy.request` function simply returns the exact response from the PubChem server as a string. This can be parsed in different ways depending on the output format you choose. See the Python [json], [xml] and [csv] packages for more information. Additionally, cheminformatics toolkits such as [Open Babel] and [RDKit] offer tools for handling SDF files in Python. |
| 76 |
|
| 77 |
The {func}`~pubchempy.get` function is very similar to the {func}`~pubchempy.request` function, except it handles `listkey` type responses automatically for you. This makes things simpler, however it means you can't take advantage of using the same `listkey` repeatedly to obtain different types of information. See the [PUG REST specification] for more information on how `listkey` responses work. |
| 78 |
|
| 79 |
### Summary of possible inputs |
| 80 |
|
| 81 |
``` |
| 82 |
<identifier> = list of cid, sid, aid, source, inchikey, listkey; string of name, smiles, xref, inchi, sdf; |
| 83 |
<domain> = substance | compound | assay |
| 84 |
|
| 85 |
compound domain |
| 86 |
<namespace> = cid | name | smiles | inchi | sdf | inchikey | <structure search> | <xref> | listkey | formula |
| 87 |
<operation> = record | property/[comma-separated list of property tags] | synonyms | sids | cids | aids | assaysummary | classification |
| 88 |
|
| 89 |
substance domain |
| 90 |
<namespace> = sid | sourceid/<source name> | sourceall/<source name> | name | <xref> | listkey |
| 91 |
<operation> = record | synonyms | sids | cids | aids | assaysummary | classification |
| 92 |
|
| 93 |
assay domain |
| 94 |
<namespace> = aid | listkey | type/<assay type> | sourceall/<source name> |
| 95 |
<assay type> = all | confirmatory | doseresponse | onhold | panel | rnai | screening | summary |
| 96 |
<operation> = record | aids | sids | cids | description | targets/{ProteinGI, ProteinName, GeneID, GeneSymbol} | doseresponse/sid |
| 97 |
|
| 98 |
<structure search> = {substructure | superstructure | similarity | identity}/{smiles | inchi | sdf | cid} |
| 99 |
<xref> = xref/{RegistryID | RN | PubMedID | MMDBID | ProteinGI | NucleotideGI | TaxonomyID | MIMID | GeneID | ProbeID | PatentID} |
| 100 |
<output> = XML | ASNT | ASNB | JSON | JSONP [ ?callback=<callback name> ] | SDF | CSV | PNG | TXT |
| 101 |
``` |
| 102 |
|
| 103 |
[csv]: https://docs.python.org/3/library/csv.html |
| 104 |
[json]: https://docs.python.org/3/library/json.html |
| 105 |
[open babel]: https://openbabel.org/docs/UseTheLibrary/Python.html |
| 106 |
[pug rest specification]: https://pubchem.ncbi.nlm.nih.gov/docs/pug-rest |
| 107 |
[python logging documentation]: https://docs.python.org/3/howto/logging.html |
| 108 |
[rdkit]: https://www.rdkit.org |
| 109 |
[xml]: https://docs.python.org/3/library/xml.etree.elementtree.html |