コード例 #1
0
ファイル: comments.py プロジェクト: WangZhenfei/cycledash
def _get_comment(comment_table, id=None, **query_kwargs):
    """Return comment with the given `id`, and any additional keyword arguments
    to modify the WHERE of the SQL query with.
    """
    if id is None:
        raise ValueError('Must provide a comment ID.')
    q = comment_table.select().where(comment_table.c.id == id)
    for colname, val in query_kwargs.items():
        q = q.where(comment_table.c[colname] == val)
    return dict(abort_if_none_for('comment')(q.execute().fetchone(), id))
コード例 #2
0
ファイル: ga4gh_wrapper.py プロジェクト: hammerlab/cycledash
    def readsGenerator(self, request):
        """Returns an iterator of reads which match the request criteria.

        The "readGroupIds" field of the request should be the ID of a BAM.

        The data hierarchy in the GA4GH server is

            dataset -> read group set -> read group

        A "read group" is equivalent to a BAM file. In Cycledash, we just want
        to serve BAM files, so the first two layers of the hierarchy are
        irrelevant. The GA4GH server requires them, though, so we have to
        create a fake dataset and a fake read group set to contain the read
        group.
        """
        bam_id = int(request.readGroupIds[0])
        bam = None
        with tables(db.engine, 'bams') as (con, bams):
            q = select(bams.c).where(bams.c.id == bam_id)
            bam = dict(abort_if_none_for('bam')(q.execute().fetchone(), bam_id))

        bam_path = self._root_path + bam['uri']

        # The strings are meaningless dummy values. They could be anything.
        dataset = datasets.AbstractDataset('dataset1')
        local_id = 'readGroupSetId'

        read_group_set = reads.AbstractReadGroupSet(dataset, local_id)

        # TODO: cache read_group? It might be re-reading the index on every request.
        read_group = reads.HtslibReadGroup(read_group_set, local_id, bam_path)

        read_group_set.addReadGroup(read_group)
        dataset.addReadGroupSet(read_group_set)

        return backend.ReadsIntervalIterator(request, read_group)
コード例 #3
0
ファイル: runs.py プロジェクト: hammerlab/cycledash
            ).returning(*runs.c)
            return dict(_abort_if_none(q.execute().fetchone(), run_id))

    @marshal_with(RunFields)
    def delete(self, run_id):
        """Delete a run by its ID."""
        with tables(db.engine, 'vcfs') as (con, runs):
            q = (runs.delete()
                 .where(runs.c.id == run_id)
                 .returning(*runs.c))
            # TODO: unattach BAMs and projects before deleting?
            # TODO: cascade tasks & genotypes deletion?
            return dict(_abort_if_none(q.execute().fetchone(), run_id))


_abort_if_none = abort_if_none_for('run')


def get_related_vcfs(run):
    """Return a list of vcfs in the same project as vcf."""
    with tables(db.engine, 'vcfs') as (con, runs):
        q = select(runs.c).where(
            runs.c.project_id == run['project_id']).where(
                runs.c.id != run['id'])
        return [dict(r) for r in q.execute().fetchall()]


def _set_or_verify_bam_id_on(run, bam_type):
    """After calling this, run['(bam_type)_bam_id'] will be set to a valid BAM ID.

    If there is not a matching BAM, it will raise voluptuous.Invalid.
コード例 #4
0
ファイル: bams.py プロジェクト: WangZhenfei/cycledash
            q = bams.update(bams.c.id == bam_id).values(
                **request.validated_body
            ).returning(*bams.c)
            return dict(_abort_if_none(q.execute().fetchone(), bam_id))

    @marshal_with(bam_fields)
    def delete(self, bam_id):
        """Delete a BAM by its ID."""
        with tables(db.engine, 'bams') as (con, bams):
            q = bams.delete(bams.c.id == bam_id).returning(*bams.c)
            q = q.execute()
            res = q.fetchone()
            return dict(_abort_if_none(res, bam_id))


_abort_if_none = abort_if_none_for('bam')


def attach_bams_to_vcfs(vcfs):
    """Attach tumor_bam and normal_bam to all VCFs."""
    with tables(db.engine, 'bams') as (con, bams):
        q = select(bams.c)
        bams = [dict(b) for b in con.execute(q).fetchall()]
    for vcf in vcfs:
        normal_bam_id = vcf.get('normal_bam_id')
        tumor_bam_id = vcf.get('tumor_bam_id')

        vcf['tumor_bam'] = (
            dict(find(bams, lambda x: x.get('id') == tumor_bam_id) or {}))
        vcf['normal_bam'] = (
            dict(find(bams, lambda x: x.get('id') == normal_bam_id) or {}))
コード例 #5
0
ファイル: genotypes.py プロジェクト: hammerlab/cycledash
        else:
            q = select(g.c).where(g.c.vcf_id == run_id)

        q = _add_range(q, g, query.get('range'))
        q = _add_filters(q, g, query.get('filters'))
        q = _add_orderings(q, g, query.get('sortBy'))
        q = _add_paging(q, g, query.get('limit'), query.get('page'))

        q = _add_ordering(q, g, 'String', 'contig', 'asc')
        q = _add_ordering(q, g, 'Integer', 'position', 'asc')
        genotypes = [dict(g) for g in con.execute(q).fetchall()]
    stats = calculate_stats(run_id, compare_to_run_id, query) if with_stats else {}
    return {'records': genotypes, 'stats': stats}


_abort_if_none = abort_if_none_for('genotype')


def star_genotype(run_id,
                  contig=None, position=None, reference=None, alternates=None,
                  sample_name=None, starred=None):
    with tables(db.engine, 'genotypes') as (con, genotypes):
        q = genotypes.update(
        ).where(genotypes.c.vcf_id == run_id
        ).where(genotypes.c.contig == contig
        ).where(genotypes.c.position == position
        ).where(genotypes.c.reference == reference
        ).where(genotypes.c.sample_name == sample_name
        ).where(genotypes.c.alternates == alternates
        ).values(
            **{'annotations:starred': starred}
コード例 #6
0
ファイル: projects.py プロジェクト: hammerlab/cycledash
        with tables(db.engine, 'projects') as (con, projects):
            q = projects.update(projects.c.id == project_id).values(
                **request.validated_body
            ).returning(*projects.c)
            return dict(_abort_if_none(q.execute().fetchone(), project_id))

    @marshal_with(ProjectFields)
    def delete(self, project_id):
        """Delete a project by its ID."""
        with tables(db.engine, 'projects') as (con, projects):
            q = projects.delete(
                projects.c.id == project_id).returning(*projects.c)
            return dict(_abort_if_none(q.execute().fetchone(), project_id))


_abort_if_none = abort_if_none_for('project')


def get_projects_tree():
    """Return a list of all projects, with their respective vcfs and bams.

    { "projects": [
      { "name": "Project Name",
        "notes": "Some test notes"
        "vcfs": [...]
        "bams": [
          { "name": "a dataset", ... }, ...
      ]}, ...
    ]}
    """
    with tables(db.engine, 'vcfs', 'user_comments', 'bams', 'projects') as \