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

Source Code for Module Bio.PDB.Residue

  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  """Residue class, used by Structure objects.""" 
  7   
  8  # My Stuff 
  9  import warnings 
 10  from Bio import BiopythonDeprecationWarning 
 11  from Bio.PDB.PDBExceptions import PDBConstructionException 
 12  from Bio.PDB.Entity import Entity, DisorderedEntityWrapper 
 13   
 14   
 15  _atom_name_dict = {} 
 16  _atom_name_dict["N"] = 1 
 17  _atom_name_dict["CA"] = 2 
 18  _atom_name_dict["C"] = 3 
 19  _atom_name_dict["O"] = 4 
 20   
 21   
22 -class Residue(Entity):
23 """Represents a residue. A Residue object stores atoms.""" 24
25 - def __init__(self, id, resname, segid):
26 self.level = "R" 27 self.disordered = 0 28 self.resname = resname 29 self.segid = segid 30 Entity.__init__(self, id)
31 32 # Special methods 33
34 - def __repr__(self):
35 resname = self.get_resname() 36 hetflag, resseq, icode = self.get_id() 37 full_id = (resname, hetflag, resseq, icode) 38 return "<Residue %s het=%s resseq=%s icode=%s>" % full_id
39 40 # Private methods 41
42 - def _sort(self, a1, a2):
43 """Sort the Atom objects. 44 45 Atoms are sorted alphabetically according to their name, 46 but N, CA, C, O always come first. 47 48 Arguments: 49 - a1, a2 - Atom objects 50 51 """ 52 name1 = a1.get_name() 53 name2 = a2.get_name() 54 if name1 == name2: 55 return(cmp(a1.get_altloc(), a2.get_altloc())) 56 if name1 in _atom_name_dict: 57 index1 = _atom_name_dict[name1] 58 else: 59 index1 = None 60 if name2 in _atom_name_dict: 61 index2 = _atom_name_dict[name2] 62 else: 63 index2 = None 64 if index1 and index2: 65 return cmp(index1, index2) 66 if index1: 67 return -1 68 if index2: 69 return 1 70 return cmp(name1, name2)
71 72 # Public methods 73
74 - def add(self, atom):
75 """Add an Atom object. 76 77 Checks for adding duplicate atoms, and raises a 78 PDBConstructionException if so. 79 """ 80 atom_id = atom.get_id() 81 if self.has_id(atom_id): 82 raise PDBConstructionException( 83 "Atom %s defined twice in residue %s" % (atom_id, self)) 84 Entity.add(self, atom)
85
86 - def sort(self):
87 self.child_list.sort(self._sort)
88
89 - def flag_disordered(self):
90 """Set the disordered flag.""" 91 self.disordered = 1
92
93 - def is_disordered(self):
94 """Return 1 if the residue contains disordered atoms.""" 95 return self.disordered
96
97 - def get_resname(self):
98 return self.resname
99
100 - def get_unpacked_list(self):
101 """Returns the list of all atoms, unpack DisorderedAtoms.""" 102 atom_list = self.get_list() 103 undisordered_atom_list = [] 104 for atom in atom_list: 105 if atom.is_disordered(): 106 undisordered_atom_list = (undisordered_atom_list + atom.disordered_get_list()) 107 else: 108 undisordered_atom_list.append(atom) 109 return undisordered_atom_list
110
111 - def get_segid(self):
112 return self.segid
113
114 - def get_atoms(self):
115 for a in self: 116 yield a
117
118 - def get_atom(self):
119 warnings.warn("`get_atom` has been deprecated and we intend to remove it" 120 " in a future release of Biopython. Please use `get_atoms` instead.", 121 BiopythonDeprecationWarning) 122 for a in self: 123 yield a
124 125
126 -class DisorderedResidue(DisorderedEntityWrapper):
127 """DisorderedResidue is a wrapper around two or more Residue objects. 128 129 It is used to represent point mutations (e.g. there is a Ser 60 and a Cys 60 130 residue, each with 50 % occupancy). 131 """ 132
133 - def __init__(self, id):
135
136 - def __repr__(self):
137 resname = self.get_resname() 138 hetflag, resseq, icode = self.get_id() 139 full_id = (resname, hetflag, resseq, icode) 140 return "<DisorderedResidue %s het=%s resseq=%i icode=%s>" % full_id
141
142 - def add(self, atom):
143 residue = self.disordered_get() 144 if not atom.is_disordered() == 2: 145 # Atoms in disordered residues should have non-blank 146 # altlocs, and are thus represented by DisorderedAtom objects. 147 resname = residue.get_resname() 148 het, resseq, icode = residue.get_id() 149 # add atom anyway, if PDBParser ignores exception the atom will be part of the residue 150 residue.add(atom) 151 raise PDBConstructionException( 152 "Blank altlocs in duplicate residue %s (%s, %i, %s)" 153 % (resname, het, resseq, icode)) 154 residue.add(atom)
155
156 - def sort(self):
157 """Sort the atoms in the child Residue objects.""" 158 for residue in self.disordered_get_list(): 159 residue.sort()
160
161 - def disordered_add(self, residue):
162 """Add a residue object and use its resname as key. 163 164 Arguments: 165 - residue - Residue object 166 167 """ 168 resname = residue.get_resname() 169 # add chain parent to residue 170 chain = self.get_parent() 171 residue.set_parent(chain) 172 assert(not self.disordered_has_id(resname)) 173 self[resname] = residue 174 self.disordered_select(resname)
175