I have a pyranges object that I am looking to iterate over. For each range, I would like to "resize" it to its centre (so start+end/2
) and save that into a separate pyranges object. There will be additional processing associated based on what ranges already exist nearby, so I can't just use a resize
function, I have to iterate over the object.
First, how would I go about manually creating a new pyranges object, then inserting one by one new ranges into it?
Second, when I try to iterate over my current pyranges object (using this line for myspot in pr.itergrs(hotspots, strand=False) :
), it gives me this error: AttributeError: 'tuple' object has no attribute 'stranded'
. This error occurs whether or not I have strands in my pyranges, and whether or not I specify strand=False
. Why does this occur?
The data I am working with: a pyranges covering hotspots in the human genome (all chr1 for the moment)
+--------------+-----------+-----------+-----------+
| Chromosome | Start | End | Score |
| (category) | (int32) | (int32) | (int64) |
|--------------+-----------+-----------+-----------|
| chr1 | 154421820 | 154421821 | 26413 |
| chr1 | 30749469 | 30749470 | 14833 |
| chr1 | 55259503 | 55259504 | 9900 |
| chr1 | 14985097 | 14985098 | 7735 |
| ... | ... | ... | ... |
| chr1 | 247975751 | 247976497 | 1 |
| chr1 | 247977599 | 247981391 | 1 |
| chr1 | 248018012 | 248019454 | 1 |
| chr1 | 248177298 | 248178490 | 1 |
+--------------+-----------+-----------+-----------+
The hotspots are sorted by their score, and I'd like to go through and for each one, resize it to its centre, keeping the score, but if there is already a hotspot within a certain range (say +/-5kb), to discard it instead, so that I end up with clear chunks between hotspots. This is the "pseudocode" I was planning to do to implement this:
add centre of hotspot 1 to a new pyranges object
create ranges with score = -1 for 5kb flanking each side
for myhotspot in hotspots:
resize to centre
if not overlap with new pyranges object
add centre
create flanking regions
discard ranges with score<0
This may not be the most elegant solution, but I'll never have more than ~2000 ranges to process so it should work fine. Is this implementable with pyranges?