Skip to content

Commit

Permalink
test/test_plot_diagnostics.jl: Additional tests for plotting.
Browse files Browse the repository at this point in the history
  • Loading branch information
mashu committed Oct 14, 2024
1 parent a70c72e commit 311f773
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/LineageCollapse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ module LineageCollapse
export compute_distance, compute_pairwise_distance, perform_clustering

function plot_diagnostics(args...; opts...)
error(isdefined(Main, :CairoMakie) ? "Invalid method call" : "Import CairoMakie to enable plotting")
if !isdefined(@__MODULE__, :CairoMakie)
error("Import CairoMakie to enable plotting")
else
error("Invalid method call")
end
end

include("data_loading.jl")
include("preprocessing.jl")
include("lineage_processing.jl")
Expand Down
18 changes: 7 additions & 11 deletions src/data_loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
delimiter::Char='\t',
required_columns=[:sequence_id, :sequence, :v_sequence_end, :j_sequence_start, :cdr3, :v_call, :j_call, :stop_codon])::DataFrame
Load data from a file and return a DataFrame.
Load data from a file (compressed or uncompressed) and return a DataFrame.
# Arguments
- `filepath::String`: Path to the data file.
- `delimiter::Char='\t'`: Delimiter used in the data file (default: tab).
- `required_columns::Vector{Symbol}`: Required columns in the data file.
- `required_columns::Vector{Symbol}`: Required columns to select from the data file.
# Returns
- `DataFrame`: DataFrame containing the loaded data.
Expand All @@ -19,14 +19,10 @@ Load data from a file and return a DataFrame.
function load_data(filepath::String;
delimiter::Char='\t',
required_columns=[:sequence_id, :sequence, :v_sequence_end, :j_sequence_start, :cdr3, :v_call, :j_call, :stop_codon])::DataFrame
# First, read the header to check for required columns
header = String.(split(readline(filepath), delimiter))
missing_columns = setdiff(String.(required_columns), header)

if !isempty(missing_columns)
throw(ArgumentError("Missing required columns: $(join(missing_columns, ", "))"))
try
df = CSV.File(filepath, delim=delimiter, select=required_columns) |> DataFrame
df = df[!, required_columns] # This is to ensure exception is thrown if any column is missing
catch e
rethrow(e)
end

# If all required columns are present, load the data
return CSV.File(filepath, delim=delimiter, select=required_columns) |> DataFrame
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ using LineageCollapse
include("test_data_loading.jl")
include("test_preprocessing.jl")
include("test_lineage_processing.jl")
include("test_plot_diagnostics.jl")
end
35 changes: 35 additions & 0 deletions test/test_plot_diagnostics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Test
using LineageCollapse

@testset "Plot Diagnostics" begin
@testset "CairoMakie not imported" begin
# Ensure CairoMakie is not imported
if isdefined(Main, :CairoMakie)
@warn "CairoMakie is already imported, which may affect the test results"
end

# Test that the function throws an error with the correct message
@test_throws ErrorException("Import CairoMakie to enable plotting") plot_diagnostics()

# Test with arguments
@test_throws ErrorException("Import CairoMakie to enable plotting") plot_diagnostics(rand(10))

# Test with keyword arguments
@test_throws ErrorException("Import CairoMakie to enable plotting") plot_diagnostics(color="red")
end

@testset "CairoMakie imported" begin
# Create a temporary module to simulate CairoMakie being imported
@eval Main module TestPlotDiagnostics
using LineageCollapse

# Simulate CairoMakie being imported
LineageCollapse.eval(:(CairoMakie = 1))

using Test
@test_throws ErrorException("Invalid method call") plot_diagnostics()
@test_throws ErrorException("Invalid method call") plot_diagnostics(rand(10))
@test_throws ErrorException("Invalid method call") plot_diagnostics(color="red")
end
end
end

0 comments on commit 311f773

Please sign in to comment.