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