Skip to content

Commit 3189d48

Browse files
authored
Add edge case checking for Universe._resolve_formats (#5148)
* Fixes #5147 * Changes made in this Pull Request: add edge case checking for _resolve_formats * Don't set topology_format = format when format is a coordinate reader * Don't set format = topology_format when topology_format is a topology reader * update CHANGELOG * add tests
1 parent ab7769f commit 3189d48

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

package/CHANGELOG

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ The rules for this file:
1414

1515

1616
-------------------------------------------------------------------------------
17-
??/??/?? IAlibay, orbeckst, marinegor, tylerjereddy
17+
??/??/?? IAlibay, orbeckst, marinegor, tylerjereddy, ljwoods2
1818

1919
* 2.11.0
2020

2121
Fixes
22+
* Fix incorrect assignment of topology_format to format (and vice versa)
23+
when a parsing class is provided to either (Issue #5147, PR #5148)
2224
* Fix incorrect TPR file parsing for GROMACS topologies produced prior
2325
to version 5.1.0 (Issue #5145, PR #5146)
2426
* Fixes incorrect assignment of secondary structure to proline residues in

package/MDAnalysis/core/universe.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import warnings
6161
import contextlib
6262
import collections
63+
import inspect
6364

6465
import MDAnalysis
6566
import sys
@@ -255,10 +256,33 @@ def _topology_from_file_like(topology_file, topology_format=None, **kwargs):
255256

256257
def _resolve_formats(*coordinates, format=None, topology_format=None):
257258
if not coordinates:
259+
# ensure that a TopologyReader is not used to attempt to parse a trajectory
260+
# Issue #5147
261+
# fully qualified absolute names prevents circular import
258262
if format is None:
259-
format = topology_format
263+
format = (
264+
None
265+
if (inspect.isclass(topology_format))
266+
and (
267+
issubclass(
268+
topology_format,
269+
MDAnalysis.topology.base.TopologyReaderBase,
270+
)
271+
)
272+
else topology_format
273+
)
260274
elif topology_format is None:
261-
topology_format = format
275+
topology_format = (
276+
None
277+
if (inspect.isclass(format))
278+
and (
279+
issubclass(
280+
format,
281+
MDAnalysis.Analysis.coordinates.base.ProtoReader,
282+
)
283+
)
284+
else format
285+
)
262286
return format, topology_format
263287

264288

testsuite/MDAnalysisTests/core/test_universe.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,28 @@ def test_universe_empty_ROMol(self):
255255
u = mda.Universe(mol, format="RDKIT")
256256
assert len(u.atoms) == 0
257257

258+
def test_Universe_top_format_no_coords(self):
259+
# Issue #5147- ensure traj format is not set to topology format
260+
u = mda.Universe(
261+
PDB,
262+
topology_format=MDAnalysis.topology.PDBParser.PDBParser,
263+
format=None,
264+
)
265+
assert not isinstance(
266+
u.trajectory, MDAnalysis.topology.PDBParser.PDBParser
267+
)
268+
269+
def test_Universe_traj_format_no_coords(self):
270+
# Issue #5147- ensure top format is not set to traj format
271+
# no way to get inferred top format from Universe- use internal method call
272+
format, topology_format = MDAnalysis.core.universe._resolve_formats(
273+
None,
274+
format=MDAnalysis.coordinates.PDB.PDBReader,
275+
topology_format=None,
276+
)
277+
278+
assert topology_format is None
279+
258280

259281
class TestUniverseFromSmiles(object):
260282
def setup_class(self):

0 commit comments

Comments
 (0)