Skip to main content

Python client for Spaya

Project description

pyspaya

Documenation PyPI PyPI - Python Version GitHub Workflow Status (with event) GitHub

Description

Clients

Url and Authorization

Description

Spaya API

This package provides an easy way to score SMILES with the Spaya API.

This restrosynthesis score (RScore) is a metric provided by the Spaya algorithms, related to the probability of a disconnection and consequently to the confidence the algorithm has on this disconnection/route.

Spaya API employs a data-driven AI approach to discover retrosynthetic routes. An iterative exploration of all possible routes is performed until commercially available starting materials are identified.

Useful links:

Score

Each scored SMILES return a RetrosynthesisResult:

from typing import Dict
from iktos.spaya import BearerToken, SpayaClientREST, RetrosynthesisResult

# Create a client
client = SpayaClientREST(url="https://spaya.ai", authorization=BearerToken(token="YourToken"))

# Start a retrosynthesis and wait for the results
result: Dict[str, RetrosynthesisResult] = client.score_smiles(smiles=["c1ccn2nccc2c1"])

# Show results
retro_result: RetrosynthesisResult = result["c1ccn2nccc2c1"]
print(f"Progress:{retro_result.progress} Status:{retro_result.status}")
print(f"Score:{retro_result.rscore} Number of steps:{retro_result.nb_steps}")

Clients

To match every case, this API provides 3 different clients:

  • SpayaClientREST: A synchronous client
  • SpayaClientAsync: An asynchronous client with reconnection and helpers for the websocket
  • SpayaClientCallback: An asynchronous client that allows you to get results using a Callback

REST

SpayaClientREST sends SMILES synchronously to be scored

Example using a list of SMILES:

from iktos.spaya import BearerToken, SpayaClientREST

# Create client with authorization
client = SpayaClientREST(url="https://spaya.ai",
                         authorization=BearerToken("myT0ken"))

# Start a retrosynthesis and wait for the results
scored_smiles = client.score_smiles(smiles=["O=C1CCCCO1", "O=C1CCCNN1",])

# Show the results
for smiles, result in scored_smiles.items():
    print(f"{smiles}: {result.rscore} / {result.nb_steps}")

Example using a DataFrame:

from iktos.spaya import BearerToken, SpayaClientREST
from pandas import DataFrame

# Create client with authorization
client = SpayaClientREST(url="https://spaya.ai/",
                         authorization=BearerToken("myT0ken"))

# Start a retrosynthesis and wait for the results with a dataframe
df = DataFrame({"input_smiles": ["O=C1CCCCO1", "O=C1CCCNN1",]})
df = client.score_smiles(
    smiles=df,
    dataframe_smiles_column="input_smiles",
    callback_progression=lambda p: print(f"progression:{p}")
)

# Show the results
print(f"result: {df}")

Example using a list of SMILES and consume function:

from iktos.spaya import BearerToken, SpayaClientREST

# Create client with authorization
client = SpayaClientREST(url="https://spaya.ai/",
                         authorization=BearerToken("myT0ken"))

# Start a retrosynthesis
client.start_retrosynthesis(smiles=["O=C1CCCCO1", "O=C1CCCNN1",])

# Consume the results as soon as possible
while not client.is_empty:
    for smiles, result in client.consume():
        print(f"{smiles} {result}")

Async

SpayaClientAsync sends SMILES asynchronously to be scored

import asyncio
from iktos.spaya import BearerToken, SpayaClientAsync

async def score():
    async with SpayaClientAsync(url="https://spaya.ai",
                                authorization=BearerToken("myT0ken")
    ) as client:
        # Start scoring SMILES
        await client.start_retrosynthesis(["O=C1CCCCO1", "O=C1CCCNN1",])

        # Wait and print scores as soon as received
        async for smiles, result in client.consume():
            print(f"{smiles}: {result.rscore} / {result.nb_steps}")

asyncio.run(score())

Callback

SpayaClientCallback calls a function as soon as a SMILES is scored

import asyncio
from iktos.spaya import (
    BearerToken, SpayaClientCallback, RetrosynthesisResult
)

async def generator(client: SpayaClientCallback):
    # Generate and start scoring SMILES
    for smiles in ["O=C1CCCCO1", "O=C1CCCNN1"]:
        await client.start_retrosynthesis(smiles)

async def callback(smiles: str, result: RetrosynthesisResult):
    # Handle the results
    print(f"{smiles}: {result.rscore} / {result.nb_steps}")

async def generate_and_score():
    async with SpayaClientCallback(url="https://spaya.ai",
                                   authorization=BearerToken("myT0ken"),
                                   callback=callback) as client:
        # Generate SMILES
        await generator(client)

        # Block until the ends
        await client.wait_result()

asyncio.run(generate_and_score())

Url and Authorization

To access the Spaya API, you need to generate a token from the Spaya API page in your Spaya account

Unless specified, use the BearerToken class

Bearer token

The server is protected by a token

from iktos.spaya import BearerToken, SpayaClientREST
SpayaClientREST(url="https://spaya.ai", authorization=BearerToken(token="itb3cy0s..."))

Custom Bearer token

The server is protected by a token and it uses a custom header

from iktos.spaya import CustomBearerToken, SpayaClientREST
SpayaClientREST(
    url="https://spaya.ai",
    authorization=CustomBearerToken(token="itb3cy0s...", header_key="X-Iktos-Authorization")
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyspaya-1.2.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyspaya-1.2.0-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file pyspaya-1.2.0.tar.gz.

File metadata

  • Download URL: pyspaya-1.2.0.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pyspaya-1.2.0.tar.gz
Algorithm Hash digest
SHA256 6eeaf9d3c8de8c0b1f69972efccf2cefb393f2b652afb4ba6b6d715d73e8ec07
MD5 b2ffa1aad6c3375316c5477004b00b1b
BLAKE2b-256 622db98082db40e18edcac573956533a1794fe2c0202e5185afd1e2a20560005

See more details on using hashes here.

File details

Details for the file pyspaya-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyspaya-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pyspaya-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f966a8bc355ddd70ece5430686cd63cbc82d8d255782409175b78d5bd984d64
MD5 ba3165f637ec7bf178b9a8d29a924cac
BLAKE2b-256 708c2a258e9e6a4509e8fc1abcbb42c0500c0ac81f0efbbfc8dc50644f87fcaf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page