From 44e7b9f3b8c4010710e606430602db972beeca44 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Wed, 7 Jun 2023 15:03:07 -0700 Subject: [PATCH] Code to calculate lddt --- foldingdiff/lddt.py | 32 ++++++++++++++++++++++++++++++++ scripts/run_docker_ost | 26 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 foldingdiff/lddt.py create mode 100755 scripts/run_docker_ost diff --git a/foldingdiff/lddt.py b/foldingdiff/lddt.py new file mode 100644 index 0000000..b1c852e --- /dev/null +++ b/foldingdiff/lddt.py @@ -0,0 +1,32 @@ +""" +Code for computing lDDT scores. +""" + +import os, sys +from pathlib import Path +import subprocess +import tempfile +import json + +IMAGE = "2d07309e7a56" # Docker image from https://git.scicore.unibas.ch/schwede/openstructure/container_registry/ + +DOCKER_OST = Path(os.path.realpath(__file__)).parent.parent / "scripts/run_docker_ost" +assert DOCKER_OST.exists(), f"Cannot find docker wrapper script {DOCKER_OST}" + + +def lddt(query: Path, ref: Path) -> float: + """Compute the lDDT between query and reference structures.""" + with tempfile.NamedTemporaryFile(dir=os.getcwd()) as outfile: + cmd = f"{DOCKER_OST} {IMAGE} compare-structures -m {query} -r {ref} --lddt -o {os.path.basename(outfile.name)}" + subprocess.call(cmd, shell=True) + + # outfile.seek(0) + data = json.loads(outfile.read().decode("utf-8")) + + if "lddt" in data: + return data["lddt"] + return -1.0 + + +if __name__ == "__main__": + print(lddt(Path(sys.argv[1]), Path(sys.argv[2]))) diff --git a/scripts/run_docker_ost b/scripts/run_docker_ost new file mode 100755 index 0000000..a55510c --- /dev/null +++ b/scripts/run_docker_ost @@ -0,0 +1,26 @@ +#!/bin/bash + +# end when error +set -e + +image_name=$1 +script_path=$2 + +if [[ ${#@} -lt 1 ]]; then + echo "Usage: run_docker_ost []" + exit 1 +fi + +if [[ -z ${script_path} ]]; then + docker run -ti --rm -v $(pwd):/home ${image_name} +else + if [[ -e $script_path ]]; then + abspath=$(readlink -f $script_path) + dirpath=$(dirname $abspath) + name=$(basename $script_path) + docker run --rm -v ${dirpath}:/home ${image_name} /home/${name} ${@:3} + else + # it is maybe an action if it does not exist + docker run --rm -v $(pwd):/home ${image_name} ${script_path} ${@:3} + fi +fi \ No newline at end of file