BioJulia / IntervalTrees.jl

A data structure for efficient manipulation of sets of intervals
MIT License
44 stars 17 forks source link

Make AbstractInterval broadcastable #50

Open CiaranOMara opened 4 years ago

CiaranOMara commented 4 years ago

Expected Behavior

I have a use case where I would like to broadcast the Interval type. Below is a contrived example.

function example(i1, i2)
    return metadata(i1) * metadata(i2)
end

interval = Interval("chr1", 1:1, '.', 2)
intervals = Interval.("chr1", [1:2, 3:4, 5:6], '.', 1:3)

@test collect(1:3) .* 2 == example.(intervals, interval)

Current Behavior

However, the above example results in the following sort of unintuitive error because Interval is not broadcastable.

Got exception outside of a @test
  MethodError: no method matching iterate(::GenomicFeatures.Interval{Nothing})
  Closest candidates are:
    iterate(!Matched::Core.SimpleVector) at essentials.jl:604
    iterate(!Matched::Core.SimpleVector, !Matched::Any) at essentials.jl:604
    iterate(!Matched::ExponentialBackOff) at error.jl:214

Possible Solution / Implementation

Defining the following solves the issue.

Broadcast.broadcastable(x::IntervalTrees.AbstractInterval) = Ref(x) 

While it's possible to define this in my own local projects or elsewhere, I wonder if it would be a useful addition here?