Skip to content

Commit

Permalink
Added print method for amp_ordinate figure captions. Moved all print …
Browse files Browse the repository at this point in the history
…methods to print_methods.R
  • Loading branch information
KasperSkytte committed Oct 16, 2018
1 parent 24b90ca commit d403ccf
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 69 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Imports:
cowplot,
readxl,
crayon,
cli,
biomformat,
doParallel,
foreach
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(print,ampvis2)
S3method(print,figcaption)
S3method(print,hmfunplot)
export(amp_alphadiv)
export(amp_boxplot)
Expand Down Expand Up @@ -31,7 +32,10 @@ importFrom(ape,reorder.phylo)
importFrom(ape,write.dna)
importFrom(biomformat,biom_data)
importFrom(biomformat,read_biom)
importFrom(cli,cat_line)
importFrom(cli,rule)
importFrom(cowplot,plot_grid)
importFrom(crayon,italic)
importFrom(crayon,underline)
importFrom(data.table,as.data.table)
importFrom(data.table,data.table)
Expand Down
9 changes: 3 additions & 6 deletions R/amp_ordinate.R
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,12 @@ amp_ordinate <- function(data,
return(.)
}
}
# set class to use custom print method
class(captionwithrefs) <- "figcaption"

# Print the caption with references, if any
if (isTRUE(print_caption)) {
cli::cat_line(cli::rule("Auto-generated figure caption (start)"))
captionwithrefs %>%
strwrap() %>%
crayon::italic() %>%
cli::cat_line()
cli::cat_line(cli::rule("Auto-generated figure caption (end)"))
print(captionwithrefs)
}

