scripts/update_variant_panels.py
def update_panels(context, mongodb, username, password, authdb, host, port, loglevel, config):
    """scout: manage interactions with a scout instance."""
    coloredlogs.install(level=loglevel)
    
    LOG.info("Running scout version %s", __version__)
    LOG.debug("Debug logging enabled.")

    mongo_config = {}
    cli_config = {}
    if config:
        LOG.debug("Use config file %s", config)
        with open(config, 'r') as in_handle:
            cli_config = yaml.load(in_handle)

    mongo_config['mongodb'] = (mongodb or cli_config.get('mongodb') or 'scout')

    mongo_config['host'] = (host or cli_config.get('host') or 'localhost')
    mongo_config['port'] = (port or cli_config.get('port') or 27017)
    mongo_config['username'] = username or cli_config.get('username')
    mongo_config['password'] = password or cli_config.get('password')
    mongo_config['authdb'] = authdb or cli_config.get('authdb') or mongo_config['mongodb']
    mongo_config['omim_api_key'] = cli_config.get('omim_api_key')

    LOG.info("Setting database name to %s", mongo_config['mongodb'])
    LOG.debug("Setting host to %s", mongo_config['host'])
    LOG.debug("Setting port to %s", mongo_config['port'])

    valid_connection = check_connection(
        host=mongo_config['host'],
        port=mongo_config['port'],
        username=mongo_config['username'],
        password=mongo_config['password'],
        authdb=mongo_config['authdb'],
    )

    LOG.info("Test if mongod is running")
    if not valid_connection:
        LOG.warning("Connection could not be established")
        context.abort()

    try:
        client = get_connection(**mongo_config)
    except ConnectionFailure:
        context.abort()

    database = client[mongo_config['mongodb']]

    LOG.info("Setting up a mongo adapter")
    mongo_config['client'] = client
    adapter = MongoAdapter(database)
    
    requests = []
    
    for case_obj in adapter.case_collection.find():
        # pp(case_obj)
        
        gene_to_panels = adapter.gene_to_panels(case_obj)
        
        variants = adapter.variant_collection.find({
            'case_id': case_obj['_id'],
            'category': 'snv',
            'variant_type': 'clinical',
        })
        
        for variant_obj in variants:
        
            panel_names = set()
            for hgnc_id in variant_obj['hgnc_ids']:
                gene_panels = gene_to_panels.get(hgnc_id, set())
                panel_names = panel_names.union(gene_panels)
            
            if panel_names:
                operation = pymongo.UpdateOne(
                    {'_id': variant_obj['_id']},
                    {
                        '$set': {
                            'panels': list(panel_names)
                        }
                    })
                requests.append(operation)
            
            if len(requests) > 5000:
                adapter.variant_collection.bulk_write(requests, ordered=False)
                requests = []
        
        if requests:
            adapter.variant_collection.bulk_write(requests, ordered=False)
            requests = []
def update_panels(context, mongodb, username, password, authdb, host, port, loglevel, config):
    """scout: manage interactions with a scout instance."""
    coloredlogs.install(level=loglevel)
    
    LOG.info("Running scout version %s", __version__)
    LOG.debug("Debug logging enabled.")

    mongo_config = {}
    cli_config = {}
    if config:
        LOG.debug("Use config file %s", config)
        with open(config, 'r') as in_handle:
            cli_config = yaml.load(in_handle)

    mongo_config['mongodb'] = (mongodb or cli_config.get('mongodb') or 'scout')

    mongo_config['host'] = (host or cli_config.get('host') or 'localhost')
    mongo_config['port'] = (port or cli_config.get('port') or 27017)
    mongo_config['username'] = username or cli_config.get('username')
    mongo_config['password'] = password or cli_config.get('password')
    mongo_config['authdb'] = authdb or cli_config.get('authdb') or mongo_config['mongodb']
    mongo_config['omim_api_key'] = cli_config.get('omim_api_key')

    LOG.info("Setting database name to %s", mongo_config['mongodb'])
    LOG.debug("Setting host to %s", mongo_config['host'])
    LOG.debug("Setting port to %s", mongo_config['port'])

    valid_connection = check_connection(
        host=mongo_config['host'],
        port=mongo_config['port'],
        username=mongo_config['username'],
        password=mongo_config['password'],
        authdb=mongo_config['authdb'],
    )

    LOG.info("Test if mongod is running")
    if not valid_connection:
        LOG.warning("Connection could not be established")
        context.abort()

    try:
        client = get_connection(**mongo_config)
    except ConnectionFailure:
        context.abort()

    database = client[mongo_config['mongodb']]

    LOG.info("Setting up a mongo adapter")
    mongo_config['client'] = client
    adapter = MongoAdapter(database)
    
    requests = []
    
    for case_obj in adapter.case_collection.find():
        # pp(case_obj)
        
        gene_to_panels = adapter.gene_to_panels(case_obj)
        
        variants = adapter.variant_collection.find({
            'case_id': case_obj['_id'],
            'category': 'snv',
            'variant_type': 'clinical',
        })
        
        for variant_obj in variants:
        
            panel_names = set()
            for hgnc_id in variant_obj['hgnc_ids']:
                gene_panels = gene_to_panels.get(hgnc_id, set())
                panel_names = panel_names.union(gene_panels)
            
            if panel_names:
                operation = pymongo.UpdateOne(
                    {'_id': variant_obj['_id']},
                    {
                        '$set': {
                            'panels': list(panel_names)
                        }
                    })
                requests.append(operation)
            
            if len(requests) > 5000:
                adapter.variant_collection.bulk_write(requests, ordered=False)
                requests = []
        
        if requests:
            adapter.variant_collection.bulk_write(requests, ordered=False)
            requests = []
scout: manage interactions with a scout instance.