11"""Heteroatom-starting recursive substituent naming."""
22
3+ from .assembly_parts import split_rendered_substituent_name
34from .formatting import (
45 count_names ,
56 format_counted_prefixes ,
2627from .rules import multipliers
2728
2829
30+ def _branch_name_text (
31+ branch_namer : RecursiveSubgraphNamer ,
32+ mol : Molecule ,
33+ start_idx : int ,
34+ exclude_atoms : set [int ],
35+ upstream_atom : int ,
36+ ) -> str :
37+ """Render a nested branch as text, consuming its boundary metadata."""
38+
39+ rendered = branch_namer (
40+ mol ,
41+ start_idx ,
42+ exclude_atoms ,
43+ upstream_atom = upstream_atom ,
44+ )
45+ name , _ = split_rendered_substituent_name (rendered )
46+ return name
47+
48+
2949def upstream_bond_order (mol : Molecule , start_idx : int , upstream_atom : int | None ) -> int :
3050 if upstream_atom is None :
3151 return 0
@@ -152,7 +172,7 @@ def name_branch_or_none(
152172) -> str :
153173 if branch_idx is None :
154174 return ""
155- return strip_outer_parentheses (branch_namer ( mol , branch_idx , exclude_atoms , upstream_atom ))
175+ return strip_outer_parentheses (_branch_name_text ( branch_namer , mol , branch_idx , exclude_atoms , upstream_atom ))
156176
157177
158178def name_carbonyl_like_fragment (
@@ -331,7 +351,7 @@ def name_oxygen_subgraph(
331351 branch = name_branch_or_none (mol , branch_idx , exclude_atoms | {start_idx , nxt }, nxt , branch_namer )
332352 return f"({ branch } peroxy)" if branch else "hydroperoxy"
333353
334- branch = branch_namer ( mol , nxt , exclude_atoms | {start_idx }, start_idx )
354+ branch = _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx }, start_idx )
335355 if branch :
336356 return oxy_prefix_from_branch (branch )
337357 return "hydroxy"
@@ -406,7 +426,7 @@ def name_nitrogen_subgraph(
406426 if sulfur_imide :
407427 branches .append (sulfur_imide )
408428 else :
409- branch = branch_namer ( mol , nxt , exclude_atoms | {start_idx }, start_idx )
429+ branch = _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx }, start_idx )
410430 if branch :
411431 branches .append (branch )
412432
@@ -446,7 +466,7 @@ def _sulfur_imide_branch_name(
446466 cyclic_name = _cyclic_sulfur_imide_ligand_name (mol , sulfur , nitrogen , next_atoms , s_oxygens )
447467 if cyclic_name :
448468 return cyclic_name
449- branches = [br for nxt in next_atoms if (br := branch_namer ( mol , nxt , local_exclude , sulfur ))]
469+ branches = [br for nxt in next_atoms if (br := _branch_name_text ( branch_namer , mol , nxt , local_exclude , sulfur ))]
450470 branches .extend (["oxo" ] * len (s_oxygens ))
451471 if not branches :
452472 return "sulfanylidene"
@@ -568,7 +588,7 @@ def name_sulfur_subgraph(
568588 br
569589 for nxt in next_atoms
570590 if (
571- br := branch_namer ( mol , nxt , exclude_atoms | {start_idx } | set (s_oxygens ) | set (s_nitrogens ), start_idx )
591+ br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx } | set (s_oxygens ) | set (s_nitrogens ), start_idx )
572592 )
573593 ]
574594 branches .extend (["oxo" ] * len (s_oxygens ))
@@ -589,7 +609,7 @@ def name_sulfur_subgraph(
589609 branches = [
590610 br
591611 for nxt in next_atoms
592- if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx } | set (s_oxygens ), start_idx ))
612+ if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx } | set (s_oxygens ), start_idx ))
593613 ]
594614 branches .extend (["oxo" ] * len (s_oxygens ))
595615 return format_lambda_substituent (
@@ -603,7 +623,7 @@ def name_sulfur_subgraph(
603623 branches = [
604624 br
605625 for nxt in next_atoms
606- if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx } | set (s_nitrogens ), start_idx ))
626+ if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx } | set (s_nitrogens ), start_idx ))
607627 ]
608628 if len (s_nitrogens ) == 1 :
609629 branches = ["imino" , * branches ]
@@ -619,7 +639,7 @@ def name_sulfur_subgraph(
619639 return "thioxo" if is_double else f"{ stereo_prefix_text } { unsubstituted_prefix ('S' ) or 'sulfanyl' } "
620640
621641 if len (next_atoms ) == 1 :
622- branch = branch_namer ( mol , next_atoms [0 ], exclude_atoms | {start_idx }, start_idx )
642+ branch = _branch_name_text ( branch_namer , mol , next_atoms [0 ], exclude_atoms | {start_idx }, start_idx )
623643 if not is_double and mol .atoms [start_idx ].charge > 0 :
624644 return f"({ stereo_prefix_text } { branch } sulfaniumyl)" if branch else f"{ stereo_prefix_text } sulfaniumyl"
625645 if branch in SIMPLE_SULFANYL_PREFIXES :
@@ -628,7 +648,7 @@ def name_sulfur_subgraph(
628648 return format_element_substituent (stereo_prefix_text , branch , "sulfanyl" , is_double = is_double )
629649 return f"{ stereo_prefix_text } { 'sulfanylidene' if is_double else 'sulfanyl' } "
630650
631- branches = [br for nxt in next_atoms if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
651+ branches = [br for nxt in next_atoms if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
632652 return format_lambda_substituent (
633653 mol , start_idx , branches , stereo_prefix_text , "sulfanylidene" if is_double else "sulfanyl"
634654 )
@@ -659,7 +679,7 @@ def name_chalcogen_subgraph(
659679 branches = [
660680 br
661681 for nxt in next_atoms
662- if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx } | set (oxo_ligands ), start_idx ))
682+ if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx } | set (oxo_ligands ), start_idx ))
663683 ]
664684 branches .extend (["oxo" ] * len (oxo_ligands ))
665685 return format_lambda_substituent (
@@ -676,7 +696,7 @@ def name_chalcogen_subgraph(
676696 else f"{ stereo_prefix_text } { unsubstituted_prefix (mol .atoms [start_idx ].symbol ) or element_suffix } "
677697 )
678698 if len (next_atoms ) == 1 :
679- branch = branch_namer ( mol , next_atoms [0 ], exclude_atoms | {start_idx }, start_idx )
699+ branch = _branch_name_text ( branch_namer , mol , next_atoms [0 ], exclude_atoms | {start_idx }, start_idx )
680700 if branch and substituent_bonding_number (mol , start_idx ) != mol .atoms [start_idx ].element .standard_valence :
681701 return format_lambda_substituent (
682702 mol , start_idx , [branch ], stereo_prefix_text , element_suffix + ("idene" if is_double else "" )
@@ -686,7 +706,7 @@ def name_chalcogen_subgraph(
686706 if branch :
687707 return format_element_substituent (stereo_prefix_text , branch , element_suffix , is_double = is_double )
688708 return f"{ stereo_prefix_text } { element_suffix + ('idene' if is_double else '' )} "
689- branches = [br for nxt in next_atoms if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
709+ branches = [br for nxt in next_atoms if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
690710 return format_lambda_substituent (
691711 mol , start_idx , branches , stereo_prefix_text , element_suffix + ("idene" if is_double else "" )
692712 )
@@ -714,7 +734,7 @@ def name_phosphorus_subgraph(
714734 branches = [
715735 br
716736 for nxt in next_atoms
717- if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx } | set (p_oxygens ), start_idx ))
737+ if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx } | set (p_oxygens ), start_idx ))
718738 ]
719739 return f"({ stereo_prefix_text } { format_counted_prefixes (branches )} { suffix } )"
720740
@@ -734,7 +754,7 @@ def name_group_13_14_subgraph(
734754 next_atoms = subgraph_neighbors (mol , start_idx , exclude_atoms , upstream_atom )
735755 if not next_atoms :
736756 return suffix
737- branches = [br for nxt in next_atoms if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
757+ branches = [br for nxt in next_atoms if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
738758 return f"({ format_counted_prefixes (branches )} { suffix } )"
739759
740760
@@ -749,7 +769,7 @@ def name_halogen_subgraph(
749769 next_atoms = subgraph_neighbors (mol , start_idx , exclude_atoms , upstream_atom )
750770 if not next_atoms :
751771 return HALOGEN_PREFIXES [symbol ]
752- branches = [br for nxt in next_atoms if (br := branch_namer ( mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
772+ branches = [br for nxt in next_atoms if (br := _branch_name_text ( branch_namer , mol , nxt , exclude_atoms | {start_idx }, start_idx ))]
753773 valence = sum (mol .get_bond (start_idx , n ).order for n in mol .get_neighbors (start_idx ))
754774 return f"({ format_counted_prefixes (branches )} lambda^{ valence } -{ HALOGEN_LAMBDA_SUFFIXES [symbol ]} )"
755775
0 commit comments