#!/usr/bin/env python
# (C) 2017 OpenEye Scientific Software Inc. All rights reserved.
#
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of OpenEye products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. OpenEye claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable OpenEye offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be
# liable for any damages or liability in connection with the Sample Code
# or its use.

# @ <SNIPPET>
from __future__ import print_function
from openeye import oechem


class IsAtomGroup(oechem.OEUnaryGroupPred):
    def __call__(self, group):
        return group.NumAtoms() > 0


class IsBondGroup(oechem.OEUnaryGroupPred):
    def __call__(self, group):
        return group.NumBonds() > 0


def DumpGroup(group):
    print("type \"%s\"" % oechem.OEGetTag(group.GetGroupType()), end=" ")
    if group.NumAtoms() > 0:
        print("atom indices:", end=" ")
        for atom in group.GetAtoms():
            print(atom.GetIdx(), end=" ")
        print()
    if group.NumBonds() > 0:
        print("bond indices:", end=" ")
        for bond in group.GetBonds():
            print(bond.GetIdx(), end=" ")
        print()


def DumpGroups(mol):
    print("groups of", mol.GetTitle())
    print("number of atom groups", oechem.OECount(mol, IsAtomGroup()))
    print("number of bond groups", oechem.OECount(mol, IsBondGroup()))
    print("number of aromatic atoms groups", oechem.OECount(mol,
                                                            oechem.OEHasGroupType(oechem.OEGetTag("aromatic atoms"))))
    print("number of aromatic bonds groups", oechem.OECount(mol,
                                                            oechem.OEHasGroupType(oechem.OEGetTag("aromatic bonds"))))
    # loop over groups
    for g in mol.GetGroups():
        DumpGroup(g)
    print()


mol = oechem.OEGraphMol()
oechem.OESmilesToMol(mol, "CN1c2ccc(cc2C(=NCC1=O)c3ccccc3)Cl")
mol.SetTitle("valium")

# generate some groups
atoms = oechem.OEAtomVector()
for atom in mol.GetAtoms(oechem.OEIsAromaticAtom()):
    atoms.append(atom)
atomgroup = mol.NewGroup(oechem.OEGetTag("aromatic atoms"), atoms)

bonds = oechem.OEBondVector()
for bond in mol.GetBonds(oechem.OEIsAromaticBond()):
    bonds.append(bond)
bondgroup = mol.NewGroup(oechem.OEGetTag("aromatic bonds"), bonds)

DumpGroups(mol)
# delete a group
mol.DeleteGroup(bondgroup)
DumpGroups(mol)
# @ </SNIPPET>
