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

Source Code for Module Bio.PDB.Dice

 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  import re 
 7  import warnings 
 8   
 9  from Bio.PDB.PDBIO import PDBIO 
10   
11   
12  _hydrogen=re.compile("[123 ]*H.*") 
13   
14   
15 -class ChainSelector(object):
16 """ 17 Only accepts residues with right chainid 18 and between start and end. Remove hydrogens, waters and ligands. 19 Only use model 0 by default. 20 """
21 - def __init__(self, chain_id, start, end, model_id=0):
22 self.chain_id=chain_id 23 self.start=start 24 self.end=end 25 self.model_id=0
26
27 - def accept_model(self, model):
28 # model - only keep model 0 29 if model.get_id()==self.model_id: 30 return 1 31 return 0
32
33 - def accept_chain(self, chain):
34 if chain.get_id()==self.chain_id: 35 return 1 36 return 0
37
38 - def accept_residue(self, residue):
39 # residue - between start and end 40 hetatm_flag, resseq, icode=residue.get_id() 41 if hetatm_flag!=" ": 42 # skip HETATMS 43 return 0 44 if icode!=" ": 45 warnings.warn("WARNING: Icode at %s" % residue.get_id(), 46 RuntimeWarning) 47 if self.start<=resseq<=self.end: 48 return 1 49 return 0
50
51 - def accept_atom(self, atom):
52 # atoms - get rid of hydrogens 53 name=atom.get_id() 54 if _hydrogen.match(name): 55 return 0 56 else: 57 return 1
58 59
60 -def extract(structure, chain_id, start, end, filename):
61 """ 62 Write out selected portion to filename. 63 """ 64 sel=ChainSelector(chain_id, start, end) 65 io=PDBIO() 66 io.set_structure(structure) 67 io.save(filename, sel)
68 69 70 if __name__=="__main__": 71 72 from Bio.PDB.PDBParser import PDBParser 73 74 import sys 75 76 p=PDBParser() 77 s=p.get_structure("scr", sys.argv[1]) 78 79 extract(s, " ", 1, 100, "out.pdb") 80