Example python code

The OpenStructure Actions API is intended to be used programatically for comparing structures, without the need to locally install OpenStructure.

Job submission and status checks are rate limited, if you send too many requests you will receive a 429 response. The results should indicate the current submission rate. The submission rate may change at any time, depending on demand of the service. Currently, the rapid submission rate is 100/m and the prolonged rate is set as 2000/6h .

The API uses a token based authentication system, so the first step is to retrieve a token for your SWISS-MODEL user account. This token is to be placed in the header of subsequent API calls.

1: Obtain a token

This can be done from the command line, but the recommended method to discover (and regenerate) your API token is to visit your SWISS-MODEL account page.

2: Start a comparison

import requests, time
token="YOURAPITOKEN"
base_url="https://swissmodel.expasy.org/ost"
No ligand comparison, use compare-structures action
with (
      open("model.pdb") as mdl, 
      open("reference.pdb") as ref
    ):
    response = requests.post(
        f"{base_url}/compare-structures",
        headers={ "Authorization": f"Token {token}" },
        files=[
          ("model", mdl), 
          ("reference", ref)
        ],
        # add optional parameters as data
        data={ "lddt": True, "local-lddt": True }    
    ) 
Compare ligands, use compare-ligand-structures action.
with (
      open("model.pdb") as mdl,
      open("reference.pdb") as ref,
      open("modelLigand1.sdf") as mdlLig1,
      open("modelLigand2.sdf") as mdlLig2,
      open("referenceLigand1.sdf") as refLig1,
      open("referenceLigand2.sdf") as refLig2
    ):
    response = requests.post(
        f"{ base_url }/compare-ligand-structures",
        headers={ "Authorization": f"Token {token}" },
        files=[
          ("model", mdl), 
          ("reference", ref),
          ("model-ligands", mdlLig1),
          ("model-ligands", mdlLig2),
          ("reference-ligands", refLig1),
          ("reference-ligands", refLig2)
        ],

        # add optional parameters as data
        data={ "lddt-pli": True, "rmsd": True },
    )
project_id = response.json()["project_id"]
print(project_id)

3: Fetch the results

# Obtain the project_id from the response created above
project_id = response.json()["project_id"]

# And loop until the project completes
while True:
    # We wait for some time
    time.sleep(10)

    # Fetch the results for this specific project_id 
    project_details = requests.get(
        f"{ base_url }/compare-results/?project_id={ project_id }", 
        headers={ "Authorization": f"Token {token}" }
        ).json()["projects"][0]

    print("Job status is now", project_details["status"])

    if project_details["status"] in ["COMPLETED", "FAILED"]:
        break

print(project_details["results"])