Package Bio :: Package PDB :: Module Chain
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.Chain

  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  """Chain class, used in Structure objects.""" 
  7   
  8  from Bio.PDB.Entity import Entity 
  9   
 10   
11 -class Chain(Entity):
12 - def __init__(self, id):
13 self.level = "C" 14 Entity.__init__(self, id)
15 16 # Private methods 17
18 - def _sort(self, r1, r2):
19 """Sort function for residues in a chain (PRIVATE). 20 21 Residues are first sorted according to their hetatm records. 22 Protein and nucleic acid residues first, hetatm residues next, 23 and waters last. Within each group, the residues are sorted according 24 to their resseq's (sequence identifiers). Finally, residues with the 25 same resseq's are sorted according to icode. 26 27 Arguments: 28 29 - r1, r2 - Residue objects 30 31 """ 32 hetflag1, resseq1, icode1 = r1.id 33 hetflag2, resseq2, icode2 = r2.id 34 if hetflag1 != hetflag2: 35 return cmp(hetflag1[0], hetflag2[0]) 36 elif resseq1 != resseq2: 37 return cmp(resseq1, resseq2) 38 return cmp(icode1, icode2)
39
40 - def _translate_id(self, id):
41 """Translate sequence identifer to tuple form (PRIVATE). 42 43 A residue id is normally a tuple (hetero flag, sequence identifier, 44 insertion code). Since for most residues the hetero flag and the 45 insertion code are blank (i.e. " "), you can just use the sequence 46 identifier to index a residue in a chain. The _translate_id method 47 translates the sequence identifier to the (" ", sequence identifier, 48 " ") tuple. 49 50 Arguments: 51 52 - id - int, residue resseq 53 54 """ 55 if isinstance(id, int): 56 id = (' ', id, ' ') 57 return id
58 59 # Special methods 60
61 - def __getitem__(self, id):
62 """Return the residue with given id. 63 64 The id of a residue is (hetero flag, sequence identifier, insertion code). 65 If id is an int, it is translated to (" ", id, " ") by the _translate_id 66 method. 67 68 Arguments: 69 70 - id - (string, int, string) or int 71 72 """ 73 id = self._translate_id(id) 74 return Entity.__getitem__(self, id)
75
76 - def __contains__(self, id):
77 """True if a residue with given id is present in this chain. 78 79 Arguments: 80 81 - id - (string, int, string) or int 82 83 """ 84 id = self._translate_id(id) 85 return Entity.__contains__(self, id)
86
87 - def __delitem__(self, id):
88 """Delete item. 89 90 Arguments: 91 92 - id - (string, int, string) or int 93 94 """ 95 id = self._translate_id(id) 96 return Entity.__delitem__(self, id)
97
98 - def __repr__(self):
99 return "<Chain id=%s>" % self.get_id()
100 101 # Public methods 102
103 - def get_unpacked_list(self):
104 """Return a list of undisordered residues. 105 106 Some Residue objects hide several disordered residues 107 (DisorderedResidue objects). This method unpacks them, 108 ie. it returns a list of simple Residue objects. 109 """ 110 unpacked_list = [] 111 for residue in self.get_list(): 112 if residue.is_disordered() == 2: 113 for dresidue in residue.disordered_get_list(): 114 unpacked_list.append(dresidue) 115 else: 116 unpacked_list.append(residue) 117 return unpacked_list
118
119 - def has_id(self, id):
120 """Return 1 if a residue with given id is present. 121 122 The id of a residue is (hetero flag, sequence identifier, insertion code). 123 124 If id is an int, it is translated to (" ", id, " ") by the _translate_id 125 method. 126 127 Arguments: 128 129 - id - (string, int, string) or int 130 131 """ 132 id = self._translate_id(id) 133 return Entity.has_id(self, id)
134 135 # Public 136
137 - def get_residues(self):
138 for r in self: 139 yield r
140
141 - def get_atoms(self):
142 for r in self.get_residues(): 143 for a in r: 144 yield a
145