Code Monkey home page Code Monkey logo

Comments (3)

marco-mariotti avatar marco-mariotti commented on June 26, 2024

Based on your example, I think you're looking for PyRanges function join, rather than intersect. The latter will report only the common portions of overlapping intervals. The output of join is as you sketch.
Briefly, you want to do join, followed by a filter to drop the self-matches, and also drop the symmetric duplicates.

gr=pr.from_dict( {'Start':[1,2,10, 14], 'End':[4,5, 20, 18], 'Chromosome':['chr1']*4, 'Strand':['+']*4})
gr
+-----------+-----------+--------------+--------------+
|     Start |       End | Chromosome   | Strand       |
|   (int32) |   (int32) | (category)   | (category)   |
|-----------+-----------+--------------+--------------|
|         1 |         4 | chr1         | +            |
|         2 |         5 | chr1         | +            |
|        10 |        20 | chr1         | +            |
|        14 |        18 | chr1         | +            |
+-----------+-----------+--------------+--------------+
Stranded PyRanges object has 4 rows and 4 columns from 1 chromosomes.
For printing, the PyRanges was sorted on Chromosome and Strand.

It is convenient to have some sort of ID column that uniquely identify every interval. If you have one already (values A,B in your example), great; if not, create one:
gr.ID=np.arange(len(gr))

Run method join:
jgr=gr.join(gr)

Filter out self matches:
jgr=jgr[jgr.ID != jgr.ID_b]

To filter out symmetric duplicates, first create a temporary column whose value will be the same for duplicates:

jgr._temp =[ '_'.join(sorted( map(str, [i, ib]))) for i, ib in zip( jgr.ID, jgr.ID_b) ]
jgr
+-----------+-----------+--------------+--------------+-----------+-----------+-----------+--------------+-----------+------------+
|     Start |       End | Chromosome   | Strand       |        ID |   Start_b |     End_b | Strand_b     |      ID_b |      _temp |
|   (int32) |   (int32) | (category)   | (category)   |   (int64) |   (int32) |   (int32) | (category)   |   (int64) |   (object) |
|-----------+-----------+--------------+--------------+-----------+-----------+-----------+--------------+-----------+------------|
|         1 |         4 | chr1         | +            |         0 |         2 |         5 | +            |         1 |        0_1 |
|         2 |         5 | chr1         | +            |         1 |         1 |         4 | +            |         0 |        0_1 |
|        10 |        20 | chr1         | +            |         2 |        14 |        18 | +            |         3 |        2_3 |
|        14 |        18 | chr1         | +            |         3 |        10 |        20 | +            |         2 |        2_3 |
+-----------+-----------+--------------+--------------+-----------+-----------+-----------+--------------+-----------+------------+
Stranded PyRanges object has 4 rows and 10 columns from 1 chromosomes.
For printing, the PyRanges was sorted on Chromosome and Strand.

And take only the first row per unique value:

final_gr=pr.PyRanges( jgr.df.groupby('_temp').first() )
final_gr
+-----------+-----------+--------------+--------------+-----------+-----------+-----------+--------------+-----------+
|     Start |       End | Chromosome   | Strand       |        ID |   Start_b |     End_b | Strand_b     |      ID_b |
|   (int32) |   (int32) | (category)   | (category)   |   (int64) |   (int32) |   (int32) | (category)   |   (int64) |
|-----------+-----------+--------------+--------------+-----------+-----------+-----------+--------------+-----------|
|         1 |         4 | chr1         | +            |         0 |         2 |         5 | +            |         1 |
|        10 |        20 | chr1         | +            |         2 |        14 |        18 | +            |         3 |
+-----------+-----------+--------------+--------------+-----------+-----------+-----------+--------------+-----------+
Stranded PyRanges object has 2 rows and 9 columns from 1 chromosomes.
For printing, the PyRanges was sorted on Chromosome and Strand.

from pyranges.

marco-mariotti avatar marco-mariotti commented on June 26, 2024

@alexlenail did that work for you? If so, I will close the issue.

from pyranges.

alexlenail avatar alexlenail commented on June 26, 2024

Thanks @marco-mariotti ! it worked, but I'm still wondering if there's a more performant way. Maybe not without rolling up my sleeves and writing my own c++ 😮‍💨

from pyranges.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.