Skip to content

Commit

Permalink
feat: add slurm script templates to package
Browse files Browse the repository at this point in the history
and use them with importlib
  • Loading branch information
kelly-sovacool committed Aug 19, 2024
1 parent c3324fc commit edc8735
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/ccbr_tools/pipeline/nextflow.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
from ..pkg_util import repo_base, msg_box
"""
Module: nextflow
This module provides functions for running Nextflow workflows in local and HPC environments.
Functions:
- run(nextfile_path=None, nextflow_args=None, mode="local", pipeline_name=None, debug=False, hpc_options={})
Run a Nextflow workflow.
"""

from ..pkg_util import repo_base, msg_box, use_template
from ..shell import shell_run
from .util import get_hpcname


def run_nextflow(
def run(
nextfile_path=None,
merge_config=None,
threads=None,
nextflow_args=None,
mode="local",
pipeline_name=None,
debug=False,
hpc_options={
"biowulf": {"profile": "biowulf", "slurm": "assets/slurm_header_biowulf.sh"},
"biowulf": {"profile": "biowulf", "slurm": "slurm_nxf_biowulf.sh"},
"fnlcr": {
"profile": "frce",
"slurm": "assets/slurm_header_frce.sh",
"slurm": "slurm_nxf_frce.sh",
},
},
):
Expand All @@ -22,8 +32,6 @@ def run_nextflow(
Args:
nextfile_path (str, optional): Path to the Nextflow file. Defaults to None.
merge_config (str, optional): Merge configuration. Defaults to None.
threads (int, optional): Number of threads. Defaults to None.
nextflow_args (list, optional): Additional Nextflow arguments. Defaults to None.
mode (str, optional): Execution mode. Defaults to "local".
hpc_options (dict, optional): HPC options. Defaults to {"biowulf": {"profile": "biowulf", "slurm": "assets/slurm_header_biowulf.sh"}, "fnlcr": {"profile": "frce", "slurm": "assets/slurm_header_frce.sh"}}.
Expand Down Expand Up @@ -70,6 +78,12 @@ def run_nextflow(

if mode == "slurm":
slurm_filename = "submit_slurm.sh"
use_template(
hpc_options[hpc]["slurm"],
output_filepath="submit_slurm.sh",
PIPELINE=pipeline_name if pipeline_name else "CCBR_nxf",
RUN_COMMAND=nextflow_command,
)
with open(slurm_filename, "w") as sbatch_file:
with open(repo_base(hpc_options[hpc]["slurm"]), "r") as template:
sbatch_file.writelines(template.readlines())
Expand All @@ -82,5 +96,6 @@ def run_nextflow(
run_command = nextflow_command
else:
raise ValueError(f"mode {mode} not recognized")

# Run Nextflow
shell_run(run_command, capture_output=False)
45 changes: 44 additions & 1 deletion src/ccbr_tools/pkg_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" Miscellaneous utility functions for the package """
"""
Miscellaneous utility functions for the package
"""

from cffconvert.cli.create_citation import create_citation
from cffconvert.cli.validate_or_write_output import validate_or_write_output
Expand All @@ -9,6 +11,8 @@
from time import localtime, strftime
import tomllib

from . import templates


class CustomClickGroup(click.Group):
def format_epilog(self, ctx, formatter):
Expand Down Expand Up @@ -149,3 +153,42 @@ def msg_box(splash, errmsg=None):
msg(("-" * (len(splash) + 4)))
if errmsg:
click.echo("\n" + errmsg, err=True)


def read_template(template_name):
"""
Read a template file
Args:
template_name (str): Name of the template file
Returns:
template (str): Contents of the template file
"""

template_files = importlib.resources.files(templates)
template_path = template_files / template_name
with open(template_path, "rt") as template_file:
return template_file.read()


def use_template(template_name, output_filepath=None, **kwargs):
"""
Uses a template, formats variables, and write it to a file.
Args:
template_name (str): The name of the template to use.
output_filepath (str, optional): The filepath to save the output file. If not provided, it will be written to `template_name` in the current working directory.
**kwargs: Keyword arguments to fill in the template variables.
Returns:
None
Raises:
FileNotFoundError: If the template file is not found.
IOError: If there is an error writing the output file.
Examples:
use_template("slurm_nxf_biowulf.sh", output_filepath="submit_slurm.sh", PIPELINE="CCBR_nxf", RUN_COMMAND="nextflow run main.nf -stub")
"""
template_str = read_template(template_name)
if not output_filepath:
output_filepath = template_name
with open(output_filepath, "wt") as outfile:
outfile.write(template_str.format(**kwargs))
14 changes: 14 additions & 0 deletions src/ccbr_tools/templates/slurm_nxf_biowulf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
#SBATCH --cpus-per-task=1
#SBATCH --mem=1g
#SBATCH --time=1-00:00:00
#SBATCH --parsable
#SBATCH -J "{PIPELINE}"
#SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --output "log/slurm_%j.log"
#SBATCH --output "log/slurm_%j.log"

module load ccbrpipeliner nextflow
NXF_SINGULARITY_CACHEDIR=/data/CCBR_Pipeliner/SIFS

{RUN_COMMAND}
15 changes: 15 additions & 0 deletions src/ccbr_tools/templates/slurm_nxf_frce.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
#SBATCH --cpus-per-task=1
#SBATCH --mem=1g
#SBATCH --time=1-00:00:00
#SBATCH --parsable
#SBATCH -J "{PIPELINE}"
#SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --output "log/slurm_%j.log"
#SBATCH --output "log/slurm_%j.log"

module load nextflow
NXF_SINGULARITY_CACHEDIR=/mnt/projects/CCBR-Pipelines/SIFs
# TODO add shared tools to the path

{RUN_COMMAND}
13 changes: 13 additions & 0 deletions src/ccbr_tools/templates/slurm_smk_biowulf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#SBATCH --cpus-per-task=1
#SBATCH --mem=1g
#SBATCH --time=1-00:00:00
#SBATCH --parsable
#SBATCH -J "{PIPELINE}"
#SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --output "log/slurm_%j.log"
#SBATCH --output "log/slurm_%j.log"

module load ccbrpipeliner snakemake/7 singularity

{RUN_COMMAND}
14 changes: 14 additions & 0 deletions src/ccbr_tools/templates/slurm_smk_frce.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
#SBATCH --cpus-per-task=1
#SBATCH --mem=1g
#SBATCH --time=1-00:00:00
#SBATCH --parsable
#SBATCH -J "{PIPELINE}"
#SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --output "log/slurm_%j.log"
#SBATCH --output "log/slurm_%j.log"

module load ccbrpipeliner snakemake/7 singularity
# TODO add shared Tools to the path

{RUN_COMMAND}
Empty file.
11 changes: 11 additions & 0 deletions tests/test_pkg_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ccbr_tools.pkg_util import read_template


def test_read_template():
template_str = read_template("slurm_nxf_biowulf.sh")
assert all(
[
template_str.startswith("#!/usr/bin/env bash"),
template_str.endswith("{RUN_COMMAND}"),
]
)

0 comments on commit edc8735

Please sign in to comment.