Scripts: placeatom.py

File placeatom.py, 1.6 KB (added by goddard, 16 years ago)
Line 
1# -----------------------------------------------------------------------------
2# Place a new atom some distance past a specified atom along a ray from the
3# center of mass of the molecule.
4#
5def place_atom(near_atom, distance, element,
6 atom_name, residue_name, residue_number):
7
8 m = near_atom.molecule
9
10 d = near_atom.coord() - center_of_mass(m) # Vector from center to atom
11 d.normalize()
12
13 xyz = near_atom.coord() + distance*d
14
15 from chimera import MolResId
16 a = m.newAtom(atom_name, element)
17 r = m.newResidue(residue_name, MolResId(residue_number))
18 r.addAtom(a)
19
20 a.setCoord(xyz) # Set atom coordinates
21 a.drawMode = a.Sphere # Set display style to sphere
22
23# -----------------------------------------------------------------------------
24#
25def center_of_mass(molecule):
26
27 from Measure.inertia import atoms_inertia
28 c = atoms_inertia(molecule.atoms)[2] # Center of mass coordinates
29 from chimera import Point
30 p = Point(*c)
31 # Transform from global coordinates to molecule coordinate system.
32 pm = molecule.openState.xform.inverse().apply(p)
33
34 return pm
35
36# -----------------------------------------------------------------------------
37# Place a helium atom 2A past a selected atom along a ray from the center of
38# mass of the molecule.
39#
40from chimera import selection
41atoms = selection.currentAtoms()
42if len(atoms) != 1:
43 from chimera.replyobj import error
44 error("Need to select exactly one atom")
45else:
46 from chimera import elements
47 place_atom(atoms[0], 2, elements.He, 'X', 'R1', 1000)