Example of R script to generate a combined RNA-Seq report
R scripts can be used to process reports in JSON format. The script below is an example of how RNA-Seq reports generated by the CLC Genomics Workbench could be processed to compare information across samples. The same idea could be used for processing other sorts of reports.
The script uses jsonlite to parse all the JSON reports. Plots are produced using the package ggplot2. This script is intended for inspiration only and is not supported.
library(jsonlite) library(tools) library(ggplot2)
The script relies on the following functions to extract the data from the parsed JSON files.
# Get the read count statistics from a parsed JSON report. get_read_count_stats <- function(parsed_report) { mapping_statistics <- parsed_report$data$mapping_statistics total_reads <- 0 stats <- c() if ("single_reads" %in% names(mapping_statistics)) { table <- mapping_statistics$single_reads$table_1 # use the id column to give names to the rows row.names(table) <- table$id stats <- c(table["Reads mapped", "percent"], table["Reads not mapped", "percent"]) total_reads <- total_reads + table["Total", "number_of_sequences"] } else { stats <- c(stats, rep(NA, 2)) } if ("paired_reads" %in% names(mapping_statistics)) { table <- mapping_statistics$paired_reads$table_1 # use the id column to give names to the rows row.names(table) <- table$id stats <- c(stats, table["Reads mapped in pairs", "percent"], table["Reads mapped in broken pairs", "percent"], table["Reads not mapped", "percent"]) total_reads <- total_reads + table["Total", "number_of_sequences"] } else { stats <- c(stats, rep(NA, 3)) } stats <- c(total_reads, stats) names(stats) <- c("reads_count", "single_mapped", "single_not_mapped", "paired_mapped_pairs", "paired_broken_pairs", "paired_not_mapped") return(data.frame(sample = basename(file_path_sans_ext(report)), t(stats))) }
#' Get the paired distance from a parsed report. Returns null if the reads were #' unpaired. get_paired_distance <- function(parsed_report) { section <- parsed_report$data$read_quality_control if (!("paired_distance" %in% names(section))) { return(NULL) } else { figure <- section$paired_distance$figure_1 return(data.frame(sample = basename(file_path_sans_ext(report)), figure$data)) } }
#' Get the figure, x axis, and y axis titles from the paired distance figure #' from a parsed report. Returns null if the reads were unpaired. get_paired_distance_titles <- function(parsed_report) { section <- parsed_report$data$read_quality_control if (!("paired_distance" %in% names(section))) { return(NULL) } else { figure <- section$paired_distance$figure_1 return(c("title" = figure$figure_title, "x" = figure$x_axis_title, "y" = figure$y_axis_title)) } }
#' Re-order the intervals for the paired distances by using the starting value of the interval. order_paired_distances <- function(paired_distance) { distances <- unique(paired_distance$distance) starting <- as.numeric(sapply(strsplit(distances, split = " - "), function(l) l[1])) distances <- distances[sort.int(starting, index.return = TRUE)$ix] paired_distance$distance <- factor(paired_distance$distance, levels = distances) # calculate the breaks used on the x axis for the paired distances breaks <- distances[round(seq(from = 1, to = length(distances), length.out = 15))] return(list(data = paired_distance, breaks = breaks)) }
Using the above functions, the script below parses all the JSON reports found in the "exported reports" folder, to build a read count statistics table (read_count_statistics), and a paired distance histogram.
reports <- list.files("exported reports/", full.names = TRUE) read_count_statistics <- data.frame() paired_distance <- data.frame() titles <- c(NA, NA, NA) for (report in reports) { parsed_report <- fromJSON(report) read_count_statistics <- rbind(read_count_statistics, get_read_count_stats(parsed_report)) paired_distance <- rbind(paired_distance, get_paired_distance(parsed_report)) titles <- get_paired_distance_titles(parsed_report) } paired_distance <- order_paired_distances(paired_distance) ggplot(paired_distance$data, aes(x = distance, y = number_of_reads, fill = sample)) + geom_bar(stat = "identity", position = "dodge") + scale_x_discrete(breaks = paired_distance$breaks, labels = paired_distance$breaks) + labs(title = titles["title"], x = titles["x"], y = titles["y"]) + theme(legend.position = "bottom")
You can try out the JSON export of RNA-Seq reports and the above script with the data included in the tutorial Expression Analysis using RNA-Seq: http://resources.qiagenbioinformatics.com/tutorials/RNASeq-droso.pdf