Commit a0edb253 authored by amillan's avatar amillan
Browse files

🔧 new argument to the function plot_dendocluster, allowing...

🔧  new argument to the function plot_dendocluster, allowing to set the sample identifiers to be displayed on the plot
parent 90564f64
#' plot_dendocluster
#'
#' Draws a dendogram where samples are clustered based on the number of elements
#' Draws a dendrogram where samples are clustered based on the number of elements
#' present on each sample from a "spectral_count_object". This graph is
#' constructed based on Spearman correlations transformed into distances
#' and plotted with the logic of the package
......@@ -14,7 +14,11 @@
#'
#' @param target_variable Character indicating the name of one column
#' from metadata. The different levels in this column will be represented
#' as different colors in the final dendogram.
#' as different colors in the final dendrogram.
#'
#' @param samples_label Character indicating the name of one column from
#' metadata. The name of different elements of this variable will be
#' displayed on the dendrogram plot.
#'
#' @param file_title Character indicating the name of the generated file.
#'
......@@ -31,7 +35,7 @@
#' @param force Logic value set at FALSE by default in order to ask
#' permission to create a pdf file in the workstation of the user.
#'
#' @return A dendogram plot (pdf) indicating the number of elements per
#' @return A dendrogram plot (pdf) indicating the number of elements per
#' sample.
#'
#' @examples
......@@ -40,12 +44,12 @@
#' data(fecal_waters)
#' str(fecal_waters$metadata)
#'
#' plot_dendocluster(fecal_waters, "Condition", "title_dendogram")
#' plot_dendocluster(fecal_waters, "Condition", "SC_name", "title_dendogram")
#'
#' plot_dendocluster(fecal_waters, "Condition", "title_dendogram",
#' plot_dendocluster(fecal_waters, "Condition", "SC_name", "title_dendogram",
#' hclust_method = "mcquitty")
#'
#' plot_dendocluster(fecal_waters, "Condition", "title_dendogram_groups",
#' plot_dendocluster(fecal_waters, "Condition", "SC_name", "title_dendogram_groups",
#' correlation_method = "pearson")
#'
#' \dontshow{setwd(.old_wd)}
......@@ -64,7 +68,7 @@
plot_dendocluster <-
function(spectral_count_object, target_variable, file_title,
function(spectral_count_object, target_variable, samples_label, file_title,
hclust_method = "ward.D", correlation_method = "spearman", force = FALSE){
spectral_count_object <- spectral_count_object
......@@ -79,11 +83,14 @@ function(spectral_count_object, target_variable, file_title,
metadata <- spectral_count_object[[2]]
if(target_variable %in% colnames(metadata)){
if((target_variable %in% colnames(metadata)) &
(samples_label %in% colnames(metadata))){
elements_meta <- as.character(metadata[[target_variable]])
dend_labels <- as.character(metadata[[samples_label]])
# list of colors
colors_condition = NULL
colors_condition <- NULL
unique_conditions <- unique(elements_meta)
print("Indicate colors by name (ex. red) or hexadecimal code (ex. #56338C)")
for(i in 1:length(unique_conditions)){
......@@ -106,16 +113,17 @@ function(spectral_count_object, target_variable, file_title,
ncolors <- length(colors_vector)
if (ncols == ncolors) {
if (is.character(file_title) == TRUE && nchar(file_title) > 1){
if (is.character(file_title) == TRUE &
nchar(file_title) > 1){
SC_sampple_id <- spectral_count_object[[1]]
colnames(SC_sampple_id) <- metadata$SampleID
colnames(SC_sampple_id) <- metadata[[samples_label]]
# Add the number of elements per sample
max_length_name <- max(nchar(colnames(SC_sampple_id)))
for (sample in 1:c(length(colnames(SC_sampple_id)))) {
sample_name <- names(SC_sampple_id[sample])
length_name <- nchar(sample_name)
length_space <- max_length_name - length_name + 7
length_space <- max_length_name - length_name + 5
length_space <- paste(replicate(length_space, " "), collapse = "")
sample_elements <- nrow(SC_sampple_id[SC_sampple_id[sample] > 0, ])
new_name <- paste(sample_name, length_space, sample_elements)
......@@ -147,13 +155,13 @@ function(spectral_count_object, target_variable, file_title,
filename <- paste(file_title, ".pdf", sep = "")
pdf(filename, width = 7, height = height)
par(cex = 1.2, mar = c(2, 2, 0, 13))
par(cex = 1.2, mar = c(2, 2, 0, 15))
print(plot(SC_sampple_id_dend, horiz = TRUE))
dev.off()
print("Clustering file generated")
}
else {
stop("The third argument must be a character indication the title of the file")
stop("The fourth argument must be a character indication the title of the file")
}
}
else{
......@@ -162,7 +170,7 @@ function(spectral_count_object, target_variable, file_title,
}
else{
var_options <- paste(colnames(metadata), collapse = ", ")
stop(paste(c("The second argument must be ONE of the following options:", var_options), collapse = " "))
stop(paste(c("The second and third arguments must be ANY of the following options:", var_options), collapse = " "))
}
}
else{
......
......@@ -145,7 +145,7 @@ str(SC_subgroups$metadata)
# ex. red, yellow, darkorange, blue
# ex. #440154FF, #FDE725FF, #404788FF, #55C667FF, #B8DE29FF
plot_dendocluster(SC_subgroups, "Condition", "graph_dendogram_by_groups")
plot_dendocluster(SC_subgroups, "Condition", "msrunfile", "graph_dendogram_by_subgroups")
# ------------- Reformating -------------
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment