-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrf-score-vs
executable file
·148 lines (130 loc) · 5.11 KB
/
rf-score-vs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import six
import gzip
from os.path import isfile, abspath, dirname, join as path_join
import argparse
import joblib
from six.moves import cPickle
import oddt
from oddt.scoring import scorer
# FIX Windows multiprocessing
# Module multiprocessing is organized differently in Python 3.4+
try:
# Python 3.4+
if sys.platform.startswith('win'):
import multiprocessing.popen_spawn_win32 as forking
else:
import multiprocessing.popen_fork as forking
except ImportError:
import multiprocessing.forking as forking
if sys.platform.startswith('win'):
# First define a modified version of Popen.
class _Popen(forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# We have to set original _MEIPASS2 value from sys._MEIPASS
# to get --onefile mode working.
os.putenv('_MEIPASS2', sys._MEIPASS)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
# On some platforms (e.g. AIX) 'os.unsetenv()' is not
# available. In those cases we cannot delete the variable
# but only set it to the empty string. The bootloader
# can handle this case.
if hasattr(os, 'unsetenv'):
os.unsetenv('_MEIPASS2')
else:
os.putenv('_MEIPASS2', '')
# Second override 'Popen' class with our modified version.
forking.Popen = _Popen
# END Fix Windows multiprocessing
import multiprocessing
__version__ = '1.0'
# arguments
parser = argparse.ArgumentParser(description='RF-Score-VS command line tools')
parser.add_argument('-n',
'--n_cpu',
dest='n_cpu',
type=int,
default=1,
help='The number of parallel processes. -1 automatically assigns maximum number of CPUs. (default=-1)')
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
# in/out files and formats
parser.add_argument('in_file', nargs='+', help='Input files of formats supported by toolkit.')
parser.add_argument('-i',
choices=['sdf', 'mol2', 'pdb', 'pdbqt'],
dest='in_format',
help='Input file(s) format')
parser.add_argument('-o',
choices=['csv', 'sdf', 'mol2', 'pdb', 'pdbqt'],
dest='out_format', help='Output file format')
parser.add_argument('-O', '--output', dest='out_file', help='Output file')
parser.add_argument('--receptor', help='Protein file')
parser.add_argument('--field',
dest='save_fields',
action='append',
default=[],
help='Field to save (eg. in CSV). Each field should be specified separately.')
if __name__ == '__main__':
multiprocessing.freeze_support()
args = parser.parse_args()
from oddt.virtualscreening import virtualscreening as vs
# Create pipeline for docking and rescoring
pipeline = vs(n_cpu=args.n_cpu if 'n_cpu' in args else 1)
for f in args.in_file:
if args.in_format:
fmt = args.in_format
else: # autodiscover
tmp = f.split('.')
if tmp[-1] == 'gz':
fmt = tmp[-2]
else:
fmt = tmp[-1]
if isfile(f):
pipeline.load_ligands(fmt, f) # add loading ligands from STDIN?
else:
raise IOError("File does not exist: '%s'" % f)
# load protein once
if args.receptor:
extension = args.receptor.split('.')[-1]
receptor = six.next(oddt.toolkit.readfile(extension, args.receptor))
if receptor:
receptor.protein = True
else:
raise Exception("Could not parse receptor file")
score_file = path_join(sys._MEIPASS if sys.frozen else dirname(abspath(__file__)), 'RFScoreVS_v2_vina.pickle')
if isfile(score_file): # load pickle
sf = scorer.load(score_file)
pipeline.score(sf, receptor)
else:
raise IOError('Could not read pickle file %s' % score_file)
# Write to file or STDOUT
if args.out_file:
if args.out_format:
fmt = args.out_format
else: # autodiscover
tmp = args.out_file.split('.')
if tmp[-1] == 'gz':
fmt = tmp[-2]
else:
fmt = tmp[-1]
if not fmt:
raise ValueError('No output format nor output file specified.')
if fmt == 'csv':
pipeline.write_csv(args.out_file, fields=args.save_fields)
else:
pipeline.write(fmt, args.out_file, overwrite=True)
else:
fmt = args.out_format
if not fmt:
raise ValueError('No output format nor output file specified.')
if fmt == 'csv':
pipeline.write_csv(sys.stdout, fields=args.save_fields)
else:
for lig in pipeline.fetch():
sys.stdout.write(lig.write(fmt))