scout/adapter/mongo/query.py
def secondary_query(self, query, mongo_query, secondary_filter=None):
        """Creates a secondary query object based on secondary parameters specified by user

            Args:
                query(dict): a dictionary of query filters specified by the users
                mongo_query(dict): the query that is going to be submitted to the database

            Returns:
                mongo_secondary_query(list): a dictionary with secondary query parameters

        """
        LOG.debug('Creating a query object with secondary parameters')

        mongo_secondary_query = []

        # loop over secondary query criteria
        for criterion in SECONDARY_CRITERIA:
            if not query.get(criterion):
                continue

            if criterion == 'gnomad_frequency':
                gnomad = query.get('gnomad_frequency')
                if gnomad == '-1':
                    # -1 means to exclude all variants that exists in gnomad
                    mongo_query['gnomad_frequency'] = {'$exists': False}
                else:
                    # Replace comma with dot
                    mongo_secondary_query.append(
                        {
                            '$or': [
                                {
                                    'gnomad_frequency': {'$lt': float(gnomad)}
                                },
                                {
                                    'gnomad_frequency': {'$exists': False}
                                }
                            ]
                        }
                    )
                LOG.debug("Adding gnomad_frequency to query")

            if criterion == 'local_obs':
                local_obs = query.get('local_obs')
                mongo_secondary_query.append({
                    '$or': [
                        {'local_obs_old': None},
                        {'local_obs_old': {'$lt': local_obs + 1}},
                    ]
                })

            if criterion in ['clingen_ngi', 'swegen']:
                mongo_secondary_query.append({
                    '$or': [
                        { criterion : {'$exists': False}},
                        { criterion : {'$lt': query[criterion] + 1}},
                    ]
                })

            if criterion == 'spidex_human':
                # construct spidex query. Build the or part starting with empty SPIDEX values
                spidex_human = query['spidex_human']

                spidex_query_or_part = []
                if ( 'not_reported' in spidex_human):
                    spidex_query_or_part.append({'spidex': {'$exists': False}})

                for spidex_level in SPIDEX_HUMAN:
                    if ( spidex_level in spidex_human ):
                        spidex_query_or_part.append({'$or': [
                                    {'$and': [{'spidex': {'$gt': SPIDEX_HUMAN[spidex_level]['neg'][0]}},
                                              {'spidex': {'$lt': SPIDEX_HUMAN[spidex_level]['neg'][1]}}]},
                                    {'$and': [{'spidex': {'$gt': SPIDEX_HUMAN[spidex_level]['pos'][0]}},
                                              {'spidex': {'$lt': SPIDEX_HUMAN[spidex_level]['pos'][1]}} ]} ]})

                mongo_secondary_query.append({'$or': spidex_query_or_part })

            if criterion == 'cadd_score':
                cadd = query['cadd_score']
                cadd_query = {'cadd_score': {'$gt': float(cadd)}}
                LOG.debug("Adding cadd_score: %s to query", cadd)

                if query.get('cadd_inclusive') is True:
                    cadd_query = {
                        '$or': [
                            cadd_query,
                            {'cadd_score': {'$exists': False}}
                            ]}
                    LOG.debug("Adding cadd inclusive to query")

                mongo_secondary_query.append(cadd_query)

            if criterion in ['genetic_models', 'functional_annotations', 'region_annotations']:
                criterion_values = query[criterion]
                if criterion == 'genetic_models':
                    mongo_secondary_query.append({criterion: {'$in': criterion_values}})
                else:
                    # filter key will be genes.[criterion (minus final char)]
                    mongo_secondary_query.append({ '.'.join(['genes', criterion[:-1]]) : {'$in': criterion_values}})

                LOG.debug("Adding {0}: {1} to query".format(criterion, ', '.join(criterion_values)))

            if criterion == 'size':
                size = query['size']
                size_query = {'length': {'$gt': int(size)}}
                LOG.debug("Adding length: %s to query" % size)

                if query.get('size_shorter'):
                    size_query = {
                        '$or': [
                            {'length': {'$lt': int(size)}},
                            {'length': {'$exists': False}}
                        ]}
                    LOG.debug("Adding size less than, undef inclusive to query.")

                mongo_secondary_query.append(size_query)

            if criterion == 'svtype':
                svtype = query['svtype']
                mongo_secondary_query.append({'sub_category': {'$in': svtype}})
                LOG.debug("Adding SV_type %s to query" %
                             ', '.join(svtype))

            if criterion == 'decipher':
                mongo_query['decipher'] = {'$exists': True}
                LOG.debug("Adding decipher to query")

            if criterion == 'depth':
                LOG.debug("add depth filter")
                mongo_secondary_query.append({
                    'tumor.read_depth': {
                        '$gt': query.get('depth'),
                    }
                })

            if criterion == 'alt_count':
                LOG.debug("add min alt count filter")
                mongo_secondary_query.append({
                    'tumor.alt_depth': {
                        '$gt': query.get('alt_count'),
                    }
                })

            if criterion == 'control_frequency':
                LOG.debug("add minimum control frequency filter")
                mongo_secondary_query.append({
                    'normal.alt_freq': {
                        '$lt': float(query.get('control_frequency')),
                    }
                })

            if criterion == 'mvl_tag':
                LOG.debug("add managed variant list filter")
                mongo_secondary_query.append({
                    'mvl_tag': {
                        '$exists': True,
                    }
                })

        return mongo_secondary_query
