9
9
from openbabel .pybel import readfile
10
10
from openbabel .pybel import Molecule as PybelWrapper
11
11
12
- from rdkit .Chem import MolFromMolBlock , MolFromMolFile , SanitizeFlags , SanitizeMol
12
+ from rdkit .Chem import MolFromMolBlock , MolFromMolFile , SanitizeFlags , SanitizeMol , MolToMolFile
13
13
14
14
from luna .util .file import is_directory_valid , get_unique_filename , remove_files
15
15
from luna .util .default_values import ENTRY_SEPARATOR , OPENBABEL
23
23
from luna .mol .standardiser import ResiduesStandardiser
24
24
from luna .mol .wrappers .base import MolWrapper
25
25
from luna .mol .wrappers .obabel import convert_molecule
26
+ from luna .mol .wrappers .rdkit import read_mol_from_file
27
+
28
+ from luna .mol .templates import LigandExpoTemplate
26
29
27
30
from luna .util .exceptions import (IllegalArgumentError , MoleculeNotFoundError , ChainNotFoundError ,
28
31
FileNotCreated , PDBNotReadError , MoleculeSizeError , MoleculeObjectError )
@@ -295,8 +298,9 @@ def get_residue_neighbors(residue, select=Select()):
295
298
296
299
297
300
def biopython_entity_to_mol (entity , select = Select (), validate_mol = True , standardize_mol = True ,
298
- add_h = False , ph = None , break_metal_bonds = False , mol_obj_type = "rdkit" , openbabel = OPENBABEL ,
299
- tmp_path = None , keep_tmp_files = False ):
301
+ template = None , add_h = False , ph = None ,
302
+ break_metal_bonds = False , mol_obj_type = "rdkit" , wrapped = True ,
303
+ openbabel = OPENBABEL , tmp_path = None , keep_tmp_files = False ):
300
304
301
305
tmp_path = tmp_path or tempfile .gettempdir ()
302
306
@@ -312,10 +316,24 @@ def biopython_entity_to_mol(entity, select=Select(), validate_mol=True, standard
312
316
313
317
logger .debug ("First: try to create a new PDB file (%s) from the provided entity." % pdb_file )
314
318
# Apparently, Open Babel creates a bug when it tries to parse a file with CONECTS containing serial numbers with more than 4 digits.
315
- # E.g.: 1OZH:A:HE3:1406, line CONECT162811627916282.
316
- # By setting preserve_atom_numbering to False, it seems the problem is solved.
319
+ # E.g.: 1OZH:A:HE3:1406, line CONECT162811627916282. By setting preserve_atom_numbering to False, it solves the problem.
317
320
save_to_file (entity , pdb_file , select , preserve_atom_numbering = False )
318
321
322
+ ini_input_file = pdb_file
323
+ if template is not None :
324
+ if entity .level == "R" and entity .is_hetatm ():
325
+ # Note that the template molecule should have no explicit hydrogens else the algorithm will fail.
326
+ rdmol = read_mol_from_file (pdb_file , mol_format = "pdb" , removeHs = True )
327
+ new_rdmol = template .assign_bond_order (rdmol , entity .resname )
328
+
329
+ ini_input_file = '%s_tmp-mol-file.mol' % filename
330
+ MolToMolFile (new_rdmol , ini_input_file )
331
+
332
+ if not keep_tmp_files :
333
+ remove_files ([pdb_file ])
334
+ else :
335
+ logger .warning ("It cannot apply a template on the provided entity because it should be a single compound (Residue class)." )
336
+
319
337
# Convert the PDB file to Mol file with the proper protonation and hydrogen addition if required.
320
338
mol_file = '%s_mol-file.mol' % filename
321
339
ob_opt = {"error-level" : 5 }
@@ -326,7 +344,7 @@ def biopython_entity_to_mol(entity, select=Select(), validate_mol=True, standard
326
344
ob_opt ["p" ] = ph
327
345
else :
328
346
ob_opt ["h" ] = ""
329
- convert_molecule (pdb_file , mol_file , opt = ob_opt , openbabel = openbabel )
347
+ convert_molecule (ini_input_file , mol_file , opt = ob_opt , openbabel = openbabel )
330
348
331
349
# Currently, ignored atoms are only metals.
332
350
ignored_atoms = []
@@ -429,6 +447,9 @@ def biopython_entity_to_mol(entity, select=Select(), validate_mol=True, standard
429
447
430
448
# Remove temporary files.
431
449
if not keep_tmp_files :
432
- remove_files ([pdb_file , mol_file ])
450
+ remove_files ([ini_input_file , mol_file ])
451
+
452
+ if wrapped :
453
+ mol_obj = MolWrapper (mol_obj )
433
454
434
455
return mol_obj , ignored_atoms
0 commit comments