Skip to content

Commit

Permalink
Add hasintersection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranOMara committed Apr 16, 2023
1 parent d64824a commit 9761d57
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/GenomicFeatures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export
IntervalCollection,
eachoverlap,
coverage,
hasintersection,

isfilled,
hasseqname,
Expand Down
29 changes: 29 additions & 0 deletions src/intervalcollection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,32 @@ function Base.iterate(it::IntervalCollectionStreamIterator{F,S,T}, state = ()) w
end
end
end

"""
hasintersection(interval::Interval, col::IntervalCollection)::Bool
Query whether an `interval` has an intersection with `col`.
"""
function hasintersection(interval::Interval, col::IntervalCollection)

# Return early if chromosome is not in the interval collection.
if !haskey(col.trees, seqname(interval))
return false
end

# Setup intersection iterator.
iter = IntervalTrees.intersect(col.trees[seqname(interval)], (leftposition(interval), rightposition(interval)))

# Attempt first iteration.
if iterate(iter) === nothing
return false
end

# Intersection exists.
return true

end

function hasintersection(col::IntervalCollection)
return interval -> hasintersection(interval, col)
end
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ end
sort(simple_intersection(intervals_a, intervals_b))
@test sort(collect(eachoverlap(ic_a, ic_b, filter=(a,b) -> isodd(first(a))))) ==
sort(simple_intersection(intervals_a, intervals_b, filter=(a,b) -> isodd(first(a))))

# Check hasintersection method.
@test hasintersection(Interval("test", 1, 1), IntervalCollection([Interval("test", 1,2)])) == true
@test hasintersection(Interval("test", 1, 1), IntervalCollection([Interval("test", 2,2)])) == false

# Check hasintersection currying.
@test Interval("test", 1, 1) |> hasintersection(IntervalCollection([Interval("test", 1,2)])) == true
@test Interval("test", 1, 1) |> hasintersection(IntervalCollection([Interval("test", 2,2)])) == false
end

@testset "Show" begin
Expand Down

0 comments on commit 9761d57

Please sign in to comment.