/
snakemake.R
68 lines (67 loc) · 2.19 KB
/
snakemake.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#' Save output, messages, warnings, and errors to the Snakemake log file
#'
#' This function checks whether a log file was specified in the Snakemake rule.
#' If so, it directs any output, messages, warnings, or errors to the
#' rule-specific log file.
#' See the Snakemake documentation on [log files](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#log-files)
#' and [R scripts](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#r-and-r-markdown)
#' for more details.
#'
#' @param quiet Silence messages about the status of the snakemake object and
#' log file (default: `TRUE`).
#'
#' @export
#' @author Kelly Sovacool \email{sovacool@@umich.edu}
#' @examples
#' # The Snakemake object doesn't exist, so nothing happens
#' log_snakemake(quiet = FALSE)
log_snakemake <- function(quiet = TRUE) {
if (exists("snakemake")) {
if (FALSE) {
snakemake <- NULL
} # silences warning "no visible binding for global variable ‘snakemake’
if (length(snakemake@log) > 0) {
log_filepath <- snakemake@log[1][[1]]
if (isFALSE(quiet)) {
message(paste("Saving output to", log_filepath))
}
log <- file(log_filepath, open = "wt")
sink(log, append = TRUE)
sink(log, append = TRUE, type = "message")
} else {
if (isFALSE(quiet)) {
message(
paste(
"No log file was specified in the Snakemake rule",
snakemake@rule
)
)
}
}
} else {
if (isFALSE(quiet)) {
message("No Snakemake object exists, so all output will print as usual")
}
}
}
#' Get the Snakemake wildcards as a tibble
#'
#' @return a tibble of wildcards, with columns as names and rows as values
#' @export
#' @author Kelly Sovacool \email{sovacool@@umich.edu}
get_wildcards_tbl <- function() {
if (!exists("snakemake")) {
stop("No Snakemake object exists")
snakemake <- NULL
} else if (length(snakemake@wildcards) == 0) {
warning("The Snakemake object contains no wildcards")
}
wildcard_names <- snakemake@wildcards %>%
names() %>%
Filter(function(x) {
nchar(x) > 0
}, .)
wildcards <- snakemake@wildcards[wildcard_names] %>%
dplyr::as_tibble()
return(wildcards)
}