12 Run Length Encoding dicts

Since you need more than one run length to describe a genome with multiple chromosomes, pyranges has a datastructure called PyRles for collections of Rles. It can be created from a PyRanges object by invoking the coverage function.

Rledicts support the arithmetic operations +, -, /, and *.

import pyranges as pr
gr = pr.load_dataset("chipseq")
gr_bg = pr.load_dataset("chipseq_background")
cs = gr.coverage()
print(cs)
## chr1
## +--------+-----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## | Runs   |   1325303 |   25 |   216270 |   25 |   57498 |  ...    |   25 |   167465 |   25 |   989343 |   25 |
## |--------+-----------+------+----------+------+---------+---------+------+----------+------+----------+------|
## | Values |         0 |    1 |        0 |    1 |       0 | ...     |    1 |        0 |    1 |        0 |    1 |
## +--------+-----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## Rle of length 247134924 containing 1762 elements
## ...
## chrY
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+----------+------+
## | Runs   |   7046809 |   25 |   147506 |   25 |   211011 |  ...    |   25 |   143271 |   25 |   156610 |   25 |
## |--------+-----------+------+----------+------+----------+---------+------+----------+------+----------+------|
## | Values |         0 |    1 |        0 |    1 |        0 | ...     |    1 |        0 |    1 |        0 |    1 |
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+----------+------+
## Rle of length 22210662 containing 46 elements
## Unstranded PyRles object with 24 chromosomes.
bg = gr_bg.coverage()
print(bg)
## chr1
## +--------+----------+------+----------+------+---------+---------+------+----------+------+-----------+------+
## | Runs   |   887771 |   25 |   106864 |   25 |   46417 |  ...    |   25 |   656076 |   25 |   1261276 |   25 |
## |--------+----------+------+----------+------+---------+---------+------+----------+------+-----------+------|
## | Values |        0 |    1 |        0 |    1 |       0 | ...     |    1 |        0 |    1 |         0 |    1 |
## +--------+----------+------+----------+------+---------+---------+------+----------+------+-----------+------+
## Rle of length 246875649 containing 1480 elements
## ...
## chrY
## +--------+------------+------+--------+------+---------+---------+------+-----------+------+------------+------+
## | Runs   |   10629111 |   25 |   3320 |   25 |   11045 |  ...    |   25 |   8620274 |   25 |   36845024 |   25 |
## |--------+------------+------+--------+------+---------+---------+------+-----------+------+------------+------|
## | Values |          0 |    1 |      0 |    1 |       0 | ...     |    1 |         0 |    1 |          0 |    1 |
## +--------+------------+------+--------+------+---------+---------+------+-----------+------+------------+------+
## Rle of length 57402239 containing 16 elements
## Unstranded PyRles object with 25 chromosomes.
print(cs + bg)
## chr1
## +--------+----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## | Runs   |   887771 |   25 |   106864 |   25 |   46417 |  ...    |   25 |   730068 |   25 |   259250 |   25 |
## |--------+----------+------+----------+------+---------+---------+------+----------+------+----------+------|
## | Values |        0 |    1 |        0 |    1 |       0 | ...     |    1 |        0 |    1 |        0 |    1 |
## +--------+----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## Rle of length 247134924 containing 3242 elements
## ...
## chrY
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+------------+------+
## | Runs   |   7046809 |   25 |   147506 |   25 |   211011 |  ...    |   25 |   156610 |   25 |   35191552 |   25 |
## |--------+-----------+------+----------+------+----------+---------+------+----------+------+------------+------|
## | Values |         0 |    1 |        0 |    1 |        0 | ...     |    1 |        0 |    1 |          0 |    1 |
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+------------+------+
## Rle of length 57402239 containing 62 elements
## Unstranded PyRles object with 25 chromosomes.

When using arithmetic operations with a stranded and an unstranded PyRle, the stranded PyRle is automatically demoted to an unstranded PyRle.

bg_stranded = gr_bg.coverage(stranded=True)
print(bg_stranded)
## chr1 +
## +--------+-----------+------+-----------+------+----------+---------+------+----------+------+-----------+------+
## | Runs   |   1041102 |   25 |   1088232 |   25 |   109724 |  ...    |   25 |   719119 |   25 |   1774357 |   25 |
## |--------+-----------+------+-----------+------+----------+---------+------+----------+------+-----------+------|
## | Values |         0 |    1 |         0 |    1 |        0 | ...     |    1 |        0 |    1 |         0 |    1 |
## +--------+-----------+------+-----------+------+----------+---------+------+----------+------+-----------+------+
## Rle of length 245614348 containing 724 elements
## ...
## chrY -
## +--------+------------+------+--------+------+-----------+------+---------+------+------------+------+
## | Runs   |   10629111 |   25 |   3320 |   25 |   1286333 |   25 |   18027 |   25 |   45465323 |   25 |
## |--------+------------+------+--------+------+-----------+------+---------+------+------------+------|
## | Values |          0 |    1 |      0 |    1 |         0 |    1 |       0 |    1 |          0 |    1 |
## +--------+------------+------+--------+------+-----------+------+---------+------+------------+------+
## Rle of length 57402239 containing 10 elements
## PyRles object with 50 chromosomes/strand pairs.
print(cs + bg_stranded)
## chr1
## +--------+----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## | Runs   |   887771 |   25 |   106864 |   25 |   46417 |  ...    |   25 |   730068 |   25 |   259250 |   25 |
## |--------+----------+------+----------+------+---------+---------+------+----------+------+----------+------|
## | Values |        0 |    1 |        0 |    1 |       0 | ...     |    1 |        0 |    1 |        0 |    1 |
## +--------+----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## Rle of length 247134924 containing 3242 elements
## ...
## chrY
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+------------+------+
## | Runs   |   7046809 |   25 |   147506 |   25 |   211011 |  ...    |   25 |   156610 |   25 |   35191552 |   25 |
## |--------+-----------+------+----------+------+----------+---------+------+----------+------+------------+------|
## | Values |         0 |    1 |        0 |    1 |        0 | ...     |    1 |        0 |    1 |          0 |    1 |
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+------------+------+
## Rle of length 57402239 containing 62 elements
## Unstranded PyRles object with 25 chromosomes.

Like Rles, PyGRles supports arithmetic operations with numbers.

print((0.67 + cs) * 5)
## chr1
## +--------+-----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## | Runs   |   1325303 |   25 |   216270 |   25 |   57498 |  ...    |   25 |   167465 |   25 |   989343 |   25 |
## |--------+-----------+------+----------+------+---------+---------+------+----------+------+----------+------|
## | Values |      3.35 | 8.35 |     3.35 | 8.35 |    3.35 | ...     | 8.35 |     3.35 | 8.35 |     3.35 | 8.35 |
## +--------+-----------+------+----------+------+---------+---------+------+----------+------+----------+------+
## Rle of length 247134924 containing 1762 elements
## ...
## chrY
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+----------+------+
## | Runs   |   7046809 |   25 |   147506 |   25 |   211011 |  ...    |   25 |   143271 |   25 |   156610 |   25 |
## |--------+-----------+------+----------+------+----------+---------+------+----------+------+----------+------|
## | Values |      3.35 | 8.35 |     3.35 | 8.35 |     3.35 | ...     | 8.35 |     3.35 | 8.35 |     3.35 | 8.35 |
## +--------+-----------+------+----------+------+----------+---------+------+----------+------+----------+------+
## Rle of length 22210662 containing 46 elements
## Unstranded PyRles object with 24 chromosomes.