Skip to content

Commit a03a819

Browse files
author
Mitjavila Ventura Adria
committed
update functions, add ggplotly, etc
1 parent f0be8f7 commit a03a819

File tree

9 files changed

+343
-444
lines changed

9 files changed

+343
-444
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
Rfunctions
2-
==========
1+
# Rfunctions
32

43
R custom functions that are useful for different omics analyses.
54

65
## `Rfunctions.R`
76

87
Script that loads all the functions by doing source of the other R scripts present in this repository.
98

10-
It use the R package `here` (if you don't have it, it will probably be installed).
9+
It use the R package `here` (if you don't have it).
1110

1211
Paths may have to be changed depending on the current directory where `here` works.
1312

Rfunctions.R

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ source(here("R/pcaplot2.R"))
1717
# A function to draw volcano plots
1818
source(here("R/volcanoplot2.R"))
1919

20-
# ----- pieAnno2 ----- #
21-
# pieAnno2 -> A function to draw pie charts with the peaks divided by promoter and distal (all non promoter).
22-
# filterAnno2 -> A function that takes the output of Annotate peak and changes the features to promoter or distal.
23-
source(here("R/pieAnno2.R"))
2420

2521
# ----- pieAnno3 ----- #
26-
# pieAnno3 -> A function to draw pie charts with the peaks divided by promoter, gene body (UTRs, introns, exons) and distal (distal intergeninc, downstream).
27-
# filterAnno3 -> A function that takes the output of Annotate peak and changes the features to promoter or distal.
28-
source(here("R/pieAnno3.R"))
22+
# pieAnno -> A function to draw pie charts with the peaks divided by promoter, gene body (UTRs, introns, exons) and distal (distal intergeninc, downstream).
23+
# filterAnno -> A function that takes the output of Annotate peak and changes the features to promoter or distal.
24+
source(here("R/pieAnno.R"))
2925

3026
# ----- barAnno ----- #
3127
# barAnno -> a function to draw barplots from a list of annotatePeak objects. Can divide peaks in promoter/distal or promoter/gene body/distal

barAnno.R

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
##################
2-
### BAR ANNO 3 ###
3-
##################
41

5-
#' @title barAnno3
2+
### barAnno()
3+
### ===============================================================================================
4+
5+
#' @title barAnno
66
#' @author amitjavilaventura
77
#'
8-
#' @usage barAnno(anno_list, names, names_order, protein = NULL, main = NULL, subtitle = NULL, ylab = "Proportion", xlab = NULL,palette = "Set1", legend_position = "right", is_anno = F, is_chip = F, anno_num = 3){
8+
#' @usage barAnno(anno_list, names, names_order = NULL, protein = NULL, main = NULL, subtitle = NULL, ylab = "Proportion", xlab = NULL,palette = "Set1", legend_position = "right", is_anno = F, facet = F, anno_num = 3, plotlify = F){
99
#'
1010
#' Function for ChIP-seq and ATAC-seq.
1111
#' It must be used after the function annotatePeak() from the R package ChIPseeker. @seealso \code{\link{annotatePeak}}
1212
#'
1313
#' It takes a list of annotation objects that come as output of annotatePeak() and changes the features to "Promoter", "Distal" and "Gene body" (or to "Promoter" and "Distal"). Finally it plots a bargraph with the distribution of all the proportions
1414
#' As a ggplot2-based function, it allows to add more layers to format the plot.
1515
#'
16-
#' @param anno_list List of annotation objects that come from annotatePeak().
16+
#' @param anno_list List of annotation objects that come from annotatePeak(). If anno_df is specified, anno_list must be null, otherwise must be specified.
1717
#' @param names Charachter vector of the same length as 'anno_list'. Names that will be given to each of the objects in anno_list. Not that will be the names plotted in the bargraph
18-
#' @param names_order Character vector with the same entries as 'names' with the order wanted to plot the data.
19-
#' @param protein Character vector of the same length as 'anno_list' with the protein chipped in each dataframe of anno_list. This names will be passed through `facet_wrap()`. Only works if is_chip is set to TRUE. Default: NULL.
18+
#' @param names_order Character vector with the same entries as 'names' with the order wanted to plot the data (optional).
19+
#' @param protein Character vector of the same length as 'anno_list' with the protein chipped in each dataframe of anno_list. This names will be passed through `facet_wrap()`. Only works if facet is set to TRUE. Default: NULL.
2020
#' @param main Character of lenght 1. Title of the plot. Default: NULL.
2121
#' @param subtitle Character of lenght 1. Subtitle of the plot. Default: NULL.
2222
#' @param ylab Character of lenght 1. Title of the Y axis. Default: NULL.
2323
#' @param xlab Character of lenght 1. Title of the X axis. Default: NULL
2424
#' @param palette Character of lenght 1. Color palette used to color the bars through the function `scale_fill_brewer()`. Default: "Set1".
2525
#' @param legend_position Character of lenght 1. Position of the legend. One of c("none", "bottom", "right", "left," "top"). Default: "right"
2626
#' @param is_anno Logical. If TRUE, takes the 'anno_list' as a list of annotation objects from annotatePeak. If FALSE, takes 'anno_list' as a list of `annotatePeak()` output that have already been formatted to data.frame (i.e. read from file)... this is done because of the behaviour of our snakemake pipeline.
27-
#' @param is_chip Logical. If TRUE, it means that the input data comes from a ChiP experiment, which will separate the plot by proteins. It can be set to FALSE even if data comes from ChIPseq, but remember that if you want to separate proteins, the names of each protein group must be different. It can be also set to TRUE if data comes from ATACseq, then in 'protein' you can write the desired grouping variable. Default: FALSE.
27+
#' @param facet Logical. If TRUE, the plot will be separated by the groups set in proteins. It can be set to FALSE even if data comes from ChIPseq, but remember that if you want to separate proteins, the names of each protein group must be different. It can be also set to TRUE if data comes from ATACseq, then in 'protein' you can write the desired grouping variable. Default: FALSE.
2828
#' @param anno_num Integer number. Number of annotations to plot, either 2 (Promoter/Distal) or 3 (Promoter/Gene body/Distal). Default: 3.
2929
#' @param position_fill Logical. If TRUE (default), it the plotted bars will represent proportion of peaks in each feature. If FALSE, the bars will have the height of the total number of peaks with the correspondent feature color.
30+
#' @param plotly Logical. If TRUE, the ggplot object will be passed to 'ggplotlify()' to transform it to an interactive plotly graph.
3031

31-
barAnno <- function(anno_list, names, names_order,
32+
barAnno <- function(anno_list,
33+
names, names_order = NULL,
3234
protein = NULL,
3335
main = NULL,
3436
subtitle = NULL,
3537
ylab = NULL, xlab = NULL,
3638
palette = "Set1",
3739
legend_position = "right",
38-
is_anno = F, is_chip = F, anno_num = 3,
39-
position_fill = T){
40+
is_anno = F, facet = F, anno_num = 3,
41+
position_fill = T, plotly = F){
4042

4143
# Load packages
4244
library(dplyr)
@@ -45,50 +47,63 @@ barAnno <- function(anno_list, names, names_order,
4547
library(ggpubr)
4648
library(magrittr)
4749

48-
# Create list for annotations from the list of annotatePeak object provided as input
49-
anno <- list()
50-
51-
# Format promoter annnotation names to take parentheses out for each annotatePeak object in the list
52-
if(is_anno){
53-
for(i in 1:length(anno_list)){
54-
anno[[i]] <- sub(" \\(.*\\)", "", anno_list[[i]]@anno$annotation)
55-
}
50+
if(facet & is.null(protein)){
51+
message1 <- "If 'facet' is TRUE, the argument 'protein' must be specified."
52+
message2 <- "The argument 'protein' must be a character vector of the same length as 'names', such as the proteins chipped in each sample or other grouping variables passed to 'facet_wrap()'"
53+
stop(paste("Error.", message1, message2, sep = " "))
5654
}
5755
else{
58-
for(i in 1:length(anno_list)){
59-
anno[[i]] <- sub(" \\(.*\\)", "", anno_list[[i]]$annotation)
60-
}
61-
}
56+
if(!is.null(anno_list) & is.null(anno_df)){
6257

63-
# Set the names of each annotatePeak object in the list with the vector names
64-
anno_df <- set_names(x = anno, nm = names) %>%
58+
# Create list for annotations from the list of annotatePeak object provided as input
59+
anno <- list()
6560

66-
# Convert the annotatePeak objects to dataframe
67-
purrr::map(~as.data.frame(x = .x)) %>%
61+
# Format promoter annnotation names to take parentheses out for each annotatePeak object in the list
62+
if(is_anno){
63+
for(i in 1:length(anno_list)){
64+
anno[[i]] <- sub(" \\(.*\\)", "", anno_list[[i]]@anno$annotation)
65+
}
66+
}
67+
else{
68+
for(i in 1:length(anno_list)){
69+
anno[[i]] <- sub(" \\(.*\\)", "", anno_list[[i]]$annotation)
70+
}
71+
}
6872

69-
# Write an extra column to each dataframe with the name of the dataframe (provided in names)
70-
purrr::imap(~mutate(.data = .x, sample = as.character(.y))) %>%
7173

72-
# Set column names
73-
purrr::map(~set_colnames(x = .x, c(c("annotation", "sample")))) %>%
74-
purrr::map(~dplyr::mutate(.data = .x, annotation = as.character(annotation)))
74+
# Set the names of each annotatePeak object in the list with the vector names
75+
anno_df <- set_names(x = anno, nm = names) %>%
7576

76-
if(is_chip){
77-
anno_df <- set_names(x = anno_df, nm = protein) %>%
77+
# Convert the annotatePeak objects to dataframe
78+
purrr::map(~as.data.frame(x = .x)) %>%
7879

79-
# Write an extra column to each dataframe with the name of the dataframe (provided in names)
80-
purrr::imap(~mutate(.data = .x, protein = as.character(.y))) %>%
80+
# Write an extra column to each dataframe with the name of the dataframe (provided in names)
81+
purrr::imap(~mutate(.data = .x, sample = as.character(.y))) %>%
8182

82-
# Set column names
83-
purrr::map(~set_colnames(x = .x, c(c("annotation", "sample", "protein"))))
84-
}
83+
# Set column names
84+
purrr::map(~set_colnames(x = .x, c(c("annotation", "sample")))) %>%
85+
purrr::map(~dplyr::mutate(.data = .x, annotation = as.character(annotation)))
86+
87+
if(is_chip){
88+
anno_df <- set_names(x = anno_df, nm = protein) %>%
89+
90+
# Write an extra column to each dataframe with the name of the dataframe (provided in names)
91+
purrr::imap(~mutate(.data = .x, protein = as.character(.y))) %>%
8592

86-
# Bind dataframes by rows
87-
anno_df <- anno_df %>% bind_rows() %>%
93+
# Set column names
94+
purrr::map(~set_colnames(x = .x, c(c("annotation", "sample", "protein"))))
95+
}
8896

89-
# Mutate the names in order to
90-
dplyr::mutate(sample = factor(sample, levels = names_order))
97+
# Bind dataframes by rows
98+
anno_df <- anno_df %>% bind_rows() %>%
99+
100+
# Mutate the names in order to
101+
dplyr::mutate(sample = factor(sample, levels = names_order))
102+
}
103+
# if input is dataframe with different conditions
91104

105+
106+
}
92107
# Rewrite annotation as distal or gene body depending on the feature
93108
if(anno_num == 2){
94109
anno_df <- anno_df %>%
@@ -110,39 +125,35 @@ barAnno <- function(anno_list, names, names_order,
110125
"5' UTR" = "Gene body",
111126
"3' UTR" = "Gene body"))
112127
}
128+
g <- ggplot(anno_df, aes(sample, fill = annotation))
113129

114130
# Create gglpot2-based barplot
115131
if(position_fill){
116-
if(is_chip){
117-
g <- ggplot(anno_df, aes(sample, fill = annotation)) + geom_bar(position = "fill") + facet_wrap(~protein)
118-
}
119-
else{
120-
g <- ggplot(anno_df, aes(sample, fill = annotation)) + geom_bar(position = "fill")
121-
}
132+
if(facet){ g <- g + geom_bar(position = "fill", color = "Black") + facet_wrap(~protein) }
133+
else{ g <- g + geom_bar(position = "fill", color = "Black") }
122134
}
123-
124135
else{
125-
if(is_chip){
126-
g <- ggplot(anno_df, aes(sample, fill = annotation)) + geom_bar() + facet_wrap(~protein)
127-
}
128-
else{
129-
g <- ggplot(anno_df, aes(sample, fill = annotation)) + geom_bar()
130-
}
136+
if(facet){ g <- g + geom_bar(color = "Black") + facet_wrap(~protein) }
137+
else{ g <- g + geom_bar(color = "Black") }
131138
}
132-
# Write plot title, subtitle and axis labels
139+
140+
# Write plot title, subtitle and axis labels
133141
g <- g + ggtitle(main, subtitle = subtitle) + ylab(ylab) + xlab(xlab) +
134142

135143
# Format colors with ggplot2 function scale_fill_brewer
136144
scale_fill_brewer(palette = palette) +
137145

138146
# Basic formatting
139-
theme_pubr() +
147+
theme_pubr(legend = legend_position, x.text.angle = 20) +
148+
theme(legend.title = element_blank())
140149

141-
theme(axis.text.x = element_text(angle = 20, hjust = 1, vjust = 1),
142-
legend.title = element_blank(),
143-
legend.position = legend_position)
150+
# Plotlify
151+
if(plotly == T){
152+
require(plotly)
153+
g <- ggplotly(g)
154+
}
144155

145-
# Draw plot
156+
# Return plot
146157
return(g)
147158
}
148159

chromHMM_functions.R

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
# Several functions to help in the visualization of chromHMM outputs
66

7-
# -------------------------------------------------------------------------------------------------
8-
# chromHMM_emission2heatmap
9-
# -------------------------------------------------------------------------------------------------
7+
# chromHMM_emission2heatmap ------
8+
# ----------------------------------------------------------------------------------------------- #
109

1110
chromHMM_emission2heatmap <- function(df = NULL, file = NULL, df_melt = NULL, features = NULL, states = NULL,
1211
title = "", subtitle = "", xlab = "Features", color = "Tomato",
13-
label_size = 2){
12+
label_size = 2, reverse_y = T, plotly = F){
1413

1514
# PACKAGES
1615
require(dplyr)
@@ -44,23 +43,31 @@ chromHMM_emission2heatmap <- function(df = NULL, file = NULL, df_melt = NULL, fe
4443
ggtitle(title, subtitle) + ylab("State") + xlab(xlab) +
4544
theme_pubr() + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1),
4645
axis.title = element_text(face = "bold", size = 12),
47-
axis.text = element_text(size = 10))
46+
axis.text = element_text(size = 10),
47+
plot.title = element_text(hjust=.5), plot.subtitle = element_text(hjust=.5))
48+
49+
# Reverse Y axis
50+
if(reverse_y){ g <- g + scale_y_reverse() }
4851

49-
# Return plot
50-
return(g)
5152

53+
# Plotlify
54+
if(plotly){
55+
require(plotly)
56+
g <- ggplotly(g)
57+
}
5258

59+
# Return plot
60+
return(g)
5361
}
5462

5563

5664

57-
# -------------------------------------------------------------------------------------------------
58-
# chromHMM_enrich2heatmap
59-
# -------------------------------------------------------------------------------------------------
65+
# chromHMM_enrich2heatmap -------
66+
# ----------------------------------------------------------------------------------------------- #
6067

6168
chromHMM_enrich2heatmap <- function(df = NULL, file = NULL, df_melt = NULL, regions = NULL, states = NULL,
6269
title = "", subtitle = "", color = "Cornflowerblue", scale_color = "scale",
63-
legend = F, label_size = 2){
70+
legend = F, label_size = 2, reverse_y = T, plotly = F){
6471

6572
# PACKAGES
6673
require(dplyr)
@@ -103,17 +110,26 @@ chromHMM_enrich2heatmap <- function(df = NULL, file = NULL, df_melt = NULL, regi
103110
axis.title = element_text(face = "bold", size = 12),
104111
axis.text = element_text(size = 10))
105112

113+
# Reverse Y axis
114+
if(reverse_y){ g <- g + scale_y_reverse() }
115+
116+
# Plotlify
117+
if(plotly){
118+
require(plotly)
119+
g <- ggplotly(g)
120+
}
121+
106122
# Return plot
107123
return(g)
108124
}
109125

110126

111-
# -------------------------------------------------------------------------------------------------
112-
# chromHMM_neighbor2heatmap
113-
# -------------------------------------------------------------------------------------------------
127+
# chromHMM_neighbor2heatmap ------
128+
# ----------------------------------------------------------------------------------------------- #
114129
chromHMM_neighbor2heatmap <- function(df = NULL, file = NULL, df_melt = NULL, states = NULL,
115130
title = "", subtitle = "", xlab = "Distance to TSS",
116-
color = "Cornflowerblue", legend = F, label_size = 2){
131+
color = "Cornflowerblue", legend = F, label_size = 2,
132+
reverse_y = T, plotly = F){
117133

118134
# PACKAGES
119135
require(tidyverse)
@@ -155,17 +171,26 @@ chromHMM_neighbor2heatmap <- function(df = NULL, file = NULL, df_melt = NULL, st
155171
theme_pubr() + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1),
156172
axis.title = element_text(face = "bold", size = 12),
157173
axis.text = element_text(size = 10))
174+
# Reverse Y axis
175+
if(reverse_y){ g <- g + scale_y_reverse() }
176+
177+
# Plotlify
178+
if(plotly){
179+
require(plotly)
180+
g <- ggplotly(g)
181+
}
182+
158183
# Return plot
159184
return(g)
160185
}
161186

