forked from Pymol-Scripts/Pymol-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poseview.py
101 lines (69 loc) · 2.71 KB
/
poseview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'''
http://pymolwiki.org/index.php/PoseView
(c) 2012 Thomas Holder, MPI for Developmental Biology
License: BSD-2-Clause
'''
from __future__ import print_function
from pymol import cmd, CmdException
def poseview(ligand='organic inorganic', protein='polymer', width=0, height=0,
filename='', exe='poseview', state=-1, quiet=1):
'''
DESCRIPTION
PoseView wrapper
http://www.biosolveit.de/poseview/
USAGE
poseview [ ligand [, protein [, width [, height [, exe [, state ]]]]]]
ARGUMENTS
ligand = string: atom selection {default: organic inorganic}
protein = string: atom selection {default: polymer}
width = int: image width {default: viewport width}
height = int: image height {default: viewport height}
filename = string: output PNG file name {default: temporary}
exe = string: path to executable {default: poseview}
SETUP
1) Put poseview executable to PATH (e.g. /usr/bin/poseview)
2) Set environment variable BIOSOLVE_LICENSE_FILE="/path/to/poseview.lic"
'''
import tempfile
import subprocess
import os
import shutil
width, height = int(width), int(height)
state, quiet = int(state), int(quiet)
if width == 0 or height == 0:
viewport = cmd.get_viewport()
if width != 0:
height = int(viewport[0] / float(width) * viewport[1])
elif height != 0:
width = int(viewport[1] / float(height) * viewport[0])
else:
width, height = viewport
exe = cmd.exp_path(exe)
tempdir = tempfile.mkdtemp()
try:
ligand_filename = os.path.join(tempdir, 'ligand.sdf')
protein_filename = os.path.join(tempdir, 'protein.pdb')
if not filename:
filename = os.path.join(tempdir, 'image.png')
cmd.save(ligand_filename, ligand, state)
cmd.save(protein_filename, protein, state)
args = [exe, '-l', ligand_filename, '-p', protein_filename, '-t', '',
'-o', filename, '-s', str(width), str(height)]
if not quiet:
print(' poseview: running...')
process = subprocess.Popen(args,
stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
stdout, _ = process.communicate()
if not quiet:
print(stdout)
print(' poseview: done')
if filename.endswith('.png'):
cmd.load(filename)
elif not quiet:
print(' Warning: cannot load "%s" into PyMOL, unsupported file type' % (filename))
except OSError:
print(' Error: Cannot execute "%s", please provide full path to poseview executable' % (exe))
raise CmdException
finally:
shutil.rmtree(tempdir)
cmd.extend('poseview', poseview)