"""
Functionality to handle different kinds of reaction SMILES with the same functions.
In a separate file than miscellaneous.py or conversion.py in order to avoid
cyclic dependencies.
"""
from enum import auto
from rxn.utilities.types import RxnEnum
from .extended_reaction_smiles import (
parse_extended_reaction_smiles,
to_extended_reaction_smiles,
)
from .reaction_equation import ReactionEquation
[docs]def parse_any_reaction_smiles(smiles: str) -> ReactionEquation:
"""
Parse a reaction SMILES in any format (will be determined automatically).
"""
return parse_reaction_smiles(smiles, reaction_format=determine_format(smiles))
[docs]def parse_reaction_smiles(
smiles: str, reaction_format: ReactionFormat
) -> ReactionEquation:
"""
Parse the reaction SMILES in a given format.
"""
if reaction_format is ReactionFormat.EXTENDED:
return parse_extended_reaction_smiles(smiles, remove_atom_maps=False)
if reaction_format is ReactionFormat.STANDARD:
return ReactionEquation.from_string(smiles)
if reaction_format is ReactionFormat.STANDARD_WITH_TILDE:
return ReactionEquation.from_string(smiles, fragment_bond="~")
raise ValueError(f"Unsupported reaction format: {reaction_format}")
[docs]def to_reaction_smiles(
reaction_equation: ReactionEquation, reaction_format: ReactionFormat
) -> str:
"""
Convert a reaction equation into a reaction SMILES of the specified format.
"""
if reaction_format is ReactionFormat.EXTENDED:
return to_extended_reaction_smiles(reaction_equation)
if reaction_format is ReactionFormat.STANDARD:
return reaction_equation.to_string()
if reaction_format is ReactionFormat.STANDARD_WITH_TILDE:
return reaction_equation.to_string(fragment_bond="~")
raise ValueError(f"Unsupported reaction format: {reaction_format}")