Package: pyspaya
pyspayaDescriptionSpaya APIRetrosynthesis parametersScoreClientsRESTAsyncCallbackUrl and AuthorizationDescriptionSpaya APIThis 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:Spaya PublicSpaya APISpaya REST documentationSpaya Websocket documentationScoreEach scored SMILES return a RetrosynthesisResult:fromtypingimportDictfromiktos.spayaimportBearerToken,SpayaClientREST,RetrosynthesisResult# Create a clientclient=SpayaClientREST(url="https://spaya.ai",authorization=BearerToken(token="YourToken"))# Start a retrosynthesis and wait for the resultsresult:Dict[str,RetrosynthesisResult]=client.score_smiles(smiles=["c1ccn2nccc2c1"])# Show resultsretro_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}")ClientsTo match every case, this API provides 3 different clients:SpayaClientREST: A synchronous clientSpayaClientAsync: An asynchronous client with reconnection and helpers for the websocketSpayaClientCallback: An asynchronous client that allows you to get results using a CallbackRESTSpayaClientRESTsends SMILES synchronously to be scoredExample using a list of SMILES:fromiktos.spayaimportBearerToken,SpayaClientREST# Create client with authorizationclient=SpayaClientREST(url="https://spaya.ai",authorization=BearerToken("myT0ken"))# Start a retrosynthesis and wait for the resultsscored_smiles=client.score_smiles(smiles=["O=C1CCCCO1","O=C1CCCNN1",])# Show the resultsforsmiles,resultinscored_smiles.items():print(f"{smiles}:{result.rscore}/{result.nb_steps}")Example using a DataFrame:fromiktos.spayaimportBearerToken,SpayaClientRESTfrompandasimportDataFrame# Create client with authorizationclient=SpayaClientREST(url="https://spaya.ai/",authorization=BearerToken("myT0ken"))# Start a retrosynthesis and wait for the results with a dataframedf=DataFrame({"input_smiles":["O=C1CCCCO1","O=C1CCCNN1",]})df=client.score_smiles(smiles=df,dataframe_smiles_column="input_smiles",callback_progression=lambdap:print(f"progression:{p}"))# Show the resultsprint(f"result:{df}")Example using a list of SMILES and consume function:fromiktos.spayaimportBearerToken,SpayaClientREST# Create client with authorizationclient=SpayaClientREST(url="https://spaya.ai/",authorization=BearerToken("myT0ken"))# Start a retrosynthesisclient.start_retrosynthesis(smiles=["O=C1CCCCO1","O=C1CCCNN1",])# Consume the results as soon as possiblewhilenotclient.is_empty:forsmiles,resultinclient.consume():print(f"{smiles}{result}")AsyncSpayaClientAsyncsends SMILES asynchronously to be scoredimportasynciofromiktos.spayaimportBearerToken,SpayaClientAsyncasyncdefscore():asyncwithSpayaClientAsync(url="https://spaya.ai",authorization=BearerToken("myT0ken"))asclient:# Start scoring SMILESawaitclient.start_retrosynthesis(["O=C1CCCCO1","O=C1CCCNN1",])# Wait and print scores as soon as receivedasyncforsmiles,resultinclient.consume():print(f"{smiles}:{result.rscore}/{result.nb_steps}")asyncio.run(score())CallbackSpayaClientCallbackcalls a function as soon as a SMILES is scoredimportasynciofromiktos.spayaimport(BearerToken,SpayaClientCallback,RetrosynthesisResult)asyncdefgenerator(client:SpayaClientCallback):# Generate and start scoring SMILESforsmilesin["O=C1CCCCO1","O=C1CCCNN1"]:awaitclient.start_retrosynthesis(smiles)asyncdefcallback(smiles:str,result:RetrosynthesisResult):# Handle the resultsprint(f"{smiles}:{result.rscore}/{result.nb_steps}")asyncdefgenerate_and_score():asyncwithSpayaClientCallback(url="https://spaya.ai",authorization=BearerToken("myT0ken"),callback=callback)asclient:# Generate SMILESawaitgenerator(client)# Block until the endsawaitclient.wait_result()asyncio.run(generate_and_score())Url and AuthorizationTo access the Spaya API, you need to generate a token from theSpaya APIpage in your
Spaya accountUnless specified, use the BearerToken classBearer tokenThe server is protected by a tokenfromiktos.spayaimportBearerToken,SpayaClientRESTSpayaClientREST(url="https://spaya.ai",authorization=BearerToken(token="itb3cy0s..."))Custom Bearer tokenThe server is protected by a token and it uses a custom headerfromiktos.spayaimportCustomBearerToken,SpayaClientRESTSpayaClientREST(url="https://spaya.ai",authorization=CustomBearerToken(token="itb3cy0s...",header_key="X-Iktos-Authorization"))