def main(argv=None):

    if argv is None:
        argv = sys.argv

    # setup command line parser
    parser = E.OptionParser(version="%prog version: $Id$",
                            usage=globals()["__doc__"])

    parser.add_option("-m", "--method", dest="method", type="choice", action="store",
                      choices=(
                          "hierarchy", "set-field", "set-pattern", "set-none"),
                      help="Method to use for conversion")

    parser.add_option("-g", "--gene-type", dest="gene_type", type="string",
                      help="feature type to get gene_id from if possible [%default]")

    parser.add_option("-t", "--transcript-type", dest="transcript_type", type="string",
                      help="feature type to get transcript_id from if possible [%default]")

    parser.add_option("-d", "--no-discard", dest="discard", action="store_false",
                      help="Do not discard feature types specified by GENE_TYPE and TRANSCRIPT_TYPE")

    parser.add_option("--gene-id", dest="gene_field_or_pattern", type="string",
                      help="Either field or pattern for the gene_id [%default]")

    parser.add_option("--transcript-id", dest="transcript_field_or_pattern", type="string",
                      help="Either field or pattern for the transcript_id [%default]")

    parser.add_option("--parent-field", dest="parent", type="string",
                      help="field that specifies the parent relationship. Currently only"
                      "if left as Parent will features with multiple parents be parsed"
                      "correctly""")

    parser.add_option("--read-twice", dest="read_twice", action="store_true",
                      help="Instead of holding the whole file in memory, read once for parsing the "
                      "hierarchy, and then again for actaully doing the conversion. Means a real file "
                      "and not a pipe must be provided.""")

    parser.add_option("--by-chrom", dest="by_chrom", action="store_true",
                      help="Parse input file one choromosome at a time. Reduces memory usage, "
                      "but input must be sorted by chromosome and features may not split accross "
                      " multiple chromosomes""")

    parser.add_option("--fail-missing-gene", dest="missing_gene", action="store_false",
                      help="Fail if no feature of type GENE_TYPE is found instead of using "
                      "defaulting to highest object in hierarchy""")

    parser.set_defaults(
        method="hierarchy",
        gene_type="gene",
        transcript_type="mRNA",
        discard=True,
        gene_field_or_pattern="ID",
        transcript_field_or_pattern="ID",
        read_twice=False,
        by_chrom=False,
        missing_gene=True,
        parent="Parent"
    )

    # add common options (-h/--help, ...) and parse command line
    (options, args) = E.Start(parser, argv=argv)

    gffs = GFF3.flat_file_iterator(options.stdin)

    if options.by_chrom:
        gffs = GFF3.chrom_iterator(gffs)
    else:
        gffs = [gffs]

    # running early so that fails early if configuration is wrong
    if options.read_twice:
        # Will throw IOError if options.stdin is not a normal file
        second_gff = GFF3.flat_file_iterator(
            IOTools.openFile(options.stdin.name))

        if options.by_chrom:
            second_gff = GFF3.chrom_iterator(second_gff)
        else:
            second_gff = iter([second_gff])
    else:
        second_gff = None

    for chunk in gffs:

        if options.read_twice:
            second_gff_chunk = second_gff.next()
        else:
            chunk = list(chunk)
            second_gff_chunk = chunk

        if options.method == "hierarchy":

            convert_hierarchy(chunk, second_gff_chunk, options)
        elif options.method == "set-field":
            gene_id_pattern = "%%(%s)s" % options.gene_field_or_pattern
            transcript_id_pattern = "%%(%s)s" % options.transcript_field_or_pattern
            convert_set(chunk, gene_id_pattern, transcript_id_pattern, options)
        elif options.method == "set-pattern":
            convert_set(chunk, options.gene_field_or_pattern,
                        options.transcript_field_or_pattern, options)
        elif options.method == "set-none":
            convert_set(chunk, None, None, options)

    # write footer and output benchmark information.
    E.Stop()
script main.
dataset/ETHPy150Open CGATOxford/cgat/scripts/gff32gtf.py/main