Skip to content

Commit

Permalink
Added option to render to pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
ssnn-airr committed Feb 15, 2024
1 parent c0f8c0c commit cb3679e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: repcred
Type: Package
Version: 0.0.1
Date: 2024-02-13
Date: 2024-02-15
Authors@R: c(person("Susanna", "Marquez", role=c("aut", "cre"),
email="[email protected]"),
person("AIRR Community", role=c("cph")))
Expand Down
35 changes: 29 additions & 6 deletions R/repcred-core.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ repcredWeb <- function(appDir=system.file("shiny-app",
#' @param downsample Whether report will downsample repertoire
#' @param genome_file A reference set of the V(D)J alleles.
#' @param outdir Directory where the report will be generated
#' @param format Output format: html, pdf, all
#' @return Path to the credibility report.
#'
#' @examples
#'
#' rep_file <- system.file(package="repcred", "extdata", "ExampleDb.tsv")
#' repcred_report(rep_file, tempdir())
#' @export
repcred_report <- function(rep, outdir=NULL,genome_file=NULL, downsample=TRUE) {

repcred_report <- function(rep, outdir=NULL,genome_file=NULL, downsample=TRUE, format=c("html", "pdf", "all")) {
format <- match.arg(format)
if (file.exists(rep)) {
if (is.null(outdir)) {
outdir <- dirname(rep)
Expand All @@ -42,7 +43,7 @@ repcred_report <- function(rep, outdir=NULL,genome_file=NULL, downsample=TRUE) {

tryCatch(
{
report_path <- render_report(rep, outdir,genome_file,downsample)
report_path <- render_report(rep, outdir,genome_file,downsample, format)
},
error = function(e) {
stop(safeError(e))
Expand Down Expand Up @@ -96,11 +97,13 @@ getCoreStats <- function(data){
#'
#' @param rep Path to repertoire
#' @param outdir Directory where the report will be generated
#' @param downsample Whether report will downsample repertoire
#' @param genome A reference set of the V(D)J alleles.
#' @param downsample Whether report will downsample repertoire
#' @param format Output format: html, pdf, all
#' @export
render_report <- function(rep,outdir,genome=NULL,downsample) {
render_report <- function(rep,outdir,genome=NULL,downsample=TRUE, format=c("html","pdf","all")) {
path = "../rstudio/templates/project/project_files/"
format <- match.arg(format)
if (!dir.exists(outdir)) {
dir.create(outdir, recursive = T)
}
Expand All @@ -112,18 +115,38 @@ render_report <- function(rep,outdir,genome=NULL,downsample) {

#setwd("../rstudio/templates/project/project_files/")

if (format == "html") {
output_format <- 'bookdown::gitbook'
} else if (format == "pdf") {
output_format <- 'bookdown::pdf_book'
} else {
output_format <- format
}

# render
xfun::in_dir(
outdir,
book <- bookdown::render_book(
input = ".",
output_format='bookdown::gitbook',
output_format=output_format,
config_file ="_bookdown.yml",
clean=FALSE,
new_session=FALSE,
params=list("rep"=rep, outdir=outdir,"genome_file"=genome,
"downsample"=downsample))
)
if (format %in% c("pdf", "all")) {
is_pdf <- grepl("_main.pdf$",book)
for (i in which(is_pdf)) {
book_i <- gsub("_main.pdf$","repcred-report.pdf",book[i])
try(
if (file.rename(book[i],book_i)) {
book[i] <- book_i
}
)
}
}

book
}

Expand Down
16 changes: 13 additions & 3 deletions inst/repcred.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# -r Repertoire file, in AIRR (TSV) format.
# -o Output directory. Will be created if it does not exist.
# Defaults to tempdir().
# -f Output format: html, pdf, all
# -h Display help.

# Imports
Expand All @@ -19,6 +20,7 @@ suppressPackageStartupMessages(library("repcred"))
REP <- NULL
OUTDIR <- tempdir()
DOWN <- TRUE
FORMAT <- "html"

# Define commmandline arguments
opt_list <- list(make_option(c("-r", "--rep"), dest="REP", default=REP,
Expand All @@ -27,7 +29,9 @@ opt_list <- list(make_option(c("-r", "--rep"), dest="REP", default=REP,
help="Downsample."),
make_option(c("-o", "--outdir"), dest="OUTDIR", default=OUTDIR,
help=paste("Output directory. Will be created if it does not exist.",
"\n\t\tDefaults to the current working directory."))
"\n\t\tDefaults to the current working directory.")),
make_option(c("-f", "--format"), dest="FORMAT", default=FORMAT,
help="Output format: html, pdf, all")
)
# Parse arguments
opt <- parse_args(OptionParser(option_list=opt_list))
Expand All @@ -43,19 +47,25 @@ if (!("REP" %in% names(opt))) {
}
}

# Check format
if (!(any(opt$FORMAT %in% c("html", "pdf", "all"))) | length(opt$FORMAT)>1) {
stop("Output format (-f/--format) must be one of: html, pdf, all")
}

opt$OUTDIR <- normalizePath(opt$OUTDIR)
dir.create(opt$OUTDIR, recursive = T)

message("\nRunning repcred")
message("|- Repertoire:\n", normalizePath(opt$REP))
message("|- Downsample:\n", opt$DOWN)
message("|- Output dir:\n", normalizePath(opt$OUTDIR),"\n")
message("|- Output dir:\n", normalizePath(opt$OUTDIR))
message("|- Output format:\n", opt$FORMAT,"\n")

sink(file(file.path(opt$OUTDIR,"message.log"),"wt"), type="message")
sink(file(file.path(opt$OUTDIR,"output.log"),"wt"), type="output")

tryCatch(
report <- render_report(rep=opt$REP, outdir=opt$OUTDIR, downsample = opt$DOWN),
report <- render_report(rep=opt$REP, outdir=opt$OUTDIR, downsample = opt$DOWN, format=opt$FORMAT),
error = function(e) {
stop(safeError(e))
}
Expand Down
5 changes: 5 additions & 0 deletions inst/rstudio/templates/project/project_files/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ output:
download: yes
sharing: no
keep_md: true
bookdown::pdf_book:
keep_tex: false
toc: true
base_format: rmarkdown::pdf_document
number_sections: true
params:
date: !r date()
echo: FALSE
Expand Down

0 comments on commit cb3679e

Please sign in to comment.