-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseqc_submit_mjobs.py
More file actions
260 lines (196 loc) · 6.27 KB
/
seqc_submit_mjobs.py
File metadata and controls
260 lines (196 loc) · 6.27 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
#!/usr/bin/env python
import yaml
import argparse
import os
import sys
import json
import subprocess
import logging
logger = logging.getLogger()
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("submit.log"),
logging.StreamHandler(sys.stdout)
]
)
def run_command_detached(cmd):
process = subprocess.Popen(
cmd,
close_fds=True
)
return process
def run_command(cmd, path_log):
"run a command and return (stdout, stderr, exit code)"
with open(path_log, "wt") as flog:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False
)
for line in iter(process.stdout.readline, b''):
line = line.decode(sys.stdout.encoding).rstrip() + "\r"
# write to individual job log
flog.write(line)
flog.flush()
# write to submit.log
logger.info(line)
def submit_job(path_ec2_keypair, platform, params, path_log):
# construct SEQC run command
cmd = [
"./seqc-submit.sh", path_ec2_keypair, "run",
platform
]
# add the rest of the parameters
cmd.extend(params)
run_command(cmd, path_log)
def pretty_print(path_ec2_keypair, platform, params):
# e.g.
# SEQC run ten_x_v2 \
# --ami-id ${PLACE_AMI_ID_HERE} \
# --user-tags Job:2,Project:10178,Sample:DEV_IGO_00002 \
# --filter-mode snRNA-seq \
# --max-insert-size 2304700 \
# --index s3://seqc-public/genomes/hg38_long_polya/ \
# --barcode-files s3://seqc-public/barcodes/ten_x_v2/flat/ \
# --genomic-fastq s3://seqc-public/test/ten_x_v2/genomic/ \
# --barcode-fastq s3://seqc-public/test/ten_x_v2/barcode/ \
# --upload-prefix s3://dp-lab-home/chunj/seqc-test/ten_x_v2/seqc-results/ \
# --output-prefix test2 \
# --email jaeyoung.chun@gmail.com \
# --star-args runRNGseed=0 \
# --no-terminate
lines = list()
lines.append("SEQC run {}".format(platform))
tmp = ""
for param in params:
if str(param).startswith("--"):
if tmp:
lines.append(tmp)
tmp = ""
tmp = " {}".format(param)
else:
tmp += " {}".format(param)
# append those that are not added yet
if tmp:
lines.append(tmp)
return " \\\n".join(lines)
def translate_params_yaml_to_list(job):
platform = None
params = list()
for key, value in job.items():
if key == "job":
continue
elif key == "platform":
platform = value
continue
# `barcode-fastq` becomes `--barcode-fastq`
# so that python argparse can parse properly
params.append("--{}".format(key))
# `user-tags` requires a special treatment
if key == "user-tags":
if type(value) == str:
# backward compatibility
# if it's a string type, use as is
# e.g. Job:2,Project:00000,Sample:DEV_IGO_00002
params.append(value)
elif type(value) == dict:
# if it's a dictionary type, convert to a comma-separated key-value pair string
# e.g.
# user-tags:
# job: 1
# Project: Project_10178
# Sample: 1454_080119_CAFPDPN_P174_IGO_10178_21
#
# --> Job:1,Project:Project_10178,Sample:1454_080119_CAFPDPN_P174_IGO_10178_21
params.append(
",".join(list(f"{k}:{v}" for k, v in value.items()))
)
continue
# to support those arguments that do not have any values
# e.g. `--no-filter-low-coverage`
if value:
params.append(value)
# convert each in params to string
# so that later we can just pass to subprocess.Popen
params = list(map(lambda x: str(x), params))
return platform, params
def main(path_yaml_input, path_ec2_keypair, ec2_keypair_name, is_dry_run):
inputs = yaml.safe_load(open(path_yaml_input))
os.makedirs("logs", exist_ok=True)
for input in inputs['jobs']:
job_name = input["job"]
platform, params = translate_params_yaml_to_list(input)
path_log = os.path.join(
"./logs/", "{}.log".format(
job_name
)
)
logger.info(
f"JOB NAME={job_name}, LOG FILE={path_log}\n" +
pretty_print(
path_ec2_keypair,
platform,
params
)
)
# skip if dry run
if is_dry_run:
logger.info(
"No actual job submission because we're in dry run mode"
)
continue
# submit job
logger.info("Submitting a job...")
submit_job(
path_ec2_keypair,
platform,
params,
path_log
)
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"--config", "-c",
action="store",
dest="path_yaml_input",
help="path to jobs.yaml",
required=True
)
parser.add_argument(
"--pem", "-k",
action="store",
dest="path_ec2_keypair",
help="path to AWS EC key pair file (*.pem)",
required=True
)
parser.add_argument(
"--key-name", "-n",
action="store",
dest="ec2_keypair_name",
help="the name of your AWS EC2 key pair",
required=False
)
parser.add_argument(
"--dry-run",
action="store_true",
dest="is_dry_run",
help="Dry run (i.e. don't actually submit the job)"
)
# parse arguments
params = parser.parse_args()
# if ec2 key name not specified, extract from pem file name.
if not params.ec2_keypair_name:
params.ec2_keypair_name, _ = os.path.splitext(
os.path.basename(params.path_ec2_keypair)
)
return params
if __name__ == "__main__":
params = parse_arguments()
logger.info("Starting...")
main(
params.path_yaml_input,
params.path_ec2_keypair,
params.ec2_keypair_name,
params.is_dry_run
)
logger.info("DONE.")