0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AutoDock4をdockerで実行する

0
Last updated at Posted at 2026-09-28

最近のMacOSではライブラリエラーが発生するためにAutoDock4.2を使用することができず、このためにDockerのコンテナbiocontainers/autogridとbiocontainers/autodockを用いてドッキングシミュレーションを行うのですが、

  • GPF、DPFファイルを作る
  • PDBQTファイルを作る

のにAutoDockToolsを使うとなるとそのコンテナも必要・・・と大変面倒くさいので、
ワンラインコマンドでAutoDock4の入力ファイルの作成、実行、結果の出力ができるツールを作りました。
MacOS 14、Windows 11上のWSLで動作確認しています。

必要なdockerコンテナ

使い方

あらかじめ、パスの通ったディレクトリにautodock-extract(以下)を配置してください。

#!/usr/local/bin/python
""" autodock-extract """
import re
import os
import sys
import subprocess

INFILE=os.path.splitext(os.path.split(sys.argv[1])[-1])[0] # (basename ext)
with open(INFILE+".dlg", encoding="utf-8") as fp:
    lines=fp.read().splitlines()
with open("_split.sh", "w", encoding="utf-8") as fp:
    fp.write(f```
grep -e '^DOCKED' {INFILE}.dlg | cut -c9- > .tmp_{INFILE}.pdbqt
cut -c-66 .tmp_{INFILE}.pdbqt > .tmp_{INFILE}.pdb
a=$(grep ENDMDL .tmp_{INFILE}.pdb | wc -l)
b=$((a-2))
csplit -k -s -n 3 -f {INFILE}. .tmp_{INFILE}.pdb '/^ENDMDL/+1' '{{'$b'}}'
for f in {INFILE}.[0-9][0-9][0-9]; do grep -v USER $f > $f.pdb; rm $f; done
rm .tmp_{INFILE}.pdbqt
rm .tmp_{INFILE}.pdb
```)
subprocess.run('sh _split.sh', check=True, shell=True)
os.remove("_split.sh")
if os.path.exists(f"{INFILE}.000.pdb") is False:
    sys.exit()

#Output Energy statistics to $INFILE.csv

DOCKED=[]
for l in lines:
    if l.startswith("DOCKED") is True:
        DOCKED.append(l)

print("Conformation,Docking,Intermolecular,Intramolecular,Tortional,Unbound")

for _line in DOCKED:
    if "DOCKED: MODEL" in _line:
        num = int(re.split(" +", _line)[2])-1
        if num < 10:
            num = "00"+str(num)
        elif num < 100:
            num = "0" +str(num)

    if "pdbqt" in _line:
    # Docking Energy
        ligand = _line.replace(".pdbqt","").split(" move ")[-1]
        os.rename(f"{INFILE}.{num}.pdb", f"{ligand}.{num}.pdb")
    elif "Estimated Free Energy of Binding" in _line:
    # Docking Energy
        a = _line.split("=")[1]
        match = re.search(r"([+-][0-9.]+) kcal", a)
        dock = match.group(1)
    elif "Final Intermolecular Energy" in _line:
    # Intermolecular Energy
        a = _line.split("=")[1]
        match = re.search(r"([+-][0-9.]+) kcal", a)
        inter = match.group(1)
    elif "Final Total Internal Energy" in _line:
    # Intramolecular Energy
        a = _line.split("=")[1]
        match = re.search(r"([+-][0-9.]+) kcal", a)
        intra = match.group(1)
    elif "Torsional Free Energy" in _line:
    # Tortional Energy
        a = _line.split("=")[1]
        match = re.search(r"([+-][0-9.]+) kcal", a)
        tor =match.group(1)
    elif "Unbound System's Energy" in _line:
    # Unbound Energy
        a = _line.split("=")[-1]
        match = re.search(r"([+-][0-9.]+) kcal", a)
        unbound = match.group(1)
    elif "ENDMDL" in _line:
        # Unbound Energy
        print(f"{ligand}.{num}.pdb,{dock},{inter},{intra},{tor},{unbound}")

続いて、autodock-docker-batch.pyを任意の場所に配置します。

