Hi Nick,

I recently was playing with (aka learning) the crystallographic symmetry
information in PDB files and now took your feature request as an
exercise. See attached file, hope it does what you had in mind.

Cheers,
  Thomas

On Mon, 2010-04-12 at 14:12 -0600, Nicolas Bock wrote:
> It would be great if pymol had support for constructing and displaying
> a supercell:
> 
> For data files that support crystallographic information, e.g. pdb,
> pymol can already display the unit cell (show cell). It would be great
> if one could easily display multiple copies of the unit cell, along
> the lines of
> 
> supercell 1,1,2
> 
> where pymol would then copy the unit cell along the c-axis and display
> 2 unit cells.
> 
> supercell 2,2,2
> 
> would display 2 copies along the a, b, and c axes, and display 8 unit
> cells in total.
> 
> As a related request it would be great if pymol could construct a unit
> cell in case the crystallographic information in the input file does
> not exist. This could come in handy for instance when reading from
> xyz. Maybe a command such as
> 
> construct_supercell a=10.2, b=10.2, c=15, alpha=90.0, beta=90,
> gamma=90
> 
> would construct a cubic cell with dimensions axbxc.
> 
> Thanks,
> 
> nick

'''
(c) 2010 Thomas Holder

PyMOL python script (load with `run supercell.py`)
Usage: See "help supercell"
'''

from pymol import cmd, cgo
from math import cos, sin, radians, sqrt
import numpy

def cellbasis(angles, edges):
	'''
	For the unit cell with given angles and edge lengths calculate the basis
	transformation (vectors) as a 4x4 numpy.array
	'''
	rad = [radians(i) for i in angles]
	basis = numpy.identity(4)
	basis[0][1] = cos(rad[2])
	basis[1][1] = sin(rad[2])
	basis[0][2] = cos(rad[1])
	basis[1][2] = (cos(rad[0]) - basis[0][1]*basis[0][2])/basis[1][1]
	basis[2][2] = sqrt(1 - basis[0][2]**2 - basis[1][2]**2)
	edges.append(1.0)
	return basis * edges # numpy.array multiplication!

def supercell(a=1, b=1, c=1, object=None, color='blue'):
	'''
DESCRIPTION

    Draw a supercell, as requested by Nicolas Bock on the pymol-users
    mailing list (Subject: [PyMOL] feature request: supercell construction
    Date: 04/12/2010 10:12:17 PM (Mon, 12 Apr 2010 14:12:17 -0600))

USAGE

    supercell a, b, c [, object [, color]]

ARGUMENTS

    a, b, c = integer: repeat cell in x,y,z direction a,b,c times
    {default: 1,1,1}

    object = string: name of object to take cell definition from

    color = string: color of cell {default: blue}

SEE ALSO

    show cell

	'''
	if object is None:
		object = cmd.get_object_list()[0]

	sym = cmd.get_symmetry(object)
	cell_edges = sym[0:3]
	cell_angles = sym[3:6]

	basis = cellbasis(cell_angles, cell_edges)
	assert isinstance(basis, numpy.ndarray)

	ts = list()
	for i in range(int(a)):
		for j in range(int(b)):
			for k in range(int(c)):
				ts.append([i,j,k])

	obj = [
		cgo.BEGIN,
		cgo.LINES,
		cgo.COLOR,
	]
	obj.extend(cmd.get_color_tuple(color))
	
	for t in ts:
		shift = basis[0:3,0:3] * t
		shift = shift[:,0] + shift[:,1] + shift[:,2]
	
		for i in range(3):
			vi = basis[0:3,i]
			vj = [
				numpy.array([0.,0.,0.]),
				basis[0:3,(i+1)%3],
				basis[0:3,(i+2)%3],
				basis[0:3,(i+1)%3] + basis[0:3,(i+2)%3]
			]
			for j in range(4):
				obj.append(cgo.VERTEX)
				obj.extend((shift + vj[j]).tolist())
				obj.append(cgo.VERTEX)
				obj.extend((shift + vj[j] + vi).tolist())

	cmd.load_cgo(obj, 'supercell')

cmd.extend('supercell', supercell)
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
PyMOL-users mailing list (PyMOL-users@lists.sourceforge.net)
Info Page: https://lists.sourceforge.net/lists/listinfo/pymol-users
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net

Reply via email to