29 Subsetting PyRles

Rles can be subsetted on Chromosome, Strand or Chromosome/Strand pairs

import pyranges as pr
gr = pr.data.chipseq()
c = gr.to_rle()
print(c["chr1"])
## chr1 +
## --
## +--------+-----------+------+---------+-------+------+-----------+------+
## | Runs   | 1541598   | 25   | 57498   | ...   | 25   | 1156833   | 25   |
## |--------+-----------+------+---------+-------+------+-----------+------|
## | Values | 0.0       | 1.0  | 0.0     | ...   | 1.0  | 0.0       | 1.0  |
## +--------+-----------+------+---------+-------+------+-----------+------+
## Rle of length 247134924 containing 894 elements (avg. length 276437.275)
## 
## chr1 -
## --
## +--------+-----------+------+----------+-------+------+----------+------+
## | Runs   | 1325303   | 25   | 494957   | ...   | 25   | 962494   | 25   |
## |--------+-----------+------+----------+-------+------+----------+------|
## | Values | 0.0       | 1.0  | 0.0      | ...   | 1.0  | 0.0      | 1.0  |
## +--------+-----------+------+----------+-------+------+----------+------+
## Rle of length 246145556 containing 868 elements (avg. length 283577.829)
## RleDict object with 2 chromosomes/strand pairs.
print(c["chr1", "+"])
## +--------+-----------+------+---------+-------+------+-----------+------+
## | Runs   | 1541598   | 25   | 57498   | ...   | 25   | 1156833   | 25   |
## |--------+-----------+------+---------+-------+------+-----------+------|
## | Values | 0.0       | 1.0  | 0.0     | ...   | 1.0  | 0.0       | 1.0  |
## +--------+-----------+------+---------+-------+------+-----------+------+
## Rle of length 247134924 containing 894 elements (avg. length 276437.275)
print(c["-"])
## chr1 -
## +--------+-----------+------+----------+-------+------+----------+------+
## | Runs   | 1325303   | 25   | 494957   | ...   | 25   | 962494   | 25   |
## |--------+-----------+------+----------+-------+------+----------+------|
## | Values | 0.0       | 1.0  | 0.0      | ...   | 1.0  | 0.0      | 1.0  |
## +--------+-----------+------+----------+-------+------+----------+------+
## Rle of length 246145556 containing 868 elements (avg. length 283577.829)
## ...
## chrY -
## +--------+-----------+------+----------+-------+------+----------+------+
## | Runs   | 7046809   | 25   | 358542   | ...   | 25   | 156610   | 25   |
## |--------+-----------+------+----------+-------+------+----------+------|
## | Values | 0.0       | 1.0  | 0.0      | ...   | 1.0  | 0.0      | 1.0  |
## +--------+-----------+------+----------+-------+------+----------+------+
## Rle of length 22210662 containing 32 elements (avg. length 694083.188)
## RleDict object with 24 chromosomes/strand pairs.

To get the coverage of one or more intervals, you can use a PyRanges-object to subset the PyRles

import pyranges as pr
f1 = pr.data.f1()
print(f1)
## +--------------+-----------+-----------+------------+-----------+--------------+
## | Chromosome   |     Start |       End | Name       |     Score | Strand       |
## | (category)   |   (int32) |   (int32) | (object)   |   (int64) | (category)   |
## |--------------+-----------+-----------+------------+-----------+--------------|
## | chr1         |         3 |         6 | interval1  |         0 | +            |
## | chr1         |         8 |         9 | interval3  |         0 | +            |
## | chr1         |         5 |         7 | interval2  |         0 | -            |
## +--------------+-----------+-----------+------------+-----------+--------------+
## Stranded PyRanges object has 3 rows and 6 columns from 1 chromosomes.
## For printing, the PyRanges was sorted on Chromosome and Strand.
f1_c = f1.to_rle()
print(f1_c)
## chr1 +
## --
## +--------+-----+-----+-----+-----+
## | Runs   | 3   | 3   | 2   | 1   |
## |--------+-----+-----+-----+-----|
## | Values | 0.0 | 1.0 | 0.0 | 1.0 |
## +--------+-----+-----+-----+-----+
## Rle of length 9 containing 4 elements (avg. length 2.25)
## 
## chr1 -
## --
## +--------+-----+-----+
## | Runs   | 5   | 2   |
## |--------+-----+-----|
## | Values | 0.0 | 1.0 |
## +--------+-----+-----+
## Rle of length 7 containing 2 elements (avg. length 3.5)
## RleDict object with 2 chromosomes/strand pairs.
print(f1_c[f1])
## +--------------+-----------+-----------+-----------+-----------+-------+
## | Chromosome   |     Start |       End |        ID |       Run | +2    |
## | (object)     |   (int64) |   (int64) |   (int64) |   (int64) | ...   |
## |--------------+-----------+-----------+-----------+-----------+-------|
## | chr1         |         3 |         6 |         0 |         3 | ...   |
## | chr1         |         8 |         9 |         1 |         1 | ...   |
## | chr1         |         5 |         7 |         0 |         2 | ...   |
## +--------------+-----------+-----------+-----------+-----------+-------+
## Stranded PyRanges object has 3 rows and 7 columns from 1 chromosomes.
## For printing, the PyRanges was sorted on Chromosome and Strand.
## 2 hidden columns: Value, Strand