Skip to content

Assembly Equivalence

extract_unique_assemblies(assemblies)

Extract unique assemblies by isomorphism from a collection of assemblies.

Parameters:

Name Type Description Default
assemblies Iterable[Assembly]

The collection of assemblies to extract unique assemblies from.

required

Returns:

Type Description
set[Assembly]

A set of unique assemblies by isomorphism.

Examples:

>>> from nasap_net import extract_unique_assemblies
>>> unique = extract_unique_assemblies(assemblies)
Source code in src/nasap_net/assembly_equivalence/unique.py
def extract_unique_assemblies(
        assemblies: Iterable[Assembly]
) -> set[Assembly]:
    """Extract unique assemblies by isomorphism from a collection of assemblies.

    Parameters
    ----------
    assemblies : Iterable[Assembly]
        The collection of assemblies to extract unique assemblies from.

    Returns
    -------
    set[Assembly]
        A set of unique assemblies by isomorphism.

    Examples
    --------
    >>> from nasap_net import extract_unique_assemblies
    >>> unique = extract_unique_assemblies(assemblies)
    """
    sig_to_unique_assembly: dict[Hashable, set[Assembly]] = defaultdict(set)
    for assembly in assemblies:
        sig = get_assembly_signature(assembly)
        is_unique = True
        for unique_assembly in sig_to_unique_assembly[sig]:
            if is_isomorphic(assembly, unique_assembly):
                is_unique = False
                break
        if is_unique:
            sig_to_unique_assembly[sig].add(assembly)
    unique_assemblies = set()
    for assemblies_set in sig_to_unique_assembly.values():
        unique_assemblies.update(assemblies_set)
    return unique_assemblies

assemblies_equivalent(assembly1, assembly2)

Check if two assemblies are equivalent.

Two assemblies are considered equivalent if they are isomorphic, meaning there exists a one-to-one mapping between their components and binding sites that preserves the following properties: - Component kinds - Bond connections - Auxiliary edges (if present) - Auxiliary edge types (if present)

Parameters:

Name Type Description Default
assembly1 Assembly

The first assembly to compare.

required
assembly2 Assembly

The second assembly to compare.

required

Returns:

Type Description
bool

True if the assemblies are equivalent, False otherwise.

Examples:

>>> from nasap_net import Component, Assembly, Bond, assemblies_equivalent
>>> M = Component(kind='M', sites=[0, 1])
>>> L = Component(kind='L', sites=[0, 1])
>>> a1 = Assembly(
...     components={'M0': M, 'L0': L},
...     bonds=[Bond('M0', 0, 'L0', 0)]
... )
>>> a2 = Assembly(
...     components={'M1': M, 'L1': L},
...     bonds=[Bond('M1', 0, 'L1', 0)]
... )
>>> # a1 and a2 have different component IDs but the same structure
>>> assemblies_equivalent(a1, a2)
True
Source code in src/nasap_net/assembly_equivalence/core.py
def assemblies_equivalent(
        assembly1: Assembly,
        assembly2: Assembly,
) -> bool:
    """Check if two assemblies are equivalent.

    Two assemblies are considered equivalent if they are isomorphic,
    meaning there exists a one-to-one mapping between their components and
    binding sites that preserves the following properties:
        - Component kinds
        - Bond connections
        - Auxiliary edges (if present)
        - Auxiliary edge types (if present)

    Parameters
    ----------
    assembly1 : Assembly
        The first assembly to compare.
    assembly2 : Assembly
        The second assembly to compare.

    Returns
    -------
    bool
        True if the assemblies are equivalent, False otherwise.

    Examples
    --------
    >>> from nasap_net import Component, Assembly, Bond, assemblies_equivalent
    >>> M = Component(kind='M', sites=[0, 1])
    >>> L = Component(kind='L', sites=[0, 1])
    >>> a1 = Assembly(
    ...     components={'M0': M, 'L0': L},
    ...     bonds=[Bond('M0', 0, 'L0', 0)]
    ... )
    >>> a2 = Assembly(
    ...     components={'M1': M, 'L1': L},
    ...     bonds=[Bond('M1', 0, 'L1', 0)]
    ... )
    >>> # a1 and a2 have different component IDs but the same structure
    >>> assemblies_equivalent(a1, a2)
    True
    """
    if assembly1 == assembly2:
        return True
    return is_isomorphic(assembly1, assembly2)