Skip to content

Commit

Permalink
Merge pull request #3 from dib-lab/clickify
Browse files Browse the repository at this point in the history
initial clickification and packaging of genome-grist
  • Loading branch information
ctb authored Oct 27, 2020
2 parents 3e4f844 + f14b7d3 commit e3b6696
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ outputs/
*~
test.db.sbt.json
.sbt.test.db
__pycache__
.eggs
35 changes: 35 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=======
License
=======

Files: *
Copyright: 2020, The Regents of the University of California.
License: BSD-3-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of The Regents of the University of
California, nor the names of contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample: SRR606249
Empty file added genome_grist/__init__.py
Empty file.
159 changes: 159 additions & 0 deletions genome_grist/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
"Enable python -m genome_grist.click"
import sys
import os
import subprocess

import click


def get_snakefile_path(name):
thisdir = os.path.dirname(__file__)
snakefile = os.path.join(thisdir, "conf", name)
return snakefile


def get_package_configfile(filename):
thisdir = os.path.dirname(__file__)
configfile = os.path.join(thisdir, "conf", filename)
return configfile


def run_snakemake(
configfile,
no_use_conda=False,
verbose=False,
snakefile_name="Snakefile",
outdir=None,
extra_args=[],
):
# find the Snakefile relative to package path
snakefile = get_snakefile_path(snakefile_name)

# basic command
cmd = ["snakemake", "-s", snakefile]

# add --use-conda
if not no_use_conda:
cmd += ["--use-conda"]

# add --outdir
if outdir:
cmd += ["--config", f"outdir={outdir}"]

# snakemake sometimes seems to want a default -j; set it to 1 for now.
# can overridden later on command line.
cmd += ["-j", "1"]

# add rest of snakemake arguments
cmd += list(extra_args)

# add configfile - try looking for it a few different ways.
configfiles = []
if os.path.isfile(configfile):
configfiles = [configfile]
elif os.path.isfile(get_package_configfile(configfile)):
configfiles = [get_package_configfile(configfile)]
else:
for suffix in ".yaml", ".conf":
tryfile = configfile + suffix
if os.path.isfile(tryfile):
configfiles = [tryfile]
break

tryfile = get_package_configfile(tryfile)
if os.path.isfile(tryfile):
configfiles = [tryfile]
break

if not configfiles:
raise ValueError(f"cannot find config file '{configfile}'")

cmd += ["--configfile"] + configfiles

if verbose:
print("final command:", cmd)

# runme
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
print(f"Error in snakemake invocation: {e}", file=sys.stderr)
return e.returncode

return 0


#
# actual command line functions
#


@click.group()
def cli():
pass


# create a run subcommand that by default passes all of its arguments
# on to snakemake (after setting Snakefile and config)
@click.command(context_settings={"ignore_unknown_options": True})
@click.argument("configfile")
@click.option("--no-use-conda", is_flag=True, default=False)
@click.option("--verbose", is_flag=True)
@click.option("--outdir", nargs=1)
@click.argument("snakemake_args", nargs=-1)
def run(configfile, snakemake_args, no_use_conda, verbose, outdir):
"execute genome-grist workflow (using snakemake underneath)"
run_snakemake(
configfile,
snakefile_name="Snakefile",
no_use_conda=no_use_conda,
verbose=verbose,
extra_args=snakemake_args,
outdir=outdir,
)


# 'check' command
@click.command()
@click.argument("configfile")
def check(configfile):
"check configuration"
run_snakemake(configfile, extra_args=["check"])


# 'showconf' command
@click.command()
@click.argument("configfile")
def showconf(configfile):
"show full configuration"
run_snakemake(configfile, extra_args=["showconf"])


# 'info' command
@click.command()
def info():
"provide basic install/config file info"
from .version import version

print(
f"""
This is genome-grist version v{version}
Package install path: {os.path.dirname(__file__)}
snakemake Snakefile: {get_snakefile_path('Snakefile')}
"""
)


cli.add_command(run)
cli.add_command(check)
cli.add_command(showconf)
cli.add_command(info)


def main():
cli()


if __name__ == "__main__":
main()
File renamed without changes.
45 changes: 45 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from setuptools import setup, find_packages

# read the contents of your README file
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

CLASSIFIERS = [
"Environment :: Console",
"Environment :: MacOS X",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS :: MacOS X",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Scientific/Engineering :: Bio-Informatics",
]

setup(
name = 'genome-grist',
version = "0.1",
description="tools to support genome and metagenome analysis",
url="https://github.com/dib-lab/genome-grist",
author="C. Titus Brown, Luiz Irber, Tessa Pierce, Taylor Reiter",
author_email="[email protected],[email protected],[email protected],[email protected]",
license="BSD 3-clause",
packages = find_packages(),
classifiers = CLASSIFIERS,
entry_points = {'console_scripts': [
'genome-grist = genome_grist.__main__:main'
]
},
include_package_data=True,
package_data = { "genome_grist": ["Snakefile", "*.yml", "*.ipynb"] },
setup_requires = [ "setuptools>=38.6.0",
'setuptools_scm', 'setuptools_scm_git_archive' ],
use_scm_version = {"write_to": "genome_grist/version.py"},
install_requires = ['snakemake>=5.10', 'click>=7'],
long_description=long_description,
long_description_content_type="text/markdown",
)
35 changes: 35 additions & 0 deletions snakemake.slurm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash -login
#SBATCH -p med2 # partition, or queue, to assign to
#SBATCH -J gather-paper # name for job
#SBATCH -N 1 # one "node", or computer
#SBATCH -n 1 # one task for this node
#SBATCH -c 32 # cores per task
#SBATCH -t 2-0 # ask for 2 days
#SBATCH --mem=120000 # memory (30,000 mb = 30gb)
#SBATCH --mail-type=ALL
#SBATCH [email protected]

# initialize conda
. ~/miniconda3/etc/profile.d/conda.sh

# activate your desired conda environment
conda activate charcoal

# fail on weird errors
set -o nounset
set -o errexit
set -x

# go to the directory you ran 'sbatch' in, OR just hardcode it...
#cd $SLURM_SUBMIT_DIR
cd ~/genome-grist

# run the snakemake!
python -m genome_grist run conf.yml -j 32 --use-conda -n

# print out various information about the job
env | grep SLURM # Print out values of the current jobs SLURM environment variables

scontrol show job ${SLURM_JOB_ID} # Print out final statistics about resource uses before job exits

sstat --format 'JobID,MaxRSS,AveCPU' -P ${SLURM_JOB_ID}.batch

0 comments on commit e3b6696

Please sign in to comment.