-
Notifications
You must be signed in to change notification settings - Fork 2
/
opera_cli.py
154 lines (129 loc) · 5.04 KB
/
opera_cli.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
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
149
150
151
152
153
"""
Calling OPERA CLI from Python.
"""
import subprocess
import logging
import os
import sys
import tempfile
import uuid
import glob
import time
from opera_cli_args import CLIArgs
from opera_cli_results import OPERAResults
import libOPERA_Py as OPERA
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
logging.info("PROJECT_ROOT: {}".format(PROJECT_ROOT))
OPERA_EXE_PATH = os.path.join("c:", os.sep, "Program Files", "OPERA", "application")
if os.environ.get('OPERA_EXE_PATH'):
OPERA_EXE_PATH = os.environ.get('OPERA_EXE_PATH')
logging.info("OPERA_EXE_PATH: {}".format(OPERA_EXE_PATH))
IS_LINUX = False
if os.environ.get('IS_LINUX') == "True":
IS_LINUX = True
logging.info("IS_LINUX: {}".format(IS_LINUX))
MATLAB_RUNTIME_PATH = "/usr/local/MATLAB/MATLAB_Runtime/v94"
if os.environ.get('MATLAB_RUNTIME_PATH'):
MATLAB_RUNTIME_PATH = os.environ.get('MATLAB_RUNTIME_PATH')
logging.info("MATLAB_RUNTIME_PATH: {}".format(MATLAB_RUNTIME_PATH))
class OPERACLI(CLIArgs, OPERAResults):
def __init__(self):
CLIArgs.__init__(self)
OPERAResults.__init__(self)
self.cli_example_1 = "OPERA -s Sample_50.sdf -o predictions.csv -a -x -n -v 2"
self.cli_example_2 = "opera -d Sample_50.csv -o predictions.txt -e logP BCF -v 1"
self.opera_exe_location = OPERA_EXE_PATH
self.matlab_path = MATLAB_RUNTIME_PATH
self.smiles_file_location = PROJECT_ROOT
self.predictions_file_location = PROJECT_ROOT
self.smiles_full_path = "" # full path and filename for smiles input file
self.predictions_full_path = "" # full path and filename for predictions csv output
self.opera = OPERA.initialize() # initializes OPERA instance
def execute_opera_py(self, smiles_filename, predictions_filename):
"""
Executes libOPERA_Py.
"""
logging.warning("Executing libOPERA_Py library.")
self.opera.OPERA('-s', smiles_filename, '-o', predictions_filename, '-a') # TODO: Try specific props (is it faster?).
def execute_opera(self, smiles_filename, predictions_filename):
"""
Executes OPERA CLI.
"""
logging.warning("Executing OPERA.exe in Windows.")
subprocess.run([os.path.join(self.opera_exe_location, "opera"),
"-s", smiles_filename, # sets input smiles file
"-o", predictions_filename, # sets output csv file
# "-c", # cleans temp files from calculations
"-a"]) # gets all opera properties
# "-e", "HL", "LogP", "MP", "BP", "LogVP", "LogWS", "pKa", "LogD", "LogBCF", "LogKoc"])
def execute_opera_linux(self, smiles_filename, predictions_filename):
"""
Execute OPERA in linux docker container.
"""
logging.warning("Executing OPERA in Linux.")
subprocess.run(["sh", self.opera_exe_location,
self.matlab_path,
"-s", smiles_filename, # sets input smiles file
"-o", predictions_filename, # sets output csv file
"-a"]) # gets all opera properties
# "-e", "HL", "LogP", "MP", "BP", "LogVP", "LogWS", "pKa", "LogD", "LogBCF", "LogKoc"])
def create_smiles_tempfile(self, smiles_list):
"""
Creates smiles file of molecules to be predicted.
Inputs: smiles - list of SMILES.
Outputs: smiles tempfile name.
"""
# tempfile_obj = open("temp/" + self.generate_filename() + ".smi", "w")
tempfile_obj = open(os.path.join(PROJECT_ROOT, "temp", self.generate_filename() + ".smi"), "w")
smiles_string = ""
for smiles in smiles_list:
smiles_string += smiles + "\n"
smiles_string = smiles_string[:-1] # trims trailing newline
tempfile_obj.write(smiles_string)
tempfile_obj.close()
return tempfile_obj
def generate_filename(self):
"""
Generates a universally unique identifier for temp filenames.
"""
return str(uuid.uuid4())
def remove_temp_files(self, *args):
"""
Closes and removes input and output temp files.
"""
for tempfile in args:
tempfile.close()
os.remove(tempfile.name)
def remove_all_temp_files(self):
"""
Removes all temp files.
"""
for file in glob.glob("temp/*"):
os.remove(file)
def run_opera_routine(self, smiles_list, props_list=None):
"""
Runs OPERA CLI routine.
"""
try:
self.remove_all_temp_files()
smiles_tempfile = self.create_smiles_tempfile(smiles_list) # creates temp file for smiles
self.smiles_full_path = smiles_tempfile.name
self.predictions_full_path = os.path.join(PROJECT_ROOT, "temp", self.generate_filename() + ".csv")
logging.warning("Initiating OPERA CLI execution.")
self.execute_opera_py(self.smiles_full_path, self.predictions_full_path) # runs opera python lib
# self.execute_opera_linux(self.smiles_full_path, self.predictions_full_path) # runs opera python lib
logging.warning("Finished executing OPERA CLI.")
predictions_data = self.get_predictions(self.predictions_full_path) # gets predictions from .csv
self.remove_temp_files(smiles_tempfile)
return predictions_data
except Exception as e:
logging.warning("Exception running OPERA: {}".format(e))
self.remove_temp_files(smiles_tempfile)
pass
if __name__ == '__main__':
method = sys.argv[1]
if method == 'init':
# Dry run of OPERA
print("Running opera_cli.py 'init'.")
opera_cli = OPERACLI()
# opera_cli.execute_opera_py('tests/test_smiles.smi', 'test_results.csv')