Repository navigation
Mol2 parse bonds - #170
Open
sukhmansaran wants to merge 2 commits into
Open
Mol2 parse bonds#170sukhmansaran wants to merge 2 commits into
sukhmansaran wants to merge 2 commits into
Conversation
Parses the BOND section into a DataFrame (bond_id, origin_atom_id, target_atom_id, bond_type, status_bits), also stored in PandasMol2.df_bonds. Bonds are parsed on request only, so read_mol2 is unchanged. Fixes BioPandas#54. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 of 5 tasks
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
PandasMol2only parsed the ATOM section, so getting the bonds of a molecule meant parsingpmol.mol2_textby hand. This PR adds BOND section parsing, following the API sketched in #54:Design choices:
read_mol2is unchanged and doesn't parse bonds, so nothing gets slower for existing users.df_bondsisNoneuntilparse_bonds()is called. It is reset toNonewhen a new molecule is read into the same object, so it can't hold bonds from the previous molecule. Likedf, it can't be assigned to directly: it raises anAttributeErrorthat points to_df_bonds.bond_id,origin_atom_idandtarget_atom_id(int64), plusbond_typeandstatus_bits(str).bond_typestays a string because Tripos bond types includeam,ar,du,unandnc; the test files contain bothamandarbonds.status_bitsis optional in the format and is the empty string when a bond doesn't specify it.@<TRIPOS>record or at the end of the file. Blank and#comment lines are skipped. A molecule without a BOND section (e.g. a single ion) gives an empty DataFrame with the same columns and dtypes. A record with fewer than 4 fields or a non-integer id raises aValueErrorthat quotes the line.mol2_text, so it behaves the same forread_mol2,read_mol2_from_listand gzipped files.Tests (
tests/mol2/test_pandas_mol2.py):test_parse_bondschecks the columns, dtypes, values and bond types for1b5e_1.mol2, and that every bond refers to an atom indf.test_parse_bonds_matches_header_countschecks, for all 83 molecules in the test data (40_mol2_files.mol2, its.gz,1b5e_2.mol2, and the CRLF fileempty_line.mol2), that the number of parsed bonds equals the bond count in the MOLECULE header.df_bonds.All 7 new tests fail on
main(AttributeError: 'PandasMol2' object has no attribute 'parse_bonds') and pass with this change. I also added a "Parsing the BOND Section" section to the Mol2 tutorial (notebook and the generated.md), with outputs produced by running the code.Related issues or pull requests
Fixes #54
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) (docs/tutorials/Working_with_MOL2_Structures_in_DataFrames.ipynband.md)PYTHONPATH='.' pytest ./biopandas -svand make sure that all unit tests pass (fulltests/suite: 134 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