#!/usr/bin/python

"""
Test for running many random intersections in parallel.  Use this for making
sure the open file bug (#38) doesn't return.  Not part of the standard test
suite because of the amount of time it takes to run....

Stored here because it's a test, but .py extension removed so nosetests doesn't
find it.
"""
import pybedtools
import sys
import os

prog = sys.argv[0]

usage = """
    Tries to create the open file bug by performing 10k iterations.

    To test BedTool.randomstats:

        %(prog)s randomstats

    To test BedTool.naive_jaccard:

        %(prog)s jaccard

    """ % locals()

if not os.path.exists('TEMP'):
    os.makedirs('TEMP')

if len(sys.argv) < 2:
    print usage
    sys.exit(1)

pybedtools.set_tempdir('TEMP')

gfn = pybedtools.chromsizes_to_file({'chr1': (0, 1000)})

a = pybedtools.example_bedtool('a.bed').set_chromsizes({'chr1': (1, 1000)})
b = pybedtools.example_bedtool('b.bed')


if sys.argv[1] == 'randomstats':
    results = a.randomstats(b, 10000, report_iterations=True, processes=8,
                            intersect_kwargs={'sorted': True})

elif sys.argv[1] == 'newrandomstats':
    results = a.randomstats(b, 10000, processes=8,
                            new=True, genome_fn=gfn,
                            intersect_kwargs={'sorted': True})

elif sys.argv[1] == 'jaccard':
    results = a.random_jaccard(other=b, iterations=10000, genome_fn=gfn,
                               processes=8, shuffle_kwargs=dict(chrom=True),
                               intersect_kwargs=None)
else:
    print usage
    sys.exit(1)