#!/usr/local/bin/python
""" autocdock-batch for docker """
import os
import sys
import re
import argparse
import subprocess
parser=argparse.ArgumentParser()
parser.add_argument("receptorfile", type=str,help="receptor pdb file")
parser.add_argument("ligandfile", type=str,help="ligand pdb file")
parser.add_argument("out_basename", type=str, help="output basename")
parser.add_argument("-gc", type=str, help="gridcenter x,y,z [angstrom]", default="auto")
parser.add_argument("-gn", type=str, help="npts x,y,z [x0.375 angstrom]")
parser.add_argument("-n", type=int, help="number of docking runs [10]", default=10)

args=parser.parse_args()
# file existence check
if os.path.exists(args.receptorfile) is False or args.receptorfile.endswith(".pdb") is False:
    print(f"error: receptorfile {args.receptorfile} is not found.")
    sys.exit()
if os.path.exists(args.ligandfile) is False or args.ligandfile.endswith(".pdb") is False:
    print(f"error: ligandfile {args.receptorfile} is not found.")
    sys.exit()
PYTHONSH="/home/mgltools/programs/mgltools_x86_64Linux2_1.5.6/bin/pythonsh"
BINDIR="/home/mgltools/programs/mgltools_x86_64Linux2_1.5.6/MGLToolsPckgs/AutoDockTools/Utilities24/"
REC_PDB=args.receptorfile
REC_PDBQT=args.receptorfile[:-4]+ ".pdbqt"
LIG_PDB=args.ligandfile
LIG_PDBQT=args.ligandfile[:-4]+ ".pdbqt"
pwd=os.getcwd()
CMD1=["docker", "run", "-ti", "--rm", "-v", f"{pwd}:/data", "kittyjosh111/mgltools:1.5.6",
      PYTHONSH, f"{BINDIR}/prepare_receptor4.py", "-r", REC_PDB, "-o", REC_PDBQT]
CMD2=["docker", "run", "-ti", "--rm", "-v", f"{pwd}:/data", "kittyjosh111/mgltools:1.5.6",
      PYTHONSH, f"{BINDIR}/prepare_ligand4.py", "-l", LIG_PDB, "-o", LIG_PDBQT]

subprocess.run(CMD1, check=True)
subprocess.run(CMD2, check=True)
#dockerを使えば、コマンドラインでdpf, gpfの生成も可能。
#make dpf,gpf
### 0928 おそらく、*.pyに渡しているオプションがdockerに対するものと勘違いされて警告が出ている
#CMD3=["docker", "run", "--rm", f"{pwd}:/data", "kittyjosh111/mgltools:1.5.6",
#      PYTHONSH, f"{BINDIR}/prepare_gpf4.py", "-r", REC_PDBQT, "-l", LIG_PDBQT,
#      "-o", f"{args.out_basename}.gpf"]
CMD3=f"docker run --rm -v {pwd}:/data kittyjosh111/mgltools:1.5.6 sh -c '{PYTHONSH} {BINDIR}/prepare_gpf4.py -r {REC_PDBQT} -l {LIG_PDBQT} -o {args.out_basename}.gpf "
### gridcenter, nptsをgpf fileに流し込む
if args.gc != "auto":
    if re.match(r"^[0-9.]+,[0-9.]+,[0-9.]+$", args.gc) is False:
        print("ERROR: invgalid gridcenter setting")
        sys.exit()
    _t=args.gc.split(",")
#    CMD3 += ["-x",_t[0], "-y", _t[1], "-z", _t[2]]
    CMD3 += f"-p gridcenter={args.gc}"
else:
    print("ERROR: no gridcenter specified.")
    sys.exit()
#    CMD3 += ["-x", "0", "-y", "0", "-z", "0"]
#    CMD3 += " -x 0 -y o -z 0"
    pass
if args.gn:
    if re.match(r"^[0-9.]+,[0-9.]+,[0-9.]+$", args.gn) is False:
        print("ERROR: invgalid npts setting")
        sys.exit()
    _t=args.gn.split(",")
#    CMD3 += ["-p", f"npts={args.gn}"]
    CMD3 += f" -p npts={args.gn}"
else:
#    CMD3 += ["-p", f"npts=40,40,40"]
    CMD3 += " -p npts=40,40,40"
CMD3 += "'"
CMD4=["docker", "run", "--rm", "-v", f"{pwd}:/data", "kittyjosh111/mgltools:1.5.6",
      PYTHONSH, f"{BINDIR}/prepare_dpf4.py", "-r", REC_PDBQT, "-l", LIG_PDBQT, "-o",
      f"{args.out_basename}.dpf"]
