Python equivalent to R GRanges and IRanges

Problem:

I am trying to convert some codes written in R to Python and part of that conversion process is find classes equivalent to the GRanges and IRanges from the GenomicRanges R package in Python. https://bioconductor.org/packages/release/bioc/vignettes/GenomicRanges/inst/doc/GenomicRangesIntroduction.html#granges-genomic-ranges

I couldn't find any equivalent library written in Python that does the same operations as the aforementioned R classes. There is however pyranges library in Python but it is not as flexible as GRanges and doesn't have IRanges implemented in it. https://github.com/biocore-ntnu/pyranges

For example in R I could add the following in the GRanges class:

gr = GRanges(
seq = Rle(DF$$chr), ranges = IRanges(DF$$Start,DF$$End), ref = DF$$ref,
alt = DF$$alt, sampleID=DF$$sampleID,
seqlengths = chromSize)


However, pyranges don't allow you to add arguments other than (chr, start, end, strand). However, in my case, I need to have the option to add sample_id, seqlengths and other arguments.

• @TheUnfunCat, do comment or provide your take, since it's your package :) – StupidWolf Aug 24 '20 at 9:13

I am not sure if your question has anything to do with IRanges. If I get it correct, the limitation you have with pyranges now is adding extra columns to the meta data, like adding values to @values slot in granges.

You can do it using insert or setattr :

import pyranges
import pandas as pd
import numpy as np
gr = pyranges.random(10)

setattr(gr,'alt',np.random.choice(['A','T','G','C'],10))

gr.insert(pd.DataFrame({'ref':np.random.choice(['A','T','G','C'],10)}))

+--------------+-----------+-----------+--------------+------------+------------+
| Chromosome   | Start     | End       | Strand       | alt        | ref        |
| (category)   | (int32)   | (int32)   | (category)   | (object)   | (object)   |
|--------------+-----------+-----------+--------------+------------+------------|
| chr1         | 27431195  | 27431295  | -            | C          | G          |
| chr2         | 197045893 | 197045993 | -            | A          | G          |
| chr4         | 86316012  | 86316112  | -            | C          | A          |
| chr8         | 4560598   | 4560698   | -            | G          | G          |
| ...          | ...       | ...       | ...          | ...        | ...        |
| chr11        | 130395526 | 130395626 | -            | A          | A          |
| chr12        | 28557156  | 28557256  | +            | T          | G          |
| chr13        | 55519337  | 55519437  | -            | T          | C          |
| chr14        | 1807028   | 1807128   | +            | G          | A          |
+--------------+-----------+-----------+--------------+------------+------------+
Stranded PyRanges object has 10 rows and 6 columns from 10 chromosomes.
For printing, the PyRanges was sorted on Chromosome and Strand.


Or directly adding them like this (thanks to @ChrisRands for pointing it out):

gr.alt = np.random.choice(['A','T','G','C'],10)

• Thanks @Chris_Rands, I have added it to the answer – StupidWolf Aug 24 '20 at 9:59
• thanks alot for your answer @StupidWolf. What about mimicking the IRanges in R? does pyranges support that too? – aBiologist Aug 24 '20 at 11:55
• what function do u need from IRanges? as in.. mimicking IRanges is really broad. Is there something you need to do specifically with IRanges and you want to reproduce that in python? – StupidWolf Aug 24 '20 at 12:03
• Mimicking this type of call in R, IRanges(start=1:10, end=11)? – aBiologist Aug 24 '20 at 12:24
• whats the difference between that and creating a pyranges object with pyranges.from_dict({'Chromosome':1,'Start':np.arange(10),'End':11}) – StupidWolf Aug 24 '20 at 12:39

In pyranges you would not need a call like the one you show. You can just convert a dataframe with the columns you mention above to a pyranges like this:

import pyranges as pr
gr = pr.PyRanges(your_df)


This avoids the code duplication shown in your example. The column names in the df are preserved in the PyRanges.