11import json
22import os
3+ import shutil
34
45import ipywidgets as ipw
56import numpy as np
67import pybis as pb
8+ import rdkit
79import traitlets as tl
810from aiida import orm , plugins
911from ase import Atoms
1012from rdkit import Chem
1113from rdkit .Chem import AllChem
1214from sklearn .decomposition import PCA
15+ from surfaces_tools .widgets import cdxml
1316
1417from ..base_connector import ElnConnector
1518
@@ -40,12 +43,31 @@ def _rdkit_opt(smiles, steps=1000):
4043 return make_ase (species , positions )
4144
4245
46+ def read_file (filename ):
47+ return open (filename , "rb" ).read ()
48+
49+
4350def import_smiles (smiles ):
4451 object_type = plugins .DataFactory ("structure" )
4552 node = object_type (ase = _rdkit_opt (smiles ))
4653 return node
4754
4855
56+ def get_molecule_cdxml (session , molecule_permid ):
57+ molecule_obis = session .get_object (molecule_permid )
58+ molecule_obis_datasets = molecule_obis .get_datasets ()
59+ for dataset in molecule_obis_datasets :
60+ dataset_files = dataset .file_list
61+ for file in dataset_files :
62+ _ , file_extension = os .path .splitext (file )
63+ if file_extension == ".cdxml" :
64+ dataset .download (destination = "cdxml_files" )
65+ structure_filepath = f"cdxml_files/{ dataset .permId } /{ file } "
66+ structure_cdxml = read_file (structure_filepath )
67+ shutil .rmtree (f"cdxml_files/{ dataset .permId } /" )
68+ return structure_cdxml
69+
70+
4971class OpenbisElnConnector (ElnConnector ):
5072 """OpenBIS ELN connector to AiiDAlab."""
5173
@@ -57,6 +79,7 @@ class OpenbisElnConnector(ElnConnector):
5779 data_type = tl .Unicode ()
5880
5981 def __init__ (self , ** kwargs ):
82+
6083 self .session = None
6184
6285 eln_instance_widget = ipw .Text (
@@ -86,7 +109,7 @@ def __init__(self, **kwargs):
86109 )
87110 tl .link ((self , "sample_uuid" ), (self .sample_uuid_widget , "value" ))
88111
89- self .output = ipw .Output ()
112+ self .output = ipw .VBox ()
90113
91114 super ().__init__ (
92115 children = [
@@ -175,18 +198,45 @@ def import_data(self):
175198 # sample = self.extract_sample_id()
176199 molecule_info_dict = json .loads (self .molecule_info )
177200
178- # print("Sample", sample)
179201 if self .data_type == "smiles" :
180202 self .node = import_smiles (molecule_info_dict ["smiles" ])
181- eln_info = {
182- "eln_instance" : self .eln_instance ,
183- "eln_type" : self .eln_type ,
184- "sample_uuid" : self .sample_uuid ,
185- "data_type" : self .data_type ,
186- "molecule_uuid" : self .molecule_uuid ,
187- }
188- self .node .set_extra ("eln" , eln_info )
189- self .node .store ()
203+
204+ elif self .data_type == "cdxml" :
205+ self .cdxml_import_widget = cdxml .CdxmlUpload2GnrWidget ()
206+ tl .dlink (
207+ (self .cdxml_import_widget , "structure" ),
208+ (self , "node" ),
209+ transform = lambda struct : orm .StructureData (ase = struct ),
210+ )
211+
212+ self .output .children = [self .cdxml_import_widget ]
213+
214+ cdxml_content = get_molecule_cdxml (self .session , self .sample_uuid )
215+ cdxml_content = cdxml_content .decode ("ascii" ) # To test
216+ (
217+ self .cdxml_import_widget .crossing_points ,
218+ self .cdxml_import_widget .cdxml_atoms ,
219+ self .cdxml_import_widget .nunits .disabled ,
220+ ) = self .cdxml_import_widget .extract_crossing_and_atom_positions (
221+ cdxml_content
222+ )
223+
224+ # Convert CDXML content to RDKit molecules and ASE Atoms objects
225+ options = [
226+ self .cdxml_import_widget .rdkit2ase (mol )
227+ for mol in rdkit .Chem .MolsFromCDXML (cdxml_content )
228+ ]
229+ self .cdxml_import_widget .atoms = options [0 ]
230+ self .cdxml_import_widget ._on_button_click ()
231+
232+ # eln_info = {
233+ # "eln_instance": self.eln_instance,
234+ # "eln_type": self.eln_type,
235+ # "sample_uuid": self.sample_uuid,
236+ # "data_type": self.data_type,
237+ # }
238+ # self.node.set_extra("eln", eln_info)
239+ # self.node.store()
190240
191241 def create_new_collection_openbis (
192242 self , project_code , collection_code , collection_type , collection_name
0 commit comments