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 PDBExceptions import PDBConstructionException 
  8  from Entity import Entity, DisorderedEntityWrapper 
  9   
 10   
 11  __doc__="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 _atom_name_dict.has_key(name1): 56 index1=_atom_name_dict[name1] 57 else: 58 index1=None 59 if _atom_name_dict.has_key(name2): 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, "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
115 -class DisorderedResidue(DisorderedEntityWrapper):
116 """ 117 DisorderedResidue is a wrapper around two or more Residue objects. It is 118 used to represent point mutations (e.g. there is a Ser 60 and a Cys 60 residue, 119 each with 50 % occupancy). 120 """
121 - def __init__(self, id):
123
124 - def __repr__(self):
125 resname=self.get_resname() 126 hetflag, resseq, icode=self.get_id() 127 full_id=(resname, hetflag, resseq, icode) 128 return "<DisorderedResidue %s het=%s resseq=%i icode=%s>" % full_id
129
130 - def add(self, atom):
131 residue=self.disordered_get() 132 if not atom.is_disordered()==2: 133 # Atoms in disordered residues should have non-blanc 134 # altlocs, and are thus represented by DisorderedAtom objects. 135 resname=residue.get_resname() 136 het, resseq, icode=residue.get_id() 137 # add atom anyway, if PDBParser ignores exception the atom will be part of the residue 138 residue.add(atom) 139 raise PDBConstructionException, "Blank altlocs in duplicate residue %s (%s, %i, %s)" % (resname, het, resseq, icode) 140 residue.add(atom)
141
142 - def sort(self):
143 "Sort the atoms in the child Residue objects." 144 for residue in self.disordered_get_list(): 145 residue.sort()
146
147 - def disordered_add(self, residue):
148 """Add a residue object and use its resname as key. 149 150 Arguments: 151 o residue - Residue object 152 """ 153 resname=residue.get_resname() 154 # add chain parent to residue 155 chain=self.get_parent() 156 residue.set_parent(chain) 157 assert(not self.disordered_has_id(resname)) 158 self[resname]=residue 159 self.disordered_select(resname)
160