ChemSpiPy icon indicating copy to clipboard operation
ChemSpiPy copied to clipboard

Implement search_by_formula and search_by_mass

Open mcs07 opened this issue 7 years ago • 6 comments

mcs07 avatar Sep 09 '18 21:09 mcs07

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!

masterjedidana avatar Oct 11 '18 00:10 masterjedidana

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 avatar Oct 11 '18 11:10 mcs07

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')

mcs07 avatar Oct 11 '18 11:10 mcs07

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

masterjedidana avatar Oct 11 '18 15:10 masterjedidana

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?

Soneji avatar Mar 29 '21 18:03 Soneji

Hi,

Try to use the implementation Matt gave above but change cs.filter_mass to cs.filter_formula (and adjust the arguments accordingly) https://github.com/mcs07/ChemSpiPy/blob/502bec819d8a9184cc81b987f1e3fa304d21032d/chemspipy/api.py#L334-L359

fredrikw avatar Mar 30 '21 07:03 fredrikw