def PDBParser(open_file, structure_id=None, forgive=2): """Parse a PDB file and return a Structure object.""" file_ = open_file.readlines() builder = StructureBuilder() c_offset = get_coords_offset(file_) t_offset = get_trailer_offset(file_) raw_coords = file_[c_offset:t_offset] raw_trailer = file_[t_offset:] raw_header = file_[:c_offset] parsed_header = parse_header(raw_header) parsed_trailer = parse_trailer(raw_trailer) structure_id = (structure_id or parsed_header.get('id')) builder.initStructure(structure_id) structure = parse_coords(builder, raw_coords, forgive) # only X-ray structures will contain crystallographic data if parsed_header.get('expdta') == 'X-RAY': symetry_info = get_symmetry(raw_header) parsed_header.update(symetry_info) structure.header = parsed_header structure.trailer = parsed_trailer structure.raw_header = raw_header structure.raw_trailer = raw_trailer return structure
def test_parse_coords(self): """testing minimal structure building and coords parsing. """ builder = StructureBuilder() builder.initStructure('JUNK') atom = 'ATOM 10 CA PRO A 2 51.588 38.262 31.417 1.00 6.58 C \n' hetatm = 'HETATM 1633 O HOH B 164 17.979 35.529 38.171 1.00 1.02 O \n' lines = ['MODEL ', atom, hetatm] z = parse_coords(builder, lines) assert len(z[(0,)]) == 2 assert len(z[(0,)][('A',)]) == 1 assert len(z[(0,)][('B',)]) == 1 z.setTable() atom1 = z.table['A'][('JUNK', 0, 'A', ('PRO', 2, ' '), ('CA', ' '))] hetatm1 = z.table['A'][('JUNK', 0, 'B', ('H_HOH', 164, ' '), ('O', ' '))] self.assertAlmostEqual([51.588 , 38.262 , 31.417][2], list(atom1.coords)[2]) self.assertAlmostEqual([17.979 , 35.529 , 38.171][2], list(hetatm1.coords)[2])
def test_parse_coords(self): """testing minimal structure building and coords parsing. """ builder = StructureBuilder() builder.initStructure("JUNK") atom = "ATOM 10 CA PRO A 2 51.588 38.262 31.417 1.00 6.58 C \n" hetatm = "HETATM 1633 O HOH B 164 17.979 35.529 38.171 1.00 1.02 O \n" lines = ["MODEL ", atom, hetatm] z = parse_coords(builder, lines) assert len(z[(0,)]) == 2 assert len(z[(0,)][("A",)]) == 1 assert len(z[(0,)][("B",)]) == 1 z.setTable() atom1 = z.table["A"][("JUNK", 0, "A", ("PRO", 2, " "), ("CA", " "))] hetatm1 = z.table["A"][("JUNK", 0, "B", ("H_HOH", 164, " "), ("O", " "))] self.assertAlmostEqual([51.588, 38.262, 31.417][2], list(atom1.coords)[2]) self.assertAlmostEqual([17.979, 35.529, 38.171][2], list(hetatm1.coords)[2])