Skip to content

Commit 347a0c0

Browse files
authored
PSFParser reads and converts string-Like residue number to the leading number (#4582)
* psf support string like resid * add psf file with insertion code * add changelog * update doc and pep8 * add test for psf
1 parent 81062f3 commit 347a0c0

File tree

6 files changed

+2138
-3
lines changed

6 files changed

+2138
-3
lines changed

package/CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ The rules for this file:
1616
-------------------------------------------------------------------------------
1717
??/??/?? IAlibay, HeetVekariya, marinegor, lilyminium, RMeli,
1818
ljwoods2, aditya292002, pstaerk, PicoCentauri, BFedder,
19-
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, orionarcher
19+
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, orionarcher,
20+
yuxuanzhuang
2021

2122
* 2.8.0
2223

2324
Fixes
25+
* Fix PSFParser error when encoutering string-like resids
26+
* (Issue #2053, Issue #4189 PR #4582)
2427
* Fix `MDAnalysis.analysis.align.AlignTraj` not accepting writer kwargs
2528
(Issue #4564, PR #4565)
2629
* Fix #4259 via removing argument `parallelizable` of `NoJump` transformation.

package/MDAnalysis/lib/util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
from functools import wraps
206206
import textwrap
207207
import weakref
208+
import itertools
208209

209210
import mmtf
210211
import numpy as np
@@ -2560,3 +2561,34 @@ def no_copy_shim():
25602561
else:
25612562
copy = False
25622563
return copy
2564+
2565+
2566+
def atoi(s: str) -> int:
2567+
"""Convert the leading number part of a string to an integer.
2568+
2569+
Parameters
2570+
----------
2571+
s : str
2572+
The string to convert to an integer.
2573+
2574+
Returns
2575+
-------
2576+
number : int
2577+
The first numeric part of the string converted to an integer.
2578+
If the string does not start with a number, 0 is returned.
2579+
2580+
Examples
2581+
--------
2582+
>>> from MDAnalysis.lib.util import atoi
2583+
>>> atoi('34f4')
2584+
34
2585+
>>> atoi('foo')
2586+
0
2587+
2588+
2589+
.. versionadded:: 2.8.0
2590+
"""
2591+
try:
2592+
return int(''.join(itertools.takewhile(str.isdigit, s.strip())))
2593+
except ValueError:
2594+
return 0

package/MDAnalysis/topology/PSFParser.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from math import ceil
4949
import numpy as np
5050

51-
from ..lib.util import openany
51+
from ..lib.util import openany, atoi
5252
from . import guessers
5353
from .base import TopologyReaderBase, squash_by, change_squash
5454
from ..core.topologyattrs import (
@@ -89,6 +89,10 @@ class PSFParser(TopologyReaderBase):
8989
- impropers
9090
9191
.. _PSF: http://www.charmm.org/documentation/c35b1/struct.html
92+
93+
94+
.. versionchanged:: 2.8.0
95+
PSFParser now reads string resids and converts them to integers.
9296
"""
9397
format = 'PSF'
9498

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

254259
# Oli: I don't think that this is the correct OUTPUT format:

0 commit comments

Comments
 (0)