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  # My Stuff 
  7  from Bio.PDB.PDBExceptions import PDBConstructionException 
  8  from Bio.PDB.Entity import Entity, DisorderedEntityWrapper 
  9   
 10   
 11  """Residue class, used by Structure objects.""" 
 12   
 13   
 14  _atom_name_dict={} 
 15  _atom_name_dict["N"]=1 
 16  _atom_name_dict["CA"]=2 
 17  _atom_name_dict["C"]=3 
 18  _atom_name_dict["O"]=4 
 19   
 20   
21 -class Residue(Entity):
22 """ 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 o a1, a2 - Atom objects 50 """ 51 name1=a1.get_name() 52 name2=a2.get_name() 53 if name1==name2: 54 return(cmp(a1.get_altloc(), a2.get_altloc())) 55 if name1 in _atom_name_dict: 56 index1=_atom_name_dict[name1] 57 else: 58 index1=None 59 if name2 in _atom_name_dict: 60 index2=_atom_name_dict[name2] 61 else: 62 index2=None 63 if index1 and index2: 64 return cmp(index1, index2) 65 if index1: 66 return -1 67 if index2: 68 return 1 69 return cmp(name1, name2)
70 71 # Public methods 72
73 - def add(self, atom):
74 """Add an Atom object. 75 76 Checks for adding duplicate atoms, and raises a 77 PDBConstructionException if so. 78 """ 79 atom_id=atom.get_id() 80 if self.has_id(atom_id): 81 raise PDBConstructionException( 82 "Atom %s defined twice in residue %s" % (atom_id, self)) 83 Entity.add(self, atom)
84
85 - def sort(self):
86 self.child_list.sort(self._sort)
87
88 - def flag_disordered(self):
89 "Set the disordered flag." 90 self.disordered=1
91
92 - def is_disordered(self):
93 "Return 1 if the residue contains disordered atoms." 94 return self.disordered
95
96 - def get_resname(self):
97 return self.resname
98
99 - def get_unpacked_list(self):
100 """ 101 Returns the list of all atoms, unpack DisorderedAtoms." 102 """ 103 atom_list=self.get_list() 104 undisordered_atom_list=[] 105 for atom in atom_list: 106 if atom.is_disordered(): 107 undisordered_atom_list=(undisordered_atom_list+ atom.disordered_get_list()) 108 else: 109 undisordered_atom_list.append(atom) 110 return undisordered_atom_list
111
112 - def get_segid(self):
113 return self.segid
114 115
116 -class DisorderedResidue(DisorderedEntityWrapper):
117 """ 118 DisorderedResidue is a wrapper around two or more Residue objects. It is 119 used to represent point mutations (e.g. there is a Ser 60 and a Cys 60 residue, 120 each with 50 % occupancy). 121 """
122 - def __init__(self, id):
124
125 - def __repr__(self):
126 resname=self.get_resname() 127 hetflag, resseq, icode=self.get_id() 128 full_id=(resname, hetflag, resseq, icode) 129 return "<DisorderedResidue %s het=%s resseq=%i icode=%s>" % full_id
130
131 - def add(self, atom):
132 residue=self.disordered_get() 133 if not atom.is_disordered()==2: 134 # Atoms in disordered residues should have non-blank 135 # altlocs, and are thus represented by DisorderedAtom objects. 136 resname=residue.get_resname() 137 het, resseq, icode=residue.get_id() 138 # add atom anyway, if PDBParser ignores exception the atom will be part of the residue 139 residue.add(atom) 140 raise PDBConstructionException( 141 "Blank altlocs in duplicate residue %s (%s, %i, %s)" 142 % (resname, het, resseq, icode)) 143 residue.add(atom)
144
145 - def sort(self):
146 "Sort the atoms in the child Residue objects." 147 for residue in self.disordered_get_list(): 148 residue.sort()
149
150 - def disordered_add(self, residue):
151 """Add a residue object and use its resname as key. 152 153 Arguments: 154 o residue - Residue object 155 """ 156 resname=residue.get_resname() 157 # add chain parent to residue 158 chain=self.get_parent() 159 residue.set_parent(chain) 160 assert(not self.disordered_has_id(resname)) 161 self[resname]=residue 162 self.disordered_select(resname)
163