# Identities

An identity is a string that says what a stationary point *is*: its formula, its InChI, its SMILES, a conformer label. Each identity comes from an **identity algorithm**, and each algorithm partitions the stationary points in a database into groups that share a value. Different algorithms partition at different resolutions, so the same stationary points can be grouped by formula, by stereoisomer, or by conformer.


# Built-in algorithms

autostorage registers the following algorithms itself:

| algorithm | kind | parent | value |
|----|----|----|----|
| [rdkit_inchi_conn](../reference/rdkit_inchi_conn.md#autostorage.rdkit_inchi_conn) | `isomer` |  | Connectivity InChI (no stereo layers), from `autogeom.inchi(geo, stereo=False)` |
| [rdkit_inchi](../reference/rdkit_inchi.md#autostorage.rdkit_inchi) | `stereoisomer` |  | Standard InChI, from `autogeom.inchi` |
| [rdkit_smiles](../reference/rdkit_smiles.md#autostorage.rdkit_smiles) | `stereoisomer` | [rdkit_inchi](../reference/rdkit_inchi.md#autostorage.rdkit_inchi) | SMILES, from `autogeom.smiles` |
| [hill_formula](../reference/hill_formula.md#autostorage.hill_formula) | `formula` | [rdkit_inchi](../reference/rdkit_inchi.md#autostorage.rdkit_inchi) | Formula in Hill order, from `autogeom.formula` |

When a [StationaryPointRow](../reference/StationaryPointRow.md#autostorage.StationaryPointRow) is added through a [Database.session()](../reference/Database.md#autostorage.Database.session), an [IdentityRow](../reference/IdentityRow.md#autostorage.IdentityRow) is attached to it for every registered algorithm, and regenerated if its geometry or `order` changes. Algorithms registered with restricted `orders = {0}`, such as [rdkit_inchi_conn](../reference/rdkit_inchi_conn.md#autostorage.rdkit_inchi_conn), [rdkit_inchi](../reference/rdkit_inchi.md#autostorage.rdkit_inchi), and [rdkit_smiles](../reference/rdkit_smiles.md#autostorage.rdkit_smiles), are skipped for higher-order saddle points. Unrestricted algorithms such as [hill_formula](../reference/hill_formula.md#autostorage.hill_formula) apply to all algorithms. Stationary points with the same value share one [IdentityRow](../reference/IdentityRow.md#autostorage.IdentityRow). Stereoisomers such as cis- and trans-1,2-difluoroethene therefore share one [rdkit_inchi_conn](../reference/rdkit_inchi_conn.md#autostorage.rdkit_inchi_conn) identity but have different [rdkit_inchi](../reference/rdkit_inchi.md#autostorage.rdkit_inchi) identities. If an algorithm fails for a geometry (for example, InChI for a metal complex), an [IdentityGenerationWarning](../reference/IdentityGenerationWarning.md#autostorage.IdentityGenerationWarning) is issued and that identity is skipped.

Look stationary points up by identity with [query.stationary_point_by_identity](../reference/query.stationary_point_by_identity.md#autostorage.query.stationary_point_by_identity):

``` python
from autostorage import Database, query, rdkit_inchi

with Database(":memory:") as db, db.session() as session:
    stmt = query.stationary_point_by_identity(rdkit_inchi, "InChI=1S/H2O/h1H2")
    waters = session.exec(stmt).all()
```


# Kinds

Every algorithm has an [IdentityKind](../reference/IdentityKind.md#autostorage.IdentityKind) that says what sort of identity it produces: `formula`, `isomer`, `stereoisomer`, or `conformer`. This lets code select algorithms by kind without naming a specific one.


# Parents and `other_geos`

An identity function has the signature `(geo, other_geos=None) -> str`. For an algorithm with a `parent`, `other_geos` holds one geometry per existing identity value among the stationary points that share this geometry's *parent* identity. The function can then return one of those existing values instead of making a new one.

[rdkit_smiles](../reference/rdkit_smiles.md#autostorage.rdkit_smiles) uses this: its parent is [rdkit_inchi](../reference/rdkit_inchi.md#autostorage.rdkit_inchi), so if a stationary point with the same InChI already has a SMILES, that SMILES is reused, even if it isn't RDKit's canonical one. The same mechanism lets a conformer algorithm compare a new geometry only against the conformers of the same species.


# Registering your own algorithm

Register an algorithm before opening the [Database](../reference/Database.md#autostorage.Database), which mirrors the registry into its `identity_algorithm` table:

``` python
import uuid
from collections.abc import Mapping

import autogeom
import numpy as np
from autogeom import Geometry

from autostorage import Database, IdentityAlgorithmRegistry, IdentityKind, rdkit_inchi


def rmsd_conformer_identity_fn(
    geo: Geometry, other_geos: Mapping[str, Geometry] | None = None
) -> str:
    """Reuse the label of an existing conformer within 0.1 Å RMSD, or make a new one."""
    for label, other_geo in (other_geos or {}).items():
        aligned = autogeom.rotate(
            autogeom.center(geo), autogeom.alignment_rotation(geo, other_geo)
        )
        diff = aligned.coordinates - autogeom.center(other_geo).coordinates
        if np.sqrt((diff**2).sum(axis=1).mean()) < 0.1:
            return label
    return uuid.uuid4().hex


rmsd_conformer = IdentityAlgorithmRegistry.register(
    name="rmsd_conformer",
    kind=IdentityKind.CONFORMER,
    identity_fn=rmsd_conformer_identity_fn,
    parent=rdkit_inchi,  # compare only against geometries with the same InChI
    orders={0},  # must be compatible with parent orders
)

db = Database(":memory:")  # new stationary points now also get an rmsd_conformer identity
db.close()
```

Algorithm names must be unique: registering a name twice raises a `ValueError`. `IdentityAlgorithmRegistry.get(name)` returns a registered algorithm, and `IdentityAlgorithmRegistry.algorithms` lists them all.

An identity function should raise `ValueError`, `NotImplementedError`, or `autogeom.GeometryConversionError` for a geometry it cannot handle; these are reported as an [IdentityGenerationWarning](../reference/IdentityGenerationWarning.md#autostorage.IdentityGenerationWarning).


# Next steps

- [Data models](models.md): the [IdentityAlgorithmRow](../reference/IdentityAlgorithmRow.md#autostorage.IdentityAlgorithmRow) and [IdentityRow](../reference/IdentityRow.md#autostorage.IdentityRow) tables that store identities.
- [Database](database.md): opening a [Database](../reference/Database.md#autostorage.Database), which mirrors the registry into it.
