diff --git a/arc/reaction.py b/arc/reaction.py index 09e9dfbe42..9ad7a019d5 100644 --- a/arc/reaction.py +++ b/arc/reaction.py @@ -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]: """ diff --git a/arc/reaction_test.py b/arc/reaction_test.py index 93753ac62e..8636a555e7 100644 --- a/arc/reaction_test.py +++ b/arc/reaction_test.py @@ -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."""