Skip to content

Commit

Permalink
Add similar motif scanning and file downloads to denovo motif enrichm…
Browse files Browse the repository at this point in the history
…ent section
  • Loading branch information
HDash committed Jul 8, 2024
1 parent 8e52686 commit d636165
Show file tree
Hide file tree
Showing 34 changed files with 913 additions and 108 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ Imports:
dplyr,
purrr,
tidyr,
tibble
heatmaply,
stats,
utils
Suggests:
BiocStyle,
BSgenome.Hsapiens.UCSC.hg19,
Expand All @@ -73,7 +75,6 @@ Suggests:
remotes,
rworkflows,
testthat (>= 3.0.0),
utils,
withr,
emoji,
curl,
Expand Down
13 changes: 11 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ export(calc_frip)
export(check_ENCODE)
export(check_JASPAR)
export(check_genome_build)
export(compare_motifs)
export(denovo_motifs)
export(find_motifs)
export(format_exptype)
export(get_JASPARCORE)
export(get_df_distances)
export(get_df_enrichment)
export(motif_enrichment)
export(motif_similarity)
export(plot_enrichment_individual)
export(plot_enrichment_overall)
export(pretty_number)
export(read_motif_file)
export(read_peak_file)
export(report_command)
export(report_header)
export(save_peak_file)
export(segregate_seqs)
export(summit_to_motif)
export(to_plotly)
Expand All @@ -31,7 +32,6 @@ import(dplyr)
import(ggplot2)
import(plotly, except = last_plot)

