JupyterNotebook R startup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cat('\n#########################################\n')
cat(paste0('#### Last run time: ', format(Sys.time(), '%Y-%m-%d %H:%M'), ' ####'))
cat('\n#########################################\n')
cat('\n#############################################################\n')
cat(paste0(R.home(), '\n'))
print(R.version)
cat('#############################################################\n\n')
cat('#############################################################\n')
sessionInfo()
cat('#############################################################\n\n')

JupyterNotebook R snippet for inline figure displaying size

1
2
3
4
5
6
7
base_len <- 6
#options(repr.plot.width = base_len, repr.plot.height = base_len)
#options(repr.function.highlight = TRUE)
mySetFigSize <- function(width = 1, height = 1) {
    options(repr.plot.width = base_len * width, repr.plot.height = base_len * height)
}
mySetFigSize(1, 1)

Setup s project directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
prj_home <- '~/Projects'
prj_dir <- 'ProjectName'
prj_path <- file.path(prj_home, prj_dir)
setwd(file.path(prj_path))

dirs <- list(
  dir_in  = file.path(prj_path, 'raw_data'),
  dir_out = file.path(prj_path, 'res_data'),
  dir_fig = file.path(prj_path, 'output_plots')
)
for (d in dirs) {
  dir.create(d, recursive = TRUE, showWarnings = FALSE)
}

Batch loading packages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pkgs <- c(
    'ggplot2', 'RColorBrewer',
    'dplyr',
    'repr', 'highr'
)
 
invisible(lapply(pkgs,  \(.) { 
    suppressPackageStartupMessages(library(., character.only = TRUE)))
  }
)

Unload a package

1
detach("package:ggplot2", unload = TRUE)

A lightweight and sane ggplot2 theme

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
theme_barebone <- function(bssz = 18){
    base_size = bssz
    ggthemes::theme_foundation(base_size = bssz) +
    theme(
        plot.background = element_blank(),
        panel.background = element_blank(), 
        panel.border = element_blank(),
        panel.grid = element_blank(), 
        axis.line.x = element_line(colour = 'black', linewidth = 0.5, linetype = 'solid'), 
        axis.line.y = element_line(colour = 'black', linewidth = 0.5, linetype = 'solid'), 
        axis.ticks = element_line(colour = 'black', linewidth = 0.5, linetype = 'solid'),
        axis.ticks.length = unit(2, 'mm'),
        axis.text = element_text(colour = 'black', size = ceiling(base_size * 0.66), face = 'plain'), 
        axis.title = element_text(colour = 'black', size = ceiling(base_size * 0.75), face = 'plain'),
        strip.background = element_blank(), 
        strip.text = element_text(),
        strip.text.x = element_text(colour = 'black', vjust = 0.5, face = 'plain'), 
        strip.text.y = element_text(colour = 'black', angle = -90, face = 'plain'),
        legend.text = element_text(colour = 'black', size = ceiling(base_size * 0.66), face = 'plain'), 
        legend.title = element_text(colour = 'black', size = ceiling(base_size * .8), face = 'plain'),
        legend.position = 'right', 
        legend.key = element_blank(),
        legend.background = element_blank(), 
        legend.box.background = element_blank(),
        #plot.margin = unit(c(1, 1, 1, 0), 'mm'),
        plot.margin = margin(t = 5, r = 2, b = 2, l = 2), 
        plot.title = element_text(colour = 'black', size = ceiling(base_size), face = 'plain'),
        plot.subtitle = element_text(colour = 'black', size = ceiling(base_size * .8))
    )   
}

Pre-process data for heatmap visualization:

ChIP-seq data formats: BigWig, Wig and Bed files

R/Bioconductor: Genome and Annoation

Add DE column and print DEG numbers for a data.frame

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
addUpDownReg <- function(deg = deg, 
                         thres_logfc = 2, thres_p = 0.05,
                         pval_col = 'p_val_adj', logfc_col = 'avg_log2FC') {
    deg$DE = dplyr::case_when(
        deg[[logfc_col]] > thres_logfc & deg[[pval_col]] < thres_p ~ 'UpReg',
        deg[[logfc_col]] < -thres_logfc & deg[[pval_col]] < thres_p ~ 'DownReg',
        TRUE ~ 'NonSig'
    ) |> factor(levels = c('UpReg', 'DownReg', 'NonSig'))
    print(table(deg$DE))
    return(deg)
}