Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-gregoricchio committed Jan 15, 2021
1 parent b9a3834 commit 9fd3050
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 46 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
@@ -1,16 +1,16 @@
Package: Rseb
Type: Package
Title: A package for NGS data managing and visualization
Version: 0.1.3
Version: 0.1.4
Author: Sebastian Gregoricchio
Maintainer: Sebastian Gregoricchio <sebastian.gregoricchio@gmail.com>
Description: An R-package for daily tasks required to handle biological data as well as avoid re-coding of small functions for quick but necessary data managing.
License: GNU GENERAL PUBLIC LICENSE version 3
Depends:
R (>= 3.2.0), BiocManager, Biostrings, biomaRt, GO.db, rtracklayer, cowplot, data.table, dplyr, ggplot2, ggrepel, matrixStats, plyr, purrr, robustbase, stringr, tidyr, tools
R (>= 3.2.0), BiocManager, Biostrings, biomaRt, GO.db, rtracklayer, cowplot, data.table, dplyr, ggplot2 (>= 3.3.3), ggrepel, matrixStats, plyr, purrr, robustbase, stringr, tidyr, tools, labeling
biocViews:
Imports:
Biostrings, biomaRt, GO.db, rtracklayer, cowplot, data.table, dplyr, ggplot2, ggrepel, matrixStats, plyr, purrr, robustbase, stringr, tidyr, tools
Biostrings, biomaRt, GO.db, rtracklayer, cowplot, data.table, dplyr, ggplot2 (>= 3.3.3), ggrepel, matrixStats, plyr, purrr, robustbase, stringr, tidyr, tools, labeling
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -31,5 +31,7 @@ export(restriction.sites.to.bed)
export(sort.bed)
export(store_packages)
export(substract.bw)
export(uniform.x.axis)
export(uniform.y.axis)
export(update_pkgs)
export(volcano)
9 changes: 8 additions & 1 deletion NEWS.md
Expand Up @@ -16,4 +16,11 @@ First release.
* Optimization of `volcano` function: parameter 'font_size' added;
* Bugs fixed in `computeMatrix.deeptools`
* Added function `build.bed`.
* Added R version dependence (set to R >= 3.2.0) to be compatible with `dplyr`.
* Added R version dependence (set to R >= 3.2.0) to be compatible with `dplyr`.

