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   
 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 print "WARNING: Icode at ", residue.get_id() 45 if self.start<=resseq<=self.end: 46 return 1 47 return 0
48
49 - def accept_atom(self, atom):
50 # atoms - get rid of hydrogens 51 name=atom.get_id() 52 if _hydrogen.match(name): 53 return 0 54 else: 55 return 1
56 57
58 -def extract(structure, chain_id, start, end, filename):
59 """ 60 Write out selected portion to filename. 61 """ 62 sel=ChainSelector(chain_id, start, end) 63 io=PDBIO() 64 io.set_structure(structure) 65 io.save(filename, sel)
66 67 68 69 if __name__=="__main__": 70 71 from Bio.PDB.PDBParser import PDBParser 72 73 import sys 74 75 p=PDBParser() 76 s=p.get_structure("scr", sys.argv[1]) 77 78 extract(s, " ", 1, 100, "out.pdb") 79