|
44 | 44 | insert!(seq::BioSequence, i, x) |
45 | 45 |
|
46 | 46 | Insert a biological symbol `x` into a biological sequence `seq`, at the given |
47 | | -index `i`. |
| 47 | +index `i`. Returns the mutated `seq`. |
| 48 | +
|
| 49 | +# Examples |
| 50 | +```jldoctest |
| 51 | +julia> seq = dna"ATGCA" |
| 52 | +5nt DNA Sequence: |
| 53 | +ATGCA |
| 54 | +
|
| 55 | +julia> insert!(seq, 3, 'A') |
| 56 | +6nt DNA Sequence: |
| 57 | +ATAGCA |
| 58 | +``` |
48 | 59 | """ |
49 | 60 | function Base.insert!(seq::BioSequence, i::Integer, x) |
| 61 | + i == length(seq) + 1 && return push!(seq, x) |
50 | 62 | checkbounds(seq, i) |
51 | 63 | resize!(seq, length(seq) + 1) |
52 | 64 | copyto!(seq, i + 1, seq, i, lastindex(seq) - i) |
53 | 65 | @inbounds seq[i] = x |
54 | 66 | return seq |
55 | 67 | end |
56 | 68 |
|
| 69 | +""" |
| 70 | + spliceinto!(seq::BioSequence, i::Integer, x) |
| 71 | +
|
| 72 | +Insert the sequence `x` into a biological sequence `seq`, at the given index `i`. |
| 73 | +After splicing, the `seq`'s symbols at indices `i:i+length(x)-1` are equal to `x`, |
| 74 | +and the the symbols that were previously there are moved to the right. |
| 75 | +
|
| 76 | +# Examples |
| 77 | +```jldoctest |
| 78 | +julia> seq = dna"TAGTGCA"; |
| 79 | +
|
| 80 | +julia> spliceinto!(seq, 3, "CAGGA") |
| 81 | +12nt DNA sequence: |
| 82 | +TACAGGAGTGCA |
| 83 | +``` |
| 84 | +""" |
| 85 | +function spliceinto!(seq::BioSequence, i::Integer, x) |
| 86 | + oldlen = length(seq) |
| 87 | + i == oldlen + 1 && return append!(seq, x) |
| 88 | + @boundscheck checkbounds(seq, i) |
| 89 | + resize!(seq, oldlen + length(x)) |
| 90 | + copyto!(seq, i + length(x), seq, i, oldlen - i + 1) |
| 91 | + copyto!(seq, i, x, 1, length(x)) |
| 92 | + return seq |
| 93 | +end |
| 94 | + |
| 95 | +""" |
| 96 | + spliceinto!(seq::BioSequence, span::UnitRange, x) |
| 97 | +
|
| 98 | +Delete the symbols at indices `span` in `seq`, and then copy `x` into the |
| 99 | +first deleted position, then return `seq`. |
| 100 | +
|
| 101 | +This is equivalent to `deleteat!(seq, span); spliceinto!(seq, first(span), x)`, |
| 102 | +but is more efficient. |
| 103 | +`span` must be nonempty, or this function will throw an `ArgumentError`. To handle |
| 104 | +potentially empty spans, check if the span is empty, and if so use `spliceinto(seq, first(span), x)`. |
| 105 | +
|
| 106 | +# Examples |
| 107 | +```jldoctest |
| 108 | +julia> seq = dna"TAGTGCA"; |
| 109 | +
|
| 110 | +julia> spliceinto!(seq, 3:5, "CAGGA") |
| 111 | +9nt DNA sequence: |
| 112 | +TACAGGACA |
| 113 | +``` |
| 114 | +""" |
| 115 | +function spliceinto!(seq::BioSequence, span::UnitRange, x) |
| 116 | + isempty(span) && throw(ArgumentError("span cannot be empty")) |
| 117 | + @boundscheck checkbounds(seq, span) |
| 118 | + oldlen = length(seq) |
| 119 | + xlen = length(x) |
| 120 | + if length(span) == xlen |
| 121 | + # Same lengths: Just copy in x |
| 122 | + copyto!(seq, first(span), x, 1, length(span)) |
| 123 | + elseif length(span) < xlen |
| 124 | + # x is longer. Resize and shift to make room for more symbols, |
| 125 | + # then copy in x |
| 126 | + resize!(seq, oldlen + xlen - length(span)) |
| 127 | + copyto!(seq, first(span) + xlen, seq, last(span) + 1, oldlen - last(span)) |
| 128 | + copyto!(seq, first(span), x, 1, xlen) |
| 129 | + else |
| 130 | + # Span is longer. Delete the rightmost bases (to cause the smallest possible shift), |
| 131 | + # then copy in |
| 132 | + deleteat!(seq, first(span) + xlen:last(span)) |
| 133 | + copyto!(seq, first(span), x, 1, xlen) |
| 134 | + end |
| 135 | + return seq |
| 136 | +end |
| 137 | + |
57 | 138 | """ |
58 | 139 | deleteat!(seq::BioSequence, range::UnitRange{<:Integer}) |
59 | 140 |
|
|
89 | 170 | Add a biological sequence `other` onto the end of biological sequence `seq`. |
90 | 171 | Modifies and returns `seq`. |
91 | 172 | """ |
92 | | -function Base.append!(seq::BioSequence, other::BioSequence) |
| 173 | +function Base.append!(seq::BioSequence, other) |
93 | 174 | resize!(seq, length(seq) + length(other)) |
94 | 175 | copyto!(seq, lastindex(seq) - length(other) + 1, other, 1, length(other)) |
95 | 176 | return seq |
|
0 commit comments