Statistiques
| Branche: | Tag: | Révision :

dockonsurf / modules / formats.py @ 8d08beb4

Historique | Voir | Annoter | Télécharger (2,02 ko)

1 e23f119b Carles
"""Module for the conversion of coordinate files to library-workable objects
2 e23f119b Carles

3 e23f119b Carles
functions:
4 83f022c9 Carles
adapt_format: Converts the coordinate files into a required library object type
5 e23f119b Carles
"""
6 e23f119b Carles
7 e23f119b Carles
import logging
8 89a980fc Carles
logger = logging.getLogger('DockOnSurf')
9 e23f119b Carles
10 e23f119b Carles
11 83f022c9 Carles
def adapt_format(requirement, coord_file):
12 e23f119b Carles
    """Converts the coordinate files into a required library object type.
13 e23f119b Carles

14 e23f119b Carles
    Depending on the library required to use and the file type, it converts the
15 e23f119b Carles
    coordinate file into a library-workable object.
16 e23f119b Carles
    @param requirement: str, the library for which the conversion should be
17 e23f119b Carles
    made. Accepted values: 'ase', 'rdkit'.
18 e23f119b Carles
    @param coord_file: str, path to the coordinates file aiming to convert.
19 e23f119b Carles
    Accepted file tyoes: 'xyz', 'mol'.
20 e23f119b Carles
    @return: an object the required library can work with.
21 e23f119b Carles
    """
22 8ab593ee Carles
    from ase.io.formats import filetype
23 8ab593ee Carles
24 8ab593ee Carles
    req_vals = ['rdkit', 'ase']
25 8ab593ee Carles
    file_type_vals = ['xyz', 'mol']
26 4381145e Carles
    lib_err = f"The conversion to the '{requirement}' library object type" \
27 4381145e Carles
              f" has not yet been implemented"
28 4381145e Carles
    conv_info = f"Converted {coord_file} to {requirement} object type"
29 4381145e Carles
30 4381145e Carles
    fil_type_err = f'The {filetype( coord_file )} file formnat is not supported'
31 4381145e Carles
32 4381145e Carles
    if requirement not in req_vals:
33 9f7bb440 Carles
        logger.error(lib_err)
34 4381145e Carles
        raise NotImplementedError(lib_err)
35 4381145e Carles
36 4381145e Carles
    if filetype(coord_file) not in file_type_vals:
37 9f7bb440 Carles
        logger.error(fil_type_err)
38 4381145e Carles
        raise NotImplementedError(fil_type_err)
39 8ab593ee Carles
40 8ab593ee Carles
    if requirement == 'rdkit':
41 8ab593ee Carles
        if filetype(coord_file) == 'xyz':
42 4381145e Carles
            from xyz2mol import read_xyz_file, xyz2mol
43 8ab593ee Carles
            atoms, charge, xyz_coordinates = read_xyz_file(coord_file)
44 8ab593ee Carles
            rd_mol_obj = xyz2mol(atoms, xyz_coordinates, charge=charge)
45 8d08beb4 Carles
            logger.debug(conv_info)
46 8ab593ee Carles
            return rd_mol_obj
47 8ab593ee Carles
        elif filetype(coord_file) == 'mol':
48 8ab593ee Carles
            from rdkit.Chem import MolFromMolFile
49 8d08beb4 Carles
            logger.debug(conv_info)
50 b9551fc2 Carles
            return MolFromMolFile(coord_file, removeHs=False)
51 8ab593ee Carles
52 8ab593ee Carles
    if requirement == 'ase':
53 8ab593ee Carles
        import ase.io
54 8d08beb4 Carles
        logger.debug(conv_info)
55 8ab593ee Carles
        return ase.io.read(coord_file)