1
2
3
4
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
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
20
22 """
23 Overload this to reject models for output.
24 """
25 return 1
26
28 """
29 Overload this to reject chains for output.
30 """
31 return 1
32
34 """
35 Overload this to reject residues for output.
36 """
37 return 1
38
40 """
41 Overload this to reject atoms for output.
42 """
43 return 1
44
45
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 """
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
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
87
90
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
114 fp=file
115 close_file=0
116
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
125
126
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
136
137
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