|
1 | 1 | """Define core molecules functionality. |
2 | 2 |
|
3 | | -Note that these classes are not currently working. |
4 | | -The `MoleculeRester` methods: `search`, `find_molecule`, `get_molecule_by_mpculeid`, |
5 | | -all return 404s when attempting to use them. |
| 3 | +Note that the `MoleculeRester`, unlike the `MaterialsRester`, |
| 4 | +has no API functionality beyond serving as an access point for |
| 5 | +the JCESR and summary resters. |
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from __future__ import annotations |
|
15 | 15 | from mp_api.client.core.utils import validate_ids |
16 | 16 | from mp_api.client.routes.molecules import MOLECULES_RESTERS |
17 | 17 |
|
18 | | - |
19 | | -class BaseMoleculeRester(CoreRester): |
| 18 | +class MoleculeRester(CoreRester): |
| 19 | + """Define molecules stub for accessing JCESR and summary data.""" |
20 | 20 | document_model = MoleculeDoc |
21 | 21 | primary_key = "molecule_id" |
22 | | - |
23 | | - def get_molecule_by_mpculeid( |
24 | | - self, mpcule_id: str, final: bool = True |
25 | | - ) -> Molecule | list[Molecule]: |
26 | | - """Get a molecule object for a given Materials Project molecules ID (MPculeID). |
27 | | -
|
28 | | - Arguments: |
29 | | - mpcule_id (str): Materials project molecule ID |
30 | | - final (bool): Whether to get the final (optimized) molecule, or the list of initial |
31 | | - (pre-optimized) structures. Defaults to True. |
32 | | -
|
33 | | - Returns: |
34 | | - molecule (Union[Molecule, List[Molecule]]): Pymatgen Molecule object or list of |
35 | | - pymatgen Molecule objects. |
36 | | - """ |
37 | | - field = "molecule" if final else "initial_molecules" |
38 | | - |
39 | | - response = self.search(molecule_ids=[mpcule_id], fields=[field]) # type: ignore |
40 | | - return response[0][field] if (response and response[0]) else response # type: ignore |
41 | | - |
42 | | - def find_molecule( |
43 | | - self, |
44 | | - filename_or_molecule: str | Molecule, |
45 | | - charge: int | None = None, |
46 | | - spin_multiplicity: int | None = None, |
47 | | - tolerance: float = 0.01, |
48 | | - allow_multiple_results: bool = False, |
49 | | - ) -> list[str] | str: |
50 | | - """Finds matching molecules from the Materials Project molecules database (MPcules). |
51 | | -
|
52 | | - Multiple results may be returned of "similar" molecules based on |
53 | | - distance using the pymatgen MoleculeMatcher algorithm. |
54 | | -
|
55 | | - Args: |
56 | | - filename_or_molecule: filename or Molecule object |
57 | | - charge: Molecule charge. Default is None, meaning that the charge will not be used to |
58 | | - restrict the output. |
59 | | - spin_multiplicity: Molecule's spin multiplicity. Default is None, meaning that the output will |
60 | | - not be restricted by spin multiplicity. |
61 | | - tolerance: RMSD difference threshold for MoleculeMatcher |
62 | | - allow_multiple_results: changes return type for either |
63 | | - a single mpcule_id or list of mpcule_ids |
64 | | - Returns: |
65 | | - A matching mpcule_id if one is found or list of results if allow_multiple_results |
66 | | - is True |
67 | | - Raises: |
68 | | - MPRestError |
69 | | - """ |
70 | | - if isinstance(filename_or_molecule, str): |
71 | | - m = Molecule.from_file(filename_or_molecule) |
72 | | - elif isinstance(filename_or_molecule, Molecule): |
73 | | - m = filename_or_molecule |
74 | | - else: |
75 | | - raise MPRestError("Provide filename or Structure object.") |
76 | | - |
77 | | - results = self._post_resource( |
78 | | - body=m.as_dict(), |
79 | | - params={ |
80 | | - "tolerance": tolerance, |
81 | | - "charge": charge, |
82 | | - "spin_multiplicity": spin_multiplicity, |
83 | | - }, |
84 | | - suburl="find_molecule", |
85 | | - use_document_model=False, |
86 | | - ).get("data") |
87 | | - |
88 | | - if len(results) > 1: # type: ignore |
89 | | - if not allow_multiple_results: |
90 | | - raise ValueError( |
91 | | - "Multiple matches found for this combination of tolerances, but " |
92 | | - "`allow_multiple_results` set to False." |
93 | | - ) |
94 | | - return results # type: ignore |
95 | | - |
96 | | - return results[0]["molecule_id"] if (results and results[0]) else [] |
97 | | - |
98 | | - def search( |
99 | | - self, |
100 | | - charge: tuple[int, int] | None = None, |
101 | | - spin_multiplicity: tuple[int, int] | None = None, |
102 | | - nelements: tuple[int, int] | None = None, |
103 | | - chemsys: str | list[str] | None = None, |
104 | | - deprecated: bool | None = None, |
105 | | - elements: list[str] | None = None, |
106 | | - exclude_elements: list[str] | None = None, |
107 | | - formula: str | list[str] | None = None, |
108 | | - molecule_ids: MPculeID | list[MPculeID] | None = None, |
109 | | - task_ids: str | list[str] | None = None, |
110 | | - num_chunks: int | None = None, |
111 | | - chunk_size: int = 1000, |
112 | | - all_fields: bool = True, |
113 | | - fields: list[str] | None = None, |
114 | | - ): |
115 | | - """Query molecule docs using a variety of search criteria. |
116 | | -
|
117 | | - Arguments: |
118 | | - charge (Tuple[int, int]): Minimum and maximum charge for the molecule. |
119 | | - spin_multiplicity (Tuple[int, int]): Minimum and maximum spin for the molecule. |
120 | | - nelements (Tuple[int, int]): Minimum and maximum number of elements |
121 | | - chemsys (str, List[str]): A chemical system, list of chemical systems |
122 | | - (e.g., Li-C-O, [C-O-H-N, Li-N]). |
123 | | - deprecated (bool): Whether the material is tagged as deprecated. |
124 | | - elements (List[str]): A list of elements. |
125 | | - exclude_elements (List(str)): List of elements to exclude. |
126 | | - formula (str, List[str]): An alphabetical formula or list of formulas |
127 | | - (e.g. "C2 Li2 O4", ["C2 H4", "C2 H6"]). |
128 | | - molecule_ids (MPculeID, List[MPculeID]): List of Materials Project Molecule IDs (MPculeIDs) to return data |
129 | | - for. |
130 | | - task_ids (str, List[str]): List of Materials Project IDs to return data for. |
131 | | - num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible. |
132 | | - chunk_size (int): Number of data entries per chunk. |
133 | | - all_fields (bool): Whether to return all fields in the document. Defaults to True. |
134 | | - fields (List[str]): List of fields in MoleculeDoc to return data for. |
135 | | - Default is molecule_id, last_updated, and formula_alphabetical if all_fields is False. |
136 | | -
|
137 | | - Returns: |
138 | | - ([MoleculeDoc]) List of molecules documents |
139 | | - """ |
140 | | - query_params: dict = {"deprecated": deprecated} |
141 | | - |
142 | | - if molecule_ids: |
143 | | - if isinstance(molecule_ids, str): |
144 | | - molecule_ids = [molecule_ids] |
145 | | - |
146 | | - query_params.update({"molecule_ids": ",".join(molecule_ids)}) |
147 | | - |
148 | | - if charge: |
149 | | - query_params.update({"charge": charge}) |
150 | | - |
151 | | - if spin_multiplicity: |
152 | | - query_params.update({"spin_multiplicity": spin_multiplicity}) |
153 | | - |
154 | | - if formula: |
155 | | - if isinstance(formula, str): |
156 | | - formula = [formula] |
157 | | - |
158 | | - query_params.update({"formula": ",".join(formula)}) |
159 | | - |
160 | | - if chemsys: |
161 | | - if isinstance(chemsys, str): |
162 | | - chemsys = [chemsys] |
163 | | - |
164 | | - query_params.update({"chemsys": ",".join(chemsys)}) |
165 | | - |
166 | | - if elements: |
167 | | - query_params.update({"elements": ",".join(elements)}) |
168 | | - |
169 | | - if exclude_elements: |
170 | | - query_params.update({"exclude_elements": ",".join(exclude_elements)}) |
171 | | - |
172 | | - if task_ids: |
173 | | - if isinstance(task_ids, str): |
174 | | - task_ids = [task_ids] |
175 | | - |
176 | | - query_params.update({"task_ids": ",".join(validate_ids(task_ids))}) |
177 | | - |
178 | | - query_params = { |
179 | | - entry: query_params[entry] |
180 | | - for entry in query_params |
181 | | - if query_params[entry] is not None |
182 | | - } |
183 | | - |
184 | | - return super()._search( |
185 | | - num_chunks=num_chunks, |
186 | | - chunk_size=chunk_size, |
187 | | - all_fields=all_fields, |
188 | | - fields=fields, |
189 | | - **query_params, |
190 | | - ) |
191 | | - |
192 | | - |
193 | | -class AssociatedMoleculeRester(BaseMoleculeRester): |
194 | | - suffix = "molecules/assoc" |
195 | | - |
196 | | - |
197 | | -class MoleculeRester(BaseMoleculeRester): |
198 | 22 | suffix = "molecules/core" |
199 | 23 | _sub_resters = MOLECULES_RESTERS |
0 commit comments