https://github.com/aspuru-guzik-group/selfies

Robust representation of semantically constrained graphs, in particular for molecules in chemistry

https://github.com/aspuru-guzik-group/selfies

Science Score: 67.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 2 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, scholar.google, sciencedirect.com, springer.com, nature.com, iop.org, rsc.org
  • Committers with academic emails
    2 of 16 committers (12.5%) from academic institutions
  • Institutional organization owner
    Organization aspuru-guzik-group has institutional domain (aspuru.chem.harvard.edu)
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.8%) to scientific vocabulary

Keywords from Contributors

drug-discovery materials-science archival interactive projection generic quantum-chemistry sequences transformers biology
Last synced: 6 months ago · JSON representation

Repository

Robust representation of semantically constrained graphs, in particular for molecules in chemistry

Basic Info
  • Host: GitHub
  • Owner: aspuru-guzik-group
  • License: apache-2.0
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 15.8 MB
Statistics
  • Stars: 764
  • Watchers: 23
  • Forks: 133
  • Open Issues: 6
  • Releases: 9
Created almost 7 years ago · Last pushed 10 months ago
Metadata Files
Readme Changelog License

README.md

SELFIES

GitHub release versions License Maintenance GitHub issues Documentation Status GitHub contributors

Self-Referencing Embedded Strings (SELFIES): A 100% robust molecular string representation\ Mario Krenn, Florian Haese, AkshatKumar Nigam, Pascal Friederich, Alan Aspuru-Guzik\ Machine Learning: Science and Technology 1, 045024 (2020), extensive blog post January 2021.\ Talk on youtube about SELFIES.\ A community paper with 31 authors on SELFIES and the future of molecular string representations.\ Blog explaining SELFIES in Japanese language\ Code-Paper in February 2023\ SELFIES in Wolfram Mathematica (since Dec 2023)\ Major contributors of v1.0.n: Alston Lo and Seyone Chithrananda\ Main developer of v2.0.0: Alston Lo\ Chemistry Advisor: Robert Pollice


A main objective is to use SELFIES as direct input into machine learning models, in particular in generative models, for the generation of molecular graphs which are syntactically and semantically valid.

SELFIES validity in a VAE latent space

Installation

Use pip to install selfies.

bash pip install selfies

To check if the correct version of selfies is installed, use the following pip command.

bash pip show selfies

To upgrade to the latest release of selfies if you are using an older version, use the following pip command. Please see the CHANGELOG to review the changes between versions of selfies, before upgrading:

bash pip install selfies --upgrade

Usage

Overview

Please refer to the documentation in our code-paper, which contains a thorough tutorial for getting started with selfies and detailed descriptions of the functions that selfies provides. We summarize some key functions below.

| Function | Description | | ------------------------------------- | ----------------------------------------------------------------- | | selfies.encoder | Translates a SMILES string into its corresponding SELFIES string. | | selfies.decoder | Translates a SELFIES string into its corresponding SMILES string. | | selfies.set_semantic_constraints | Configures the semantic constraints that selfies operates on. | | selfies.len_selfies | Returns the number of symbols in a SELFIES string. | | selfies.split_selfies | Tokenizes a SELFIES string into its individual symbols. | | selfies.get_alphabet_from_selfies | Constructs an alphabet from an iterable of SELFIES strings. | | selfies.selfies_to_encoding | Converts a SELFIES string into its label and/or one-hot encoding. | | selfies.encoding_to_selfies | Converts a label or one-hot encoding into a SELFIES string. |

Examples

Translation between SELFIES and SMILES representations:

```python import selfies as sf

benzene = "c1ccccc1"

SMILES -> SELFIES -> SMILES translation

try: benzenesf = sf.encoder(benzene) # [C][=C][C][=C][C][=C][Ring1][=Branch1] benzenesmi = sf.decoder(benzene_sf) # C1=CC=CC=C1 except sf.EncoderError: pass # sf.encoder error! except sf.DecoderError: pass # sf.decoder error!

lenbenzene = sf.lenselfies(benzene_sf) # 8

symbolsbenzene = list(sf.splitselfies(benzene_sf))

['[C]', '[=C]', '[C]', '[=C]', '[C]', '[=C]', '[Ring1]', '[=Branch1]']

```

