Answer to main question:
import pyranges as pr
import numpy as np
np.random.seed(42 * 10)
# create large df to test on
gr = pr.random(int(1e5), length=1, chromsizes={"chr1": 249250621})
gr.Score = np.random.randint(250, size=len(gr))
# function that does what you want
f = lambda df: df.loc[df.groupby("Cluster").Score.idxmax()]
# tada!
c = gr.cluster(slack=5000).apply(f)
# small example that is easy to validate
# if several hotspots, choose the one with the highest score
gr2 = pr.from_string("""Chromosome Start End Score
chr1 1 2 5
chr1 4999 5000 10
chr1 10001 10002 1
chr1 15000 15001 2
chr1 20100 20101 5
""")
gr2.cluster(slack=5000).apply(f)
# +--------------+-----------+-----------+-----------+-----------+
# | Chromosome | Start | End | Score | Cluster |
# | (category) | (int32) | (int32) | (int64) | (int32) |
# |--------------+-----------+-----------+-----------+-----------|
# | chr1 | 4999 | 5000 | 10 | 1 |
# | chr1 | 15000 | 15001 | 2 | 2 |
# | chr1 | 20100 | 20101 | 5 | 3 |
# +--------------+-----------+-----------+-----------+-----------+
# Unstranded PyRanges object has 3 rows and 5 columns from 1 chromosomes.
# For printing, the PyRanges was sorted on Chromosome.
Could you please post an example of what data you have and what you want to end up with?
Inserting row after row is going to be slow, whether it is pandas or pyranges. It is best to collect all the data and then create a new dataframe or PyRanges.
pr.itergrs
is for iterating over multiple pyranges at the same time. It takes a list of pyranges while you seem to have a collection of tuples. The function iterates over every chromosome/strand pair in the list and for it extracts the data from every pyranges. See the code example at the end.
To iterate over a pyranges you can do for k, df in gr: # ...
.
Anyways, to do what you want:
import pyranges as pr
import numpy as np
f1 = pr.data.f1()
f1.Start = ((f1.Start + f1.End)/2).astype(np.int32) # int is 64 bit
f1.End = f1.Start + 1
- I understand I have not done the addititonal processing you speak of, but if you can post an example I'm sure I'd be able to help you :)
Code example pr.itergrs
:
import pyranges as pr
f1 = pr.data.f1()
f2 = pr.data.f2()
for k, dfs in pr.itergrs([f1, f2], keys=True):
print(k)
for df in dfs:
print("df:")
print(df)
('chr1', '+')
df:
Chromosome Start End Name Score Strand
0 chr1 3 6 interval1 0 +
2 chr1 8 9 interval3 0 +
df:
Chromosome Start End Name Score Strand
0 chr1 1 2 a 0 +
('chr1', '-')
df:
Chromosome Start End Name Score Strand
1 chr1 5 7 interval2 0 -
df:
Chromosome Start End Name Score Strand
1 chr1 6 7 b 0 -