File: objects.py

package info (click to toggle)
python-chemspipy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: python: 1,125; makefile: 14
file content (207 lines) | stat: -rw-r--r-- 5,218 bytes parent folder | download
1
2
3
4
5
6
7
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
# -*- coding: utf-8 -*-
"""
chemspipy.objects
~~~~~~~~~~~~~~~~~

Objects returned by ChemSpiPy API methods.

"""

from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import warnings


from .utils import memoized_property


class Compound(object):
    """ A class for retrieving and caching details about a specific ChemSpider record.

    The purpose of this class is to provide access to various parts of the ChemSpider API that return information about
    a compound given its ChemSpider ID. Information is loaded lazily when requested, and cached for future access.
    """

    def __init__(self, cs, record_id):
        """

        :param ChemSpider cs: ``ChemSpider`` session.
        :param int|string record_id: Compound record ID.
        """
        self._cs = cs
        self._record_id = int(record_id)
        # TODO: Allow optional initialize  with a record-type response from the API (kwarg or class method from_dict?).

    def __eq__(self, other):
        return isinstance(other, Compound) and self.csid == other.csid

    def __repr__(self):
        return 'Compound(%r)' % self.csid

    def _repr_png_(self):
        """For IPython notebook, display 2D image."""
        return self.image

    @property
    def record_id(self):
        """Compound record ID.

        :rtype: int
        """
        return self._record_id

    @property
    def csid(self):
        """ChemSpider ID.

        .. deprecated:: 2.0.0
           Use :py:attr:`~chemspipy.objects.Compound.record_id` instead.

        :rtype: int
        """
        warnings.warn('Use record_id instead of csid.', DeprecationWarning)
        return self._record_id

    @property
    def image_url(self):
        """Return the URL of a PNG image of the 2D chemical structure.

        :rtype: string
        """
        return 'http://www.chemspider.com/ImagesHandler.ashx?id=%s' % self.record_id

    @memoized_property
    def _details(self):
        """Request compound info and cache the result."""
        return self._cs.get_details(self.record_id)

    @property
    def molecular_formula(self):
        """Return the molecular formula for this Compound.

        :rtype: string
        """
        return self._details['formula']

    @property
    def smiles(self):
        """Return the SMILES for this Compound.

        :rtype: string
        """
        return self._details['smiles']

    # TODO: Convert tool to get inchi?

    @property
    def stdinchi(self):
        """Return the Standard InChI for this Compound.

        .. deprecated:: 2.0.0
           Use :py:attr:`~chemspipy.objects.Compound.inchi` instead.

        :rtype: string
        """
        warnings.warn('Use inchi instead of stdinchi.', DeprecationWarning)
        return self.inchi

    @property
    def stdinchikey(self):
        """Return the Standard InChIKey for this Compound.

        .. deprecated:: 2.0.0
           Use :py:attr:`~chemspipy.objects.Compound.inchikey` instead.

        :rtype: string
        """
        warnings.warn('Use inchikey instead of stdinchikey.', DeprecationWarning)
        return self.inchikey

    @property
    def inchi(self):
        """Return the InChI for this Compound.

        :rtype: string
        """
        return self._cs.convert(self.mol_2d, 'Mol', 'InChI')

    @property
    def inchikey(self):
        """Return the InChIKey for this Compound.

        :rtype: string
        """
        return self._cs.convert(self.mol_2d, 'Mol', 'InChIKey')

    @property
    def average_mass(self):
        """Return the average mass of this Compound.

        :rtype: float
        """
        return self._details['averageMass']

    @property
    def molecular_weight(self):
        """Return the molecular weight of this Compound.

        :rtype: float
        """
        return self._details['molecularWeight']

    @property
    def monoisotopic_mass(self):
        """Return the monoisotopic mass of this Compound.

        :rtype: float
        """
        return self._details['monoisotopicMass']

    @property
    def nominal_mass(self):
        """Return the nominal mass of this Compound.

        :rtype: float
        """
        return self._details['nominalMass']

    @property
    def common_name(self):
        """Return the common name for this Compound.

        :rtype: string
        """
        return self._details['commonName']

    @memoized_property
    def mol_2d(self):
        """Return the MOL file for this Compound with 2D coordinates.

        :rtype: string
        """
        return self._details['mol2D']

    @memoized_property
    def mol_3d(self):
        """Return the MOL file for this Compound with 3D coordinates.

        :rtype: string
        """
        return self._details['mol3D']

    @memoized_property
    def image(self):
        """Return a 2D depiction of this Compound.

        :rtype: bytes
        """
        return self._cs.get_image(self.record_id)

    @memoized_property
    def external_references(self):
        """Return external references for this Compound.

        :rtype: list[dict]
        """
        return self._cs.get_external_references(self.record_id)