scout/adapter/mongo/case.py
def cases(self, owner=None, collaborator=None, query=None, skip_assigned=False,
              has_causatives=False, reruns=False, finished=False,
              research_requested=False, is_research=False, status=None,
              phenotype_terms=False, pinned=False, cohort=False, name_query=None,
              yield_query=False):
        """Fetches all cases from the backend.

        Args:
            collaborator(str): If collaborator should be considered
            owner(str): Query cases for specified case owner only
            query(dict): If a specific query is used
            skip_assigned(bool)
            has_causatives(bool)
            reruns(bool)
            finished(bool)
            research_requested(bool)
            is_research(bool)
            status(str)
            phenotype_terms(bool): Fetch all cases with phenotype terms
            pinned(bool): Fetch all cases with pinned variants
            name_query(str): Could be hpo term, HPO-group, user, part of display name,
                             part of inds or part of synopsis
            yield_query(bool): If true, only return mongo query dict for use in
                                compound querying.

        Returns:
            Cases ordered by date.
            If yield_query is True, does not pose query to db;
                instead returns corresponding query dict
                that can be reused in compound queries or for testing.
        """
        LOG.debug("Fetch all cases")
        query = query or {}

        # Prioritize when both owner and collaborator params are present
        if collaborator and owner:
            collaborator = None

        if collaborator:
            LOG.debug("Use collaborator {0}".format(collaborator))
            query['collaborators'] = collaborator

        if owner:
            LOG.debug("Use owner {0}".format(owner))
            query['owner'] = owner

        if skip_assigned:
            query['assignees'] = {'$exists': False}

        if has_causatives:
            query['causatives'] = {'$exists': True, '$ne': []}

        if reruns:
            query['rerun_requested'] = True

        if status:
            query['status'] = status

        elif finished:
            query['status'] = {'$in': ['solved', 'archived']}

        if research_requested:
            query['research_requested'] = True

        if is_research:
            query['is_research'] = {'$exists': True, '$eq': True}

        if phenotype_terms:
            query['phenotype_terms'] = {'$exists': True, '$ne': []}

        if pinned:
            query['suspects'] = {'$exists': True, '$ne': []}

        if cohort:
            query['cohorts'] = {'$exists': True, '$ne': []}

        if name_query:
            name_value = name_query.split(':')[-1] # capture ant value provided after query descriptor
            users = self.user_collection.find({'name': {'$regex': name_query, '$options': 'i'}})
            if users.count() > 0:
                query['assignees'] = {'$in': [user['email'] for user in users]}
            elif name_query.startswith('HP:'):
                LOG.debug("HPO case query")
                if name_value:
                    query['phenotype_terms.phenotype_id'] = name_query
                else: # query for cases with no HPO terms
                    query['$or'] = [ {'phenotype_terms' : {'$size' : 0}}, {'phenotype_terms' : {'$exists' : False}} ]
            elif name_query.startswith('PG:'):
                LOG.debug("PG case query")
                if name_value:
                    phenotype_group_query = name_query.replace('PG:', 'HP:')
                    query['phenotype_groups.phenotype_id'] = phenotype_group_query
                else: # query for cases with no phenotype groups
                    query['$or'] = [ {'phenotype_groups' : {'$size' : 0}}, {'phenotype_groups' : {'$exists' : False}} ]
            elif name_query.startswith('synopsis:'):
                if name_value:
                    query['$text']={'$search':name_value}
                else: # query for cases with missing synopsis
                    query['synopsis'] = ''
            elif name_query.startswith('cohort:'):
                query['cohorts'] = name_value
            elif name_query.startswith('panel:'):
                query['panels'] = {'$elemMatch': {'panel_name': name_value,
                                    'is_default': True }}
            elif name_query.startswith('status:'):
                status_query = name_query.replace('status:','')
                query['status'] = status_query
            elif name_query.startswith('is_research'):
                query['is_research'] = {'$exists': True, '$eq': True}
            else:
                query['$or'] = [
                    {'display_name': {'$regex': name_query}},
                    {'individuals.display_name': {'$regex': name_query}},
                ]

        if yield_query:
            return query

        LOG.info("Get cases with query {0}".format(query))
        return self.case_collection.find(query).sort('updated_at', -1)
