pybedtools.bedtool.BedTool.randomintersection

BedTool.randomintersection(other, iterations, intersect_kwargs=None, shuffle_kwargs=None, debug=False, report_iterations=False, processes=None, _orig_processes=None)[source]

Perform iterations shufflings, each time intersecting with other.

Returns a generator of integers where each integer is the number of intersections of a shuffled file with other. This distribution can be used in downstream analysis for things like empirical p-values.

intersect_kwargs and shuffle_kwargs are passed to self.intersect() and self.shuffle() respectively. By default for intersect, u=True is specified – but s=True might be a useful option for strand-specific work.

Useful kwargs for shuffle_kwargs are chrom, excl, or incl. If you use the “seed” kwarg, that seed will be used each time shuffleBed is called – so all your randomization results will be identical for each iteration. To get around this and to allow for tests, debug=True will set the seed to the iteration number. You may also break up the intersections across multiple processes with processes > 1.

Example usage:

>>> chromsizes = {'chr1':(0, 1000)}
>>> a = pybedtools.example_bedtool('a.bed')
>>> a = a.set_chromsizes(chromsizes)
>>> b = pybedtools.example_bedtool('b.bed')
>>> results = a.randomintersection(b, 10, debug=True)
>>> print(list(results))
[1, 0, 1, 2, 4, 2, 2, 1, 2, 4]