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

dockonsurf / modules / formats.py @ 8d08beb4

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

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

3
functions:
4
adapt_format: Converts the coordinate files into a required library object type
5
"""
6

    
7
import logging
8
logger = logging.getLogger('DockOnSurf')
9

    
10

    
11
def adapt_format(requirement, coord_file):
12
    """Converts the coordinate files into a required library object type.
13

14
    Depending on the library required to use and the file type, it converts the
15
    coordinate file into a library-workable object.
16
    @param requirement: str, the library for which the conversion should be
17
    made. Accepted values: 'ase', 'rdkit'.
18
    @param coord_file: str, path to the coordinates file aiming to convert.
19
    Accepted file tyoes: 'xyz', 'mol'.
20
    @return: an object the required library can work with.
21
    """
22
    from ase.io.formats import filetype
23

    
24
    req_vals = ['rdkit', 'ase']
25
    file_type_vals = ['xyz', 'mol']
26
    lib_err = f"The conversion to the '{requirement}' library object type" \
27
              f" has not yet been implemented"
28
    conv_info = f"Converted {coord_file} to {requirement} object type"
29

    
30
    fil_type_err = f'The {filetype( coord_file )} file formnat is not supported'
31

    
32
    if requirement not in req_vals:
33
        logger.error(lib_err)
34
        raise NotImplementedError(lib_err)
35

    
36
    if filetype(coord_file) not in file_type_vals:
37
        logger.error(fil_type_err)
38
        raise NotImplementedError(fil_type_err)
39

    
40
    if requirement == 'rdkit':
41
        if filetype(coord_file) == 'xyz':
42
            from xyz2mol import read_xyz_file, xyz2mol
43
            atoms, charge, xyz_coordinates = read_xyz_file(coord_file)
44
            rd_mol_obj = xyz2mol(atoms, xyz_coordinates, charge=charge)
45
            logger.debug(conv_info)
46
            return rd_mol_obj
47
        elif filetype(coord_file) == 'mol':
48
            from rdkit.Chem import MolFromMolFile
49
            logger.debug(conv_info)
50
            return MolFromMolFile(coord_file, removeHs=False)
51

    
52
    if requirement == 'ase':
53
        import ase.io
54
        logger.debug(conv_info)
55
        return ase.io.read(coord_file)