How to retrieve data as a Molecule object via MPRester

I am trying a test to get methane via MPRester, however I can only get the structure property, which returns pymatgen.core.structure.Structure object.

criteria = {'pretty_formula': 'H4C'}
properties = ['task_id', 'structure']
with MPRester(api_key=API_KEY) as m:
    data = m.query(criteria=criteria, properties=properties)
data

is there a way to get methane via MPRester where one of the returned result is a pymatgen.core.structure.Molecule object instead?

Hi @thr_si, I’m not sure this is possible with the existing MPRester, but it should be possible to retreive Molecule objects using the new MP API (see ‘Molecules’ endpoint).

It should also be possible to convert the Structure to a Molecule relatively easily, but might require some clever manipulation of the underlying attributes (e.g. Structure.sites or Structure.lattice)

1 Like

Hi @rkingsbury , I am looking at the Molecules endpoint in the new mp-api. Is there a python method for this? I have look through the documentation and I am not sure how to retrieve the data easily. A simple example of how to get Molecule object from this via code would be helpful :slight_smile: The reason I want the Molecule object in particular is so that I can access its SMILES string. Thank you.

Hi @thr_si , there is some documentation including code examples available at Materials Project - API . This is still in development so apologies if the examples and docs are a bit sparse.

For molecules specifically, this works:

from mp_api.matproj import MPRester

with MPRester(api_key="xxxx") as mpr:

    # retrieve a single MaterialsDoc
    molecules_doc = mpr.molecules.get_document_by_id("mol-45827")

You can find a Molecule object in the .molecule key of the returned MoleculesDoc