Back to index

python-biopython  1.60
Chain.py
Go to the documentation of this file.
00001 # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk)
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.  
00005 
00006 """Chain class, used in Structure objects."""
00007 
00008 from Bio.PDB.Entity import Entity
00009 
00010 
00011 class Chain(Entity):
00012     def __init__(self, id):
00013         self.level="C"
00014         Entity.__init__(self, id)
00015 
00016     # Private methods
00017 
00018     def _sort(self, r1, r2):
00019         """Sort function for residues in a chain
00020 
00021         Residues are first sorted according to their hetatm records.
00022         Protein and nucleic acid residues first, hetatm residues next, 
00023         and waters last. Within each group, the residues are sorted according
00024         to their resseq's (sequence identifiers). Finally, residues with the
00025         same resseq's are sorted according to icode.
00026 
00027         Arguments:
00028         o r1, r2 - Residue objects
00029         """
00030         hetflag1, resseq1, icode1=r1.id
00031         hetflag2, resseq2, icode2=r2.id
00032         if hetflag1!=hetflag2:
00033             return cmp(hetflag1[0], hetflag2[0])
00034         elif resseq1!=resseq2:
00035             return cmp(resseq1, resseq2)
00036         return cmp(icode1, icode2)
00037 
00038     def _translate_id(self, id):
00039         """
00040         A residue id is normally a tuple (hetero flag, sequence identifier, 
00041         insertion code). Since for most residues the hetero flag and the 
00042         insertion code are blank (i.e. " "), you can just use the sequence 
00043         identifier to index a residue in a chain. The _translate_id method
00044         translates the sequence identifier to the (" ", sequence identifier,
00045         " ") tuple. 
00046 
00047         Arguments:
00048         o id - int, residue resseq 
00049         """
00050         if isinstance(id, int):
00051             id=(' ', id, ' ')
00052         return id
00053             
00054     # Special methods   
00055 
00056     def __getitem__(self, id):
00057         """Return the residue with given id.
00058 
00059         The id of a residue is (hetero flag, sequence identifier, insertion code). 
00060         If id is an int, it is translated to (" ", id, " ") by the _translate_id
00061         method. 
00062 
00063         Arguments:
00064         o id - (string, int, string) or int
00065         """
00066         id=self._translate_id(id)
00067         return Entity.__getitem__(self, id)
00068 
00069     def __contains__(self, id):
00070         """True if a residue with given id is present in this chain.
00071 
00072         Arguments:
00073         o id - (string, int, string) or int
00074         """
00075         id=self._translate_id(id)
00076         return Entity.__contains__(self, id)
00077 
00078     def __delitem__(self, id):
00079         """
00080         Arguments:
00081         o id - (string, int, string) or int
00082         """
00083         id=self._translate_id(id)
00084         return Entity.__delitem__(self, id)
00085 
00086     def __repr__(self):
00087         return "<Chain id=%s>" % self.get_id()
00088 
00089     # Public methods
00090 
00091     def get_unpacked_list(self):
00092         """Return a list of undisordered residues.
00093 
00094         Some Residue objects hide several disordered residues
00095         (DisorderedResidue objects). This method unpacks them, 
00096         ie. it returns a list of simple Residue objects.
00097         """
00098         unpacked_list=[]
00099         for residue in self.get_list():
00100             if residue.is_disordered()==2:
00101                 for dresidue in residue.disordered_get_list():
00102                     unpacked_list.append(dresidue)
00103             else:
00104                 unpacked_list.append(residue)
00105         return unpacked_list
00106 
00107     def has_id(self, id):
00108         """Return 1 if a residue with given id is present.
00109 
00110         The id of a residue is (hetero flag, sequence identifier, insertion code).
00111                 If id is an int, it is translated to (" ", id, " ") by the _translate_id
00112         method.
00113 
00114         Arguments:
00115         o id - (string, int, string) or int
00116         """
00117         id=self._translate_id(id)
00118         return Entity.has_id(self, id)
00119 
00120 
00121     # Public
00122 
00123     def get_residues(self):
00124         for r in self:
00125             yield r
00126 
00127     def get_atoms(self):
00128         for r in self:
00129             for a in r:
00130                 yield a
00131