From 09aa17ebeebf412d37542e09804c94e05c7c5f89 Mon Sep 17 00:00:00 2001 From: Co Quach Date: Tue, 17 Oct 2023 15:16:36 -0500 Subject: [PATCH 1/4] parse atom/residue name/id by slicing string --- gmso/formats/gro.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/gmso/formats/gro.py b/gmso/formats/gro.py index f9315648f..f5fa3b6ac 100644 --- a/gmso/formats/gro.py +++ b/gmso/formats/gro.py @@ -57,7 +57,12 @@ def read_gro(filename): coords = u.nm * np.zeros(shape=(n_atoms, 3)) for row, _ in enumerate(coords): line = gro_file.readline() - content = line.split() + res_id = int(line[:5]) + res_name = line[5:10].strip() + atom_name = line[10:15].strip() + atom_id = line[15:20].strip() + + positions = line[20:].split() if not line: msg = ( "Incorrect number of lines in .gro file. Based on the " @@ -66,22 +71,17 @@ def read_gro(filename): ) raise ValueError(msg.format(n_atoms)) - res = content[0] - atom_name = content[1] - atom_id = content[2] coords[row] = u.nm * np.array( [ - float(content[3]), - float(content[4]), - float(content[5]), + float(positions[0]), + float(positions[1]), + float(positions[2]), ] ) site = Atom(name=atom_name, position=coords[row]) - r = re.compile("([0-9]+)([a-zA-Z]+)") - m = r.match(res) - site.molecule = (m.group(2), int(m.group(1))) - site.residue = (m.group(2), int(m.group(1))) + site.molecule = (res_name, res_id) + site.residue = (res_name, res_id) top.add_site(site, update_types=False) top.update_topology() From 4957211bfe385f00e5a568d41e6e424fc55e1ffa Mon Sep 17 00:00:00 2001 From: Co Quach <43968221+daico007@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:27:28 -0500 Subject: [PATCH 2/4] Update gmso/formats/gro.py --- gmso/formats/gro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gmso/formats/gro.py b/gmso/formats/gro.py index f5fa3b6ac..ab910183c 100644 --- a/gmso/formats/gro.py +++ b/gmso/formats/gro.py @@ -57,7 +57,7 @@ def read_gro(filename): coords = u.nm * np.zeros(shape=(n_atoms, 3)) for row, _ in enumerate(coords): line = gro_file.readline() - res_id = int(line[:5]) + res_id = int(line[:5].strip()) res_name = line[5:10].strip() atom_name = line[10:15].strip() atom_id = line[15:20].strip() From 2df87bd15e35eb90739dc138a7940fc54101a286 Mon Sep 17 00:00:00 2001 From: Co Quach Date: Tue, 17 Oct 2023 22:52:13 -0500 Subject: [PATCH 3/4] reorder error checking block, update test --- gmso/formats/gro.py | 11 +++++------ gmso/tests/test_gro.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/gmso/formats/gro.py b/gmso/formats/gro.py index ab910183c..891a8c311 100644 --- a/gmso/formats/gro.py +++ b/gmso/formats/gro.py @@ -57,12 +57,6 @@ def read_gro(filename): coords = u.nm * np.zeros(shape=(n_atoms, 3)) for row, _ in enumerate(coords): line = gro_file.readline() - res_id = int(line[:5].strip()) - res_name = line[5:10].strip() - atom_name = line[10:15].strip() - atom_id = line[15:20].strip() - - positions = line[20:].split() if not line: msg = ( "Incorrect number of lines in .gro file. Based on the " @@ -70,7 +64,12 @@ def read_gro(filename): "atoms were expected, but at least one fewer was found." ) raise ValueError(msg.format(n_atoms)) + res_id = int(line[:5].strip()) + res_name = line[5:10].strip() + atom_name = line[10:15].strip() + atom_id = line[15:20].strip() + positions = line[20:].split() coords[row] = u.nm * np.array( [ float(positions[0]), diff --git a/gmso/tests/test_gro.py b/gmso/tests/test_gro.py index 946d9f5dc..d19227dd2 100644 --- a/gmso/tests/test_gro.py +++ b/gmso/tests/test_gro.py @@ -39,7 +39,7 @@ def test_read_gro(self): ) def test_wrong_n_atoms(self): - with pytest.raises(IndexError): + with pytest.raises(ValueError): Topology.load(get_fn("too_few_atoms.gro")) with pytest.raises(ValueError): Topology.load(get_fn("too_many_atoms.gro")) From 908b408c3099115bd6e90d4fff8e342effc8a064 Mon Sep 17 00:00:00 2001 From: Co Quach Date: Thu, 11 Jan 2024 13:21:06 -0600 Subject: [PATCH 4/4] add warnings if velocity info is present --- gmso/formats/gro.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gmso/formats/gro.py b/gmso/formats/gro.py index f6eb8d26e..bf21aef42 100644 --- a/gmso/formats/gro.py +++ b/gmso/formats/gro.py @@ -42,8 +42,8 @@ def read_gro(filename): Gro files do not specify connections between atoms, the returned topology will not have connections between sites either. - Currently this implementation does not support a gro file with more than 1 - frame. + Currently this implementation does not support parsing velocities from a gro file or gro file + with more than 1 frame. All residues and resid information from the gro file are currently lost when converting to `topology`. @@ -82,6 +82,11 @@ def read_gro(filename): site.molecule = (res_name, res_id) site.residue = (res_name, res_id) top.add_site(site, update_types=False) + + if len(positions) == 6: + warnings.warn( + "Velocity information presents but will not be parsed." + ) top.update_topology() # Box information