diff --git a/package/CHANGELOG b/package/CHANGELOG index 56455f2ab9e..ccec770ef65 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -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) diff --git a/package/MDAnalysis/topology/ITPParser.py b/package/MDAnalysis/topology/ITPParser.py index 952ee866d69..228fccebe73 100644 --- a/package/MDAnalysis/topology/ITPParser.py +++ b/package/MDAnalysis/topology/ITPParser.py @@ -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): diff --git a/testsuite/MDAnalysisTests/data/gromacs/gromos54a7_edited.ff/test.itp b/testsuite/MDAnalysisTests/data/gromacs/gromos54a7_edited.ff/test.itp new file mode 100644 index 00000000000..def77229992 --- /dev/null +++ b/testsuite/MDAnalysisTests/data/gromacs/gromos54a7_edited.ff/test.itp @@ -0,0 +1,2 @@ +[ atoms ] +1 H 1 SOL HW1 1 0.41 1.00800 \ No newline at end of file diff --git a/testsuite/MDAnalysisTests/topology/test_itp.py b/testsuite/MDAnalysisTests/topology/test_itp.py index 3f0607d8def..bd79384a0ae 100644 --- a/testsuite/MDAnalysisTests/topology/test_itp.py +++ b/testsuite/MDAnalysisTests/topology/test_itp.py @@ -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 @@ -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