import(tibble)
import(tidyr)
importFrom(BSgenome,getSeq)
importFrom(BiocFileCache,BiocFileCache)
Expand All @@ -54,6 +54,7 @@ importFrom(Rsamtools,countBam)
importFrom(S4Vectors,queryHits)
importFrom(S4Vectors,subjectHits)
importFrom(SummarizedExperiment,assay)
importFrom(heatmaply,heatmaply)
importFrom(htmltools,tagList)
importFrom(htmlwidgets,JS)
importFrom(magrittr,"%>%")
Expand All @@ -62,8 +63,13 @@ importFrom(memes,runAme)
importFrom(memes,runFimo)
importFrom(memes,runTomTom)
importFrom(plotly,ggplotly)
importFrom(plotly,layout)
importFrom(plotly,subplot)
importFrom(purrr,map_chr)
importFrom(purrr,map_df)
importFrom(rmarkdown,render)
importFrom(stats,quantile)
importFrom(stats,sd)
importFrom(tidyr,pivot_longer)
importFrom(tools,file_ext)
importFrom(tools,file_path_sans_ext)
Expand All @@ -73,6 +79,9 @@ importFrom(universalmotif,read_jaspar)
importFrom(universalmotif,read_meme)
importFrom(universalmotif,read_transfac)
importFrom(universalmotif,read_uniprobe)
importFrom(utils,capture.output)
importFrom(utils,read.table)
importFrom(utils,write.table)
importFrom(viridis,scale_color_viridis)
importFrom(viridis,scale_fill_viridis)
importFrom(viridis,viridis)
29 changes: 15 additions & 14 deletions R/MotifPeeker.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#' de-novo motif discovery for the third section of the report. (default = TRUE)
#' @param download_buttons A logical indicating whether to include download
#' buttons for various files within the HTML report. (default = TRUE)
#' @param output_dir A character string specifying the directory to save the
#' @param out_dir A character string specifying the directory to save the
#' output files. (default = \code{tempdir()}) A sub-directory with the output
#' files will be created in this directory.
#' @param save_runfiles A logical indicating whether to save intermediate files
Expand Down Expand Up @@ -86,7 +86,7 @@
#' @import ggplot2
#' @import dplyr
#' @import tidyr
#' @import tibble
#' @importFrom stats quantile sd
#' @importFrom viridis scale_fill_viridis scale_color_viridis
#' @importFrom tools file_path_sans_ext
#' @importFrom rmarkdown render
Expand Down Expand Up @@ -134,7 +134,7 @@
#' denovo_motifs = 3,
#' motif_db = NULL,
#' download_buttons = TRUE,
#' output_dir = tempdir(),
#' out_dir = tempdir(),
#' workers = 2,
#' debug = FALSE,
#' quiet = TRUE,
Expand All @@ -159,7 +159,7 @@ MotifPeeker <- function(
motif_db = NULL,
download_buttons = TRUE,
meme_path = NULL,
output_dir = tempdir(),
out_dir = tempdir(),
save_runfiles = FALSE,
display = NULL,
workers = 2,
Expand Down Expand Up @@ -203,15 +203,16 @@ MotifPeeker <- function(
}

### Create output folder ###
if (!dir.exists(output_dir)) {
if (!dir.exists(out_dir)) {
stp_msg <- "Output directory does not exist."
stopper(stp_msg)
}
output_dir <- file.path(
output_dir,
out_dir <- file.path(
out_dir,
paste0("MotifPeeker_", format(Sys.time(), "%Y%m%d_%H%M%S"))
)
dir.create(output_dir, showWarnings = debug)
dir.create(out_dir, showWarnings = debug)
out_dir <- normalizePath(out_dir)

### Store arguments in a list ###
args_list <- list(
Expand All @@ -231,7 +232,7 @@ MotifPeeker <- function(
trim_seq_width = trim_seq_width,
download_buttons = download_buttons,
meme_path = meme_path,
output_dir = output_dir,
out_dir = out_dir,
save_runfiles = save_runfiles,
workers = workers,
debug = debug,
Expand All @@ -243,7 +244,7 @@ MotifPeeker <- function(
"MotifPeeker.Rmd", package = "MotifPeeker")
rmarkdown::render(
input = rmd_file,
output_dir = output_dir,
output_dir = out_dir,
output_file = "MotifPeeker.html",
quiet = quiet,
params = args_list
Expand All @@ -252,7 +253,7 @@ MotifPeeker <- function(
### Display report ###
messager(
"Script run successfully. \nOutput saved at:",
output_dir,
out_dir,
"\nTime taken:",
round(difftime(Sys.time(), start_time, units = "mins"), 2), "mins.",
v = verbose
Expand All @@ -262,11 +263,11 @@ MotifPeeker <- function(
display <- tolower(display)
if (display == "browser") {
check_dep("utils")
utils::browseURL(file.path(output_dir, "MotifPeeker.html"))
utils::browseURL(file.path(out_dir, "MotifPeeker.html"))
} else if (display == "rstudio") {
file.show(file.path(output_dir, "MotifPeeker.html"))
file.show(file.path(out_dir, "MotifPeeker.html"))
}
}

return(output_dir)
return(out_dir)
}
17 changes: 13 additions & 4 deletions R/check_dep.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
#' Stop execution if a package is not attached.
#'
#' @param pkg a character string of the package name
#' @param fatal a logical value indicating whether to stop execution if the
#' package is not attached.
#' @param custom_msg a custom message to display if the package is not attached.
#'
#' @return Null
#'
#' @keywords internal
check_dep <- function(pkg){
check_dep <- function(pkg, fatal = TRUE, custom_msg = NULL){
if (is.null(custom_msg)) {
custom_msg <- paste("Package", shQuote(pkg), "is required to run this",
"function.")
}
if (!requireNamespace(pkg, quietly = TRUE)) {
stp_msg <- paste("Package", shQuote(pkg), "is required to run this",
"function.")
stop(stp_msg)
if (fatal) {
stop(custom_msg)
} else {
warning(custom_msg)
}
}
}
63 changes: 63 additions & 0 deletions R/download_button.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' Create a download button
#'
#' Creates a download button for a file or directory, suitable to embed into
#' an HTML report.
#'
#' @param path A character string specifying the path to the file or directory.
#' @param type A character string specifying the type of download. Either
#' \code{"file"} or \code{"dir"}.
#' @param add_button A logical indicating whether to add the download button to
#' the HTML report. (default = TRUE)
#' @inheritParams downloadthis::download_file
#' @inheritDotParams downloadthis::download_file
#'
#' @importFrom htmltools tagList
#'
#' @inherit downloadthis::download_file return
#'
#' @seealso \code{\link[downloadthis]{download_file}}
#'
#' @keywords internal
download_button <- function(path,
type,
button_label,
output_name = NULL,
button_type = "success",
has_icon = TRUE,
icon = "fa fa-save",
add_button = TRUE,
...) {
if (add_button) {
wrn_msg <- paste("Package", shQuote("downloadthis"), "is required to",
"add download buttons to the HTML report. Skipping",
"download buttons...")
check_dep("downloadthis", fatal = FALSE, custom_msg = wrn_msg)

type <- tolower(type)
if (type == "dir") {
btn <- downloadthis::download_dir(
path = path,
output_name = output_name,
button_label = button_label,
button_type = button_type,
has_icon = has_icon,
icon = icon,
self_contained = TRUE
)
} else if (type == "file") {
btn <- downloadthis::download_file(
path = path,
output_name = output_name,
button_label = button_label,
button_type = button_type,
has_icon = has_icon,
icon = icon,
self_contained = TRUE
)
}

return(btn)
} else {
return(invisible())
}
}
65 changes: 65 additions & 0 deletions R/get_download_buttons.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' Get download buttons for peak file, STREME and TOMOTM output
#'
#' @param comparison_i Index of the comparison pair group.
#' @param start_i Index of the first comparison pair.
#' @param segregated_peaks A list of peak files generated from
#' \code{\link{segregate_seqs}}.
#' @param out_dir A character vector of the directory with STREME and TOMTOM
#' output.
#' @param add_buttons A logical indicating whether to prepare download buttons.
#' @param verbose A logical indicating whether to print messages.
#'
#' @returns A list of download buttons for peak file, STREME and TOMTOM output.
#'
#' @keywords internal
get_download_buttons <- function(comparison_i,
start_i,
segregated_peaks,
out_dir,
add_buttons = TRUE,
verbose = FALSE) {
if (!add_buttons) return(NULL)
messager("Generating download buttons...", v = verbose)

### Peak file button ###
btns1 <- lapply(seq_len(4), function(x) {
peak_file <- save_peak_file(segregated_peaks[[comparison_i]][[x]])
download_button(
peak_file,
type = "file",
button_label = "Download: <code>Peak file</code>",
output_name = paste0("peak_", random_string(6))
)
})

### STREME output button ###
btns2 <- lapply(seq_len(4), function(x) {
streme_path <- file.path(out_dir, "streme", start_i + x - 1)
if (!dir.exists(streme_path)) return(NULL)
download_button(
streme_path,
type = "dir",
button_label = "Download: <code>STREME output</code>",
output_name = paste0("streme_", random_string(6))
)
})

### TOMTOM output button ###
btns3 <- lapply(seq_len(4), function(x) {
tomtom_path <- file.path(out_dir, "tomtom", start_i + x - 1)
if (!dir.exists(tomtom_path)) return(NULL)
download_button(
tomtom_path,
type = "dir",
button_label = "Download: <code>TOMTOM output</code>",
output_name = paste0("tomtom_", random_string(6))
)
})

all_btns <- list(
peak_file = btns1,
streme_output = btns2,
tomtom_output = btns3
)
return(all_btns)
}
3 changes: 2 additions & 1 deletion R/link_JASPAR.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ link_JASPAR <- function(motif_id, download = FALSE) {
motif_id, ".jaspar"))
} else {
## Return HTML embeddable matrix profile link
return(paste0("https://jaspar.elixir.no/matrix/", motif_id))
return(paste0("<a href='https://jaspar.elixir.no/matrix/", motif_id,
"' target='_blank'>", motif_id, "</a>"))
}
}
8 changes: 4 additions & 4 deletions R/messager.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
#'
#' @keywords internal
messager <- function(...,
v = Sys.getenv("VERBOSE")!="FALSE",
parallel = FALSE) {
v = Sys.getenv("VERBOSE") != "FALSE",
parallel = TRUE) {
msg <- paste(...)

message_parallel <- function(...) {
system(sprintf('echo "%s"', paste0(..., collapse = "")))
}
if(isTRUE(parallel)){
if(v) try({message_parallel(...)})
if(v) try({message_parallel(msg)})
} else {
msg <- paste(...)
if (v) try({message(msg)})
}
}
Loading

0 comments on commit d636165

Please sign in to comment.