# Path: chanjo_report/server/constants.py
# LEVELS = OrderedDict([
#     (10, 'completeness_10'),
#     (15, 'completeness_15'),
#     (20, 'completeness_20'),
#     (50, 'completeness_50'),
#     (100, 'completeness_100'),
# ])
# 
# Path: chanjo_report/server/extensions.py



import itertools
import logging
from flask import abort, flash
from sqlalchemy.exc import OperationalError
from sqlalchemy import func
from chanjo.store.models import Transcript, TranscriptStat, Sample
from chanjo.sex import predict_sex
from chanjo_report.server.constants import LEVELS
from chanjo_report.server.extensions import api
    """Generate key metrics rows."""
    query = (
        api.query(
            TranscriptStat,
            func.avg(TranscriptStat.mean_coverage).label('mean_coverage'),
            func.avg(TranscriptStat.completeness_10).label('completeness_10'),
            func.avg(TranscriptStat.completeness_15).label('completeness_15'),
            func.avg(TranscriptStat.completeness_20).label('completeness_20'),
            func.avg(TranscriptStat.completeness_50).label('completeness_50'),
            func.avg(TranscriptStat.completeness_100).label('completeness_100'),
        )
        .filter(TranscriptStat.sample_id.in_(samples_ids))
        .group_by(TranscriptStat.sample_id)
    )

    if genes:
        query = (query.join(TranscriptStat.transcript)
                      .filter(Transcript.gene_id.in_(genes)))
    return query


def transcripts_rows(sample_ids, genes=None, level=10):
    """Generate metrics rows for transcripts."""
    for sample_id in sample_ids:
        sample_obj = Sample.query.get(sample_id)
        all_tx = TranscriptStat.query.filter_by(sample_id=sample_id)
        if genes:
            all_tx = (all_tx.join(TranscriptStat.transcript)
                            .filter(Transcript.gene_id.in_(genes)))
        tx_count = all_tx.count()

# Path: chanjo_report/server/constants.py
# LEVELS = OrderedDict([
#     (10, 'completeness_10'),
#     (15, 'completeness_15'),
#     (20, 'completeness_20'),
#     (50, 'completeness_50'),
#     (100, 'completeness_100'),
# ])
# 
# Path: chanjo_report/server/extensions.py



# Path: chanjo_report/server/blueprints/report/utils.py
import itertools
import logging
from flask import abort, flash
from sqlalchemy.exc import OperationalError
from sqlalchemy import func
from chanjo.store.models import Transcript, TranscriptStat, Sample
from chanjo.sex import predict_sex
from chanjo_report.server.constants import LEVELS
from chanjo_report.server.extensions import api

    """Generate key metrics rows."""
    query = (
        api.query(
            TranscriptStat,
            func.avg(TranscriptStat.mean_coverage).label('mean_coverage'),
            func.avg(TranscriptStat.completeness_10).label('completeness_10'),
            func.avg(TranscriptStat.completeness_15).label('completeness_15'),
            func.avg(TranscriptStat.completeness_20).label('completeness_20'),
            func.avg(TranscriptStat.completeness_50).label('completeness_50'),
            func.avg(TranscriptStat.completeness_100).label('completeness_100'),
        )
        .filter(TranscriptStat.sample_id.in_(samples_ids))
        .group_by(TranscriptStat.sample_id)
    )

    if genes:
        query = (query.join(TranscriptStat.transcript)
                      .filter(Transcript.gene_id.in_(genes)))
    return query


def transcripts_rows(sample_ids, genes=None, level=10):
    """Generate metrics rows for transcripts."""
    for sample_id in sample_ids:
        sample_obj = Sample.query.get(sample_id)
        all_tx = TranscriptStat.query.filter_by(sample_id=sample_id)
        if genes:
            all_tx = (all_tx.join(TranscriptStat.transcript)
                            .filter(Transcript.gene_id.in_(genes)))
        tx_count = all_tx.count()

        stat_field = getattr(TranscriptStat, LEVELS[level])