Skip to content

Commit

Permalink
test: basic unit tests for plot_pca()
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Jul 24, 2024
1 parent dfb9ec5 commit 24a209c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ filter_counts <- function(renee_ds) {
log_counts <- log((as.matrix(df.filt[, samples_to_include] + 0.5)))
rownames(log_counts) <- df.filt[, 1]

pca.df <- plot_pca(log_counts,
pcaPlot <- plot_pca(log_counts,
sample_metadata,
samples_to_include,
samples_to_rename_manually,
Expand Down
4 changes: 2 additions & 2 deletions R/pca.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Perform and plot a Principal Coordinate Analysis
#' Perform and plot a Principal Components Analysis
#'
#' @param log_counts log-transformed filtered counts
#' @inheritParams filter_counts
Expand Down Expand Up @@ -49,7 +49,7 @@ plot_pca <- function(log_counts,

# plot PCA
pcaPlot <- ggplot(pca.df, aes(x = xdata, y = ydata, text = sample)) +
geom_point(aes(color = group), text = sample, size = point_size_for_pca) +
geom_point(aes(color = group), size = point_size_for_pca) +
theme_bw() +
theme(
legend.position = legend_position_for_pca,
Expand Down
59 changes: 59 additions & 0 deletions tests/testthat/test-pca.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
log_counts <- structure(
c(
4.68203758078952, 4.85622577980595, 4.78525385564269,
4.49631900610197, 4.06045359796882, 4.23984554358195, 4.5945977606547,
4.64452454872198, 5.01435137189661, 4.97202092328668, 4.63530665635318,
4.2685504137767, 4.55963475259408, 4.48551456875509, 4.8871583679813,
4.80572893488115, 4.62485692334614, 3.28500783834613, 4.27910086185478,
4.44991091582054, 5.1543875974913, 5.04097648388479, 3.93341992672261,
3.60201340344135, 4.38819757498796, 4.52837375303351, 5.04374399504142,
5.16774070067169, 4.88762227733767, 3.4002608223214, 4.86037842060906,
5.24497940734463, 4.91946017962122, 5.04632856830695, 4.96825190546972,
4.23362208987531, 4.11790170891073, 4.05303076635639, 4.92868500786216,
4.94726383191083, 3.80832003906478, 4.59490650795107, 4.85738793032812,
4.40655427188147, 4.22382743940071, 4.58770826515446, 4.96057466313368,
4.99824283163569, 4.5437556841171, 4.97124300205374, 4.93192370480255,
4.13835014251084, 5.0939015418123, 5.03285949305828
),
dim = c(6L, 9L),
dimnames = list(
c("Mrpl15_32", "Lypla1_34", "Tcea1_36", "Atp6v1h_44", "Rb1cc1_54", "Pcmtd1_68"),
c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3")
)
)
sample_meta <- structure(
list(
Sample = c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"),
Group = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
Replicate = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
Batch = c(1, 2, 2, 1, 1, 2, 1, 2, 2),
Label = c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3")
),
row.names = c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"),
class = "data.frame"
)

test_that("plot_pca layers are expected", {
p <- plot_pca(log_counts,
sample_meta,
samples_to_include = c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"),
samples_to_rename_manually = NULL,
groups_column = "Group",
labels_column = "Label",
color_values = c(
"#5954d6", "#e1562c", "#b80058", "#00c6f8", "#d163e6", "#00a76c",
"#ff9287", "#008cf9", "#006e00", "#796880", "#FFA500", "#878500"
),
principal_component_on_x_axis = 1,
principal_component_on_y_axis = 2,
legend_position_for_pca = "top",
point_size_for_pca = 1,
add_labels_to_pca = TRUE,
label_font_size = 3,
label_offset_y_ = 2,
label_offset_x_ = 2
)

expect_s3_class(p$layers[[1]], "ggproto")
expect_s3_class(p$layers[[1]]$geom, "GeomPoint")
})

0 comments on commit 24a209c

Please sign in to comment.