1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
# Copyright 2009-2011 by Eric Talevich. All rights reserved.
# Revisions copyright 2009-2013 by Peter Cock. All rights reserved.
# Revisions copyright 2013 Lenna X. Peterson. All rights reserved.
# Revisions copyright 2020 Joao Rodrigues. All rights reserved.
#
# Converted by Eric Talevich from an older unit test copyright 2002
# by Thomas Hamelryck.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.
"""Unit tests for the Bio.PDB.PDBIO module."""
import os
import tempfile
import unittest
import warnings
from Bio import BiopythonWarning
from Bio.PDB import PDBParser, PDBIO, Select
from Bio.PDB import Atom, Residue
from Bio.PDB.PDBExceptions import PDBConstructionException, PDBConstructionWarning
class WriteTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.io = PDBIO()
self.parser = PDBParser(PERMISSIVE=1)
with warnings.catch_warnings():
warnings.simplefilter("ignore", PDBConstructionWarning)
self.structure = self.parser.get_structure("example", "PDB/1A8O.pdb")
def test_pdbio_write_structure(self):
"""Write a full structure using PDBIO."""
struct1 = self.structure
# Ensure that set_structure doesn't alter parent
parent = struct1.parent
# Write full model to temp file
self.io.set_structure(struct1)
self.assertIs(parent, struct1.parent)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
struct2 = self.parser.get_structure("1a8o", filename)
nresidues = len(list(struct2.get_residues()))
self.assertEqual(len(struct2), 1)
self.assertEqual(nresidues, 158)
finally:
os.remove(filename)
def test_pdbio_write_preserve_numbering(self):
"""Test writing PDB and preserve atom numbering."""
self.io.set_structure(self.structure)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename) # default preserve_atom_numbering=False
struct = self.parser.get_structure("1a8o", filename)
serials = [a.serial_number for a in struct.get_atoms()]
og_serials = list(range(1, len(serials) + 1))
self.assertEqual(og_serials, serials)
finally:
os.remove(filename)
def test_pdbio_write_auto_numbering(self):
"""Test writing PDB and do not preserve atom numbering."""
self.io.set_structure(self.structure)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename, preserve_atom_numbering=True)
struct = self.parser.get_structure("1a8o", filename)
serials = [a.serial_number for a in struct.get_atoms()]
og_serials = [a.serial_number for a in self.structure.get_atoms()]
self.assertEqual(og_serials, serials)
finally:
os.remove(filename)
def test_pdbio_write_residue(self):
"""Write a single residue using PDBIO."""
struct1 = self.structure
residue1 = list(struct1.get_residues())[0]
# Ensure that set_structure doesn't alter parent
parent = residue1.parent
# Write full model to temp file
self.io.set_structure(residue1)
self.assertIs(parent, residue1.parent)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
struct2 = self.parser.get_structure("1a8o", filename)
nresidues = len(list(struct2.get_residues()))
self.assertEqual(nresidues, 1)
finally:
os.remove(filename)
def test_pdbio_write_residue_w_chain(self):
"""Write a single residue (chain id == X) using PDBIO."""
struct1 = self.structure.copy() # make copy so we can change it
residue1 = list(struct1.get_residues())[0]
# Modify parent id
parent = residue1.parent
parent.id = "X"
# Write full model to temp file
self.io.set_structure(residue1)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
struct2 = self.parser.get_structure("1a8o", filename)
nresidues = len(list(struct2.get_residues()))
self.assertEqual(nresidues, 1)
# Assert chain remained the same
chain_id = [c.id for c in struct2.get_chains()][0]
self.assertEqual(chain_id, "X")
finally:
os.remove(filename)
def test_pdbio_write_residue_wout_chain(self):
"""Write a single orphan residue using PDBIO."""
struct1 = self.structure
residue1 = list(struct1.get_residues())[0]
residue1.parent = None # detach residue
# Write full model to temp file
self.io.set_structure(residue1)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
struct2 = self.parser.get_structure("1a8o", filename)
nresidues = len(list(struct2.get_residues()))
self.assertEqual(nresidues, 1)
# Assert chain is default: "A"
chain_id = [c.id for c in struct2.get_chains()][0]
self.assertEqual(chain_id, "A")
finally:
os.remove(filename)
def test_pdbio_write_custom_residue(self):
"""Write a chainless residue using PDBIO."""
res = Residue.Residue((" ", 1, " "), "DUM", "")
atm = Atom.Atom("CA", [0.1, 0.1, 0.1], 1.0, 1.0, " ", "CA", 1, "C")
res.add(atm)
# Ensure that set_structure doesn't alter parent
parent = res.parent
# Write full model to temp file
self.io.set_structure(res)
self.assertIs(parent, res.parent)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
struct2 = self.parser.get_structure("res", filename)
latoms = list(struct2.get_atoms())
self.assertEqual(len(latoms), 1)
self.assertEqual(latoms[0].name, "CA")
self.assertEqual(latoms[0].parent.resname, "DUM")
self.assertEqual(latoms[0].parent.parent.id, "A")
finally:
os.remove(filename)
def test_pdbio_select(self):
"""Write a selection of the structure using a Select subclass."""
# Selection class to filter all alpha carbons
class CAonly(Select):
"""Accepts only CA residues."""
def accept_atom(self, atom):
if atom.name == "CA" and atom.element == "C":
return 1
struct1 = self.structure
# Ensure that set_structure doesn't alter parent
parent = struct1.parent
# Write to temp file
self.io.set_structure(struct1)
self.assertIs(parent, struct1.parent)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename, CAonly())
struct2 = self.parser.get_structure("1a8o", filename)
nresidues = len(list(struct2.get_residues()))
self.assertEqual(nresidues, 70)
finally:
os.remove(filename)
def test_pdbio_missing_occupancy(self):
"""Write PDB file with missing occupancy."""
with warnings.catch_warnings():
warnings.simplefilter("ignore", PDBConstructionWarning)
structure = self.parser.get_structure("test", "PDB/occupancy.pdb")
self.io.set_structure(structure)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", BiopythonWarning)
self.io.save(filename)
self.assertEqual(len(w), 1, w)
with warnings.catch_warnings():
warnings.simplefilter("ignore", PDBConstructionWarning)
struct2 = self.parser.get_structure("test", filename)
atoms = struct2[0]["A"][(" ", 152, " ")]
self.assertEqual(atoms["N"].get_occupancy(), None)
finally:
os.remove(filename)
def test_pdbio_write_truncated(self):
"""Test parsing of truncated lines."""
struct = self.structure
# Write to temp file
self.io.set_structure(struct)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
# Check if there are lines besides 'ATOM', 'TER' and 'END'
with open(filename) as handle:
record_set = {l[0:6] for l in handle}
record_set -= {
"ATOM ",
"HETATM",
"MODEL ",
"ENDMDL",
"TER\n",
"TER ",
"END\n",
"END ",
}
self.assertEqual(record_set, set())
finally:
os.remove(filename)
def test_model_numbering(self):
"""Preserve model serial numbers during I/O."""
def confirm_numbering(struct):
self.assertEqual(len(struct), 3)
for idx, model in enumerate(struct):
self.assertEqual(model.serial_num, idx + 1)
self.assertEqual(model.serial_num, model.id + 1)
def confirm_single_end(fname):
"""Ensure there is only one END statement in multi-model files."""
with open(fname) as handle:
end_stment = []
for iline, line in enumerate(handle):
if line.strip() == "END":
end_stment.append((line, iline))
self.assertEqual(len(end_stment), 1) # Only one?
self.assertEqual(end_stment[0][1], iline) # Last line of the file?
with warnings.catch_warnings():
warnings.simplefilter("ignore", PDBConstructionWarning)
struct1 = self.parser.get_structure("1lcd", "PDB/1LCD.pdb")
confirm_numbering(struct1)
# Round trip: serialize and parse again
self.io.set_structure(struct1)
filenumber, filename = tempfile.mkstemp()
os.close(filenumber)
try:
self.io.save(filename)
struct2 = self.parser.get_structure("1lcd", filename)
confirm_numbering(struct2)
confirm_single_end(filename)
finally:
os.remove(filename)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
|