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
|
# Copyright 2016 by Jacek Smietanski. All rights reserved.
# 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.
"""Testing access to the PDB over the internet."""
import contextlib
import os
import shutil
import tempfile
import unittest
import requires_internet
# We want to test this module:
from Bio.PDB.PDBList import PDBList
requires_internet.check()
class TestPBDListGetList(unittest.TestCase):
"""Test methods responsible for getting lists of entries."""
def test_get_recent_changes(self):
"""Tests the Bio.PDB.PDBList.get_recent_changes method."""
# obsolete_pdb declared to prevent from creating the "obsolete" directory
pdblist = PDBList(obsolete_pdb="unimportant")
url = pdblist.pdb_server + "/pub/pdb/data/status/latest/added.pdb"
entries = pdblist.get_status_list(url)
self.assertIsNotNone(entries)
def test_get_all_entries(self):
"""Tests the Bio.PDB.PDBList.get_all_entries method."""
# obsolete_pdb declared to prevent from creating the "obsolete" directory
pdblist = PDBList(obsolete_pdb="unimportant")
entries = pdblist.get_all_entries()
# As number of entries constantly grow, test checks if a certain number was
# exceeded
self.assertGreater(len(entries), 100000)
def test_get_all_obsolete(self):
"""Tests the Bio.PDB.PDBList.get_all_obsolete method."""
# obsolete_pdb declared to prevent from creating the "obsolete" directory
pdblist = PDBList(obsolete_pdb="unimportant")
entries = pdblist.get_all_obsolete()
# As number of obsolete entries constantly grow, test checks if a certain number
# was exceeded
self.assertGreater(len(entries), 3000)
def test_get_all_assemblies(self):
"""Tests the Bio.PDB.PDBList.get_all_assemblies method."""
# obsolete_pdb declared to prevent from creating the "obsolete" directory
pdblist = PDBList(obsolete_pdb="unimportant")
entries = pdblist.get_all_assemblies()
# As number of obsolete entries constantly grow, test checks if a certain number
# was exceeded
self.assertGreater(len(entries), 100000)
class TestPDBListGetStructure(unittest.TestCase):
"""Test methods responsible for getting structures."""
@contextlib.contextmanager
def make_temp_directory(self, directory):
temp_dir = tempfile.mkdtemp(dir=directory)
try:
yield temp_dir
finally:
shutil.rmtree(temp_dir)
def check(self, structure, filename, file_format, obsolete=False, pdir=None):
with self.make_temp_directory(os.getcwd()) as tmp:
pdblist = PDBList(pdb=tmp)
path = os.path.join(tmp, filename)
if pdir:
pdir = os.path.join(tmp, pdir)
pdblist.retrieve_pdb_file(
structure, obsolete=obsolete, pdir=pdir, file_format=file_format
)
self.assertTrue(os.path.isfile(path))
def test_retrieve_pdb_file_small_pdb(self):
"""Tests retrieving the small molecule in pdb format."""
structure = "127d"
self.check(
structure, os.path.join(structure[1:3], f"pdb{structure}.ent"), "pdb"
)
def test_retrieve_pdb_file_large_pdb(self):
"""Tests retrieving the bundle for large molecule in pdb-like format."""
structure = "3k1q"
self.check(
structure,
os.path.join(structure[1:3], f"{structure}-pdb-bundle.tar"),
"bundle",
)
def test_retrieve_pdb_file_obsolete_pdb(self):
"""Tests retrieving the obsolete molecule in pdb format."""
structure = "347d"
self.check(
structure,
os.path.join("obsolete", structure[1:3], f"pdb{structure}.ent"),
"pdb",
obsolete=True,
)
def test_retrieve_pdb_file_obsolete_mmcif(self):
"""Tests retrieving the obsolete molecule in mmcif format."""
structure = "347d"
self.check(
structure,
os.path.join("obsolete", structure[1:3], f"{structure}.cif"),
"mmCif",
obsolete=True,
)
def test_retrieve_pdb_file_mmcif(self):
"""Tests retrieving the (non-obsolete) molecule in mmcif format."""
structure = "127d"
self.check(structure, os.path.join(structure[1:3], f"{structure}.cif"), "mmCif")
def test_retrieve_pdb_file_obsolete_xml(self):
"""Tests retrieving the obsolete molecule in mmcif format."""
structure = "347d"
self.check(
structure,
os.path.join("obsolete", structure[1:3], f"{structure}.xml"),
"xml",
obsolete=True,
)
def test_retrieve_pdb_file_xml(self):
"""Tests retrieving the (non obsolete) molecule in xml format."""
structure = "127d"
self.check(structure, os.path.join(structure[1:3], f"{structure}.xml"), "xml")
def test_retrieve_pdb_file_mmtf(self):
"""Tests retrieving the molecule in mmtf format."""
structure = "127d"
self.check(structure, os.path.join(structure[1:3], f"{structure}.mmtf"), "mmtf")
def test_double_retrieve_structure(self):
"""Tests retrieving the same file to different directories."""
structure = "127d"
self.check(structure, os.path.join("a", f"{structure}.cif"), "mmCif", pdir="a")
self.check(structure, os.path.join("b", f"{structure}.cif"), "mmCif", pdir="b")
def test_invalid_file_format(self):
pdb_list = PDBList()
with self.assertRaises(ValueError) as context:
pdb_list.retrieve_pdb_file("127d", file_format="invalid")
self.assertEqual(
"Specified file_format invalid does not exist or is not supported. Please use one of the "
"following: pdb, mmCif, xml, mmtf, bundle.",
str(context.exception),
)
class TestPDBListGetAssembly(unittest.TestCase):
"""Test methods responsible for getting assemblies."""
@contextlib.contextmanager
def make_temp_directory(self, directory):
temp_dir = tempfile.mkdtemp(dir=directory)
try:
yield temp_dir
finally:
shutil.rmtree(temp_dir)
def check(self, structure, assembly_num, filename, file_format, pdir=None):
with self.make_temp_directory(os.getcwd()) as tmp:
pdblist = PDBList(pdb=tmp)
path = os.path.join(tmp, filename)
if pdir:
pdir = os.path.join(tmp, pdir)
pdblist.retrieve_assembly_file(
structure, assembly_num, pdir=pdir, file_format=file_format
)
self.assertTrue(os.path.isfile(path))
def test_retrieve_assembly_file_mmcif(self):
"""Tests retrieving a small assembly in mmCif format."""
structure = "127d"
assembly_num = "1"
self.check(
structure,
assembly_num,
os.path.join(structure[1:3], f"{structure}-assembly{assembly_num}.cif"),
"mmCif",
)
def test_retrieve_assembly_file_pdb(self):
"""Tests retrieving a small assembly in pdb format."""
structure = "127d"
assembly_num = "1"
self.check(
structure,
assembly_num,
os.path.join(structure[1:3], f"{structure}.pdb{assembly_num}"),
"pdb",
)
def test_double_retrieve_assembly(self):
"""Tests retrieving the same file to different directories."""
structure = "127d"
assembly_num = "1"
self.check(
structure,
assembly_num,
os.path.join("a", f"{structure}-assembly{assembly_num}.cif"),
"mmCif",
pdir="a",
)
self.check(
structure,
assembly_num,
os.path.join("b", f"{structure}-assembly{assembly_num}.cif"),
"mmCif",
pdir="b",
)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
|