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