def findLigandRings(atoms):
    matrix = hf.getDistMatrix(atoms)
    bdist = 2.0 # angstrom of bond dinstance
    graph = hf.makeGraph(nodes_list = range(len(matrix)), distance_matrix = matrix, cutoff = bdist**2)
    rings5,rings6 = findRings(graph)
    RING = rings5+rings6
    ligand_rings = {}
    for r in range(len(RING)):
        # res name first...
        
        # get the first atom PDB string in the ring
        atstring = atoms[RING[r][0]][2]
        ringname = hf.pmvAtomStrip(atstring) 
        ringname_short = ringname.rsplit(":",1)[0].strip()
        if ringname_short:
            c = 0
            while ringname_short+":"+str(c) in ligand_rings.keys():
                c+=1
                if c > 20:
                    ringname_short = None
                    break
        if ringname_short:
            lnam = ringname_short+":"+str(c) 
        else:
            lnam = "rng:"+str(r)

        ligand_rings[lnam] = []
        for a in RING[r]:
            ligand_rings[lnam].append(atoms[a][2])
    return ligand_rings
def getAromaticRes(receptor):
    # TODO add acids and amides?
    aromatic_res = { "PHE": ["A"],
                 "TYR": ["A"],
                 "HIS": ["A", "N", "NA"],
                 "TRP": ["A", "N", "NA"],
                 "ARG": ["N", "C" , "NA"],
                 "DA" : ["A", "NA", "N"],  # DNA
                 "DC" : ["A", "N", "NA"],
                 "DT" : ["A", "N", "NA"],
                 "DG" : ["A", "N", "NA"],                 
                 "A" : ["A", "NA", "N"],   # DNA/RNA
                 "C" : ["A", "N", "NA"],
                 "G" : ["A", "N", "NA"],                 
                 "T" : ["A", "N", "NA"],
                 "U" : ["A", "N", "NA"],
                 }
    residues = {}
    special = [] # must be treated as a ligand part
    for a in receptor:
        a = a.strip()
        rtype = a[16:21].strip().upper() # NOTE: we can recognize only all caps!
        if a.startswith("HETATM"):
            special.append(a)
        elif a.startswith('ATOM') and rtype in aromatic_res.keys():
            if hf.getAtype(a) in aromatic_res[rtype]:
                res_id = hf.pmvAtomStrip(a).rsplit(":",1)[0] # A:GLY48:N -> A:GLY48
                if not res_id in residues.keys():
                    residues[res_id] = []
                residues[res_id].append(a)
    if special and DEBUG:
        print "found potential co-factor: %d"  % len(special)
        for a in special: print a
    return residues, special # residues = { "A:GLY48" : [ atom, atom, atom..]}