Skip to content

Commit 95405c9

Browse files
committed
Change rowMeans in dictionary for eigenvector calculation
Adapt prepare multideconv folds with dictionary addition for pipeML
1 parent e82aa1f commit 95405c9

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

R/cell_deconvolution.R

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,9 @@ prepare_multideconv_folds <- function(data, folds, cells_extra = NULL) {
25182518
return = FALSE
25192519
)
25202520

2521+
## Deconvolution dictionary
2522+
deconv_subgroups = deconvolution_dictionary(deconv_subgroups)
2523+
25212524
train_cell_data <- deconv_subgroups[[1]] %>%
25222525
dplyr::mutate(target = obs_train)
25232526

@@ -2600,7 +2603,7 @@ prepare_multideconv_folds <- function(data, folds, cells_extra = NULL) {
26002603
#' - The clustering is performed globally across all pathways, ensuring consistent
26012604
#' interpretation of clusters across all cell types.
26022605
#' - Each deconvolution feature is assigned to the pathway cluster with the
2603-
#' highest mean correlation score.
2606+
#' highest eigenvector score.
26042607
#'
26052608
#' @seealso
26062609
#' \code{\link[CellTFusion]{compute.modules.relationship}},
@@ -2639,13 +2642,16 @@ deconvolution_dictionary = function(deconv_subgroups, pathway_matrix){
26392642
clusters_global <- split(names(clusters_global), clusters_global)
26402643
names(clusters_global) <- paste0("Cluster_", seq_along(clusters_global))
26412644

2642-
# Calculate mean correlation for each cluster dynamically
2645+
# Calculate eigenvector-based score (PC1) for each cluster
26432646
corr_matrix_global <- data.frame(global_x[[1]])
26442647
for (k in seq_along(clusters_global)) {
26452648
cluster_name <- names(clusters_global)[k]
2646-
corr_matrix_global[[paste0(cluster_name, "_Score")]] <- rowMeans(
2647-
corr_matrix_global[, clusters_global[[k]], drop = FALSE], na.rm = TRUE
2648-
)
2649+
sub_mat <- corr_matrix_global[, clusters_global[[k]], drop = FALSE]
2650+
2651+
# Compute eigenvector (PC1) direction per feature (deconv row)
2652+
pca_res <- prcomp(sub_mat, center = TRUE, scale. = TRUE)
2653+
pc1_scores <- pca_res$x[, 1] # first principal component
2654+
corr_matrix_global[[paste0(cluster_name, "_Score")]] <- pc1_scores
26492655
}
26502656

26512657
# Classify features based on the highest mean correlation across all clusters
@@ -2664,12 +2670,14 @@ deconvolution_dictionary = function(deconv_subgroups, pathway_matrix){
26642670
x <- CellTFusion::compute.modules.relationship(cell_subgroups[[cell]], pathway_matrix, return = TRUE, plot = FALSE)
26652671
corr_matrix <- data.frame(x[[1]])
26662672

2667-
#Calculate mean correlation for each cluster dynamically
26682673
for (k in seq_along(clusters_global)) {
26692674
cluster_name <- names(clusters_global)[k]
2670-
corr_matrix[[paste0(cluster_name, "_Score")]] <- rowMeans(
2671-
corr_matrix[, clusters_global[[k]], drop = FALSE], na.rm = TRUE
2672-
)
2675+
sub_mat <- corr_matrix[, clusters_global[[k]], drop = FALSE]
2676+
2677+
# Compute eigenvector-based (PC1) score per cluster
2678+
pca_res <- prcomp(sub_mat, center = TRUE, scale. = TRUE)
2679+
pc1_scores <- pca_res$x[, 1]
2680+
corr_matrix[[paste0(cluster_name, "_Score")]] <- pc1_scores
26732681
}
26742682

26752683
#Identify which cluster each feature belongs to based on the highest mean score
@@ -2706,3 +2714,4 @@ deconvolution_dictionary = function(deconv_subgroups, pathway_matrix){
27062714

27072715
return(deconv_subgroups)
27082716
}
2717+

0 commit comments

Comments
 (0)