Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T09:59:09.543Z
Answers.Text: Thanks @Chris_Rands, I have added it to the answer
Answers.UserDisplayName: StupidWolf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T11:55:11.927Z
Answers.Text: thanks alot for your answer @StupidWolf. What about mimicking the IRanges in R? does pyranges support that too?
Answers.UserDisplayName: aBiologist
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T12:03:31.300Z
Answers.Text: 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?
Answers.UserDisplayName: StupidWolf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T12:24:12.343Z
Answers.Text: Mimicking this type of call in R, IRanges(start=1:10, end=11)?
Answers.UserDisplayName: aBiologist
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T12:39:35.780Z
Answers.Text: whats the difference between that and creating a pyranges object with ```pyranges.from_dict({'Chromosome':1,'Start':np.arange(10),'End':11})```
Answers.UserDisplayName: StupidWolf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T12:40:53.067Z
Answers.Text: 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
Answers.UserDisplayName: StupidWolf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T17:23:09.857Z
Answers.Text: 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.
Answers.UserDisplayName: Ram RS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T09:11:55.160Z
Answers.LastActivityDate: 2020-08-24T09:58:48.420Z
Answers.LastEditDate: 2020-08-24T09:58:48.420Z
Answers.UserDisplayName: StupidWolf
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-28T19:55:44.923Z
Answers.LastActivityDate: 2021-02-28T19:55:44.923Z
Answers.UserDisplayName: The Unfun Cat
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-24T09:13:02.163Z
Comments.Text: @TheUnfunCat, do comment or provide your take, since it's your package :)
Comments.UserDisplayName: StupidWolf
Tags: <r><python><bioconductor>
Title: Python equivalent to R GRanges and IRanges
ThreadText: # 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**: @username_3, 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 @username_1. 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.
  
  
  ---
  
  