Skip to content

Commit

Permalink
Move code common used for benchmarks and tests into Utilities.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranOMara committed Jun 10, 2020
1 parent 481fe0d commit c10623f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
25 changes: 0 additions & 25 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,6 @@ N = 1000
SEED = 1234
SEQNAMES = "chr" .* string.(1:3)

# Generate an array of n random Interval{Int} object. With sequence names
# samples from seqnames, and intervals drawn to lie in [1, maxpos].
function random_intervals(seqnames::Vector{String}, maxpos::Int, n::Int)
seq_dist = Categorical(length(seqnames))
strand_dist = Categorical(2)
length_dist = Normal(1000, 1000)
intervals = Vector{Interval{Int}}(undef, n)
for i in 1:n
intlen = maxpos
while intlen >= maxpos || intlen <= 0
intlen = ceil(Int, rand(length_dist))
end
first = rand(1:maxpos-intlen)
last = first + intlen - 1
strand = rand(strand_dist) == 1 ? STRAND_POS : STRAND_NEG
intervals[i] = Interval{Int}(seqnames[rand(seq_dist)], first, last, strand, i)
end
return intervals
end

function random_intervals(seqnames::Vector{String}, maxpos::Int, n::Int, seed::Int)
Random.seed!(seed)
return random_intervals(seqnames, maxpos, n)
end

intervals = random_intervals(SEQNAMES, 1000, N, SEED)
intervals_sorted = sort(intervals)

Expand Down
32 changes: 32 additions & 0 deletions test/Utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Utilities

using Distributions
using GenomicFeatures
using Random

# Generate an array of n random Interval{Int} object. With sequence names
# samples from seqnames, and intervals drawn to lie in [1, maxpos].
function random_intervals(seqnames::Vector{String}, maxpos::Int, n::Int)
seq_dist = Categorical(length(seqnames))
strand_dist = Categorical(2)
length_dist = Normal(1000, 1000)
intervals = Vector{Interval{Int}}(undef, n)
for i in 1:n
intlen = maxpos
while intlen >= maxpos || intlen <= 0
intlen = ceil(Int, rand(length_dist))
end
first = rand(1:maxpos-intlen)
last = first + intlen - 1
strand = rand(strand_dist) == 1 ? STRAND_POS : STRAND_NEG
intervals[i] = Interval{Int}(seqnames[rand(seq_dist)], first, last, strand, i)
end
return intervals
end

function random_intervals(seqnames::Vector{String}, maxpos::Int, n::Int, seed::Int)
Random.seed!(seed)
return random_intervals(seqnames, maxpos, n)
end

end # module Utilities
25 changes: 3 additions & 22 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using GenomicFeatures
using Test

using Distributions
import Random

include(joinpath(@__DIR__, "Utilities.jl"))
import .Utilities: random_intervals


# Test that an array of intervals is well ordered
function is_all_ordered(intervals::Vector{I}) where I <: Interval
Expand All @@ -15,27 +17,6 @@ function is_all_ordered(intervals::Vector{I}) where I <: Interval
return true
end

# Generate an array of n random Interval{Int} object. With sequence names
# samples from seqnames, and intervals drawn to lie in [1, maxpos].
function random_intervals(seqnames, maxpos::Int, n::Int)
seq_dist = Categorical(length(seqnames))
strand_dist = Categorical(2)
length_dist = Normal(1000, 1000)
intervals = Vector{Interval{Int}}(undef, n)
for i in 1:n
intlen = maxpos
while intlen >= maxpos || intlen <= 0
intlen = ceil(Int, rand(length_dist))
end
first = rand(1:maxpos-intlen)
last = first + intlen - 1
strand = rand(strand_dist) == 1 ? STRAND_POS : STRAND_NEG
intervals[i] = Interval{Int}(seqnames[rand(seq_dist)],
first, last, strand, i)
end
return intervals
end

# A simple interval intersection implementation to test against.
function simple_intersection(intervals_a, intervals_b; filter=(a,b)->true)
sort!(intervals_a)
Expand Down

0 comments on commit c10623f

Please sign in to comment.