##### Return #####
Expand Down
65 changes: 2 additions & 63 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,69 +146,7 @@ amp_rename <- function(data, tax_class = NULL, tax_empty = "best", tax_level = "

return(data)
}
#' Prints heatmap+functions plot (internal function)
#'
#' @param hmfunplot plot
#'
#' @export
#' @importFrom cowplot plot_grid
#'
print.hmfunplot <- function(hmfunplot) {
print(cowplot::plot_grid(hmfunplot$heatmap,
hmfunplot$functions,
ncol = 2,
rel_widths = attributes(hmfunplot)[["rel_widths"]],
align = "h",
axis = "tb"
))
}
#' Prints ampvis2 object summary (internal function)
#'
#' @param data (\emph{required}) Data list as loaded with \code{\link{amp_load}}.
#'
#' @importFrom crayon underline
#' @export
#' @author Kasper Skytte Andersen \email{kasperskytteandersen@@gmail.com}
#'
print.ampvis2 <- function(data) {
### calculate basic statistics and useful information about the data, print it
if (!isTRUE(attributes(data)$normalised)) {
# calculate basic stats and store in attributes for use in print.ampvis2
readstats <- attributes(data)$readstats <- list(
"Total#Reads" = as.character(sum(data$abund)),
"Min#Reads" = as.character(min(colSums(data$abund))),
"Max#Reads" = as.character(max(colSums(data$abund))),
"Median#Reads" = as.character(median(colSums(data$abund))),
"Avg#Reads" = as.character(round(mean(colSums(data$abund)), digits = 2))
)
} else if (isTRUE(attributes(data)$normalised)) {
readstats <- attributes(data)$readstats
}
cat(class(data), "object with", length(data), "elements.", crayon::underline("\nSummary of OTU table:\n"))
print.table(c(
"Samples" = as.character(ncol(data$abund)),
"OTUs" = as.character(nrow(data$abund)),
readstats
),
justify = "right"
)
if (isTRUE(attributes(data)$normalised)) {
cat("(The read counts have been normalised)\n")
}
cat(crayon::underline("\nAssigned taxonomy:\n"))
print.table(c(
"Kingdom" = paste0(sum(nchar(data$tax$Kingdom) > 3), "(", round(sum(nchar(data$tax$Kingdom) > 3) / nrow(data$abund), digits = 2) * 100, "%)"),
"Phylum" = paste0(sum(nchar(data$tax$Phylum) > 3), "(", round(sum(nchar(data$tax$Phylum) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Class" = paste0(sum(nchar(data$tax$Class) > 3), "(", round(sum(nchar(data$tax$Class) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Order" = paste0(sum(nchar(data$tax$Order) > 3), "(", round(sum(nchar(data$tax$Order) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Family" = paste0(sum(nchar(data$tax$Family) > 3), "(", round(sum(nchar(data$tax$Family) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Genus" = paste0(sum(nchar(data$tax$Genus) > 3), "(", round(sum(nchar(data$tax$Genus) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Species" = paste0(sum(nchar(data$tax$Species) > 3), "(", round(sum(nchar(data$tax$Species) > 3) / nrow(data$abund) * 100, digits = 2), "%)")
),
justify = "right"
)
cat(crayon::underline("\nMetadata variables:"), as.character(ncol(data$metadata)), "\n", paste(as.character(colnames(data$metadata)), collapse = ", "))
}

#' Functional information tool
#'
#' Makes raw MiDAS function data compatible with ampvis format. Internal function, not exported.
Expand Down Expand Up @@ -321,6 +259,7 @@ amp_cleanMiF <- function(data) {
)
return(MiF)
}

#' Calculate weighted or unweighted UniFrac distances. Adopted from fastUniFrac() from phyloseq
#'
#' @param abund Abundance table with OTU counts, in \code{ampvis2} objects it is available with simply data$abund
Expand Down
79 changes: 79 additions & 0 deletions R/print_methods.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Prints heatmap+functions plot (internal function)
#'
#' @param hmfunplot plot
#'
#' @export
#' @importFrom cowplot plot_grid
print.hmfunplot <- function(hmfunplot) {
print(cowplot::plot_grid(hmfunplot$heatmap,
hmfunplot$functions,
ncol = 2,
rel_widths = attributes(hmfunplot)[["rel_widths"]],
align = "h",
axis = "tb"
))
}

#' Prints ampvis2 object summary (internal function)
#'
#' @param data (\emph{required}) Data list as loaded with \code{\link{amp_load}}.
#'
#' @importFrom crayon underline
#' @export
#' @author Kasper Skytte Andersen \email{kasperskytteandersen@@gmail.com}
print.ampvis2 <- function(data) {
### calculate basic statistics and useful information about the data, print it
if (!isTRUE(attributes(data)$normalised)) {
# calculate basic stats and store in attributes for use in print.ampvis2
readstats <- attributes(data)$readstats <- list(
"Total#Reads" = as.character(sum(data$abund)),
"Min#Reads" = as.character(min(colSums(data$abund))),
"Max#Reads" = as.character(max(colSums(data$abund))),
"Median#Reads" = as.character(median(colSums(data$abund))),
"Avg#Reads" = as.character(round(mean(colSums(data$abund)), digits = 2))
)
} else if (isTRUE(attributes(data)$normalised)) {
readstats <- attributes(data)$readstats
}
cat(class(data), "object with", length(data), "elements.", crayon::underline("\nSummary of OTU table:\n"))
print.table(c(
"Samples" = as.character(ncol(data$abund)),
"OTUs" = as.character(nrow(data$abund)),
readstats
),
justify = "right"
)
if (isTRUE(attributes(data)$normalised)) {
cat("(The read counts have been normalised)\n")
}
cat(crayon::underline("\nAssigned taxonomy:\n"))
print.table(c(
"Kingdom" = paste0(sum(nchar(data$tax$Kingdom) > 3), "(", round(sum(nchar(data$tax$Kingdom) > 3) / nrow(data$abund), digits = 2) * 100, "%)"),
"Phylum" = paste0(sum(nchar(data$tax$Phylum) > 3), "(", round(sum(nchar(data$tax$Phylum) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Class" = paste0(sum(nchar(data$tax$Class) > 3), "(", round(sum(nchar(data$tax$Class) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Order" = paste0(sum(nchar(data$tax$Order) > 3), "(", round(sum(nchar(data$tax$Order) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Family" = paste0(sum(nchar(data$tax$Family) > 3), "(", round(sum(nchar(data$tax$Family) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Genus" = paste0(sum(nchar(data$tax$Genus) > 3), "(", round(sum(nchar(data$tax$Genus) > 3) / nrow(data$abund) * 100, digits = 2), "%)"),
"Species" = paste0(sum(nchar(data$tax$Species) > 3), "(", round(sum(nchar(data$tax$Species) > 3) / nrow(data$abund) * 100, digits = 2), "%)")
),
justify = "right"
)
cat(crayon::underline("\nMetadata variables:"), as.character(ncol(data$metadata)), "\n", paste(as.character(colnames(data$metadata)), collapse = ", "))
}

#' Print method for figure caption created by amp_ordinate
#'
#' @param captionwithrefs Character vector with the caption
#'
#' @importFrom cli cat_line rule
#' @importFrom crayon italic
#' @export
#' @author Kasper Skytte Andersen \email{kasperskytteandersen@@gmail.com}
print.figcaption <- function(captionwithrefs) {
cli::cat_line(cli::rule("Auto-generated figure caption (start)"))
captionwithrefs %>%
strwrap() %>%
crayon::italic() %>%
cli::cat_line()
cli::cat_line(cli::rule("Auto-generated figure caption (end)"))
}
17 changes: 17 additions & 0 deletions man/print.figcaption.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d403ccf

Please sign in to comment.