Skip to content

Commit 23ed062

Browse files
committed
Raise ValueError if trying to match a template with wrong number of reactants.
In a regular RMG run this would never happen, but sometimes other tools could be, for example, trying to see if a reaction matches a certain family, and they might ask "can you match this template?", so it's helpful to return a sensible error that they can handle.
1 parent 0be289b commit 23ed062

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

rmgpy/data/kinetics/family.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2710,12 +2710,16 @@ def get_labeled_reactants_and_products(self, reactants, products, relabel_atoms=
27102710
template_reactants = [x.item for x in template.reactants]
27112711

27122712
if len(reactants0) == 1:
2713+
if len(template_reactants) != 1:
2714+
raise ValueError(f"Reaction has {len(reactants0)} reactants but template has {len(template_reactants)} reactants.")
27132715
molecule = reactants0[0]
27142716
mappings = self._match_reactant_to_template(molecule, template_reactants[0])
27152717
mappings = [[map0] for map0 in mappings]
27162718
num_mappings = len(mappings)
27172719
reactant_structures = [molecule]
27182720
elif len(reactants0) == 2:
2721+
if len(template_reactants) != 2:
2722+
raise ValueError(f"Reaction has {len(reactants0)} reactants but template has {len(template_reactants)} reactants.")
27192723
molecule_a = reactants0[0]
27202724
molecule_b = reactants0[1]
27212725
# get mappings in forward direction
@@ -2730,6 +2734,8 @@ def get_labeled_reactants_and_products(self, reactants, products, relabel_atoms=
27302734
reactant_structures = [molecule_a, molecule_b]
27312735
num_mappings = len(mappings_a) * len(mappings_b)
27322736
elif len(reactants0) == 3:
2737+
if len(template_reactants) != 3:
2738+
raise ValueError(f"Reaction has {len(reactants0)} reactants but template has {len(template_reactants)} reactants.")
27332739
molecule_a = reactants0[0]
27342740
molecule_b = reactants0[1]
27352741
molecule_c = reactants0[2]
@@ -2744,7 +2750,7 @@ def get_labeled_reactants_and_products(self, reactants, products, relabel_atoms=
27442750
reactant_structures = [molecule_a, molecule_b, molecule_c]
27452751
num_mappings = len(mappings_a) * len(mappings_b) * len(mappings_c)
27462752
else:
2747-
raise IndexError('You have {0} reactants, which is unexpected!'.format(len(reactants)))
2753+
raise NotImplementedError('You have {0} reactants, which is unexpected!'.format(len(reactants)))
27482754

27492755
for mapping in mappings:
27502756
try:

0 commit comments

Comments
 (0)