Skip to content

Commit

Permalink
PSFParser reads and converts string-Like residue number to the leadin…
Browse files Browse the repository at this point in the history
…g number (#4582)

* psf support string like resid

* add psf file with insertion code

* add changelog

* update doc and pep8

* add test for psf
  • Loading branch information
yuxuanzhuang committed May 31, 2024
1 parent 81062f3 commit 347a0c0
Show file tree
Hide file tree
Showing 6 changed files with 2,138 additions and 3 deletions.
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 `MDAnalysis.analysis.align.AlignTraj` not accepting writer kwargs
(Issue #4564, PR #4565)
* Fix #4259 via removing argument `parallelizable` of `NoJump` transformation.
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

0 comments on commit 347a0c0

Please sign in to comment.