Move Insertion-related code to edits/Insertion.jl

This commit is contained in:
Thomas A. Christensen II 2022-12-30 14:43:55 -06:00
parent b36ab69c72
commit 9f9d0899bd
2 changed files with 32 additions and 31 deletions

View file

@ -1,24 +1,6 @@
include("edits/Substitution.jl")
include("edits/Deletion.jl")
"""
Insertion{S <: BioSequence}
Represents the insertion of a `S` into a sequence. The location of the insertion
is stored outside the struct.
"""
struct Insertion{S<:BioSequence}
seq::S
function Insertion{S}(x::S) where {S<:BioSequence}
isempty(x) && error("Insertion must be at least 1 symbol")
return new(x)
end
end
Insertion(s::BioSequence) = Insertion{typeof(s)}(s)
Base.length(x::Insertion) = length(x.seq)
Base.:(==)(x::Insertion, y::Insertion) = x.seq == y.seq
Base.hash(x::Insertion, h::UInt) = hash(Insertion, hash(x.seq, h))
include("edits/Insertion.jl")
"""
Edit{S <: BioSequence, T <: BioSymbol}
@ -77,15 +59,3 @@ function lendiff(edit::Edit)
x = edit.x
return x isa Substitution ? 0 : (x isa Deletion ? -length(x) : length(x.x))
end
function _refbases(i::Insertion, reference::S, pos::UInt) where {S<:BioSequence}
return S([reference[pos]])
end
function _altbases(i::Insertion, reference::S, pos::UInt) where {S<:BioSequence}
if pos == 1
return S([i.seq..., reference[pos]])
else
return S([reference[pos], i.seq...])
end
end

31
src/edits/Insertion.jl Normal file
View file

@ -0,0 +1,31 @@
"""
Insertion{S <: BioSequence}
Represents the insertion of a `S` into a sequence. The location of the insertion
is stored outside the struct.
"""
struct Insertion{S<:BioSequence}
seq::S
function Insertion{S}(x::S) where {S<:BioSequence}
isempty(x) && error("Insertion must be at least 1 symbol")
return new(x)
end
end
Insertion(s::BioSequence) = Insertion{typeof(s)}(s)
Base.length(x::Insertion) = length(x.seq)
Base.:(==)(x::Insertion, y::Insertion) = x.seq == y.seq
Base.hash(x::Insertion, h::UInt) = hash(Insertion, hash(x.seq, h))
function _refbases(i::Insertion, reference::S, pos::UInt) where {S<:BioSequence}
return S([reference[pos]])
end
function _altbases(i::Insertion, reference::S, pos::UInt) where {S<:BioSequence}
if pos == 1
return S([i.seq..., reference[pos]])
else
return S([reference[pos], i.seq...])
end
end