As you advised I created a bit of code that would find the last value in the 1D spectra with a non-zero value:
function AbstractPlotting.convert_arguments(::AbstractPlotting.PointBased, spec::KmerFrequencySpectra{1})
tv = view(spec.data, firstindex(spec.data):findprev(x -> x > 0, spec.data, lastindex(spec.data)))
return ([Point2f0(i, j) for (i, j) in enumerate(tv)],)
end
Which gives a very nice result now:
I tried to do the same for the 2D case but hit a bit of a snag you might have some insight on:
So I made this:
function _find_plottable_subset(spec::KmerFrequencySpectra{2})
mat = spec.data
furthest_row = 0
furthest_col = 0
for i in 1:256
for j in 1:256
v = mat[i, j]
furthest_row = ifelse((j > furthest_row) & !iszero(v), j, furthest_row)
furthest_col = ifelse((i > furthest_col) & !iszero(v), i, furthest_col)
end
end
return mat[1:furthest_col, 1:furthest_row]
end
function AbstractPlotting.convert_arguments(::AbstractPlotting.SurfaceLike, spec::KmerFrequencySpectra{2})
# lower to something which e.g. heatmap or contour understand
return (_find_plottable_subset(spec),)
end
Which should find forthest row and column out from (0,0) in a matrix, that has a non-zero value, and subset it for plotting, however I hit an error:
julia> twodspec = spectra(kca, kcb)
Count histogram of motifs appearing more than 0 times
julia> typeof(twodspec)
KmerAnalysis.KmerFrequencySpectra{2}
julia> twodspec.data
256×256 Array{UInt64,2}:
0x0000000000000000 0x00000000000436c5 … 0x0000000000000000 0x0000000000000000
0x0000000000045019 0x000000000000054d 0x0000000000000000 0x0000000000000000
0x0000000000000256 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000002f 0x0000000000000000 0x0000000000000000
0x0000000000000019 0x0000000000000049 0x0000000000000000 0x0000000000000000
0x0000000000000004 0x000000000000001f … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000009a 0x0000000000000000 0x0000000000000000
0x0000000000000002 0x00000000000000af 0x0000000000000000 0x0000000000000000
0x0000000000000030 0x000000000000005b 0x0000000000000000 0x0000000000000000
0x0000000000000006 0x0000000000000055 0x0000000000000000 0x0000000000000000
0x000000000000001d 0x0000000000000072 … 0x0000000000000000 0x0000000000000000
0x0000000000000018 0x0000000000000039 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000001a 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000010 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000008 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000000f … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000006 0x0000000000000005 0x0000000000000000 0x0000000000000000
0x0000000000000001 0x0000000000000013 0x0000000000000000 0x0000000000000000
⋮ ⋱ ⋮
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
julia> heatmap(twodspec)
ERROR: BoundsError: attempt to access (Observable{Array{UInt64,2}} with 0 listeners. Value:
UInt64[0x0000000000000000 0x00000000000436c5 … 0x0000000000000000 0x0000000000000000; 0x0000000000045019 0x000000000000054d … 0x0000000000000000 0x0000000000000000; … ; 0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000; 0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000],)
at index [3]
Stacktrace:
[1] getindex at ./tuple.jl:24 [inlined]
[2] calculated_attributes!(::Type{Heatmap{...}}, ::Heatmap{...}) at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/interfaces.jl:295
[3] calculated_attributes! at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/interfaces.jl:38 [inlined]
[4] Heatmap{...}(::Scene, ::Attributes, ::Tuple{Observable{KmerAnalysis.KmerFrequencySpectra{2}}}, ::Observable{Tuple{Array{UInt64,2}}}) at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/interfaces.jl:456
[5] plot!(::Scene, ::Type{Heatmap{...}}, ::Attributes, ::Tuple{Observable{KmerAnalysis.KmerFrequencySpectra{2}}}, ::Observable{Tuple{Array{UInt64,2}}}) at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/interfaces.jl:628
[6] #plot!#217(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(plot!), ::Scene, ::Type{Heatmap{...}}, ::Attributes, ::KmerAnalysis.KmerFrequencySpectra{2}) at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/interfaces.jl:571
[7] plot! at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/interfaces.jl:540 [inlined]
[8] #heatmap#110(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(heatmap), ::KmerAnalysis.KmerFrequencySpectra{2}) at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/recipes.jl:15
[9] heatmap(::KmerAnalysis.KmerFrequencySpectra{2}) at /Users/bward/.julia/packages/AbstractPlotting/jOgYQ/src/recipes.jl:13
[10] top-level scope at REPL[16]:1
Which is odd as the following works:
julia> heatmap(twodspec.data[1:31, 1:36])
julia> heatmap(twodspec.data[1:31, 1:36], colorrange = (0.0, 10000.0))
And twodspec.data[1:31, 1:36]
results in the same matrix as my auto function in convert_arguments!
:
julia> KmerAnalysis._find_plottable_subset(twodspec)
31×36 Array{UInt64,2}:
0x0000000000000000 0x00000000000436c5 … 0x0000000000000000 0x0000000000000000
0x0000000000045019 0x000000000000054d 0x0000000000000000 0x0000000000000000
0x0000000000000256 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000002f 0x0000000000000000 0x0000000000000000
0x0000000000000019 0x0000000000000049 0x0000000000000000 0x0000000000000000
0x0000000000000004 0x000000000000001f … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000009a 0x0000000000000000 0x0000000000000000
0x0000000000000002 0x00000000000000af 0x0000000000000003 0x0000000000000000
0x0000000000000030 0x000000000000005b 0x0000000000000014 0x0000000000000000
0x0000000000000006 0x0000000000000055 0x0000000000000005 0x0000000000000002
0x000000000000001d 0x0000000000000072 … 0x0000000000000000 0x0000000000000000
0x0000000000000018 0x0000000000000039 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000001a 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000010 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000008 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x000000000000000f … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000006 0x0000000000000005 0x0000000000000000 0x0000000000000000
0x0000000000000001 0x0000000000000013 0x0000000000000000 0x0000000000000000
0x0000000000000005 0x0000000000000016 … 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 … 0x0000000000000000 0x0000000000000000
Why might it not work for convert_arguments!
, but if I subset the matrix manually it works?
EDIT:
I found that using convert_arguments recursively solved my problem:
function AbstractPlotting.convert_arguments(p::AbstractPlotting.SurfaceLike, spec::KmerFrequencySpectra{2})
return AbstractPlotting.convert_arguments(p, _find_plottable_subset(spec))
end