def secondary_query(self, query, mongo_query, secondary_filter=None):
        """Creates a secondary query object based on secondary parameters specified by user

            Args:
                query(dict): a dictionary of query filters specified by the users
                mongo_query(dict): the query that is going to be submitted to the database

            Returns:
                mongo_secondary_query(list): a dictionary with secondary query parameters

        """
        LOG.debug('Creating a query object with secondary parameters')

        mongo_secondary_query = []

        # loop over secondary query criteria
        for criterion in SECONDARY_CRITERIA:
            if not query.get(criterion):
                continue

            if criterion == 'gnomad_frequency':
                gnomad = query.get('gnomad_frequency')
                if gnomad == '-1':
                    # -1 means to exclude all variants that exists in gnomad
                    mongo_query['gnomad_frequency'] = {'$exists': False}
                else:
                    # Replace comma with dot
                    mongo_secondary_query.append(
                        {
                            '$or': [
                                {
                                    'gnomad_frequency': {'$lt': float(gnomad)}
                                },
                                {
                                    'gnomad_frequency': {'$exists': False}
                                }
                            ]
                        }
                    )
                LOG.debug("Adding gnomad_frequency to query")

            if criterion == 'local_obs':
                local_obs = query.get('local_obs')
                mongo_secondary_query.append({
                    '$or': [
                        {'local_obs_old': None},
                        {'local_obs_old': {'$lt': local_obs + 1}},
                    ]
                })

            if criterion in ['clingen_ngi', 'swegen']:
                mongo_secondary_query.append({
                    '$or': [
                        { criterion : {'$exists': False}},
                        { criterion : {'$lt': query[criterion] + 1}},
                    ]
                })

            if criterion == 'spidex_human':
                # construct spidex query. Build the or part starting with empty SPIDEX values
                spidex_human = query['spidex_human']

                spidex_query_or_part = []
                if ( 'not_reported' in spidex_human):
                    spidex_query_or_part.append({'spidex': {'$exists': False}})

                for spidex_level in SPIDEX_HUMAN:
                    if ( spidex_level in spidex_human ):
                        spidex_query_or_part.append({'$or': [
                                    {'$and': [{'spidex': {'$gt': SPIDEX_HUMAN[spidex_level]['neg'][0]}},
                                              {'spidex': {'$lt': SPIDEX_HUMAN[spidex_level]['neg'][1]}}]},
                                    {'$and': [{'spidex': {'$gt': SPIDEX_HUMAN[spidex_level]['pos'][0]}},
                                              {'spidex': {'$lt': SPIDEX_HUMAN[spidex_level]['pos'][1]}} ]} ]})

                mongo_secondary_query.append({'$or': spidex_query_or_part })

            if criterion == 'cadd_score':
                cadd = query['cadd_score']
                cadd_query = {'cadd_score': {'$gt': float(cadd)}}
                LOG.debug("Adding cadd_score: %s to query", cadd)

                if query.get('cadd_inclusive') is True:
                    cadd_query = {
                        '$or': [
                            cadd_query,
                            {'cadd_score': {'$exists': False}}
                            ]}
                    LOG.debug("Adding cadd inclusive to query")

                mongo_secondary_query.append(cadd_query)

            if criterion in ['genetic_models', 'functional_annotations', 'region_annotations']:
                criterion_values = query[criterion]
                if criterion == 'genetic_models':
                    mongo_secondary_query.append({criterion: {'$in': criterion_values}})
                else:
                    # filter key will be genes.[criterion (minus final char)]
                    mongo_secondary_query.append({ '.'.join(['genes', criterion[:-1]]) : {'$in': criterion_values}})

                LOG.debug("Adding {0}: {1} to query".format(criterion, ', '.join(criterion_values)))

            if criterion == 'size':
                size = query['size']
                size_query = {'length': {'$gt': int(size)}}
                LOG.debug("Adding length: %s to query" % size)

                if query.get('size_shorter'):
                    size_query = {
                        '$or': [
                            {'length': {'$lt': int(size)}},
                            {'length': {'$exists': False}}
                        ]}
                    LOG.debug("Adding size less than, undef inclusive to query.")

                mongo_secondary_query.append(size_query)

            if criterion == 'svtype':
                svtype = query['svtype']
                mongo_secondary_query.append({'sub_category': {'$in': svtype}})
                LOG.debug("Adding SV_type %s to query" %
                             ', '.join(svtype))

            if criterion == 'decipher':
                mongo_query['decipher'] = {'$exists': True}
                LOG.debug("Adding decipher to query")

            if criterion == 'depth':
                LOG.debug("add depth filter")
                mongo_secondary_query.append({
                    'tumor.read_depth': {
                        '$gt': query.get('depth'),
                    }
                })

            if criterion == 'alt_count':
                LOG.debug("add min alt count filter")
                mongo_secondary_query.append({
                    'tumor.alt_depth': {
                        '$gt': query.get('alt_count'),
                    }
                })

            if criterion == 'control_frequency':
                LOG.debug("add minimum control frequency filter")
                mongo_secondary_query.append({
                    'normal.alt_freq': {
                        '$lt': float(query.get('control_frequency')),
                    }
                })

            if criterion == 'mvl_tag':
                LOG.debug("add managed variant list filter")
                mongo_secondary_query.append({
                    'mvl_tag': {
                        '$exists': True,
                    }
                })

        return mongo_secondary_query
Creates a secondary query object based on secondary parameters specified by user

            Args:
                query(dict): a dictionary of query filters specified by the users
                mongo_query(dict): the query that is going to be submitted to the database

            Returns:
                mongo_secondary_query(list): a dictionary with secondary query parameters