162187

163-
# -------------------------------------------------------------------------------------------------
164-
# chromHMM_states2barplot
165-
# -------------------------------------------------------------------------------------------------
188+
# chromHMM_states2barplot ------
189+
# ----------------------------------------------------------------------------------------------- #
166190

167191
chromHMM_states2barplot <- function(df = NULL, file = NULL, df_processed = NULL, states = NULL,
168-
title = "", subtitle = "", color = c("Cornflowerblue", "Darkblue"), scale_color = ""){
192+
title = "", subtitle = "", color = c("Cornflowerblue", "Darkblue"), scale_color = "",
193+
plotly = F){
169194

170195
# PACKAGES
171196
require(dplyr)
@@ -207,7 +232,7 @@ chromHMM_states2barplot <- function(df = NULL, file = NULL, df_processed = NULL
207232
}
208233
else{
209234
g <- ggplot(df2, aes(state, bases, fill = state)) + geom_col(show.legend = F)
210-
}
235+
}
211236

212237

213238
g <- g + coord_flip() + scale_y_continuous(expand = expansion(mult = c(0.01, 0.1))) +
@@ -216,6 +241,11 @@ chromHMM_states2barplot <- function(df = NULL, file = NULL, df_processed = NULL
216241
axis.title = element_text(face = "bold", size = 14),
217242
legend.position = "right")
218243

244+
# Plotlify
245+
if(plotly){
246+
require(plotly)
247+
g <- ggplotly(g)
248+
}
219249

220250
# Return plot
221251
return(g)

0 commit comments

Comments
 (0)