forked from jkimlab/mySyntenyPortal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-from-qns
executable file
·166 lines (121 loc) · 4.76 KB
/
run-from-qns
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
#!/usr/bin/env python3
# import argparse
import argparse
import tempfile
import os
from pathlib import Path
import subprocess
from shutil import copyfile
from typing import List, Union
import sys
QNS_DIR = "qns"
def handle_parser():
"""
Load in Parser from QNS and add docker tag
"""
launcher_parser = argparse.ArgumentParser()
launcher_parser.add_argument("-d",
"--docker",
action="store_true",
help="Describes whether or not to use docker-compose to run qns.",
dest="docker")
launcher_parser.add_argument('--run-qns',
action='store_true',
default=False,
dest="run_qns")
launcher_parser.add_argument('--clean-qns-output',
action='store_true',
default=False,
dest="clean_qns_output")
launcher_parser.add_argument('-v', '--verbosity',
help=argparse.SUPPRESS,
action='store',
type=int,
dest='verbosity')
launcher_parser.add_argument('-i', '--source-dir',
action='store',
type=str,
dest='input_directory',
help="mySyntenyPortal input directory (qns output directory)",
default="qns_output")
return launcher_parser
def add_custom_args(qns_args: List[str], temp_output_folder: Path) -> List[str]:
"""
Removes qns parameters that the launcher handles and add needed parameters
Returns:
"""
# Tags to remove or modify are held as arguments to launcher parser
# They accept a value, so it's easiest to capture it so it doesn't get passed on to qns
qns_args = ['-m', '--export-config', '-v 0', f"-o {temp_output_folder}"] + qns_args
return qns_args
def run_command(parameters: Union[str, List[str]]):
if isinstance(parameters, str):
parameters = parameters.split()
process = subprocess.Popen(parameters, stdout=subprocess.PIPE)
output, error = process.communicate()
if not error:
return True, output
else:
return False, error
def determine_command(launcher_args, qns_args, temp_dir):
# Msg currently not being used. Maybe hookup with QNS logging.
msg = ""
cmd = ""
if launcher_args.docker:
msg += "Running QNS with Docker...\n"
cmd += "docker-compose -f docker-compose.yml run qns "
else:
msg += "Running QNS within venv...\n"
cmd += "./qns.py "
args_for_qns = add_custom_args(qns_args, temp_dir)
return cmd + " ".join(args_for_qns)
def move_files(dir):
"""
Move files from QNS ouput to MSP input
"""
qns_output = os.listdir(dir)
for file in qns_output:
file_path = dir + '/' + file
if file.endswith(".sizes") or file.endswith(".synteny"):
target = f"{root_dir}/data/example_inputs/" + file
copyfile(file_path, target)
print(f"Moving {file_path} to {target}")
elif file.endswith(".conf"):
target = f"{root_dir}/configurations/" + file
copyfile(file_path, target)
print(f"Moving {file_path} to {target}")
# Start Here
qns_found = os.path.isdir(QNS_DIR)
if not qns_found:
sys.stderr.write("QNS directory not found, aborting.\n")
sys.exit(1)
print(QNS_DIR)
root_dir = os.getcwd()
# with get_temp_dir() as temp_dir:
print(f"Current directory: {os.getcwd()}")
parser = handle_parser()
launcher_args, qns_args = parser.parse_known_args()
input_directory = os.path.join(root_dir, launcher_args.input_directory)
cmd = determine_command(launcher_args, qns_args, input_directory)
print (f"Making directory {input_directory}")
Path(input_directory).mkdir(parents=True, exist_ok=True)
if launcher_args.run_qns:
print(f"Chdir to {QNS_DIR}")
os.chdir(QNS_DIR)
print(f"Running QNS command: {cmd}")
sucess, output = run_command(cmd)
if not sucess:
print(f"Running QNS command failed. Output:")
print(output)
sys.exit(1)
else:
print(f"Running QNS command Succeeded.")
print(f"Current directory: {os.getcwd()}")
move_files(input_directory)
print("Check here for diagram: http://localhost:9090/mySyntenyPortal/htdocs/syncircos.php")
print("Running kick-off-docker.sh")
os.chdir(root_dir)
run_command(f"{root_dir}/kick-off-docker.sh")
if launcher_args.clean_qns_output:
run_command(f"rm -rf {input_directory}")
print(f"\nRemoved Cache stored at {input_directory}...")