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