<p>You can use the <code>.extend()</code> method. Starting with your data above, <code>gr.extend({&quot;3&quot;: -10000, &quot;5&quot;: 10000})</code> would generate the following:</p>
<pre><code>+--------------+-----------+-----------+--------------+
| Chromosome   |     Start |       End | Strand       |
| (category)   |   (int32) |   (int32) | (category)   |
|--------------+-----------+-----------+--------------|
| chr1         |  10010000 |  20010000 | -            |
| chr2         |  19990000 |  29990000 | +            |
| chr22        |  30010000 |  40010000 | -            |
+--------------+-----------+-----------+--------------+
</code></pre>
<p>I don't know a way of to apply this &quot;range extension&quot; in a strand specific way (to be able to generate the desired output exactly) but if this is really needed, you should be able to generate 2 <code>gr</code> objects, one for each strand, do the manipulation and bring these together.</p>

CC BY-SA 4.0
2022-10-06T21:13:05.143Z
OP is not doing anything strand-specific, just adding 1000 to coordinates irrespective of strand. Your answer provides more flexibility than OP needs.
Ram RS
CC BY-SA 4.0
2022-10-05T17:12:03.337Z
2022-10-05T17:30:38.160Z
2022-10-05T17:30:38.160Z
haci
<p>I would like to add <code>n</code> (e.g., 1k) bases to a PyRange object (i.e., shifting PyRanges coordinates).
How can I do that?</p>
<p><strong>Input:</strong></p>
<pre class="lang-py prettyprint-override"><code>import pyranges as pr
gr = pr.random(3)

# gr:
# +--------------+-----------+-----------+--------------+
# | Chromosome   |     Start |       End | Strand       |
# | (category)   |   (int32) |   (int32) | (category)   |
# |--------------+-----------+-----------+--------------|
# | chr1         |  10000000 |  20000000 | -            |
# | chr2         |  20000000 |  30000000 | +            |
# | chr22        |  30000000 |  40000000 | -            |
# +--------------+-----------+-----------+--------------+
</code></pre>
<p><strong>Desired output:</strong></p>
<pre class="lang-py prettyprint-override"><code># magical command[gr + 1000]:
# +--------------+-----------+-----------+--------------+
# | Chromosome   |     Start |       End | Strand       |
# | (category)   |   (int32) |   (int32) | (category)   |
# |--------------+-----------+-----------+--------------|
# | chr1         |  10001000 |  20001000 | -            |
# | chr2         |  20001000 |  30001000 | +            |
# | chr22        |  30001000 |  40001000 | -            |
# +--------------+-----------+-----------+--------------+
</code></pre>

<python><pyranges>
How to add N bases to PyRange object (i.e., shifting PyRanges coordinates)
# How to add N bases to PyRange object (i.e., shifting PyRanges coordinates)

(Asked by: username_0 on 2022-10-05)

I would like to add n (e.g., 1k) bases to a PyRange object (i.e., shifting PyRanges coordinates).
How can I do that?
Input:
import pyranges as pr
gr = pr.random(3)

# gr:
# +--------------+-----------+-----------+--------------+
# | Chromosome   |     Start |       End | Strand       |
# | (category)   |   (int32) |   (int32) | (category)   |
# |--------------+-----------+-----------+--------------|
# | chr1         |  10000000 |  20000000 | -            |
# | chr2         |  20000000 |  30000000 | +            |
# | chr22        |  30000000 |  40000000 | -            |
# +--------------+-----------+-----------+--------------+

Desired output:
# magical command[gr + 1000]:
# +--------------+-----------+-----------+--------------+
# | Chromosome   |     Start |       End | Strand       |
# | (category)   |   (int32) |   (int32) | (category)   |
# |--------------+-----------+-----------+--------------|
# | chr1         |  10001000 |  20001000 | -            |
# | chr2         |  20001000 |  30001000 | +            |
# | chr22        |  30001000 |  40001000 | -            |
# +--------------+-----------+-----------+--------------+

**Tags:** python, pyranges

---

## 1 answers

### Answer by username_1 on 2022-10-05. Score: 2 | Accepted answer

You can use the .extend() method. Starting with your data above, gr.extend({"3": -10000, "5": 10000}) would generate the following:
+--------------+-----------+-----------+--------------+
| Chromosome   |     Start |       End | Strand       |
| (category)   |   (int32) |   (int32) | (category)   |
|--------------+-----------+-----------+--------------|
| chr1         |  10010000 |  20010000 | -            |
| chr2         |  19990000 |  29990000 | +            |
| chr22        |  30010000 |  40010000 | -            |
+--------------+-----------+-----------+--------------+

I don't know a way of to apply this "range extension" in a strand specific way (to be able to generate the desired output exactly) but if this is really needed, you should be able to generate 2 gr objects, one for each strand, do the manipulation and bring these together.

> **Comments:**
> **username_2**: OP is not doing anything strand-specific, just adding 1000 to coordinates irrespective of strand. Your answer provides more flexibility than OP needs.  

---

