File: Atom.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (279 lines) | stat: -rw-r--r-- 8,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright (C) 2002, Thomas Hamelryck (thamelry@vub.ac.be)
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.           

# Python stuff
from Numeric import array, sum, sqrt, matrixmultiply

# My stuff
from Entity import DisorderedEntityWrapper
from Vector import Vector

__doc__="Atom class, used in Structure objects."


class Atom:
    def __init__(self, name, coord, bfactor, occupancy, altloc, fullname, serial_number):
        """
        Atom object.

        The Atom object stores atom name (both with and without spaces), 
        coordinates, B factor, occupancy, alternative location specifier
        and (optionally) anisotropic B factor and standard deviations of 
        B factor and positions.
  
        @param name: atom name (eg. "CA"). Note that spaces are normally stripped.
        @type name: string

        @param coord: atomic coordinates (x,y,z)
        @type coord: Numpy array (Float0, size 3)

        @param bfactor: isotropic B factor
        @type bfactor: number 

        @param occupancy: occupancy (0.0-1.0)
        @type occupancy: number

        @param altloc: alternative location specifier for disordered atoms
        @type altloc: string

        @param fullname: full atom name, including spaces, e.g. " CA ". Normally
        these spaces are stripped from the atom name. 
        @type fullname: string
        """
        self.level="A"
        # Reference to the residue 
        self.parent=None
        # the atomic data
        self.name=name      # eg. CA, spaces are removed from atom name
        self.fullname=fullname  # e.g. " CA ", spaces included
        self.coord=coord
        self.bfactor=bfactor
        self.occupancy=occupancy
        self.altloc=altloc
        self.full_id=None   # (structure id, model id, chain id, residue id, atom id)
        self.id=name        # id of atom is the atom name (e.g. "CA")
        self.disordered_flag=0
        self.anisou_array=None
        self.siguij_array=None
        self.sigatm_array=None
        self.serial_number=serial_number
        # Dictionary that keeps addictional properties
        self.xtra={}

    # Special methods   

    def __repr__(self):
        "Print Atom object as <Atom atom_name>."
        return "<Atom %s>" % self.get_id()

    def __sub__(self, other):
        """
        Calculate distance between two atoms.
        
        Example:
            >>> distance=atom1-atom2

        @param other: the other atom
        @type other: L{Atom}
        """
        diff=self.coord-other.coord
        return sqrt(sum(diff*diff))

    # set methods

    def set_serial_number(self, n):
        self.serial_number=n

    def set_bfactor(self, bfactor):
        self.bfactor=bfactor

    def set_coord(self, coord):
        self.coord=coord

    def set_altloc(self, altloc):
        self.altloc=altloc

    def set_occupancy(self, occupancy):
        self.occupancy=occupancy

    def set_sigatm(self, sigatm_array):
        """
        Set standard deviation of atomic parameters.

        The standard deviation of atomic parameters consists
        of 3 positional, 1 B factor and 1 occupancy standard 
        deviation.

        @param sigatm_array: standard deviations of atomic parameters.
        @type sigatm_array: Numpy array (length 5)
        """
        self.sigatm_array=sigatm_array

    def set_siguij(self, siguij_array):
        """
        Set standard deviations of anisotropic temperature factors.

        @param siguij_array: standard deviations of anisotropic temperature factors.
        @type siguij_array: Numpy array (length 6)
        """
        self.siguij_array=siguij_array

    def set_anisou(self, anisou_array):
        """
        Set anisotropic B factor.

        @param anisou_array: anisotropic B factor.
        @type anisou_array: Numpy array (length 6)
        """
        self.anisou_array=anisou_array


    # Public methods    

    def flag_disorder(self):
        """Set the disordered flag to 1.

        The disordered flag indicates whether the atom is disordered or not.
        """
        self.disordered_flag=1

    def is_disordered(self):
        "Return the disordered flag (1 if disordered, 0 otherwise)."
        return self.disordered_flag 

    def set_parent(self, parent):
        """Set the parent residue.

        Arguments:
        o parent - Residue object
        """
        self.parent=parent
    
    def detach_parent(self):
        "Remove reference to parent."
        self.parent=None

    def get_sigatm(self):
        "Return standard deviation of atomic parameters."
        return self.sigatm_array

    def get_siguij(self):
        "Return standard deviations of anisotropic temperature factors."
        return self.siguij_array

    def get_anisou(self):
        "Return anisotropic B factor."
        return self.anisou_array

    def get_parent(self):
        "Return parent residue."
        return self.parent

    def get_serial_number(self):
        return self.serial_number

    def get_name(self):
        "Return atom name."
        return self.name

    def get_id(self):
        "Return the id of the atom (which is its atom name)."
        return self.id

    def get_full_id(self):
        """Return the full id of the atom.

        The full id of an atom is the tuple 
        (structure id, model id, chain id, residue id, atom name, altloc).
        """
        return self.parent.get_full_id()+((self.name, self.altloc),)
    
    def get_coord(self):
        "Return atomic coordinates."
        return self.coord

    def get_bfactor(self):
        "Return B factor."
        return self.bfactor

    def get_occupancy(self):
        "Return occupancy."
        return self.occupancy

    def get_fullname(self):
        "Return the atom name, including leading and trailing spaces."
        return self.fullname

    def get_altloc(self):
        "Return alternative location specifier."
        return self.altloc

    def get_level(self):
        return self.level

    def transform(self, rot, tran):
        """
        Apply rotation and translation to the atomic coordinates.

        Example:
                >>> rotation=rotmat(pi, Vector(1,0,0))
                >>> translation=array((0,0,1), 'f')
                >>> atom.transform(rotation, translation)

        @param rot: A right multiplying rotation matrix
        @type rot: 3x3 Numpy array

        @param tran: the translation vector
        @type tran: size 3 Numpy array
        """
        self.coord=matrixmultiply(self.coord, rot)+tran
        
    def get_vector(self):
        """
        Return coordinates as Vector.

        @return: coordinates as 3D vector
        @rtype: Vector
        """
        x,y,z=self.coord
        return Vector(x,y,z)


class DisorderedAtom(DisorderedEntityWrapper):
    """
    This class contains all Atom objects that represent the same disordered
    atom. One of these atoms is "selected" and all method calls not caught
    by DisorderedAtom are forwarded to the selected Atom object. In that way, a
    DisorderedAtom behaves exactly like a normal Atom. By default, the selected 
    Atom object represents the Atom object with the highest occupancy, but a 
    different Atom object can be selected by using the disordered_select(altloc) 
    method. 
    """
    def __init__(self, id):
        """
        Arguments:
        o id - string, atom name
        """
        self.last_occupancy=-1
        DisorderedEntityWrapper.__init__(self, id)

    # Special methods

    def __repr__(self):
        return "<Disordered Atom %s>" % self.get_id() 

    def disordered_add(self, atom):
        "Add a disordered atom."
        # Add atom to dict, use altloc as key   
        atom.flag_disorder()
        # set the residue parent of the added atom
        residue=self.get_parent()
        atom.set_parent(residue)
        altloc=atom.get_altloc()
        occupancy=atom.get_occupancy()
        self[altloc]=atom
        if occupancy>self.last_occupancy:
            self.last_occupancy=occupancy
            self.disordered_select(altloc)