def pe_test(argv):
    parser = argparse.ArgumentParser(description='Calculate enrichment of discordant pairs at SV breakpoints.', prog='svtk pe-test', formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('vcf', help='Variants.')
    parser.add_argument('disc', help='Table of discordant pair coordinates.')
    parser.add_argument('fout', type=argparse.FileType('w'), help='Output table of PE counts.')
    parser.add_argument('-o', '--window-out', type=int, default=500, help='Window outside breakpoint to query for discordant pairs. [500]')
    parser.add_argument('-i', '--window-in', type=int, default=50, help='Window inside breakpoint to query for discordant pairs. [50]')
    parser.add_argument('-b', '--background', type=int, default=160, help='Number of background samples to sample for PE evidence. [160]')
    parser.add_argument('--common', default=False, action='store_true', help='Ignore background for common AF')
    parser.add_argument('-s', '--samples', type=argparse.FileType('r'), default=None, help='Whitelist of samples to restrict testing to.')
    parser.add_argument('--index', default=None, help='Tabix index of discordant pair file. Required if discordant pair file is hosted remotely.')
    parser.add_argument('--medianfile', default=None, help="Median coverage statistics for each library (optional). If provided, each sample's split counts will be normalized accordingly. Same format as RdTest, one column per sample.")
    parser.add_argument('--log', action='store_true', default=False, help='Print progress log to stderr.')
    if (len(argv) == 0):
        parser.print_help()
        sys.exit(1)
    args = parser.parse_args(argv)
    if (args.vcf in '- stdin'.split()):
        vcf = pysam.VariantFile(sys.stdin)
    else:
        vcf = pysam.VariantFile(args.vcf)
    if (args.fout in '- stdout'.split()):
        fout = sys.stdout
    else:
        fout = args.fout
    header = 'name log_pval called_median bg_median bg_frac'.split()
    args.fout.write(('\t'.join(header) + '\n'))
    if (args.samples is not None):
        whitelist = [s.strip() for s in args.samples.readlines()]
    else:
        whitelist = None
    if (args.index is not None):
        discfile = pysam.TabixFile(args.disc, index=args.index)
    else:
        if args.disc.startswith('http'):
            raise Exception('Must provide tabix index with remote URL')
        discfile = pysam.TabixFile(args.disc)
    if (args.medianfile is not None):
        medians = pd.read_table(args.medianfile)
        medians = pd.melt(medians, var_name='sample', value_name='median_cov')
    else:
        medians = None
    runner = PETestRunner(vcf, discfile, fout, args.background, args.common, args.window_in, args.window_out, whitelist, medians=medians, log=args.log)
    runner.run()
def pe_test(argv):
    parser = argparse.ArgumentParser(description='Calculate enrichment of discordant pairs at SV breakpoints.', prog='svtk pe-test', formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('vcf', help='Variants.')
    parser.add_argument('disc', help='Table of discordant pair coordinates.')
    parser.add_argument('fout', type=argparse.FileType('w'), help='Output table of PE counts.')
    parser.add_argument('-o', '--window-out', type=int, default=500, help='Window outside breakpoint to query for discordant pairs. [500]')
    parser.add_argument('-i', '--window-in', type=int, default=50, help='Window inside breakpoint to query for discordant pairs. [50]')
    parser.add_argument('-b', '--background', type=int, default=160, help='Number of background samples to sample for PE evidence. [160]')
    parser.add_argument('--common', default=False, action='store_true', help='Ignore background for common AF')
    parser.add_argument('-s', '--samples', type=argparse.FileType('r'), default=None, help='Whitelist of samples to restrict testing to.')
    parser.add_argument('--index', default=None, help='Tabix index of discordant pair file. Required if discordant pair file is hosted remotely.')
    parser.add_argument('--medianfile', default=None, help="Median coverage statistics for each library (optional). If provided, each sample's split counts will be normalized accordingly. Same format as RdTest, one column per sample.")
    parser.add_argument('--log', action='store_true', default=False, help='Print progress log to stderr.')
    if (len(argv) == 0):
        parser.print_help()
        sys.exit(1)
    args = parser.parse_args(argv)
    if (args.vcf in '- stdin'.split()):
        vcf = pysam.VariantFile(sys.stdin)
    else:
        vcf = pysam.VariantFile(args.vcf)
    if (args.fout in '- stdout'.split()):
        fout = sys.stdout
    else:
        fout = args.fout
    header = 'name log_pval called_median bg_median bg_frac'.split()
    args.fout.write(('\t'.join(header) + '\n'))
    if (args.samples is not None):
        whitelist = [s.strip() for s in args.samples.readlines()]
    else:
        whitelist = None
    if (args.index is not None):
        discfile = pysam.TabixFile(args.disc, index=args.index)
    else:
        if args.disc.startswith('http'):
            raise Exception('Must provide tabix index with remote URL')
        discfile = pysam.TabixFile(args.disc)
    if (args.medianfile is not None):
        medians = pd.read_table(args.medianfile)
        medians = pd.melt(medians, var_name='sample', value_name='median_cov')
    else:
        medians = None
    runner = PETestRunner(vcf, discfile, fout, args.background, args.common, args.window_in, args.window_out, whitelist, medians=medians, log=args.log)
    runner.run()