Skip to main content
deleted 211 characters in body
Source Link
import pyranges as pr
assert int(pr.__version__.split(".")[2]) >= 93, "pip install pyranges==0.0.93"
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!
cresult = gr.clustermax_disjoint(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.clustermax_disjoint(slack=5000).apply(f)

# +--------------+-----------+-----------+-----------+-----------+
# | Chromosome   |     Start |       End |     Score |   Cluster |
# | (category)   |   (int32) |   (int32) |   (int64) |   (int32) |
# |--------------+-----------+-----------+-----------+-----------|
# | chr1         |      4999 |      50001 |        10 2 |         15 |
# | chr1         |     15000 |     1500110001 |         210002 |         21 |
# | chr1         |     20100 |     20101 |         5 |         3 |
# +--------------+-----------+-----------+-----------+-----------+
# Unstranded PyRanges object has 3 rows and 54 columns from 1 chromosomes.
# For printing, the PyRanges was sorted on Chromosome.
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.
import pyranges as pr
assert int(pr.__version__.split(".")[2]) >= 93, "pip install pyranges==0.0.93"
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))

result = gr.max_disjoint(slack=5000)

# small example that is easy to validate
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.max_disjoint(slack=5000)

# +--------------+-----------+-----------+-----------+
# | Chromosome   |     Start |       End |     Score |
# | (category)   |   (int32) |   (int32) |   (int64) |
# |--------------+-----------+-----------+-----------|
# | chr1         |         1 |         2 |         5 |
# | chr1         |     10001 |     10002 |         1 |
# | chr1         |     20100 |     20101 |         5 |
# +--------------+-----------+-----------+-----------+
# Unstranded PyRanges object has 3 rows and 4 columns from 1 chromosomes.
# For printing, the PyRanges was sorted on Chromosome.
deleted 1 character in body
Source Link
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, shape=lensize=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.
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, shape=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.
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.
added 1514 characters in body
Source Link

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, shape=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?

Could you please post an example of what data you have and what you want to end up with?

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, shape=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?

Source Link
Loading