Skip to content

Instantly share code, notes, and snippets.

@Natay
Last active June 19, 2024 15:44
Show Gist options
  • Save Natay/3290b607338e4154ab57215391d14c7b to your computer and use it in GitHub Desktop.
Save Natay/3290b607338e4154ab57215391d14c7b to your computer and use it in GitHub Desktop.
Generate a pavian report given a directory of files
# Load pavion
library(pavian)
# Load the command line arguments
args = commandArgs(trailingOnly=TRUE)
# Directory containing input data files.
INPUT_DIR = args[1]
# Directory to store the final HTML report.
OUTPUT_DIR = args[2]
# Ensure DIRs does not have a trailing /
# Output file name
OUT_FILE = args[3] #'pavion-report.html'
# Report title
REPORT_TITLE = args[4] #"Report title"
# Date to put on the report
DATE = args[5]
# Comma seperated string to filter taxa in sankey.
# Example: Chordata,artificial sequences
FILTER_TAXA = trimws(unlist(strsplit(args[6], ",")))
# Combine sample names into a single
sample_set_names_combined <- function(){
res <- sapply(data$Name, basename)
res <- paste(res, collapse="_")
res <- gsub("[^A-Za-z\\-_]","_", res)
if (nchar(res) == 0){
return("Set1")
} else {
return(res)
}
}
# Load data found in directory
#data <- pavian::read_sample_data(system.file("shinyapp","example-data","brain-biopsies",package="pavian"))
data <- pavian::read_sample_data(dir(INPUT_DIR, pattern=NULL, all.files=FALSE, full.names=TRUE), is_files=TRUE)
# Gather the reports
reports <- pavian::read_reports(data$ReportFilePath, data$Name)
# Get the R Markdown template for final report file
rmd_file <- system.file("pavian-report.Rmd",package="pavian")
# Make a local copy of the template file
local_temp <- file.path(OUTPUT_DIR, "template.Rmd")
file.copy(rmd_file, local_temp, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(doc_title=REPORT_TITLE,
doc_author="",
doc_date=DATE,
set_name=sample_set_names_combined(),
all_data_loaded=TRUE,
sample_data=data,
reports=reports,
include_sankey=TRUE,
filter_taxa=FILTER_TAXA)
# Create the final html report.
rmarkdown::render(local_temp, output_file = OUT_FILE ,
params = params, output_format = "html_document",
envir = new.env())
# Remove the temporary R Markdown template used to create report.
file.remove(local_temp)
@Natay
Copy link
Author

Natay commented May 21, 2020

Run from command line using line below, after filling out the <> with appropriate values:

R -f pavion-report.R --args < input dir > < output dir > <out put file> < title > < date > < sankey filter taxa ( comma separated ) > 

# Example 
R -f pavion-report.R --args /input /output output.html "Report Title" `date +'%Y-%m-%d'` "Chordata, artificial sequences"

@Gerlex89
Copy link

Gerlex89 commented Feb 9, 2022

Hi,

I'm trying to automate a workflow and your script is what I'm looking for. However, I get this error:

> # Create the final html report. 
> rmarkdown::render(local_temp, output_file = OUT_FILE ,
+                   params = params, output_format = "html_document",
+                   envir = new.env())


processing file: template.Rmd
  |......                                                                |   9%
   inline R code fragments

  |.............                                                         |  18%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |...................                                                   |  27%
  ordinary text without R code

  |.........................                                             |  36%
label: sample_summary (with options) 
List of 1
 $ message: logi FALSE

Quitting from lines 58-98 (template.Rmd) 
Error in `colnames<-`(`*tmp*`, value = character(0)) : 
  attempt to set 'colnames' on an object with less than two dimensions
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> colnames<-

Execution halted

My command looks like this:

R -f pavian-report.R --args /path/to//Zymo-LOG-EVEN_kraken2_report.txt /path/to/1_Kraken2_classification output.html "Report Title"

In Krona the input just works, but I'd like to make use of Pavian if possible.

@karlkashofer
Copy link

Thanks, this is great !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment