Skip to content

Commit

Permalink
To rxn smiles (#729)
Browse files Browse the repository at this point in the history
Added a feature to turn get a reaction SMILES from an ARCReaction.
  • Loading branch information
kfir4444 authored Feb 11, 2024
2 parents 218bde2 + 0f65858 commit 7579da2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions arc/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,25 @@ def copy_e0_values(self, other_rxn: Optional['ARCReaction']):
if spc.label == other_spc.label:
spc.e0 = spc.e0 or other_spc.e0

def get_rxn_smiles(self) -> Optional[str]:
"""
returns the reaction smiles of the reaction.
Raises:
ValueError: If any of the species (reactants or products) has no SMILES (or could not be generated for some reason).
Returns: string
The reaction SMILES
"""
reactants, products = self.get_reactants_and_products(arc=True, return_copies=True)
smiles_r = [reactant.mol.copy(deep=True).to_smiles() for reactant in reactants]
smiles_p = [product.mol.copy(deep=True).to_smiles() for product in products]
if not any(smiles_r) or not any(smiles_p):
raise ValueError(f"""Could not find smiles for one or more species
got: reactants: {smiles_r}
products: {smiles_p}""")
return ".".join(smiles_r)+">>"+".".join(smiles_p)


def remove_dup_species(species_list: List[ARCSpecies]) -> List[ARCSpecies]:
"""
Expand Down
14 changes: 14 additions & 0 deletions arc/reaction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,20 @@ def test_load_ts_xyz_user_guess_from_files(self):
self.assertEqual(len(arc_object.reactions[0].ts_species.ts_guesses), 2)
self.assertEqual(len(arc_object.reactions[0].ts_species.ts_guesses[1].initial_xyz['symbols']), 19)

def test_get_rxn_smiles(self):
"""Tests the get_rxn_smiles method"""
self.assertEqual(self.rxn1.get_rxn_smiles(), "C.[OH]>>[CH3].O")
self.assertEqual(self.rxn2.get_rxn_smiles(), "C[CH2].[OH]>>C=C.O")
self.assertEqual(self.rxn3.get_rxn_smiles(), "CC[NH]>>[CH2]CN")
self.assertEqual(self.rxn4.get_rxn_smiles(), "[NH2].[NH]N>>N.[N]N")
self.assertEqual(self.rxn5.get_rxn_smiles(), "[NH2].[NH2]>>[NH].N")
self.assertEqual(self.rxn6.get_rxn_smiles(), "[NH2].[NH]N>>N.[N-]=[NH2+]")
self.assertEqual(self.rxn7.get_rxn_smiles(), "[NH2].[NH]N>>N.[N]N")
self.assertEqual(self.rxn8.get_rxn_smiles(), "C.[OH]>>[CH3].O")
self.assertEqual(self.rxn9.get_rxn_smiles(), "NCO[O]>>C=N.[O]O")
self.assertEqual(self.rxn10.get_rxn_smiles(), "N=O.[O-][N+]=O>>[O-][NH+]=O.[N]=O")
self.assertEqual(self.rxn11.get_rxn_smiles(), "C[CH]C>>[CH2]CC")

@classmethod
def tearDownClass(cls):
"""A function that is run ONCE after all unit tests in this class."""
Expand Down

0 comments on commit 7579da2

Please sign in to comment.