Skip to content

Commit

Permalink
modified guess_atom_element for more accurate guess (#4168)
Browse files Browse the repository at this point in the history
* modified guess_atom_element for more accurate guess
Co-authored-by: Rocco Meli <[email protected]>
Co-authored-by: pillose <[email protected]>
  • Loading branch information
pillose authored Aug 16, 2023
1 parent a14e98d commit acfd122
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Chronological list of authors
- Mohit Kumar
- Shubham Kumar
- Zaheer Timol
- Geongi Moon

External code
-------------
Expand Down
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
??/??/?? IAlibay, hmacdope
??/??/?? IAlibay, hmacdope, pillose

* 2.7.0

Fixes
* Fix Atom type guessing error (PR #4168, Issue #4167)

Enhancements

Expand Down
9 changes: 7 additions & 2 deletions package/MDAnalysis/topology/guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ def guess_atom_element(atomname):
try:
return tables.atomelements[atomname.upper()]
except KeyError:
# strip symbols and numbers
# strip symbols
no_symbols = re.sub(SYMBOLS, '', atomname)
name = re.sub(NUMBERS, '', no_symbols).upper()

# split name by numbers
no_numbers = re.split(NUMBERS, no_symbols)
no_numbers = list(filter(None, no_numbers)) #remove ''
# if no_numbers is not empty, use the first element of no_numbers
name = no_numbers[0].upper() if no_numbers else ''

# just in case
if name in tables.atomelements:
Expand Down
5 changes: 5 additions & 0 deletions testsuite/MDAnalysisTests/topology/test_guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ def test_guess_atom_element_1H(self):
('zn', 'ZN'),
('Ca2+', 'CA'),
('CA', 'C'),
('N0A', 'N'),
('C0U', 'C'),
('C0S', 'C'),
('Na+', 'NA'),
('Cu2+', 'CU')
))
def test_guess_element_from_name(self, name, element):
assert guessers.guess_atom_element(name) == element
Expand Down

0 comments on commit acfd122

Please sign in to comment.