def cases(self, owner=None, collaborator=None, query=None, skip_assigned=False,
              has_causatives=False, reruns=False, finished=False,
              research_requested=False, is_research=False, status=None,
              phenotype_terms=False, pinned=False, cohort=False, name_query=None,
              yield_query=False):
        """Fetches all cases from the backend.

        Args:
            collaborator(str): If collaborator should be considered
            owner(str): Query cases for specified case owner only
            query(dict): If a specific query is used
            skip_assigned(bool)
            has_causatives(bool)
            reruns(bool)
            finished(bool)
            research_requested(bool)
            is_research(bool)
            status(str)
            phenotype_terms(bool): Fetch all cases with phenotype terms
            pinned(bool): Fetch all cases with pinned variants
            name_query(str): Could be hpo term, HPO-group, user, part of display name,
                             part of inds or part of synopsis
            yield_query(bool): If true, only return mongo query dict for use in
                                compound querying.

        Returns:
            Cases ordered by date.
            If yield_query is True, does not pose query to db;
                instead returns corresponding query dict
                that can be reused in compound queries or for testing.
        """
        LOG.debug("Fetch all cases")
        query = query or {}

        # Prioritize when both owner and collaborator params are present
        if collaborator and owner:
            collaborator = None

        if collaborator:
            LOG.debug("Use collaborator {0}".format(collaborator))
            query['collaborators'] = collaborator

        if owner:
            LOG.debug("Use owner {0}".format(owner))
            query['owner'] = owner

        if skip_assigned:
            query['assignees'] = {'$exists': False}

        if has_causatives:
            query['causatives'] = {'$exists': True, '$ne': []}

        if reruns:
            query['rerun_requested'] = True

        if status:
            query['status'] = status

        elif finished:
            query['status'] = {'$in': ['solved', 'archived']}

        if research_requested:
            query['research_requested'] = True

        if is_research:
            query['is_research'] = {'$exists': True, '$eq': True}

        if phenotype_terms:
            query['phenotype_terms'] = {'$exists': True, '$ne': []}

        if pinned:
            query['suspects'] = {'$exists': True, '$ne': []}

        if cohort:
            query['cohorts'] = {'$exists': True, '$ne': []}

        if name_query:
            name_value = name_query.split(':')[-1] # capture ant value provided after query descriptor
            users = self.user_collection.find({'name': {'$regex': name_query, '$options': 'i'}})
            if users.count() > 0:
                query['assignees'] = {'$in': [user['email'] for user in users]}
            elif name_query.startswith('HP:'):
                LOG.debug("HPO case query")
                if name_value:
                    query['phenotype_terms.phenotype_id'] = name_query
                else: # query for cases with no HPO terms
                    query['$or'] = [ {'phenotype_terms' : {'$size' : 0}}, {'phenotype_terms' : {'$exists' : False}} ]
            elif name_query.startswith('PG:'):
                LOG.debug("PG case query")
                if name_value:
                    phenotype_group_query = name_query.replace('PG:', 'HP:')
                    query['phenotype_groups.phenotype_id'] = phenotype_group_query
                else: # query for cases with no phenotype groups
                    query['$or'] = [ {'phenotype_groups' : {'$size' : 0}}, {'phenotype_groups' : {'$exists' : False}} ]
            elif name_query.startswith('synopsis:'):
                if name_value:
                    query['$text']={'$search':name_value}
                else: # query for cases with missing synopsis
                    query['synopsis'] = ''
            elif name_query.startswith('cohort:'):
                query['cohorts'] = name_value
            elif name_query.startswith('panel:'):
                query['panels'] = {'$elemMatch': {'panel_name': name_value,
                                    'is_default': True }}
            elif name_query.startswith('status:'):
                status_query = name_query.replace('status:','')
                query['status'] = status_query
            elif name_query.startswith('is_research'):
                query['is_research'] = {'$exists': True, '$eq': True}
            else:
                query['$or'] = [
                    {'display_name': {'$regex': name_query}},
                    {'individuals.display_name': {'$regex': name_query}},
                ]

        if yield_query:
            return query

        LOG.info("Get cases with query {0}".format(query))
        return self.case_collection.find(query).sort('updated_at', -1)
Fetches all cases from the backend.

        Args:
            collaborator(str): If collaborator should be considered
            owner(str): Query cases for specified case owner only
            query(dict): If a specific query is used
            skip_assigned(bool)
            has_causatives(bool)
            reruns(bool)
            finished(bool)
            research_requested(bool)
            is_research(bool)
            status(str)
            phenotype_terms(bool): Fetch all cases with phenotype terms
            pinned(bool): Fetch all cases with pinned variants
            name_query(str): Could be hpo term, HPO-group, user, part of display name,
                             part of inds or part of synopsis
            yield_query(bool): If true, only return mongo query dict for use in
                                compound querying.

        Returns:
            Cases ordered by date.
            If yield_query is True, does not pose query to db;
                instead returns corresponding query dict
                that can be reused in compound queries or for testing.