You can use the ChemOffice ChemScript library for python to search structures on the SNB API.
ChemScript 22 supports python 3.9 and 3.10.
The following query will search through a Materials library, other query examples can be found on the article "Signals Notebook: Search chemical structures using the API"
{
"query": {
"$and": [
{
"$match": {
"field": "assetTypeEid",
"mode": "keyword",
"value": "assetType:YOUR_LIBRARY_EID"
}
},
{
"$chemsearch": {
"mime": "chemical/x-mdl-molfile",
"molecule": "YOURMOLFILE",
"options": "full=no,similar=no"
}
},
{
"$and": [
{
"$match": {
"field": "isMaterial",
"value": true
}
},
{
"$not": [
{
"$match": {
"field": "type",
"value": "assetType"
}
}
]
}
]
}
]
}
}
For the purpose of this example:
- The sdf file will be called structures.sdf
- The query file will be called query.json
- Your API key is added as text on the script headers, it's recommended your API key comes from a secure system variable instead.
The following script iterates through the sdf file, queries for any matches and returns a response:
from ChemScript import *
import requests
import json
baseURL = 'https://tenant.signalsresearch.revvitycloud.com/api/rest/v1.0/'
searchEndpoint = '/entities/search?page%5Boffset%5D=0&page%5Blimit%5D=100&source=SN'
sdf = SDFileReader.OpenFile('structures.sdf')
filetemplate = open('query.json', 'r')
querybody =filetemplate.read()
headers = {'x-api-key': 'YOUR_KEY',
'Content-Type': 'application/vnd.api+json',
'Accept-Encoding': 'gzip, deflate, br'}
structure = sdf.ReadNext()
while (structure != None):
mol = structure.WriteData('chemical/x-mdl-molfile')
mol = str(mol,'utf-8')
body = json.loads(querybody)
body['query']['$and'][1]['$chemsearch']['molecule'] = mol #insert the molfile into the query
response = requests.post(url=baseURL+searchEndpoint, json=body, headers = headers)
responsebody = json.loads(response.content)
print(responsebody['meta']) #do what you need to do with the response
structure = sdf.ReadNext()
This script will only return the first 100 results due to the limit on the endpoint, a paging method needs to be implemented if more than 100 results need to processed per request.
Comments
0 comments
Article is closed for comments.