# Workflow entry

An `ae` workflow runs on one fixed graph: one stereoisomer of a species, or one TS channel. The user starts from SMILES, and the first step turns it into the distinct, fully determined graphs, one workflow each.


# A species: SMILES to stereoisomers

[from_smiles](../reference/from_smiles.md#protomol.from_smiles) gives a stereo graph with every atom explicit: the SMILES atoms in SMILES order, then the implicit H in the order of the atoms they're bonded to. Stereo the SMILES leaves out stays undetermined, while sites whose two configurations give the same graph (the C of a methyl group) get a fixed parity:

``` python
>>> import protomol as pm
>>> gra = pm.from_smiles("CC(O)CC")    # butan-2-ol, centre not specified
>>> gra.symbols[:5]
('C', 'C', 'O', 'C', 'C')
>>> pm.is_determined(gra)              # C1 has an undetermined Tetrahedral
False
>>> pm.stereo_at(gra, 1).parity is None
True
>>> pm.stereo_at(gra, 0).parity        # a methyl C: trivial, so filled
1
>>> # in a notebook: "?" marks the undetermined C1
>>> pm.display(gra)  # doctest: +SKIP
```

[stereoisomers](../reference/stereoisomers.md#protomol.stereoisomers) expands every undetermined LS into the distinct, determined graphs, with the atom indices of `gra`; both mirror images are kept:

``` python
>>> isomers = pm.stereoisomers(gra)    # (R) and (S), as two determined graphs
>>> [pm.is_determined(smg) for smg in isomers]
[True, True]
>>> [pm.stereo_at(smg, 1).parity for smg in isomers]
[1, -1]
>>> mirror = pm.enantiomer(isomers[0])  # the same graph as isomers[1], up to numbering
>>> pm.canonical_json(mirror) == pm.canonical_json(isomers[1])
True
>>> # one workflow each:
>>> # for smg in isomers:
>>> #     run_species_workflow(smg)    # ae, illustrative
```

Configurations that give the same graph are listed once: butane-2,3-diol has two centres but three stereoisomers, as (R,S) and (S,R) are one meso form. [stereoisomers](../reference/stereoisomers.md#protomol.stereoisomers) takes a stereo graph: a [MolGraph](../reference/MolGraph.md#protomol.MolGraph) (no stereo) gets its sites from a scheme first, here [low_t](../reference/low_t.md#protomol.low_t):

``` python
>>> len(pm.stereoisomers(pm.from_smiles("CC(O)C(O)C")))
3
>>> len(pm.stereoisomers(pm.low_t(pm.without_stereo(gra))))
2
```

A species given with full stereo needs no expansion:

``` python
>>> gra = pm.from_smiles("C[C@@H](O)/C=C/C")   # one stereoisomer of pent-3-en-2-ol
>>> pm.is_determined(gra)
True
>>> gra == pm.examples.species()                # the test graph in protomol.examples
True
```


# A reaction: reactant and product SMILES to TS channels

[ts_graphs](../reference/ts_graphs.md#protomol.ts_graphs) finds the distinct TS channels (CCV). The reactants and products are each one graph, which may hold several molecules (`"CC.[OH]"`), with the same charge and spin. The atoms of every channel follow the reactant graph's order, and the product LS are carried over through the channel's atom mapping:

``` python
>>> rct = pm.from_smiles("CC.[OH]")
>>> prd = pm.from_smiles("C[CH2].O")
>>> channels = pm.ts_graphs(rct, prd)  # one StereoReactionGraph per distinct channel
>>> len(channels)
1
>>> ts = channels[0]
>>> ts.breaking, ts.forming            # an H moves from C0 to O2
(((0, 3),), ((2, 3),))
>>> pm.reactants(ts) == rct
True
>>> pm.low_t(ts) == pm.examples.abstraction_ts()  # the test graph, with its ts slots
True
>>> # the breaking C0-H3 in red, the forming O2-H3 in blue
>>> pm.display(ts, atom_indices=True)  # doctest: +SKIP
```

A bond homolysis gives two radicals, whose default spin is the triplet's; give the products the reactant's spin for the singlet channel:

``` python
>>> rct = pm.from_smiles("CCO")
>>> len(pm.ts_graphs(rct, pm.from_smiles("[CH2]O.[CH3]", spin=rct.spin)))
1
```

A channel whose sides leave some stereo open stands for several TSs. [stereoisomers](../reference/stereoisomers.md#protomol.stereoisomers) lists them: each undetermined LS on either side takes every configuration, and each change's `ts` slot follows by the TS-slot rules. SN2 at an open centre gives backside attack (the [TrigonalBipyramidal](../reference/TrigonalBipyramidal.md#protomol.TrigonalBipyramidal) TS, inversion) and frontside attack (retention), each with its mirror image:

``` python
>>> (srg,) = pm.ts_graphs(pm.from_smiles("FC(Cl)Br.[I-]"), pm.from_smiles("FC(Cl)I.[Br-]"))
>>> sorted(pm.stereo_at(ts, 1).ts.kind for ts in pm.stereoisomers(srg))
['Tetrahedral', 'Tetrahedral', 'TrigonalBipyramidal', 'TrigonalBipyramidal']
```

``` python
for srg in pm.ts_graphs(rct, prd):
    for ts in pm.stereoisomers(pm.low_t(srg)):   # the TS stereoisomers (P84)
        run_ts_workflow(ts)            # ae, illustrative
```


# Atom-mapped reaction SMILES (the INCITE import)

[from_reaction_smiles](../reference/from_reaction_smiles.md#protomol.from_reaction_smiles) reads an atom-mapped reaction SMILES. Atom k is the atom with the k-th smallest map number, and every atom, H included, must be written and mapped, on both sides. Each side is perceived as [from_smiles](../reference/from_smiles.md#protomol.from_smiles) does, and, as a reader, it sets the `ts` slots by the TS-slot rules, as [low_t](../reference/low_t.md#protomol.low_t) does:

``` python
>>> rxn = pm.from_reaction_smiles(
...     "[C:1]([C:2]([H:7])([H:8])[H:9])([H:4])([H:5])[H:6].[O:3][H:10]"
...     ">>[C:1]([C:2]([H:7])([H:8])[H:9])([H:5])[H:6].[H:4][O:3][H:10]"
... )
>>> rxn.breaking, rxn.forming
(((0, 3),), ((2, 3),))
>>> rxn == pm.examples.abstraction_ts()  # the same channel as `ts_graphs` gave above
True
>>> pm.from_reaction_smiles("[CH3:1][H:2].[OH:3]>>[CH3:1].[H:2][OH:3]")
Traceback (most recent call last):
...
protomol._types.GraphError: '[CH3:1][H:2].[OH:3]' has implicit H; write and map every H as an atom
```
