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

PSFParser reads and converts string-Like residue number to the leading number #4582

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ The rules for this file:
-------------------------------------------------------------------------------
??/??/?? IAlibay, HeetVekariya, marinegor, lilyminium, RMeli,
ljwoods2, aditya292002, pstaerk, PicoCentauri, BFedder,
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, orionarcher
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, orionarcher,
yuxuanzhuang

* 2.8.0

Fixes
* Fix PSFParser error when encoutering string-like resids
* (Issue #2053, Issue #4189 PR #4582)
* Fix doctest errors of analysis/pca.py related to rounding issues
(Issue #3925, PR #4377)
* Convert openmm Quantity to raw value for KE and PE in OpenMMSimulationReader.
Expand Down
32 changes: 32 additions & 0 deletions package/MDAnalysis/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
from functools import wraps
import textwrap
import weakref
import itertools

import mmtf
import numpy as np
Expand Down Expand Up @@ -2560,3 +2561,34 @@ def no_copy_shim():
else:
copy = False
return copy


def atoi(s: str) -> int:
"""Convert the leading number part of a string to an integer.

Parameters
----------
s : str
The string to convert to an integer.

Returns
-------
number : int
The first numeric part of the string converted to an integer.
If the string does not start with a number, 0 is returned.

Examples
--------
>>> from MDAnalysis.lib.util import atoi
>>> atoi('34f4')
34
>>> atoi('foo')
0


.. versionadded:: 2.8.0
"""
try:
return int(''.join(itertools.takewhile(str.isdigit, s.strip())))
except ValueError:
return 0
9 changes: 7 additions & 2 deletions package/MDAnalysis/topology/PSFParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from math import ceil
import numpy as np

from ..lib.util import openany
from ..lib.util import openany, atoi
from . import guessers
from .base import TopologyReaderBase, squash_by, change_squash
from ..core.topologyattrs import (
Expand Down Expand Up @@ -89,6 +89,10 @@ class PSFParser(TopologyReaderBase):
- impropers

.. _PSF: http://www.charmm.org/documentation/c35b1/struct.html


.. versionchanged:: 2.8.0
PSFParser now reads string resids and converts them to integers.
"""
format = 'PSF'

Expand Down Expand Up @@ -248,7 +252,8 @@ def _parseatoms(self, lines, atoms_per, numlines):
}
atom_parser = atom_parsers[self._format]
# once partitioned, assigned each component the correct type
set_type = lambda x: (int(x[0]) - 1, x[1] or "SYSTEM", int(x[2]), x[3],
set_type = lambda x: (int(x[0]) - 1, x[1] or "SYSTEM",
atoi(x[2]), x[3],
x[4], x[5], float(x[6]), float(x[7]))

# Oli: I don't think that this is the correct OUTPUT format:
Expand Down
Loading
Loading