Repository navigation
Fix read_mol2 failing when the ATOM section ends the input - #171
Open
sukhmansaran wants to merge 1 commit into
Open
sukhmansaran wants to merge 1 commit into
sukhmansaran wants to merge 1 commit into
Conversation
_get_atomsection only set last_idx_plus1 when a blank line or another @<TRIPOS> record followed the ATOM section, so a molecule whose ATOM section is last (e.g. a single ion without bonds) failed with UnboundLocalError. The section now runs to the end of the input in that case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code of Conduct
OK
Description
PandasMol2._get_atomsection()only setslast_idx_plus1when it finds a blank line or another@<TRIPOS>record after the ATOM section. If the ATOM section is the last thing in the input, that variable is never set, and reading the molecule fails:read_mol2()hits the same error for a file that ends with its ATOM section, with or without a trailing newline. This happens for molecules without bonds (e.g. a single ion), where some tools write no BOND section.The fix sets
last_idx_plus1 = len(mol2_lst)before the loop, so in that case the ATOM section runs to the end of the input. When a blank line or@<TRIPOS>record follows the section, it still ends there as before, and theValueErrorfor input without an ATOM section is unchanged.This is a different case from #136, which was fixed by #140 (blank lines between sections).
Tests (
tests/mol2/test_pandas_mol2.py):test_read_mol2_atom_section_at_end_of_inputreads a molecule whose ATOM section is last viaread_mol2_from_list, with and without a newline after the last atom.test_read_mol2_file_atom_section_at_end_of_filereads a single-ion.mol2file that ends with its ATOM section viaread_mol2.Both fail on
mainwith theUnboundLocalErrorabove and pass with this change.Related issues or pull requests
Related to #170 (Mol2 BOND parsing). The two PRs touch different parts of
pandas_mol2.py, the tests file and the changelog, and merge with each other without conflicts.Pull Request Checklist
./docs/sources/CHANGELOG.mdfile (if applicable) (added todocs/CHANGELOG.md, where the current "Unreleased" section is)./biopandas/*/testsdirectories (if applicable) (intests/mol2/)biopandas/docs/sources/(if applicable) (not needed, no API change)PYTHONPATH='.' pytest ./biopandas -svand make sure that all unit tests pass (fulltests/suite: 129 passed, 7 skipped, 13 xfailed with pandas 3.1.1;tests/mol2also passes with pandas 2.3.3.tests/pdb/test_read_pdb.pywas deselected locally because it needs network access to RCSB.)flake8 ./biopandas(no warnings on the changed files)🤖 Generated with Claude Code