Package Bio :: Package PDB :: Module Atom
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.Atom

  1  # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk) 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package.            
  5   
  6  """Atom class, used in Structure objects.""" 
  7   
  8  import numpy 
  9  import warnings 
 10   
 11  from Bio.PDB.Entity import DisorderedEntityWrapper 
 12  from Bio.PDB.PDBExceptions import PDBConstructionWarning 
 13  from Bio.PDB.Vector import Vector 
 14  from Bio.Data import IUPACData 
 15   
16 -class Atom(object):
17 - def __init__(self, name, coord, bfactor, occupancy, altloc, fullname, serial_number, 18 element=None):
19 """ 20 Atom object. 21 22 The Atom object stores atom name (both with and without spaces), 23 coordinates, B factor, occupancy, alternative location specifier 24 and (optionally) anisotropic B factor and standard deviations of 25 B factor and positions. 26 27 @param name: atom name (eg. "CA"). Note that spaces are normally stripped. 28 @type name: string 29 30 @param coord: atomic coordinates (x,y,z) 31 @type coord: Numeric array (Float0, size 3) 32 33 @param bfactor: isotropic B factor 34 @type bfactor: number 35 36 @param occupancy: occupancy (0.0-1.0) 37 @type occupancy: number 38 39 @param altloc: alternative location specifier for disordered atoms 40 @type altloc: string 41 42 @param fullname: full atom name, including spaces, e.g. " CA ". Normally 43 these spaces are stripped from the atom name. 44 @type fullname: string 45 46 @param element: atom element, e.g. "C" for Carbon, "HG" for mercury, 47 @type fullname: uppercase string (or None if unknown) 48 49 """ 50 self.level="A" 51 # Reference to the residue 52 self.parent=None 53 # the atomic data 54 self.name=name # eg. CA, spaces are removed from atom name 55 self.fullname=fullname # e.g. " CA ", spaces included 56 self.coord=coord 57 self.bfactor=bfactor 58 self.occupancy=occupancy 59 self.altloc=altloc 60 self.full_id=None # (structure id, model id, chain id, residue id, atom id) 61 self.id=name # id of atom is the atom name (e.g. "CA") 62 self.disordered_flag=0 63 self.anisou_array=None 64 self.siguij_array=None 65 self.sigatm_array=None 66 self.serial_number=serial_number 67 # Dictionary that keeps addictional properties 68 self.xtra={} 69 assert not element or element == element.upper(), element 70 self.element = self._assign_element(element) 71 self.mass = self._assign_atom_mass()
72
73 - def _assign_element(self, element):
74 """Tries to guess element from atom name if not recognised.""" 75 if not element or element.capitalize() not in IUPACData.atom_weights: 76 # Inorganic elements have their name shifted left by one position 77 # (is a convention in PDB, but not part of the standard). 78 # isdigit() check on last two characters to avoid mis-assignment of 79 # hydrogens atoms (GLN HE21 for example) 80 81 if self.fullname[0] != " " and not self.fullname[2:].isdigit(): 82 putative_element = self.name.strip() 83 else: 84 # Hs may have digit in [0] 85 if self.name[0].isdigit(): 86 putative_element = self.name[1] 87 else: 88 putative_element = self.name[0] 89 90 if putative_element.capitalize() in IUPACData.atom_weights: 91 msg = "Used element %r for Atom (name=%s) with given element %r" \ 92 % (putative_element, self.name, element) 93 element = putative_element 94 else: 95 msg = "Could not assign element %r for Atom (name=%s) with given element %r" \ 96 % (putative_element, self.name, element) 97 element = "" 98 warnings.warn(msg, PDBConstructionWarning) 99 100 return element
101
102 - def _assign_atom_mass(self):
103 # Needed for Bio/Struct/Geometry.py C.O.M. function 104 if self.element: 105 return IUPACData.atom_weights[self.element.capitalize()] 106 else: 107 return float('NaN')
108 109 110 # Special methods 111
112 - def __repr__(self):
113 "Print Atom object as <Atom atom_name>." 114 return "<Atom %s>" % self.get_id()
115
116 - def __sub__(self, other):
117 """ 118 Calculate distance between two atoms. 119 120 Example: 121 >>> distance=atom1-atom2 122 123 @param other: the other atom 124 @type other: L{Atom} 125 """ 126 diff=self.coord-other.coord 127 return numpy.sqrt(numpy.dot(diff,diff))
128 129 # set methods 130
131 - def set_serial_number(self, n):
132 self.serial_number=n
133
134 - def set_bfactor(self, bfactor):
135 self.bfactor=bfactor
136
137 - def set_coord(self, coord):
138 self.coord=coord
139
140 - def set_altloc(self, altloc):
141 self.altloc=altloc
142
143 - def set_occupancy(self, occupancy):
144 self.occupancy=occupancy
145
146 - def set_sigatm(self, sigatm_array):
147 """ 148 Set standard deviation of atomic parameters. 149 150 The standard deviation of atomic parameters consists 151 of 3 positional, 1 B factor and 1 occupancy standard 152 deviation. 153 154 @param sigatm_array: standard deviations of atomic parameters. 155 @type sigatm_array: Numeric array (length 5) 156 """ 157 self.sigatm_array=sigatm_array
158
159 - def set_siguij(self, siguij_array):
160 """ 161 Set standard deviations of anisotropic temperature factors. 162 163 @param siguij_array: standard deviations of anisotropic temperature factors. 164 @type siguij_array: Numeric array (length 6) 165 """ 166 self.siguij_array=siguij_array
167
168 - def set_anisou(self, anisou_array):
169 """ 170 Set anisotropic B factor. 171 172 @param anisou_array: anisotropic B factor. 173 @type anisou_array: Numeric array (length 6) 174 """ 175 self.anisou_array=anisou_array
176 177 178 # Public methods 179
180 - def flag_disorder(self):
181 """Set the disordered flag to 1. 182 183 The disordered flag indicates whether the atom is disordered or not. 184 """ 185 self.disordered_flag=1
186
187 - def is_disordered(self):
188 "Return the disordered flag (1 if disordered, 0 otherwise)." 189 return self.disordered_flag
190
191 - def set_parent(self, parent):
192 """Set the parent residue. 193 194 Arguments: 195 o parent - Residue object 196 """ 197 self.parent=parent
198
199 - def detach_parent(self):
200 "Remove reference to parent." 201 self.parent=None
202
203 - def get_sigatm(self):
204 "Return standard deviation of atomic parameters." 205 return self.sigatm_array
206
207 - def get_siguij(self):
208 "Return standard deviations of anisotropic temperature factors." 209 return self.siguij_array
210
211 - def get_anisou(self):
212 "Return anisotropic B factor." 213 return self.anisou_array
214
215 - def get_parent(self):
216 "Return parent residue." 217 return self.parent
218
219 - def get_serial_number(self):
220 return self.serial_number
221
222 - def get_name(self):
223 "Return atom name." 224 return self.name
225
226 - def get_id(self):
227 "Return the id of the atom (which is its atom name)." 228 return self.id
229
230 - def get_full_id(self):
231 """Return the full id of the atom. 232 233 The full id of an atom is the tuple 234 (structure id, model id, chain id, residue id, atom name, altloc). 235 """ 236 return self.parent.get_full_id()+((self.name, self.altloc),)
237
238 - def get_coord(self):
239 "Return atomic coordinates." 240 return self.coord
241
242 - def get_bfactor(self):
243 "Return B factor." 244 return self.bfactor
245
246 - def get_occupancy(self):
247 "Return occupancy." 248 return self.occupancy
249
250 - def get_fullname(self):
251 "Return the atom name, including leading and trailing spaces." 252 return self.fullname
253
254 - def get_altloc(self):
255 "Return alternative location specifier." 256 return self.altloc
257
258 - def get_level(self):
259 return self.level
260
261 - def transform(self, rot, tran):
262 """ 263 Apply rotation and translation to the atomic coordinates. 264 265 Example: 266 >>> rotation=rotmat(pi, Vector(1,0,0)) 267 >>> translation=array((0,0,1), 'f') 268 >>> atom.transform(rotation, translation) 269 270 @param rot: A right multiplying rotation matrix 271 @type rot: 3x3 Numeric array 272 273 @param tran: the translation vector 274 @type tran: size 3 Numeric array 275 """ 276 self.coord=numpy.dot(self.coord, rot)+tran
277
278 - def get_vector(self):
279 """ 280 Return coordinates as Vector. 281 282 @return: coordinates as 3D vector 283 @rtype: Vector 284 """ 285 x,y,z=self.coord 286 return Vector(x,y,z)
287 288
289 -class DisorderedAtom(DisorderedEntityWrapper):
290 """ 291 This class contains all Atom objects that represent the same disordered 292 atom. One of these atoms is "selected" and all method calls not caught 293 by DisorderedAtom are forwarded to the selected Atom object. In that way, a 294 DisorderedAtom behaves exactly like a normal Atom. By default, the selected 295 Atom object represents the Atom object with the highest occupancy, but a 296 different Atom object can be selected by using the disordered_select(altloc) 297 method. 298 """
299 - def __init__(self, id):
300 """ 301 Arguments: 302 o id - string, atom name 303 """ 304 self.last_occupancy=-1 305 DisorderedEntityWrapper.__init__(self, id)
306 307 # Special methods 308
309 - def __repr__(self):
310 return "<Disordered Atom %s>" % self.get_id()
311
312 - def disordered_add(self, atom):
313 "Add a disordered atom." 314 # Add atom to dict, use altloc as key 315 atom.flag_disorder() 316 # set the residue parent of the added atom 317 residue=self.get_parent() 318 atom.set_parent(residue) 319 altloc=atom.get_altloc() 320 occupancy=atom.get_occupancy() 321 self[altloc]=atom 322 if occupancy>self.last_occupancy: 323 self.last_occupancy=occupancy 324 self.disordered_select(altloc)
325