Hi,
I’m trying to show my custom matrix type to display by overloading the Base.show
method, but it doesn’t call the overloaded method. The following is a reproducible code:
# https://github.com/BioJulia/BioSymbols.jl
import BioSymbols: DNA
struct PWM{T,S<:Real} <: AbstractMatrix{S}
# symbol type (DNA or RNA)
typ::Type{T}
# score matrix (row: symbols, column: position)
data::Matrix{S}
end
function Base.size(pwm::PWM)
return size(pwm.data)
end
function Base.getindex(pwm::PWM, i::Integer, j::Integer)
return getindex(pwm.data, i, j)
end
function Base.show(io::IO, pwm::PWM{DNA})
compact(x) = string(x ≥ 0 ? " " : "", sprint(showcompact, x))
cells = hcat(['A', 'C', 'G', 'T'], compact.(pwm.data))
width = maximum(length.(cells), 1)
print(io, summary(pwm), ':')
for i in 1:size(cells, 1)
println(io)
print(io, ' ', rpad(cells[i,1], width[1]+1))
for j in 2:size(cells, 2)
print(io, rpad(cells[i,j], width[j]+1))
end
end
end
I expect I see ACGT characters on the leftmost column:
julia> pwm = PWM(DNA, randn(4, 5)) # not overloaded?
4×5 PWM{BioSymbols.DNA,Float64}:
2.05598 -1.48329 -0.909718 -0.915152 -0.199301
-1.65405 0.456539 0.0655709 -1.18943 0.158246
0.0463668 -0.647083 -0.430253 -0.251961 1.59043
-0.333135 1.01648 -0.665186 0.922921 0.199385
julia> pwm isa PWM{DNA} # pwm is actually an object of PWM{DNA}
true
julia> show(pwm) # but this is okay
4×5 PWM{BioSymbols.DNA,Float64}:
A 2.05598 -1.48329 -0.909718 -0.915152 -0.199301
C -1.65405 0.456539 0.0655709 -1.18943 0.158246
G 0.0463668 -0.647083 -0.430253 -0.251961 1.59043
T -0.333135 1.01648 -0.665186 0.922921 0.199385
Is this a bug of Julia or am I missing something?
Thank you.