Skip to content

Fix read_mol2 failing when the ATOM section ends the input - #171

Open
sukhmansaran wants to merge 1 commit into
BioPandas:mainfrom
sukhmansaran:mol2-atom-section-eof
Open

sukhmansaran wants to merge 1 commit into
BioPandas:mainfrom
sukhmansaran:mol2-atom-section-eof

Conversation

@sukhmansaran

Copy link
Copy Markdown

Code of Conduct

OK

Description

PandasMol2._get_atomsection() only sets last_idx_plus1 when 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:

from biopandas.mol2 import PandasMol2

lines = [
    "@<TRIPOS>MOLECULE\n", "sodium\n", "1 0 0 0 0\n", "SMALL\n", "USER_CHARGES\n", "\n",
    "@<TRIPOS>ATOM\n",
    "1 Na 0.0000 0.0000 0.0000 Na 1 NA 1.0000\n",
]
PandasMol2().read_mol2_from_list(lines, "sodium")
# UnboundLocalError: cannot access local variable 'last_idx_plus1' where it is not associated with a value

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 the ValueError for 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_input reads a molecule whose ATOM section is last via read_mol2_from_list, with and without a newline after the last atom.
  • test_read_mol2_file_atom_section_at_end_of_file reads a single-ion .mol2 file that ends with its ATOM section via read_mol2.

Both fail on main with the UnboundLocalError above 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

  • Added a note about the modification or contribution to the ./docs/sources/CHANGELOG.md file (if applicable) (added to docs/CHANGELOG.md, where the current "Unreleased" section is)
  • Added appropriate unit test functions in the ./biopandas/*/tests directories (if applicable) (in tests/mol2/)
  • Modify documentation in the corresponding Jupyter Notebook under biopandas/docs/sources/ (if applicable) (not needed, no API change)
  • Ran PYTHONPATH='.' pytest ./biopandas -sv and make sure that all unit tests pass (full tests/ suite: 129 passed, 7 skipped, 13 xfailed with pandas 3.1.1; tests/mol2 also passes with pandas 2.3.3. tests/pdb/test_read_pdb.py was deselected locally because it needs network access to RCSB.)
  • Checked for style issues by running flake8 ./biopandas (no warnings on the changed files)

🤖 Generated with Claude Code

_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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant