a b/README.md
1
# SELFIES
2
3
[![GitHub release](https://img.shields.io/github/release/aspuru-guzik-group/selfies.svg)](https://GitHub.com/aspuru-guzik-group/selfies/releases/)
4
![versions](https://img.shields.io/pypi/pyversions/selfies.svg)
5
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-blue.svg)](https://GitHub.com/aspuru-guzik-group/selfies/graphs/commit-activity)
7
[![GitHub issues](https://img.shields.io/github/issues/aspuru-guzik-group/selfies.svg)](https://GitHub.com/aspuru-guzik-group/selfies/issues/)
8
[![Documentation Status](https://readthedocs.org/projects/selfiesv2/badge/?version=latest)](http://selfiesv2.readthedocs.io/?badge=latest)
9
[![GitHub contributors](https://img.shields.io/github/contributors/aspuru-guzik-group/selfies.svg)](https://GitHub.com/aspuru-guzik-group/selfies/graphs/contributors/)
10
11
12
**Self-Referencing Embedded Strings (SELFIES): A 100% robust molecular string representation**\
13
_Mario Krenn, Florian Haese, AkshatKumar Nigam, Pascal Friederich, Alan Aspuru-Guzik_\
14
[*Machine Learning: Science and Technology* **1**, 045024 (2020)](https://iopscience.iop.org/article/10.1088/2632-2153/aba947), [extensive blog post January 2021](https://aspuru.substack.com/p/molecular-graph-representations-and).\
15
[Talk on youtube about SELFIES](https://www.youtube.com/watch?v=CaIyUmfGXDk).\
16
[A community paper with 31 authors on SELFIES and the future of molecular string representations](https://arxiv.org/abs/2204.00056).\
17
[Blog explaining SELFIES in Japanese language](https://blacktanktop.hatenablog.com/entry/2021/08/12/115613)\
18
**[Code-Paper in February 2023](https://pubs.rsc.org/en/content/articlelanding/2023/DD/D3DD00044C)**\
19
[SELFIES in Wolfram Mathematica](https://resources.wolframcloud.com/PacletRepository/resources/WolframChemistry/Selfies/)  (since Dec 2023)\
20
Major contributors of v1.0.n: _[Alston Lo](https://github.com/alstonlo) and [Seyone Chithrananda](https://github.com/seyonechithrananda)_\
21
Main developer of v2.0.0: _[Alston Lo](https://github.com/alstonlo)_\
22
Chemistry Advisor: [Robert Pollice](https://scholar.google.at/citations?user=JR2N3JIAAAAJ)
23
24
---
25
26
A main objective is to use SELFIES as direct input into machine learning models,
27
in particular in generative models, for the generation of molecular graphs
28
which are syntactically and semantically valid.
29
30
<p align="center">
31
   <img src="https://github.com/aspuru-guzik-group/selfies/blob/master/examples/VAE_LS_Validity.png" alt="SELFIES validity in a VAE latent space" width="666px">
32
</p>
33
34
## Installation
35
Use pip to install ``selfies``.
36
37
```bash
38
pip install selfies
39
```
40
41
To check if the correct version of ``selfies`` is installed, use
42
the following pip command.
43
44
```bash
45
pip show selfies
46
```
47
48
To upgrade to the latest release of ``selfies`` if you are using an
49
older version, use the following pip command. Please see the
50
[CHANGELOG](https://github.com/aspuru-guzik-group/selfies/blob/master/CHANGELOG.md)
51
to review the changes between versions of `selfies`, before upgrading:
52
53
```bash
54
pip install selfies --upgrade
55
```
56
57
58
## Usage
59
60
### Overview
61
62
Please refer to the [documentation in our code-paper](https://pubs.rsc.org/en/content/articlelanding/2023/DD/D3DD00044C),
63
which contains a thorough tutorial  for getting started with ``selfies``
64
and detailed descriptions of the functions
65
that ``selfies`` provides. We summarize some key functions below.
66
67
| Function                              | Description                                                       |
68
| ------------------------------------- | ----------------------------------------------------------------- |
69
| ``selfies.encoder``                   | Translates a SMILES string into its corresponding SELFIES string. |
70
| ``selfies.decoder``                   | Translates a SELFIES string into its corresponding SMILES string. |
71
| ``selfies.set_semantic_constraints``  | Configures the semantic constraints that ``selfies`` operates on. |
72
| ``selfies.len_selfies``               | Returns the number of symbols in a SELFIES string.                |
73
| ``selfies.split_selfies``             | Tokenizes a SELFIES string into its individual symbols.           |
74
| ``selfies.get_alphabet_from_selfies`` | Constructs an alphabet from an iterable of SELFIES strings.       |
75
| ``selfies.selfies_to_encoding``       | Converts a SELFIES string into its label and/or one-hot encoding. |
76
| ``selfies.encoding_to_selfies``       | Converts a label or one-hot encoding into a SELFIES string.       |
77
78
79
### Examples
80
81
#### Translation between SELFIES and SMILES representations:
82
83
```python
84
import selfies as sf
85
86
benzene = "c1ccccc1"
87
88
# SMILES -> SELFIES -> SMILES translation
89
try:
90
    benzene_sf = sf.encoder(benzene)  # [C][=C][C][=C][C][=C][Ring1][=Branch1]
91
    benzene_smi = sf.decoder(benzene_sf)  # C1=CC=CC=C1
92
except sf.EncoderError:
93
    pass  # sf.encoder error!
94
except sf.DecoderError:
95
    pass  # sf.decoder error!
96
97
len_benzene = sf.len_selfies(benzene_sf)  # 8
98
99
symbols_benzene = list(sf.split_selfies(benzene_sf))
100
# ['[C]', '[=C]', '[C]', '[=C]', '[C]', '[=C]', '[Ring1]', '[=Branch1]']
101
```
102
103
#### Very simple creation of random valid molecules:
104
A key property of SELFIES is the possibility to create valid random molecules in a very simple way -- inspired by a tweet by [Rajarshi Guha](https://twitter.com/rguha/status/1543601839983284224):
105
106
```python
107
import selfies as sf
108
import random
109
110
alphabet=sf.get_semantic_robust_alphabet() # Gets the alphabet of robust symbols
111
rnd_selfies=''.join(random.sample(list(alphabet), 9))
112
rnd_smiles=sf.decoder(rnd_selfies)
113
print(rnd_smiles)
114
```
115
These simple lines gives crazy molecules, but all are valid. Can be used as a start for more advanced filtering techniques or for machine learning models.
116
117
#### Integer and one-hot encoding SELFIES:
118
119
In this example, we first build an alphabet from a dataset of SELFIES strings,
120
and then convert a SELFIES string into its padded encoding. Note that we use the
121
``[nop]`` ([no operation](https://en.wikipedia.org/wiki/NOP_(code) ))
122
symbol to pad our SELFIES, which is a special SELFIES symbol that is always
123
ignored and skipped over by ``selfies.decoder``, making it a useful
124
padding character.
125
126
```python
127
import selfies as sf
128
129
dataset = ["[C][O][C]", "[F][C][F]", "[O][=O]", "[C][C][O][C][C]"]
130
alphabet = sf.get_alphabet_from_selfies(dataset)
131
alphabet.add("[nop]")  # [nop] is a special padding symbol
132
alphabet = list(sorted(alphabet))  # ['[=O]', '[C]', '[F]', '[O]', '[nop]']
133
134
pad_to_len = max(sf.len_selfies(s) for s in dataset)  # 5
135
symbol_to_idx = {s: i for i, s in enumerate(alphabet)}
136
137
dimethyl_ether = dataset[0]  # [C][O][C]
138
139
label, one_hot = sf.selfies_to_encoding(
140
   selfies=dimethyl_ether,
141
   vocab_stoi=symbol_to_idx,
142
   pad_to_len=pad_to_len,
143
   enc_type="both"
144
)
145
# label = [1, 3, 1, 4, 4]
146
# one_hot = [[0, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]
147
```
148
149
#### Customizing SELFIES:
150
151
In this example, we relax the semantic constraints of ``selfies`` to allow
152
for hypervalences (caution: hypervalence rules are much less understood
153
than octet rules. Some molecules containing hypervalences are important,
154
but generally, it is not known which molecules are stable and reasonable).
155
156
```python
157
import selfies as sf
158
159
hypervalent_sf = sf.encoder('O=I(O)(O)(O)(O)O', strict=False)  # orthoperiodic acid
160
standard_derived_smi = sf.decoder(hypervalent_sf)
161
# OI (the default constraints for I allows for only 1 bond)
162
163
sf.set_semantic_constraints("hypervalent")
164
relaxed_derived_smi = sf.decoder(hypervalent_sf)
165
# O=I(O)(O)(O)(O)O (the hypervalent constraints for I allows for 7 bonds)
166
```
167
168
#### Explaining Translation:
169
170
You can get an "attribution" list that traces the connection between input and output tokens. For example let's see which tokens in the SELFIES string ``[C][N][C][Branch1][C][P][C][C][Ring1][=Branch1]`` are responsible for the output SMILES tokens.
171
172
```python
173
selfies = "[C][N][C][Branch1][C][P][C][C][Ring1][=Branch1]"
174
smiles, attr = sf.decoder(
175
    selfies, attribute=True)
176
print('SELFIES', selfies)
177
print('SMILES', smiles)
178
print('Attribution:')
179
for smiles_token in attr:
180
    print(smiles_token)
181
182
# output
183
SELFIES [C][N][C][Branch1][C][P][C][C][Ring1][=Branch1]
184
SMILES C1NC(P)CC1
185
Attribution:
186
AttributionMap(index=0, token='C', attribution=[Attribution(index=0, token='[C]')])
187
AttributionMap(index=2, token='N', attribution=[Attribution(index=1, token='[N]')])
188
AttributionMap(index=3, token='C', attribution=[Attribution(index=2, token='[C]')])
189
AttributionMap(index=5, token='P', attribution=[Attribution(index=3, token='[Branch1]'), Attribution(index=5, token='[P]')])
190
AttributionMap(index=7, token='C', attribution=[Attribution(index=6, token='[C]')])
191
AttributionMap(index=8, token='C', attribution=[Attribution(index=7, token='[C]')])
192
```
193
194
``attr`` is a list of `AttributionMap`s containing the output token, its index, and input tokens that led to it. For example, the ``P`` appearing in the output SMILES at that location is a result of both the ``[Branch1]`` token at position 3 and the ``[P]`` token at index 5. This works for both encoding and decoding. For finer control of tracking the translation (like tracking rings), you can access attributions in the underlying molecular graph with ``get_attribution``.
195
196
### More Usages and Examples
197
198
* More examples can be found in the ``examples/`` directory, including a
199
[variational autoencoder that runs on the SELFIES](https://github.com/aspuru-guzik-group/selfies/tree/master/examples/vae_example) language.
200
* This [ICLR2020 paper](https://arxiv.org/abs/1909.11655) used SELFIES in a
201
genetic algorithm to achieve state-of-the-art performance for inverse design,
202
with the [code here](https://github.com/aspuru-guzik-group/GA).
203
* SELFIES allows for [highly efficient exploration and interpolation of the chemical space](https://chemrxiv.org/articles/preprint/Beyond_Generative_Models_Superfast_Traversal_Optimization_Novelty_Exploration_and_Discovery_STONED_Algorithm_for_Molecules_using_SELFIES/13383266), with a [deterministic algorithms, see code](https://github.com/aspuru-guzik-group/stoned-selfies).
204
* We use SELFIES for [Deep Molecular dreaming](https://arxiv.org/abs/2012.09712), a new generative model inspired by interpretable neural networks in computational vision. See the [code of PASITHEA here](https://github.com/aspuru-guzik-group/Pasithea).
205
* Kohulan Rajan, Achim Zielesny, Christoph Steinbeck show in two papers that SELFIES outperforms other representations in [img2string](https://link.springer.com/article/10.1186/s13321-020-00469-w) and [string2string](https://chemrxiv.org/articles/preprint/STOUT_SMILES_to_IUPAC_Names_Using_Neural_Machine_Translation/13469202/1) translation tasks, see the codes of [DECIMER](https://github.com/Kohulan/DECIMER-Image-to-SMILES) and [STOUT](https://github.com/Kohulan/Smiles-TO-iUpac-Translator).
206
* Nathan Frey, Vijay Gadepally, and Bharath Ramsundar used SELFIES with normalizing flows to develop the [FastFlows](https://arxiv.org/abs/2201.12419) framework for deep chemical generative modeling.
207
* An improvement to the old genetic algorithm, the authors have also released [JANUS](https://arxiv.org/abs/2106.04011), which allows for more efficient optimization in the chemical space. JANUS makes use of [STONED-SELFIES](https://pubs.rsc.org/en/content/articlepdf/2021/sc/d1sc00231g) and a neural network for efficient sampling.
208
209
## Tests
210
`selfies` uses `pytest` with `tox` as its testing framework.
211
All tests can be found in  the `tests/` directory. To run the test suite for
212
SELFIES, install ``tox`` and run:
213
214
```bash
215
tox -- --trials=10000 --dataset_samples=10000
216
```
217
218
By default, `selfies` is tested against a random subset
219
(of size ``dataset_samples=10000``) on various datasets:
220
221
 * 130K molecules from [QM9](https://www.nature.com/articles/sdata201422)
222
 * 250K molecules from [ZINC](https://en.wikipedia.org/wiki/ZINC_database)
223
 * 50K molecules from a dataset of [non-fullerene acceptors for organic solar cells](https://www.sciencedirect.com/science/article/pii/S2542435117301307)
224
 * 160K+ molecules from various [MoleculeNet](https://moleculenet.org/datasets-1) datasets
225
226
In first releases, we also tested the 36M+ molecules from the [eMolecules Database](https://downloads.emolecules.com/free/2024-12-01/).
227
228
229
## Version History
230
See [CHANGELOG](https://github.com/aspuru-guzik-group/selfies/blob/master/CHANGELOG.md).
231
232
## Credits
233
234
We thank Jacques Boitreaud, Andrew Brereton, Nessa Carson (supersciencegrl), Matthew Carbone (x94carbone),  Vladimir Chupakhin (chupvl), Nathan Frey (ncfrey), Theophile Gaudin,
235
HelloJocelynLu, Hyunmin Kim (hmkim), Minjie Li, Vincent Mallet, Alexander Minidis (DocMinus), Kohulan Rajan (Kohulan),
236
Kevin Ryan (LeanAndMean), Benjamin Sanchez-Lengeling, Andrew White, Zhenpeng Yao and Adamo Young for their suggestions and bug reports,
237
and Robert Pollice for chemistry advices.
238
239
## License
240
241
[Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/)