Performs Principal Component Analysis (PCA) and generates a biplot for visualizing sample relationships based on gene expression data. Uses PCAtools for analysis and ggplot2 for visualization. Colors are automatically generated from RColorBrewer palettes.
generate_pca(
data,
sampleInfo,
sampleName_var,
annotate_sample_by = NULL,
label_points = FALSE,
sample_subset = NULL,
target_subset = NULL,
shape_by = NULL,
encircle = TRUE,
encircleFill = TRUE,
sample_colors = NULL,
output_dir = NULL,
plot_name = NULL,
plot_title = NULL,
plot_width = 10,
plot_height = 8,
...
)A matrix with targets in rows, samples in columns.
Row names should be the target names, and column names are the sample names.
It is assumed that data has already been transformed using log2(x + 1)
for each NULISAseq normalized count value x, i.e. NPQ.
A data frame with sample metadata. Rows are samples, columns are sample metadata variables.
Character string specifying the name of the column in sampleInfo
that matches the column names of data.
Character string specifying the column name from sampleInfo
to use for coloring points. Only one variable is allowed; defaults to NULL.
Logical indicating whether to add sample labels to the plot;
defaults to FALSE.
Vector of sample names for selected samples to include in PCA,
should match the existing column names of data; defaults to NULL (all samples).
Vector of target names for selected targets to include in PCA,
should match the existing row names of data; defaults to NULL (all targets).
Character string specifying the column name from sampleInfo
to use for point shapes; defaults to NULL.
Logical indicating whether to draw ellipses around groups;
defaults to TRUE.
Logical indicating whether to fill the ellipses;
defaults to TRUE.
Named vector of custom colors for sample groups.
Names should match the levels in annotate_sample_by; defaults to NULL.
Character string specifying the directory path to save the plot.
If NULL, the plot is not saved; defaults to NULL. If provided without
plot_name, a default filename with timestamp will be generated.
Character string specifying the filename for the saved plot,
including file extension (.pdf, .png, .jpg, or .svg). If NULL and output_dir
is provided, a default filename with timestamp will be used; defaults to NULL.
Character string for the title of the PCA plot; defaults to NULL.
Numeric value for the width of the saved plot in inches; defaults to 10.
Numeric value for the height of the saved plot in inches; defaults to 8.
Additional arguments passed to PCAtools::biplot function.
A list containing:
Character vector of target names used in the PCA after filtering.
The PCAtools PCA object containing all PCA results.
Data frame containing the PC scores (PC1, PC2, etc.) for each sample.
The ggplot2 object of the PCA biplot.
Character string of the full path to the saved file, or NULL if not saved.
The function performs the following steps:
Filters data to specified samples and targets
Removes targets with all zero values
Scales data by row (Z-score transformation)
Removes rows with NA, NaN, or Inf values after scaling
Performs PCA using PCAtools
Generates biplot with specified aesthetics
Optionally saves to file
To specify custom colors for sample groups, use the sample_colors parameter:
my_colors <- c("Control" = "#FF0000", "Treatment" = "#0000FF")if (FALSE) { # \dontrun{
# Basic PCA plot
result <- generate_pca(
data = Data_NPQ,
sampleInfo = sample_metadata,
sampleName_var = "SampleName",
annotate_sample_by = "Group"
)
# PCA with sample labels and custom shapes
result <- generate_pca(
data = Data_NPQ,
sampleInfo = sample_metadata,
sampleName_var = "SampleName",
annotate_sample_by = "Group",
shape_by = "Batch",
label_points = TRUE
)
# PCA with custom colors
custom_colors <- c("Control" = "blue", "Treatment" = "red")
result <- generate_pca(
data = Data_NPQ,
sampleInfo = sample_metadata,
sampleName_var = "SampleName",
annotate_sample_by = "Group",
sample_colors = custom_colors
)
# Save PCA plot to file
result <- generate_pca(
data = Data_NPQ,
sampleInfo = sample_metadata,
sampleName_var = "SampleName",
annotate_sample_by = "Group",
output_dir = "output/figures",
plot_name = "pca_analysis.pdf",
plot_title = "PCA Analysis of Expression Data",
plot_width = 12,
plot_height = 10
)
} # }