-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
84 lines (57 loc) · 1.67 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: "PyRanges documentation"
author: "Endre Bakken Stovner"
date: "`r Sys.Date()`"
site: "bookdown::bookdown_site"
output:
bookdown::gitbook: default
documentclass: book
---
# Introduction to PyRanges
PyRanges are collections of intervals that support comparison operations (like
overlap and intersection) and other methods that are useful for genomic
analyses. The ranges can have an arbitrary number of meta-data fields, i.e.
columns associated with them.
The data in PyRanges objects are stored in a pandas dataframe. This means the
vast Python ecosystem for high-performance scientific computing is available to
manipulate the data in PyRanges-objects.
```{python tidy=FALSE}
import pyranges as pr
from pyranges import PyRanges
import pandas as pd
from io import StringIO
f1 = """Chromosome Start End Score Strand
chr1 4 7 23.8 +
chr1 6 11 0.13 -
chr2 0 14 42.42 +"""
df1 = pd.read_table(StringIO(f1), sep="\s+")
gr1 = PyRanges(df1)
```
Now we can subset the PyRange in various ways:
```{python}
print(gr1)
print(gr1["chr1", 0:5])
print(gr1["chr1", "-", 6:100])
print(gr1.Score)
```
And we can perform comparison operations with two PyRanges:
```{python}
f2 = """Chromosome Start End Score Strand
chr1 5 6 -0.01 -
chr1 9 12 200 +
chr3 0 14 21.21 -"""
df2 = pd.read_table(StringIO(f2), sep="\s+")
gr2 = PyRanges(df2)
print(gr2)
print(gr1.intersection(gr2, strandedness="opposite"))
print(gr1.intersection(gr2, strandedness=False))
```
There are also convenience methods for single PyRanges:
```{python}
# The range objects also contain other convenience functions.
print(gr1.cluster())
```
The underlying dataframe can always be accessed:
```{python}
print(gr1.df)
```