#!/usr/bin/env python3
# (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.

#############################################################################
#
#############################################################################

import sys
from openeye import oechem


def main(argv=[__name__]):

    itf = oechem.OEInterface(InterfaceData)

    if not oechem.OEParseCommandLine(itf, argv):
        return 1

    iname = itf.GetString("-in")

    # check input/output files

    ifs = oechem.oemolistream()
    if not ifs.open(iname):
        oechem.OEThrow.Fatal("Cannot open input file!")

    # read a molecule

    mol = oechem.OEGraphMol()
    if not oechem.OEReadMolecule(ifs, mol):
        oechem.OEThrow.Fatal("Cannot read input file!")

    for atom in mol.GetAtoms(oechem.OEAtomIsInRing()):

        sameringatoms = []

        for ringatom in mol.GetAtoms(oechem.OEAtomIsInRing()):

            if AtomsInSameRing(atom, ringatom):
                sameringatoms.append(ringatom)

        if len(sameringatoms) != 0:
            sameringstr = " ".join([str(a) for a in sameringatoms])
            print("atom %s in the same ring as %s" % (str(atom), sameringstr))


class ChainAtomOrAlreadyTraversed(oechem.OEUnaryAtomPred):
    def __init__(self, exclude):
        oechem.OEUnaryAtomPred.__init__(self)
        self.exclude = exclude

    def __call__(self, atom):
        if not atom.IsInRing():
            return False
        return (atom in self.exclude)

    def CreateCopy(self):
        return ChainAtomOrAlreadyTraversed(self.exclude).__disown__()


def AtomsInSameRing(atomA, atomB):

    if not atomA.IsInRing() or not atomB.IsInRing():
        return False

    if atomA == atomB:
        return True

    firstpath = [a for a in oechem.OEShortestPath(atomA, atomB, oechem.OEAtomIsInChain())]
    firstpathlength = len(firstpath)

    if firstpathlength == 2:
        return True  # neighbors

    if firstpathlength == 0:
        return False  # not is same ring system

    smallestA = oechem.OEAtomGetSmallestRingSize(atomA)
    smallestB = oechem.OEAtomGetSmallestRingSize(atomB)

    if firstpathlength > smallestA and firstpathlength > smallestB:
        return False  # too far away

    # try to find the second shortest different path
    excludepred = ChainAtomOrAlreadyTraversed(firstpath[1:-1])
    secondpath = [a for a in oechem.OEShortestPath(atomA, atomB, excludepred)]
    secondpathlength = len(secondpath)

    if secondpathlength == 0:
        return False  # can not be in the same ring

    if secondpathlength > smallestA and secondpathlength > smallestB:
        return False  # too far away

    sumringsize = len(firstpath) + len(secondpath) - 2
    if sumringsize > smallestA and sumringsize > smallestB:
        return False

    inringA = oechem.OEAtomIsInRingSize(atomA, sumringsize)
    inringB = oechem.OEAtomIsInRingSize(atomB, sumringsize)
    return inringA and inringB


#############################################################################
# INTERFACE
#############################################################################


InterfaceData = '''
!CATEGORY "input/output options"
    !PARAMETER -in
      !ALIAS -i
      !TYPE string
      !REQUIRED true
      !KEYLESS 1
      !VISIBILITY simple
      !BRIEF Input filename
    !END
!END
'''

if __name__ == "__main__":
    sys.exit(main(sys.argv))
