Skip to content

Commit

Permalink
feat: formatted all R code with 'styler'
Browse files Browse the repository at this point in the history
  • Loading branch information
m-jahn committed Apr 29, 2024
1 parent a156490 commit c2a2001
Show file tree
Hide file tree
Showing 27 changed files with 476 additions and 340 deletions.
1 change: 0 additions & 1 deletion R/ConsensusPeak.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
GetConsensusPeak <- function(peak.file, peak.folder = NULL, mspc.path = NULL, rep.type = c("bio", "tec"), stringency.threshold = 1e-8,
weak.threshold = 1e-4, gamma = 1e-8, alpha = 0.05, min.overlap.num = 1,
multiple.intersections = c("Lowest", "Highest"), parallelism.degree = 1) {

# check parameters
rep.type <- match.arg(arg = rep.type)
multiple.intersections <- match.arg(arg = multiple.intersections)
Expand Down
56 changes: 27 additions & 29 deletions R/LoadTrack.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,16 @@
#' meta.info = sample.meta
#' )
LoadTrackFile <- function(
track.file, track.folder = NULL,
format = c("bam", "wig", "bw", "bedgraph", "txt"),
region = NULL, extend = 2000,
gtf.gr = NULL, gene.name = "HNRNPC",
gene.name.type = c("gene_name", "gene_id"),
meta.info = NULL, meta.file = "",
bamcoverage.path = NULL,
norm.method = c("RPKM", "CPM", "BPM", "RPGC", "None"),
single.nuc = FALSE, single.nuc.region = NULL,
bin.size = 10, bc.extra.para = NULL, n.cores = 1
) {
track.file, track.folder = NULL,
format = c("bam", "wig", "bw", "bedgraph", "txt"),
region = NULL, extend = 2000,
gtf.gr = NULL, gene.name = "HNRNPC",
gene.name.type = c("gene_name", "gene_id"),
meta.info = NULL, meta.file = "",
bamcoverage.path = NULL,
norm.method = c("RPKM", "CPM", "BPM", "RPGC", "None"),
single.nuc = FALSE, single.nuc.region = NULL,
bin.size = 10, bc.extra.para = NULL, n.cores = 1) {
# check parameters
format <- match.arg(arg = format)
gene.name.type <- match.arg(arg = gene.name.type)
Expand All @@ -83,8 +82,8 @@ LoadTrackFile <- function(
if (format == "bam") {
seqnames <- Rsamtools::scanBamHeader(track.file[1]) %>%
lapply(function(x) x$targets) %>%
unname %>%
unlist
unname() %>%
unlist()
gr <- GenomicRanges::GRanges(
seqnames = names(seqnames[1]),
IRanges(start = 1, end = min(100000, seqnames[1]))
Expand Down Expand Up @@ -128,20 +127,20 @@ LoadTrackFile <- function(
BiocParallel::bplapply(track.file, BPPARAM = BiocParallel::MulticoreParam(), FUN = index_bam)
}
if (single.nuc) {
if (is.null(n.cores) || n.cores == 1) {
track.list <- lapply(
track.file,
single_nuc_cov,
single.nuc.region
)
} else {
track.list <- BiocParallel::bplapply(
track.file,
BPPARAM = BiocParallel::MulticoreParam(),
FUN = single_nuc_cov,
single.nuc.region
)
}
if (is.null(n.cores) || n.cores == 1) {
track.list <- lapply(
track.file,
single_nuc_cov,
single.nuc.region
)
} else {
track.list <- BiocParallel::bplapply(
track.file,
BPPARAM = BiocParallel::MulticoreParam(),
FUN = single_nuc_cov,
single.nuc.region
)
}
} else {
if (norm.method == "None") {
message("Calculating coverage with GenomicAlignments when 'norm.method = None'")
Expand Down Expand Up @@ -310,8 +309,7 @@ single_nuc_cov <- function(x, single.nuc.region) {
}

bam_coverage <- function(
x, bamcoverage.path, bin.size, norm.method, bc.extra.para, gr
) {
x, bamcoverage.path, bin.size, norm.method, bc.extra.para, gr) {
# bigwig file
out.bw.file <- tempfile(fileext = c(".bw"))
# prepare bamCoverage cmd
Expand Down
74 changes: 44 additions & 30 deletions R/geom_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@
#' single.nuc = TRUE,
#' rect.color = "white"
#' ) +
#' geom_base(bam.file = bam.file,
#' bs.fa.seq = BSgenome.Hsapiens.UCSC.hg19)
#' geom_base(
#' bam.file = bam.file,
#' bs.fa.seq = BSgenome.Hsapiens.UCSC.hg19
#' )
#'
geom_base <- function(bam.file, fa.file = NULL, bs.fa.seq = NULL, chr.split = "[[:space:]]",
nuc.offset = -0.1, nuc.size = 4, nuc.padding = 0.05, nuc.padding.r = 0,
Expand All @@ -93,16 +95,17 @@ geom_base <- function(bam.file, fa.file = NULL, bs.fa.seq = NULL, chr.split = "[
"*" = "#FFC0CB"
), aa.border.color = "white", aa.size = 4, aa.margin = 2, aa.height = 0.4,
plot.space = 2.5, plot.height = 0.5) {
structure(list(
bam.file = bam.file, fa.file = fa.file, bs.fa.seq = bs.fa.seq, chr.split = chr.split,
nuc.offset = nuc.offset, nuc.size = nuc.size, nuc.padding = nuc.padding, nuc.padding.r = nuc.padding.r,
nuc.color = nuc.color, guide.line = guide.line, guide.line.color = guide.line.color, guide.line.type = guide.line.type,
mark.type = mark.type, star.size = star.size,
show.aa = show.aa, sens = sens, numcode = numcode, NAstring = NAstring, ambiguous = ambiguous,
aa.color = aa.color, aa.border.color = aa.border.color, aa.size = aa.size, aa.margin = aa.margin, aa.height = aa.height,
plot.space = plot.space, plot.height = plot.height
),
class = "base"
structure(
list(
bam.file = bam.file, fa.file = fa.file, bs.fa.seq = bs.fa.seq, chr.split = chr.split,
nuc.offset = nuc.offset, nuc.size = nuc.size, nuc.padding = nuc.padding, nuc.padding.r = nuc.padding.r,
nuc.color = nuc.color, guide.line = guide.line, guide.line.color = guide.line.color, guide.line.type = guide.line.type,
mark.type = mark.type, star.size = star.size,
show.aa = show.aa, sens = sens, numcode = numcode, NAstring = NAstring, ambiguous = ambiguous,
aa.color = aa.color, aa.border.color = aa.border.color, aa.size = aa.size, aa.margin = aa.margin, aa.height = aa.height,
plot.space = plot.space, plot.height = plot.height
),
class = "base"
)
}

Expand All @@ -122,8 +125,10 @@ ggplot_add.base <- function(object, plot, object_name) {
# plot.region.end <- plot.data[nrow(plot.data), "start"]
plot.region.end <- max(plot.data[, "start"])
region <-
GenomicRanges::GRanges(plot.chr,
IRanges::IRanges(plot.region.start, plot.region.end))
GenomicRanges::GRanges(
plot.chr,
IRanges::IRanges(plot.region.start, plot.region.end)
)

# get parameters
bam.file <- object$bam.file
Expand Down Expand Up @@ -155,9 +160,11 @@ ggplot_add.base <- function(object, plot, object_name) {

# get position AGCT frequency
pos.nuc.freq <-
GenomicAlignments::alphabetFrequencyFromBam(bam.file,
param = region,
baseOnly = TRUE)
GenomicAlignments::alphabetFrequencyFromBam(
bam.file,
param = region,
baseOnly = TRUE
)
# filter out others
pos.nuc.freq <-
pos.nuc.freq[, c("A", "G", "C", "T")] %>% as.data.frame()
Expand Down Expand Up @@ -202,15 +209,15 @@ ggplot_add.base <- function(object, plot, object_name) {
# get position with alt
alt.pos <- pos.nuc.freq.long %>%
dplyr::filter(.data$Ref == .data$Base &
.data$Total != .data$Freq) %>%
.data$Total != .data$Freq) %>%
dplyr::pull(.data$Pos) %>%
unique()
alt.pos.nuc.freq.long <-
pos.nuc.freq.long %>% dplyr::filter(.data$Pos %in% c(alt.pos))
# get position without alt
ref.pos <- pos.nuc.freq.long %>%
dplyr::filter(.data$Ref == .data$Base &
.data$Total == .data$Freq) %>%
.data$Total == .data$Freq) %>%
dplyr::pull(.data$Pos) %>%
unique()
ref.pos.nuc.freq.long <-
Expand Down Expand Up @@ -336,9 +343,11 @@ ggplot_add.base <- function(object, plot, object_name) {
label.r = unit(nuc.padding.r, "lines")
) +
labs(y = "Base") +
geom_hline(yintercept = guide.line,
color = guide.line.color,
linetype = guide.line.type)
geom_hline(
yintercept = guide.line,
color = guide.line.color,
linetype = guide.line.type
)

# make final AA plot
options(digits = nchar(max(list_translated[[1]]$Pos)) + 1)
Expand All @@ -358,17 +367,20 @@ ggplot_add.base <- function(object, plot, object_name) {
),
color = aa.border.color
) +
geom_text(data = list_translated[[reading_frame]],
aes_string(x = "Pos", y = "y", label = "anno"))
geom_text(
data = list_translated[[reading_frame]],
aes_string(x = "Pos", y = "y", label = "anno")
)
}
aa_plot <- aa_plot +
labs(y = "AA") +
theme_aa(margin.len = aa.margin, fill.color = aa.color)
final.plot <-
patchwork::wrap_plots(base.plot,
aa_plot,
ncol = 1,
heights = c(1, aa.height))
aa_plot,
ncol = 1,
heights = c(1, aa.height)
)
} else {
# create plot without amino acid
final.plot <- base.plot +
Expand All @@ -387,9 +399,11 @@ ggplot_add.base <- function(object, plot, object_name) {
label.r = unit(nuc.padding.r, "lines")
) +
labs(y = "Base") +
geom_hline(yintercept = guide.line,
color = guide.line.color,
linetype = guide.line.type)
geom_hline(
yintercept = guide.line,
color = guide.line.color,
linetype = guide.line.type
)
}

# assemble plot
Expand Down
13 changes: 7 additions & 6 deletions R/geom_cnv.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
geom_cnv <- function(cnv.df, bin.col = 3, cn.col = 4, ref.cn = 2,
bin.point.color = "grey", bin.point.alpha = 0.6, cn.line.color = "red",
ref.line.color = "black", plot.space = 0.1, plot.height = 0.2) {
structure(list(
cnv.df = cnv.df, bin.col = bin.col, cn.col = cn.col, ref.cn = ref.cn,
bin.point.color = bin.point.color, bin.point.alpha = bin.point.alpha, cn.line.color = cn.line.color,
ref.line.color = ref.line.color, plot.space = plot.space, plot.height = plot.height
),
class = "cnv"
structure(
list(
cnv.df = cnv.df, bin.col = bin.col, cn.col = cn.col, ref.cn = ref.cn,
bin.point.color = bin.point.color, bin.point.alpha = bin.point.alpha, cn.line.color = cn.line.color,
ref.line.color = ref.line.color, plot.space = plot.space, plot.height = plot.height
),
class = "cnv"
)
}

Expand Down
17 changes: 10 additions & 7 deletions R/geom_coverage.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@
#' geom_coverage(
#' data = track.df, facet.key = "Type",
#' mark.region = data.frame(
#' start = c(21678900,21732001,21737590),
#' end = c(21679900,21732400,21737650),
#' label=c("M1", "M2", "M3")),
#' start = c(21678900, 21732001, 21737590),
#' end = c(21679900, 21732400, 21737650),
#' label = c("M1", "M2", "M3")
#' ),
#' mark.color = grey(0.4)
#' )
#'
Expand All @@ -112,7 +113,8 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
if (!is.null(color)) {
testcolors <- sapply(color, function(x) {
tryCatch(is.matrix(col2rgb(x)),
error = function(e) FALSE)
error = function(e) FALSE
)
})
if (length(color) < length(unique(data[, group.key]))) {
warning("Fewer colors provided than there are groups in ", group.key, " variable, falling back to default colors")
Expand Down Expand Up @@ -259,9 +261,10 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,

if (range.position == "in") {
data.range <- data.range %>%
dplyr::summarise(.groups = "drop_last",
min_score = pretty(.data[[ymax.str]])[1],
max_score = tail(pretty(.data[[ymax.str]]), 1)
dplyr::summarise(
.groups = "drop_last",
min_score = pretty(.data[[ymax.str]])[1],
max_score = tail(pretty(.data[[ymax.str]]), 1)
)
data.range$label <- paste0("[", data.range$min_score, ", ", data.range$max_score, "]")
region.range <- geom_text(
Expand Down
11 changes: 6 additions & 5 deletions R/geom_feature.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
#' # )
geom_feature <- function(feature.file = NULL, feature.df = NULL, feature.color = "black", feature.size = 5,
plot.space = 0.1, plot.height = 0.1) {
structure(list(
feature.file = feature.file, feature.df = feature.df, feature.color = feature.color, feature.size = feature.size,
plot.space = plot.space, plot.height = plot.height
),
class = "feature"
structure(
list(
feature.file = feature.file, feature.df = feature.df, feature.color = feature.color, feature.size = feature.size,
plot.space = plot.space, plot.height = plot.height
),
class = "feature"
)
}

Expand Down
13 changes: 7 additions & 6 deletions R/geom_gc.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
geom_gc <- function(fa.file = NULL, bs.fa.seq = NULL, chr.split = "[[:space:]]", guide.line = NULL,
line.color = "black", guide.line.color = "red", guide.line.type = "dashed",
plot.space = 0.1, plot.height = 0.2) {
structure(list(
fa.file = fa.file, bs.fa.seq = bs.fa.seq, chr.split = chr.split, guide.line = guide.line,
line.color = line.color, guide.line.color = guide.line.color, guide.line.type = guide.line.type,
plot.space = plot.space, plot.height = plot.height
),
class = "gc"
structure(
list(
fa.file = fa.file, bs.fa.seq = bs.fa.seq, chr.split = chr.split, guide.line = guide.line,
line.color = line.color, guide.line.color = guide.line.color, guide.line.type = guide.line.type,
plot.space = plot.space, plot.height = plot.height
),
class = "gc"
)
}

Expand Down
6 changes: 4 additions & 2 deletions R/geom_gene.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ geom_gene <- function(gtf.gr,
arrow.gap = NULL,
arrow.num = 50,
color.by = "strand",
fill.color = c("-" = "cornflowerblue",
"+" = "darkolivegreen3"),
fill.color = c(
"-" = "cornflowerblue",
"+" = "darkolivegreen3"
),
show.utr = FALSE,
label.size = 3,
label.vjust = 2,
Expand Down
18 changes: 9 additions & 9 deletions R/geom_ideogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ geom_ideogram <- function(genome = "hg19", mark.color = "red", mark.alpha = 0.7,
highlight.centromere = FALSE, highlight.color = "green", highlight.alpha = 0.7, highlight.line.size = 1,
highlight.shadow.color = "black", highlight.shadow.alpha = 0.7, highlight.shadow.line.size = 1,
plot.space = 0.1, plot.height = 0.1) {

# test if suggested package is installed
requireNamespace("ggbio", quietly = TRUE)

structure(list(
genome = genome, mark.color = mark.color, mark.alpha = mark.alpha, mark.line.size = mark.line.size,
add.shadow = add.shadow, shadow.color = shadow.color, shadow.alpha = shadow.alpha, shadow.line.size = shadow.line.size,
highlight.centromere = highlight.centromere, highlight.color = highlight.color, highlight.alpha = highlight.alpha,
highlight.line.size = highlight.line.size, highlight.shadow.color = highlight.shadow.color, highlight.shadow.alpha = highlight.shadow.alpha,
highlight.shadow.line.size = highlight.shadow.line.size, plot.space = plot.space, plot.height = plot.height
),
class = "ideogram"
structure(
list(
genome = genome, mark.color = mark.color, mark.alpha = mark.alpha, mark.line.size = mark.line.size,
add.shadow = add.shadow, shadow.color = shadow.color, shadow.alpha = shadow.alpha, shadow.line.size = shadow.line.size,
highlight.centromere = highlight.centromere, highlight.color = highlight.color, highlight.alpha = highlight.alpha,
highlight.line.size = highlight.line.size, highlight.shadow.color = highlight.shadow.color, highlight.shadow.alpha = highlight.shadow.alpha,
highlight.shadow.line.size = highlight.shadow.line.size, plot.space = plot.space, plot.height = plot.height
),
class = "ideogram"
)
}

Expand Down
Loading

0 comments on commit c2a2001

Please sign in to comment.