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