Skip to content

Commit 6885377

Browse files
authored
Merge pull request #67 from MangiolaLaboratory/tar_factories
update empty droplets
2 parents 19cc35c + 164f265 commit 6885377

14 files changed

+1105
-509
lines changed

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export(create_pseudobulk)
2121
export(doublet_identification)
2222
export(empty_droplet_id)
2323
export(evaluate_hpc)
24+
export(expand_tiered_arguments)
2425
export(find_variable_genes)
2526
export(get_positions)
2627
export(get_unique_tissues)
@@ -34,6 +35,7 @@ export(map_test_differential_abundance)
3435
export(non_batch_variation_removal)
3536
export(normalise_abundance_seurat_SCT)
3637
export(preprocessing_output)
38+
export(preprocessing_output_factory)
3739
export(pseudobulk_merge)
3840
export(read_data_container)
3941
export(reference_label_coarse_id)
@@ -176,14 +178,17 @@ importFrom(readr,write_lines)
176178
importFrom(reshape2,melt)
177179
importFrom(rlang,enquo)
178180
importFrom(rlang,is_symbolic)
181+
importFrom(rlang,parse_expr)
179182
importFrom(rlang,quo_is_symbolic)
180183
importFrom(rlang,quo_name)
181184
importFrom(scales,rescale)
182185
importFrom(scales,viridis_pal)
183186
importFrom(scater,isOutlier)
184187
importFrom(scuttle,logNormCounts)
185188
importFrom(scuttle,perCellQCMetrics)
189+
importFrom(stats,as.formula)
186190
importFrom(stringr,str_detect)
191+
importFrom(stringr,str_extract)
187192
importFrom(stringr,str_remove)
188193
importFrom(stringr,str_remove_all)
189194
importFrom(stringr,str_replace)

R/data.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' Dummy HPC Dataset
2+
#'
3+
#' This dataset, named `dummy_hpc`, is a synthetic example dataset used for demonstrating
4+
#' high-performance computing (HPC) data processing and analysis techniques.
5+
#'
6+
#' @format A data frame with multiple rows and columns representing synthetic HPC data. Each column represents a different variable in the dataset.
7+
#'
8+
#' @details
9+
#' The `dummy_hpc` dataset is created for educational and demonstration purposes. It includes
10+
#' simulated data points that resemble typical HPC workload metrics. The dataset can be used to
11+
#' showcase various data processing, analysis, and visualisation techniques in the context of HPC.
12+
#'
13+
#' @usage
14+
#' data(dummy_hpc)
15+
#'
16+
#' @examples
17+
#' # Load the dataset
18+
#' data(dummy_hpc)
19+
#'
20+
#' # Display the first few rows of the dataset
21+
#' head(dummy_hpc)
22+
#'
23+
#' # Example analysis: summary statistics
24+
#' summary(dummy_hpc)
25+
#'
26+
#' @keywords datasets
27+
#'
28+
#' @noRd
29+
#'
30+
"dummy_hpc"

R/data_raw.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# save(dummy_hpc, file = "data/dummy_hpc.rda", compress = "xz")

