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

dockonsurf / modules / formats.py @ f3004731

Historique | Voir | Annoter | Télécharger (3,17 ko)

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

3
functions:
4
confs_to_mol_list: Converts the conformers inside a rdkit mol object to a list
5
    of separate mol objects.
6
rdkit_mol_to_ase_atoms: Converts a rdkit mol object into ase Atoms object.
7
adapt_format: Converts the coordinate files into a required library object type
8
"""
9

    
10
import logging
11

    
12
import rdkit.Chem.AllChem as Chem
13

    
14
logger = logging.getLogger('DockOnSurf')
15

    
16

    
17
def confs_to_mol_list(mol: Chem.rdchem.Mol, idx_lst=None):
18
    """Converts the conformers inside a rdkit mol object to a list of
19
    separate mol objects.
20

21
    @param mol: rdkit mol object containing at least one conformer.
22
    @param idx_lst:
23
    @return: list of separate mol objects.
24
    """
25
    if idx_lst is None:
26
        idx_lst = list(range(mol.GetNumConformers()))
27
    return [Chem.MolFromMolBlock(Chem.MolToMolBlock(mol, confId=int(idx)),
28
                                 removeHs=False) for idx in idx_lst]
29

    
30

    
31
def rdkit_mol_to_ase_atoms(mol: Chem.rdchem.Mol):
32
    """Converts a rdkit mol object into ase Atoms object.
33
    @param mol: rdkit mol object containing only one conformer.
34
    @return ase.Atoms: ase Atoms object with the same coordinates.
35
    """
36
    from ase import Atoms
37
    symbols = [atm.GetSymbol() for atm in mol.GetAtoms()]
38
    positions = mol.GetConformer(0).GetPositions()
39
    return Atoms(symbols=symbols, positions=positions)
40

    
41

    
42
def adapt_format(requirement, coord_file):
43
    """Converts the coordinate files into a required library object type.
44

45
    Depending on the library required to use and the file type, it converts the
46
    coordinate file into a library-workable object.
47
    @param requirement: str, the library for which the conversion should be
48
    made. Accepted values: 'ase', 'rdkit'.
49
    @param coord_file: str, path to the coordinates file aiming to convert.
50
    Accepted file tyoes: 'xyz', 'mol'.
51
    @return: an object the required library can work with.
52
    """
53
    from ase.io.formats import filetype
54

    
55
    req_vals = ['rdkit', 'ase']
56
    file_type_vals = ['xyz', 'mol']
57
    lib_err = f"The conversion to the '{requirement}' library object type" \
58
              f" has not yet been implemented"
59
    conv_info = f"Converted {coord_file} to {requirement} object type"
60

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

    
63
    if requirement not in req_vals:
64
        logger.error(lib_err)
65
        raise NotImplementedError(lib_err)
66

    
67
    if filetype(coord_file) not in file_type_vals:
68
        logger.error(fil_type_err)
69
        raise NotImplementedError(fil_type_err)
70

    
71
    if requirement == 'rdkit':
72
        if filetype(coord_file) == 'xyz':
73
            from xyz2mol import read_xyz_file, xyz2mol
74
            atoms, charge, xyz_coordinates = read_xyz_file(coord_file)
75
            rd_mol_obj = xyz2mol(atoms, xyz_coordinates, charge=charge)
76
            logger.debug(conv_info)
77
            return rd_mol_obj
78
        elif filetype(coord_file) == 'mol':
79
            from rdkit.Chem import MolFromMolFile
80
            logger.debug(conv_info)
81
            return MolFromMolFile(coord_file, removeHs=False)
82

    
83
    if requirement == 'ase':
84
        import ase.io
85
        logger.debug(conv_info)
86
        return ase.io.read(coord_file)