next up previous contents index
Next: Changing the selection and Up: Atom selections in Python Previous: The AtomSel class (DEPRECATED)   Contents   Index

An atom selection example

In the first example, we load the molecule alanin.pdb, and create an atom selection consisting of the alpha carbons. Note that AtomSel is the name of the class which generates atom selection instances. We show the string representation of the object by entering its name and pressing return; this shows the text used in the selection.

Next we demenstrate how atom selections act like tuples: we can get their length using the built-in len() command,and return a copy of the selected atoms in a tuple by using the slice operator [:].

Finally, we demonstrate the get and set operations. The get() operation takes any number of string arguments; for each argument, it returns a Python list of values corresponding to that string. The set() operation allows only one property to be changed at a time. However, you can pass in either a single value, which will be applied to all atoms in the selection, or a tuple or list of the same size as the atom selection, in which case values in the list will be assigned to the corresponding atom in the selection. We take advantage of this behavior in the following example by first saving the current value of beta for the selection, then setting the value of beta to 5 for all selected atoms, and finally resetting the original values using the results of the get().

>>> from molecule import *
>>> from AtomSel import AtomSel
>>> load('alanin.pdb')

>>> CA = AtomSel('name CA')
>>> CA                  
name CA
>>> len(CA)
12
>>> CA[:]
(0, 5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65)
>>> resname, resid = CA.get('resname', 'resid')
>>> resname
['ACE', 'ALA', 'ALA', 'ALA', 'ALA', 'ALA', 'ALA', 'ALA', 'ALA', 'ALA', 'ALA', 'C
BX']
>>> resid
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> x,y,z=CA.get('x','y','z')
>>> x
[-2.1840000152587891, 1.4500000476837158, 1.9809999465942383, 0.54100000858306885, 
2.8090000152587891, 5.9079999923706055, 5.0440001487731934, 4.5659999847412109,
7.9340000152587891, 9.7329998016357422, 8.1689996719360352, 9.2229995727539062]
>>> y
[0.5910000205039978, 0.0, 3.6429998874664307, 4.8410000801086426, 2.5559999942779541, 
3.7860000133514404, 7.4190001487731934, 6.7989997863769531, 5.0819997787475586, 
7.9559998512268066, 10.515999794006348, 8.5710000991821289]
>>> z
[0.9100000262260437, 0.0, -0.9089999794960022, 2.3880000114440918, 4.3920001983642578, 
2.5859999656677246, 3.244999885559082, 6.9559998512268066, 7.2639999389648438, 
5.5669999122619629, 7.8870000839233398, 11.013999938964844]

>>> beta = CA.get('beta')
>>> CA.set('beta',5)
>>> CA.set('beta',beta)
>>>


next up previous contents index
Next: Changing the selection and Up: Atom selections in Python Previous: The AtomSel class (DEPRECATED)   Contents   Index
vmd@ks.uiuc.edu