mcs07/ChemSpiPy

Implement search_by_formula and search_by_mass

Opened this issue · 6 comments

mcs07 commented
Implement search_by_formula and search_by_mass

Hi- I was wondering if it's possible to search_by_mass and search_by_formula as stated in the documentation? I get an error when I try to follow the documentation. I am using ChemSpiPy==2.0.0.

>>> cs.search_by_mass(680, 0.001)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ChemSpider' object has no attribute 'search_by_mass'
>>> cs.search_by_formula('C44H30N4Zn')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ChemSpider' object has no attribute 'search_by_formula'
>>>

This issue makes me think that it hasn't been implemented yet.
Thanks!

mcs07 commented

Yeah, I didn't manage to add these before the 2.0.0 release. It is on the 'To Do list' for the next release, when I get some time.

mcs07 commented

Here's a rough idea of how you could implement it yourself:

def search_by_mass(mass, mass_range, datasources=None, order=None, direction=None):
    qid = cs.filter_mass(mass, mass_range, datasources, order, direction)
    while True:
        status = cs.filter_status(qid)
        if status['status'] == 'Complete':
            csids = cs.filter_results(qid)
            compounds = [objects.Compound(cs, csid) for csid in csids]
            return compounds
        elif status['status'] in {'Failed', 'Unknown', 'Suspended', 'Not Found'}:
            raise Exception('Error searching by mass')

Thanks very much for the quick reply!!!!!!!!
Seems to have worked when in an interactive python session when I from chemspipy import objects.

Hi,

I am also using this, do you know how we can search by formula?

When I search:

for result in cs.search("C6H6"):
        print(result.record_id)

I get no results, but when I type Benzene it comes up? Is there any reason for this?

Hi,

Try to use the implementation Matt gave above but change cs.filter_mass to cs.filter_formula (and adjust the arguments accordingly)

ChemSpiPy/chemspipy/api.py

Lines 334 to 359 in 502bec8

def filter_formula(self, formula, datasources=None, order=None, direction=None):
"""Search compounds by formula.
Optionally filter the results by data source. Use :meth:`~chemspipy.api.ChemSpider.get_datasources` to get the
available datasources.
The accepted values for ``order`` are: :data:`~chemspipy.api.RECORD_ID`, :data:`~chemspipy.api.MASS_DEFECT`,
:data:`~chemspipy.api.MOLECULAR_WEIGHT`, :data:`~chemspipy.api.REFERENCE_COUNT`,
:data:`~chemspipy.api.DATASOURCE_COUNT`, :data:`~chemspipy.api.PUBMED_COUNT` or
:data:`~chemspipy.api.RSC_COUNT`.
:param string formula: Molecular formula.
:param list[string] datasources: (Optional) List of datasources to restrict the results to.
:param string order: (Optional) Field to sort the result by.
:param string direction: (Optional) :data:`~chemspipy.api.ASCENDING` or :data:`~chemspipy.api.DESCENDING`.
:return: Query ID that may be passed to ``filter_status`` and ``filter_results``.
:rtype: string
"""
json = {
'formula': formula,
'dataSources': datasources,
'orderBy': ORDERS.get(order),
'orderDirection': DIRECTIONS.get(direction)
}
response = self.post(api='compounds', namespace='filter', endpoint='formula', json=json)
return response['queryId']