if args.n:
    CMD4 += ["-p", f"ga_run={args.n}"]
print("making gpf")
print(CMD3)
subprocess.run(CMD3, check=True, shell=True) # capture_output=Trueで警告出力を抑制
print("making dpf")
print(CMD4)
subprocess.run(CMD4, check=True)

### gridcenter, nptsをgpf fileに流し込む
if args.gc:
    if re.match(r"^[0-9.]+,[0-9.]+,[0-9.]+$", args.gc) is False:
        print("ERROR: invgalid gridcenter setting")
        sys.exit()
else:
    args.gc="160 10 10"
if args.gn:
    if re.match(r"^[0-9.]+,[0-9.]+,[0-9.]+$", args.gn) is False:
        print("ERROR: invgalid npts setting")
        sys.exit()
else:
    args.gn="40 40 40"

# python gpfset.py -n [npts: nx,ny,nz] -c [gridcenter: x,y,z] xxx.gpf > xxx2.gpf
# CMD5, CMD6をlistにすると動かなかった
CMD5=f"docker run --rm -v {pwd}:/data -w /data biocontainers/autogrid:v4.2.6-6-deb_cv1 " + \
     f"sh -c 'autogrid4 -p {args.out_basename}.gpf -l {args.out_basename}.glg'"
CMD6=f"docker run --rm -v {pwd}:/data -w /data biocontainers/autodock:v4.2.6-6-deb_cv1 " + \
     f"sh -c 'autodock4 -p {args.out_basename}.dpf -l {args.out_basename}.dlg'"
print("autogrid4")
subprocess.run(CMD5, check=True, shell=True) # shell=True 必要
print("autodock4")
subprocess.run(CMD6, check=True, shell=True) # shell=True 必要
subprocess.run(f"python autodock-extract.py {args.out_basename}.dlg > {args.out_basename}.csv", \
    check=True, shell=True)
subprocess.run(f"cat {LIG_PDB[:-4]}.*.pdb > {LIG_PDB[:-4]}-ALL.pdb", \
    check=True, shell=True)

-h オプションで使い方を表示できます。

$ python autodock-batch-docker.py -h<br>
usage:
 autodock-batch-docker.py
 [-h]
 [-gc GC]
 [-gn GN]
 [-n N]
 receptorfile ligandfile out_basename

positional arguments:
  receptorfile  receptor pdb file
  ligandfile &nbsp;  ligand pdb file
out_basename  output basename

options:
  -h, --help   show this help message and exit
  -gc GC       gridcenter x,y,z [angstrom]
  -gn GN       npts x,y,z [points = x0.375 angstrom]
  -n N         number of docking runs [10]

やっていること

  1. receptor.pdbからreceptor.pdbqt を生成 (prepare_receptor4.py)
  2. ligand.pdbからligand.pdbqt を生成 (prepare_ligand4.py)
  3. 引数のグリッド設定からsystem.gpf を生成 (prepare_gpf4.py)
  4. system.dpf を生成 (prepare_dpf4.py)
  5. autogrid4 プログラムを実行 (autogrid4)
  6. autodock4 プログラムを実行 (autodock4)
  7. dlgファイルからドッキング結果のPDBファイルと、ドッキングスコアを記録したcsvファイルを生成 (autodock-extract)

MGLToolsの中には、AutoDockToolsというコマンドラインで動くスクリプト群が用意されており、これを利用しています。
prepare_***スクリプトはpythonで書かれていますが、普通のインタプリタでは実行できません。MGLToolsに付属のpythonshプログラムが必要です。

実行

カレントディレクトリにreceptor.pdb, ligand.pdbを配置し下記を実行します。
引数の「15」はドッキング回数です(15回のドッキングを実行)

$ python autodock-batch-docker.py receptor.pdb ligand.pdb test -n 15 -gc 0,0,0 -gn 40,40,40
setting PYTHONHOME environment
adding gasteiger charges to peptide
setting PYTHONHOME environment
making gpf
setting PYTHONHOME environment
making dpf
setting PYTHONHOME environment
autogrid4
autodock4
autodock4: WARNING: Using autodock4.0 unbound extended model in autodock4.2!

以上が実行された後、カレントディレクトリにtest.xxx.pdb、test.csvが保存されます。

終わりに

nvidiaが公開している、GPU対応AutoDock4を使えば高速化できるかもしれません(やるとは言っていない)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?