def sr_test(argv):
    parser = argparse.ArgumentParser(description='Calculate enrichment of clipped reads at SV breakpoints.', prog='svtk sr-test', formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('vcf', help='VCF of variant calls. Standardized to include CHR2, END, SVTYPE, STRANDS in INFO.')
    parser.add_argument('countfile', help='Tabix indexed file of split counts. Columns: chrom,pos,clip,count,sample')
    parser.add_argument('fout', help='Output table of most significant start/endpositions.')
    parser.add_argument('-w', '--window', type=int, default=100, help='Window around variant start/end to consider for split read support. [100]')
    parser.add_argument('--common', default=False, action='store_true', help='Ignore background for common AF')
    parser.add_argument('-b', '--background', type=int, default=160, help='Number of background samples to choose for comparison in t-test. [160]')
    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)
    vcf = pysam.VariantFile(args.vcf)
    if (args.index is not None):
        countfile = pysam.TabixFile(args.countfile, index=args.index, parser=pysam.asTuple())
    else:
        if args.countfile.startswith('http'):
            raise Exception('Must provide tabix index with remote URL')
        countfile = pysam.TabixFile(args.countfile, parser=pysam.asTuple())
    if (args.fout in '- stdout'.split()):
        fout = sys.stdout
    else:
        fout = open(args.fout, 'w')
    header = 'name coord pos log_pval called_median bg_median bg_frac'.split()
    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.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 = SRTestRunner(vcf, countfile, fout, args.background, args.common, args.window, whitelist, medians=medians, log=args.log)
    runner.run()
def sr_test(argv):
    parser = argparse.ArgumentParser(description='Calculate enrichment of clipped reads at SV breakpoints.', prog='svtk sr-test', formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('vcf', help='VCF of variant calls. Standardized to include CHR2, END, SVTYPE, STRANDS in INFO.')
    parser.add_argument('countfile', help='Tabix indexed file of split counts. Columns: chrom,pos,clip,count,sample')
    parser.add_argument('fout', help='Output table of most significant start/endpositions.')
    parser.add_argument('-w', '--window', type=int, default=100, help='Window around variant start/end to consider for split read support. [100]')
    parser.add_argument('--common', default=False, action='store_true', help='Ignore background for common AF')
    parser.add_argument('-b', '--background', type=int, default=160, help='Number of background samples to choose for comparison in t-test. [160]')
    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)
    vcf = pysam.VariantFile(args.vcf)
    if (args.index is not None):
        countfile = pysam.TabixFile(args.countfile, index=args.index, parser=pysam.asTuple())
    else:
        if args.countfile.startswith('http'):
            raise Exception('Must provide tabix index with remote URL')
        countfile = pysam.TabixFile(args.countfile, parser=pysam.asTuple())
    if (args.fout in '- stdout'.split()):
        fout = sys.stdout
    else:
        fout = open(args.fout, 'w')
    header = 'name coord pos log_pval called_median bg_median bg_frac'.split()
    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.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 = SRTestRunner(vcf, countfile, fout, args.background, args.common, args.window, whitelist, medians=medians, log=args.log)
    runner.run()