Skip to content

Commit

Permalink
update rest of structure to move cli functionality to separate package
Browse files Browse the repository at this point in the history
move testutils inside chanjo
  • Loading branch information
robinandeer committed Sep 26, 2017
1 parent bbf3a0e commit ca006eb
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 43 deletions.
3 changes: 3 additions & 0 deletions chanjo/cli/__init__.py
Expand Up @@ -2,3 +2,6 @@
from .calculate import calculate
from .sex import sex
from .load import link, load
from .sambamba import sambamba
from .db import db_cmd
from .init import init
30 changes: 15 additions & 15 deletions chanjo/store/cli.py → chanjo/cli/db.py
Expand Up @@ -3,42 +3,42 @@

import click

from .api import ChanjoDB
from .models import Sample
from chanjo.store.api import ChanjoDB
from chanjo.store.models import Sample

log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)


@click.group()
@click.group('db')
@click.pass_context
def db(context):
def db_cmd(context):
"""Interact with the database for maintainance tasks."""
context.obj['db'] = ChanjoDB(uri=context.obj['database'])


@db.command()
@db_cmd.command()
@click.option('--reset', is_flag=True, help='tear down existing db')
@click.pass_context
def setup(context, reset):
"""Initialize a new datbase from scratch."""
if reset:
log.info('tearing down existing database')
LOG.info('tearing down existing database')
context.obj['db'].tear_down()
log.info('setting up new database')
LOG.info('setting up new database')
context.obj['db'].set_up()


@db.command()
@db_cmd.command()
@click.argument('sample_id', type=str)
@click.pass_context
def remove(context, sample_id):
"""Remove all traces of a sample from the database."""
db = context.obj['db']
log.debug('find sample in database with id: %s', sample_id)
store = context.obj['db']
LOG.debug('find sample in database with id: %s', sample_id)
sample_obj = Sample.query.get(sample_id)
if sample_obj is None:
log.warn('sample (%s) not found in database', sample_id)
LOG.warning('sample (%s) not found in database', sample_id)
context.abort()
log.info('delete sample (%s) from database', sample_id)
db.session.delete(sample_obj)
db.save()
LOG.info('delete sample (%s) from database', sample_id)
store.session.delete(sample_obj)
store.save()
22 changes: 11 additions & 11 deletions chanjo/init/cli.py → chanjo/cli/init.py
@@ -1,17 +1,17 @@
# -*- coding: utf-8 -*-
import codecs
from distutils.spawn import find_executable
import logging

import click
from path import Path
import ruamel.yaml
from distutils.spawn import find_executable

from chanjo.store.api import ChanjoDB
from .bootstrap import pull, BED_NAME, DB_NAME
from .demo import setup_demo, DEMO_BED_NAME
from chanjo.init.bootstrap import pull, BED_NAME, DB_NAME
from chanjo.init.demo import setup_demo, DEMO_BED_NAME

log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)


@click.command()
Expand All @@ -25,29 +25,29 @@ def init(context, force, demo, auto, root_dir):
is_bootstrapped = False
root_path = Path(root_dir)

log.info("setting up chanjo under: %s", root_path)
LOG.info("setting up chanjo under: %s", root_path)
db_uri = context.obj.get('database')
db_uri = db_uri or "sqlite:///{}".format(root_path.joinpath(DB_NAME).abspath())

# test setup of sambamba
sambamba_bin = find_executable('sambamba')
if sambamba_bin is None: # pragma: no cover
log.warn("'sambamba' command not found")
LOG.warning("'sambamba' command not found")
else:
log.debug("'sambamba' found: %s", sambamba_bin)
LOG.debug("'sambamba' found: %s", sambamba_bin)

if demo:
log.info("copying demo files: %s", root_dir)
LOG.info("copying demo files: %s", root_dir)
setup_demo(root_dir, force=force)

log.info("configure new chanjo database: %s", db_uri)
LOG.info("configure new chanjo database: %s", db_uri)
chanjo_db = ChanjoDB(db_uri)
chanjo_db.set_up()
is_bootstrapped = True
elif auto or click.confirm('Bootstrap HGNC transcript BED?'):
pull(root_dir, force=force)

log.info("configure new chanjo database: %s", db_uri)
LOG.info("configure new chanjo database: %s", db_uri)
chanjo_db = ChanjoDB(db_uri)
chanjo_db.set_up()
is_bootstrapped = True
Expand All @@ -58,7 +58,7 @@ def init(context, force, demo, auto, root_dir):
with codecs.open(conf_path, 'w', encoding='utf-8') as conf_handle:
data = {'database': db_uri}
data_str = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
log.info("writing config file: %s", conf_path)
LOG.info("writing config file: %s", conf_path)
conf_handle.write(data_str)

if is_bootstrapped:
Expand Down
11 changes: 6 additions & 5 deletions chanjo/sambamba/cli.py → chanjo/cli/sambamba.py
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
import click
import logging

from .run import run_sambamba
import click

from chanjo.sambamba import run_sambamba

log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)


@click.command()
Expand All @@ -21,9 +22,9 @@
@click.pass_context
def sambamba(context, bam_file, regions, cov_thresholds, outfile):
"""Run Sambamba from chanjo."""
log.info("Running chanjo sambamba")
LOG.info("Running chanjo sambamba")
try:
run_sambamba(bam_file, regions, outfile, cov_thresholds)
except Exception:
log.exception('something went really wrong :_(')
LOG.exception('something went really wrong :_(')
context.abort()
File renamed without changes.
3 changes: 0 additions & 3 deletions chanjo/sambamba/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/init/test_utils.py → chanjo/testutils.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import os


Expand Down
6 changes: 3 additions & 3 deletions setup.py
Expand Up @@ -94,10 +94,10 @@ def run_tests(self):
'chanjo = chanjo.cli:root',
],
'chanjo.subcommands.4': [
'init = chanjo.init.cli:init',
'init = chanjo.cli:init',
'sex = chanjo.cli:sex',
'sambamba = chanjo.sambamba.cli:sambamba',
'db = chanjo.store.cli:db',
'sambamba = chanjo.cli:sambamba',
'db = chanjo.cli:db_cmd',
'load = chanjo.cli:load',
'link = chanjo.cli:link',
'calculate = chanjo.cli:calculate',
Expand Down
3 changes: 1 addition & 2 deletions tests/init/test_init_cli.py → tests/cli/test_cli_init.py
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from tempfile import gettempdir

from mock import patch
import ruamel.yaml

from test_utils import FakeZipFile, fake_urlretrieve
from chanjo.testutils import FakeZipFile, fake_urlretrieve


def test_init_demo(tmpdir, invoke_cli):
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/init/test_init_bootstrap.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os

from mock import patch
from chanjo.init import bootstrap

from test_utils import FakeZipFile, fake_urlretrieve
from chanjo.init import bootstrap
from chanjo.testutils import FakeZipFile, fake_urlretrieve


@patch('urllib.request.urlretrieve', fake_urlretrieve)
Expand Down
File renamed without changes.

0 comments on commit ca006eb

Please sign in to comment.