def main(args=None):
if args is None:<EOL><INDENT>parser = get_argument_parser()<EOL>args = parser.parse_args()<EOL><DEDENT>input_file = args.annotation_file<EOL>output_file = args.output_file<EOL>species = args.species<EOL>chrom_pat = args.chromosome_pattern<EOL>field_name = args.field_name<EOL>log_file = args.log_file<EOL>quiet = args.quiet<EOL>verbose = args.verbose<EOL>log_stream = sys.stdout<EOL>if output_file == '<STR_LIT:->':<EOL><INDENT>log_stream = sys.stderr<EOL><DEDENT>logger = misc.get_logger(log_stream = log_stream, log_file = log_file,<EOL>quiet = quiet, verbose = verbose)<EOL>if chrom_pat is None:<EOL><INDENT>chrom_pat = re.compile(ensembl.SPECIES_CHROMPAT[species])<EOL><DEDENT>else:<EOL><INDENT>chrom_pat = re.compile(chrom_pat)<EOL><DEDENT>logger.info('<STR_LIT>',<EOL>chrom_pat.pattern)<EOL>types = Counter()<EOL>sources = Counter()<EOL>genes = Counter()<EOL>gene_chroms = dict()<EOL>gene_ids = dict()<EOL>genes2 = Counter()<EOL>polymorphic = set()<EOL>chromosomes = set()<EOL>excluded_chromosomes = set()<EOL>transcripts = {}<EOL>gene_id = None<EOL>gene_name = None<EOL>i = <NUM_LIT:0><EOL>missing = <NUM_LIT:0><EOL>logger.info('<STR_LIT>')<EOL>if input_file == '<STR_LIT:->':<EOL><INDENT>input_file = None<EOL><DEDENT>with misc.smart_open_read(input_file, mode = '<STR_LIT:rb>', try_gzip = True) as fh:<EOL><INDENT>reader = csv.reader(fh, dialect = '<STR_LIT>')<EOL>for l in reader:<EOL><INDENT>i += <NUM_LIT:1><EOL>if len(l) > <NUM_LIT:1> and l[<NUM_LIT:2>] == field_name:<EOL><INDENT>attr = parse_attributes(l[<NUM_LIT:8>])<EOL>type_ = attr['<STR_LIT>']<EOL>if type_ not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>chrom = l[<NUM_LIT:0>]<EOL>m = chrom_pat.match(chrom)<EOL>if m is None:<EOL><INDENT>excluded_chromosomes.add(chrom)<EOL>continue<EOL><DEDENT>chromosomes.add(m.group())<EOL>source = l[<NUM_LIT:1>]<EOL>gene_id = attr['<STR_LIT>']<EOL>try:<EOL><INDENT>gene_name = attr['<STR_LIT>']<EOL><DEDENT>except KeyError as e:<EOL><INDENT>missing += <NUM_LIT:1><EOL>continue<EOL><DEDENT>if gene_id in genes:<EOL><INDENT>if genes[gene_id] != gene_name:<EOL><INDENT>raise ValueError('<STR_LIT>' %(gene_id) +<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>genes[gene_id] = gene_name<EOL><DEDENT><DEDENT><DEDENT><DEDENT>logger.info('<STR_LIT>', i)<EOL>logger.info('<STR_LIT>', len(excluded_chromosomes))<EOL>logger.info('<STR_LIT:U+002CU+0020>'.join(sorted(excluded_chromosomes)))<EOL>n = len(genes)<EOL>m = len(set(genes.values()))<EOL>logger.info('<STR_LIT>', len(chromosomes))<EOL>logger.info('<STR_LIT>', n)<EOL>logger.info('<STR_LIT>', m)<EOL>with misc.smart_open_write(output_file) as ofh:<EOL><INDENT>writer = csv.writer(ofh, dialect = '<STR_LIT>',<EOL>lineterminator = os.linesep, quoting = csv.QUOTE_NONE)<EOL>for g in sorted(genes.keys()):<EOL><INDENT>writer.writerow([g, genes[g]])<EOL><DEDENT><DEDENT>return <NUM_LIT:0><EOL>
Extract Ensembl IDs and store in tab-delimited text file.

    Parameters
    ----------
    args: argparse.Namespace object, optional
        The argument values. If not specified, the values will be obtained by
        parsing the command line arguments using the `argparse` module.

    Returns
    -------
    int
        Exit code (0 if no error occurred).