Commit edbe9a74 authored by peastman's avatar peastman
Browse files

Handle hex values for atom and residue numbers

parent ba66e90e
......@@ -141,6 +141,8 @@ class PdbStructure(object):
self.sequences = []
self.modified_residues = []
# read file
self._atom_numbers_are_hex = False
self._residue_numbers_are_hex = False
self._load(input_stream)
def _load(self, input_stream):
......@@ -148,7 +150,7 @@ class PdbStructure(object):
for pdb_line in input_stream:
# Look for atoms
if (pdb_line.find("ATOM ") == 0) or (pdb_line.find("HETATM") == 0):
self._add_atom(Atom(pdb_line))
self._add_atom(Atom(pdb_line, self))
# Notice MODEL punctuation, for the next level of detail
# in the structure->model->chain->residue->atom->position hierarchy
elif (pdb_line.find("MODEL") == 0):
......@@ -653,7 +655,7 @@ class Residue(object):
class Atom(object):
"""Atom represents one atom in a PDB structure.
"""
def __init__(self, pdb_line):
def __init__(self, pdb_line, pdbstructure=None):
"""Create a new pdb.Atom from an ATOM or HETATM line.
Example line:
......@@ -688,10 +690,17 @@ class Atom(object):
self.is_first_atom_in_chain = False
self.is_final_atom_in_chain = False
self.is_first_residue_in_chain = False
self.is_final_residue_in_chain = False
self.is_final_residue_in_chain = False
# Start parsing fields from pdb line
self.record_name = pdb_line[0:6].strip()
self.serial_number = int(pdb_line[6:11])
if pdbstructure is not None and pdbstructure._atom_numbers_are_hex:
self.serial_number = int(pdb_line[6:11], 16)
else:
try:
self.serial_number = int(pdb_line[6:11])
except:
self.serial_number = int(pdb_line[6:11], 16)
pdbstructure._atom_numbers_are_hex = True
self.name_with_spaces = pdb_line[12:16]
alternate_location_indicator = pdb_line[16]
......@@ -707,7 +716,14 @@ class Atom(object):
self.residue_name = self.residue_name_with_spaces.strip()
self.chain_id = pdb_line[21]
self.residue_number = int(pdb_line[22:26])
if pdbstructure is not None and pdbstructure._residue_numbers_are_hex:
self.residue_number = int(pdb_line[22:26], 16)
else:
try:
self.residue_number = int(pdb_line[22:26])
except:
self.residue_number = int(pdb_line[22:26], 16)
pdbstructure._residue_numbers_are_hex = True
self.insertion_code = pdb_line[26]
# coordinates, occupancy, and temperature factor belong in Atom.Location object
x = float(pdb_line[30:38])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment