Skip to content

Only cast mmCIF columns that are in the file - #169

Open
TheeCoderAhmed wants to merge 1 commit into
BioPandas:mainfrom
TheeCoderAhmed:fix/mmcif-missing-columns
Open

TheeCoderAhmed wants to merge 1 commit into
BioPandas:mainfrom
TheeCoderAhmed:fix/mmcif-missing-columns

Conversation

@TheeCoderAhmed

Copy link
Copy Markdown

Description

PandasMmcif._construct_df() casts the _atom_site columns with full_df.astype(mmcif_col_types, errors="ignore"). errors="ignore" only covers values that fail to convert; pandas still raises a KeyError for any key in the dtype dict that isn't a column. So any mmCIF file whose _atom_site loop lacks one of the columns in mmcif_col_types can't be read, which is what #168 hits with auth_atom_id:

KeyError: "Only a column name can be used for the key in a dtype mappings argument. 'auth_atom_id' not found in columns."

This builds the dtype dict from the columns that are actually present before calling astype(). Files that have every column are cast exactly as before.

Added test_read_mmcif_without_auth_atom_id to tests/mmcif/test_read_mmcif.py, which reads a two-atom mmCIF without auth_atom_id (and a few other optional columns). It fails with the KeyError without the change and passes with it. The full test suite passes.

Related issues or pull requests

Fixes #168

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)
  • Modify documentation in the corresponding Jupyter Notebook under biopandas/docs/sources/ (if applicable)
  • Ran PYTHONPATH='.' pytest ./biopandas -sv and make sure that all unit tests pass
  • Checked for style issues by running flake8 ./biopandas (no new warnings on the changed lines)

DataFrame.astype() raises a KeyError for dtype keys that are not columns, even with errors="ignore". mmCIF files whose atom_site loop lacks one of the columns in mmcif_col_types (for example auth_atom_id) could not be read.

Fixes BioPandas#168

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.

mmCIF parsing fails with KeyError when _atom_site.auth_atom_id field is absent

1 participant