|
| 1 | +from __future__ import print_function |
| 2 | +import ipywidgets as ipw |
| 3 | + |
| 4 | +from aiida.tools.dbimporters.plugins.cod import CodDbImporter |
| 5 | + |
| 6 | +class CodQueryWidget(ipw.VBox): |
| 7 | + '''Query structures in Crystallography Open Database (COD) |
| 8 | + Useful class members: |
| 9 | + :ivar has_structure: link to a method to be overloaded. It is called evey time when `self.drop_structure` |
| 10 | + widget has changed its name |
| 11 | + :vartype has_structure: function |
| 12 | + ''' |
| 13 | + |
| 14 | + def __init__(self, **kwargs): |
| 15 | + description = ipw.HTML("""<h3>Get crystal structures from |
| 16 | + <a href="http://www.crystallography.net">Crystallography Open Database</a></h3> |
| 17 | + <b>Queries by formula</b> |
| 18 | + <br> |
| 19 | + For the queries by formula please adhere to the Hill notation. |
| 20 | + The chemical symbol of an element should be followed by its number in the structure, |
| 21 | + except when there is only one atom. |
| 22 | + When the structure does NOT contain carbon atoms, all the elements are listed alphabetically. |
| 23 | + Example: <i>O2 Si</i>. |
| 24 | + <br> |
| 25 | + In case the structure DOES contain carbon atoms its number following the 'C' symbol is indicated first. |
| 26 | + If hydrogen is also present in the structure then 'H' symbol and its number is indicated second. |
| 27 | + The remaining elements are listed in the alphabetical order. Example: <i>C H4 N2 O</i>. |
| 28 | + <br> |
| 29 | + <b>Queries by the structure id number</b> |
| 30 | + <br> |
| 31 | + For the queries by structure id, plese provide the database id number. Example: <i>1008786</i> |
| 32 | + """) |
| 33 | + layout = ipw.Layout(width="400px") |
| 34 | + style = {"description_width":"initial"} |
| 35 | + self.inp_elements = ipw.Text(description="", value="", placeholder='e.g.: Ni Ti or id number', layout=layout, style=style) |
| 36 | + self.btn_query = ipw.Button(description='Query') |
| 37 | + self.query_message = ipw.HTML("Waiting for input...") |
| 38 | + self.drop_structure = ipw.Dropdown(description="", options=[("select structure",{"status":False})], |
| 39 | + style=style, layout=layout ) |
| 40 | + self.link = ipw.HTML("Link to the web-page will appear here") |
| 41 | + self.structure_ase = None |
| 42 | + |
| 43 | + self.btn_query.on_click(self._on_click_query) |
| 44 | + self.drop_structure.observe(self._on_select_structure, names=['value']) |
| 45 | + |
| 46 | + children = [description, |
| 47 | + ipw.HBox([self.btn_query, self.inp_elements]), |
| 48 | + self.query_message, |
| 49 | + ipw.HBox([self.drop_structure, self.link])] |
| 50 | + super(CodQueryWidget, self).__init__(children=children, **kwargs) |
| 51 | + |
| 52 | + def _query(self, idn=None,formula=None): |
| 53 | + importer = CodDbImporter() |
| 54 | + if idn is not None: |
| 55 | + return importer.query(id=idn) |
| 56 | + elif formula is not None: |
| 57 | + return importer.query(formula=formula) |
| 58 | + |
| 59 | + def _on_click_query(self, change): |
| 60 | + structures = [("select structure", {"status":False})] |
| 61 | + idn = None |
| 62 | + formula = None |
| 63 | + self.query_message.value = "Quering the database ... " |
| 64 | + try: |
| 65 | + idn = int(self.inp_elements.value) |
| 66 | + except: |
| 67 | + formula = str(self.inp_elements.value) |
| 68 | + |
| 69 | + for entry in self._query(idn=idn, formula=formula): |
| 70 | + try: |
| 71 | + entry_cif = entry.get_cif_node() |
| 72 | + formula = entry_cif.get_ase().get_chemical_formula() |
| 73 | + except: |
| 74 | + continue |
| 75 | + entry_add = ("{} (id: {})".format(formula, entry.source['id']), |
| 76 | + { |
| 77 | + "status": True, |
| 78 | + "cif": entry_cif, |
| 79 | + "url": entry.source['uri'], |
| 80 | + "id": entry.source['id'], |
| 81 | + } |
| 82 | + ) |
| 83 | + structures.append(entry_add) |
| 84 | + |
| 85 | + self.query_message.value += "{} structures found".format(len(structures)-1) |
| 86 | + self.drop_structure.options = structures |
| 87 | + |
| 88 | + def _on_select_structure(self, change): |
| 89 | + selected = change['new'] |
| 90 | + if selected['status'] is False: |
| 91 | + self.structure_ase = None |
| 92 | + return |
| 93 | + self.structure_ase = selected['cif'].get_ase() |
| 94 | + formula = self.structure_ase.get_chemical_formula() |
| 95 | + struct_url = selected['url'].split('.cif')[0]+'.html' |
| 96 | + self.link.value='<a href="{}" target="_blank">COD entry {}</a>'.format(struct_url, selected['id']) |
| 97 | + if not self.on_structure_selection is None: |
| 98 | + self.on_structure_selection(structure_ase=self.structure_ase, name=formula) |
| 99 | + |
| 100 | + def on_structure_selection(self, structure_ase=None, name=None): |
| 101 | + pass |
0 commit comments