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 """Initialize the class.""" 27 self.level = "R" 28 self.disordered = 0 29 self.resname = resname 30 self.segid = segid 31 Entity.__init__(self, id)
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 - def add(self, atom):
40 """Add an Atom object. 41 42 Checks for adding duplicate atoms, and raises a 43 PDBConstructionException if so. 44 """ 45 atom_id = atom.get_id() 46 if self.has_id(atom_id): 47 raise PDBConstructionException( 48 "Atom %s defined twice in residue %s" % (atom_id, self)) 49 Entity.add(self, atom)
50
51 - def sort(self):
52 """Sort child atoms. 53 54 Atoms N, CA, C, O always come first, thereafter alphabetically 55 by name, with any alternative location specifier for disordered 56 atoms (altloc) as a tie-breaker. 57 """ 58 warnings.warn("The custom sort() method will be removed in the " 59 "future in favour of rich comparison methods. Use the " 60 "built-in sorted() function instead.", 61 BiopythonDeprecationWarning) 62 self.child_list.sort()
63
64 - def flag_disordered(self):
65 """Set the disordered flag.""" 66 self.disordered = 1
67
68 - def is_disordered(self):
69 """Return 1 if the residue contains disordered atoms.""" 70 return self.disordered
71
72 - def get_resname(self):
73 return self.resname
74
75 - def get_unpacked_list(self):
76 """Return the list of all atoms, unpack DisorderedAtoms.""" 77 atom_list = self.get_list() 78 undisordered_atom_list = [] 79 for atom in atom_list: 80 if atom.is_disordered(): 81 undisordered_atom_list = (undisordered_atom_list + atom.disordered_get_list()) 82 else: 83 undisordered_atom_list.append(atom) 84 return undisordered_atom_list
85
86 - def get_segid(self):
87 return self.segid
88
89 - def get_atoms(self):
90 for a in self: 91 yield a
92
93 - def get_atom(self):
94 warnings.warn("`get_atom` has been deprecated and we intend to remove it" 95 " in a future release of Biopython. Please use `get_atoms` instead.", 96 BiopythonDeprecationWarning) 97 for a in self: 98 yield a
99 100
101 -class DisorderedResidue(DisorderedEntityWrapper):
102 """DisorderedResidue is a wrapper around two or more Residue objects. 103 104 It is used to represent point mutations (e.g. there is a Ser 60 and a Cys 60 105 residue, each with 50 % occupancy). 106 """ 107
108 - def __init__(self, id):
109 """Initialize the class.""" 110 DisorderedEntityWrapper.__init__(self, id)
111
112 - def __repr__(self):
113 resname = self.get_resname() 114 hetflag, resseq, icode = self.get_id() 115 full_id = (resname, hetflag, resseq, icode) 116 return "<DisorderedResidue %s het=%s resseq=%i icode=%s>" % full_id
117
118 - def add(self, atom):
119 residue = self.disordered_get() 120 if not atom.is_disordered() == 2: 121 # Atoms in disordered residues should have non-blank 122 # altlocs, and are thus represented by DisorderedAtom objects. 123 resname = residue.get_resname() 124 het, resseq, icode = residue.get_id() 125 # add atom anyway, if PDBParser ignores exception the atom will be part of the residue 126 residue.add(atom) 127 raise PDBConstructionException( 128 "Blank altlocs in duplicate residue %s (%s, %i, %s)" 129 % (resname, het, resseq, icode)) 130 residue.add(atom)
131
132 - def sort(self):
133 """Sort the atoms in the child Residue objects.""" 134 for residue in self.disordered_get_list(): 135 residue.sort()
136
137 - def disordered_add(self, residue):
138 """Add a residue object and use its resname as key. 139 140 Arguments: 141 - residue - Residue object 142 143 """ 144 resname = residue.get_resname() 145 # add chain parent to residue 146 chain = self.get_parent() 147 residue.set_parent(chain) 148 assert(not self.disordered_has_id(resname)) 149 self[resname] = residue 150 self.disordered_select(resname)
151