| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# |
| 4 |
# BEDOPS |
| 5 |
# Copyright (C) 2011-2025 Shane Neph, Scott Kuehn and Alex Reynolds |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along |
| 18 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
# |
| 21 |
|
| 22 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 23 |
|
| 24 |
import sys |
| 25 |
import os |
| 26 |
import argparse |
| 27 |
import errno |
| 28 |
import subprocess |
| 29 |
import random |
| 30 |
import string |
| 31 |
import logging |
| 32 |
|
| 33 |
name = "update-sort-bed-starch-slurm" |
| 34 |
citation = " citation: http://bioinformatics.oxfordjournals.org/content/28/14/1919.abstract" |
| 35 |
authors = " authors: Alex Reynolds and Shane Neph" |
| 36 |
version = " version: 2.4.42" |
| 37 |
usage = """ $ update-sort-bed-starch-slurm [ --slurm-memory <MB> ] |
| 38 |
[ --slurm-partition <SLURM partition> ] |
| 39 |
[ --slurm-workdir <working directory> ] |
| 40 |
[ --slurm-output <SLURM output directory> ] |
| 41 |
[ --slurm-error <SLURM error directory> ] |
| 42 |
[ --bedextract-path <path to bedextract> ] |
| 43 |
[ --sort-bed-path <path to sort-bed> ] |
| 44 |
[ --unstarch-path <path to unstarch> ] |
| 45 |
[ --starch-path <path to starch> ] |
| 46 |
[ --starchcat-path <path to starchcat> ] |
| 47 |
[ --debug ] |
| 48 |
--input-original <old-bed-file> |
| 49 |
--input-backup <renamed-old-bed-file> |
| 50 |
--output-temp <intermediate-new-bed-file> |
| 51 |
--output-final <new-bed-file>""" |
| 52 |
help = """ |
| 53 |
The 'update-sort-bed-starch-slurm' utility applies an updated sort order on |
| 54 |
Starch files sorted per pre-v2.4.20 sort-bed, using a SLURM job scheduler to |
| 55 |
coordinate resorting each chromosome in "--input-original" per post-v2.4.20 |
| 56 |
sort-bed and writing the result to a Starch file at "--output-final". |
| 57 |
|
| 58 |
When migration is finished, "--input-backup" specifies the new name of the |
| 59 |
original input file. |
| 60 |
|
| 61 |
As migration progresses, intermediate results are written to "--output-temp" |
| 62 |
and then written to "--output-final" upon completion. |
| 63 |
|
| 64 |
Each sort task is given 8 GB of memory and is assigned to the "queue0" |
| 65 |
partition, unless the "--slurm-memory" and "--slurm-partition" options are |
| 66 |
set. If your input is larger than 8 GB, you will need to allocate more |
| 67 |
memory. |
| 68 |
|
| 69 |
Because this launches all work on the specified cluster partition, the paths |
| 70 |
specified by "--input-original", "--input-backup", "--output-final", and |
| 71 |
"--output-temp" must be accessible to all computational nodes. For example, |
| 72 |
using /tmp may fail, as the /tmp path is almost certainly unique to a node; |
| 73 |
it is necesssary to use a path shared among all nodes. |
| 74 |
|
| 75 |
Note that this utility will not work on entirely unsorted BED files, but only |
| 76 |
on files with a sort order from pre-v2.4.20 sort-bed, where there are ties on |
| 77 |
the first three columns. |
| 78 |
|
| 79 |
In fact, until further refinements are made, this convenience utility could |
| 80 |
fail silently on inputs which are not BED, or which are not sorted per pre- |
| 81 |
v2.4.20 order, or which do not follow exact specification, all of which can |
| 82 |
lead a per-chromosome resort task to fail. |
| 83 |
""" |
| 84 |
|
| 85 |
def main(): |
| 86 |
slurm_memory = "8000" |
| 87 |
slurm_partition = "queue0" |
| 88 |
slurm_workdir = os.getcwd() |
| 89 |
slurm_output = None |
| 90 |
slurm_error = None |
| 91 |
|
| 92 |
# concatenation and other minor steps do not require much memory |
| 93 |
slurm_backup_input_memory = "500" |
| 94 |
slurm_concatenation_memory = "500" |
| 95 |
slurm_cleanup_memory = "500" |
| 96 |
slurm_output_move_memory = "500" |
| 97 |
|
| 98 |
parser = argparse.ArgumentParser(prog=name, usage=usage, add_help=False) |
| 99 |
parser.add_argument('--help', '-h', action='store_true', dest='help') |
| 100 |
parser.add_argument('--slurm-memory', '-m', type=str, action="store", dest='slurm_memory') |
| 101 |
parser.add_argument('--slurm-partition', '-p', type=str, action="store", dest='slurm_partition') |
| 102 |
parser.add_argument('--slurm-workdir', '-w', type=str, action="store", dest='slurm_workdir') |
| 103 |
parser.add_argument('--slurm-output', '-u', type=str, action="store", dest='slurm_output') |
| 104 |
parser.add_argument('--slurm-error', '-e', type=str, action="store", dest='slurm_error') |
| 105 |
parser.add_argument('--input-original', '-i', type=str, action="store", dest='input_original_fn') |
| 106 |
parser.add_argument('--input-backup', '-b', type=str, action="store", dest='input_backup_fn') |
| 107 |
parser.add_argument('--output-temp', '-t', type=str, action="store", dest='output_temp_fn') |
| 108 |
parser.add_argument('--output-final', '-o', type=str, action="store", dest='output_final_fn') |
| 109 |
parser.add_argument('--sort-bed-path', '-s', type=str, action="store", dest='sort_bed_path') |
| 110 |
parser.add_argument('--unstarch-path', '-a', type=str, action="store", dest='unstarch_path') |
| 111 |
parser.add_argument('--starch-path', '-c', type=str, action="store", dest='starch_path') |
| 112 |
parser.add_argument('--starchcat-path', '-z', type=str, action="store", dest='starchcat_path') |
| 113 |
parser.add_argument('--debug', '-d', action="store_true", dest='debug') |
| 114 |
args = parser.parse_args() |
| 115 |
|
| 116 |
if args.help or (not args.input_original_fn or not args.input_backup_fn or not args.output_temp_fn or not args.output_final_fn): |
| 117 |
sys.stdout.write(name + '\n') |
| 118 |
sys.stdout.write(citation + '\n') |
| 119 |
sys.stdout.write(version + '\n') |
| 120 |
sys.stdout.write(authors + '\n\n') |
| 121 |
sys.stdout.write(usage + '\n') |
| 122 |
sys.stdout.write(help) |
| 123 |
if args.help: |
| 124 |
sys.exit(os.EX_OK) |
| 125 |
else: |
| 126 |
sys.exit(errno.EINVAL) |
| 127 |
|
| 128 |
logger = None |
| 129 |
if args.debug: |
| 130 |
logging.basicConfig(level=logging.DEBUG) |
| 131 |
logger = logging.getLogger(__name__) |
| 132 |
|
| 133 |
if logger: logger.info('Locating original input file [%s]' % (args.input_original_fn)) |
| 134 |
if not os.path.exists(args.input_original_fn): |
| 135 |
if logger: logger.info('Not able to locate original input file') |
| 136 |
sys.stderr.write("ERROR: Input file [%s] does not exist\n" % (args.input_original_fn)) |
| 137 |
sys.exit(errno.EINVAL) |
| 138 |
|
| 139 |
if logger: logger.info('Locating final output file [%s]' % (args.output_final_fn)) |
| 140 |
if os.path.exists(args.output_final_fn): |
| 141 |
if logger: logger.info('Found output file [%s] which will be replaced' % (args.output_final_fn)) |
| 142 |
sys.stderr.write("Note: Output file [%s] exists and will be replaced\n" % (args.output_final_fn)) |
| 143 |
|
| 144 |
if logger: logger.info('Verfying that \"sbatch\" can be found') |
| 145 |
if not cmd_exists('sbatch'): |
| 146 |
sys.stderr.write("ERROR: This script must be run on a system with SLURM binaries available\n") |
| 147 |
sys.exit(errno.EEXIST) |
| 148 |
|
| 149 |
if logger: logger.info('Testing if original input and backup input file paths are equal') |
| 150 |
if args.input_original_fn == args.input_backup_fn: |
| 151 |
sys.stderr.write("ERROR: Input filename [%s] cannot be the same as the input backup filename [%s]\n" % (args.input_original_fn, args.input_backup_fn)) |
| 152 |
sys.exit(errno.EINVAL) |
| 153 |
|
| 154 |
if logger: logger.info('Testing if temporary output and final output file paths are equal') |
| 155 |
if args.output_temp_fn == args.output_final_fn: |
| 156 |
sys.stderr.write("ERROR: Output filename [%s] cannot be the same as the output temporary filename [%s]\n" % (args.output_final_fn, args.output_temp_fn)) |
| 157 |
sys.exit(errno.EINVAL) |
| 158 |
|
| 159 |
# parse args |
| 160 |
if args.slurm_memory: |
| 161 |
slurm_memory = args.slurm_memory |
| 162 |
if logger: logger.info('SLURM memory allocation set to [%s]' % (slurm_memory)) |
| 163 |
|
| 164 |
if args.slurm_partition: |
| 165 |
slurm_partition = args.slurm_partition |
| 166 |
if logger: logger.info('SLURM partition set to [%s]' % (slurm_partition)) |
| 167 |
|
| 168 |
if args.slurm_workdir: |
| 169 |
slurm_workdir = args.slurm_workdir |
| 170 |
if logger: logger.info('SLURM work directory set to [%s]' % (slurm_workdir)) |
| 171 |
|
| 172 |
if args.slurm_output: |
| 173 |
slurm_output = args.slurm_output |
| 174 |
if logger: logger.info('SLURM output log set to [%s]' % (slurm_output)) |
| 175 |
|
| 176 |
if args.slurm_error: |
| 177 |
slurm_error = args.slurm_error |
| 178 |
if logger: logger.info('SLURM error log set to [%s]' % (slurm_error)) |
| 179 |
|
| 180 |
if not slurm_output: |
| 181 |
slurm_output = slurm_workdir |
| 182 |
if logger: logger.info('SLURM output log reassigned to [%s]' % (slurm_output)) |
| 183 |
|
| 184 |
if not slurm_error: |
| 185 |
slurm_error = slurm_workdir |
| 186 |
if logger: logger.info('SLURM error log reassigned to [%s]' % (slurm_error)) |
| 187 |
|
| 188 |
sort_bed_path = None |
| 189 |
if logger: logger.info('Locating \"sort-bed\" binary') |
| 190 |
if not args.sort_bed_path: |
| 191 |
sort_bed_path = find_binary('sort-bed') |
| 192 |
if not sort_bed_path: |
| 193 |
if logger: logger.info('Could not locate \"sort-bed\" binary') |
| 194 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS sort-bed\n") |
| 195 |
sys.exit(errno.EEXIST) |
| 196 |
elif not cmd_exists('sort-bed'): |
| 197 |
if logger: logger.info('Could not locate \"sort-bed\" binary') |
| 198 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS sort-bed\n") |
| 199 |
sys.exit(errno.EEXIST) |
| 200 |
else: |
| 201 |
sort_bed_path = args.sort_bed_path |
| 202 |
if logger: logger.info('Location of \"sort-bed\" is set to [%s]' % (sort_bed_path)) |
| 203 |
|
| 204 |
unstarch_path = None |
| 205 |
if logger: logger.info('Locating \"unstarch\" binary') |
| 206 |
if not args.unstarch_path: |
| 207 |
unstarch_path = find_binary('unstarch') |
| 208 |
if not unstarch_path: |
| 209 |
if logger: logger.info('Could not locate \"unstarch\" binary') |
| 210 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS unstarch\n") |
| 211 |
sys.exit(errno.EEXIST) |
| 212 |
elif not cmd_exists('unstarch'): |
| 213 |
if logger: logger.info('Could not locate \"unstarch\" binary') |
| 214 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS unstarch\n") |
| 215 |
sys.exit(errno.EEXIST) |
| 216 |
else: |
| 217 |
unstarch_path = args.unstarch_path |
| 218 |
if logger: logger.info('Location of \"unstarch\" is set to [%s]' % (unstarch_path)) |
| 219 |
|
| 220 |
starch_path = None |
| 221 |
if logger: logger.info('Locating \"starch\" binary') |
| 222 |
if not args.starch_path: |
| 223 |
starch_path = find_binary('starch') |
| 224 |
if not starch_path: |
| 225 |
if logger: logger.info('Could not locate \"starch\" binary') |
| 226 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS starch\n") |
| 227 |
sys.exit(errno.EEXIST) |
| 228 |
elif not cmd_exists('starch'): |
| 229 |
if logger: logger.info('Could not locate \"starch\" binary') |
| 230 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS starch\n") |
| 231 |
sys.exit(errno.EEXIST) |
| 232 |
else: |
| 233 |
starch_path = args.starch_path |
| 234 |
if logger: logger.info('Location of \"starch\" is set to [%s]' % (starch_path)) |
| 235 |
|
| 236 |
starchcat_path = None |
| 237 |
if logger: logger.info('Locating \"starchcat\" binary') |
| 238 |
if not args.starchcat_path: |
| 239 |
starchcat_path = find_binary('starchcat') |
| 240 |
if not starchcat_path: |
| 241 |
if logger: logger.info('Could not locate \"starchcat\" binary') |
| 242 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS starchcat\n") |
| 243 |
sys.exit(errno.EEXIST) |
| 244 |
elif not cmd_exists('starchcat'): |
| 245 |
if logger: logger.info('Could not locate \"starchcat\" binary') |
| 246 |
sys.stderr.write("ERROR: This script must be run on a system with BEDOPS starchcat\n") |
| 247 |
sys.exit(errno.EEXIST) |
| 248 |
else: |
| 249 |
starchcat_path = args.starchcat_path |
| 250 |
if logger: logger.info('Location of \"starchcat\" is set to [%s]' % (starchcat_path)) |
| 251 |
|
| 252 |
# build a list of chromosomes upon which to do work |
| 253 |
list_chromosome_cmd_components = [ |
| 254 |
unstarch_path, |
| 255 |
'--list-chr', |
| 256 |
args.input_original_fn |
| 257 |
] |
| 258 |
if logger: logger.info('Listing Starch chromosomes via [%s]' % (' '.join(list_chromosome_cmd_components))) |
| 259 |
try: |
| 260 |
list_chromosome_cmd_result = subprocess.check_output(list_chromosome_cmd_components) |
| 261 |
except subprocess.CalledProcessError as err: |
| 262 |
list_chromosome_cmd_result = "ERROR: Command '{}' returned with error (code {}): {}".format(err.cmd, err.returncode, err.output) |
| 263 |
raise |
| 264 |
chromosome_list = list_chromosome_cmd_result.decode('utf-8').rstrip('\n').split('\n') |
| 265 |
if logger: logger.info('Chromosomes to sort are [%s]' % (str(chromosome_list))) |
| 266 |
|
| 267 |
# fire up the mid-range saloons^H^H^H per-chromosome sort tasks |
| 268 |
job_prefix = ''.join(random.choice(string.lowercase) for x in range(8)) |
| 269 |
if logger: logger.info('Setting job prefix to [%s]' % (job_prefix)) |
| 270 |
|
| 271 |
per_chr_job_ids = [] |
| 272 |
per_chr_job_fns = [] |
| 273 |
|
| 274 |
for chromosome in chromosome_list: |
| 275 |
temp_dest = os.path.join(os.getcwd(), '_'.join([job_prefix, chromosome])) |
| 276 |
if logger: logger.info('Per-chromosome [%s] job name set to [%s]' % (chromosome, temp_dest)) |
| 277 |
per_chromosome_sort_cmd_components = [ |
| 278 |
'sbatch', |
| 279 |
'--parsable', |
| 280 |
'--workdir', |
| 281 |
slurm_workdir, |
| 282 |
'--output', |
| 283 |
os.path.join(slurm_output, '_'.join([job_prefix, 'out', chromosome])), |
| 284 |
'--error', |
| 285 |
os.path.join(slurm_error, '_'.join([job_prefix, 'err', chromosome])), |
| 286 |
'--mem', |
| 287 |
slurm_memory, |
| 288 |
'--partition', |
| 289 |
slurm_partition, |
| 290 |
'--wrap', |
| 291 |
'"srun ' + unstarch_path + ' ' + chromosome + ' ' + args.input_original_fn + ' | ' + sort_bed_path + ' - | ' + starch_path + ' - > ' + temp_dest + '"' |
| 292 |
] |
| 293 |
if logger: logger.info('Submitting job via [%s]' % (' '.join(per_chromosome_sort_cmd_components))) |
| 294 |
try: |
| 295 |
per_chromosome_process = subprocess.Popen(' '.join(per_chromosome_sort_cmd_components), |
| 296 |
shell=True, |
| 297 |
stdin=subprocess.PIPE, |
| 298 |
stdout=subprocess.PIPE, |
| 299 |
stderr=subprocess.STDOUT, |
| 300 |
close_fds=True) |
| 301 |
(per_chromosome_stdout, per_chromosome_stderr) = per_chromosome_process.communicate() |
| 302 |
per_chr_job_id = per_chromosome_stdout.decode('utf-8').rstrip('\n') |
| 303 |
per_chr_job_ids.append(per_chr_job_id) |
| 304 |
if logger: logger.info('Per-chromosome [%s] job id set to [%s]' % (chromosome, per_chr_job_id)) |
| 305 |
per_chr_job_fns.append(temp_dest) |
| 306 |
except subprocess.CalledProcessError as err: |
| 307 |
sys.stderr.write("ERROR: Per-chromosome sort task submission [%s] returned with error (code %d)\n" % (' '.join(err.cmd), err.returncode)) |
| 308 |
sys.exit(errno.EINVAL) |
| 309 |
if logger: logger.info('Per-chromosome sort task outputs [%s] to be concatenated' % (str(per_chr_job_fns))) |
| 310 |
per_chr_dependency_string = 'afterok:' + ':'.join(per_chr_job_ids) |
| 311 |
|
| 312 |
# concatenate per-chromosome resorted files to output temp file |
| 313 |
concatenation_cmd_components = [ |
| 314 |
'sbatch', |
| 315 |
'--parsable', |
| 316 |
'--workdir', |
| 317 |
slurm_workdir, |
| 318 |
'--output', |
| 319 |
os.path.join(slurm_output, '_'.join([job_prefix, 'out', 'concatenation'])), |
| 320 |
'--error', |
| 321 |
os.path.join(slurm_error, '_'.join([job_prefix, 'err', 'concatenation'])), |
| 322 |
'--mem', |
| 323 |
slurm_concatenation_memory, |
| 324 |
'--partition', |
| 325 |
slurm_partition, |
| 326 |
'--dependency', |
| 327 |
per_chr_dependency_string, |
| 328 |
'--wrap', |
| 329 |
'"srun ' + starchcat_path + ' ' + ' '.join(per_chr_job_fns) + ' > ' + args.output_temp_fn + '"' |
| 330 |
] |
| 331 |
if logger: logger.info('Submitting concatenation job via [%s]' % (' '.join(concatenation_cmd_components))) |
| 332 |
try: |
| 333 |
concatenation_process = subprocess.Popen(' '.join(concatenation_cmd_components), |
| 334 |
shell=True, |
| 335 |
stdin=subprocess.PIPE, |
| 336 |
stdout=subprocess.PIPE, |
| 337 |
stderr=subprocess.STDOUT, |
| 338 |
close_fds=True) |
| 339 |
(concatenation_stdout, concatenation_stderr) = concatenation_process.communicate() |
| 340 |
concatenation_job_id = concatenation_stdout.decode('utf-8').rstrip('\n') |
| 341 |
if logger: logger.info('Concatenation job id set to [%s]' % (concatenation_job_id)) |
| 342 |
concatenation_job_dependency_string = 'afterok:' + concatenation_job_id |
| 343 |
except subprocess.CalledProcessError as err: |
| 344 |
sys.stderr.write("ERROR: Concatenation task submission [%s] returned with error (code %d)\n" % (' '.join(err.cmd), err.returncode)) |
| 345 |
sys.exit(errno.EINVAL) |
| 346 |
|
| 347 |
# move original input file to backup file |
| 348 |
backup_input_cmd_components = [ |
| 349 |
'sbatch', |
| 350 |
'--parsable', |
| 351 |
'--workdir', |
| 352 |
slurm_workdir, |
| 353 |
'--output', |
| 354 |
os.path.join(slurm_output, '_'.join([job_prefix, 'out', 'backup_input'])), |
| 355 |
'--error', |
| 356 |
os.path.join(slurm_error, '_'.join([job_prefix, 'err', 'backup_input'])), |
| 357 |
'--mem', |
| 358 |
slurm_backup_input_memory, |
| 359 |
'--partition', |
| 360 |
slurm_partition, |
| 361 |
'--dependency', |
| 362 |
concatenation_job_dependency_string, |
| 363 |
'--wrap', |
| 364 |
'"srun mv ' + args.input_original_fn + ' ' + args.input_backup_fn + '"' |
| 365 |
] |
| 366 |
if logger: logger.info('Submitting backup job via [%s]' % (' '.join(backup_input_cmd_components))) |
| 367 |
try: |
| 368 |
backup_input_process = subprocess.Popen(' '.join(backup_input_cmd_components), |
| 369 |
shell=True, |
| 370 |
stdin=subprocess.PIPE, |
| 371 |
stdout=subprocess.PIPE, |
| 372 |
stderr=subprocess.STDOUT, |
| 373 |
close_fds=True) |
| 374 |
(backup_input_stdout, backup_input_stderr) = backup_input_process.communicate() |
| 375 |
backup_input_job_id = backup_input_stdout.decode('utf-8').rstrip('\n') |
| 376 |
if logger: logger.info('Backup job id set to [%s]' % (backup_input_job_id)) |
| 377 |
backup_input_job_dependency_string = 'afterok:' + backup_input_job_id |
| 378 |
except subprocess.CalledProcessError as err: |
| 379 |
sys.stderr.write("ERROR: Backup input task submission [%s] returned with error (code %d)\n" % (' '.join(err.cmd), err.returncode)) |
| 380 |
sys.exit(errno.EINVAL) |
| 381 |
|
| 382 |
# move output temp file to output final file |
| 383 |
output_move_cmd_components = [ |
| 384 |
'sbatch', |
| 385 |
'--parsable', |
| 386 |
'--workdir', |
| 387 |
slurm_workdir, |
| 388 |
'--output', |
| 389 |
os.path.join(slurm_output, '_'.join([job_prefix, 'out', 'concatenation'])), |
| 390 |
'--error', |
| 391 |
os.path.join(slurm_error, '_'.join([job_prefix, 'err', 'concatenation'])), |
| 392 |
'--mem', |
| 393 |
slurm_output_move_memory, |
| 394 |
'--partition', |
| 395 |
slurm_partition, |
| 396 |
'--dependency', |
| 397 |
backup_input_job_dependency_string, |
| 398 |
'--wrap', |
| 399 |
'"srun mv ' + args.output_temp_fn + ' ' + args.output_final_fn + '"' |
| 400 |
] |
| 401 |
if logger: logger.info('Submitting final move job via [%s]' % (' '.join(output_move_cmd_components))) |
| 402 |
try: |
| 403 |
output_move_process = subprocess.Popen(' '.join(output_move_cmd_components), |
| 404 |
shell=True, |
| 405 |
stdin=subprocess.PIPE, |
| 406 |
stdout=subprocess.PIPE, |
| 407 |
stderr=subprocess.STDOUT, |
| 408 |
close_fds=True) |
| 409 |
(output_move_stdout, output_move_stderr) = output_move_process.communicate() |
| 410 |
output_move_job_id = output_move_stdout.decode('utf-8').rstrip('\n') |
| 411 |
if logger: logger.info('Final move job id set to [%s]' % (output_move_job_id)) |
| 412 |
output_move_job_dependency_string = 'afterok:' + output_move_job_id |
| 413 |
except subprocess.CalledProcessError as err: |
| 414 |
sys.stderr.write("ERROR: Output move task submission [%s] returned with error (code %d)\n" % (' '.join(err.cmd), err.returncode)) |
| 415 |
sys.exit(errno.EINVAL) |
| 416 |
|
| 417 |
# perform cleanup |
| 418 |
cleanup_cmd_components = [ |
| 419 |
'sbatch', |
| 420 |
'--parsable', |
| 421 |
'--workdir', |
| 422 |
slurm_workdir, |
| 423 |
'--output', |
| 424 |
os.path.join(slurm_output, '_'.join([job_prefix, 'out', 'concatenation'])), |
| 425 |
'--error', |
| 426 |
os.path.join(slurm_error, '_'.join([job_prefix, 'err', 'concatenation'])), |
| 427 |
'--mem', |
| 428 |
slurm_cleanup_memory, |
| 429 |
'--partition', |
| 430 |
slurm_partition, |
| 431 |
'--dependency', |
| 432 |
output_move_job_dependency_string, |
| 433 |
'--wrap', |
| 434 |
'"srun rm -f ' + os.path.join(slurm_workdir, job_prefix) + '_* ' + os.path.join(slurm_output, job_prefix) + '_*"' |
| 435 |
] |
| 436 |
try: |
| 437 |
cleanup_process = subprocess.Popen(' '.join(cleanup_cmd_components), |
| 438 |
shell=True, |
| 439 |
stdin=subprocess.PIPE, |
| 440 |
stdout=subprocess.PIPE, |
| 441 |
stderr=subprocess.STDOUT, |
| 442 |
close_fds=True) |
| 443 |
(cleanup_stdout, cleanup_stderr) = cleanup_process.communicate() |
| 444 |
cleanup_job_id = cleanup_stdout.decode('utf-8').rstrip('\n') |
| 445 |
if logger: logger.info('Cleanup job id set to [%s]' % (cleanup_job_id)) |
| 446 |
except subprocess.CalledProcessError as err: |
| 447 |
sys.stderr.write("ERROR: Cleanup task submission [%s] returned with error (code %d)\n" % (' '.join(err.cmd), err.returncode)) |
| 448 |
sys.exit(errno.EINVAL) |
| 449 |
|
| 450 |
def find_binary(binary_to_find): |
| 451 |
for p in os.environ['PATH'].split(':'): |
| 452 |
for r, d, f in os.walk(p): |
| 453 |
for filename in f: |
| 454 |
if filename == binary_to_find: |
| 455 |
return os.path.join(r, filename) |
| 456 |
return None |
| 457 |
|
| 458 |
def cmd_exists(cmd): |
| 459 |
return subprocess.call("type " + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0 |
| 460 |
|
| 461 |
if __name__ == "__main__": |
| 462 |
main() |