Scripts: dipole.py

File dipole.py, 1.1 KB (added by pett, 10 years ago)

calculate dipole moment and show dipole vector

Line 
1from chimera import Point, openModels, Molecule, Vector, UserError
2
3showVector = True # change to "False" to hide dipole vector
4vectorScale = 1 # may want to increase/decrease for better presentation
5
6def centerOfMass(model):
7 return Point([a.coord() for a in model.atoms],
8 [a.element.mass for a in model.atoms])
9
10# calculation method conscripted from:
11# A server and database for dipole moments of proteins
12# Nucleic Acids Res. 2007 Jul;35(Web Server issue):W512-21
13# doi:10.1093/nar/gkm307
14for m in openModels.list(modelTypes=[Molecule]):
15 com = centerOfMass(m)
16 dipole = Vector()
17 for a in m.atoms:
18 try:
19 dipole += a.charge * (a.coord() - com)
20 except AttributeError:
21 raise UserError("No charge assigned to %s" % a)
22 # 4.803 is conversion factor to Debyes for angstrom measurements
23 print "Dipole moment for %s %s: %.3f" % (m, m.name, 4.803 * dipole.length)
24 if showVector:
25 v = com + vectorScale * dipole
26 bildString = ".arrow %g %g %g %g %g %g .1 .2 .9" % (
27 com[0], com[1], com[2], v[0], v[1], v[2])
28 from StringIO import StringIO
29 bild = StringIO(bildString)
30 openModels.open(bild, type="Bild", identifyAs="%s dipole" % m.name, sameAs=m)