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, charge=" "):
69 """Returns an ATOM PDB string (PRIVATE).""" 70 if hetfield!=" ": 71 record_type="HETATM" 72 else: 73 record_type="ATOM " 74 if atom.element: 75 #TODO - Check against a list of allowed elements? 76 element = atom.element.strip().upper() 77 if len(atom.element) > 2 or not element.isalpha(): 78 raise ValueError("Unrecognised element %s" % repr(atom.element)) 79 element = element.rjust(2) 80 else: 81 element = " " 82 name=atom.get_fullname() 83 altloc=atom.get_altloc() 84 x, y, z=atom.get_coord() 85 bfactor=atom.get_bfactor() 86 occupancy=atom.get_occupancy() 87 args=(record_type, atom_number, name, altloc, resname, chain_id, 88 resseq, icode, x, y, z, occupancy, bfactor, segid, 89 element, charge) 90 return _ATOM_FORMAT_STRING % args
91 92 # Public methods 93
94 - def set_structure(self, structure):
96
97 - def save(self, file, select=Select(), write_end=0):
98 """ 99 @param file: output file 100 @type file: string or filehandle 101 102 @param select: selects which entities will be written. 103 @type select: 104 select hould have the following methods: 105 - accept_model(model) 106 - accept_chain(chain) 107 - accept_residue(residue) 108 - accept_atom(atom) 109 These methods should return 1 if the entity 110 is to be written out, 0 otherwise. 111 112 Typically select is a subclass of L{Select}. 113 """ 114 get_atom_line=self._get_atom_line 115 if isinstance(file, basestring): 116 fp=open(file, "w") 117 close_file=1 118 else: 119 # filehandle, I hope :-) 120 fp=file 121 close_file=0 122 # multiple models? 123 if len(self.structure)>1 or self.use_model_flag: 124 model_flag=1 125 else: 126 model_flag=0 127 for model in self.structure.get_list(): 128 if not select.accept_model(model): 129 continue 130 # necessary for ENDMDL 131 # do not write ENDMDL if no residues were written 132 # for this model 133 model_residues_written=0 134 atom_number=1 135 if model_flag: 136 fp.write("MODEL \n") 137 for chain in model.get_list(): 138 if not select.accept_chain(chain): 139 continue 140 chain_id=chain.get_id() 141 # necessary for TER 142 # do not write TER if no residues were written 143 # for this chain 144 chain_residues_written=0 145 for residue in chain.get_unpacked_list(): 146 if not select.accept_residue(residue): 147 continue 148 hetfield, resseq, icode=residue.get_id() 149 resname=residue.get_resname() 150 segid=residue.get_segid() 151 for atom in residue.get_unpacked_list(): 152 if select.accept_atom(atom): 153 chain_residues_written=1 154 model_residues_written=1 155 s=get_atom_line(atom, hetfield, segid, atom_number, resname, 156 resseq, icode, chain_id) 157 fp.write(s) 158 atom_number=atom_number+1 159 if chain_residues_written: 160 fp.write("TER\n") 161 if model_flag and model_residues_written: 162 fp.write("ENDMDL\n") 163 if write_end: 164 fp.write('END\n') 165 if close_file: 166 fp.close()
167 168 if __name__=="__main__": 169 170 from Bio.PDB.PDBParser import PDBParser 171 172 import sys 173 174 p=PDBParser(PERMISSIVE=1) 175 176 s=p.get_structure("test", sys.argv[1]) 177 178 io=PDBIO() 179 io.set_structure(s) 180 io.save("out1.pdb") 181 182 fp=open("out2.pdb", "w") 183 s1=p.get_structure("test1", sys.argv[1]) 184 s2=p.get_structure("test2", sys.argv[2]) 185 io=PDBIO(1) 186 io.set_structure(s1) 187 io.save(fp) 188 io.set_structure(s2) 189 io.save(fp, write_end=1) 190 fp.close() 191