|
4 | 4 | from rdkit.Chem import AllChem, Draw |
5 | 5 | from .utils import (Substitution, requires_config, |
6 | 6 | sequential_palette, get_auto_palette) |
| 7 | +from .highlight import Highlight |
7 | 8 | try: |
8 | 9 | from IPython.display import display_svg, display_html, Javascript |
9 | 10 | from ipywidgets import ColorPicker |
@@ -73,15 +74,41 @@ def configure(self, size=(-1, -1), black_font=True, fill_rings=None, |
73 | 74 | if self.label and any(h.text is None for h in self.highlights): |
74 | 75 | raise ValueError("Cannot use empty highlight text if label is set") |
75 | 76 |
|
| 77 | + # extend highlights if indices is nested list |
| 78 | + extended = [] |
| 79 | + for h in self.highlights: |
| 80 | + if isinstance(h.indices[0], (list, tuple)) and not h.same_color: |
| 81 | + ext = [Highlight(ix, h.text, h.color, h.fill_ring) |
| 82 | + for ix in h.indices] |
| 83 | + extended.extend(ext) |
| 84 | + else: |
| 85 | + extended.append(h) |
| 86 | + self.highlights = extended |
| 87 | + |
76 | 88 | # automatic colors if not set |
77 | | - if all(h.color is None for h in self.highlights): |
| 89 | + if any(h.color is None for h in self.highlights): |
78 | 90 | num_hl = len(self.highlights) |
79 | 91 | if num_hl > 5: |
80 | 92 | palette = get_auto_palette(num_hl) |
81 | 93 | else: |
82 | 94 | palette = sequential_palette |
83 | | - for highlight, color in zip(self.highlights, palette): |
84 | | - highlight.color = color |
| 95 | + for h, color in zip(self.highlights, palette): |
| 96 | + h.color = h.color if h.color else color |
| 97 | + |
| 98 | + # extend highlights if indices is nested list or text is list |
| 99 | + extended = [] |
| 100 | + for h in self.highlights: |
| 101 | + if isinstance(h.indices[0], (list, tuple)): |
| 102 | + ext = [Highlight(ix, h.text, h.color, h.fill_ring) |
| 103 | + for ix in h.indices] |
| 104 | + extended.extend(ext) |
| 105 | + elif isinstance(h.text, (list, tuple)): |
| 106 | + ext = [Highlight(h.indices, txt, h.color, h.fill_ring) |
| 107 | + for txt in h.text] |
| 108 | + extended.extend(ext) |
| 109 | + else: |
| 110 | + extended.append(h) |
| 111 | + self.highlights = extended |
85 | 112 |
|
86 | 113 | # MolDrawOptions defaults |
87 | 114 | opts = Draw.MolDrawOptions() |
@@ -212,7 +239,7 @@ def generate_label(self): |
212 | 239 | # find start index of text in label |
213 | 240 | start = self._find_text(text, 0, starts) |
214 | 241 | if start < 0: |
215 | | - warnings.warn(f"No match found in label for {highlight}") |
| 242 | + warnings.warn(f"{highlight.text!r} unmatched or already found in label") |
216 | 243 | continue |
217 | 244 | end = start + size |
218 | 245 | # create substitution string |
|
0 commit comments