def main_bigg(args=None, urlopen=urlopen):
    """Entry point for BiGG import program.

    If the ``args`` are provided, these should be a list of strings that will
    be used instead of ``sys.argv[1:]``. This is mostly useful for testing.
    """
    parser = argparse.ArgumentParser(
        description='Import from BiGG database')
    parser.add_argument('--dest', metavar='path', default='.',
                        help='Destination directory (default is ".")')
    parser.add_argument('--no-exchange', action='store_true',
                        help=('Disable importing exchange reactions as'
                              ' exchange compound file.'))
    parser.add_argument('--split-subsystem', action='store_true',
                        help='Enable splitting reaction files by subsystem')
    parser.add_argument('--merge-compounds', action='store_true',
                        help=('Merge identical compounds occuring in various'
                              ' compartments.'))
    parser.add_argument('--force', action='store_true',
                        help='Enable overwriting model files')
    parser.add_argument('id', help='BiGG model to import ("list" to see all)')

    args = parser.parse_args(args)

    # Set up logging for the command line interface
    if 'PSAMM_DEBUG' in os.environ:
        level = getattr(logging, os.environ['PSAMM_DEBUG'].upper(), None)
        if level is not None:
            logging.basicConfig(level=level)
    else:
        logging.basicConfig(
            level=logging.INFO, format='%(levelname)s: %(message)s')

    # Print list of available models
    if args.id == 'list':
        print('Available models:')
        f = urlopen('http://bigg.ucsd.edu/api/v2/models')
        doc = json.loads(f.read().decode('utf-8'))
        results = doc['results']
        id_width = min(max(len(result['bigg_id']) for result in results), 16)
        for result in sorted(results, key=lambda x: x.get('organism')):
            print('{} {}'.format(
                result.get('bigg_id').ljust(id_width), result.get('organism')))
        return 0

    importer_entry = None
    try:
        importer_entry = next(
            pkg_resources.iter_entry_points('psamm.importer', 'JSON'))
    except StopIteration:
        logger.error('Failed to locate the COBRA JSON model importer!')
        sys.exit(-1)

    importer_class = importer_entry.load()
    importer = importer_class()

    try:
        f = urlopen(
            'http://bigg.ucsd.edu/api/v2/models/{}/download'.format(
                url_quote(args.id)))
        model = importer.import_model(codecs.getreader('utf-8')(f))
    except ModelLoadError as e:
        logger.error('Failed to load model!', exc_info=True)
        importer.help()
        parser.error(text_type(e))
    except ParseError as e:
        logger.error('Failed to parse model!', exc_info=True)
        logger.error(text_type(e))
        sys.exit(-1)

    if args.merge_compounds:
        compounds_before = len(model.compounds)
        sbml.merge_equivalent_compounds(model)
        if len(model.compounds) < compounds_before:
            logger.info(
                'Merged {} compound entries into {} entries by'
                ' removing duplicates in various compartments'.format(
                    compounds_before, len(model.compounds)))

    print('Model: {}'.format(model.name))
    print('- Biomass reaction: {}'.format(model.biomass_reaction))
    print('- Compartments: {}'.format(len(model.compartments)))
    print('- Compounds: {}'.format(len(model.compounds)))
    print('- Reactions: {}'.format(len(model.reactions)))
    print('- Genes: {}'.format(count_genes(model)))

    # Check if dest directory is empty. If we get an error assume that the
    # directory does not exist.
    dest_is_empty = False
    try:
        dest_is_empty = len(os.listdir(args.dest)) == 0
    except OSError:
        dest_is_empty = True

    if not dest_is_empty:
        if not args.force:
            logger.error('Destination directory is not empty. Use --force'
                         ' option to proceed anyway, overwriting any existing'
                         ' files in {}'.format(args.dest))
            return 1
        else:
            logger.warning('Destination directory is not empty, overwriting'
                           ' existing files in {}'.format(args.dest))

    # Create destination directory if not exists
    dest = args.dest
    mkdir_p(dest)

    convert_exchange = not args.no_exchange
    write_yaml_model(model, dest, convert_exchange=convert_exchange,
                     split_subsystem=args.split_subsystem)