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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# -*- coding: utf-8 -*-
# The DiverseSelector library provides a set of tools to select molecule
# subset with maximum molecular diversity.
#
# Copyright (C) 2022 The QC-Devs Community
#
# This file is part of DiverseSelector.
#
# DiverseSelector is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# DiverseSelector is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
#
# --

"""Feature generation module."""

import os
from pathlib import PurePath
import sys
from typing import Union

import numpy as np
import pandas as pd
from rdkit import Chem
from rdkit.Chem import AllChem, Descriptors, MACCSkeys, rdMHFPFingerprint, Draw

__all__ = [
    "DescriptorGenerator",
    "FingerprintGenerator",
    "feature_reader",
    "aug_features",
]


class DescriptorGenerator:
    """Compute molecular features."""

    def __init__(self,
                 mols: list = None):
        """Coinstructor of DescriptorGenerator."""
        self.mols = mols

    def mordred_desc(self, ignore_3D: bool = False):  # noqa: N803
        """Mordred molecular descriptor generation.

        Parameters
        ----------
        ignore_3D : bool, optional
            Ignore 3D coordinates. The default=False.

        Returns
        -------
        df_features: PandasDataFrame
            A `pandas.DataFrame` object with compute Mordred descriptors.

        """
        from mordred import Calculator, descriptors  # pylint: disable=C0415
        # if only compute 2D descriptors, set ignore_3D=True
        calc = Calculator(descs=descriptors, ignore_3D=ignore_3D)
        df_features = pd.DataFrame(calc.pandas(self.mols))

        return df_features

    @staticmethod
    def padelpy_desc(mol_file: Union[str, PurePath],
                     keep_csv: bool = False,
                     maxruntime: int = -1,
                     waitingjobs: int = -1,
                     threads: int = -1,
                     d_2d: bool = True,
                     d_3d: bool = True,
                     config: str = None,
                     convert3d: bool = False,
                     descriptortypes: str = None,
                     detectaromaticity: bool = False,
                     fingerprints: bool = False,
                     log: bool = False,
                     maxcpdperfile: int = 0,
                     removesalt: bool = False,
                     retain3d: bool = False,
                     standardizenitro: bool = False,
                     standardizetautomers: bool = False,
                     tautomerlist: str = None,
                     usefilenameasmolname: bool = False,
                     sp_timeout: int = None,
                     headless: bool = True):  # pylint: disable=R0201
        """PADEL molecular descriptor generation.

        Parameters
        ----------
        mol_file : str
            Molecule file name.
        keep_csv : bool, optional
            If True, the csv file is kept. Default=False.
        maxruntime : int, optional
            Additional keyword arguments.
            See https://github.com/ecrl/padelpy/blob/master/padelpy/wrapper.py.

        Returns
        -------
        df_features: PandasDataFrame
            A `pandas.DataFrame` object with compute Mordred descriptors.

        """
        cwd = os.path.dirname(os.path.abspath(__file__))
        sys.path.append(os.path.join(cwd, "padelpy"))

        from padelpy import padeldescriptor  # pylint: disable=C0415
        # if only compute 2D descriptors,
        # ignore_3D=True

        csv_fname = (
                str(os.path.basename(mol_file)).split(".", maxsplit=1)[0]
                + "_padel_descriptors.csv"
        )

        padeldescriptor(
            maxruntime=maxruntime,
            waitingjobs=waitingjobs,
            threads=threads,
            d_2d=d_2d,
            d_3d=d_3d,
            config=config,
            convert3d=convert3d,
            descriptortypes=descriptortypes,
            detectaromaticity=detectaromaticity,
            mol_dir=mol_file,
            d_file=csv_fname,
            fingerprints=fingerprints,
            log=log,
            maxcpdperfile=maxcpdperfile,
            removesalt=removesalt,
            retain3d=retain3d,
            retainorder=True,
            standardizenitro=standardizenitro,
            standardizetautomers=standardizetautomers,
            tautomerlist=tautomerlist,
            usefilenameasmolname=usefilenameasmolname,
            sp_timeout=sp_timeout,
            headless=headless,
        )

        df_features = pd.read_csv(csv_fname, sep=",", index_col="Name")

        if not keep_csv:
            os.remove(csv_fname)

        return df_features

    def rdkit_desc(self,
                   use_fragment: bool = True,
                   ipc_avg: bool = True):
        """Generation RDKit molecular descriptors.

        Parameters
        ----------
        use_fragment : bool, optional
            If True, the return value includes the fragment binary descriptors like "fr_XXX".
        ipc_avg : bool, optional
            If True, the IPC descriptor calculates with avg=True option.

        Returns
        -------
        df_features: PandasDataFrame
            A `pandas.DataFrame` object with compute Mordred descriptors.

        """
        # parsing descriptor information
        # parsing descriptor information
        desc_list = []
        descriptor_types = []
        for descriptor, function in Descriptors.descList:
            if use_fragment is False and descriptor.startswith("fr_"):
                continue
            descriptor_types.append(descriptor)
            desc_list.append((descriptor, function))

        # check initialization
        assert len(descriptor_types) == len(desc_list)

        arr_features = np.full(shape=(len(self.mols), len(desc_list)),
                               fill_value=np.nan)

        for idx_row, mol in enumerate(self.mols):
            # this part is modified from
            # https://github.com/deepchem/deepchem/blob/master/deepchem/feat/molecule_featurizers/
            # rdkit_descriptors.py#L11-L98
            for idx_col, (desc_name, function) in enumerate(desc_list):
                if desc_name == "Ipc" and ipc_avg:
                    feature = function(mol, avg=True)
                else:
                    feature = function(mol)
                arr_features[idx_row, idx_col] = feature

        df_features = pd.DataFrame(arr_features, columns=descriptor_types)

        return df_features

    def rdkit_frag_desc(self):
        """Generation of the RDKit fragment features.

        Returns
        -------
        df_features: PandasDataFrame
            A `pandas.DataFrame` object with compute Mordred descriptors.

        """
        # http://rdkit.org/docs/source/rdkit.Chem.Fragments.html
        # this implementation is taken from https://github.com/Ryan-Rhys/FlowMO/blob/
        # e221d989914f906501e1ad19cd3629d88eac1785/property_prediction/data_utils.py#L111
        fragments = {d[0]: d[1] for d in Descriptors.descList[115:]}
        frag_features = np.zeros((len(self.mols), len(fragments)))
        for idx, mol in enumerate(self.mols):
            features = [fragments[d](mol) for d in fragments]
            frag_features[idx, :] = features

        feature_names = [desc[0] for desc in Descriptors.descList[115:]]
        df_features = pd.DataFrame(data=frag_features, columns=feature_names)

        return df_features


