Skip to content

Instantly share code, notes, and snippets.

@ebolyen
Created March 28, 2014 17:20
Show Gist options
  • Save ebolyen/9838010 to your computer and use it in GitHub Desktop.
Save ebolyen/9838010 to your computer and use it in GitHub Desktop.
Running Monsoon
module load qiime
slurmit.py -ms job.txt MyJob
#!/usr/bin/env python
"""A simple slurm based cluster submission script."""
__author__ = "Jens Reeder"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Jens Reeder", "Rob Knight", "Greg Caporaso", "Jai Ram Rideout"]
__license__ = "GPL"
__version__ = "1.8.0-dev"
__maintainer__ = "Greg Caporaso"
__email__ = "gregcaporaso@gmail.com"
from optparse import OptionParser
from os.path import exists, normpath, sep
from os import remove, rename, rmdir, makedirs, close
from tempfile import mkstemp
import subprocess
from qiime.util import make_option,\
parse_command_line_parameters, load_qiime_config
from qiime.denoiser.make_cluster_jobs import make_jobs, submit_jobs
qiime_config = load_qiime_config()
script_info = {}
script_info[
'brief_description'] = "Starts multiple jobs in parallel on slurm based multiprocessor systems."
script_info[
'script_description'] = "This script is designed to start multiple jobs in parallel on cluster systems with a slurm based scheduling system."
script_info['script_usage'] = [
("Job submission example",
"Start each command listed in test_jobs.txt in parallel. The run ID for these jobs will be RUNID.",
"%prog -ms test_jobs.txt RUNID"),
("Queue specification example",
"Submit the commands listed in test_jobs.txt to the specified queue.",
"%prog -ms test_jobs.txt -q himem RUNID"),
("Jobs output directory specification example",
"Submit the commands listed in test_jobs.txt, with the jobs put under the "
"my_jobs/ directory.",
"%prog -ms test_jobs.txt -j my_jobs/ RUNID")
]
script_info['output_description'] = "No output is created."
script_info['required_options'] = []
script_info['optional_options'] = [
make_option('-m', '--make_jobs', action='store_true',
help='make the job files [default: %default]'),
make_option('-s', '--submit_jobs', action='store_true',
help='submit the job files [default: %default]'),
make_option('-q', '--queue',
help='name of queue to submit to [default: %default]',
default=qiime_config['slurm_queue']),
make_option('-j', '--job_dir',
help='directory to store the jobs [default: %default]',
default="jobs/")
]
script_info['version'] = __version__
script_info['disallow_positional_arguments'] = False
def make_jobs(commands, job_prefix, queue, jobs_dir="jobs/"):
filenames = []
for command in commands:
fd, job_name = mkstemp(dir=jobs_dir, prefix=job_prefix + "_",
suffix=".txt")
close(fd)
out_fh = open(job_name, "w")
out_fh.write("#!/bin/sh\n")
out_fh.write(command)
out_fh.close()
filenames.append(job_name)
return filenames
def main():
option_parser, opts, args =\
parse_command_line_parameters(**script_info)
if opts.submit_jobs and not opts.make_jobs:
option_parser.error('Must pass -m if passing -s. (Sorry about this, '
'it\'s for backwards-compatibility.)')
min_args = 2
if len(args) != min_args:
option_parser.error('Program requires <commands file> and '
'<job prefix>')
if (len(args[1]) > 10 or len(args[1]) == 0):
option_parser.error('job prefix must be 1-10 characters long')
if(not exists(opts.job_dir)):
try:
makedirs(opts.job_dir)
except OSError:
exit(" Jobs directory can not be created. "
"Check for permissions or file with the same name: %s\n"
% opts.job_dir)
commands = list(open(args[0]))
job_prefix = args[1]
if (opts.make_jobs):
filenames = make_jobs(
commands,
job_prefix,
opts.queue,
opts.job_dir)
else:
exit("Should we ever get here???")
if (opts.submit_jobs):
for f in filenames:
subprocess.call("".join([
"sbatch",
" -p ", opts.queue,
" -J ", job_prefix,
" -o ", normpath(opts.job_dir), sep, job_prefix, "_%j.out",
" ", f
]), shell=True)
if __name__ == "__main__":
main()
@gregcaporaso
Copy link

@ebolyen, you should update the __author__ line, and add yourself to __credits__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment