Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |