Hello all, I am a beginner in the julia language and I quite like it.
I am trying to make a struct that checks whether the input is valid:
struct Dna
# Basic elements of a DNA string
sequence::String
identifier::String
comment::String
# Checking Wether the DNA is valid.
Dna(sequence::String, identifier::String, comment::String) =
validate_dna(sequence) ? new(sequence, identifier, comment) :
throw(ArgumentError("Sequence of $identifier is invalid."))
end
function validate_dna(string::String)
#=Validating DNA input
:param string: String The string to be validated.
:return: Bool: 1 If the string is a valid DNA string, 0 otherwise.
=#
accepted_letters = Set(['A', 'T', 'C', 'G'])
accepted_chars = map(x -> issubset(x, accepted_letters), collect(string))
return all(accepted_chars)
end
When I execute this function; the input is not validated as I would expect:
# Expected ERROR: Sequence of 12 is invalid:
julia> a = dna("ATxCTG", "12", "a test sequence")
dna("ATxCTG", "12", "a test sequence")
# Expected no error
julia> b = dna("ATCTG", "12", "a test sequence")
dna("ATCTG", "12", "a test sequence")
No error messages are generated. Could somebody help me improve this code?
The validate_dna()
function works
julia> validate_dna("AAx")
false
julia> validate_dna("AAG")
true