Back to index

python-biopython  1.60
Dice.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 import re
00007 import warnings
00008 
00009 from Bio.PDB.PDBIO import PDBIO
00010 
00011 
00012 _hydrogen=re.compile("[123 ]*H.*")
00013 
00014 
00015 class ChainSelector(object):
00016     """
00017     Only accepts residues with right chainid
00018     and between start and end. Remove hydrogens, waters and ligands.
00019     Only use model 0 by default.
00020     """
00021     def __init__(self, chain_id, start, end, model_id=0):
00022         self.chain_id=chain_id
00023         self.start=start
00024         self.end=end
00025         self.model_id=0
00026 
00027     def accept_model(self, model):
00028         # model - only keep model 0
00029         if model.get_id()==self.model_id:
00030             return 1
00031         return 0
00032 
00033     def accept_chain(self, chain):
00034         if chain.get_id()==self.chain_id:
00035             return 1
00036         return 0
00037 
00038     def accept_residue(self, residue):
00039         # residue - between start and end
00040         hetatm_flag, resseq, icode=residue.get_id()
00041         if hetatm_flag!=" ":
00042             # skip HETATMS
00043             return 0
00044         if icode!=" ":
00045             warnings.warn("WARNING: Icode at %s" % residue.get_id(),
00046                           RuntimeWarning)
00047         if self.start<=resseq<=self.end:
00048             return 1
00049         return 0
00050 
00051     def accept_atom(self, atom):
00052         # atoms - get rid of hydrogens
00053         name=atom.get_id()
00054         if _hydrogen.match(name):
00055             return 0
00056         else:
00057             return 1
00058 
00059 
00060 def extract(structure, chain_id, start, end, filename):
00061     """
00062     Write out selected portion to filename.
00063     """
00064     sel=ChainSelector(chain_id, start, end)
00065     io=PDBIO()
00066     io.set_structure(structure)
00067     io.save(filename, sel)
00068 
00069 
00070 
00071 if __name__=="__main__":
00072 
00073     from Bio.PDB.PDBParser import PDBParser
00074 
00075     import sys
00076 
00077     p=PDBParser()
00078     s=p.get_structure("scr", sys.argv[1])
00079 
00080     extract(s, " ", 1, 100, "out.pdb")
00081