Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
vjbaskar committed Feb 3, 2025
1 parent 1c6812d commit 6eff77e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 32 deletions.
4 changes: 2 additions & 2 deletions solosis/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click

from solosis.commands.alignment import cellranger_arc, cellranger_count, starsolo
from solosis.commands.farm import farm
from solosis.commands.farm import single_job
from solosis.commands.filesystem import disk_usage, file_count
from solosis.commands.irods import iget_cellranger, iget_fastqs
from solosis.commands.ncl_bsu import migrate
Expand Down Expand Up @@ -91,7 +91,7 @@ def sc_rna():

# farm
farm_cmds.add_command(farm.single_cmd)
farm_cmds.add_command(farm.run_ipynb)
#farm_cmds.add_command(farm.run_ipynb)

if __name__ == "__main__":
cli()
37 changes: 37 additions & 0 deletions solosis/commands/farm/run_notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import subprocess
import click
from .. import helpers
from solosis.utils import echo_lsf_submission_message, echo_message, log_command, bash_submit

# Script directory in the solosis package
script_dir = os.path.dirname(os.path.abspath(__file__))
codebase = os.path.join(script_dir, "..", "..", "codebase")



@click.command("run_ipynb")
@helpers.farm
@click.option(
"-n", "--notebook", required=True, type=str, help="Path to the notebook to run"
)
@click.option(
"-j",
"--job_name",
required=False,
type=str,
help="Name of the job"
)

def run_ipynb(notebook, job_name, **kwargs):
"""
Run a jupyter notebook on the farm.
"""
job_runner = os.path.join(codebase, "single_job.sh")
# To do: Install jupyter in base solosis conda env
command_to_exec = f"source ~/.bashrc && conda activate /software/cellgen/team298/shared/envs/hl-conda/hl_minimal_v1.0.0 && jupyter nbconvert --to notebook --execute {notebook}"
bash_submit(
job_runner, command_to_exec=command_to_exec, job_name="notebook", **kwargs
)


Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Script directory in the solosis package
script_dir = os.path.dirname(os.path.abspath(__file__))

codebase = os.path.join(script_dir, "..", "..", "..", "bin")

## Functions supporting farm submission
def bash_submit(job_runner: str, **kwargs) -> None:
Expand All @@ -33,7 +33,7 @@ def _single_command_bsub(command_to_exec, job_name, queue, time, cores, mem, **k
Run a single command on the farm.
"""
job_runner = os.path.abspath(
os.path.join(script_dir, "../../../bin/farm/single_job.sh")
os.path.join(codebase, "/farm/single_job.sh")
)
if len(command_to_exec) == 0:
echo_message("No command to execute", type="error")
Expand Down Expand Up @@ -68,38 +68,13 @@ def single_cmd(ctx, command_to_exec, job_name, **kwargs):
mem = kwargs.get("mem")
log_command(ctx)
if job_name == "default":
job_name = f"{ctx.command_path}_{ctx.obj['execution_id']}"
job_name = f"{ctx.obj['execution_id']}"
else:
job_name = f"{job_name}_{ctx.obj['execution_id']}"
echo_lsf_submission_message(f"Job name :{job_name} submitted to queue: {queue}")
echo_message(f"Job name :{job_name} submitted to queue: {queue}")
_single_command_bsub(command_to_exec, job_name=job_name, **kwargs)


@click.command("run_ipynb")
@helpers.farm
@click.option(
"-n", "--notebook", required=True, type=str, help="Path to the notebook to run"
)
@click.option(
"-j",
"--job_name",
required=False,
type=str,
help="Name of the job",
) # random val to be implemented (from import random)


def run_ipynb(notebook, job_name, **kwargs):
"""
Run a jupyter notebook on the farm.
"""
job_runner = os.path.join(codebase, "single_job.sh")
# To do: Install jupyter in base solosis conda env
command_to_exec = f"source ~/.bashrc && conda activate /software/cellgen/team298/shared/envs/hl-conda/hl_minimal_v1.0.0 && jupyter nbconvert --to notebook --execute {notebook}"
bash_submit(
job_runner, command_to_exec=command_to_exec, job_name="notebook", **kwargs
)


if __name__ == "__main__":
cmd = os.path.join(codebase, "test.sh")
Expand Down
24 changes: 23 additions & 1 deletion solosis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import re
from datetime import datetime

import subprocess
import click


Expand Down Expand Up @@ -104,3 +104,25 @@ def echo_lsf_submission_message(job_stdout):

echo_message("Use `bjobs` to monitor job completion.", "info")
echo_message("View job logs at $HOME/logs.", "info")


## Functions supporting farm submission
def bash_submit(job_runner: str, **kwargs) -> None:
"""
Runs a command. Command can be a bash command (du -hs ) or a script (test.sh)
While running exports all kwargs as environment variables
This can be reused to run any bash script in all the subcommands
If the command needs to be submitted to farm use single_command or array_command
"""
# env variables are set to
for k, v in kwargs.items():
kwargs[str(k)] = str(v)
# Capture result
result = subprocess.run(
[job_runner], capture_output=True, text=True, env={**os.environ, **kwargs}
)

click.echo(result.stdout)
click.echo(result.stderr)


0 comments on commit 6eff77e

Please sign in to comment.