From 2fa57827195b9a3e5d95ce7b883664a7a696d3eb Mon Sep 17 00:00:00 2001 From: "Thomas A. Christensen II" <25492070+MillironX@users.noreply.github.com> Date: Wed, 11 Jan 2023 16:21:16 -0600 Subject: [PATCH] Add more informative errors to `_is_valid` --- src/Haplotype.jl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Haplotype.jl b/src/Haplotype.jl index 4c97ecd..6223780 100644 --- a/src/Haplotype.jl +++ b/src/Haplotype.jl @@ -29,7 +29,8 @@ function Haplotype{S,T}( ) where {S<:BioSequence,T<:BioSymbol} sort!(edits; by=x -> x.pos) result = Haplotype{S,T}(ref, edits, Unsafe()) - _is_valid(result) || error("TODO") # report what kind of error message? + valid, message = _is_valid(result) + valid || error(message) return result end @@ -62,11 +63,12 @@ function _is_valid(h::Haplotype) op = edit.x # Sanity check: for this to be a valid variant, it must be comprised of valid # variations - _is_valid(Variation(h.ref, edit)) || return false + _is_valid(Variation(h.ref, edit)) || return (false, "Invalid Variation") # For substitutions we simply do not allow another modification of the same base if op isa Substitution - pos in valid_positions || return false + pos in valid_positions || + return (false, "Multiple modifications at same position") valid_positions = (first(valid_positions) + 1):last(valid_positions) last_was_insert = false # Insertions affect 0 reference bases, so it does not modify the valid positions @@ -75,18 +77,18 @@ function _is_valid(h::Haplotype) elseif op isa Insertion pos in ((first(valid_positions) - 1 + last_was_insert):(last(valid_positions) + 1)) || - return false + return (false, "Multiple insertions at same position") last_was_insert = true # Deletions obviously invalidate the reference bases that are deleted. elseif op isa Deletion len = length(op) pos in (first(valid_positions):(last(valid_positions) - len + 1)) || - return false + return (false, "Deletion out of range") valid_positions = (first(valid_positions) + len):last(valid_positions) last_was_insert = false end end - return true + return (true, "") end function Haplotype(