Very simple creation of random valid molecules:

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:

```python import selfies as sf import random

alphabet=sf.getsemanticrobustalphabet() # Gets the alphabet of robust symbols rndselfies=''.join(random.sample(list(alphabet), 9)) rndsmiles=sf.decoder(rndselfies) print(rnd_smiles) ``` 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.

Integer and one-hot encoding SELFIES:

In this example, we first build an alphabet from a dataset of SELFIES strings, and then convert a SELFIES string into its padded encoding. Note that we use the [nop] (no operation) symbol to pad our SELFIES, which is a special SELFIES symbol that is always ignored and skipped over by selfies.decoder, making it a useful padding character.

```python import selfies as sf

dataset = ["[C][O][C]", "[F][C][F]", "[O][=O]", "[C][C][O][C][C]"] alphabet = sf.getalphabetfrom_selfies(dataset) alphabet.add("[nop]") # [nop] is a special padding symbol alphabet = list(sorted(alphabet)) # ['[=O]', '[C]', '[F]', '[O]', '[nop]']

padtolen = max(sf.lenselfies(s) for s in dataset) # 5 symbolto_idx = {s: i for i, s in enumerate(alphabet)}

dimethyl_ether = dataset[0] # [C][O][C]

label, onehot = sf.selfiestoencoding( selfies=dimethylether, vocabstoi=symboltoidx, padtolen=padtolen, enctype="both" )

label = [1, 3, 1, 4, 4]

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]]

```

Customizing SELFIES:

In this example, we relax the semantic constraints of selfies to allow for hypervalences (caution: hypervalence rules are much less understood than octet rules. Some molecules containing hypervalences are important, but generally, it is not known which molecules are stable and reasonable).

```python import selfies as sf

hypervalentsf = sf.encoder('O=I(O)(O)(O)(O)O', strict=False) # orthoperiodic acid standardderivedsmi = sf.decoder(hypervalentsf)

OI (the default constraints for I allows for only 1 bond)

sf.setsemanticconstraints("hypervalent") relaxedderivedsmi = sf.decoder(hypervalent_sf)

O=I(O)(O)(O)(O)O (the hypervalent constraints for I allows for 7 bonds)

```

Explaining Translation:

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.

```python selfies = "[C][N][C][Branch1][C][P][C][C][Ring1][=Branch1]" smiles, attr = sf.decoder( selfies, attribute=True) print('SELFIES', selfies) print('SMILES', smiles) print('Attribution:') for smilestoken in attr: print(smilestoken)

output

SELFIES [C][N][C][Branch1][C][P][C][C][Ring1][=Branch1] SMILES C1NC(P)CC1 Attribution: AttributionMap(index=0, token='C', attribution=[Attribution(index=0, token='[C]')]) AttributionMap(index=2, token='N', attribution=[Attribution(index=1, token='[N]')]) AttributionMap(index=3, token='C', attribution=[Attribution(index=2, token='[C]')]) AttributionMap(index=5, token='P', attribution=[Attribution(index=3, token='[Branch1]'), Attribution(index=5, token='[P]')]) AttributionMap(index=7, token='C', attribution=[Attribution(index=6, token='[C]')]) AttributionMap(index=8, token='C', attribution=[Attribution(index=7, token='[C]')]) ```

attr is a list of AttributionMaps 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.

More Usages and Examples

Tests

selfies uses pytest with tox as its testing framework. All tests can be found in the tests/ directory. To run the test suite for SELFIES, install tox and run:

bash tox -- --trials=10000 --dataset_samples=10000

By default, selfies is tested against a random subset (of size dataset_samples=10000) on various datasets:

In first releases, we also tested the 36M+ molecules from the eMolecules Database.

Version History

See CHANGELOG.

Credits

We thank Jacques Boitreaud, Andrew Brereton, Nessa Carson (supersciencegrl), Matthew Carbone (x94carbone), Vladimir Chupakhin (chupvl), Nathan Frey (ncfrey), Theophile Gaudin, HelloJocelynLu, Hyunmin Kim (hmkim), Minjie Li, Vincent Mallet, Alexander Minidis (DocMinus), Kohulan Rajan (Kohulan), Kevin Ryan (LeanAndMean), Benjamin Sanchez-Lengeling, Andrew White, Zhenpeng Yao and Adamo Young for their suggestions and bug reports, and Robert Pollice for chemistry advices.

