Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base.parse and Base.tryparse for DNA and RNA #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `Base.parse` and `Base.tryparse` for `DNA` and `RNA`

## [4.0.0]
### Added
Expand Down
19 changes: 19 additions & 0 deletions docs/src/nucleicacids.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ true

```

`parse` also works on characters and strings in a case-insensitive way:
```jldoctest
julia> parse(DNA, "T")
DNA_T

julia> parse(RNA, "U")
RNA_U

julia> parse(RNA, "U") == parse(RNA, "u")
true

julia> tryparse(DNA, "A") # tryparse returns either a DNA nucleotide or nothing
DNA_A

julia> tryparse(DNA, "SD")

```


`print` and `show` methods are defined to output the text representation of a symbol:
```jldoctest
julia> print(DNA_A) # un-decorated text
Expand Down
4 changes: 2 additions & 2 deletions docs/src/sequences.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ A quick way to create a DNA/RNA sequence is storing symbols in a vector.

```jldoctest
julia> seq = [DNA_A, DNA_C, DNA_G, DNA_T]
4-element Array{DNA,1}:
4-element Vector{DNA}:
DNA_A
DNA_C
DNA_G
DNA_T

julia> [convert(DNA, x) for x in "ACGT"] # from a string
4-element Array{DNA,1}:
4-element Vector{DNA}:
DNA_A
DNA_C
DNA_G
Expand Down
60 changes: 39 additions & 21 deletions src/nucleicacid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,59 @@ RNA(nt::DNA) = convert(RNA, nt)

# Conversion from/to characters
# -----------------------------

function Base.convert(::Type{DNA}, c::Char)
if c > '\uff'
throw(InexactError(:convert, DNA, c))
function Base.convert(t::Union{Type{DNA},Type{RNA}}, c::Char)
nt = tryparse(t, c)
if nt === nothing
throw(InexactError(:convert, t, c))
end
@inbounds dna = char_to_dna[convert(Int, c) + 1]
return nt
end
DNA(c::Char) = convert(DNA, c)
RNA(c::Char) = convert(RNA, c)

function Base.convert(::Type{Char}, nt::DNA)
return dna_to_char[encoded_data(nt)+1]
end
Char(nt::DNA) = convert(Char, nt)

function Base.convert(::Type{Char}, nt::RNA)
return rna_to_char[encoded_data(nt)+1]
end
Char(nt::RNA) = convert(Char, nt)

function Base.tryparse(::Type{DNA}, c::Char)
c > '\uff' && return nothing
@inbounds dna = char_to_dna[convert(Int, c)+1]
if !isvalid(DNA, dna)
throw(InexactError(:convert, DNA, c))
return nothing
end
return encode(DNA, dna)
end
DNA(c::Char) = convert(DNA, c)

function Base.convert(::Type{RNA}, c::Char)
if c > '\uff'
throw(InexactError(:convert, RNA, c))
end
@inbounds rna = char_to_rna[convert(Int, c) + 1]
function Base.tryparse(::Type{RNA}, c::Char)
c > '\uff' && return nothing
@inbounds rna = char_to_rna[convert(Int, c)+1]
if !isvalid(RNA, rna)
throw(InexactError(:convert, RNA, c))
return nothing
end
return encode(RNA, rna)
end
RNA(c::Char) = convert(RNA, c)

function Base.convert(::Type{Char}, nt::DNA)
return dna_to_char[encoded_data(nt) + 1]
end
Char(nt::DNA) = convert(Char, nt)

function Base.convert(::Type{Char}, nt::RNA)
return rna_to_char[encoded_data(nt) + 1]
function Base.tryparse(t::Union{Type{DNA},Type{RNA}}, s::AbstractString)
sizeof(s) == 1 && return tryparse(t, first(s))
stripped = strip(s)
sizeof(stripped) == 1 && return tryparse(t, first(stripped))
return nothing
end
Char(nt::RNA) = convert(Char, nt)

function Base.parse(t::Union{Type{DNA},Type{RNA}}, c::Union{AbstractString,Char})
nt = tryparse(t, c)
if nt === nothing
throw(ArgumentError("invalid nucleotide"))
end
return nt
end

# Encoding of DNA and RNA NucleicAcids
# ------------------------------------
Expand Down
68 changes: 53 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ end
@test encoded_data(RNA_N) === 0b1111
end

@testset "stringbyte" begin
for T in (DNA, RNA)
@test all(alphabet(DNA)) do i
UInt8(Char(i)) == stringbyte(i)
end
end
end
@testset "stringbyte" begin
for T in (DNA, RNA)
@test all(alphabet(DNA)) do i
UInt8(Char(i)) == stringbyte(i)
end
end
end
end

@testset "Char" begin
Expand Down Expand Up @@ -290,11 +290,11 @@ end
end

@testset "Broadcasting" begin
v = DNA[DNA_A, DNA_C, DNA_G, DNA_C]
v[2:3] .= DNA_A
@test v == [DNA_A, DNA_A, DNA_A, DNA_C]
v .= DNA_T
@test v == fill(DNA_T, 4)
v = DNA[DNA_A, DNA_C, DNA_G, DNA_C]
v[2:3] .= DNA_A
@test v == [DNA_A, DNA_A, DNA_A, DNA_C]
v .= DNA_T
@test v == fill(DNA_T, 4)
end

@testset "Show DNA" begin
Expand Down Expand Up @@ -384,6 +384,44 @@ end
@test collect(ACGUN) == [RNA_A, RNA_C, RNA_G, RNA_U, RNA_N]
end

@testset "Parsers" begin
@testset "Valid Cases" begin
fromto = [('a', DNA_A), ('c', RNA_C), ('s', DNA_S), ('s', RNA_S)]

for (from, to) in fromto
@test parse(typeof(to), from) === tryparse(typeof(to), from) === to
# Strings also work
str_from = string(from)
@test parse(typeof(to), str_from) === tryparse(typeof(to), str_from) === to
# Case doesn't matter
@test parse(typeof(to), uppercase(from)) === parse(typeof(to), lowercase(from)) === to
# Whitespace doesn't matter
whitespace_from = "\t" * from * " \n"
@test parse(typeof(to), whitespace_from) === tryparse(typeof(to), whitespace_from) === to
end
end

@testset "Invalid Cases" begin
@test_throws ArgumentError parse(DNA, "")
@test_throws ArgumentError parse(RNA, "")
@test_throws ArgumentError parse(DNA, "U")
@test_throws ArgumentError parse(RNA, "T")
@test_throws ArgumentError parse(DNA, "AL")
@test_throws ArgumentError parse(RNA, "LA")
@test_throws ArgumentError parse(RNA, '\0')
@test_throws ArgumentError parse(DNA, '@')
@test_throws ArgumentError parse(DNA, '亜')
@test tryparse(DNA, "U") === tryparse(RNA, "T") == nothing
@test tryparse(DNA, "") === tryparse(RNA, "") == nothing
@test tryparse(DNA, "AL") === tryparse(RNA, "AL") === nothing
@test tryparse(DNA, "LA") === tryparse(RNA, "LA") === nothing
@test tryparse(DNA, "ALAA") === tryparse(RNA, "ALAA") === nothing
@test tryparse(DNA, '\0') === tryparse(RNA, '\0') === nothing
@test tryparse(DNA, '@') === tryparse(RNA, '@') === nothing
@test tryparse(DNA, '亜') === tryparse(RNA, '亜') === nothing
end
end

@testset "Hashing" begin
@test hash(DNA_A) != hash(RNA_A)
@test hash(DNA_A) != hash(DNA_G)
Expand Down Expand Up @@ -424,9 +462,9 @@ end
end

@testset "stringbyte" begin
@test all(alphabet(AminoAcid)) do i
UInt8(Char(i)) == stringbyte(i)
end
@test all(alphabet(AminoAcid)) do i
UInt8(Char(i)) == stringbyte(i)
end
end

@testset "isvalid" begin
Expand Down