-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rprofile
More file actions
194 lines (160 loc) · 4.84 KB
/
Copy path.Rprofile
File metadata and controls
194 lines (160 loc) · 4.84 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
if (requireNamespace("logger", quietly = TRUE)) {
library(logger)
} else {
message("Package `logger` not available.")
}
## This makes sure that R loads the workflowr package
## automatically, everytime the project is loaded
# if (requireNamespace("workflowr", quietly = TRUE)) {
# message("Loading .Rprofile for the current workflowr project")
# library("workflowr")
# } else {
# message("workflowr package not installed, please run install.packages(\"workflowr\") to use the workflowr functions")
# }
options(
formatR.width = 79,
formatR.args.newline = TRUE,
formatR.brace.newline = FALSE,
formatR.indent = 2
)
# DISPLAY envvar set to xpra session
if (stringr::str_detect(Sys.info()["nodename"], "rkn")) {
Sys.setenv("DISPLAY" = ":103")
}
mkDir <- function(path) {
if (!dir.exists(here(path))) {
dir.create(here(path), recursive = TRUE)
}
}
load_utils <- function() {
util <- new.env()
source(here("analysis/99_util.R"), local = util)
print(ls.str(envir = util))
return(util)
}
session_info <- function() {
session <- sessionInfo()
saveRDS(session, here(str_glue("logs/R/{prefix}_session.rds")))
print(session)
}
save_versions <- function(...) {
packages <- list(...)
package_versions <- purrr::map(
packages,
(\(pkg) as.character(packageVersion(pkg)))
) |>
purrr::set_names(packages)
package_versions$R <- as.character(package_version(R.Version()))
yaml_path <- here::here(stringr::str_glue("logs/R/{prefix}_versions.yaml"))
yaml::write_yaml(package_versions, yaml_path)
log_info("[VERSIONS] Saved package versions to {yaml_path}")
}
saveplot <- function(plotObj, path = NULL, h = 7, w = 7, name = NULL, ...) {
objname <- ifelse(is.null(name), deparse(substitute(plotObj)), name)
if (is.null(path)) {
fileprefix <- here(str_glue("plots/{prefix}"))
mkDir(fileprefix)
filepath <- str_glue("{fileprefix}/{objname}.pdf")
} else {
filepath <- here(path)
}
if ("gg" %in% class(plotObj)) {
tryCatch(
{
ggplot2::ggsave(
filepath,
width = w,
height = h,
plot = plotObj,
...
)
message(stringr::str_glue("Saved {objname} to {filepath}"))
log_info("[PLOT]: Saved {objname} to {filepath}")
},
error = function(e) {
print(e)
log_warn("[PLOT]: {objname} might still be locked by git annex.")
}
)
} else if ("Heatmap" %in% class(plotObj)) {
tryCatch(
{
pdf(filepath,
w = w, h = h
)
draw(plotObj)
dev.off()
message(stringr::str_glue("Saved {objname} to {filepath}"))
log_info("[PLOT]: Saved {objname} to {filepath}")
},
error = function(e) {
print(e)
log_warn("[PLOT]: {objname} is probably still locked by git annex")
}
)
} else {
log_error("[PLOT] SOMETHING WENT WRONG?!?!")
}
}
saveobj <- function(obj, path = NULL) {
objname <- deparse(substitute(obj))
if (is.null(path)) {
fileprefix <- here(str_glue("results/R/{prefix}"))
mkDir(fileprefix)
filepath <- str_glue("{fileprefix}/{objname}.rds")
} else {
filepath <- here(path)
}
tryCatch(
{
saveRDS(obj, file = filepath)
message(stringr::str_glue("Saved {objname} to {filepath}"))
log_info("[OBJECT]: Saved {objname} to {filepath}")
},
error = function(e) {
log_warn("[OBJECT]: {objname} is probably still locked by git annex")
}
)
}
objsize <- function(obj) {
utils:::format.object_size(object.size(obj), "auto")
}
parseArguments <- function(arguments, description) {
parser <- ArgumentParser(description = description)
# Add Arugment groups
map(toupper(names(arguments)), function(x) parser$add_argument_group(x))
# Add arguments
for (group in arguments) {
map(group, function(x) do.call(parser$add_argument, x))
}
return(parser)
}
parseSnakefile <- function(rule) {
code <- paste(
"import snakemake",
"import os",
paste0("os.chdir('", here::here(), "')"),
"workflow = snakemake.Workflow(snakefile = 'workflow/Snakefile', rerun_triggers = 'mtime')",
"workflow.include('workflow/Snakefile')",
paste0("rule_obj = workflow.get_rule('", rule, "')"),
"input = {name: str(file) for name, file in rule_obj._input.items()}",
"output = {name: str(file) for name, file in rule_obj._output.items()}",
sep = "\n"
)
py <- reticulate::py_run_string(code)
snakemake <- list(
input = py$input,
output = py$output
)
snakemake
}
colours <- list(
studies = c(
"Barzadd" = "#999999", "Chiang" = "#E69F00",
"Dhiman" = "#56B4E9", "Hefzi" = "#009E73", "Kol" = "#F0E442",
"Orellana" = "#0072B2", "Tzani" = "#D55E00", "van Wijk" = "#CC79A7"
),
cellLines = c("CHO-S" = "red", "CHO-K1" = "black"),
producers = c("TRUE" = "orange", "FALSE" = "white"),
libraries = c("PolyA" = "#b9cd4f", "rRNA" = "#0191c1")
)