Skip to content

Commit 88cc15a

Browse files
committed
Made Gene mutable and added shift
1 parent 97aec7a commit 88cc15a

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/GenomicAnnotations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export PointLocus, SingleNucleotide, BetweenNucleotides
1717
export Join, Order, Complement
1818
export Locus
1919
export eachposition
20+
export shift, shift!
2021

2122
export relative_position
2223

src/locus.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ Base.show(io::IO, locus::SpanLocus{OpenLeftSpan}) = print(io, string("<", locus.
7676
Base.show(io::IO, locus::SpanLocus{OpenRightSpan}) = print(io, string(locus.position.start, "..", ">", locus.position.stop))
7777
Base.show(io::IO, locus::Join) = print(io, string("join(", join(locus.loc, ","), ")"))
7878
Base.show(io::IO, locus::Order) = print(io, string("order(", join(locus.loc, ","), ")"))
79-
function Base.show(io::IO, locus::Complement)
80-
print(io, "complement(", string(locus.loc), ")")
81-
end
79+
Base.show(io::IO, locus::Complement) = print(io, "complement(", string(locus.loc), ")")
8280

8381

8482
Base.:(==)(loc1::SpanLocus{ClosedSpan}, loc2::SpanLocus{ClosedSpan}) = loc1.position == loc2.position
@@ -185,6 +183,8 @@ end
185183
186184
Shift the position of `Locus` loc by `p`.
187185
"""
188-
function shift(loc, p)
189-
190-
end
186+
shift(p::PointLocus{T}, n) where T = PointLocus(p.position + n, T)
187+
shift(p::SpanLocus{T}, n) where T = SpanLocus(p.position .+ n, T)
188+
shift(P::Join{T}, n) where T = Join(map(p -> shift(p, n), P.loc))
189+
shift(P::Order{T}, n) where T = Order(map(p -> shift(p, n), P.loc))
190+
shift(p::Complement, n) = Complement(shift(p.loc, n))

src/record.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ end
3131
Record(args...) = Record{Gene}(args...)
3232

3333

34-
struct Gene <: AbstractGene
34+
mutable struct Gene <: AbstractGene
3535
parent::Record{Gene}
3636
index::UInt
3737
locus::AbstractLocus
@@ -444,4 +444,9 @@ function locus!(gene::AbstractGene, loc::AbstractLocus)
444444
newgene
445445
end
446446

447-
Base.isless(g1::AbstractGene, g2::AbstractGene) = ((locus(g1) == locus(g2)) && (feature(g1) == "gene" && feature(g2) != "gene")) || (locus(g1) < locus(g2))
447+
Base.isless(g1::AbstractGene, g2::AbstractGene) = ((locus(g1) == locus(g2)) && (feature(g1) == "gene" && feature(g2) != "gene")) || (locus(g1) < locus(g2))
448+
449+
function shift!(gene, p)
450+
setfield!(gene, :locus, shift(locus(gene), p))
451+
return gene
452+
end

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ using Test
133133
end
134134

135135
@testset "Locus" begin
136+
@test Locus("1..2") == Locus("1..2")
136137
@test locus(chr.genes[2]) < locus(chr.genes[4])
137138
@test locus(chr.genes[2]) == locus(chr.genes[2])
138139
@test iscomplement(chr.genes[2]) == false
@@ -156,6 +157,23 @@ using Test
156157
@test iscompound(Locus("1..10")) == false
157158
@test iscompound(Locus("<1..>10")) == false
158159
@test iscompound(Locus("1^2")) == false
160+
161+
@test shift(Locus("1..3"), 1) == Locus("2..4")
162+
@test shift(Locus("join(1..3,7..9)"), 1) == Locus("join(2..4,8..10)")
163+
@test shift(Locus("order(1..3,7..9)"), 1) == Locus("order(2..4,8..10)")
164+
@test shift(Locus("complement(join(1..3,7..9))"), 1) == Locus("complement(join(2..4,8..10))")
165+
@test shift(Locus("join(complement(1..3),complement(7..9))"), 1) == Locus("join(complement(2..4),complement(8..10))")
166+
@test shift(Locus("1..10"), 1) == Locus("2..11")
167+
@test shift(Locus("1^2"), 1) == Locus("2^3")
168+
169+
@test begin
170+
shift!(chr.genes[2], 1)
171+
locus(chr.genes[2]) == Locus("4..207")
172+
end
173+
@test begin
174+
shift!(chr.genes[2], -1)
175+
locus(chr.genes[2]) == Locus("3..206")
176+
end
159177
end
160178

161179
seq = dna"atgtccatatacaacggtatctccacctcaggtttagatctcaacaacggaaccattgccgacatgagacagttaggtatcgtcgagagttacaagctaaaacgagcagtagtcagctctgcatctgaagccgctgaagttctactaagggtggataacatcatccgtgcaagaccaagaaccgccaatagacaacatatgtaa"

0 commit comments

Comments
 (0)