def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if not argv:
        argv = sys.argv

    # setup command line parser
    parser = E.OptionParser(
        version="%prog version: $Id: snp2table.py 2861 2010-02-23 17:36:32Z andreas $", usage=globals()["__doc__"])

    parser.add_option("-g", "--genome-file", dest="genome_file", type="string",
                      help="filename with genome [default=%default].")
    parser.add_option("-a", "--annotations-tsv-file", dest="filename_annotations", type="string",
                      help="filename with base annotations (output from gtf2fasta.py) [default=%default].")
    parser.add_option("-f", "--exons-file", dest="filename_exons", type="string",
                      help="filename with exon information (gff formatted file)  [default=%default].")
    parser.add_option("-j", "--junctions-bed-file", dest="filename_junctions", type="string",
                      help="filename with junction information (filename with exon junctions)  [default=%default].")
    parser.add_option("-c", "--vcf-file", dest="filename_vcf", type="string",
                      help="vcf file to parse [default=%default].")
    parser.add_option("-i", "--input-format", dest="input_format", type="choice",
                      choices=("pileup", "vcf"),
                      help="input format [default=%default].")
    parser.add_option("--vcf-sample", dest="vcf_sample", type="string",
                      help="sample id in vcf file to analyse [default=%default].")

    parser.set_defaults(
        genome_file=None,
        filename_annotations=None,
        filename_exons=None,
        filename_junctions=None,
        input_format="pileup",
        vcf_sample=None,
        filename_vcf=None,
    )

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

    ninput, nskipped, noutput = 0, 0, 0

    if options.genome_file:
        fasta = IndexedFasta.IndexedFasta(options.genome_file)
    else:
        fasta = None

    if options.filename_junctions:
        junctions = readJunctions(options.filename_junctions)
    else:
        junctions = None

    # setup iterator
    if options.input_format == "pileup":
        iterator = pysam.Pileup.iterate(sys.stdin)
    elif options.input_format == "vcf":
        if not options.vcf_sample:
            raise ValueError(
                "vcf format requires sample id (--vcf-sample) to be set")
        if not options.filename_vcf:
            raise ValueError(
                "reading from vcf requires vcf filename (--filename-vcf) to be set)")

        iterator = pysam.Pileup.iterate_from_vcf(
            options.filename_vcf, options.vcf_sample)

    modules = []
    modules.append(BaseAnnotatorSNP())

    if options.filename_exons:
        modules.append(BaseAnnotatorExons(options.filename_exons, fasta=fasta))
    if options.filename_annotations:
        modules.append(BaseAnnotatorCodon(
            options.filename_annotations, fasta=fasta, junctions=junctions))
    if options.filename_junctions:
        modules.append(
            BaseAnnotatorSpliceSites(options.filename_junctions, fasta=fasta))

    options.stdout.write("\t".join([x.getHeader() for x in modules]) + "\n")

    for snp in iterator:
        ninput += 1

        # translate chromosome according to fasta
        if fasta:
            try:
                snp = snp._replace(chromosome=fasta.getToken(snp.chromosome))
            except KeyError:
                E.warn("unknown contig `%s` for snp `%s`" %
                       (snp.chromosome, str(snp)))
                continue

        for module in modules:
            module.update(snp)

        options.stdout.write("\t".join(map(str, modules)) + "\n")

        noutput += 1

    E.info("ninput=%i, noutput=%i, nskipped=%i" % (ninput, noutput, nskipped))

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