|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from snakemake.utils import read_job_properties |
| 7 | + |
| 8 | +# last command-line argument is the job script |
| 9 | +jobscript = sys.argv[-1] |
| 10 | + |
| 11 | +# all other command-line arguments are the dependencies |
| 12 | +dependencies = set(sys.argv[1:-1]) |
| 13 | + |
| 14 | +# parse the job script for the job properties that are encoded by snakemake within |
| 15 | +# this also includes the information contained in the cluster-config file as job_properties["cluster"] |
| 16 | +job_properties = read_job_properties(jobscript) |
| 17 | + |
| 18 | +sample = "" |
| 19 | +#find the sample name from the Snakemake rule properties (this assumes that in the Snakefile there exists a wildcard 'sample') |
| 20 | +if "sample" in job_properties["wildcards"]: |
| 21 | + sample = job_properties["wildcards"]["sample"] |
| 22 | + |
| 23 | +#add a unit to the sample name (assumes that there is a wildcard 'unit', in our snakefile this is the number of the partition) |
| 24 | +if "unit" in job_properties["wildcards"]: |
| 25 | + sample = sample+"-"+job_properties["wildcards"]["unit"] |
| 26 | + |
| 27 | +#add the sample name (this assumes that in the Snakefile there exists a wildcard 'sample') to the Jobname specified in the cluster_config file |
| 28 | +if len(sample) > 0: |
| 29 | + job_properties["cluster"]["J"] = job_properties["cluster"]["J"]+"-"+sample |
| 30 | + |
| 31 | +#add rule name to log files (assumes that the log files, as specified in the cluster_config file ends in 'stdout.txt' or 'stderr.txt') |
| 32 | +job_properties["cluster"]["output"] = job_properties["cluster"]["output"].replace("stdout.txt", job_properties["rule"]+"."+sample+".stdout.txt") |
| 33 | +job_properties["cluster"]["error"] = job_properties["cluster"]["error"].replace("stderr.txt", job_properties["rule"]+"."+sample+".stderr.txt") |
| 34 | + |
| 35 | +#determine threads from the Snakemake profile, i.e. as determined in the Snakefile and the main config file respectively |
| 36 | +job_properties["cluster"]["ntasks"] = job_properties["threads"] |
| 37 | +job_properties["cluster"]["ntasks-per-node"] = job_properties["threads"] |
| 38 | + |
| 39 | +# create list with command line arguments |
| 40 | +cmdline = ["sbatch"] |
| 41 | + |
| 42 | +# create string for slurm submit options for rule |
| 43 | +slurm_args = "--partition={partition} --qos={qos} --mem={mem} --ntasks={ntasks} --ntasks-per-node={ntasks-per-node} --time={time} --hint={hint} --output={output} --error={error} -N {N} -J {J}".format(**job_properties["cluster"]) |
| 44 | +cmdline.append(slurm_args) |
| 45 | + |
| 46 | +# now work on dependencies |
| 47 | +if dependencies: |
| 48 | + cmdline.append("--dependency") |
| 49 | + # only keep numbers (which are the jobids) in dependencies list. this is necessary because slurm returns more than the jobid. For other schedulers this could be different! |
| 50 | + dependencies = [x for x in dependencies if x.isdigit()] |
| 51 | + cmdline.append("afterok:" + ":".join(dependencies)) |
| 52 | + |
| 53 | +cmdline.append(jobscript) |
| 54 | + |
| 55 | +#now write final commandback to the system |
| 56 | +os.system(" ".join(cmdline)) |
0 commit comments