Comments (3)
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.
@alexlenail did that work for you? If so, I will close the issue.
from pyranges.
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)
- left join broken with ValueError and TypeError HOT 22
- Future warning when trying to find intersection between non overlapping ranges HOT 9
- pandas futurewarning in init.py HOT 2
- Additional how-to-pages HOT 1
- Stable non-conflicting interface: v.1.0.0 HOT 2
- Return overlap between PyRanges objects, keeping columns from both? HOT 6
- subtract does not respect strandedness default
- when using .coverage: AttributeError: module 'numpy' has no attribute 'long' HOT 1
- count_overlaps causes overflow errors when number of reads are large HOT 1
- ModuleNotFoundError: No module named 'pyfaidx' HOT 2
- count_overlaps documentation says it supports nb_cpu but it is not implemented HOT 1
- overlap default strandedness argument incorrectly documented
- pr.get_sequence may not be working? HOT 4
- PyRanges.intersect - "invert=True" kwarg behavior HOT 1
- pyranges.cluster() Exception: Starts/Ends not int64 or int32: int64 HOT 9
- Unexpected behaviour with strandedness of pyranges object HOT 8
- PyRanges read_bed produces wrong number of chromosomes when cast to categorical HOT 1
- ResourceWarning: files opened in readers.read_bed are never closed HOT 3
- SyntaxError: invalid syntax for L131 in names.py HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyranges.