File size: 3,224 Bytes
56793c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8

"""Utility functions."""

import sys
import os
import glob
import tempfile
import shutil
from pathlib import Path
from typing import Tuple

from rdkit import Chem
from jdk import install as _jre_install, _JRE_DIR


def install_jre(version: int = 19):
    """Install a Java Runtime Environment."""
    path = get_jre_in_dir(_JRE_DIR, version)
    if len(path) == 0:
        # Could not find JRE, install it
        _ = _jre_install(version, jre=True)
        path = get_jre_in_dir(_JRE_DIR, version)
    return path


def make_temp_jre() -> Tuple[str, str]:
    """Copy the installed JRE to a temporary file.



    Allows multiprocessing capacities.



    :return: a tuple of (path to temp dir, path to the JRE)

    """
    # Path of JRE installation
    install_path = [path for path in Path(install_jre()).parents
                    if path.as_posix().endswith('jre') and not path.as_posix().endswith('.jre')][0]
    # Path of temp dir
    outdir = mktempdir()
    # Make copy into dir
    shutil.copytree(install_path, os.path.join(outdir, 'jre'))
    # Find JRE in temp dir
    path = get_jre_in_dir(outdir)
    return outdir, path


def get_jre_in_dir(dir: str):
    """Recursively search the directory to find a JRE."""
    paths = glob.glob(os.path.join(dir, '**', 'server',
                                   'jvm.dll' if sys.platform == "win32" else 'libjvm.so'
                                   ), recursive=True)
    path = [path for path in paths if f'jre-{version}' in path]
    if len(path):
        return path[0]
    return None


def install_java(version: int = 19):
    """Install a Java Runtime Environment."""
    path = get_java_in_dir(_JRE_DIR, version)
    if path is None:
        # Could not find JRE, install it
        _ = _jre_install(version, jre=True)
        path = get_java_in_dir(_JRE_DIR, version)
    return path


def get_java_in_dir(dir: str, version: int):
    """Recursively search the directory to find a JRE."""
    paths = glob.glob(os.path.join(dir, '**', 'bin',
                                  'java.exe' if sys.platform == "win32" else 'java'
                                  ), recursive=True)
    path = [path for path in paths if f'jdk-{version}' in path]
    if len(path):
        return os.path.abspath(path[0])
    return None


def mktempdir(suffix: str = None) -> str:
    """Return the path to a writeable temporary directory."""
    dir = tempfile.mkdtemp(suffix=suffix)
    return dir


def mktempfile(suffix: str = None) -> str:
    """Return the path to a writeable temporary file."""
    file = tempfile.mkstemp(suffix=suffix)
    os.close(file[0])
    return file[1]


def needsHs(mol: Chem.Mol) -> bool:
    """Return if the molecule lacks hydrogen atoms or not.



    :param mol: RDKit Molecule

    :return: True if the molecule lacks hydrogens.

    """
    for atom in mol.GetAtoms():
        nHNbrs = 0
        for nbr in atom.GetNeighbors():
            if nbr.GetAtomicNum() == 1:
                nHNbrs += 1
        noNeighbors = False
        if atom.GetTotalNumHs(noNeighbors) > nHNbrs:
            return True
    return False