Skip to content

Commit

Permalink
refactor: simplify templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Aug 26, 2024
1 parent 8689e01 commit eaf946d
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 63 deletions.
4 changes: 3 additions & 1 deletion src/ccbr_tools/pipeline/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import sys


def get_singularity_cachedir(output_dir, cache_dir=None):
def get_singularity_cachedir(output_dir=None, cache_dir=None):
"""Returns the singularity cache directory.
If no user-provided cache directory is provided,
the default singularity cache is in the output directory.
"""
if not output_dir:
output_dir = os.getcwd()
if not cache_dir:
cache_dir = os.path.join(output_dir, ".singularity")
return cache_dir
Expand Down
19 changes: 13 additions & 6 deletions src/ccbr_tools/pipeline/hpc.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from .util import get_hpcname
from .cache import get_singularity_cachedir


class Cluster:
def __init__(self):
self.name = None
self.slurm_script = {"nxf": None, "smk": None}
self.modules = {"nxf": None, "smk": None}
self.env_vars = "\n".join(
(f"export SINGULARITY_CACHEDIR={get_singularity_cachedir()}",)
)
self.singularity_sif_dir = None

def __repr__(self):
return f"{self.__class__}({self.__dict__})"

def __bool__(self):
return bool(self.name)

Expand All @@ -18,10 +24,6 @@ class Biowulf(Cluster):
def __init__(self):
super().__init__()
self.name = "biowulf"
self.slurm_script = {
"nxf": "slurm_nxf_biowulf.sh",
"smk": "slurm_smk_biowulf.sh",
}
self.modules = {
"nxf": "ccbrpipeliner nextflow",
"smk": "ccbrpipeliner snakemake/7 singularity",
Expand All @@ -33,9 +35,14 @@ class FRCE(Cluster):
def __init__(self):
super().__init__()
self.name = "frce"
self.slurm_script = {"nxf": "slurm_nxf_frce.sh", "smk": "slurm_smk_frce.sh"}
self.modules = {"nxf": "nextflow", "smk": "snakemake/7 singularity"}
self.singularity_sif_dir = "/mnt/projects/CCBR-Pipelines/SIFs"
self.env_vars = "\n".join(
(
self.env_vars,
f"export PATH=${{PATH}}:/mnt/projects/CCBR-Pipelines/bin",
)
)


def get_hpc(debug=False):
Expand Down
10 changes: 5 additions & 5 deletions src/ccbr_tools/pipeline/nextflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..pkg_util import msg_box, use_template
from ..shell import shell_run
from .hpc import get_hpc
from .cache import get_singularity_cachedir


def run(
Expand Down Expand Up @@ -72,18 +73,17 @@ def run(
if mode == "slurm":
slurm_filename = "submit_slurm.sh"
use_template(
hpc.slurm_script["nxf"],
output_filepath=slurm_filename,
"submit_slurm.sh",
PIPELINE=pipeline_name if pipeline_name else "CCBR_nxf",
MODULES=hpc.modules,
ENV_VARS=hpc.env_vars,
RUN_COMMAND=nextflow_command,
)
run_command = f"sbatch {slurm_filename}"
msg_box("Slurm batch job", errmsg=run_command)
elif mode == "local":
if hpc:
nextflow_command = (
f'bash -c "module load {hpc.modules} && {nextflow_command}"'
)
nextflow_command = f'bash -c "module load {hpc.modules} && {hpc.env_vars} && {nextflow_command}"'
run_command = nextflow_command
else:
raise ValueError(f"mode {mode} not recognized")
Expand Down
2 changes: 1 addition & 1 deletion src/ccbr_tools/pkg_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def read_template(template_name):

def use_template(template_name, output_filepath=None, **kwargs):
"""
Uses a template, formats variables, and write it to a file.
Uses a template, formats variables, and writes 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.
Expand Down
14 changes: 0 additions & 14 deletions src/ccbr_tools/templates/slurm_nxf_biowulf.sh

This file was deleted.

15 changes: 0 additions & 15 deletions src/ccbr_tools/templates/slurm_nxf_frce.sh

This file was deleted.

14 changes: 0 additions & 14 deletions src/ccbr_tools/templates/slurm_smk_frce.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#SBATCH --output "log/slurm_%j.log"
#SBATCH --output "log/slurm_%j.log"

module load ccbrpipeliner snakemake/7 singularity
module load {MODULES}

{ENV_VARS}

{RUN_COMMAND}
7 changes: 2 additions & 5 deletions tests/test_hpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ def test_hpc_biowulf():
[
hpc,
hpc.name == "biowulf",
hpc.slurm_script
== {"nxf": "slurm_nxf_biowulf.sh", "smk": "slurm_smk_biowulf.sh"},
hpc.singularity_sif_dir == "/data/CCBR_Pipeliner/SIFs",
]
)
Expand All @@ -20,13 +18,12 @@ def test_hpc_frce():
[
hpc,
hpc.name == "frce",
hpc.slurm_script
== {"nxf": "slurm_nxf_frce.sh", "smk": "slurm_smk_frce.sh"},
"/mnt/projects/CCBR-Pipelines/bin" in hpc.env_vars,
hpc.singularity_sif_dir == "/mnt/projects/CCBR-Pipelines/SIFs",
]
)


def test_hpc_none():
hpc = get_hpc(debug="")
assert not any([hpc, hpc.name, *hpc.slurm_script.values(), hpc.singularity_sif_dir])
assert not any([hpc, hpc.name, *hpc.modules.values(), hpc.singularity_sif_dir])
2 changes: 1 addition & 1 deletion tests/test_pkg_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def test_read_template():
template_str = read_template("slurm_nxf_biowulf.sh")
template_str = read_template("submit_slurm.sh")
assert all(
[
template_str.startswith("#!/usr/bin/env bash"),
Expand Down

0 comments on commit eaf946d

Please sign in to comment.