License

Apache License 2.0

Owner

  • Name: Aspuru-Guzik group repo
  • Login: aspuru-guzik-group
  • Kind: organization

GitHub Events

Total
  • Create event: 2
  • Release event: 1
  • Issues event: 15
  • Watch event: 121
  • Delete event: 1
  • Issue comment event: 26
  • Push event: 12
  • Pull request event: 3
  • Fork event: 12
Last Year
  • Create event: 2
  • Release event: 1
  • Issues event: 15
  • Watch event: 121
  • Delete event: 1
  • Issue comment event: 26
  • Push event: 12
  • Pull request event: 3
  • Fork event: 12

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 606
  • Total Committers: 16
  • Avg Commits per committer: 37.875
  • Development Distribution Score (DDS): 0.417
Past Year
  • Commits: 18
  • Committers: 4
  • Avg Commits per committer: 4.5
  • Development Distribution Score (DDS): 0.278
Top Committers
Name Email Commits
alstonlo 4****o 353
Mario Krenn 4****0 161
seyonechithrananda s****c@g****m 39
Andrew White w****w@g****m 18
Florian Häse h****n@g****m 9
Nathan Frey n****3@g****m 4
Haydn Jones h****t@g****m 3
Jannis Born j****b@z****m 3
dependabot[bot] 4****] 3
vandrw v****5@g****m 3
Akshat Nigam a****8@g****m 2
Darren Wee d****e@u****u 2
Francois Berenger u****e@s****g 2
Robert Pollice r****e@g****m 2
HelloJocelynLu j****0@n****u 1
C CS@C****l 1
Committer Domains (Top 20 + Academic)

Packages

  • Total packages: 3
  • Total downloads:
    • pypi 47,433 last-month
  • Total docker downloads: 151
  • Total dependent packages: 23
    (may contain duplicates)
  • Total dependent repositories: 33
    (may contain duplicates)
  • Total versions: 32
  • Total maintainers: 1
pypi.org: selfies

SELFIES (SELF-referencIng Embedded Strings) is a general-purpose, sequence-based, robust representation of semantically constrained graphs.

  • Versions: 16
  • Dependent Packages: 20
  • Dependent Repositories: 30
  • Downloads: 47,433 Last month
  • Docker Downloads: 151
Rankings
Dependent packages count: 0.6%
Stargazers count: 2.6%
Average: 2.6%
Dependent repos count: 2.7%
Docker downloads count: 2.7%
Downloads: 2.8%
Forks count: 4.3%
Maintainers (1)
Last synced: 6 months ago
proxy.golang.org: github.com/aspuru-guzik-group/selfies
  • Versions: 9
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.4%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 6 months ago
conda-forge.org: selfies
  • Versions: 7
  • Dependent Packages: 3
  • Dependent Repositories: 3
Rankings
Dependent packages count: 15.6%
Average: 17.1%
Forks count: 17.3%
Stargazers count: 17.5%
Dependent repos count: 18.0%
Last synced: 6 months ago

Dependencies

docs/requirements.txt pypi
  • nbsphinx *
  • sphinx-autodoc-typehints *
  • sphinx-rtd-theme *
.github/workflows/ci.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v1 composite
setup.py pypi
original_code_from_paper/environment.yml pypi
  • absl-py ==0.7.0
  • astor ==0.7.1
  • deepsmiles ==1.0.1
  • gast ==0.2.2
  • grpcio ==1.18.0
  • keras-applications ==1.0.7
  • keras-preprocessing ==1.0.9
  • markdown ==3.0.1
  • matplotlib ==3.0.3
  • mock ==2.0.0
  • numpy ==1.16.1
  • pbr ==5.1.2
  • selfies ==0.1.1
  • tensorboard ==1.12.2
  • tensorflow ==1.13.0rc2
  • tensorflow-estimator ==1.13.0rc0
  • termcolor ==1.1.0