@@ -310,15 +310,24 @@ def run(self, molecule, work_dir=None, queue=None):
310
310
if work_dir is None :
311
311
work_dir = _os .getcwd ()
312
312
313
- # Flag whether the molecule is a SMILES string.
313
+ # Try to create a molecule from the SMILES string.
314
314
if isinstance (molecule , str ):
315
- is_smiles = True
316
- else :
317
- is_smiles = False
315
+ try :
316
+ smiles = molecule
317
+ molecule = _smiles (molecule )
318
+ edit_mol = molecule ._sire_object .edit ()
319
+ edit_mol = edit_mol .rename (f"smiles:{ smiles } " ).molecule ()
320
+ molecule ._sire_object = edit_mol .commit ()
321
+ except Exception as e :
322
+ msg = "Unable to convert SMILES to Molecule using RDKit."
323
+ if _isVerbose ():
324
+ msg += ": " + getattr (e , "message" , repr (e ))
325
+ raise _ThirdPartyError (msg ) from e
326
+ else :
327
+ raise _ThirdPartyError (msg ) from None
318
328
319
- if not is_smiles :
320
- # Create a copy of the molecule.
321
- new_mol = molecule .copy ()
329
+ # Create a copy of the molecule.
330
+ new_mol = molecule .copy ()
322
331
323
332
# Choose the program to run with depending on the force field compatibility.
324
333
# If tLEaP and pdb2gmx are supported, default to tLEaP, then use pdb2gmx if
@@ -328,8 +337,7 @@ def run(self, molecule, work_dir=None, queue=None):
328
337
if self ._tleap :
329
338
if _tleap_exe is not None :
330
339
output = self ._run_tleap (molecule , str (work_dir ))
331
- if not is_smiles :
332
- new_mol ._ion_water_model = self ._water_model
340
+ new_mol ._ion_water_model = self ._water_model
333
341
# Otherwise, try using pdb2gmx.
334
342
elif self ._pdb2gmx :
335
343
if _gmx_exe is not None :
@@ -371,41 +379,23 @@ def run(self, molecule, work_dir=None, queue=None):
371
379
# Make the molecule 'mol' compatible with 'par_mol'. This will create
372
380
# a mapping between atom indices in the two molecules and add all of
373
381
# the new properties from 'par_mol' to 'mol'.
374
- if is_smiles :
375
- new_mol = par_mol
376
-
377
- # We'll now add MolName and ResName info to the molecule, since
378
- # this will be missing.
379
-
380
- # Rename the molecule with the original SMILES string.
381
- edit_mol = new_mol ._sire_object .edit ()
382
- edit_mol = edit_mol .rename (molecule ).molecule ()
383
-
384
- # Rename the residue LIG.
385
- resname = _SireMol .ResName ("LIG" )
386
- edit_mol = edit_mol .residue (_SireMol .ResIdx (0 )).rename (resname ).molecule ()
387
-
388
- # Commit the changes.
389
- new_mol ._sire_object = edit_mol .commit ()
390
-
382
+ if self ._ensure_compatible :
383
+ new_mol .makeCompatibleWith (
384
+ par_mol ,
385
+ property_map = self ._property_map ,
386
+ overwrite = True ,
387
+ verbose = False ,
388
+ )
391
389
else :
392
- if self . _ensure_compatible :
390
+ try :
393
391
new_mol .makeCompatibleWith (
394
392
par_mol ,
395
393
property_map = self ._property_map ,
396
394
overwrite = True ,
397
395
verbose = False ,
398
396
)
399
- else :
400
- try :
401
- new_mol .makeCompatibleWith (
402
- par_mol ,
403
- property_map = self ._property_map ,
404
- overwrite = True ,
405
- verbose = False ,
406
- )
407
- except :
408
- new_mol = par_mol
397
+ except :
398
+ new_mol = par_mol
409
399
410
400
# Record the forcefield used to parameterise the molecule.
411
401
new_mol ._forcefield = self ._forcefield
@@ -421,33 +411,18 @@ def _run_tleap(self, molecule, work_dir):
421
411
Parameters
422
412
----------
423
413
424
- molecule : :class:`Molecule <BioSimSpace._SireWrappers.Molecule>`, str
425
- The molecule to parameterise, either as a Molecule object or SMILES
426
- string.
414
+ molecule : :class:`Molecule <BioSimSpace._SireWrappers.Molecule>`
415
+ The molecule to parameterise.
427
416
428
417
work_dir : str
429
418
The working directory.
430
419
"""
431
420
432
- # Convert SMILES to a molecule.
433
- if isinstance (molecule , str ):
434
- try :
435
- _molecule = _smiles (molecule )
436
- except Exception as e :
437
- msg = "Unable to convert SMILES to Molecule using RDKit."
438
- if _isVerbose ():
439
- msg += ": " + getattr (e , "message" , repr (e ))
440
- raise _ThirdPartyError (msg ) from e
441
- else :
442
- raise _ThirdPartyError (msg ) from None
443
- else :
444
- _molecule = molecule
445
-
446
421
# Write the system to a PDB file.
447
422
try :
448
423
# LEaP expects residue numbering to be ascending and continuous.
449
424
renumbered_molecule = _SireIO .renumberConstituents (
450
- _molecule .toSystem ()._sire_object
425
+ molecule .toSystem ()._sire_object
451
426
)[0 ]
452
427
renumbered_molecule = _Molecule (renumbered_molecule )
453
428
_IO .saveMolecules (
@@ -570,9 +545,8 @@ def _run_pdb2gmx(self, molecule, work_dir):
570
545
Parameters
571
546
----------
572
547
573
- molecule : :class:`Molecule <BioSimSpace._SireWrappers.Molecule>`, str
574
- The molecule to parameterise, either as a Molecule object or SMILES
575
- string.
548
+ molecule : :class:`Molecule <BioSimSpace._SireWrappers.Molecule>`
549
+ The molecule to parameterise.
576
550
577
551
work_dir : str
578
552
The working directory.
@@ -587,25 +561,11 @@ def _run_pdb2gmx(self, molecule, work_dir):
587
561
"'pdb2gmx' does not support the '%s' force field." % self ._forcefield
588
562
)
589
563
590
- # Convert SMILES to a molecule.
591
- if isinstance (molecule , str ):
592
- try :
593
- _molecule = _smiles (molecule )
594
- except Exception as e :
595
- msg = "Unable to convert SMILES to Molecule using RDKit."
596
- if _isVerbose ():
597
- msg += ": " + getattr (e , "message" , repr (e ))
598
- raise _ThirdPartyError (msg ) from e
599
- else :
600
- raise _ThirdPartyError (msg ) from None
601
- else :
602
- _molecule = molecule
603
-
604
564
# Write the system to a PDB file.
605
565
try :
606
566
_IO .saveMolecules (
607
567
_os .path .join (str (work_dir ), "input" ),
608
- _molecule ,
568
+ molecule ,
609
569
"pdb" ,
610
570
property_map = self ._property_map ,
611
571
)
@@ -1007,21 +967,24 @@ def run(self, molecule, work_dir=None, queue=None):
1007
967
if work_dir is None :
1008
968
work_dir = _os .getcwd ()
1009
969
1010
- # Convert SMILES to a molecule.
970
+ # Try to create a molecule from the SMILES string .
1011
971
if isinstance (molecule , str ):
1012
- is_smiles = True
1013
972
try :
1014
- new_mol = _smiles (molecule )
973
+ smiles = molecule
974
+ molecule = _smiles (molecule )
975
+ edit_mol = molecule ._sire_object .edit ()
976
+ edit_mol = edit_mol .rename (f"smiles:{ smiles } " ).molecule ()
977
+ molecule ._sire_object = edit_mol .commit ()
1015
978
except Exception as e :
1016
979
msg = "Unable to convert SMILES to Molecule using RDKit."
1017
980
if _isVerbose ():
1018
981
msg += ": " + getattr (e , "message" , repr (e ))
1019
982
raise _ThirdPartyError (msg ) from e
1020
983
else :
1021
984
raise _ThirdPartyError (msg ) from None
1022
- else :
1023
- is_smiles = False
1024
- new_mol = molecule .copy ()
985
+
986
+ # Create a copy of the molecule.
987
+ new_mol = molecule .copy ()
1025
988
1026
989
# Use the net molecular charge passed as an option.
1027
990
if self ._net_charge is not None :
@@ -1251,45 +1214,23 @@ def run(self, molecule, work_dir=None, queue=None):
1251
1214
# Make the molecule 'mol' compatible with 'par_mol'. This will create
1252
1215
# a mapping between atom indices in the two molecules and add all of
1253
1216
# the new properties from 'par_mol' to 'mol'.
1254
- if is_smiles :
1255
- new_mol = par_mol
1256
-
1257
- # We'll now add MolName and ResName info to the molecule, since
1258
- # this will be missing.
1259
-
1260
- # Rename the molecule with the original SMILES string.
1261
- edit_mol = new_mol ._sire_object .edit ()
1262
- edit_mol = edit_mol .rename (molecule ).molecule ()
1263
-
1264
- # Rename the residue LIG.
1265
- resname = _SireMol .ResName ("LIG" )
1266
- edit_mol = (
1267
- edit_mol .residue (_SireMol .ResIdx (0 ))
1268
- .rename (resname )
1269
- .molecule ()
1217
+ if self ._ensure_compatible :
1218
+ new_mol .makeCompatibleWith (
1219
+ par_mol ,
1220
+ property_map = self ._property_map ,
1221
+ overwrite = True ,
1222
+ verbose = False ,
1270
1223
)
1271
-
1272
- # Commit the changes.
1273
- new_mol ._sire_object = edit_mol .commit ()
1274
-
1275
1224
else :
1276
- if self . _ensure_compatible :
1225
+ try :
1277
1226
new_mol .makeCompatibleWith (
1278
1227
par_mol ,
1279
1228
property_map = self ._property_map ,
1280
1229
overwrite = True ,
1281
1230
verbose = False ,
1282
1231
)
1283
- else :
1284
- try :
1285
- new_mol .makeCompatibleWith (
1286
- par_mol ,
1287
- property_map = self ._property_map ,
1288
- overwrite = True ,
1289
- verbose = False ,
1290
- )
1291
- except :
1292
- new_mol = par_mol
1232
+ except :
1233
+ new_mol = par_mol
1293
1234
1294
1235
# Record the forcefield used to parameterise the molecule.
1295
1236
new_mol ._forcefield = ff
0 commit comments