-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicasso.R
136 lines (120 loc) · 4.49 KB
/
picasso.R
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
#################################
# @ AIM:The launcher of picasso
# @ DATE:2023.04.16
# @ AUTHOR:C, J; L, LH
#################################
# 1 core packages
libraries <- c("tidyverse", "rlang", "magrittr")
lapply(
libraries,
function(x) {
suppressPackageStartupMessages(library(x, character.only = TRUE))
}
)
# 2 set important variable
picasso_path <- getwd()
black_list <- c("\\.git", "\\.vscode", "picture", "knowledge_base", "utils")
# 3 file management system functions:
# @load_necessary
# @choose_pipeline
# @list_pipeline
# @load_script
# load necessary
load_necessary <- function() {
## basical utils
load_script(dir = "utils", script = "utils_log")
load_script(dir = "utils", script = "utils_process")
load_script(dir = "utils", script = "utils_api")
load_script(dir = "utils", script = "utils_parallel")
# load_script(dir = "utils", script = "utils_package")
# load_script(dir = "utils/input_your_parameter", script = "utils_parameter")
## color
# load_script(dir='visualization/colour',script='palette')
## picture
# load_script(dir='visualization/plot',script='themes')
}
# choose the pipeline
choose_pipeline <- function(pipeline = NULL,
module = NULL) {
# If the pipeline name is not specified, list available pipelines
if (is.null(pipeline)) {
list_pipeline()
pipeline <- readline(prompt = "Please specify a pipeline and press 'Enter'\n")
}
# Load all necessary packages and functions
load_necessary()
# Load scripts for the selected pipeline
lapply(pipeline, function(p) {
list.files(path = picasso_path, recursive = F, full = T) %>%
lapply(., function(ls) {
pipe <- list.files(path = ls, recursive = F, full = T) %>%
grep(., pattern = p, value = T)
if (!is.null(module)) {
pipe <- lapply(module, function(m) {
list.dirs(path = pipe, recursive = F, full = T) %>%
grep(., pattern = m, value = T)
})
}
pipe
})
}) %>%
unlist(., recursive = T) %>%
stringr::str_split(., pattern = "PICASSO/", simplify = T, n = 2) %>%
.[, 2] %T>%
cat(paste0("is successfully loaded.", collapse = "\n"), "\n") %>%
lapply(., function(rs) {
rs %>% load_script(dir = .)
})
invisible(NULL)
}
# list all pipelines
list_pipeline <- function(pipeline = NULL, module = F) {
# Get all of the pipelines
dir_1 <- list.dirs(path = picasso_path, recursive = F)
dir_1 <- dir_1[!grepl(dir_1, pattern = paste(black_list, collapse = "|"))]
for (d in dir_1) {
# Get the name of the total class
total_class <- stringr::str_split(d, pattern = "PICASSO/", simplify = T, n = 2)[, 2]
cat("\n#----", total_class, "----#\n")
# Get all of the pipelines within the total class
dir_2 <- list.dirs(path = d, recursive = F)
for (p in dir_2) { # pipeline
# Get the name of the pipeline
pipe_exist <- stringr::str_split(p, pattern = paste0(total_class, "/"), simplify = T, n = 2)[, 2]
if (is.null(pipe_exist)) {
next
}
# If the user specifies a pipeline, only include it in the list
if (!is.null(pipeline)) {
pipe_exist_bool <- grepl(pipe_exist, pattern = pipeline)
if (!pipe_exist_bool) {
next
}
}
cat("-->", pipe_exist, "\n")
# Get all of the modules within the pipeline
dir_3 <- list.dirs(path = grep(p, pattern = pipe_exist, value = T), recursive = F)
if (module == T) {
for (m in dir_3) { # module
# Get the name of the module
modules <- stringr::str_split(m, pattern = paste0(pipe_exist, "/"), simplify = T, n = 2)[, 2]
if (is.null(modules)) {
next
}
cat("--> -->", modules, "\n")
}
}
}
}
}
# load the specified script
load_script <- function(dir, script = "(\\.r)|(\\.R)$") {
list.files(
path = paste0(picasso_path, "/", dir),
pattern = paste(script, collapse = "|"),
recursive = T, full = T
) %>%
grep(., pattern = "(\\.r)|(\\.R)$", value = T) %>%
lapply(., source)
invisible(NULL)
}