Skip to content

Commit

Permalink
Try #1:
Browse files Browse the repository at this point in the history
  • Loading branch information
biojulia-bors[bot] committed Feb 7, 2019
2 parents dce44f6 + 9f54376 commit b04dedd
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/BioGenerics.jl
@@ -1,5 +1,21 @@
# BioGenerics.jl
# ==============
#
# Core types and methods common to many packages in the BioJulia ecosystem.
#
# This file is a part of BioJulia.
# License is MIT: https://github.com/BioJulia/BioGenerics/blob/master/LICENSE.md

module BioGenerics


greet() = print("Hello World!")
#include("declare.jl")
#include("Exceptions.jl")
include("IO.jl")
#include("Mem.jl")
#include("Ragel.jl")
#include("ReaderHelper.jl")
#include("RecordHelper.jl")
include("Testing.jl")

end # module
end # module BioGenerics
110 changes: 110 additions & 0 deletions src/IO.jl
@@ -0,0 +1,110 @@
# BioGenerics.IO
# ==============
#
# I/O interfaces for BioJulia packages.
#
# This file is a part of BioJulia.
# License is MIT: https://github.com/BioJulia/BioCore.jl/blob/master/LICENSE.md

module IO

# IO Types
# ---------

"Abstract formatted input/output type."
abstract type AbstractFormattedIO end

"""
stream(io::AbstractFormattedIO)
Return the underlying `IO` object; subtypes of `AbstractFormattedIO` must
implement this method.
"""
function stream end

# delegate method call
for f in (:eof, :flush, :close)
@eval function Base.$(f)(io::AbstractFormattedIO)
return $(f)(stream(io))
end
end

function Base.open(f::Function, ::Type{T}, args...; kwargs...) where T <: AbstractFormattedIO
io = open(T, args...; kwargs...)
try
f(io)
finally
close(io)
end
end

"""
Abstract data reader type.
See `subtypes(AbstractReader)` for all available data readers.
"""
abstract type AbstractReader <: AbstractFormattedIO end

Base.IteratorSize(::Type{T}) where T <: AbstractReader = Base.SizeUnknown()

function Base.open(::Type{T}, filepath::AbstractString, args...; kwargs...) where T <: AbstractReader
return T(open(filepath), args...; kwargs...)
end

function Base.read(input::AbstractReader)
return read!(input, eltype(input)())
end

"""
tryread!(reader::AbstractReader, output)
Try to read the next element into `output` from `reader`.
If the result could not be read, then `nothing` will be returned instead.
"""
function tryread!(reader::AbstractReader, output)
try
read!(reader, output)
return output
catch ex
if isa(ex, EOFError)
return nothing
end
rethrow()
end
end

function Base.iterate(reader::AbstractReader, nextone = eltype(reader)())
if tryread!(reader, nextone) === nothing
return nothing
else
return copy(nextone), nextone
end
end


"""
Abstract data writer type.
See `subtypes(AbstractWriter)` for all available data writers.
"""
abstract type AbstractWriter <: AbstractFormattedIO end

function Base.open(::Type{T}, filepath::AbstractString, args...; kwargs_...) where T <: AbstractWriter
kwargs = collect(kwargs_)
i = findfirst(kwarg -> kwarg[1] == :append, kwargs)
if i !== nothing
append = kwargs[i][2]
if !isa(append, Bool)
throw(ArgumentError("append must be boolean"))
end
deleteat!(kwargs, i)
else
append = false
end
return T(open(filepath, append ? "a" : "w"), args...; kwargs...)
end

#

end # module BioGenerics.IO
58 changes: 58 additions & 0 deletions src/Testing.jl
@@ -0,0 +1,58 @@
# Testing
# =======
#
# Utilities to assist testing of BioJulia packages.
#
# This file is a part of BioJulia.
# License is MIT: https://github.com/BioJulia/BioCore.jl/blob/master/LICENSE.md

module Testing

function random_array(n::Integer, elements, probs)
cumprobs = cumsum(probs)
x = Vector{eltype(elements)}(undef, n)
for i in 1:n
x[i] = elements[searchsorted(cumprobs, rand()).start]
end
return x
end

function random_seq(n::Integer, nts, probs)
cumprobs = cumsum(probs)
x = Vector{Char}(undef, n)
for i in 1:n
x[i] = nts[searchsorted(cumprobs, rand()).start]
end
return String(x)
end

function random_dna(n, probs = [0.24, 0.24, 0.24, 0.24, 0.04])
return random_seq(n, ['A', 'C', 'G', 'T', 'N'], probs)
end

function random_rna(n, probs = [0.24, 0.24, 0.24, 0.24, 0.04])
return random_seq(n, ['A', 'C', 'G', 'U', 'N'], probs)
end

function random_aa(len)
return random_seq(len,
['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I',
'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V', 'X' ],
push!(fill(0.049, 20), 0.02))
end

function intempdir(fn::Function, parent = tempdir())
dirname = mktempdir(parent)
try
cd(fn, dirname)
finally
rm(dirname, recursive = true)
end
end

function random_interval(minstart, maxstop)
start = rand(minstart:maxstop)
return start:rand(start:maxstop)
end

end # Module Testing

0 comments on commit b04dedd

Please sign in to comment.