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

modified guess_atom_element for more accurate guess #4168

Merged
merged 19 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
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
??/??/?? IAlibay, 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 = list(filter(None, no_numbers))[0].upper() if no_numbers else ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if I'm missing something obvious, but do you need to apply filter again here? You've already removed the '' entries and you shouldn't have any None entries ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think I made a mistake while modifying the code...


# 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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more nit sorry could you test Na+ -> Na?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added two tests for Na+ and Cu2+

('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
Loading