Skip to content

feat: add rxn helpers #1

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

feat: add rxn helpers #1

wants to merge 11 commits into from

Conversation

MrtinoRG
Copy link
Collaborator

@MrtinoRG MrtinoRG commented Mar 10, 2025

Summary by Sourcery

Add reaction-related utility functions and image processing capabilities to the chemenv project, introducing new tools for molecule and reaction schema extraction, mapping, and SMILES manipulation.

New Features:

  • Add molecule image extraction using DECIMER and MolScribe models
  • Implement reaction schema extraction with RxnScribe
  • Introduce reaction SMILES mapping and canonicalization utilities

Enhancements:

  • Extend chemenv toolset with reaction processing capabilities
  • Add support for extracting functional groups using Exomol

Copy link

sourcery-ai bot commented Mar 10, 2025

Reviewer's Guide by Sourcery

This pull request introduces new functionalities to the chemenv modal app, focusing on reaction SMILES manipulation and atom mapping. It includes functions for unifying and canonicalizing reaction SMILES, obtaining atom mappings using RxnMapper, and extracting functional groups using Exomol. The changes involve adding new Modal apps and functions, integrating external tools like DECIMER, MolScribe, RxnScribe, and RxnMapper, and defining new images for these tools.

Sequence diagram for get_rxn_mapping

sequenceDiagram
    participant User
    participant chemenv_modal_app
    participant rxn_mapper_image

    User->>chemenv_modal_app: get_rxn_mapping(rxns: list[str])
    chemenv_modal_app->>rxn_mapper_image: _get_rxn_mapper_confidence(rxns)
    rxn_mapper_image-->>chemenv_modal_app: list[dict] (atom mappings)
    chemenv_modal_app-->>User: list[dict] (atom mappings)
Loading

Sequence diagram for unify_rxn_smiles

sequenceDiagram
    participant User
    participant chemenv_modal_app
    participant rxn_utils_image

    User->>chemenv_modal_app: unify_rxn_smiles(rxn_smiles: str)
    chemenv_modal_app->>rxn_utils_image: _unify_rxn_smiles(rxn_smiles)
    rxn_utils_image-->>chemenv_modal_app: str (unified SMILES)
    chemenv_modal_app-->>User: str (unified SMILES)
Loading

Updated class diagram for rxn_schema_processing

classDiagram
  class rxn_schema_processing {
    <<image>> decimer_image
    <<image>> rxnscribe_image
    + _decimer_extractor(image: bytes) : str
    + _molscribe_extractor(image: bytes) : dict[str, Any]
    + _rxnscribe_extractor(image: bytes) : list[dict]
  }
Loading

File-Level Changes

Change Details Files
Introduced new Modal apps and functions for reaction SMILES manipulation and atom mapping.
  • Added a new Modal app named rxnenv.
  • Added functions for unifying and canonicalizing reaction SMILES.
  • Added a function to obtain atom mappings for reactions using RxnMapper.
  • Integrated functions into the main chemenv app.
  • Added a function to get functional groups of a molecule using Exomol.
src/chemenv/modal_app.py
Integrated external tools for image-based molecule and reaction extraction.
  • Added functions to extract molecules from images using DECIMER and MolScribe.
  • Added a function to extract reaction schemas from images using RxnScribe.
src/chemenv/modal_app.py
src/chemenv/tools/rxn_schema_processing.py
Defined new Modal images for external tools.
  • Created images for DECIMER, MolScribe, RxnScribe, RxnMapper, and rxn-chem-utils.
  • Configured GPU usage and volume mounts for MolScribe and RxnScribe images.
  • Installed necessary dependencies for each tool within its respective image.
src/chemenv/tools/util_tool.py
src/chemenv/tools/rxn_schema_processing.py
src/chemenv/tools/rxn_utils.py
Added utility functions for reaction SMILES processing and functional group detection.
  • Implemented functions to unify and canonicalize reaction SMILES using rxn-chem-utils.
  • Implemented a function to obtain atom mappings and confidence scores using RxnMapper.
  • Implemented a function to retrieve functional groups of a molecule using Exomol.
src/chemenv/modal_app.py
src/chemenv/tools/util_tool.py
src/chemenv/tools/rxn_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@MrtinoRG MrtinoRG marked this pull request as ready for review April 2, 2025 17:42
@MrtinoRG MrtinoRG requested review from kjappelbaum and Copilot April 2, 2025 17:43
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds new helper functions for reaction SMILES processing and extraction of chemical data using RxnMapper, Exomol, DECIMER, MolScribe, and RxnScribe.

  • Introduces functions to unify, canonicalize, and map reaction SMILES.
  • Adds functionality to extract functional groups and predict molecular/reaction data from images.
  • Integrates the new functions into the modal app with dedicated endpoints and volumes.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/chemenv/tools/util_tool.py Updated image initialization and added Exomol helper.
src/chemenv/tools/rxn_utils.py Added functions for RxnMapper confidence, and SMILES helpers.
src/chemenv/tools/rxn_schema_processing.py Added functions for reaction schema extraction using DECIMER, MolScribe, and RxnScribe.
src/chemenv/modal_app.py Integrated new extraction and reaction mapping endpoints into the app.
Comments suppressed due to low confidence (1)

src/chemenv/tools/rxn_schema_processing.py:4

  • For consistency with other pip_install calls, consider passing the package name as a list (e.g., ["decimer"]) to avoid potential type issues.
decimer_image = Image.debian_slim(python_version="3.10.0").pip_install("decimer")

from rxn.chemutils.miscellaneous import canonicalize_any


def _get_rxn_mapper_confidence(rxns: list[str]) -> list[dict]:
Copy link
Preview

Copilot AI Apr 2, 2025

Choose a reason for hiding this comment

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

The function's docstring is inconsistent: it describes a single reaction string and a float return value, whereas the signature indicates it accepts a list of reaction strings and returns a list of dictionaries. Please update the docstring to accurately reflect the parameter type (list[str]) and return type (list[dict]).

Copilot uses AI. Check for mistakes.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @MrtinoRG - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a unit test suite to verify the functionality of the newly added functions.
  • It would be helpful to add docstrings to the internal helper functions (those starting with an underscore).
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

for result in results:
for key, value in result.items():
for v in value:
if "molfile" in v:
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the nested loops in _rxnscribe_extractor to use a helper function for removing 'molfile', reducing complexity and improving readability

You can reduce the three levels of nesting by extracting the "molfile" removal into a small helper that operates on each result. For example, instead of:

for result in results:
    for key, value in result.items():
        for v in value:
            if "molfile" in v:
                v.pop("molfile")

you might define a helper function:

def remove_molfile_from_list(dicts: list[dict], key: str = "molfile") -> None:
    for d in dicts:
        d.pop(key, None)

and then simplify the loop in _rxnscribe_extractor to:

for result in results:
    for value in result.values():
        remove_molfile_from_list(value)

This refactoring preserves functionality and makes the code clearer and easier to maintain.

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