1
$\begingroup$

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 | -            |
# +--------------+-----------+-----------+--------------+
$\endgroup$

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ OP is not doing anything strand-specific, just adding 1000 to coordinates irrespective of strand. Your answer provides more flexibility than OP needs. $\endgroup$
    – Ram RS
    Commented Oct 6, 2022 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.