Molecules may be created using only Python code. The following example shows how to create a single water molecule.
Function
Import the object that tracks open models and several
classes from the
Create an instance of a Molecule
Molecule contains residues. For our example, we will
create a single residue of HOH. The four arguments are:
the residue type, chain identifier, sequence number and
insertion code. Note that a residue is created as part
of a particular molecule.
Now we create the atoms. The newAtom function arguments
are the atom name and its element type, which must be
an instance of Element. You can create an Element
instance from either its name or atomic number.
Set the coordinates for the atoms so that they can be displayed.
Next, we add the atoms into the residue.
Next, we create the bonds between the atoms.
Finally, we add the new molecule into the list of
open models.
Call the function to create a water molecule. If multiple water molecules were needed, they should probably be created as
multiple residues (with different sequence numbers) in the same molecule.
You can execute the example code by downloading the linked Python script and
opening it with the
File→Open
menu item or with the
open
command. Note that the .py extension is required for the open
dialog/command to recognize that the file is a Python script.
createWater
creates a water molecule.
def createWater():
chimera
module.
from chimera import openModels, Molecule, Element, Coord
m = Molecule()
r = m.newResidue("HOH", " ", 1, " ")
atomO = m.newAtom("O", Element("O"))
atomH1 = m.newAtom("H1", Element(1))
atomH2 = m.newAtom("H2", Element("H"))
from math import radians, sin, cos
bondLength = 0.95718
angle = radians(104.474)
atomO.setCoord(Coord(0, 0, 0))
atomH1.setCoord(Coord(bondLength, 0, 0))
atomH2.setCoord(Coord(bondLength * cos(angle), bondLength * sin(angle), 0))
r.addAtom(atomO)
r.addAtom(atomH1)
r.addAtom(atomH2)
m.newBond(atomO, atomH1)
m.newBond(atomO, atomH2)
openModels.add([m])
createWater()
Code Notes
Running the Example