def test_chemical_composition(self): """ Test crystals.PDBParser returns the same chemical composition as BIO.PDB.PDBParser implementation, i.e. the same elements in the right proportions. """ with catch_warnings(): filterwarnings( "ignore", category=biopdb.PDBExceptions.PDBConstructionWarning) with tempfile.TemporaryDirectory() as temp_dir: for protein_id in self.test_ids: with self.subTest(f"Protein ID: {protein_id}"): with PDBParser(protein_id, download_dir=temp_dir) as parser: fname = self.pdb_list.retrieve_pdb_file( protein_id, pdir=temp_dir, file_format="pdb") # Note: Bio.PDB atoms store element as uppercase strings. Thus, they must be changed to titlecase crystals_chemical_composition = Counter( [atm.element for atm in parser.atoms()]) biopdb_chemical_composition = Counter([ atm.element.title() for atm in self.biopdb_parser.get_structure( protein_id, fname).get_atoms() ]) self.assertDictEqual( biopdb_chemical_composition, crystals_chemical_composition, )
def test_symmetry_operators(self): """ Test that the non-translation part of the symmetry_operators is an invertible matrix of determinant 1 | -1 """ with tempfile.TemporaryDirectory() as temp_dir: with PDBParser("1fbb", download_dir=temp_dir) as parser: for sym_op in parser.symmetry_operators(): t = sym_op[:3, :3] self.assertAlmostEqual(abs(np.linalg.det(t)), 1, places=5)
def test_pdb_parser_symmetry_operators(): """Test that the non-translation part of the symmetry_operators is an invertible matrix of determinant 1 | -1""" with tempfile.TemporaryDirectory() as temp_dir: with PDBParser("1fbb", download_dir=temp_dir) as parser: for sym_op in parser.symmetry_operators(): t = sym_op[:3, :3] assert round(abs(abs(np.linalg.det(t)) - 1), 5) == 0
def test_residues(self): """ Test the parsing of residues for 1fbb """ with tempfile.TemporaryDirectory() as temp_dir: with PDBParser("1fbb", download_dir=temp_dir) as parser: residues = list(parser.residues()) atoms = list(parser.atoms()) # Flatten residues into a list of atoms residue_atoms = list(chain.from_iterable(residues)) self.assertEqual(len(residue_atoms), len(atoms))
def test_atomic_positions(self): """ Test crystals.PDBParser returns atoms in the same position as the BIO.PDB.PDBParser implementation """ with catch_warnings(): filterwarnings( "ignore", category=biopdb.PDBExceptions.PDBConstructionWarning ) with tempfile.TemporaryDirectory() as temp_dir: for protein_id in ("1fbb", "1fat", "1gzx"): with self.subTest(f"Protein ID: {protein_id}"): with PDBParser(protein_id, download_dir=temp_dir) as parser: fname = self.pdb_list.retrieve_pdb_file( protein_id, pdir=temp_dir, file_format="pdb" ) biopdb_atoms = self.biopdb_parser.get_structure( protein_id, fname ).get_atoms() crystals_atoms = parser.atoms() # To compare atom positions, we build "generic" atoms (just tuples (elem, coords)) # Note: Bio.PDB atoms store element as uppercase strings. Thus, they must be changed to titlecase # Since numpy arrays are unhashable, they are converted to tuples # crystals.PDBParser returns atoms in fractional coordinates, so we must also do the same with Bio.PDB atoms bio_pdb_generic_atoms = set() for atm in biopdb_atoms: coords = np.round( frac_coords( atm.coord_fractional, parser.lattice_vectors() ), 3, ) elem = atm.element.title() bio_pdb_generic_atoms.add( GenericAtom(elem, tuple(coords)) ) crystals_generic_atoms = set() for atm in crystals_atoms: coords = np.round(atm.coords_fractional, 3) crystals_generic_atoms.add( GenericAtom(atm.element, tuple(coords)) ) self.assertEqual( bio_pdb_generic_atoms, crystals_generic_atoms )
def test_chemical_composition(protein_id): """Test crystals.PDBParser returns the same chemical composition as BIO.PDB.PDBParser implementation, i.e. the same elements in the right proportions.""" pdb_list = biopdb.PDBList(verbose=False, obsolete_pdb=gettempdir()) biopdb_parser = biopdb.PDBParser() with catch_warnings(): filterwarnings("ignore", category=biopdb.PDBExceptions.PDBConstructionWarning) with tempfile.TemporaryDirectory() as temp_dir: with PDBParser(protein_id, download_dir=temp_dir) as parser: fname = pdb_list.retrieve_pdb_file(protein_id, pdir=temp_dir, file_format="pdb") # Note: Bio.PDB atoms store element as uppercase strings. Thus, they must be changed to titlecase crystals_chemical_composition = Counter( [atm.element for atm in parser.atoms()]) biopdb_chemical_composition = Counter([ atm.element.title() for atm in biopdb_parser.get_structure( protein_id, fname).get_atoms() ]) assert biopdb_chemical_composition == crystals_chemical_composition
def test_default_download_dir(self): """ Test that the file is saved in the correct temporary directory by default """ filename = PDBParser.download_pdb_file("1fbb") self.assertTrue(filename.exists()) self.assertEqual(filename.parent, STRUCTURE_CACHE)
def test_fractional_atoms(self): """ Test the PDBParser returns fractional atomic coordinates. """ with tempfile.TemporaryDirectory() as temp_dir: with PDBParser("1fbb", download_dir=temp_dir) as parser: for atm in parser.atoms(): pass # TODO: find a test for this.
def test_pdb_parser_default_download_dir(): """Test that the file is saved in the correct temporary directory by default""" filename = PDBParser.download_pdb_file("1fbb") assert filename.exists() assert filename.parent == STRUCTURE_CACHE