"vscode:/vscode.git/clone" did not exist on "9f9ce41f4728b0de40b4a8b8d8af365f29f6f5cc"
test_rdkit_utils.py 1.49 KB
Newer Older
1
2
3
4
5
import numpy as np
import os
import shutil

from dgl.data.utils import download, _get_dgl_url, extract_archive
6
from dgllife.utils.rdkit_utils import get_mol_3d_coordinates, load_molecule
7
8
9
10
11
12
from rdkit import Chem
from rdkit.Chem import AllChem

def test_get_mol_3D_coordinates():
    mol = Chem.MolFromSmiles('CCO')
    # Test the case when conformation does not exist
13
    assert get_mol_3d_coordinates(mol) is None
14
15
16
17

    # Test the case when conformation exists
    AllChem.EmbedMolecule(mol)
    AllChem.MMFFOptimizeMolecule(mol)
18
    coords = get_mol_3d_coordinates(mol)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    assert isinstance(coords, np.ndarray)
    assert coords.shape == (mol.GetNumAtoms(), 3)

def remove_dir(dir):
    if os.path.isdir(dir):
        try:
            shutil.rmtree(dir)
        except OSError:
            pass

def test_load_molecule():
    remove_dir('tmp1')
    remove_dir('tmp2')

    url = _get_dgl_url('dgllife/example_mols.tar.gz')
    local_path = 'tmp1/example_mols.tar.gz'
    download(url, path=local_path)
    extract_archive(local_path, 'tmp2')

    load_molecule('tmp2/example_mols/example.sdf')
    load_molecule('tmp2/example_mols/example.mol2', use_conformation=False, sanitize=True)
    load_molecule('tmp2/example_mols/example.pdbqt', calc_charges=True)
    mol, _ = load_molecule('tmp2/example_mols/example.pdb', remove_hs=True)
    assert mol.GetNumAtoms() == mol.GetNumHeavyAtoms()

    remove_dir('tmp1')
    remove_dir('tmp2')

if __name__ == '__main__':
    test_get_mol_3D_coordinates()
    test_load_molecule()