class FingerprintGenerator:
    """Fingerprint generator."""

    def __init__(self, mols: list) -> None:
        """Fingerprint generator.

        Parameters
        ----------
        mols : RDKitMol
            Molecule object.
        """
        self.mols = mols

        # molecule names
        mol_names = [
            Chem.MolToSmiles(mol)
            if mol.GetPropsAsDict().get("_Name") is None
            else mol.GetProp("_Name")
            for mol in mols
        ]
        self.mol_names = mol_names

    def compute_fingerprint(
            self,
            fp_type: str = "SECFP",
            n_bits: int = 2048,
            radius: int = 3,
            min_radius: int = 1,
            random_seed: int = 12345,
            rings: bool = True,
            isomeric: bool = True,
            kekulize: bool = False,
    ):
        """Compute fingerprints.

        Parameters
        ----------
        fp_type : str, optional
            Supported fingerprints: SECFP, ECFP, Morgan, RDKitFingerprint and MACCSkeys.
            Default="SECFP".
        n_bits : int, optional
            Number of bits of fingerprint. Default=2048.
        radius : int, optional
            The maximum radius of the substructure that is generated at each atom. Default=3.
        min_radius : int, optional
            The minimum radius that is used to extract n-grams.
        random_seed : int, optional
            The random seed number. Default=12345.
        rings : bool, optional
            Whether the rings (SSSR) are extracted from the molecule and added to the shingling.
            Default=True.
        isomeric : bool, optional
            Whether the SMILES added to the shingling are isomeric. Default=False.
        kekulize : bool, optional
            Whether the SMILES added to the shingling are kekulized. Default=True.
        """
        if fp_type.upper() in [
            "SECFP",
            "ECFP",
            "MORGAN",
            "RDKFINGERPRINT",
            "MACCSKEYS",
        ]:
            fps = [
                self.rdkit_fingerprint_low(
                    mol,
                    fp_type=fp_type,
                    n_bits=n_bits,
                    radius=radius,
                    min_radius=min_radius,
                    random_seed=random_seed,
                    rings=rings,
                    isomeric=isomeric,
                    kekulize=kekulize,
                )
                for mol in self.mols
            ]
        # todo: add support of e3fp

        # other cases
        else:
            raise ValueError(f"{fp_type} is not an supported fingerprint type.")

        df_fps = pd.DataFrame(np.array(fps), index=self.mol_names)

        return df_fps

    @staticmethod
    def rdkit_fingerprint_low(
            mol,
            fp_type: str = "SECFP",
            n_bits: int = 2048,
            radius: int = 3,
            min_radius: int = 1,
            random_seed: int = 12345,
            rings: bool = True,
            isomeric: bool = False,
            kekulize: bool = False,
    ):
        """
        Generate required molecular fingerprints.

        Parameters
        ----------
        mols : RDKitMol
            Molecule object.
        fp_type : str, optional
            Supported fingerprints: SECFP, ECFP, Morgan, RDKitFingerprint and MACCSkeys.
            Default="SECFP".
        n_bits : int, optional
            Number of bits of fingerprint. Default=2048.
        radius : int, optional
            The maximum radius of the substructure that is generated at each atom. Default=3.
        min_radius : int, optional
            The minimum radius that is used to extract n-grams.
        random_seed : int, optional
            The random seed number. Default=12345.
        rings : bool, optional
            Whether the rings (SSSR) are extracted from the molecule and added to the shingling.
            Default=True.
        isomeric : bool, optional
            Whether the SMILES added to the shingling are isomeric. Default=False.
        kekulize : bool, optional
            Whether the SMILES added to the shingling are kekulized. Default=True.

        Returns
        -------
        fp : ExplicitBitVector
            The computed molecular fingerprint.

        Notes
        -----
        fingerprint types:
        1. topological fingerprints: RDKFingerprint, Tanimoto, Dice, Cosine, Sokal, Russel,
        Kulczynski, McConnaughey, and Tversky
        2. MACCS keys:
        3. Atom pairs and topological torsions
        4. Morgan fingerprints (circular fingerprints): Morgan, ECFP, FCFP

        """
        # SECFP: SMILES extended connectivity fingerprint
        # https://jcheminf.biomedcentral.com/articles/10.1186/s13321-018-0321-8
        if fp_type.upper() == "SECFP":
            secfp_encoder = rdMHFPFingerprint.MHFPEncoder(random_seed)
            fp = secfp_encoder.EncodeSECFPMol(
                mol,
                radius=radius,
                rings=rings,
                isomeric=isomeric,
                kekulize=kekulize,
                min_radius=min_radius,
                length=n_bits,
            )
        # ECFP
        # https://github.com/deepchem/deepchem/blob/1a2d2e9ff097fdbf58894d1f91359fe466c65810/deepchem/utils/rdkit_utils.py#L414
        # https://www.rdkit.org/docs/source/rdkit.Chem.rdMolDescriptors.html
        elif fp_type.upper() == "ECFP":
            # radius=3 --> ECFP6
            fp = AllChem.GetMorganFingerprintAsBitVect(
                mol=mol,
                radius=radius,
                nBits=n_bits,
                useChirality=isomeric,
                useFeatures=False,
            )
        elif fp_type.upper() == "MORGAN":
            fp = AllChem.GetMorganFingerprintAsBitVect(
                mol=mol,
                radius=radius,
                nBits=n_bits,
                useChirality=isomeric,
                useFeatures=True,
            )
        # https://www.rdkit.org/docs/source/rdkit.Chem.rdmolops.html#rdkit.Chem.rdmolops.RDKFingerprint
        elif fp_type.upper() == "RDKFINGERPRINT":
            fp = Chem.rdmolops.RDKFingerprint(
                mol=mol,
                minPath=1,
                # maxPath=mol.GetNumBonds(),
                maxPath=10,
                fpSize=n_bits,
                nBitsPerHash=2,
                useHs=True,
                tgtDensity=0,
                minSize=128,
                branchedPaths=True,
                useBondOrder=True,
            )
        # SMARTS-based implementation of the 166 public MACCS keys
        # https://www.rdkit.org/docs/GettingStartedInPython.html#fingerprinting-and-molecular-similarity
        elif fp_type == "MaCCSKeys":
            fp = MACCSkeys.GenMACCSKeys(mol)
        else:
            # todo: add more
            # https://github.com/keiserlab/e3fp
            # https://chemfp.readthedocs.io/en/latest/fp_types.html
            # https://xenonpy.readthedocs.io/en/stable/_modules/xenonpy/descriptor/fingerprint.html
            raise NotImplementedError(f"{fp_type} is not implemented yet.")

        return fp


