-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
260 lines (240 loc) · 11 KB
/
Copy pathsetup.py
File metadata and controls
260 lines (240 loc) · 11 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import multiprocessing.pool
import shutil
from pathlib import Path
from typing import Optional,Any,Sequence
import subprocess
import argparse
import sys
import logging
import multiprocessing
from itertools import product
ROOT_DIR = Path(__file__).absolute().parent
try:
import submodules.RNALoops.Misc.Applications.RNAmotiFold.motifs.get_RNA3D_motifs as motifs
except ImportError as e:
print(f"Submodule was not correctly cloned. If you didn't clone this repo with --recurse-submodules run git submodule update --init --recursive from {ROOT_DIR}")
raise e
logger = logging.getLogger("RNAmotiFold")
def get_cmd_args():
"""Contains cmd_argument parsing solely for the purpose of checking if an already installed gapc is given"""
parser = argparse.ArgumentParser(
prog="SetUp.py",
description="Set up script for RNAmotiFold. Checks if a modified Bellman's GAP compiler is installed and prepares algorithms.",
epilog="Does anyone read these anyways?",
)
parser.add_argument(
"--cmake_path",
nargs="?",
dest="cmake_path",
default=shutil.which("cmake"),
type=str,
help="If you don't have cmake installed globally or are using a specific CMake version you can set the path here. Default is the cmake version return by 'which cmake'"
)
parser.add_argument(
"--gapc_path",
nargs="?",
action=preinstalled_check,
dest="preinstalled_gapc_path",
default=None,
type=str,
help="You may input the absolute path to a preinstalled gapcM version. If you don't the script will check if there is already a gapc installed (globally or locally) and if it isn't it will run a CMake Script to set it up locally.",
)
parser.add_argument(
"--perl_path",
nargs="?",
dest="perl_path",
default=shutil.which("perl"),
type=str,
help="if you have an alternative perl interpreter you can set the path to it here. Otherwise this script will use the one returned by which perl.",
)
parser.add_argument(
"-v",
"--version",
help=f"Specify which RNA 3D Motif sequence version you want to use. Default is the newest version.",
dest="version",
type=str,
default="current",
)
parser.add_argument(
"-w",
"-workers",
type=int,
dest="workers",
default=5,
help="Specify how many parallel processes may be spawned to speed up algorithm compilation. Default is 5.",
)
args = parser.parse_known_args()
return args[0]
class preinstalled_check(argparse.Action):
def __init__(self, option_strings:str, dest:str, **kwargs:Any):
super().__init__(option_strings, dest, **kwargs)
def __call__(self, parser:argparse.ArgumentParser, namespace:argparse.Namespace, value: Optional[str|Sequence[Any]], option_string:Optional[str]=None):
if value is None:
setattr(namespace, self.dest, value)
elif isinstance(value,str):
if Path(value).is_file():
try:
version_check = subprocess.run(
[f"{value}", "--version"], capture_output=True, check=True
)
except (subprocess.CalledProcessError, PermissionError) as error:
raise RuntimeError(
"Unable to open file, check the above error for more information."
) from error
else:
if version_check.stdout.decode()[:4] == "gapc":
setattr(namespace, self.dest, Path(value))
else:
raise RuntimeError(
"The given file is not an instance of the modified Bellman's GAP compiler."
)
else:
raise FileNotFoundError("The given file does not exist.")
else:
raise ValueError("Why is my value a Sequence ?")
# Checks if gapc is installed with which or locally
def _detect_gapc() -> Path|None:
"""Checks for a gapc installation with which and globs RNAmotiFold folder for any gapc instance (which is presumed to be a modified gapc, if you have a different gapc in here that's on you)"""
global_gapc = shutil.which("gapc")
if global_gapc is not None:
return Path(global_gapc)
else:
local_gapc = list(ROOT_DIR.glob("**/bin/gapc"))
try:
return local_gapc[0]
except IndexError:
return None
def setup_algorithms(gapc_path: Path, perl_path: Path, poolboys: int) -> bool:
RNALOOPS_PATH = _check_submodule("RNALoops")
RNAMOTIFOLD_BIN = Path.joinpath(ROOT_DIR, "Build", "bin")
RNAMOTIFOLD_BIN.mkdir(exist_ok=True, parents=True)
compilation_list:list[str] = []
algorithms = [
"".join(x)
for x in list(product(["RNAmotiFold", "RNAmoSh", "RNAmotiCes"], ["","Motmicro","_motmacro_pfc","_motmacro_subopt", "_subopt", "_pfc"]))
]
for algorithm in algorithms:
if "_" in algorithm: #There are no motmicro versions of subopt or pfc because of equal structures with different energies in Microstate, see paper Lost in Folding space for details
options = "-t"
compilation = f'{Path.joinpath(RNALOOPS_PATH,"Misc","Applications","RNAmotiFold","compile.sh")} GAPC="{gapc_path}" ALG="{algorithm}" ARGS="{options}" FILE="RNAmotiFold_subopt_pfc.gap" PERL="{perl_path}" && cd {RNALOOPS_PATH} && mv {algorithm} {RNAMOTIFOLD_BIN}'
else:
options = "-t --kbacktrace --kbest --no-coopt-class"
compilation = f'{Path.joinpath(RNALOOPS_PATH,"Misc","Applications","RNAmotiFold","compile.sh")} GAPC="{gapc_path}" ALG="{algorithm}" ARGS="{options}" FILE="RNAmotiFold.gap" PERL="{perl_path}" && cd {RNALOOPS_PATH} && mv {algorithm} {RNAMOTIFOLD_BIN}'
compilation_list.append(compilation)
The_Pool = multiprocessing.Pool(processes=poolboys)
joblist:list[multiprocessing.pool.AsyncResult[bool]]=[]
compilation_success_list:list[bool] = []
for job in compilation_list:
obj = The_Pool.apply_async(work_func, (job,))
joblist.append(obj)
The_Pool.close()
The_Pool.join()
for obj in joblist:
compilation_success_list.append(obj.successful())
return all(compilation_success_list)
def work_func(call:str):
try:
subprocess.run(call, shell=True, check=True)
return True
except subprocess.CalledProcessError as error:
raise error
def _check_submodule(submodule: str) -> Path:
SUBMOD_DIR = Path.joinpath(ROOT_DIR, "submodules", f"{submodule}")
if len(list(SUBMOD_DIR.glob("*"))) == 0:
raise ModuleNotFoundError(
f"Submodule was not correctly cloned. If you didn't clone this repo with --recurse-submodules run git submodule update --init --recursive from {ROOT_DIR}"
)
else:
return SUBMOD_DIR
def run_cmake(cmake_path:Optional[str]) -> Path:
if cmake_path is None:
raise FileNotFoundError("CMake was not found, please install it or set the path with --cmake_path")
BUILD_PATH = Path.joinpath(ROOT_DIR, "Build")
BUILD_PATH.mkdir(exist_ok=True)
try:
build_process = subprocess.run(
f"{cmake_path} ..",
shell=True,
check=True,
stdout=sys.stdout,
stderr=sys.stdout,
cwd=BUILD_PATH,
)
except subprocess.CalledProcessError as error:
print("Error during CMake configuration, exiting...")
raise error
try:
build_process = subprocess.run(
f"{cmake_path} --build .",
shell=True,
check=True,
stdout=sys.stdout,
stderr=sys.stdout,
cwd=BUILD_PATH,
)
except subprocess.CalledProcessError as error:
print("Error during CMake building, exiting...")
raise error
if not build_process.returncode:
return Path.joinpath(BUILD_PATH, "gapc-prefix", "bin", "gapc")
raise RuntimeError(f"Could not build RNAmotiFold, something went wrong: {build_process.stderr}")
# Does all the updating with tradeoffs between update algorithms and no update
def updates(RNAmotiFold_paramteres:list[str],motif_version: str, workers: int) -> bool:
update_parser = argparse.ArgumentParser()
update_parser.add_argument('--perl_path',default=None,dest='perl')
update_parser.add_argument('--gapc_path',default=None,dest='gapc')
namespace = update_parser.parse_args(RNAmotiFold_paramteres)
update = motifs._uninteractive_update(version=motif_version) #type:ignore
if update:
if namespace.perl:
perl_interpreter = namespace.perl
else:
perl_interpreter = shutil.which("perl")
if perl_interpreter is None:
print(
"Could not find a perl interpreter, please input path to your perl interpreter: ",
end="",
)
perl_interpreter = Path(input())
if namespace.gapc:
gapcM_path = namespace.gapc
else:
gapcM_path = _detect_gapc()
if gapcM_path is None:
print(
"Could not find gapc, please enter path to your gapcM executeable: ", end=""
)
gapcM_path = Path(input())
setup_algorithms(perl_path=perl_interpreter, gapc_path=gapcM_path, poolboys=workers)
return True
else:
return False
def main():
"""main setup function that checks for the gap compiler, installs it if necessary, fetches newest motif sequences and (re)compiles all preset algorithms (RNAmotiFold, RNAmoSh, RNAmotiCes)"""
args = get_cmd_args()
done:bool=False
if args.preinstalled_gapc_path is None:
print(
"No preinstalled gap compiler set in commandline, checking with which and searching RNAmotiFold folder..."
)
auto_gapc_path = _detect_gapc()
if auto_gapc_path is None:
print("No installed gapc found, installing...")
cmake_generated_gapc_path = run_cmake(args.cmake_path) #type:ignore
print("gap compiler installed, installing algorithms...")
motifs._uninteractive_update(args.version) #type:ignore
done=setup_algorithms(cmake_generated_gapc_path, args.perl_path, args.workers)
else:
print(f"gap compiler found in {auto_gapc_path}. Using it to set up algorithms...")
motifs._uninteractive_update(args.version) #type:ignore
done=setup_algorithms(auto_gapc_path, args.perl_path, args.workers)
else:
print("Preinstalled gap compiler given, using it to install RNAmotiFold...")
motifs._uninteractive_update(args.version) #type:ignore
done=setup_algorithms(args.preinstalled_gapc_path, args.perl_path, args.workers)
if done:
print("Algorithms are all set up, you can now use RNAmotiFold")
else:
print("Something went wrong compiling the RNAmotiFold algorithms, please check outputs")
if __name__ == "__main__":
main()