R/factories.R

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#' Parse a Function Call String
2+
#'
3+
#' This function takes a string representing a function call and parses it into
4+
#' its constituent parts: the function name and the argument names.
5+
#'
6+
#' @param input_string A character string representing a function call, e.g., "report(empty_droplets_tbl, arg1)".
7+
#'
8+
#' @return A list containing two elements:
9+
#' \item{function_name}{A character string representing the name of the function.}
10+
#' \item{arguments}{A character vector containing the names of the arguments.}
11+
#'
12+
#' @importFrom rlang parse_expr
13+
#'
14+
#' @examples
15+
#' # Example usage:
16+
#' command <- quote(report(empty_droplets_tbl, arg1))
17+
#' output <- parse_function_call(command)
18+
#' print(output)
19+
#'
20+
#' @noRd
21+
parse_function_call <- function(command) {
22+
# Parse the input string to an expression
23+
expr <- command |> deparse() |> parse_expr()
24+
25+
# Extract the function name
26+
function_name <- as.character(expr[[1]])
27+
28+
# Extract the arguments
29+
args <- as.list(expr[-1])
30+
31+
# Convert arguments to character vector
32+
args <- sapply(args, as.character)
33+
34+
# Create the output list
35+
result <- list(function_name = function_name, arguments = args)
36+
37+
return(result)
38+
}
39+
40+
#' Parse a Function Call String and Expand Tiered Arguments
41+
#'
42+
#' This function takes a string representing a function call, parses it into
43+
#' its constituent parts (function name and arguments), and expands the specified
44+
#' tiered arguments based on the provided tier labels.
45+
#'
46+
#' @param command A character string representing a function call, e.g., "report(empty_droplets_tbl, arg1)".
47+
#' @param tiers A character vector indicating the tier labels for the specified tiered arguments, e.g., c("_1", "_2", "_3").
48+
#' @param tiered_args A character vector specifying which arguments should be tiered, e.g., c("empty_droplets_tbl").
49+
#'
50+
#' @return A character string representing the modified function call with tiered arguments expanded.
51+
#'
52+
#' @importFrom rlang parse_expr
53+
#'
54+
#' @examples
55+
#' # Example usage:
56+
#' input_string <- "report(empty_droplets_tbl, arg1)"
57+
#' tiers <- c("_1", "_2", "_3")
58+
#' tiered_args <- c("empty_droplets_tbl", "another_arg")
59+
#' output <- expand_tiered_arguments(input_string, tiers, tiered_args)
60+
#' print(output)
61+
#'
62+
#' @export
63+
expand_tiered_arguments <- function(command, tiers, tiered_args) {
64+
# Parse the input command to get function name and arguments
65+
parsed_call <- parse_function_call(command)
66+
67+
function_name <- parsed_call$function_name
68+
arguments <- parsed_call$arguments
69+
70+
# Expand tiered arguments
71+
expanded_arguments <- unlist(lapply(arguments, function(arg) {
72+
if (arg %in% tiered_args) {
73+
# Generate tiered arguments using provided tier labels
74+
return(paste0(arg, "_", tiers))
75+
} else {
76+
# Return the argument as is
77+
return(arg)
78+
}
79+
}))
80+
81+
# Construct the new function call string
82+
paste0(function_name, "(", paste(expanded_arguments, collapse = ", "), ")") |>
83+
rlang::parse_expr()
84+
85+
}
86+
87+
88+
89+
#' @importFrom stringr str_extract
90+
factory_split = function(name_output, command, tiers, arguments_to_tier = c(), other_arguments_to_tier = c() ){
91+
92+
if(command |> deparse() |> str_detect("%>%") |> any())
93+
stop("HPCell says: no \"%>%\" allowed in the command, please use \"|>\" ")
94+
95+
#input = command |> deparse() |> paste(collapse = "") |> str_extract("[a-zA-Z0-9_]+\\(([a-zA-Z0-9_]+),.*", group=1)
96+
97+
# Filter out arguments to be tiered from the input command
98+
other_arguments_to_tier <- other_arguments_to_tier |> str_subset(arguments_to_tier, negate = TRUE)
99+
100+
map2(tiers, names(tiers), ~ {
101+
102+
103+
# Pattern
104+
pattern = as.name("map")
105+
106+
if(arguments_to_tier |> length() > 0)
107+
pattern = pattern |> c(substitute(slice(input, index = arg ), list(input = as.symbol(arguments_to_tier), arg=.x)) )
108+
109+
if(other_arguments_to_tier |> length() > 0)
110+
pattern = pattern |> c(glue("{other_arguments_to_tier}_{.y}") |> lapply(as.name))
111+
112+
pattern = as.call(pattern)
113+
114+
115+
# Resources
116+
if(length(tiers) == 1)
117+
resources = targets::tar_option_get("resources")
118+
else
119+
resources = tar_resources(crew = tar_resources_crew(.y))
120+
121+
122+
tar_target_raw(
123+
glue("{name_output}_{.y}") |> as.character(),
124+
command |> add_tier_inputs(other_arguments_to_tier, .y),
125+
pattern = pattern,
126+
iteration = "list",
127+
resources = resources
128+
)
129+
})
130+
}
131+
132+
factory_collapse = function(name_output, command, tiered_input, tiers, ...){
133+
134+
command = command |> expand_tiered_arguments(names(tiers), tiered_input)
135+
136+
tar_target_raw(name_output, command, ...)
137+
}
138+
139+
# factory_tiering = function(preparation, tiering, collapsing, tiers){
140+
#
141+
# t1 = tar_target_raw(preparation[[1]], preparation[[2]])
142+
# t2 = factory_split(
143+
# tiering[[1]],
144+
# tiering[[2]],
145+
# tiers,
146+
# tiering[[3]]
147+
# )
148+
# t3 = factory_collapse(
149+
# collapsing[[1]],
150+
# collapsing[[2]],
151+
# collapsing[[3]],
152+
# tiers
153+
#
154+
# )
155+
#
156+
# list(t1, t2, t3)
157+
# }

R/functions.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ preprocessing_output <- function(input_read_RNA_assay,
878878
#' @export
879879

880880
# Create pseudobulk for each sample
881-
create_pseudobulk <- function(preprocessing_output_S, sample_names ,x ,...) {
881+
create_pseudobulk <- function(preprocessing_output_S, sample_names , x = c() ,...) {
882882
#Fix GChecks
883883
.sample = NULL
884884
.feature = NULL
@@ -900,7 +900,7 @@ create_pseudobulk <- function(preprocessing_output_S, sample_names ,x ,...) {
900900
mutate(sample_hpc = sample_names) |>
901901

902902
# Aggregate
903-
aggregate_cells(c(sample_hpc, !!x), slot = "data") |>
903+
aggregate_cells(c(sample_hpc, any_of(x)), slot = "data") |>
904904
as_SummarizedExperiment(.sample, .feature, any_of(c("RNA", "ADT"))) |>
905905
pivot_longer(cols = assays, names_to = "data_source", values_to = "count") |>
906906
filter(!count |> is.na()) |>

0 commit comments

Comments
 (0)