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

Source Code for Module Bio.PDB.PDBIO'

  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  __doc__="Output of PDB files." 
  7   
  8   
  9  _ATOM_FORMAT_STRING="%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f%6.2f%6.2f      %4s%2s%2s\n" 
 10   
 11   
12 -class Select:
13 """ 14 Default selection (everything) during writing - can be used as base class 15 to implement selective output. This selects which entities will be written out. 16 """ 17
18 - def __repr__(self):
19 return "<Select all>"
20
21 - def accept_model(self, model):
22 """ 23 Overload this to reject models for output. 24 """ 25 return 1
26
27 - def accept_chain(self, chain):
28 """ 29 Overload this to reject chains for output. 30 """ 31 return 1
32
33 - def accept_residue(self, residue):
34 """ 35 Overload this to reject residues for output. 36 """ 37 return 1
38
39 - def accept_atom(self, atom):
40 """ 41 Overload this to reject atoms for output. 42 """ 43 return 1
44 45
46 -class PDBIO:
47 """ 48 Write a Structure object (or a subset of a Structure object) as a PDB file. 49 50 51 Example: 52 >>> p=PDBParser() 53 >>> s=p.get_structure("1fat", "1fat.pdb") 54 >>> io=PDBIO() 55 >>> io.set_structure(s) 56 >>> io.save("out.pdb") 57 """
58 - def __init__(self, use_model_flag=0):
59 """ 60 @param use_model_flag: if 1, force use of the MODEL record in output. 61 @type use_model_flag: int 62 """ 63 self.use_model_flag=use_model_flag
64 65 # private mathods 66
67 - def _get_atom_line(self, atom, hetfield, segid, atom_number, resname, 68 resseq, icode, chain_id, element=" ", charge=" "):
69 """ 70 Returns an ATOM PDB string. 71 """ 72 if hetfield!=" ": 73 record_type="HETATM" 74 else: 75 record_type="ATOM " 76 name=atom.get_fullname() 77 altloc=atom.get_altloc() 78 x, y, z=atom.get_coord() 79 bfactor=atom.get_bfactor() 80 occupancy=atom.get_occupancy() 81 args=(record_type, atom_number, name, altloc, resname, chain_id, 82 resseq, icode, x, y, z, occupancy, bfactor, segid, 83 element, charge) 84 return _ATOM_FORMAT_STRING % args
85 86 # Public methods 87
88 - def set_structure(self, structure):
90
91 - def save(self, file, select=Select()):
92 """ 93 @param file: output file 94 @type file: string or filehandle 95 96 @param select: selects which entities will be written. 97 @type select: 98 select hould have the following methods: 99 - accept_model(model) 100 - accept_chain(chain) 101 - accept_residue(residue) 102 - accept_atom(atom) 103 These methods should return 1 if the entity 104 is to be written out, 0 otherwise. 105 106 Typically select is a subclass of L{Select}. 107 """ 108 get_atom_line=self._get_atom_line 109 if isinstance(file, basestring): 110 fp=open(file, "w") 111 close_file=1 112 else: 113 # filehandle, I hope :-) 114 fp=file 115 close_file=0 116 # multiple models? 117 if len(self.structure)>1 or self.use_model_flag: 118 model_flag=1 119 else: 120 model_flag=0 121 for model in self.structure.get_list(): 122 if not select.accept_model(model): 123 continue 124 # necessary for ENDMDL 125 # do not write ENDMDL if no residues were written 126 # for this model 127 model_residues_written=0 128 atom_number=1 129 if model_flag: 130 fp.write("MODEL \n") 131 for chain in model.get_list(): 132 if not select.accept_chain(chain): 133 continue 134 chain_id=chain.get_id() 135 # necessary for TER 136 # do not write TER if no residues were written 137 # for this chain 138 chain_residues_written=0 139 for residue in chain.get_unpacked_list(): 140 if not select.accept_residue(residue): 141 continue 142 hetfield, resseq, icode=residue.get_id() 143 resname=residue.get_resname() 144 segid=residue.get_segid() 145 for atom in residue.get_unpacked_list(): 146 if select.accept_atom(atom): 147 chain_residues_written=1 148 model_residues_written=1 149 s=get_atom_line(atom, hetfield, segid, atom_number, resname, 150 resseq, icode, chain_id) 151 fp.write(s) 152 atom_number=atom_number+1 153 if chain_residues_written: 154 fp.write("TER\n") 155 if model_flag and model_residues_written: 156 fp.write("ENDMDL\n") 157 if close_file: 158 fp.close()
159 160 if __name__=="__main__": 161 162 from Bio.PDB.PDBParser import PDBParser 163 164 import sys 165 166 p=PDBParser(PERMISSIVE=1) 167 168 s=p.get_structure("test", sys.argv[1]) 169 170 io=PDBIO() 171 io.set_structure(s) 172 io.save("out1.pdb") 173 174 fp=open("out2.pdb", "w") 175 s1=p.get_structure("test1", sys.argv[1]) 176 s2=p.get_structure("test2", sys.argv[2]) 177 io=PDBIO(1) 178 io.set_structure(s1) 179 io.save(fp) 180 io.set_structure(s2) 181 io.save(fp) 182 fp.close() 183