# PyMol script - to be invoked with "run SymGen.py"

# This generates symmetry mates for the object defined by
# <base_object> and <subset> ("protein" and "(all)"by default) 
# within a large radius (<sym_cutoff>) to ensure that the 
# complete local environment of the object is represented.
# Then all symmetry mates which have atoms within <mate_cutoff>
# of the base object are selected and written out to
# files with names of the form "sym###.pdb".
#
# Symmetry objects get the prefix "sym"; existing objects
# with this prefix are deleted.

# parameters
base_object = "protein"
subset="(all)"
mate_cutoff = 8.0
sym_cutoff=32.0

#--------------------------------------------------------------------------

# delete existing symmetry mates
objs = cmd.get_names()   
objs = [ n for n in objs if n[0:3]=="sym" ]
for obj in objs: cmd.delete(obj)

# create symmetry mates
cmd.color("grey",base_object)
cmd.symexp("sym",base_object,subset,sym_cutoff)

objs = cmd.get_names()   # get new symmetry mates
objs = [ n for n in objs if n[0:3]=="sym" ]
labels = [ obj[3:] for obj in objs ]
labels.sort()  # impose arbitrary but consistent ordering

j = 1
for label in labels:
    if cmd.get_type("sym"+label)!="object:molecule":
        print "Something's wrong!"
    else:
        expression =  "("+base_object+" within "+str(mate_cutoff)
	expression += " of sym"+label+")"
	cmd.select("analysis",expression)
	if len(cmd.get_pdbstr("(analysis)"))>4:    # any contacts?
	    cmd.save("sym%03d.pdb" % j,"(sym"+label+")")
	    j += 1

