<p>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 <code>@values</code> slot in <code>granges</code>.</p>
<p>You can do it using <code>insert</code> or <code>setattr</code> :</p>
<pre><code>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.
</code></pre>
<p>Or directly adding them like this (thanks to @ChrisRands for pointing it out):</p>
<pre><code>gr.alt = np.random.choice(['A','T','G','C'],10)
</code></pre>

CC BY-SA 4.0
2020-08-24T09:59:09.543Z
Thanks @Chris_Rands, I have added it to the answer
StupidWolf
CC BY-SA 4.0
2020-08-24T11:55:11.927Z
thanks alot for your answer @StupidWolf. What about mimicking the IRanges in R? does pyranges support that too?
aBiologist
CC BY-SA 4.0
2020-08-24T12:03:31.300Z
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
CC BY-SA 4.0
2020-08-24T12:24:12.343Z
Mimicking this type of call in R, IRanges(start=1:10, end=11)?
aBiologist
CC BY-SA 4.0
2020-08-24T12:39:35.780Z
whats the difference between that and creating a pyranges object with ```pyranges.from_dict({'Chromosome':1,'Start':np.arange(10),'End':11})```
StupidWolf
CC BY-SA 4.0
2020-08-24T12:40:53.067Z
you cannot mimick a call... it's really weird... you have a function and it must do something, which is my question in the first place
StupidWolf
CC BY-SA 4.0
2020-08-24T17:23:09.857Z
Yeah, functionality can be "mimic"-ed i.e. with an equivalent implementation, but one cannot expect to Find-Replace function names and expect code to be ported over to a new language. I guess OP has some R code and wishes to implement that in py.
Ram RS
CC BY-SA 4.0
2020-08-24T09:11:55.160Z
2020-08-24T09:58:48.420Z
2020-08-24T09:58:48.420Z
StupidWolf
<p>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:</p>
<pre><code>import pyranges as pr
gr = pr.PyRanges(your_df)
</code></pre>
<p>This avoids the code duplication shown in your example. The column names in the df are preserved in the PyRanges.</p>

CC BY-SA 4.0
2021-02-28T19:55:44.923Z
2021-02-28T19:55:44.923Z
The Unfun Cat
<p><strong>Problem:</strong></p>
<p>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. <a href="https://bioconductor.org/packages/release/bioc/vignettes/GenomicRanges/inst/doc/GenomicRangesIntroduction.html#granges-genomic-ranges" rel="nofollow noreferrer">https://bioconductor.org/packages/release/bioc/vignettes/GenomicRanges/inst/doc/GenomicRangesIntroduction.html#granges-genomic-ranges</a></p>
<p>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. <a href="https://github.com/biocore-ntnu/pyranges" rel="nofollow noreferrer">https://github.com/biocore-ntnu/pyranges</a></p>
<p>For example in R I could add the following in the GRanges class:</p>
<pre><code>gr = GRanges(
seq = Rle(DF<span class="math-container">$chr),
ranges = IRanges(DF$</span>Start,DF<span class="math-container">$End),
ref = DF$</span>ref,
alt = DF<span class="math-container">$alt,
sampleID=DF$</span>sampleID,
seqlengths = chromSize)
</code></pre>
<p>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.</p>
<p>Thank you in advance.
Best</p>

CC BY-SA 4.0
2020-08-24T09:13:02.163Z
@TheUnfunCat, do comment or provide your take, since it's your package :)
StupidWolf
<r><python><bioconductor>
Python equivalent to R GRanges and IRanges
# Python equivalent to R GRanges and IRanges

(Asked by: username_0 on 2020-08-23)

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.
Thank you in advance.
Best

**Tags:** r, python, bioconductor
> **Comments:**
> **username_1**: @TheUnfunCat, do comment or provide your take, since it's your package :)  

---

## 2 answers

### Answer by username_1 on 2020-08-24. Score: 3

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)

> **Comments:**
> **username_1**: Thanks @Chris_Rands, I have added it to the answer  
> **username_0**: thanks alot for your answer @StupidWolf. What about mimicking the IRanges in R? does pyranges support that too?  
> **username_1**: 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?  
> **username_0**: Mimicking this type of call in R, IRanges(start=1:10, end=11)?  
> **username_1**: whats the difference between that and creating a pyranges object with ```pyranges.from_dict({'Chromosome':1,'Start':np.arange(10),'End':11})```  
> **username_1**: you cannot mimick a call... it's really weird... you have a function and it must do something, which is my question in the first place  
> **username_2**: Yeah, functionality can be "mimic"-ed i.e. with an equivalent implementation, but one cannot expect to Find-Replace function names and expect code to be ported over to a new language. I guess OP has some R code and wishes to implement that in py.  

---

### Answer by username_3 on 2021-02-28. Score: 0

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.


---

