Debian Codegraph @

docs/guide/gettingstarted.md

raw
1 (gettingstarted)=
2
3 # Getting started
4
5 This page gives a introduction on how to get started with PubChemPy. This assumes you already have PubChemPy {ref}`installed <install>`.
6
7 ## Retrieving a Compound
8
9 Retrieving information about a specific Compound in the PubChem database is simple.
10
11 Begin by importing PubChemPy:
12
13 ```pycon
14 >>> import pubchempy as pcp
15 ```
16
17 Let's get the {class}`~pubchempy.Compound` with [CID 5090]:
18
19 ```pycon
20 >>> c = pcp.Compound.from_cid(5090)
21 ```
22
23 Now we have a {class}`~pubchempy.Compound` object called `c`. We can get all the information we need from this object:
24
25 ```pycon
26 >>> print(c.molecular_formula)
27 C17H14O4S
28 >>> print(c.molecular_weight)
29 314.4
30 >>> print(c.smiles)
31 CS(=O)(=O)C1=CC=C(C=C1)C2=C(C(=O)OC2)C3=CC=CC=C3
32 >>> print(c.xlogp)
33 2.3
34 >>> print(c.iupac_name)
35 3-(4-methylsulfonylphenyl)-4-phenyl-2H-furan-5-one
36 >>> print(c.synonyms)
37 ['rofecoxib', 'Vioxx', 'Ceoxx', '162011-90-7', 'MK 966', ... ]
38 ```
39
40 ````{note}
41 All the code examples in this documentation will assume you have imported PubChemPy as `pcp`. If you prefer, you can alternatively import specific functions and classes by name and use them directly:
42
43 ```python
44 from pubchempy import Compound, get_compounds
45 c = Compound.from_cid(1423)
46 cs = get_compounds("Aspirin", "name")
47 ```
48 ````
49
50 ## Searching
51
52 What if you don't know the PubChem CID of the Compound you want? Just use the {func}`~pubchempy.get_compounds` function, for example with a compound name input:
53
54 ```pycon
55 >>> results = pcp.get_compounds("Glucose", "name")
56 >>> print(results)
57 [Compound(5793)]
58 ```
59
60 The first argument is the identifier, and the second argument is the identifier type, which must be one of `name`, `smiles`, `sdf`, `inchi`, `inchikey` or `formula`. More often than not, only a single result will be returned, but sometimes there are multiple results for a given identifier. Therefore, {func}`~pubchempy.get_compounds` returns a list of {class}`~pubchempy.Compound` objects (even if there is only one result).
61
62 It is possible to iterate over this list to get the individual {class}`~pubchempy.Compound` objects:
63
64 ```pycon
65 >>> for compound in results:
66 ... print(compound.smiles)
67 C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O
68 ```
69
70 Or you can access the first result directly:
71
72 ```pycon
73 >>> compound = results[0]
74 >>> print(compound.smiles)
75 C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O
76 ```
77
78 Retrieving the compound record(s) for a SMILES input is just as easy:
79
80 ```pycon
81 >>> pcp.get_compounds("C1=CC2=C(C3=C(C=CC=N3)C=C2)N=C1", "smiles")
82 [Compound(1318)]
83 ```
84
85 ```{note}
86 Beware that line notation inputs like SMILES and InChI can return automatically generated records that aren't actually present in PubChem, and therefore have no CID and are missing many properties that are too complicated to calculate on the fly.
87 ```
88
89 That's all the most basic things you can do with PubChemPy. Read on for more some more advanced usage examples.
90
91 [cid 5090]: https://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=5090