Skip to content

Commit

Permalink
Use absolute file paths in ITPParser (MDAnalysis#3108)
Browse files Browse the repository at this point in the history
Fixes MDAnalysis#3037

Co-authored-by: Lily Wang <31115101+lilyminium@users.noreply.github.com>
  • Loading branch information
aditya-kamath and lilyminium committed Apr 28, 2021
1 parent 17797e9 commit 3a34048
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The rules for this file:
* 2.0.0

Fixes
* ITPParser now accepts relative paths (Issue #3037, PR #3108)
* Fixed issue with unassigned 'GAP' variable in fasta2algin function when
resids are provided in input (Issue #3124)
* Improve diffusionmap coverage (Issue #3208)
Expand Down
3 changes: 1 addition & 2 deletions package/MDAnalysis/topology/ITPParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,9 @@ def find_path(self, path):
current_file = self.current_file

try:
path = path.name
path = os.path.abspath(path.name)
except AttributeError:
pass

current_dir = os.path.dirname(current_file)
dir_path = os.path.join(current_dir, path)
if os.path.exists(dir_path):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ atoms ]
1 H 1 SOL HW1 1 0.41 1.00800
55 changes: 54 additions & 1 deletion testsuite/MDAnalysisTests/topology/test_itp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
import pytest

from pathlib import Path
import MDAnalysis as mda
import numpy as np
from numpy.testing import assert_almost_equal, assert_equal
Expand Down Expand Up @@ -366,3 +366,56 @@ def test_missing_endif(self):
with pytest.raises(IOError):
with self.parser(ITP_no_endif) as p:
top = p.parse(include_dir=GMX_DIR)


class TestRelativePath:
def test_relstring(self, tmpdir):
content = """ #include "../sub3/test2.itp"
[ atoms ]
1 H 1 SOL HW1 1 0.41 1.00800
"""
content2 = """[ atoms ]
1 H 1 SOL HW1 1 0.41 1.00800
"""
p = tmpdir.mkdir("sub1").join("test.itp")
p.write(content)
p3 = tmpdir.mkdir("sub3").join("test2.itp")
p3.write(content2)
p2 = tmpdir.mkdir("sub2")
p2.chdir()
with p2.as_cwd() as pchange:
u = mda.Universe(str("../sub1/test.itp"), format='ITP')

def test_relpath(self, tmpdir):
content = """
[ atoms ]
1 H 1 SOL HW1 1 0.41 1.00800
"""
p = tmpdir.mkdir("sub1").join("test.itp")
p.write(content)
p2 = tmpdir.mkdir("sub2")
p2.chdir()
with p2.as_cwd() as pchange:
relpath = Path("../sub1/test.itp")
u = mda.Universe(relpath, format='ITP')

def test_relative_path(self, tmpdir):
test_itp_content = '#include "../atoms.itp"'
atoms_itp_content = """
[ moleculetype ]
UNK 3
[ atoms ]
1 H 1 SOL HW1 1 0.41 1.00800
"""
with tmpdir.as_cwd():
with open("atoms.itp", "w") as f:
f.write(atoms_itp_content)
subdir = tmpdir.mkdir("subdir")
with subdir.as_cwd():
with open("test.itp", "w") as f:
f.write(test_itp_content)
subsubdir = subdir.mkdir("subsubdir")
with subsubdir.as_cwd():
u = mda.Universe("../test.itp")
assert len(u.atoms) == 1

0 comments on commit 3a34048

Please sign in to comment.