def feature_reader(file_name: str,
                   sep: str = ",",
                   engine: str = "python",
                   **kwargs):
    """Load molecule features/descriptors.

    Parameters
    ----------
    file_name : str
        File name that provides molecular features.
    sep : str, optional
        Separator use for CSV like files. Default=",".
    engine : str, optional
        Engine name used for reading files, where "python" supports regular expression for CSV
        formats, “xlrd” supports old-style Excel files (.xls), “openpyxl” supports newer Excel file
        formats, “odf” supports OpenDocument file formats (.odf, .ods, .odt), “pyxlsb” supports
        binary Excel files. One should note that the dependency should be installed properly to
        make it work. Default="python".
    **kwargs
        Additional keyword arguments passed to
        `pd.read_csv() <https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html>`_
        or `pd.read_excel() <https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html>`_.

    Returns
    -------
    df : PandasDataFrame
        A `pandas.DataFrame` object with molecular features.
    """

    # use `str` function to support PosixPath
    if str(file_name).lower().endswith((".csv", ".txt")):
        df = pd.read_csv(file_name, sep=sep, engine=engine, *kwargs)
    elif (
        str(file_name)
        .lower()
        .endswith((".xlsx", ".xls", "xlsb", ".odf", ".ods", ".odt"))
    ):
        df = pd.read_excel(file_name, engine=engine, *kwargs)

    return df


