Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hexadecimal ATOM id #383

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pdb2pqr/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,15 @@ def __init__(self, line):
:type line: str
"""
super().__init__(line)
self.serial = int(line[6:11].strip())
try:
self.serial = int(line[6:11].strip())
except ValueError:
try:
self.serial = int(line[6:11].strip(), 16)
except ValueError:
_LOGGER.error(f"Error parsing atom number: {line[6:11].strip()}.")
_LOGGER.error("Use integers or VMD's hexadecimal notation")
raise ValueError(f"Error parsing atom number: {line[6:11].strip()}. Use integers or VMD's hexadecimal notation")
self.name = line[12:16].strip()
self.alt_loc = line[16].strip()
self.res_name = line[17:20].strip()
Expand Down
37 changes: 18 additions & 19 deletions pdb2pqr/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,21 @@ def from_pqr_line(cls, line):
else:
err = f"Unable to parse line: {line}"
raise ValueError(err)
atom.serial = int(words.pop(0))
atom.name = words.pop(0)
atom.res_name = words.pop(0)
token = words.pop(0)
try:
atom.res_seq = int(token)
except ValueError:
atom.chain_id = token
atom.res_seq = int(words.pop(0))
token = words.pop(0)
atom.serial = line[6:11].strip()
try:
atom.x = float(token)
except ValueError:
atom.ins_code = token
atom.x = float(words.pop(0))
atom.y = float(words.pop(0))
atom.z = float(words.pop(0))
atom.charge = float(words.pop(0))
atom.radius = float(words.pop(0))
atom.serial = int(atom.serial)
except:
atom.serial = int(atom.serial, 16)
atom.name = line[12:16].strip()
atom.res_name = line[17:21].strip()
atom.chain_id = line[21:22]
atom.res_seq = int(line[22:26].strip())
atom.ins_code = line[26:27]
atom.x = float(line[30:38].strip())
atom.y = float(line[38:46].strip())
atom.z = float(line[46:54].strip())
atom.charge = float(line[54:62].strip())
atom.radius = float(line[62:69].strip())
return atom

@classmethod
Expand Down Expand Up @@ -265,7 +261,10 @@ def get_common_string_rep(self, chainflag=False):
outstr = ""
tstr = self.type
outstr += str.ljust(tstr, 6)[:6]
tstr = f"{self.serial:d}"
if self.serial > 99999:
tstr = f"{hex(self.serial)[2:]}"
else:
tstr = f"{self.serial:d}"
outstr += str.rjust(tstr, 5)[:5]
outstr += " "
tstr = self.name
Expand Down
10 changes: 10 additions & 0 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ def test_ph_naming(naming_test, tmp_path):
f"--drop-water --whitespace --with-ph={naming_test['pH']} "
f"--titration-state-method=propka"
)

@pytest.mark.parametrize(
"input_pdb, expected_pqr",
[pytest.param("5urf-assembly1.pdb", "5urf-assembly1.pqr", id="HEX Atom Id")],
)
@pytest.mark.long_test
def test_hex_pdb(input_pdb, expected_pqr, tmp_path):
"""Tests for HEX ATOM Id."""
args = "--log-level=ERROR --ff=AMBER --ffout AMBER"
output_pqr = Path(input_pdb).stem + ".pqr"
common.run_pdb2pqr(
args=args,
input_pdb=common.DATA_DIR / input_pdb,
Expand Down
Loading