Skip to content

Commit

Permalink
Merge 38dc098 into 220522c
Browse files Browse the repository at this point in the history
  • Loading branch information
Måns Magnusson committed Sep 28, 2017
2 parents 220522c + 38dc098 commit a061731
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
17 changes: 17 additions & 0 deletions chanjo/calculate.py
Expand Up @@ -30,3 +30,20 @@ def gene_metrics(self, *genes):
.filter(Transcript.gene_id.in_(genes))
.group_by(Transcript.gene_id))
return query

def mean_cov_gene(self, gene_id, sample_id):
"""Return all transcript stats for a gene"""
mean = None
result = (self.query(Transcript, TranscriptStat)
.filter(Transcript.id == TranscriptStat.transcript_id)
.filter(Transcript.gene_id==gene_id)
.filter(TranscriptStat.sample_id == sample_id)
.all()
)
if not result:
return mean
mean = 0
for i,res in enumerate(result):
mean += res[1].mean_coverage
mean = mean/(i+1)
return mean
1 change: 1 addition & 0 deletions chanjo/cli/__init__.py
Expand Up @@ -5,3 +5,4 @@
from .sambamba import sambamba
from .db import db_cmd
from .init import init
from .view import view
24 changes: 24 additions & 0 deletions chanjo/cli/calculate.py
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import json
import logging
from pprint import pprint as pp

import click


from chanjo.store.api import ChanjoDB
from chanjo.store.constants import STAT_COLUMNS

Expand Down Expand Up @@ -37,3 +39,25 @@ def mean(context, sample, pretty):
for result in query:
row = {column: value for column, value in zip(columns, result)}
click.echo(dump_json(row, pretty=pretty))

@calculate.command()
@click.option('-i', '--gene-id', type=int, help='Gene id for a gene')
@click.option('-s', '--sample', multiple=True, help='sample(s) to limit query to')
@click.option('-p', '--pretty', is_flag=True)
@click.pass_context
def gene(context, gene_id, sample, pretty):
"""Calculate average coverage for a gene"""
if not (gene_id or gene_symbol):
LOG.warning('Please specify a gene')
context.abort()
if not sample:
LOG.warning('Please specify at least one sample')
context.abort()

for sample_id in sample:
result = context.obj['db'].mean_cov_gene(gene_id, sample_id)
if result is None:
LOG.info("No result could be found")
return
row = {'average_coverage':result, 'sample_id': sample_id, 'gene': gene_id}
click.echo(dump_json(row, pretty=pretty))
67 changes: 67 additions & 0 deletions chanjo/cli/view.py
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
import logging

import click

from chanjo.store.api import ChanjoDB
from chanjo.store.models import TranscriptStat
from .calculate import dump_json

LOG = logging.getLogger(__name__)

@click.group()
@click.pass_context
def view(context):
"""View infromation from database."""
if not context.obj['database']:
LOG.warning("Please point to a database")
context.abort()
context.obj['db'] = ChanjoDB(uri=context.obj['database'])


@view.command()
@click.option('-s', '--sample', multiple=True, help='sample(s) to limit query to')
@click.option('-g', '--group', multiple=True, help='group(s) to limit query to')
@click.pass_context
def sample(context, sample, group):
"""View information on samples."""
result = context.obj['db'].sample(sample, group)
for res in result:
click.echo("sample_id: {0}, group_id: {1}, source: {2}".format(
res.id, res.group_id, res.source
))

@view.command()
@click.option('-i', '--gene-id', type=int, help='Gene id for a gene')
@click.option('--gene-symbol', help='Gene symbol for a gene')
@click.option('-p', '--pretty', is_flag=True)
@click.pass_context
def gene(context, gene_id, gene_symbol, pretty):
"""Display all transcripts for a gene"""
if not (gene_id or gene_symbol):
LOG.warning('Please specify a gene')
context.abort()

result = context.obj['db'].gene(gene_id, gene_symbol)
if result.count() == 0:
LOG.info("No genes found")

for res in result:
row = {
'id': res.id,
'gene_id': res.gene_id,
'gene_name': res.gene_name,
'chromosome': res.chromosome,
'length': res.length,
}
click.echo(dump_json(row, pretty=pretty))

@view.command()
@click.option('-s', '--sample', help='sample to limit query to')
@click.pass_context
def incomplete(context, sample):
"""Wiew all transcripts with incomplete exons for a sample"""
query = context.obj['db'].query(TranscriptStat).filter_by(sample_id=sample)
result = query.filter(TranscriptStat._incomplete_exons.isnot(None))
for res in result:
print(res)
21 changes: 20 additions & 1 deletion chanjo/store/api.py
Expand Up @@ -6,7 +6,7 @@
from alchy import Manager

from chanjo.calculate import CalculateMixin
from .models import BASE
from .models import (BASE, Sample, Transcript)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -122,3 +122,22 @@ def save(self):
self.session.rollback()
raise error
return self

def sample(self, sample_ids=None, groups=None):
"""Return samples from database"""
sql_query = self.query(Sample)
if sample_ids:
sql_query = sql_query.filter(Sample.id.in_(sample_ids))
if groups:
sql_query = sql_query.filter(Sample.group_id.in_(groups))

return sql_query

def gene(self, gene_id=None, gene_symbol=None):
"""Return transcripts from certain genes"""
sql_query = self.query(Transcript)
if gene_id:
sql_query = sql_query.filter_by(gene_id=gene_id)
if gene_symbol:
sql_query = sql_query.filter_by(gene_name=gene_symbol)
return sql_query
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -101,6 +101,7 @@ def run_tests(self):
'load = chanjo.cli:load',
'link = chanjo.cli:link',
'calculate = chanjo.cli:calculate',
'view = chanjo.cli:view',
]
},

Expand Down

0 comments on commit a061731

Please sign in to comment.