def aug_features(features,
                 target_prop,
                 weight: Union[np.ndarray, float, int] = None) -> np.ndarray:
    r"""Augmented features.

    Parameters
    ----------
    features : np.ndarray or PandasDataFrame
        Molecular features.
    target_prop : np.ndarray or PandasDataFrame
        Target properties.
    weight : np.ndarray, optional
        Weight for each molecule. When weight is not provided (None), the property will be
        directly augmented to the features without repeats. Default=None.

    Returns
    -------
    aug_features : np.ndarray
        Augmented features matrix.

    Notes
    -----
    When the property becomes the focus instead of structural diversity, we can just use the
    property matrix as the feature matrix without using fingerprints or molecular descriptors.

    The augmented feature matrix is a concatenation of the original features matrix and the
    property matrix. When a weight matrix is provided, then the repeat-value for each property to
    be :math:`repeats = \left \lceil weight \times n_{features} \right \rceil`
    where :math:`n_{features}` denotes the number of features (length of the
    fingerprint/descriptors), and the final augmented features matrix is of feature matrix
    augmented by the property matrix for :math:`repeat` times.

    To achieve sampling with respect to target properties and diversity with respect to a
    fingerprint/feature-vector, you need to append the target properties to the feature vector.
    For each property, specify the :math:`repeats`: the more times you repeat the property
    value the higher its weight.

    """
    if isinstance(features, pd.DataFrame):
        features = features.to_numpy()
    if isinstance(target_prop, pd.DataFrame):
        target_prop = target_prop.to_numpy()

    # define the repeat-value for each property
    if weight is not None:
        repeats = np.ceil(np.multiply(weight, features.shape[1]))
    else:
        repeats = 1

    features_new = np.hstack((features,
                              np.repeat(target_prop, repeats=repeats, axis=1)))

    return features_new