Validating the content of a struct field

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

Have you checked your capitalization?

Thank you for the response, what capitalisation do you mean? I have checked the spelling of the variables, I think they are all correctly spelled. If you think I made a mistake could you point out where it is?

Capitalization of Dna (versus dna)

Thank you, that was quite in-attentive of me.

Just needed a fresh pair of eyes, it happens to the best of us :wink:

2 Likes