Skip to content

Commit

Permalink
Issues #5, #6, and #7. Also made the export of object graph faster by…
Browse files Browse the repository at this point in the history
… saving a history of objects. Instead of always calling openBIS for the objects. One may check inside the dictionary if it is already there.
  • Loading branch information
fabioacl committed Dec 13, 2024
1 parent 11295fa commit 18ac957
Show file tree
Hide file tree
Showing 25 changed files with 2,984 additions and 65 deletions.
19 changes: 18 additions & 1 deletion Notebooks/Metadata_Schemas_LinkML/materialMLinfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ classes:
openbis_code: AM
openbis_label: Atomistic Model
slots:
- structure
- volume
- cell
- dimensionality
- atoms_positions
- periodic_boundary_conditions
Expand Down Expand Up @@ -1770,6 +1771,22 @@ types:
# slots are first-class entities in the metamodel
# declaring them here allows them to be reused elsewhere
slots:
cell:
description: Cell
range: object
multivalued: false
slot_uri:
annotations:
openbis_type: JSON
openbis_label: Cell
volume:
description: Volume
range: double
multivalued: false
slot_uri:
annotations:
openbis_type: REAL
openbis_label: Volume
filepath_executable:
description: Filepath executable
range: string
Expand Down
240 changes: 240 additions & 0 deletions Notebooks/connection_to_openbis/.create_molecule-0.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display\n",
"import ipywidgets as ipw\n",
"import widgets\n",
"import utils\n",
"import os\n",
"import rdkit\n",
"from rdkit.Chem import AllChem, Draw, rdMolDescriptors\n",
"import io\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"CONFIG = utils.read_json(\"config.json\")\n",
"CONFIG_ELN = utils.get_aiidalab_eln_config()\n",
"# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n",
"OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n",
"\n",
"molecule_widgets = widgets.ObjectPropertiesWidgets(\"Molecule\")\n",
"\n",
"molecule_structure = ipw.Image(\n",
" value=utils.read_file(CONFIG[\"default_image_filepath\"]), format='jpg', \n",
" width='420px', height='450px', layout=ipw.Layout(border='solid 1px #cccccc')\n",
")\n",
"\n",
"molecule_cdxml_file = ipw.FileUpload(multiple = False, accept = '.cdxml')\n",
"\n",
"molecule_support_files = ipw.FileUpload(multiple = True)\n",
"\n",
"increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n",
"create_button = utils.Button(\n",
" description = '', disabled = False, button_style = '', tooltip = 'Save', \n",
" icon = 'save', layout = ipw.Layout(width = '100px', height = '50px')\n",
")\n",
"quit_button = utils.Button(\n",
" description = '', disabled = False, button_style = '', \n",
" tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n",
")\n",
"save_close_buttons_hbox = ipw.HBox([create_button, quit_button])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def close_notebook(b):\n",
" display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n",
"\n",
"def load_molecule_structure(change):\n",
" for filename in molecule_cdxml_file.value:\n",
" file_info = molecule_cdxml_file.value[filename]\n",
" utils.save_file(file_info['content'], \"structures/structure.cdxml\")\n",
" \n",
" cdxml_molecule = utils.read_file(\"structures/structure.cdxml\")\n",
" molecules = rdkit.Chem.MolsFromCDXML(cdxml_molecule)\n",
" \n",
" if len(molecules) == 1:\n",
" mol = molecules[0] # Get first molecule\n",
" mol_chemical_formula = rdMolDescriptors.CalcMolFormula(mol) # Sum Formula\n",
" mol_smiles = rdkit.Chem.MolToSmiles(mol) # Canonical Smiles\n",
" chem_mol = rdkit.Chem.MolFromSmiles(mol_smiles)\n",
" \n",
" if chem_mol is not None:\n",
" AllChem.Compute2DCoords(chem_mol) # Add coords to the atoms in the molecule\n",
" img = Draw.MolToImage(chem_mol)\n",
" buffer = io.BytesIO()\n",
" img.save(buffer, format=\"PNG\")\n",
" molecule_structure.value = buffer.getvalue()\n",
" img.save(\"structures/structure.png\")\n",
" else:\n",
" print(f\"Cannot generate molecule image.\")\n",
" \n",
" molecule_widgets.properties_widgets[\"sum_formula\"].value = mol_chemical_formula\n",
" molecule_widgets.properties_widgets[\"smiles\"].value = mol_smiles\n",
" \n",
" elif len(molecules) > 1:\n",
" print(f\"There are more than one molecule in the file: {filename}\") \n",
" else:\n",
" print(f\"There are no molecules in the file: {filename}\")\n",
" \n",
"def create_molecule_openbis(b):\n",
" object_properties = {}\n",
" for prop in CONFIG[\"objects\"][\"Molecule\"][\"properties\"]:\n",
" object_properties[prop] = molecule_widgets.properties_widgets[prop].value\n",
" \n",
" # Check if the molecule is already in openBIS\n",
" smiles_molecules_openbis = [molecule.props['smiles'] for molecule in utils.get_openbis_objects(OPENBIS_SESSION, type =\"MOLECULE\")]\n",
" \n",
" if object_properties[\"smiles\"] in smiles_molecules_openbis:\n",
" display(utils.Javascript(data = \"alert('Molecule is already in openBIS!')\"))\n",
" \n",
" if Path(\"structures/structure.png\").exists():\n",
" os.remove(\"structures/structure.png\")\n",
" \n",
" if Path(\"structures/structure.png\").exists():\n",
" os.remove(\"structures/structure.cdxml\")\n",
" else:\n",
" molecule_object = utils.create_openbis_object(\n",
" OPENBIS_SESSION, type=\"MOLECULE\", \n",
" collection=\"/MATERIALS/MOLECULES/MOLECULE_COLLECTION\", \n",
" props = object_properties\n",
" )\n",
" \n",
" # Send molecule image to openBIS\n",
" if Path(\"structures/structure.png\").exists():\n",
" # Upload cdxml\n",
" utils.create_openbis_dataset(OPENBIS_SESSION, type = \"ELN_PREVIEW\", object = molecule_object, file = \"structures/structure.png\")\n",
" os.remove(\"structures/structure.png\")\n",
" \n",
" if Path(\"structures/structure.png\").exists():\n",
" # Upload cdxml\n",
" utils.create_openbis_dataset(OPENBIS_SESSION, type = \"RAW_DATA\", object = molecule_object, file = \"structures/structure.cdxml\")\n",
" os.remove(\"structures/structure.cdxml\")\n",
" \n",
" # Additional support files\n",
" upload_datasets(molecule_object, molecule_support_files)\n",
" \n",
" display(utils.Javascript(data = \"alert('Upload successful!')\"))\n",
"\n",
"def upload_datasets(method_object, support_files_widget):\n",
" for filename in support_files_widget.value:\n",
" file_info = support_files_widget.value[filename]\n",
" utils.save_file(file_info['content'], filename)\n",
" utils.create_openbis_dataset(OPENBIS_SESSION, type = 'RAW_DATA', object = method_object, file = filename)\n",
" os.remove(filename)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create molecule"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Upload CDXML file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(molecule_structure)\n",
"display(molecule_cdxml_file)\n",
"molecule_cdxml_file.observe(load_molecule_structure, names = \"value\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Properties"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(molecule_widgets)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Support files"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(molecule_support_files)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Save results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(save_close_buttons_hbox)\n",
"display(increase_buttons_size)\n",
"create_button.on_click(create_molecule_openbis)\n",
"quit_button.on_click(close_notebook)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 18ac957

Please sign in to comment.