15 Overlapping Ranges

PyRanges objects can be subtracted from other PyRanges to report the intervals in self that are not in other.

import pyranges as pr
gr = pr.data.aorta()
gr2 = pr.data.aorta2()
print(gr.subtract(gr2))
## +--------------+-----------+-----------+------------+-----------+--------------+
## | Chromosome   |     Start |       End | Name       |     Score | Strand       |
## | (category)   |   (int32) |   (int32) | (object)   |   (int64) | (category)   |
## |--------------+-----------+-----------+------------+-----------+--------------|
## | chr1         |      9939 |      9988 | H3K27me3   |         7 | +            |
## | chr1         |      9953 |      9988 | H3K27me3   |         5 | +            |
## | chr1         |    110246 |    110445 | H3K27me3   |         1 | +            |
## | chr1         |      9916 |      9988 | H3K27me3   |         5 | -            |
## | chr1         |      9951 |      9988 | H3K27me3   |         8 | -            |
## | chr1         |      9978 |      9988 | H3K27me3   |         7 | -            |
## +--------------+-----------+-----------+------------+-----------+--------------+
## Stranded PyRanges object has 6 rows and 6 columns from 1 chromosomes.
## For printing, the PyRanges was sorted on Chromosome and Strand.

Both methods also take a strandedness option, which can either be "same", "opposite" or False/None

print(gr.subtract(gr2, strandedness="opposite"))
## +--------------+-----------+-----------+------------+-----------+--------------+
## | Chromosome   | Start     | End       | Name       | Score     | Strand       |
## | (category)   | (int32)   | (int32)   | (object)   | (int64)   | (category)   |
## |--------------+-----------+-----------+------------+-----------+--------------|
## | chr1         | 9939      | 9988      | H3K27me3   | 7         | +            |
## | chr1         | 9953      | 9988      | H3K27me3   | 5         | +            |
## | chr1         | 10348     | 10445     | H3K27me3   | 4         | +            |
## | chr1         | 110246    | 110445    | H3K27me3   | 1         | +            |
## | ...          | ...       | ...       | ...        | ...       | ...          |
## | chr1         | 9978      | 10073     | H3K27me3   | 7         | -            |
## | chr1         | 10001     | 10073     | H3K27me3   | 5         | -            |
## | chr1         | 10272     | 10280     | H3K27me3   | 1         | -            |
## | chr1         | 10272     | 10280     | H3K27me3   | 6         | -            |
## +--------------+-----------+-----------+------------+-----------+--------------+
## Stranded PyRanges object has 10 rows and 6 columns from 1 chromosomes.
## For printing, the PyRanges was sorted on Chromosome and Strand.