Skip to content

Compound

Compound

A lightweight dataclass for storing details about an individual compound (id, SMILES, tags, and arbitrary metadata).

The Compound class is a simple container used throughout the Workbench chemistry workflows to carry a compound's identifier, structure, and associated tags/metadata.

Compound dataclass

Compound: Store details about an individual compound.

Source code in src/workbench/api/compound.py
@dataclass
class Compound:
    """Compound: Store details about an individual compound."""

    id: str
    smiles: str = None
    tags: List[str] = field(default_factory=list)
    meta: dict = field(default_factory=dict)
    log: logging.Logger = field(default_factory=lambda: logging.getLogger("workbench"), init=False)

    def add_tag(self, tag: str) -> None:
        """Add a single tag to the tags list."""
        if tag not in self.tags:
            self.tags.append(tag)

    def remove_tag(self, tag: str) -> None:
        """Remove a single tag from the tags list."""
        if tag in self.tags:
            self.tags.remove(tag)

    def add_meta(self, key: str, value) -> None:
        """Add metadata to the Compound."""
        self.meta[key] = value

    def details(self) -> dict:
        """Compound Details

        Returns:
            dict: A dictionary of details about the Compound.
        """
        return {"project": "XYZ", "smiles": self.smiles, "tags": self.tags, "meta": self.meta}

    def image(self, width: int = 300, height: int = 200) -> str:
        """Generate an SVG image of the Compound

        Args:
            width (int, optional): The width of the image (default: 300)
            height (int, optional): The height of the image (default: 200)

        Returns:
            str: The SVG image of the Compound
        """
        from workbench.utils.theme_manager import ThemeManager  # Avoid circular import

        return svg_from_smiles(self.smiles, width, height, background=ThemeManager().background())

    def __str__(self) -> str:
        """User-friendly string representation."""
        str_output = (
            f"Compound({self.id})\n  SMILES: {self.smiles}\n  Tags: {', '.join(self.tags) if self.tags else 'None'}"
        )
        str_output += f"\n  Meta: {self.meta if self.meta else 'None'}"
        return str_output

__str__()

User-friendly string representation.

Source code in src/workbench/api/compound.py
def __str__(self) -> str:
    """User-friendly string representation."""
    str_output = (
        f"Compound({self.id})\n  SMILES: {self.smiles}\n  Tags: {', '.join(self.tags) if self.tags else 'None'}"
    )
    str_output += f"\n  Meta: {self.meta if self.meta else 'None'}"
    return str_output

add_meta(key, value)

Add metadata to the Compound.

Source code in src/workbench/api/compound.py
def add_meta(self, key: str, value) -> None:
    """Add metadata to the Compound."""
    self.meta[key] = value

add_tag(tag)

Add a single tag to the tags list.

Source code in src/workbench/api/compound.py
def add_tag(self, tag: str) -> None:
    """Add a single tag to the tags list."""
    if tag not in self.tags:
        self.tags.append(tag)

details()

Compound Details

Returns:

Name Type Description
dict dict

A dictionary of details about the Compound.

Source code in src/workbench/api/compound.py
def details(self) -> dict:
    """Compound Details

    Returns:
        dict: A dictionary of details about the Compound.
    """
    return {"project": "XYZ", "smiles": self.smiles, "tags": self.tags, "meta": self.meta}

image(width=300, height=200)

Generate an SVG image of the Compound

Parameters:

Name Type Description Default
width int

The width of the image (default: 300)

300
height int

The height of the image (default: 200)

200

Returns:

Name Type Description
str str

The SVG image of the Compound

Source code in src/workbench/api/compound.py
def image(self, width: int = 300, height: int = 200) -> str:
    """Generate an SVG image of the Compound

    Args:
        width (int, optional): The width of the image (default: 300)
        height (int, optional): The height of the image (default: 200)

    Returns:
        str: The SVG image of the Compound
    """
    from workbench.utils.theme_manager import ThemeManager  # Avoid circular import

    return svg_from_smiles(self.smiles, width, height, background=ThemeManager().background())

remove_tag(tag)

Remove a single tag from the tags list.

Source code in src/workbench/api/compound.py
def remove_tag(self, tag: str) -> None:
    """Remove a single tag from the tags list."""
    if tag in self.tags:
        self.tags.remove(tag)