dockonsurf / modules / formats.py @ 439ce5f7
Historique | Voir | Annoter | Télécharger (3,53 ko)
1 | e23f119b | Carles | """Module for the conversion of coordinate files to library-workable objects
|
---|---|---|---|
2 | e23f119b | Carles |
|
3 | e23f119b | Carles | functions:
|
4 | f3004731 | Carles | confs_to_mol_list: Converts the conformers inside a rdkit mol object to a list
|
5 | f3004731 | Carles | of separate mol objects.
|
6 | f3004731 | Carles | rdkit_mol_to_ase_atoms: Converts a rdkit mol object into ase Atoms object.
|
7 | 83f022c9 | Carles | adapt_format: Converts the coordinate files into a required library object type
|
8 | e23f119b | Carles | """
|
9 | e23f119b | Carles | |
10 | e23f119b | Carles | import logging |
11 | f3004731 | Carles | |
12 | f3004731 | Carles | import rdkit.Chem.AllChem as Chem |
13 | f3004731 | Carles | |
14 | 89a980fc | Carles | logger = logging.getLogger('DockOnSurf')
|
15 | e23f119b | Carles | |
16 | e23f119b | Carles | |
17 | f3004731 | Carles | def confs_to_mol_list(mol: Chem.rdchem.Mol, idx_lst=None): |
18 | f3004731 | Carles | """Converts the conformers inside a rdkit mol object to a list of
|
19 | f3004731 | Carles | separate mol objects.
|
20 | f3004731 | Carles |
|
21 | f3004731 | Carles | @param mol: rdkit mol object containing at least one conformer.
|
22 | 9510666f | Carles | @param idx_lst: list of conformer indices to be considered. If not passed,
|
23 | 9510666f | Carles | all conformers are considered.
|
24 | f3004731 | Carles | @return: list of separate mol objects.
|
25 | f3004731 | Carles | """
|
26 | f3004731 | Carles | if idx_lst is None: |
27 | f3004731 | Carles | idx_lst = list(range(mol.GetNumConformers())) |
28 | f3004731 | Carles | return [Chem.MolFromMolBlock(Chem.MolToMolBlock(mol, confId=int(idx)), |
29 | f3004731 | Carles | removeHs=False) for idx in idx_lst] |
30 | f3004731 | Carles | |
31 | f3004731 | Carles | |
32 | f3004731 | Carles | def rdkit_mol_to_ase_atoms(mol: Chem.rdchem.Mol): |
33 | f3004731 | Carles | """Converts a rdkit mol object into ase Atoms object.
|
34 | f3004731 | Carles | @param mol: rdkit mol object containing only one conformer.
|
35 | f3004731 | Carles | @return ase.Atoms: ase Atoms object with the same coordinates.
|
36 | f3004731 | Carles | """
|
37 | f3004731 | Carles | from ase import Atoms |
38 | 4933cb8a | Carles Martí | if mol.GetNumConformers() > 1: |
39 | 9510666f | Carles | logger.warning('A mol object with multiple conformers is parsed, '
|
40 | 9510666f | Carles | 'converting to Atoms only the first conformer')
|
41 | f3004731 | Carles | symbols = [atm.GetSymbol() for atm in mol.GetAtoms()] |
42 | f3004731 | Carles | positions = mol.GetConformer(0).GetPositions()
|
43 | f3004731 | Carles | return Atoms(symbols=symbols, positions=positions)
|
44 | f3004731 | Carles | |
45 | f3004731 | Carles | |
46 | 83f022c9 | Carles | def adapt_format(requirement, coord_file): |
47 | e23f119b | Carles | """Converts the coordinate files into a required library object type.
|
48 | e23f119b | Carles |
|
49 | e23f119b | Carles | Depending on the library required to use and the file type, it converts the
|
50 | e23f119b | Carles | coordinate file into a library-workable object.
|
51 | e23f119b | Carles | @param requirement: str, the library for which the conversion should be
|
52 | e23f119b | Carles | made. Accepted values: 'ase', 'rdkit'.
|
53 | e23f119b | Carles | @param coord_file: str, path to the coordinates file aiming to convert.
|
54 | e23f119b | Carles | Accepted file tyoes: 'xyz', 'mol'.
|
55 | e23f119b | Carles | @return: an object the required library can work with.
|
56 | e23f119b | Carles | """
|
57 | 439ce5f7 | Carles | import ase.io |
58 | 8ab593ee | Carles | from ase.io.formats import filetype |
59 | 8ab593ee | Carles | |
60 | 8ab593ee | Carles | req_vals = ['rdkit', 'ase'] |
61 | 8ab593ee | Carles | file_type_vals = ['xyz', 'mol'] |
62 | 4381145e | Carles | lib_err = f"The conversion to the '{requirement}' library object type" \
|
63 | 4381145e | Carles | f" has not yet been implemented"
|
64 | 4381145e | Carles | conv_info = f"Converted {coord_file} to {requirement} object type"
|
65 | 4381145e | Carles | |
66 | f3004731 | Carles | fil_type_err = f'The {filetype(coord_file)} file formnat is not supported'
|
67 | 4381145e | Carles | |
68 | 4381145e | Carles | if requirement not in req_vals: |
69 | 9f7bb440 | Carles | logger.error(lib_err) |
70 | 4381145e | Carles | raise NotImplementedError(lib_err) |
71 | 4381145e | Carles | |
72 | 4381145e | Carles | if filetype(coord_file) not in file_type_vals: |
73 | 9f7bb440 | Carles | logger.error(fil_type_err) |
74 | 4381145e | Carles | raise NotImplementedError(fil_type_err) |
75 | 8ab593ee | Carles | |
76 | 8ab593ee | Carles | if requirement == 'rdkit': |
77 | 8ab593ee | Carles | if filetype(coord_file) == 'xyz': |
78 | 439ce5f7 | Carles | from xyz2mol import xyz2mol |
79 | 439ce5f7 | Carles | ase_atms = ase.io.read(coord_file) |
80 | 439ce5f7 | Carles | atomic_nums = ase_atms.get_atomic_numbers().tolist() |
81 | 439ce5f7 | Carles | xyz_coordinates = ase_atms.positions.tolist() |
82 | 439ce5f7 | Carles | rd_mol_obj = xyz2mol(atomic_nums, xyz_coordinates, charge=0) # TODO Charge |
83 | 8d08beb4 | Carles | logger.debug(conv_info) |
84 | 8ab593ee | Carles | return rd_mol_obj
|
85 | 8ab593ee | Carles | elif filetype(coord_file) == 'mol': |
86 | 8ab593ee | Carles | from rdkit.Chem import MolFromMolFile |
87 | 8d08beb4 | Carles | logger.debug(conv_info) |
88 | b9551fc2 | Carles | return MolFromMolFile(coord_file, removeHs=False) |
89 | 8ab593ee | Carles | |
90 | 8ab593ee | Carles | if requirement == 'ase': |
91 | 8d08beb4 | Carles | logger.debug(conv_info) |
92 | 8ab593ee | Carles | return ase.io.read(coord_file) |