dockonsurf / modules / formats.py @ f3004731
Historique | Voir | Annoter | Télécharger (3,17 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 | f3004731 | Carles | @param idx_lst:
|
23 | f3004731 | Carles | @return: list of separate mol objects.
|
24 | f3004731 | Carles | """
|
25 | f3004731 | Carles | if idx_lst is None: |
26 | f3004731 | Carles | idx_lst = list(range(mol.GetNumConformers())) |
27 | f3004731 | Carles | return [Chem.MolFromMolBlock(Chem.MolToMolBlock(mol, confId=int(idx)), |
28 | f3004731 | Carles | removeHs=False) for idx in idx_lst] |
29 | f3004731 | Carles | |
30 | f3004731 | Carles | |
31 | f3004731 | Carles | def rdkit_mol_to_ase_atoms(mol: Chem.rdchem.Mol): |
32 | f3004731 | Carles | """Converts a rdkit mol object into ase Atoms object.
|
33 | f3004731 | Carles | @param mol: rdkit mol object containing only one conformer.
|
34 | f3004731 | Carles | @return ase.Atoms: ase Atoms object with the same coordinates.
|
35 | f3004731 | Carles | """
|
36 | f3004731 | Carles | from ase import Atoms |
37 | f3004731 | Carles | symbols = [atm.GetSymbol() for atm in mol.GetAtoms()] |
38 | f3004731 | Carles | positions = mol.GetConformer(0).GetPositions()
|
39 | f3004731 | Carles | return Atoms(symbols=symbols, positions=positions)
|
40 | f3004731 | Carles | |
41 | f3004731 | Carles | |
42 | 83f022c9 | Carles | def adapt_format(requirement, coord_file): |
43 | e23f119b | Carles | """Converts the coordinate files into a required library object type.
|
44 | e23f119b | Carles |
|
45 | e23f119b | Carles | Depending on the library required to use and the file type, it converts the
|
46 | e23f119b | Carles | coordinate file into a library-workable object.
|
47 | e23f119b | Carles | @param requirement: str, the library for which the conversion should be
|
48 | e23f119b | Carles | made. Accepted values: 'ase', 'rdkit'.
|
49 | e23f119b | Carles | @param coord_file: str, path to the coordinates file aiming to convert.
|
50 | e23f119b | Carles | Accepted file tyoes: 'xyz', 'mol'.
|
51 | e23f119b | Carles | @return: an object the required library can work with.
|
52 | e23f119b | Carles | """
|
53 | 8ab593ee | Carles | from ase.io.formats import filetype |
54 | 8ab593ee | Carles | |
55 | 8ab593ee | Carles | req_vals = ['rdkit', 'ase'] |
56 | 8ab593ee | Carles | file_type_vals = ['xyz', 'mol'] |
57 | 4381145e | Carles | lib_err = f"The conversion to the '{requirement}' library object type" \
|
58 | 4381145e | Carles | f" has not yet been implemented"
|
59 | 4381145e | Carles | conv_info = f"Converted {coord_file} to {requirement} object type"
|
60 | 4381145e | Carles | |
61 | f3004731 | Carles | fil_type_err = f'The {filetype(coord_file)} file formnat is not supported'
|
62 | 4381145e | Carles | |
63 | 4381145e | Carles | if requirement not in req_vals: |
64 | 9f7bb440 | Carles | logger.error(lib_err) |
65 | 4381145e | Carles | raise NotImplementedError(lib_err) |
66 | 4381145e | Carles | |
67 | 4381145e | Carles | if filetype(coord_file) not in file_type_vals: |
68 | 9f7bb440 | Carles | logger.error(fil_type_err) |
69 | 4381145e | Carles | raise NotImplementedError(fil_type_err) |
70 | 8ab593ee | Carles | |
71 | 8ab593ee | Carles | if requirement == 'rdkit': |
72 | 8ab593ee | Carles | if filetype(coord_file) == 'xyz': |
73 | 4381145e | Carles | from xyz2mol import read_xyz_file, xyz2mol |
74 | 8ab593ee | Carles | atoms, charge, xyz_coordinates = read_xyz_file(coord_file) |
75 | 8ab593ee | Carles | rd_mol_obj = xyz2mol(atoms, xyz_coordinates, charge=charge) |
76 | 8d08beb4 | Carles | logger.debug(conv_info) |
77 | 8ab593ee | Carles | return rd_mol_obj
|
78 | 8ab593ee | Carles | elif filetype(coord_file) == 'mol': |
79 | 8ab593ee | Carles | from rdkit.Chem import MolFromMolFile |
80 | 8d08beb4 | Carles | logger.debug(conv_info) |
81 | b9551fc2 | Carles | return MolFromMolFile(coord_file, removeHs=False) |
82 | 8ab593ee | Carles | |
83 | 8ab593ee | Carles | if requirement == 'ase': |
84 | 8ab593ee | Carles | import ase.io |
85 | 8d08beb4 | Carles | logger.debug(conv_info) |
86 | 8ab593ee | Carles | return ase.io.read(coord_file) |