## v0.1.4 - January 15th 2021
* Bugs fixed in `plot.density.profile`
* Bugs fixed in `build.bed`
* Bugs fixed in `intersect.bedtools`
* Updated dependencies required (`labeling`)
* Added functions `uniform.x.axis` and `uniform.y.axis`
14 changes: 9 additions & 5 deletions R/build.bed.R
Expand Up @@ -141,9 +141,9 @@ build.bed = function(

# TRACK LINE BUILDING
##############################################################################
track.line = NULL
if (!is.null(bed.file.name)) {
if (export.track.line == TRUE) {
track.line = c()
if (!is.null(track.name)) {if (length(track.name) == 1) {track.line = c(track.line, paste('track name="', track.name, '"', sep = ""))}}
if (!is.null(display.mode)) {if (length(display.mode) == 1 & display.mode %in% c("hide", "dense", "full", "pack", "squish")) {track.line = c(track.line, paste('visibility="', display.mode, '"', sep = ""))}}
if (itemRgb.ON == TRUE & !is.null(itemRgb)) {track.line = c(track.line, 'itemRgb="on"')}
Expand Down Expand Up @@ -197,18 +197,22 @@ build.bed = function(

# OUTPUT GENERATION
##############################################################################
# Coerce track.line when empty
if (is.null(track.line)) {track.line = ""}

# Export file if required
if (!is.null(bed.file.name)) {
if (length(bed.file.name) == 1) {
if (export.track.line == T & track.line != "") {
write(x = track.line, file = bed.file.name)
write.table(x = bed, file = bed.file.name,
quote = F, sep = "\t", row.names = F, col.names = F, append = T)
write(x = track.line, file = bed.file.name)
write.table(x = bed, file = bed.file.name,
quote = F, sep = "\t", row.names = F, col.names = F, append = T)
message(paste("Bed file exported as -> ", bed.file.name, sep = ""))
} else {
write.table(x = bed, file = bed.file.name,
quote = F, sep = "\t", row.names = F, col.names = F)
message(paste("Bed file exported as -> ", bed.file.name, sep = ""))
}
message(paste("Bed file exported as -> ", bed.file.name, sep = ""))
}
}

Expand Down
18 changes: 9 additions & 9 deletions R/intersect.bedtools.R
Expand Up @@ -24,8 +24,8 @@
#'
#' @param f Numeric value defining the minimum overlap required as a fraction of A. Default is 1E-9 (i.e. 1bp). By default \code{NULL}.
#' @param F. Numeric value defining the minimum overlap required as a fraction of B. Default is 1E-9 (i.e., 1bp). By default \code{NULL}.
#' @param r Numeric value defining the required reciprocal fraction of overlap for A and B. In other words, if -f is 0.90 and -r is used, this requires that B overlap at least 90\% of A and that A also overlaps at least 90\% of B. By default \code{NULL}.
#' @param e Numeric value defining the minimum fraction to be satisfied for A _OR_ B. In other words, if -e is used with -f 0.90 and -F 0.10 this requires that either 90\% of A is covered OR 10\% of B is covered. Without -e, both fractions would have to be satisfied. By default \code{NULL}.
#' @param r Logic value defining if the fraction (parameter \code{f}) is required to be reciprocal fraction of overlap for A and B. In other words, if -f is 0.90 and -r is used, this requires that B overlap at least 90\% of A and that A also overlaps at least 90\% of B. By default \code{NULL}.
#' @param e Logic value defining if the fraction (parameter \code{f}) must be satisfied for A _OR_ B. In other words, if -e is used with -f 0.90 and -F 0.10 this requires that either 90\% of A is covered OR 10\% of B is covered. Without -e, both fractions would have to be satisfied. By default \code{NULL}.
#' @param s Logic value to define if to force “strandedness”. That is, only report hits in B that overlap A on the same strand. By default, overlaps are reported without respect to strand. By default \code{FALSE}.
#' @param S Logic value to define if to require different strandedness. That is, only report hits in B that overlap A on the _opposite_ strand. By default, overlaps are reported without respect to strand. By default \code{FALSE}.
#'
Expand Down Expand Up @@ -91,8 +91,8 @@ intersect.bedtools =
# options for overlaps restriction
f = NULL,
F. = NULL,
r = NULL,
e = NULL,
r = FALSE,
e = FALSE,
s = FALSE,
S = FALSE,

Expand Down Expand Up @@ -161,18 +161,18 @@ intersect.bedtools =


###### options for overlaps restriction
if (!is.null(f)) {command = paste(command, f)}
if (!is.null(F.)) {command = paste(command, F.)}
if (!is.null(r)) {command = paste(command, r)}
if (!is.null(e)) {command = paste(command, e)}
if (!is.null(f)) {command = paste(command, "-f", f)}
if (!is.null(F.)) {command = paste(command, "-F", F.)}
if (r == T & !is.null(f)) {command = paste(command, "-r")}
if (e == T & !is.null(f)) {command = paste(command, "-e")}
if (s == T) {command = paste(command, "-s")}
if (S == T) {command = paste(command, "-S")}


###### files handling options
if (split == T) {command = paste(command, "-split")}
if (sorted == T) {command = paste(command, "-sorted")}
if (!is.null(g) & length(g) == 1) {command = paste(command, add.quotes(g))}
if (!is.null(g) & length(g) == 1) {command = paste(command, "-g", add.quotes(g))}


###### Add output command
Expand Down
28 changes: 16 additions & 12 deletions R/plot.density.profile.R
Expand Up @@ -3,13 +3,13 @@
#' @description Plots the density profile of NGS data signals, using as input a score matrix computed by deeptools's computeMatrix function or by \link{computeMatrix.deeptools} from this package.
#'
#' @param matrix.file A single string indicating a full path to a matrix.gz file generated by \code{deepTools/computeMatrix} or by \link{computeMatrix.deeptools}, or a list generated by the function \link{read.computeMatrix.file}.
#' @param plot.by.group Logic value to define whether plot by group of regions or by sample. By default \code{TRUE}.
#' @param missing.data.as.zero Logic value to define whether treat missing data as 0. If set as \code{FALSE} missing data will be converted to \code{NA} and will be excluded from the computations of the signal. By default \code{TRUE}.
#' @param plot.by.group Logical value to define whether plot by group of regions or by sample. By default \code{TRUE}.
#' @param missing.data.as.zero Logical value to define whether treat missing data as 0. If set as \code{FALSE} missing data will be converted to \code{NA} and will be excluded from the computations of the signal. By default \code{TRUE}.
#' @param sample.names Samples names could be defined by a string vector. If set as \code{NULL} sample names will be get automatically by the matrix file. By default \code{NULL}. \cr Example: \code{c("sample1", "sample2", "sample3")}
#' @param region.names Region names could be defined by a string vector. If set as \code{NULL} sample names will be get automatically by the matrix file. By default \code{NULL}. \cr Example: \code{c("regionA", "regionB")}
#' @param signal.type String indicating the signal to be computed and plotted. Available parameters are "mean", "median" and "sum". By default "mean".
#' @param error.type String indicating the type of error to be computed and plotted. Available parameters are "sem" and "sd", standard error mean and standard deviation respectively. By default "sem". Parameter considered only when \code{plot.error = TRUE)}.
#' @param plot.error Logic value to define whether to plot the error around the signal. By default \code{TRUE}.
#' @param plot.error Logical value to define whether to plot the error around the signal. By default \code{TRUE}.
#' @param error.transparency Numeric value to define the alpha/transparency of the error. By default 0.125. Parameter considered only when \code{plot.error = TRUE)}.
#' @param title Title of each plot could be defined by a string vector. If set as \code{NULL} titles will be generated automatically. By default \code{NULL}. \cr Example: \code{c("Title1", "Title2")}
#' @param x.lab Single string to define the X-axis label for all the plots. By default \code{NULL}, the label will be defined automatically.
Expand All @@ -21,15 +21,16 @@
#' @param y.identical.auto Logical value to define whether use the same Y-axis range for all the plots automatically depending on the values. Not used when \code{y.lim} is not \code{NULL}. By default \code{TRUE}.
#' @param y.ticks.number Define the number of ticks to display in the Y-axis. By default 5. Active only when \code{y.identical.auto = TRUE}.
#' @param text.size Numeric value to define the size of the text for the labels of all the plots. By default 12.
#' @param plot.vertical.lines Logic value to define whether to plot a dashed gray vertical line in correspondence of the reference points of each plot. By default \code{TRUE}.
#' @param write.reference.points Logic value to define whether to indicate the reference points on each plot. Applied only when \code{x.lim} is \code{NULL}. By default \code{TRUE}.
#' @param plot.vertical.lines Logical value to define whether to plot a dashed gray vertical line in correspondence of the reference points of each plot. By default \code{TRUE}.
#' @param write.reference.points Logical value to define whether to indicate the reference points on each plot. Applied only when \code{x.lim} is \code{NULL}. By default \code{TRUE}.
#' @param colors Vector to define the line and error area colors. If only one value is provided it will applied to all the samples/groups. If the number of values is lower than the the required one, a random set of colors will be generated. All standard R.colors values are accepted. By default \code{c("#00A5CF", "#F8766D", "#AC88FF", "#E08B00", "#00BA38", "#BB9D00", "#FF61C9", "gray30")}.
#' @param n.row.multiplot Numeric value to define the number of rows in the final multiplot.
#' @param export.multiplot Logic value to define whether to export the multiplot generated by the function. By default \code{FALSE}.
#' @param export.multiplot Logical value to define whether to export the multiplot generated by the function. By default \code{FALSE}.
#' @param multiplot.export.file Name of the PDF file of the multiplot to be exported when \code{export.multiplot = T}. \cr By default "/working_directory/multiplot_current.date_current.time.pdf".
#' @param real.width.single.plot Numeric value, in inches, to define the real width of each plot in the multiplot exported, if required. By default 2.5 inches.
#' @param real.width.single.plot Numeric value, in inches, to define the real width of each plot in the multiplot exported, if required. By default 3.5 inches.
#' @param real.height.single.plot Numeric value, in inches, to define the real height of each plot in the multiplot exported, if required. By default 3 inches.
#' @param print.multiplot Logic value to define whether to print the multiplot once created. By default \code{FALSE}.
#' @param by.row Logical value to define whether the plots should be arranged by row. By default \code{TRUE}.
#' @param print.multiplot Logical value to define whether to print the multiplot once created. By default \code{FALSE}.
#'
#' @return The functions returns a list containing:
#' \itemize{
Expand Down Expand Up @@ -67,6 +68,7 @@
# @importFrom purrr reduce
# @importFrom cowplot plot_grid
# @importFrom tools toTitleCase
# @importFrom labeling extended

plot.density.profile = function(
matrix.file,
Expand Down Expand Up @@ -95,7 +97,8 @@ plot.density.profile = function(
export.multiplot = F,
multiplot.export.file = paste(getwd(), "/multiplot.", Sys.Date(), "_", gsub(pattern = ":", replacement = ".", x = format(Sys.time(), "%X")), ".pdf", sep=""),
real.width.single.plot = 2.5, #inch
real.height.single.plot = 3, #inch
real.height.single.plot = 3.5, #inch
by.row = TRUE,
print.multiplot = F) {

##############################################################################
Expand All @@ -113,6 +116,7 @@ plot.density.profile = function(
# require(purrr)
# require(cowplot)
# require(tools)
# require(labeling)

##############################################################################
## Convert and check x.lim and y.lim and title
Expand Down Expand Up @@ -488,8 +492,8 @@ plot.density.profile = function(
reference_point = as.vector(stringr::str_split(string = filter(.data = metadata, parameters == "ref_point")$values, pattern = ",")[[1]])
body = as.numeric(as.vector(stringr::str_split(string = filter(.data = metadata, parameters == "body")$values, pattern = ",")[[1]]))

if (length(reference_point) == 1) {reference_point = rep(reference_point, length(plot.list))}
if (length(body) == 1) {body = rep(body, length(plot.list))}
if (length(reference_point) < length(plot.list)) {reference_point = rep(reference_point[1], length(plot.list))}
if (length(body) < length(plot.list)) {body = rep(body[1], length(plot.list))}

for (i in 1:length(plot.list)) {
labels = ggplot_build(plot.list[[i]])$layout$panel_params[[1]]$x$get_labels()
Expand Down Expand Up @@ -520,7 +524,7 @@ plot.density.profile = function(

##############################################################################
# generate the multiplot
multiplot = cowplot::plot_grid(plotlist = plot.list, nrow = n.row.multiplot)
multiplot = cowplot::plot_grid(plotlist = plot.list, nrow = n.row.multiplot, byrow = by.row)

# Print multiplot if required
if (print.multiplot == T) {print(multiplot)}
Expand Down
89 changes: 89 additions & 0 deletions R/uniform.x.axis.R
@@ -0,0 +1,89 @@
#' @title Plot X-axis uniforming
#'
#' @description Takes a list of ggplot2 plots, compares their X-axis ranges and applies the highest/lowest limits to each plot in order to uniform all the plots. It can be used also to set the ticks step (to just change the breaks set all parameters as \code{FALSE}).
#'
#' @param plot.list A single plot or a list of plots.
#' @param x.min Either a logical value to define whether uniform the lower limit or a numeric value defining the lower limit. By default \code{TRUE}.
#' @param x.max Either a logical value to define whether uniform the upper limit or a numeric value defining the upper limit. By default \code{TRUE}.
#' @param ticks.each Numeric value to define every how much should be placed a tick. By default \code{NULL}, ticks will be placed automatically.
#'
#' @return Returns a plot list (or a single plot when only one input plot is provided) equivalent to the input list provided by the user in which the X-axis of all the plots will be uniformed.
#'
#' @export uniform.x.axis

uniform.x.axis = function(
plot.list,
x.min = TRUE,
x.max = TRUE,
ticks.each = NULL)

{ # BEGIN function -----------------------------------------------------------------------

# Check list length
if (!("list" %in% class(plot.list))) {
if ("ggplot" %in% class(plot.list) | "gg" %in% class(plot.list)) {
plot.list = list(plot.list)
} else {
return(warning("The 'plot.list' parameter must be a ggplot object or a list of ggplot objects."))}
}



# Load required libraries
require(ggplot2)



# Get current X ranges
x.min_list = c()
x.max_list = c()

for (i in 1:length(plot.list)) {
x.min_list = c(x.min_list, ggplot_build(plot.list[[i]])$layout$panel_params[[1]]$x$limits[1])
x.max_list = c(x.max_list, ggplot_build(plot.list[[i]])$layout$panel_params[[1]]$x$limits[2])
}



# Re-define the limits
if (length(x.min) != 1 | length(x.max) != 1) {return(warning("The 'x.min' and 'x.max' parameters must be either a unique logical or numeric value."))}

if (is.na(x.min) | is.numeric(x.min) | is.null(x.min)) {
new_x.min = x.min} else if (x.min == TRUE) {
new_x.min = min(x.min_list)} else {
new_x.min = NA}

if (is.na(x.max) | is.numeric(x.max) | is.null(x.max)) {
new_x.max = x.max} else if (x.max == TRUE) {
new_x.max = max(x.max_list)} else {
new_x.max = NA}



# Re-define breaks
if (!is.null(ticks.each)) {
breaks_list = list()

for (i in 1:length(plot.list)) {
min = ifelse(test = is.na(new_x.min), yes = x.min_list[i], no = new_x.min)
max = ifelse(test = is.na(new_x.max), yes = x.max_list[i], no = new_x.max)
breaks_list[[i]] = seq(min, max, ticks.each)
}
}



# Re-define the X-axis
for (i in 1:length(plot.list)) {
if (is.null(ticks.each)) {
plot.list[[i]] = plot.list[[i]] + scale_x_continuous(limits = c(new_x.min, new_x.max))
} else {
plot.list[[i]] = plot.list[[i]] + scale_x_continuous(breaks = breaks_list[[i]], limits = c(new_x.min, new_x.max))
}
}


# Return the modified plots
if (length(plot.list) == 1) {return(plot.list[[1]])} else {return(plot.list)}

} # END function

0 comments on commit 9fd3050

Please sign in to comment.